Files
UnrealEngineUWP/Engine/Plugins/Runtime/StateTree/Source/StateTreeEditorModule/Private/StateTreePropertyBindingCompiler.cpp
mikko mononen 34a1e22051 StateTree: Allow to bind to deep property paths (including arrays and instanced struct/object)
- refactored the property binding representation, editor binding shave now more structure, and removed intermediate representation
- added functionality to resolve property paths agains a known value
- added instanced struct and object indirection types
- added editor functionality to allow to bind to further than first level of properties
- refactored editor tree traversal, allow to access values too
- simplified statetree node ui
- requires to recompile trees, bumped version

#rb Mieszko.Zielinski
#preflight 63e6204ff15c83b79312aca5

[CL 24117094 by mikko mononen in ue5-main branch]
2023-02-10 07:22:48 -05:00

348 lines
13 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "StateTreePropertyBindingCompiler.h"
#include "IPropertyAccessEditor.h"
#include "PropertyPathHelpers.h"
#include "StateTreeCompiler.h"
#include "StateTreeCompilerLog.h"
#include "StateTreeEditorPropertyBindings.h"
#include "Misc/EnumerateRange.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(StateTreePropertyBindingCompiler)
bool FStateTreePropertyBindingCompiler::Init(FStateTreePropertyBindings& InPropertyBindings, FStateTreeCompilerLog& InLog)
{
Log = &InLog;
PropertyBindings = &InPropertyBindings;
PropertyBindings->Reset();
SourceStructs.Reset();
return true;
}
bool FStateTreePropertyBindingCompiler::CompileBatch(const FStateTreeBindableStructDesc& TargetStruct, TConstArrayView<FStateTreePropertyPathBinding> BatchPropertyBindings, int32& OutBatchIndex)
{
check(Log);
check(PropertyBindings);
OutBatchIndex = INDEX_NONE;
StoreSourceStructs();
const int32 BindingsBegin = PropertyBindings->PropertyPathBindings.Num();
for (const FStateTreePropertyPathBinding& Binding : BatchPropertyBindings)
{
if (Binding.GetTargetPath().GetStructID() != TargetStruct.ID)
{
continue;
}
// Source must be in the source array
const FGuid SourceStructID = Binding.GetSourcePath().GetStructID();
const int32 SourceStructIdx = SourceStructs.IndexOfByPredicate([SourceStructID](const FStateTreeBindableStructDesc& Struct)
{
return (Struct.ID == SourceStructID);
});
if (SourceStructIdx == INDEX_NONE)
{
Log->Reportf(EMessageSeverity::Error, TargetStruct,
TEXT("Could not find a binding source."));
return false;
}
const FStateTreeBindableStructDesc& SourceStruct = SourceStructs[SourceStructIdx];
FString Error;
TArray<FStateTreePropertyPathIndirection> SourceIndirections;
TArray<FStateTreePropertyPathIndirection> TargetIndirections;
if (!Binding.GetSourcePath().ResolveIndirections(SourceStruct.Struct, SourceIndirections, &Error))
{
Log->Reportf(EMessageSeverity::Error, TargetStruct, TEXT("Resolving path in %s: %s"), *SourceStruct.Name.ToString(), *Error);
return false;
}
if (!Binding.GetTargetPath().ResolveIndirections(TargetStruct.Struct, TargetIndirections, &Error))
{
Log->Reportf(EMessageSeverity::Error, TargetStruct, TEXT("Resolving path in %s: %s"), *TargetStruct.Name.ToString(), *Error);
return false;
}
FStateTreePropertyCopy DummyCopy;
FStateTreePropertyPathIndirection LastSourceIndirection = !SourceIndirections.IsEmpty() ? SourceIndirections.Last() : FStateTreePropertyPathIndirection(SourceStruct.Struct);
FStateTreePropertyPathIndirection LastTargetIndirection = !TargetIndirections.IsEmpty() ? TargetIndirections.Last() : FStateTreePropertyPathIndirection(TargetStruct.Struct);
if (!PropertyBindings->ResolveCopyType(DummyCopy, LastSourceIndirection, LastTargetIndirection))
{
Log->Reportf(EMessageSeverity::Error, TargetStruct,
TEXT("Failed to resolve property copy type between %s:%s and %s:%s."),
*SourceStruct.Name.ToString(), *Binding.GetSourcePath().ToString(),
*TargetStruct.Name.ToString(), *Binding.GetTargetPath().ToString());
return false;
}
if (const auto Validation = UE::StateTree::Compiler::IsValidIndex16(SourceStructIdx); Validation.DidFail())
{
if (Log != nullptr)
{
Validation.Log(*Log, TEXT("Source Struct Index"), SourceStruct);
}
return false;
}
PropertyBindings->PropertyPathBindings.Emplace(FStateTreeIndex16(SourceStructIdx), Binding.GetSourcePath(), Binding.GetTargetPath());
}
const int32 BindingsEnd = PropertyBindings->PropertyPathBindings.Num();
if (BindingsBegin != BindingsEnd)
{
FStateTreePropertyCopyBatch& Batch = PropertyBindings->CopyBatches.AddDefaulted_GetRef();
Batch.TargetStruct = TargetStruct;
Batch.BindingsBegin = IntCastChecked<uint16>(BindingsBegin);
Batch.BindingsEnd = IntCastChecked<uint16>(BindingsEnd);
OutBatchIndex = PropertyBindings->CopyBatches.Num() - 1;
}
return true;
}
void FStateTreePropertyBindingCompiler::Finalize()
{
StoreSourceStructs();
}
int32 FStateTreePropertyBindingCompiler::AddSourceStruct(const FStateTreeBindableStructDesc& SourceStruct)
{
const int32 ExistingIndex = SourceStructs.IndexOfByPredicate([&SourceStruct](const FStateTreeBindableStructDesc& Struct) { return (Struct.ID == SourceStruct.ID); });
if (ExistingIndex != INDEX_NONE)
{
const FStateTreeBindableStructDesc& ExistingStruct = SourceStructs[ExistingIndex];
UE_LOG(LogStateTree, Error, TEXT("Struct '%s' already exists as '%s'"),
*SourceStruct.Name.ToString(), *SourceStruct.ID.ToString(), *ExistingStruct.Name.ToString());
}
SourceStructs.Add(SourceStruct);
return SourceStructs.Num() - 1;
}
int32 FStateTreePropertyBindingCompiler::GetSourceStructIndexByID(const FGuid& ID) const
{
return SourceStructs.IndexOfByPredicate([ID](const FStateTreeBindableStructDesc& Structs) { return (Structs.ID == ID); });
}
PRAGMA_DISABLE_DEPRECATION_WARNINGS
bool FStateTreePropertyBindingCompiler::ResolvePropertyPath(const FStateTreeBindableStructDesc& InStructDesc, const FStateTreeEditorPropertyPath& InPath,
TArray<FStateTreePropertySegment>& OutSegments, const FProperty*& OutLeafProperty, int32& OutLeafArrayIndex,
FStateTreeCompilerLog* InLog, const FStateTreeBindableStructDesc* InLogContextStruct)
{
if (!InPath.IsValid())
{
if (InLog != nullptr && InLogContextStruct != nullptr)
{
InLog->Reportf(EMessageSeverity::Error, *InLogContextStruct,
TEXT("Invalid path '%s:%s'."),
*InStructDesc.Name.ToString(), *InPath.ToString());
}
return false;
}
// If the path is empty, we're pointing directly at the source struct.
if (InPath.Path.Num() == 0)
{
OutLeafProperty = nullptr;
OutLeafArrayIndex = INDEX_NONE;
return true;
}
const UStruct* CurrentStruct = InStructDesc.Struct;
const FProperty* LeafProperty = nullptr;
int32 LeafArrayIndex = INDEX_NONE;
bool bResult = true;
for (int32 SegmentIndex = 0; SegmentIndex < InPath.Path.Num(); SegmentIndex++)
{
const FString& SegmentString = InPath.Path[SegmentIndex];
const TCHAR* PropertyNamePtr = nullptr;
int32 PropertyNameLength = 0;
int32 ArrayIndex = INDEX_NONE;
PropertyPathHelpers::FindFieldNameAndArrayIndex(SegmentString.Len(), *SegmentString, PropertyNameLength, &PropertyNamePtr, ArrayIndex);
ensure(PropertyNamePtr != nullptr);
FString PropertyNameString(PropertyNameLength, PropertyNamePtr);
const FName PropertyName = FName(*PropertyNameString, FNAME_Find);
const bool bFinalSegment = SegmentIndex == (InPath.Path.Num() - 1);
if (CurrentStruct == nullptr)
{
if (InLog != nullptr && InLogContextStruct != nullptr)
{
InLog->Reportf(EMessageSeverity::Error, *InLogContextStruct,
TEXT("Malformed path '%s:%s'."),
*InStructDesc.Name.ToString(), *InPath.ToString(SegmentIndex, TEXT("<"), TEXT(">")));
}
bResult = false;
break;
}
const FProperty* Property = CurrentStruct->FindPropertyByName(PropertyName);
if (Property == nullptr)
{
// TODO: use core redirects to fix up the name.
if (InLog != nullptr && InLogContextStruct != nullptr)
{
InLog->Reportf(EMessageSeverity::Error, *InLogContextStruct,
TEXT("Malformed path '%s:%s', could not find property '%s%s.%s'."),
*InStructDesc.Name.ToString(), *InPath.ToString(SegmentIndex, TEXT("<"), TEXT(">")),
CurrentStruct->GetPrefixCPP(), *CurrentStruct->GetName(), *PropertyName.ToString());
}
bResult = false;
break;
}
if (const auto Validation = UE::StateTree::Compiler::IsValidIndex16(ArrayIndex); Validation.DidFail())
{
if (InLog != nullptr)
{
Validation.Log(*InLog, TEXT("ArrayIndex"), InStructDesc);
}
return false;
}
FStateTreePropertySegment& Segment = OutSegments.AddDefaulted_GetRef();
Segment.Name = PropertyName;
Segment.ArrayIndex = FStateTreeIndex16(ArrayIndex);
// Check to see if it is an array access first
const FArrayProperty* ArrayProperty = CastField<FArrayProperty>(Property);
if (ArrayProperty != nullptr && ArrayIndex != INDEX_NONE)
{
// It is an array, now check to see if this is an array of structures
if (const FStructProperty* ArrayOfStructsProperty = CastField<FStructProperty>(ArrayProperty->Inner))
{
Segment.Type = EStateTreePropertyAccessType::IndexArray;
CurrentStruct = ArrayOfStructsProperty->Struct;
}
// if it's not an array of structs, maybe it's an array of objects
else if (const FObjectPropertyBase* ArrayOfObjectsProperty = CastField<FObjectPropertyBase>(ArrayProperty->Inner))
{
Segment.Type = EStateTreePropertyAccessType::IndexArray;
CurrentStruct = ArrayOfObjectsProperty->PropertyClass;
if (!bFinalSegment)
{
// Object arrays need an object dereference adding if non-leaf
FStateTreePropertySegment& ExtraSegment = OutSegments.AddDefaulted_GetRef();
ExtraSegment.ArrayIndex = FStateTreeIndex16(0);
ExtraSegment.Type = EStateTreePropertyAccessType::Object;
const FProperty* InnerProperty = ArrayProperty->Inner;
if (const FObjectProperty* ObjectProperty = CastField<FObjectProperty>(InnerProperty))
{
ExtraSegment.Type = EStateTreePropertyAccessType::Object;
}
else if (const FWeakObjectProperty* WeakObjectProperty = CastField<FWeakObjectProperty>(InnerProperty))
{
ExtraSegment.Type = EStateTreePropertyAccessType::WeakObject;
}
else if (const FSoftObjectProperty* SoftObjectProperty = CastField<FSoftObjectProperty>(InnerProperty))
{
ExtraSegment.Type = EStateTreePropertyAccessType::SoftObject;
}
}
}
else
{
Segment.Type = EStateTreePropertyAccessType::IndexArray;
Segment.ArrayIndex = FStateTreeIndex16(ArrayIndex);
CurrentStruct = nullptr;
}
}
// Leaf segments all get treated the same, plain, array, struct or object. Copy type is figured out separately.
else if (bFinalSegment)
{
Segment.Type = EStateTreePropertyAccessType::Offset;
CurrentStruct = nullptr;
}
// Check to see if this is a simple structure (eg. not an array of structures)
else if (const FStructProperty* StructProperty = CastField<FStructProperty>(Property))
{
Segment.Type = EStateTreePropertyAccessType::Offset;
CurrentStruct = StructProperty->Struct;
}
// Check to see if this is a simple object (eg. not an array of objects)
else if (const FObjectProperty* ObjectProperty = CastField<FObjectProperty>(Property))
{
Segment.Type = EStateTreePropertyAccessType::Object;
CurrentStruct = ObjectProperty->PropertyClass;
}
// Check to see if this is a simple weak object property (eg. not an array of weak objects).
else if (const FWeakObjectProperty* WeakObjectProperty = CastField<FWeakObjectProperty>(Property))
{
Segment.Type = EStateTreePropertyAccessType::WeakObject;
CurrentStruct = WeakObjectProperty->PropertyClass;
}
// Check to see if this is a simple soft object property (eg. not an array of soft objects).
else if (const FSoftObjectProperty* SoftObjectProperty = CastField<FSoftObjectProperty>(Property))
{
Segment.Type = EStateTreePropertyAccessType::SoftObject;
CurrentStruct = SoftObjectProperty->PropertyClass;
}
else
{
if (InLog != nullptr && InLogContextStruct != nullptr)
{
InLog->Reportf(EMessageSeverity::Error, *InLogContextStruct,
TEXT("Unsupported segment %s in path '%s:%s'."),
*InStructDesc.Name.ToString(), *InPath.ToString(SegmentIndex, TEXT("<"), TEXT(">")),
*Property->GetCPPType(), *InStructDesc.Name.ToString(), *InPath.ToString(SegmentIndex, TEXT("<"), TEXT(">")));
}
bResult = false;
break;
}
if (bFinalSegment)
{
LeafProperty = Property;
LeafArrayIndex = ArrayIndex;
}
}
if (!bResult)
{
return false;
}
OutLeafProperty = LeafProperty;
OutLeafArrayIndex = LeafArrayIndex;
return true;
}
EPropertyAccessCompatibility FStateTreePropertyBindingCompiler::GetPropertyCompatibility(const FProperty* FromProperty, const FProperty* ToProperty)
{
const EStateTreePropertyAccessCompatibility Result = FStateTreePropertyBindings::GetPropertyCompatibility(FromProperty, ToProperty);
if (Result == EStateTreePropertyAccessCompatibility::Compatible)
{
return EPropertyAccessCompatibility::Compatible;
}
if (Result == EStateTreePropertyAccessCompatibility::Promotable)
{
return EPropertyAccessCompatibility::Promotable;
}
return EPropertyAccessCompatibility::Incompatible;
}
PRAGMA_ENABLE_DEPRECATION_WARNINGS
void FStateTreePropertyBindingCompiler::StoreSourceStructs()
{
// Check that existing structs are compatible
check(PropertyBindings->SourceStructs.Num() <= SourceStructs.Num());
for (int32 i = 0; i < PropertyBindings->SourceStructs.Num(); i++)
{
check(PropertyBindings->SourceStructs[i] == SourceStructs[i]);
}
// Add new
if (SourceStructs.Num() > PropertyBindings->SourceStructs.Num())
{
for (int32 i = PropertyBindings->SourceStructs.Num(); i < SourceStructs.Num(); i++)
{
PropertyBindings->SourceStructs.Add(SourceStructs[i]);
}
}
}