2019-12-26 14:45:42 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2016-09-23 17:31:51 -04:00
|
|
|
|
2018-09-05 04:55:55 -04:00
|
|
|
#include "MobileJSStructDeserializerBackend.h"
|
2018-06-26 08:42:47 -04:00
|
|
|
|
2018-09-05 04:55:55 -04:00
|
|
|
#if PLATFORM_ANDROID || PLATFORM_IOS
|
2018-06-26 08:42:47 -04:00
|
|
|
|
2018-09-05 04:55:55 -04:00
|
|
|
#include "MobileJSScripting.h"
|
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
|
|
|
#include "UObject/UnrealType.h"
|
|
|
|
|
#include "Templates/Casts.h"
|
2016-09-23 17:31:51 -04:00
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
// @todo: this function is copied from CEFJSStructDeserializerBackend.cpp. Move shared utility code to a common header file
|
|
|
|
|
/**
|
|
|
|
|
* Sets the value of the given property.
|
|
|
|
|
*
|
|
|
|
|
* @param Property The property to set.
|
|
|
|
|
* @param Outer The property that contains the property to be set, if any.
|
|
|
|
|
* @param Data A pointer to the memory holding the property's data.
|
|
|
|
|
* @param ArrayIndex The index of the element to set (if the property is an array).
|
|
|
|
|
* @return true on success, false otherwise.
|
|
|
|
|
* @see ClearPropertyValue
|
|
|
|
|
*/
|
2019-12-13 11:07:03 -05:00
|
|
|
template<typename FPropertyType, typename PropertyType>
|
|
|
|
|
bool SetPropertyValue( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, const PropertyType& Value )
|
2016-09-23 17:31:51 -04:00
|
|
|
{
|
|
|
|
|
PropertyType* ValuePtr = nullptr;
|
2019-12-13 11:07:03 -05:00
|
|
|
FArrayProperty* ArrayProperty = CastField<FArrayProperty>(Outer);
|
2016-09-23 17:31:51 -04:00
|
|
|
|
|
|
|
|
if (ArrayProperty != nullptr)
|
|
|
|
|
{
|
|
|
|
|
if (ArrayProperty->Inner != Property)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->template ContainerPtrToValuePtr<void>(Data));
|
|
|
|
|
int32 Index = ArrayHelper.AddValue();
|
|
|
|
|
|
|
|
|
|
ValuePtr = (PropertyType*)ArrayHelper.GetRawPtr(Index);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-13 11:07:03 -05:00
|
|
|
FPropertyType* TypedProperty = CastField<FPropertyType>(Property);
|
2016-09-23 17:31:51 -04:00
|
|
|
|
|
|
|
|
if (TypedProperty == nullptr || ArrayIndex >= TypedProperty->ArrayDim)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ValuePtr = TypedProperty->template ContainerPtrToValuePtr<PropertyType>(Data, ArrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ValuePtr == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*ValuePtr = Value;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 11:07:03 -05:00
|
|
|
bool FMobileJSStructDeserializerBackend::ReadProperty( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex )
|
2016-09-23 17:31:51 -04:00
|
|
|
{
|
|
|
|
|
switch (GetLastNotation())
|
|
|
|
|
{
|
|
|
|
|
case EJsonNotation::String:
|
|
|
|
|
{
|
2019-12-13 11:07:03 -05:00
|
|
|
if (Property->IsA<FStructProperty>())
|
2016-09-23 17:31:51 -04:00
|
|
|
{
|
2019-12-13 11:07:03 -05:00
|
|
|
FStructProperty* StructProperty = CastField<FStructProperty>(Property);
|
2016-09-23 17:31:51 -04:00
|
|
|
|
|
|
|
|
if ( StructProperty->Struct == FWebJSFunction::StaticStruct())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
FGuid CallbackID;
|
|
|
|
|
if (!FGuid::Parse(GetReader()->GetValueAsString(), CallbackID))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FWebJSFunction CallbackObject(Scripting, CallbackID);
|
2019-12-13 11:07:03 -05:00
|
|
|
return SetPropertyValue<FStructProperty, FWebJSFunction>(Property, Outer, Data, ArrayIndex, CallbackObject);
|
2016-09-23 17:31:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we reach this, default to parent class behavior
|
|
|
|
|
return FJsonStructDeserializerBackend::ReadProperty(Property, Outer, Data, ArrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 04:55:55 -04:00
|
|
|
FMobileJSStructDeserializerBackend::FMobileJSStructDeserializerBackend(FMobileJSScriptingRef InScripting, const FString& JsonString)
|
2018-09-06 14:36:13 -04:00
|
|
|
: FJsonStructDeserializerBackend(Reader)
|
|
|
|
|
, Scripting(InScripting)
|
2016-09-23 17:31:51 -04:00
|
|
|
, JsonData()
|
|
|
|
|
, Reader(JsonData)
|
|
|
|
|
{
|
Allow FString instances containing code units outside of the basic multilingual plane to be losslessly processed regardless of whether TCHAR is 2 or 4 bytes.
Most UE4 platforms use a 2-byte TCHAR, however some still use a 4-byte TCHAR. The platforms that use a 4-byte TCHAR expect their string data to be UTF-32, however there are parts of UE4 that serialize FString data as a series of UCS2CHAR, simply narrowing or widening each TCHAR in turn. This can result in invalid or corrupted UTF-32 strings (either UTF-32 strings containing UTF-16 surrogates, or UTF-32 code points that have been truncated to 2-bytes), which leads to either odd behavior or crashes.
This change updates the parts of UE4 that process FString data as a series of 2-byte values to do so on the correct UTF-16 interpretation of the data, converting to/from UTF-32 as required on platforms that use a 4-byte TCHAR. This conversion is a no-op on platforms that use a 2-byte TCHAR as the string is already assumed to be valid UTF-16 data. It should also be noted that while FString may contain UTF-16 code units on platforms using a 2-byte TCHAR, this change doesn't do anything to make FString represent a Unicode string on those platforms (ie, a string that understands and works on code points), but is rather just a bag of code units.
Two new variable-width string converters have be added to facilitate the conversion (modelled after the TCHAR<->UTF-8 converters), TUTF16ToUTF32_Convert and TUTF32ToUTF16_Convert. These are used for both TCHAR<->UTF16CHAR conversion when needed, but also for TCHAR<->wchar_t conversion on platforms that use char16_t for TCHAR along with having a 4-byte wchar_t (as defined by the new PLATFORM_WCHAR_IS_4_BYTES option).
These conversion routines are accessed either via the conversion macros (TCHAR_TO_UTF16, UTF16_TO_TCHAR, TCHAR_TO_WCHAR, and WCHAR_TO_TCHAR), or by using a conversion struct (FTCHARToUTF16, FUTF16ToTCHAR, FTCHARToWChar, and FWCharToTCHAR), which is the same pattern as the existing TCHAR<->UTF-8 conversion. Both the macros and the structs are defined as no-ops when the conversion isn't needed, but always exist so that code can be written in a portable way.
Very little code actually needed updating to use UTF-16, as the vast majority makes no assumptions about the size of TCHAR, nor how FString should be serialized. The main places were the FString archive serialization and the JSON reader/writer, along with some minor fixes to the UTF-8 conversion logic for platforms using a 4-byte TCHAR.
Tests have been added to verify that an FString representing a UTF-32 code point can be losslessly converted to/from UTF-8 and UTF-16, and serialized to/from an archive.
#jira
#rb Steve.Robb, Josh.Adams
#ROBOMERGE-SOURCE: CL 8676728 via CL 8687863
#ROBOMERGE-BOT: (v421-8677696)
[CL 8688048 by jamie dale in Main branch]
2019-09-16 05:44:11 -04:00
|
|
|
// Note: This is a no-op on platforms that are using a 16-bit TCHAR
|
|
|
|
|
FTCHARToUTF16 UTF16String(*JsonString, JsonString.Len());
|
|
|
|
|
|
|
|
|
|
JsonData.Append((uint8*)UTF16String.Get(), UTF16String.Length() * sizeof(UTF16CHAR));
|
2016-09-23 17:31:51 -04:00
|
|
|
}
|
2018-06-26 08:42:47 -04:00
|
|
|
|
2018-09-05 04:55:55 -04:00
|
|
|
#endif // PLATFORM_ANDROID || PLATFORM_IOS
|