2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2014-10-01 18:20:53 -04:00
# 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"
2014-10-01 18:20:53 -04:00
/* Internal helpers
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
namespace StructDeserializer
{
2014-10-27 07:54:20 -04:00
/**
* Structure for the read state stack .
*/
2014-10-01 18:20:53 -04:00
struct FReadState
{
2014-10-27 07:54:20 -04:00
/** Holds the property's current array index. */
2019-06-07 11:22:52 -04:00
int32 ArrayIndex = 0 ;
2014-10-01 18:20:53 -04:00
2014-10-27 07:54:20 -04:00
/** Holds a pointer to the property's data. */
2019-06-07 11:22:52 -04:00
void * Data = nullptr ;
2014-10-01 18:20:53 -04:00
2014-10-27 07:54:20 -04:00
/** Holds the property's meta data. */
2019-12-13 11:07:03 -05:00
FProperty * Property = nullptr ;
2014-10-01 18:20:53 -04:00
2014-10-27 07:54:20 -04:00
/** Holds a pointer to the UStruct describing the data. */
2019-06-07 11:22:52 -04:00
UStruct * TypeInfo = nullptr ;
2014-10-01 18:20:53 -04:00
} ;
2014-10-27 07:54:20 -04:00
/**
* Finds the class for the given stack state .
*
* @ param State The stack state to find the class for .
* @ return The class , if found .
*/
2014-10-01 18:20:53 -04:00
UStruct * FindClass ( const FReadState & State )
{
UStruct * Class = nullptr ;
if ( State . Property ! = nullptr )
{
2019-12-13 11:07:03 -05:00
FProperty * ParentProperty = State . Property ;
2014-10-01 18:20:53 -04:00
2019-12-13 11:07:03 -05:00
if ( FArrayProperty * ArrayProperty = CastField < FArrayProperty > ( ParentProperty ) )
2014-10-01 18:20:53 -04:00
{
ParentProperty = ArrayProperty - > Inner ;
}
2019-12-13 11:07:03 -05:00
FStructProperty * StructProperty = CastField < FStructProperty > ( ParentProperty ) ;
FObjectPropertyBase * ObjectProperty = CastField < FObjectPropertyBase > ( ParentProperty ) ;
2014-10-01 18:20:53 -04:00
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 :
{
2019-06-07 11:22:52 -04:00
// rehash the set now that we are done with it
2019-12-13 11:07:03 -05:00
if ( FSetProperty * SetProperty = CastField < FSetProperty > ( CurrentState . Property ) )
2019-06-07 11:22:52 -04:00
{
FScriptSetHelper SetHelper ( SetProperty , CurrentState . Data ) ;
SetHelper . Rehash ( ) ;
}
2014-10-01 18:20:53 -04:00
if ( StateStack . Num ( ) = = 0 )
{
UE_LOG ( LogSerialization , Verbose , TEXT ( " Malformed input: Found ArrayEnd without matching ArrayStart " ) ) ;
return false ;
}
2015-09-09 20:32:05 -04:00
CurrentState = StateStack . Pop ( /*bAllowShrinking*/ false ) ;
2014-10-01 18:20:53 -04:00
}
break ;
case EStructDeserializerBackendTokens : : ArrayStart :
{
FReadState NewState ;
2020-03-15 10:33:45 -04:00
NewState . Property = FindFProperty < FProperty > ( CurrentState . TypeInfo , * PropertyName ) ;
2014-10-01 18:20:53 -04:00
2015-09-09 20:32:05 -04:00
if ( NewState . Property ! = nullptr )
2014-10-01 18:20:53 -04:00
{
2019-06-07 11:22:52 -04:00
if ( Policies . PropertyFilter & & ! Policies . PropertyFilter ( NewState . Property , CurrentState . Property ) )
2015-09-09 20:32:05 -04:00
{
2019-06-07 11:22:52 -04:00
Backend . SkipArray ( ) ;
2015-09-09 20:32:05 -04:00
continue ;
}
2019-06-07 11:22:52 -04:00
// handle set property
2019-12-13 11:07:03 -05:00
if ( FSetProperty * SetProperty = CastField < FSetProperty > ( NewState . Property ) )
2019-06-07 11:22:52 -04:00
{
NewState . Data = SetProperty - > ContainerPtrToValuePtr < void > ( CurrentState . Data , CurrentState . ArrayIndex ) ;
FScriptSetHelper SetHelper ( SetProperty , NewState . Data ) ;
SetHelper . EmptyElements ( ) ;
}
// handle array property
2021-02-03 14:57:28 -04:00
else if ( FArrayProperty * ArrayProperty = CastField < FArrayProperty > ( NewState . Property ) )
2019-06-07 11:22:52 -04:00
{
2021-02-03 14:57:28 -04:00
// 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 ;
2019-06-07 11:22:52 -04:00
}
2015-09-09 20:32:05 -04:00
2019-06-07 11:22:52 -04:00
NewState . TypeInfo = FindClass ( NewState ) ;
2015-09-09 20:32:05 -04:00
StateStack . Push ( CurrentState ) ;
CurrentState = NewState ;
}
else
{
// error: array property not found
2014-10-01 18:20:53 -04:00
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 :
{
2019-06-07 11:22:52 -04:00
// Set are serialized as array, so no property name will be set for each entry
2019-12-13 11:07:03 -05:00
if ( PropertyName . IsEmpty ( ) & & ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = FSetProperty : : StaticClass ( ) ) )
2019-06-07 11:22:52 -04:00
{
// handle set element
2019-12-13 11:07:03 -05:00
FSetProperty * SetProperty = CastField < FSetProperty > ( CurrentState . Property ) ;
2019-06-07 11:22:52 -04:00
FScriptSetHelper SetHelper ( SetProperty , CurrentState . Data ) ;
2019-12-13 11:07:03 -05:00
FProperty * Property = SetProperty - > ElementProp ;
2019-06-07 11:22:52 -04:00
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 ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 20:32:05 -04:00
// handle array element
2019-12-13 11:07:03 -05:00
FArrayProperty * ArrayProperty = CastField < FArrayProperty > ( CurrentState . Property ) ;
FProperty * Property = nullptr ;
2014-10-01 18:20:53 -04:00
if ( ArrayProperty ! = nullptr )
{
2015-09-09 20:32:05 -04:00
// dynamic array element
2014-10-01 18:20:53 -04:00
Property = ArrayProperty - > Inner ;
}
else
{
2015-09-09 20:32:05 -04:00
// static array element
2014-10-01 18:20:53 -04:00
Property = CurrentState . Property ;
}
if ( Property = = nullptr )
{
2015-09-09 20:32:05 -04:00
// error: no meta data for array element
2014-10-01 18:20:53 -04:00
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 ;
}
2019-12-13 11:07:03 -05:00
else if ( ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = FMapProperty : : StaticClass ( ) ) )
2015-09-09 20:32:05 -04:00
{
// handle map element
2019-12-13 11:07:03 -05:00
FMapProperty * MapProperty = CastField < FMapProperty > ( CurrentState . Property ) ;
2016-10-03 14:40:19 -04:00
FScriptMapHelper MapHelper ( MapProperty , CurrentState . Data ) ;
2019-12-13 11:07:03 -05:00
FProperty * Property = MapProperty - > ValueProp ;
2015-09-09 20:32:05 -04:00
int32 PairIndex = MapHelper . AddDefaultValue_Invalid_NeedsRehash ( ) ;
uint8 * PairPtr = MapHelper . GetPairPtr ( PairIndex ) ;
2022-02-25 10:39:39 -05:00
MapProperty - > KeyProp - > ImportText_Direct ( * PropertyName , PairPtr , nullptr , PPF_None ) ;
2015-09-09 20:32:05 -04:00
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 ( ) ) ;
}
}
2014-10-01 18:20:53 -04:00
else
{
// handle scalar property
2020-03-15 10:33:45 -04:00
FProperty * Property = FindFProperty < FProperty > ( CurrentState . TypeInfo , * PropertyName ) ;
2014-10-01 18:20:53 -04:00
2015-09-09 20:32:05 -04:00
if ( Property ! = nullptr )
2014-10-01 18:20:53 -04:00
{
2019-06-07 11:22:52 -04:00
if ( Policies . PropertyFilter & & ! Policies . PropertyFilter ( Property , CurrentState . Property ) )
2015-07-17 17:27:47 -04:00
{
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 ( ) ) ;
}
2014-10-01 18:20:53 -04:00
}
2015-09-09 20:32:05 -04:00
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 ;
}
}
2014-10-01 18:20:53 -04:00
}
}
break ;
case EStructDeserializerBackendTokens : : StructureEnd :
{
2015-09-09 20:32:05 -04:00
// rehash if value was a map
2019-12-13 11:07:03 -05:00
FMapProperty * MapProperty = CastField < FMapProperty > ( CurrentState . Property ) ;
2015-09-09 20:32:05 -04:00
if ( MapProperty ! = nullptr )
{
FScriptMapHelper MapHelper ( MapProperty , CurrentState . Data ) ;
MapHelper . Rehash ( ) ;
}
2019-06-07 11:22:52 -04:00
// ending of root structure
2014-10-01 18:20:53 -04:00
if ( StateStack . Num ( ) = = 0 )
{
return true ;
}
2015-09-09 20:32:05 -04:00
CurrentState = StateStack . Pop ( /*bAllowShrinking*/ false ) ;
2014-10-01 18:20:53 -04:00
}
break ;
case EStructDeserializerBackendTokens : : StructureStart :
{
FReadState NewState ;
if ( PropertyName . IsEmpty ( ) )
{
// skip root structure
if ( CurrentState . Property = = nullptr )
{
2019-06-07 11:22:52 -04:00
check ( StateStack . Num ( ) = = 0 ) ;
2014-10-01 18:20:53 -04:00
continue ;
}
2019-06-07 11:22:52 -04:00
// handle struct element inside set
2019-12-13 11:07:03 -05:00
if ( FSetProperty * SetProperty = CastField < FSetProperty > ( CurrentState . Property ) )
2014-10-01 18:20:53 -04:00
{
2019-06-07 11:22:52 -04:00
FScriptSetHelper SetHelper ( SetProperty , CurrentState . Data ) ;
const int32 ElementIndex = SetHelper . AddDefaultValue_Invalid_NeedsRehash ( ) ;
uint8 * ElementPtr = SetHelper . GetElementPtr ( ElementIndex ) ;
2014-10-01 18:20:53 -04:00
2019-06-07 11:22:52 -04:00
NewState . Data = ElementPtr ;
NewState . Property = SetProperty - > ElementProp ;
}
// handle struct element inside array
2019-12-13 11:07:03 -05:00
else if ( FArrayProperty * ArrayProperty = CastField < FArrayProperty > ( CurrentState . Property ) )
2019-06-07 11:22:52 -04:00
{
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. " ) ) ;
2014-10-01 18:20:53 -04:00
return false ;
}
}
2019-06-07 11:22:52 -04:00
// handle map or struct element inside map
2019-12-13 11:07:03 -05:00
else if ( ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = FMapProperty : : StaticClass ( ) ) )
2015-09-09 20:32:05 -04:00
{
2019-12-13 11:07:03 -05:00
FMapProperty * MapProperty = CastField < FMapProperty > ( CurrentState . Property ) ;
2015-09-09 20:32:05 -04:00
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 ;
2022-02-25 10:39:39 -05:00
MapProperty - > KeyProp - > ImportText_Direct ( * PropertyName , PairPtr , nullptr , PPF_None ) ;
2015-09-09 20:32:05 -04:00
}
2014-10-01 18:20:53 -04:00
else
{
2020-03-15 10:33:45 -04:00
NewState . Property = FindFProperty < FProperty > ( CurrentState . TypeInfo , * PropertyName ) ;
2014-10-01 18:20:53 -04:00
2019-06-07 11:22:52 -04:00
// unrecognized property
2014-10-01 18:20:53 -04:00
if ( NewState . Property = = nullptr )
{
2019-06-07 11:22:52 -04:00
// error: map or struct property not found
2014-10-01 18:20:53 -04:00
if ( Policies . MissingFields ! = EStructDeserializerErrorPolicies : : Ignore )
{
2016-06-16 11:54:44 -04:00
UE_LOG ( LogSerialization , Verbose , TEXT ( " Map, Set, or struct property '%s' not found " ) , * PropertyName ) ;
2014-10-01 18:20:53 -04:00
}
if ( Policies . MissingFields = = EStructDeserializerErrorPolicies : : Error )
{
return false ;
}
}
2019-06-07 11:22:52 -04:00
// handle map property start
2019-12-13 11:07:03 -05:00
else if ( FMapProperty * MapProperty = CastField < FMapProperty > ( NewState . Property ) )
2015-09-09 20:32:05 -04:00
{
NewState . Data = MapProperty - > ContainerPtrToValuePtr < void > ( CurrentState . Data , CurrentState . ArrayIndex ) ;
FScriptMapHelper MapHelper ( MapProperty , NewState . Data ) ;
MapHelper . EmptyValues ( ) ;
}
2019-06-07 11:22:52 -04:00
// handle struct property
2014-10-01 18:20:53 -04:00
else
{
NewState . Data = NewState . Property - > ContainerPtrToValuePtr < void > ( CurrentState . Data ) ;
}
}
if ( NewState . Property ! = nullptr )
{
2015-09-09 20:32:05 -04:00
// skip struct property if property filter is set and rejects it
2015-07-17 17:27:47 -04:00
if ( Policies . PropertyFilter & & ! Policies . PropertyFilter ( NewState . Property , CurrentState . Property ) )
{
Backend . SkipStructure ( ) ;
continue ;
}
2014-10-01 18:20:53 -04:00
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 ;
}
2020-10-09 22:42:26 -04:00
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
2021-01-13 12:39:10 -04:00
check ( CurrentState . Property ) ;
2020-10-09 22:42:26 -04:00
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 ( ) ;
}
2021-01-13 12:39:10 -04:00
else if ( CurrentState . Property - > ArrayDim > 1 & & CurrentState . ArrayIndex < CurrentState . Property - > ArrayDim ) //-V522 - Property should never be null
2020-10-09 22:42:26 -04:00
{
// 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 ;
}