Files
UnrealEngineUWP/Engine/Source/Runtime/Serialization/Private/StructDeserializer.cpp

1064 lines
35 KiB
C++
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "StructDeserializer.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 "IStructDeserializerBackend.h"
#include "UObject/PropertyPortFlags.h"
/* Internal helpers
*****************************************************************************/
namespace StructDeserializer
{
/**
* Structure for the read state stack.
*/
struct FReadState
{
/** Holds the property's current array index. */
int32 ArrayIndex = 0;
/** Holds a pointer to the property's data. */
void* Data = nullptr;
/** Holds the property's meta data. */
FProperty* Property = nullptr;
/** Holds a pointer to the UStruct describing the data. */
UStruct* TypeInfo = nullptr;
};
/**
* Finds the class for the given stack state.
*
* @param State The stack state to find the class for.
* @return The class, if found.
*/
UStruct* FindClass( const FReadState& State )
{
UStruct* Class = nullptr;
if (State.Property != nullptr)
{
FProperty* ParentProperty = State.Property;
if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(ParentProperty))
{
ParentProperty = ArrayProperty->Inner;
}
FStructProperty* StructProperty = CastField<FStructProperty>(ParentProperty);
FObjectPropertyBase* ObjectProperty = CastField<FObjectPropertyBase>(ParentProperty);
if (StructProperty != nullptr)
{
Class = StructProperty->Struct;
}
else if (ObjectProperty != nullptr)
{
Class = ObjectProperty->PropertyClass;
}
}
else
{
UObject* RootObject = static_cast<UObject*>(State.Data);
Class = RootObject->GetClass();
}
return Class;
}
}
/* FStructDeserializer static interface
*****************************************************************************/
bool FStructDeserializer::Deserialize( void* OutStruct, UStruct& TypeInfo, IStructDeserializerBackend& Backend, const FStructDeserializerPolicies& Policies )
{
using namespace StructDeserializer;
check(OutStruct != nullptr);
// initialize deserialization
FReadState CurrentState;
{
CurrentState.ArrayIndex = 0;
CurrentState.Data = OutStruct;
CurrentState.Property = nullptr;
CurrentState.TypeInfo = &TypeInfo;
}
TArray<FReadState> StateStack;
EStructDeserializerBackendTokens Token;
// process state stack
while (Backend.GetNextToken(Token))
{
FString PropertyName = Backend.GetCurrentPropertyName();
switch (Token)
{
case EStructDeserializerBackendTokens::ArrayEnd:
{
// rehash the set now that we are done with it
if (FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property))
{
FScriptSetHelper SetHelper(SetProperty, CurrentState.Data);
SetHelper.Rehash();
}
if (StateStack.Num() == 0)
{
UE_LOG(LogSerialization, Verbose, TEXT("Malformed input: Found ArrayEnd without matching ArrayStart"));
return false;
}
CurrentState = StateStack.Pop(/*bAllowShrinking*/ false);
}
break;
case EStructDeserializerBackendTokens::ArrayStart:
{
FReadState NewState;
NewState.Property = FindFProperty<FProperty>(CurrentState.TypeInfo, *PropertyName);
if (NewState.Property != nullptr)
{
if (Policies.PropertyFilter && !Policies.PropertyFilter(NewState.Property, CurrentState.Property))
{
Backend.SkipArray();
continue;
}
// handle set property
if (FSetProperty* SetProperty = CastField<FSetProperty>(NewState.Property))
{
NewState.Data = SetProperty->ContainerPtrToValuePtr<void>(CurrentState.Data, CurrentState.ArrayIndex);
FScriptSetHelper SetHelper(SetProperty, NewState.Data);
SetHelper.EmptyElements();
}
// handle array property
else if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(NewState.Property))
{
// fast path for byte array
if (Backend.ReadPODArray(ArrayProperty, CurrentState.Data))
{
// read the entire array, move to the next property
continue;
}
// failed to read as a pod array, read as regular array iterating on each property
else
{
NewState.Data = CurrentState.Data;
}
}
// handle static array
else
{
NewState.Data = CurrentState.Data;
}
NewState.TypeInfo = FindClass(NewState);
StateStack.Push(CurrentState);
CurrentState = NewState;
}
else
{
// error: array property not found
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("The array property '%s' does not exist"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
Backend.SkipArray();
}
}
break;
case EStructDeserializerBackendTokens::Error:
{
return false;
}
case EStructDeserializerBackendTokens::Property:
{
// Set are serialized as array, so no property name will be set for each entry
if (PropertyName.IsEmpty() && (CurrentState.Property != nullptr) && (CurrentState.Property->GetClass() == FSetProperty::StaticClass()))
{
// handle set element
FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property);
FScriptSetHelper SetHelper(SetProperty, CurrentState.Data);
FProperty* Property = SetProperty->ElementProp;
const int32 ElementIndex = SetHelper.AddDefaultValue_Invalid_NeedsRehash();
uint8* ElementPtr = SetHelper.GetElementPtr(ElementIndex);
if (!Backend.ReadProperty(Property, CurrentState.Property, ElementPtr, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("An item in Set '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
}
// Otherwise we are dealing with dynamic or static array
else if (PropertyName.IsEmpty())
{
// handle array element
FArrayProperty* ArrayProperty = CastField<FArrayProperty>(CurrentState.Property);
FProperty* Property = nullptr;
if (ArrayProperty != nullptr)
{
// dynamic array element
Property = ArrayProperty->Inner;
}
else
{
// static array element
Property = CurrentState.Property;
}
if (Property == nullptr)
{
// error: no meta data for array element
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("Failed to serialize array element %i"), CurrentState.ArrayIndex);
}
return false;
}
else if (!Backend.ReadProperty(Property, CurrentState.Property, CurrentState.Data, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("The array element '%s[%i]' could not be read (%s)"), *PropertyName, CurrentState.ArrayIndex, *Backend.GetDebugString());
}
++CurrentState.ArrayIndex;
}
else if ((CurrentState.Property != nullptr) && (CurrentState.Property->GetClass() == FMapProperty::StaticClass()))
{
// handle map element
FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property);
Copying //UE4/Dev-Sequencer to //UE4/Dev-Main (Source: //UE4/Dev-Sequencer @ 3148965) #lockdown Nick.Penwarden #rb none ========================== MAJOR FEATURES + CHANGES ========================== Change 2883376 on 2016/02/26 by Max.Chen Sequencer: Refactored track instance API to better deal with invalid object bindings (fixes UE-27286) Change 3117044 on 2016/09/07 by Max.Chen Cine Camera: Add GetCineCameraComponent function from Cine Camera Actor. #jira UE-34036 Change 3117127 on 2016/09/07 by Max.Preussner MediaAssets: File media source path improvements Change 3117128 on 2016/09/07 by Max.Preussner PS4Media: Copied memory allocator fixes (CL# 3114158) Change 3117142 on 2016/09/07 by Max.Preussner MediaPlayerEditor: Normalizing paths of drag & drop media files Change 3117143 on 2016/09/07 by Max.Preussner Media: Made media player name accessible via IMediaPlayer Change 3117161 on 2016/09/07 by Max.Preussner PS4Media: Fixed CPU/GPU may crash due to race condition in destructor (UE-35696) Copied from Release-4.13 CL# 3117159 Change 3117184 on 2016/09/08 by Max.Chen Sequencer: Update sequencer selection on undo so that the sequencer selection stays in sync with the scene selection. Clear cached set of spawned objects in the spawn register only for spawned objects, rather than completely. This fixes an issue where deleting a selected spawnable and then undoing doesn't restore the spawnable as selected because the cached spawned objects gets cleared wholesale. #jira UE-27683 Change 3117831 on 2016/09/08 by Max.Chen Sequencer: Add option to create sub sequences for each master sequence shot. #jira UE-35378 Change 3118467 on 2016/09/08 by Max.Preussner Slate: ScrollyZoomy documentation cleanup pass Change 3118468 on 2016/09/08 by Max.Preussner MediaPlayerEditor: Added OriginalSize viewport mode (UE-35560) #jira UE-35560 Change 3118700 on 2016/09/08 by Max.Preussner Media: Removed still image tracks and sinks (UE-35767) #jira UE-35767 Change 3118987 on 2016/09/09 by Max.Chen Sequencer: Initialize player on post initialize components of level sequence actor. This is a speculative fix for GetSequencePlayer not initialized in Actor::BeginPlay before a Begin Play event in a level blueprint is invoked. #jira UE-34439 Change 3119896 on 2016/09/09 by Max.Preussner MediaAssets: Logging URL when failing to validate media source Change 3119921 on 2016/09/09 by Max.Preussner MediaAssets: Verbose logging sink shutdown in media texture Change 3120173 on 2016/09/09 by Max.Preussner WmfMedia: Refactored playback topology handling to support multiple tracks & track switching #jira UE-35383 #jira UE-35385 #jira UE-32582 Change 3120587 on 2016/09/11 by Max.Chen Fbx Export: Fix double transforms on an exported mesh. Added an option to map the skeletal motion to the root bone. In General Settings (Miscellaneous). #jira UE-35174 Change 3120685 on 2016/09/11 by Max.Chen Sequencer: Subtitles #jira UE-35824 Change 3121957 on 2016/09/12 by Max.Preussner MediaAssets: Replaced legacy texture sink shutdown code to fix race condition Change 3122113 on 2016/09/12 by Max.Preussner Media: Renamed Script track type to Text Change 3122386 on 2016/09/13 by Max.Chen Sequencer: Render movies with handles #jira UETOOL-733 Change 3124278 on 2016/09/14 by Max.Chen Sequencer: Add nullptr check for camera anim. #jira UE-35911 Change 3127211 on 2016/09/15 by Max.Preussner MediaAssets: Implemented Per-platform media player overrides in MediaAsset derived classes (UE-35478) #jira UE-35478 Change 3127536 on 2016/09/15 by Max.Preussner MediaAssets: Renamed platform player overrides property #jira UE-35478 Change 3127539 on 2016/09/15 by Max.Preussner MediaPlayerEditor: Implemented platform player overrides details customization for media assets (UE-35478) #jira UE-35478 Change 3127614 on 2016/09/15 by Max.Preussner MediaAssets: Fixed MediaSource serialization (UE-35478) #jira UE-35478 Change 3127617 on 2016/09/15 by Max.Preussner MediaAssets: Implemented PlatformMediaSource (UE-35387) #jira UE-35387 Change 3127626 on 2016/09/15 by Max.Preussner MediaAssets: Started to implement PlatformMediaSource customization (UE-35387) #jira UE-35387 Change 3128686 on 2016/09/16 by Max.Preussner WmfMedia: Added QuickTime to known video sub types Change 3128703 on 2016/09/16 by Max.Preussner WmfMedia: Fixed GuidToString printing incorrect byte sequence Change 3128705 on 2016/09/16 by Max.Preussner Core: Slightly more complicated unit test for TripleBuffer Change 3129281 on 2016/09/16 by Max.Preussner MediaPlayerEditor: Finished customization for PlatformMediaSource (UE-35387) #jira UE-35387 Change 3129291 on 2016/09/16 by Max.Preussner MediaAssets: Added verbose logging for dropped video frames Change 3130495 on 2016/09/19 by Max.Preussner PropertyEditor: Added missing forward declarations; code and documentation cleanup pass. Change 3131531 on 2016/09/19 by Max.Preussner Core: Accepting comma in milliseconds separator when parsing FTimespan Change 3131533 on 2016/09/19 by Max.Preussner Media: Started to implement subtitle support Change 3132468 on 2016/09/20 by Max.Preussner Core: Fixed TMap deserialization in struct serializer & updated unit tests Change 3132846 on 2016/09/20 by Max.Preussner SlateRemoteServer: Fixed Editor freezes and leaks memory when slate remote enabled (UE-35907) #jira UE-35907 Change 3136577 on 2016/09/22 by Frank.Fella Sequencer - Always use a unique name when creating dynamic material instances for animation to prevent reuse and resource issues. Change 3136661 on 2016/09/22 by Max.Preussner WmfMedia: Fixed memory leak while playing videos (UE-36289) #jira UE-36289 Change 3137035 on 2016/09/22 by Cody.Albert Changed FMovieScene3DTransformTrackInstance::Update to update ComponentVelocity Change 3137155 on 2016/09/22 by Max.Preussner MediaAssets: Added OpenFile method to MediaPlayer. Change 3138413 on 2016/09/23 by Cody.Albert Fixed ComponentVelocity to use UpdateData instead of FApp::GetDeltaTime() Change 3138627 on 2016/09/23 by Max.Preussner WmfMedia: Fixed FourCC types printing in reverse order Change 3139020 on 2016/09/23 by Max.Preussner MediaAssets: Fixed Crash when after playing media that requires a different conversion shader (UE-36393) #jira UE-36393 Change 3139028 on 2016/09/23 by Max.Preussner MediaPlayerEditor: Trimming leading & trailing whitespace in URL text box Change 3139046 on 2016/09/23 by Max.Preussner MediaPlayerEditor: Implemented statistics tab #jira UE-35395 Change 3139072 on 2016/09/23 by Max.Preussner MediaPlayerEditor: Stats and Info tab UI polish Change 3142667 on 2016/09/27 by Max.Preussner MediaAssets: Fixed serialization of older assets Change 3142669 on 2016/09/27 by Max.Preussner Automation: Allowing movie files to be renamed to match platform requirements Merged from Fortnite-Main CL# 3140907 Change 3145836 on 2016/09/29 by andrew.porter Adding and updating media framework test content Change 3145920 on 2016/09/29 by tim.gautier Added Media Audio actor to level QA-Media Change 3145979 on 2016/09/29 by andrew.porter Updating media player test content Change 3146311 on 2016/09/30 by Andrew.Rodham Sequencer: Fixed cursor jumping around inconsistently when ending a drag - The time slider controller was using a mixture of GetScreenSpacePosition and GetLastScreenSpacePosition when calculating times. It now only uses the current screen position. #jira UE-34738 Change 3147838 on 2016/09/30 by Max.Chen Sequencer: Fix crash when rendering with handle frames = 0. #jira UE-36708 Change 3147875 on 2016/10/01 by Max.Chen Sequencer - Don't crash when a bool track or visibility track has a null runtime object. #jira UE-36707 Change 3148176 on 2016/10/01 by Max.Chen Sequencer: When keep playback range in section bounds, infinite sections should be bounded by their keyframe times. #jira UE-36666 Change 3148824 on 2016/10/03 by Max.Preussner Media: Continued to implement subtitle tracks UpgradeNotes: - caption tracks were split into captions, subtitles, and generic text tracks - added IMediaOverlaySink - IMediaOutput::SetCaptionSink renamed to SetOverlaySink [CL 3149180 by Max Chen in Main branch]
2016-10-03 14:40:19 -04:00
FScriptMapHelper MapHelper(MapProperty, CurrentState.Data);
FProperty* Property = MapProperty->ValueProp;
int32 PairIndex = MapHelper.AddDefaultValue_Invalid_NeedsRehash();
uint8* PairPtr = MapHelper.GetPairPtr(PairIndex);
MapProperty->KeyProp->ImportText_Direct(*PropertyName, PairPtr, nullptr, PPF_None);
if (!Backend.ReadProperty(Property, CurrentState.Property, PairPtr, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("An item in map '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
}
else
{
// handle scalar property
FProperty* Property = FindFProperty<FProperty>(CurrentState.TypeInfo, *PropertyName);
if (Property != nullptr)
{
if (Policies.PropertyFilter && !Policies.PropertyFilter(Property, CurrentState.Property))
{
continue;
}
if (!Backend.ReadProperty(Property, CurrentState.Property, CurrentState.Data, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
}
else
{
// error: scalar property not found
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' does not exist"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
}
}
break;
case EStructDeserializerBackendTokens::StructureEnd:
{
// rehash if value was a map
FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property);
if (MapProperty != nullptr)
{
FScriptMapHelper MapHelper(MapProperty, CurrentState.Data);
MapHelper.Rehash();
}
// ending of root structure
if (StateStack.Num() == 0)
{
return true;
}
CurrentState = StateStack.Pop(/*bAllowShrinking*/ false);
}
break;
case EStructDeserializerBackendTokens::StructureStart:
{
FReadState NewState;
if (PropertyName.IsEmpty())
{
// skip root structure
if (CurrentState.Property == nullptr)
{
check(StateStack.Num() == 0);
continue;
}
// handle struct element inside set
if (FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property))
{
FScriptSetHelper SetHelper(SetProperty, CurrentState.Data);
const int32 ElementIndex = SetHelper.AddDefaultValue_Invalid_NeedsRehash();
uint8* ElementPtr = SetHelper.GetElementPtr(ElementIndex);
NewState.Data = ElementPtr;
NewState.Property = SetProperty->ElementProp;
}
// handle struct element inside array
else if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(CurrentState.Property))
{
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
const int32 ArrayIndex = ArrayHelper.AddValue();
NewState.Property = ArrayProperty->Inner;
NewState.Data = ArrayHelper.GetRawPtr(ArrayIndex);
}
else
{
UE_LOG(LogSerialization, Verbose, TEXT("Found unnamed value outside of array or set."));
return false;
}
}
// handle map or struct element inside map
else if ((CurrentState.Property != nullptr) && (CurrentState.Property->GetClass() == FMapProperty::StaticClass()))
{
FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property);
FScriptMapHelper MapHelper(MapProperty, CurrentState.Data);
int32 PairIndex = MapHelper.AddDefaultValue_Invalid_NeedsRehash();
uint8* PairPtr = MapHelper.GetPairPtr(PairIndex);
NewState.Data = PairPtr + MapHelper.MapLayout.ValueOffset;
NewState.Property = MapProperty->ValueProp;
MapProperty->KeyProp->ImportText_Direct(*PropertyName, PairPtr, nullptr, PPF_None);
}
else
{
NewState.Property = FindFProperty<FProperty>(CurrentState.TypeInfo, *PropertyName);
// unrecognized property
if (NewState.Property == nullptr)
{
// error: map or struct property not found
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("Map, Set, or struct property '%s' not found"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
// handle map property start
else if (FMapProperty* MapProperty = CastField<FMapProperty>(NewState.Property))
{
NewState.Data = MapProperty->ContainerPtrToValuePtr<void>(CurrentState.Data, CurrentState.ArrayIndex);
FScriptMapHelper MapHelper(MapProperty, NewState.Data);
MapHelper.EmptyValues();
}
// handle struct property
else
{
NewState.Data = NewState.Property->ContainerPtrToValuePtr<void>(CurrentState.Data);
}
}
if (NewState.Property != nullptr)
{
// skip struct property if property filter is set and rejects it
if (Policies.PropertyFilter && !Policies.PropertyFilter(NewState.Property, CurrentState.Property))
{
Backend.SkipStructure();
continue;
}
NewState.ArrayIndex = 0;
NewState.TypeInfo = FindClass(NewState);
StateStack.Push(CurrentState);
CurrentState = NewState;
}
else
{
// error: structured property not found
Backend.SkipStructure();
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("Structured property '%s' not found"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
}
default:
continue;
}
}
// root structure not completed
return false;
}
bool FStructDeserializer::DeserializeElement(void* OutAddress, UStruct& OwnerInfo, int32 InElementIndex, IStructDeserializerBackend& Backend, const FStructDeserializerPolicies& Policies)
{
using namespace StructDeserializer;
check(OutAddress != nullptr);
// initialize deserialization
FReadState CurrentState;
{
CurrentState.ArrayIndex = InElementIndex == INDEX_NONE ? 0 : InElementIndex;
CurrentState.Data = OutAddress;
CurrentState.TypeInfo = &OwnerInfo;
}
TArray<FReadState> StateStack;
EStructDeserializerBackendTokens Token;
// process state stack
while (Backend.GetNextToken(Token))
{
const FString PropertyName = Backend.GetCurrentPropertyName();
switch (Token)
{
case EStructDeserializerBackendTokens::ArrayEnd:
{
// rehash the set/maps -> we're closing them
check(CurrentState.Property);
if (FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property))
{
FScriptSetHelper SetHelper(SetProperty, CurrentState.Data);
SetHelper.Rehash();
}
else if (FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property))
{
FScriptMapHelper MapHelper(MapProperty, CurrentState.Data);
MapHelper.Rehash();
}
else if (CurrentState.Property->ArrayDim > 1 && CurrentState.ArrayIndex < CurrentState.Property->ArrayDim) //-V522 - Property should never be null
{
// error: array entry not found in static array
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("The static array '%s' of size %d only had %d entries"), *CurrentState.Property->GetFName().ToString(), CurrentState.Property->ArrayDim, CurrentState.ArrayIndex);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
if (StateStack.Num() == 0)
{
UE_LOG(LogSerialization, Verbose, TEXT("Malformed input: Found ArrayEnd without matching ArrayStart"));
return false;
}
CurrentState = StateStack.Pop(/*bAllowShrinking*/ false);
}
break;
case EStructDeserializerBackendTokens::ArrayStart:
{
FReadState NewState;
NewState.Property = FindFProperty<FProperty>(CurrentState.TypeInfo, *PropertyName);
if (NewState.Property != nullptr)
{
if (Policies.PropertyFilter && !Policies.PropertyFilter(NewState.Property, CurrentState.Property))
{
Backend.SkipArray();
continue;
}
if (FSetProperty* SetProperty = CastField<FSetProperty>(NewState.Property))
{
NewState.Data = SetProperty->ContainerPtrToValuePtr<void>(CurrentState.Data);
NewState.ArrayIndex = 0;
}
else if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(NewState.Property))
{
NewState.Data = CurrentState.Data;
NewState.ArrayIndex = 0;
}
else if (FMapProperty* MapProperty = CastField<FMapProperty>(NewState.Property))
{
NewState.Data = MapProperty->ContainerPtrToValuePtr<void>(CurrentState.Data);
NewState.ArrayIndex = 0;
}
// static array property
else if (NewState.Property != nullptr)
{
NewState.Data = CurrentState.Data;
NewState.ArrayIndex = 0;
}
NewState.TypeInfo = FindClass(NewState);
StateStack.Push(CurrentState);
CurrentState = NewState;
}
else
{
// error: array property not found
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' does not exist"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
Backend.SkipArray();
}
}
break;
case EStructDeserializerBackendTokens::Error:
{
return false;
}
case EStructDeserializerBackendTokens::Property:
{
// Set are serialized as array, so no property name will be set for each entry
if (PropertyName.IsEmpty() && (CurrentState.Property != nullptr) && (CurrentState.Property->GetClass() == FSetProperty::StaticClass()))
{
// handle set element
FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property);
FScriptSetHelper SetHelper(SetProperty, CurrentState.Data);
FProperty* Property = SetProperty->ElementProp;
if (SetHelper.IsValidIndex(CurrentState.ArrayIndex))
{
uint8* ElementPtr = SetHelper.GetElementPtr(CurrentState.ArrayIndex);
constexpr int32 ReadIndex = 0; //Pointer is offset so reading index is 0
if (!Backend.ReadProperty(Property, CurrentState.Property, ElementPtr, ReadIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("An item in Set '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
}
else
{
//Too many entries for given Set
UE_LOG(LogSerialization, Verbose, TEXT("TSet %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), SetHelper.Num(), CurrentState.ArrayIndex);
continue;
}
++CurrentState.ArrayIndex;
}
// Maps can be serialized as array, so no property name will be set for each entry. Each entry will be taken in order
if (PropertyName.IsEmpty() && (CurrentState.Property != nullptr) && (CurrentState.Property->GetClass() == FMapProperty::StaticClass()))
{
// handle map element
FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property);
FScriptMapHelper MapHelper(MapProperty, CurrentState.Data);
FProperty* Property = MapProperty->ValueProp;
//When written as array, maps won't include the key, only values
if (MapHelper.IsValidIndex(CurrentState.ArrayIndex))
{
uint8* PairPtr = MapHelper.GetPairPtr(CurrentState.ArrayIndex);
constexpr int32 ReadIndex = 0; //Pointer is offset so reading index is 0
if (!Backend.ReadProperty(Property, CurrentState.Property, PairPtr, ReadIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("An item in Set '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
}
else
{
//Too many entries for given Map
UE_LOG(LogSerialization, Verbose, TEXT("TMap %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), MapHelper.Num(), CurrentState.ArrayIndex);
continue;
}
++CurrentState.ArrayIndex;
}
// Otherwise we are dealing with dynamic or static array
else if (PropertyName.IsEmpty())
{
// When reading the property, the deserialize behavior is to add element. We bypass that with the property
FArrayProperty* ArrayProperty = CastField<FArrayProperty>(CurrentState.Property);
FProperty* Property = nullptr;
void* DataAddress = CurrentState.Data;
int32 CurrentArrayIndex = CurrentState.ArrayIndex;
if (ArrayProperty != nullptr)
{
// dynamic array element
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
if (ArrayHelper.IsValidIndex(CurrentState.ArrayIndex))
{
Property = ArrayProperty->Inner;
DataAddress = ArrayHelper.GetRawPtr(CurrentState.ArrayIndex);
//Arraydim will be 1 for inner TArray properties. Offset the read data and keep index at 0
CurrentArrayIndex = 0;
}
else
{
//Too many entries in TArray
UE_LOG(LogSerialization, Verbose, TEXT("TArray %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), CurrentState.Property->ArrayDim, CurrentState.ArrayIndex);
continue;
}
}
else
{
// static array element
if (CurrentState.ArrayIndex >= 0 && CurrentState.ArrayIndex < CurrentState.Property->ArrayDim)
{
Property = CurrentState.Property;
}
else
{
//Too many entries in static Array
UE_LOG(LogSerialization, Verbose, TEXT("Static array %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), CurrentState.Property->ArrayDim, CurrentState.ArrayIndex);
continue;
}
}
if (Property == nullptr)
{
// error: no meta data for array element
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("Failed to serialize array element %i"), CurrentState.ArrayIndex);
}
return false;
}
else if (!Backend.ReadProperty(Property, nullptr, DataAddress, CurrentArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("The array element '%s[%i]' could not be read (%s)"), *PropertyName, CurrentState.ArrayIndex, *Backend.GetDebugString());
}
++CurrentState.ArrayIndex;
}
else
{
// handle scalar property
FProperty* Property = FindFProperty<FProperty>(CurrentState.TypeInfo, *PropertyName);
if (Property != nullptr)
{
if (Policies.PropertyFilter && !Policies.PropertyFilter(Property, CurrentState.Property))
{
continue;
}
//Direct set element
if (FSetProperty* SetProperty = CastField<FSetProperty>(Property))
{
FScriptSetHelper SetHelper(SetProperty, SetProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//If a specific index is asked and it's not valid, skip the property
if (SetHelper.IsValidIndex(CurrentState.ArrayIndex))
{
Property = SetProperty->ElementProp;
//Offset the pointer directly and give index 0 to be read so no offsetting is done during deserialization
CurrentState.Data = SetHelper.GetElementPtr(CurrentState.ArrayIndex);
CurrentState.ArrayIndex = 0;
if (!Backend.ReadProperty(Property, nullptr, CurrentState.Data, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
//On element of a set was written so rehash it
SetHelper.Rehash();
continue;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("Set %s has dimension of %d and trying to read element %d"), *SetProperty->GetFName().ToString(), SetHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
else if (FMapProperty* MapProperty = CastField<FMapProperty>(Property))
{
FScriptMapHelper MapHelper(MapProperty, MapProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//If a specific index is asked and it's not valid, skip the property
if (MapHelper.IsValidIndex(CurrentState.ArrayIndex))
{
Property = MapProperty->ValueProp;
//Offset the pointer directly and give index 0 to be read so no offsetting is done during deserialization
CurrentState.Data = MapHelper.GetPairPtr(CurrentState.ArrayIndex);
CurrentState.ArrayIndex = 0;
if (!Backend.ReadProperty(Property, nullptr, CurrentState.Data, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
//On element of a map was written so rehash it
MapHelper.Rehash();
continue;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("TMap %s has dimension of %d and trying to read element %d"), *MapProperty->GetFName().ToString(), MapHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
else if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(Property))
{
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//If a specific index is asked and it's not valid, skip the property
if (ArrayHelper.IsValidIndex(CurrentState.ArrayIndex))
{
Property = ArrayProperty->Inner;
//Offset the pointer directly and give index 0 to be read so no offsetting is done during deserialization
CurrentState.Data = ArrayHelper.GetRawPtr(CurrentState.ArrayIndex);
CurrentState.ArrayIndex = 0;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("TArray %s has dimension of %d and trying to read element %d"), *ArrayProperty->GetFName().ToString(), ArrayHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
if (!Backend.ReadProperty(Property, nullptr, CurrentState.Data, CurrentState.ArrayIndex))
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' could not be read (%s)"), *PropertyName, *Backend.GetDebugString());
}
}
else
{
// error: scalar property not found
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("The property '%s' does not exist"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
}
}
break;
case EStructDeserializerBackendTokens::StructureEnd:
{
// rehash if value was a map, set
if(FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property))
{
FScriptMapHelper MapHelper(MapProperty, MapProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
MapHelper.Rehash();
}
else if (FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property))
{
FScriptSetHelper SetHelper(SetProperty, SetProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
SetHelper.Rehash();
}
// ending of root structure
if (StateStack.Num() == 0)
{
return true;
}
CurrentState = StateStack.Pop(/*bAllowShrinking*/ false);
}
break;
case EStructDeserializerBackendTokens::StructureStart:
{
FReadState NewState;
if (PropertyName.IsEmpty())
{
// skip root structure
if (CurrentState.Property == nullptr)
{
check(StateStack.Num() == 0);
continue;
}
// handle struct element inside set
if (FSetProperty* SetProperty = CastField<FSetProperty>(CurrentState.Property))
{
FScriptSetHelper SetHelper(SetProperty, CurrentState.Data);
if (SetHelper.IsValidIndex(CurrentState.ArrayIndex))
{
NewState.Property = SetProperty->ElementProp;
NewState.Data = SetHelper.GetElementPtr(CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
++CurrentState.ArrayIndex;
}
else
{
//Too many entries
UE_LOG(LogSerialization, Verbose, TEXT("TSet %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), SetHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
else if (FMapProperty* MapProperty = CastField<FMapProperty>(CurrentState.Property))
{
FScriptMapHelper MapHelper(MapProperty, CurrentState.Data);
if (MapHelper.IsValidIndex(CurrentState.ArrayIndex))
{
NewState.Property = MapProperty->ValueProp;
NewState.Data = MapHelper.GetValuePtr(CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
++CurrentState.ArrayIndex;
}
else
{
//Too many entries
UE_LOG(LogSerialization, Verbose, TEXT("TMap %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), MapHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
// handle struct element inside array
else if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(CurrentState.Property))
{
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//We're going over the array, make sure indexing is valid. Could add support for growing arrays
if (ArrayHelper.IsValidIndex(CurrentState.ArrayIndex))
{
NewState.Property = ArrayProperty->Inner;
NewState.Data = ArrayHelper.GetRawPtr(CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
++CurrentState.ArrayIndex;
}
else
{
//Too many entries in TArray
UE_LOG(LogSerialization, Verbose, TEXT("TArray %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), ArrayHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
else
{
//Property was found so we might be in a static array of struct
if (CurrentState.ArrayIndex >= 0 && CurrentState.ArrayIndex < CurrentState.Property->ArrayDim)
{
NewState.Property = CurrentState.Property;
NewState.Data = NewState.Property->ContainerPtrToValuePtr<void>(CurrentState.Data, CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
++CurrentState.ArrayIndex;
}
else
{
//Too many entries in static Array
UE_LOG(LogSerialization, Verbose, TEXT("Static array %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), CurrentState.Property->ArrayDim, CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
}
else
{
NewState.Property = FindFProperty<FProperty>(CurrentState.TypeInfo, *PropertyName);
// unrecognized property
if (NewState.Property == nullptr)
{
// error: map or struct property not found
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("Map, Set, or struct property '%s' not found"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
// handle map property start
else if (FMapProperty* MapProperty = CastField<FMapProperty>(NewState.Property))
{
if (FStructProperty* SetStructProperty = CastField<FStructProperty>(MapProperty->ValueProp))
{
FScriptMapHelper MapHelper(MapProperty, MapProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//If a specific index is asked and it's not valid, skip the property
if (MapHelper.IsValidIndex(CurrentState.ArrayIndex))
{
//We're skipping a level directly so CurrentState becomes the outer (set) and NewState the inner (Element prop)
CurrentState.Property = NewState.Property;
NewState.Property = SetStructProperty;
NewState.Data = MapHelper.GetValuePtr(CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("TMap %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), MapHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
}
//Handle Set property entry
else if (FSetProperty* SetProperty = CastField<FSetProperty>(NewState.Property))
{
if (FStructProperty* SetStructProperty = CastField<FStructProperty>(SetProperty->ElementProp))
{
FScriptSetHelper SetHelper(SetProperty, SetProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//If a specific index is asked and it's not valid, skip the property
if (SetHelper.IsValidIndex(CurrentState.ArrayIndex))
{
//We're skipping a level directly so CurrentState becomes the outer (set) and NewState the inner (Element prop)
CurrentState.Property = NewState.Property;
NewState.Property = SetProperty->ElementProp;
NewState.Data = SetHelper.GetElementPtr(CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("TArray %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), SetHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
}
//Handle array property entry
else if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(NewState.Property))
{
if (FStructProperty* ArrayStructProperty = CastField<FStructProperty>(ArrayProperty->Inner))
{
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->ContainerPtrToValuePtr<void>(CurrentState.Data));
//If a specific index is asked and it's not valid, skip the property
if (ArrayHelper.IsValidIndex(CurrentState.ArrayIndex))
{
//NewState will become the outer when going through the properties.
//When reading from Array, we expect to read from one level. When its a struct, it's not.
CurrentState.Property = NewState.Property;
NewState.Property = ArrayProperty->Inner;
NewState.Data = ArrayHelper.GetRawPtr(CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("TArray %s has dimension of %d and trying to read element %d"), *CurrentState.Property->GetFName().ToString(), ArrayHelper.Num(), CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
}
// handle struct property
else
{
if (CurrentState.ArrayIndex >= 0 && CurrentState.ArrayIndex < NewState.Property->ArrayDim)
{
NewState.Data = NewState.Property->ContainerPtrToValuePtr<void>(CurrentState.Data, CurrentState.ArrayIndex);
NewState.ArrayIndex = 0;
}
else
{
//Index out of bound
UE_LOG(LogSerialization, Verbose, TEXT("Static array %s has dimension of %d and trying to read element %d"), *NewState.Property->GetFName().ToString(), NewState.Property->ArrayDim, CurrentState.ArrayIndex);
Backend.SkipStructure();
continue;
}
}
}
if (NewState.Property != nullptr)
{
// skip struct property if property filter is set and rejects it
if (Policies.PropertyFilter && !Policies.PropertyFilter(NewState.Property, CurrentState.Property))
{
Backend.SkipStructure();
continue;
}
NewState.TypeInfo = FindClass(NewState);
StateStack.Push(CurrentState);
CurrentState = NewState;
}
else
{
// error: structured property not found
Backend.SkipStructure();
if (Policies.MissingFields != EStructDeserializerErrorPolicies::Ignore)
{
UE_LOG(LogSerialization, Verbose, TEXT("Structured property '%s' not found"), *PropertyName);
}
if (Policies.MissingFields == EStructDeserializerErrorPolicies::Error)
{
return false;
}
}
}
default:
continue;
}
}
// root structure not completed
return false;
}