/
AggregateConverter.cpp
137 lines (107 loc) · 3.7 KB
/
AggregateConverter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "Conversion/Converters/AggregateConverter.h"
#include "LogSpeckle.h"
#include "Templates/SubclassOf.h"
void UAggregateConverter::OnConvertersChangeHandler()
{
SpeckleTypeMap.Empty();
for(int i = 0; i < SpeckleConverters.Num(); i++)
{
const UObject* Converter = SpeckleConverters[i];
if(Converter != nullptr && !Converter->GetClass()->ImplementsInterface(USpeckleConverter::StaticClass()))
{
UE_LOG(LogSpeckle, Warning, TEXT("Converter {%s} is not a valid converter, Expected to implement interface %s"), *Converter->GetClass()->GetName(), *USpeckleConverter::StaticClass()->GetName())
SpeckleConverters.RemoveAt(i);
i--;
}
}
}
#if WITH_EDITOR
void UAggregateConverter::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
if (PropertyChangedEvent.GetPropertyName() == GET_MEMBER_NAME_CHECKED(UAggregateConverter, SpeckleConverters))
{
OnConvertersChangeHandler();
}
}
#endif
UBase* UAggregateConverter::ConvertToSpeckle_Implementation(const UObject* Object)
{
//TODO implement ToSpeckle
unimplemented();
return nullptr;
}
UObject* UAggregateConverter::ConvertToNative_Implementation(const UBase* Object, UWorld* World, TScriptInterface<ISpeckleConverter>& )
{
return ConvertToNativeInternal(Object, World);
}
UObject* UAggregateConverter::ConvertToNativeInternal(const UBase* Object, UWorld* World)
{
check(IsInGameThread());
if(!IsValid(Object)) return nullptr;
const TSubclassOf<UBase> Type = Object->GetClass();
UObject* Converter = GetConverter(Type).GetObject();
if(!IsValid(Converter))
{
if(Type != UBase::StaticClass())
{
UE_LOG(LogSpeckle, Warning, TEXT("Skipping Object %s: No conversion functions exist for %s"), *Object->Id, *Type->GetName());
}
return nullptr;
}
UE_LOG(LogSpeckle, Log, TEXT("Converting object of type: %s id: %s"), *Type->GetName(), *Object->Id);
FEditorScriptExecutionGuard ScriptGuard;
TScriptInterface<ISpeckleConverter> MainConverter = this;
check(Converter->IsValidLowLevel());
return Execute_ConvertToNative(Converter, Object, World, MainConverter);
}
bool UAggregateConverter::CanConvertToNative_Implementation(TSubclassOf<UBase> BaseType)
{
return GetConverter(BaseType).GetInterface() != nullptr;
}
TScriptInterface<ISpeckleConverter> UAggregateConverter::GetConverter(const TSubclassOf<UBase> BaseType)
{
// Check if this SpeckleType has a known converter.
if(SpeckleTypeMap.Contains(BaseType))
{
return SpeckleTypeMap[BaseType];
}
// Try and find one that can convert this SpeckleType.
FEditorScriptExecutionGuard ScriptGuard;
for(UObject* Converter : SpeckleConverters)
{
if(!CheckValidConverter(Converter)) continue;
if(Execute_CanConvertToNative(Converter, BaseType))
{
//Found a Converter! Save this mapping for next time.
SpeckleTypeMap.Add(BaseType, Converter);
return Converter;
}
}
// SpeckleType has no conversions.
SpeckleTypeMap.Add(BaseType, nullptr);
return nullptr;
}
void UAggregateConverter::FinishConversion_Implementation()
{
FinishConversion_Internal();
}
void UAggregateConverter::FinishConversion_Internal()
{
for (UObject* Converter : SpeckleConverters)
{
if(!CheckValidConverter(Converter)) continue;
Execute_FinishConversion(Converter);
}
OnConvertersChangeHandler();
}
bool UAggregateConverter::CheckValidConverter(const UObject* Converter, bool LogWarning)
{
if(Converter == nullptr) return false;
if(Converter->Implements<USpeckleConverter>()) return true;
if(LogWarning)
{
UE_LOG(LogSpeckle, Warning, TEXT("Converter {%s} is not a valid converter, Expected to implement interface {%s}"), *Converter->GetClass()->GetName(), *USpeckleConverter::StaticClass()->GetName())
}
return false;
}