2019-01-02 00:55:51 -05:00
// Copyright 1998-2019 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. */
2014-10-01 18:20:53 -04:00
int32 ArrayIndex ;
2014-10-27 07:54:20 -04:00
/** Holds a pointer to the property's data. */
2014-10-01 18:20:53 -04:00
void * Data ;
2014-10-27 07:54:20 -04:00
/** Holds the property's meta data. */
2014-10-01 18:20:53 -04:00
UProperty * Property ;
2014-10-27 07:54:20 -04:00
/** Holds a pointer to the UStruct describing the data. */
2014-10-01 18:20:53 -04:00
UStruct * TypeInfo ;
} ;
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 )
{
UProperty * ParentProperty = State . Property ;
UArrayProperty * ArrayProperty = Cast < UArrayProperty > ( ParentProperty ) ;
if ( ArrayProperty ! = nullptr )
{
ParentProperty = ArrayProperty - > Inner ;
}
UStructProperty * StructProperty = Cast < UStructProperty > ( ParentProperty ) ;
UObjectPropertyBase * ObjectProperty = Cast < UObjectPropertyBase > ( 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 :
{
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 ;
NewState . Property = FindField < UProperty > ( CurrentState . TypeInfo , * PropertyName ) ;
2015-09-09 20:32:05 -04:00
if ( NewState . Property ! = nullptr )
2014-10-01 18:20:53 -04:00
{
2015-09-09 20:32:05 -04:00
// handle array property
if ( Policies . PropertyFilter & & Policies . PropertyFilter ( NewState . Property , CurrentState . Property ) )
{
continue ;
}
NewState . ArrayIndex = 0 ;
NewState . Data = CurrentState . Data ;
NewState . TypeInfo = FindClass ( NewState ) ;
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 :
{
if ( PropertyName . IsEmpty ( ) )
{
2015-09-09 20:32:05 -04:00
// handle array element
2014-10-01 18:20:53 -04:00
UArrayProperty * ArrayProperty = Cast < UArrayProperty > ( CurrentState . Property ) ;
2015-09-09 20:32:05 -04:00
UProperty * 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 ;
}
2015-09-09 20:32:05 -04:00
else if ( ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = UMapProperty : : StaticClass ( ) ) )
{
// handle map element
UMapProperty * MapProperty = Cast < UMapProperty > ( CurrentState . Property ) ;
2016-10-03 14:40:19 -04:00
FScriptMapHelper MapHelper ( MapProperty , CurrentState . Data ) ;
2015-09-09 20:32:05 -04:00
UProperty * Property = MapProperty - > ValueProp ;
int32 PairIndex = MapHelper . AddDefaultValue_Invalid_NeedsRehash ( ) ;
uint8 * PairPtr = MapHelper . GetPairPtr ( PairIndex ) ;
2018-11-29 07:15:13 -05:00
MapProperty - > KeyProp - > ImportText ( * PropertyName , PairPtr , PPF_None , nullptr ) ;
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 ( ) ) ;
}
}
2016-06-16 11:54:44 -04:00
else if ( ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = USetProperty : : StaticClass ( ) ) )
{
// handle set element
USetProperty * SetProperty = Cast < USetProperty > ( CurrentState . Property ) ;
FScriptSetHelper SetHelper ( SetProperty , SetProperty - > ContainerPtrToValuePtr < void > ( CurrentState . Data ) ) ;
UProperty * 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 ( ) ) ;
}
}
2014-10-01 18:20:53 -04:00
else
{
// handle scalar property
2015-09-09 20:32:05 -04:00
UProperty * Property = FindField < UProperty > ( 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
{
2015-07-17 17:27:47 -04:00
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 ( ) ) ;
}
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
UMapProperty * MapProperty = Cast < UMapProperty > ( CurrentState . Property ) ;
if ( MapProperty ! = nullptr )
{
FScriptMapHelper MapHelper ( MapProperty , CurrentState . Data ) ;
MapHelper . Rehash ( ) ;
}
2016-06-16 11:54:44 -04:00
// rehash if value was a set
USetProperty * SetProperty = Cast < USetProperty > ( CurrentState . Property ) ;
if ( SetProperty ! = nullptr )
{
FScriptSetHelper SetHelper ( SetProperty , CurrentState . Data ) ;
SetHelper . Rehash ( ) ;
}
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 )
{
continue ;
}
2015-09-09 20:32:05 -04:00
// handle struct element inside array
2014-10-01 18:20:53 -04:00
UArrayProperty * ArrayProperty = Cast < UArrayProperty > ( CurrentState . Property ) ;
if ( ArrayProperty = = nullptr )
{
2015-09-09 20:32:05 -04:00
UE_LOG ( LogSerialization , Verbose , TEXT ( " Found unnamed value outside of array " ) ) ;
2014-10-01 18:20:53 -04:00
return false ;
}
FScriptArrayHelper ArrayHelper ( ArrayProperty , ArrayProperty - > ContainerPtrToValuePtr < void > ( CurrentState . Data ) ) ;
const int32 ArrayIndex = ArrayHelper . AddValue ( ) ;
NewState . Property = ArrayProperty - > Inner ;
NewState . Data = ArrayHelper . GetRawPtr ( ArrayIndex ) ;
}
2015-09-09 20:32:05 -04:00
else if ( ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = UMapProperty : : StaticClass ( ) ) )
{
// handle map or struct element inside map
UMapProperty * MapProperty = Cast < UMapProperty > ( 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 ;
2018-11-29 07:15:13 -05:00
MapProperty - > KeyProp - > ImportText ( * PropertyName , PairPtr , PPF_None , nullptr ) ;
2015-09-09 20:32:05 -04:00
}
2016-06-16 11:54:44 -04:00
else if ( ( CurrentState . Property ! = nullptr ) & & ( CurrentState . Property - > GetClass ( ) = = USetProperty : : StaticClass ( ) ) )
{
// handle map or struct element inside map
USetProperty * SetProperty = Cast < USetProperty > ( CurrentState . Property ) ;
FScriptSetHelper SetHelper ( SetProperty , CurrentState . Data ) ;
const int32 ElementIndex = SetHelper . AddDefaultValue_Invalid_NeedsRehash ( ) ;
uint8 * ElementPtr = SetHelper . GetElementPtr ( ElementIndex ) ;
2018-11-29 07:15:13 -05:00
NewState . Data = ElementPtr ;
2016-06-16 11:54:44 -04:00
NewState . Property = SetProperty - > ElementProp ;
}
2014-10-01 18:20:53 -04:00
else
{
NewState . Property = FindField < UProperty > ( CurrentState . TypeInfo , * PropertyName ) ;
if ( NewState . Property = = nullptr )
{
2016-06-16 11:54:44 -04:00
// error: map, set, 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 ;
}
}
2015-09-09 20:32:05 -04:00
else if ( NewState . Property - > GetClass ( ) = = UMapProperty : : StaticClass ( ) )
{
// handle map property
UMapProperty * MapProperty = Cast < UMapProperty > ( NewState . Property ) ;
NewState . Data = MapProperty - > ContainerPtrToValuePtr < void > ( CurrentState . Data , CurrentState . ArrayIndex ) ;
FScriptMapHelper MapHelper ( MapProperty , NewState . Data ) ;
MapHelper . EmptyValues ( ) ;
}
2016-06-16 11:54:44 -04:00
else if ( NewState . Property - > GetClass ( ) = = USetProperty : : StaticClass ( ) )
{
// handle set property
USetProperty * SetProperty = Cast < USetProperty > ( NewState . Property ) ;
NewState . Data = SetProperty - > ContainerPtrToValuePtr < void > ( CurrentState . Data , CurrentState . ArrayIndex ) ;
FScriptSetHelper SetHelper ( SetProperty , NewState . Data ) ;
SetHelper . EmptyElements ( ) ;
}
2014-10-01 18:20:53 -04:00
else
{
2015-09-09 20:32:05 -04:00
// handle struct property
2014-10-01 18:20:53 -04:00
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 ;
}