CEFInterfaceJSStructDeserializerBackend.cpp
12.2 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Engine/Source/Runtime/WebBrowser/Private/CEF/CEFJSStructDeserializerBackend.cpp
#include "CEF/CEFInterfaceJSStructDeserializerBackend.h"
#if WITH_CEF3
#include "UObject/EnumProperty.h"
#include "UObject/TextProperty.h"
#include "WebInterfaceJSFunction.h"
/* Internal helpers
*****************************************************************************/
namespace {
template<typename ValueType, typename ContainerType, typename KeyType>
ValueType GetNumeric(CefRefPtr<ContainerType> Container, KeyType Key)
{
switch(Container->GetType(Key))
{
case VTYPE_BOOL:
return static_cast<ValueType>(Container->GetBool(Key));
case VTYPE_INT:
return static_cast<ValueType>(Container->GetInt(Key));
case VTYPE_DOUBLE:
return static_cast<ValueType>(Container->GetDouble(Key));
case VTYPE_STRING:
case VTYPE_DICTIONARY:
case VTYPE_LIST:
case VTYPE_NULL:
case VTYPE_BINARY:
default:
return static_cast<ValueType>(0);
}
}
template<typename ContainerType, typename KeyType>
void AssignTokenFromContainer(ContainerType Container, KeyType Key, EStructDeserializerBackendTokens& OutToken, FString& PropertyName, TSharedPtr<ICefInterfaceContainerWalker>& Retval)
{
switch (Container->GetType(Key))
{
case VTYPE_NULL:
case VTYPE_BOOL:
case VTYPE_INT:
case VTYPE_DOUBLE:
case VTYPE_STRING:
OutToken = EStructDeserializerBackendTokens::Property;
break;
case VTYPE_DICTIONARY:
{
CefRefPtr<CefDictionaryValue> Dictionary = Container->GetDictionary(Key);
if (Dictionary->GetType("$type") == VTYPE_STRING )
{
OutToken = EStructDeserializerBackendTokens::Property;
}
else
{
TSharedPtr<ICefInterfaceContainerWalker> NewWalker(new FCefInterfaceDictionaryValueWalker(Retval, Dictionary));
Retval = NewWalker->GetNextToken(OutToken, PropertyName);
}
break;
}
case VTYPE_LIST:
{
TSharedPtr<ICefInterfaceContainerWalker> NewWalker(new FCefInterfaceListValueWalker(Retval, Container->GetList(Key)));
Retval = NewWalker->GetNextToken(OutToken, PropertyName);
break;
}
case VTYPE_BINARY:
case VTYPE_INVALID:
default:
OutToken = EStructDeserializerBackendTokens::Error;
break;
}
}
/**
* Gets a pointer to object of the given property.
*
* @param Property The property to get.
* @param Outer The property that contains the property to be get, if any.
* @param Data A pointer to the memory holding the property's data.
* @param ArrayIndex The index of the element to set (if the property is an array).
* @return A pointer to the object represented by the property, null otherwise..
* @see ClearPropertyValue
*/
void* GetPropertyValuePtr( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex )
{
check(Property);
if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(Outer))
{
if (ArrayProperty->Inner != Property)
{
return nullptr;
}
FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->template ContainerPtrToValuePtr<void>(Data));
int32 Index = ArrayHelper.AddValue();
return ArrayHelper.GetRawPtr(Index);
}
if (ArrayIndex >= Property->ArrayDim)
{
return nullptr;
}
return Property->template ContainerPtrToValuePtr<void>(Data, ArrayIndex);
}
/**
* Sets the value of the given property.
*
* @param Property The property to set.
* @param Outer The property that contains the property to be set, if any.
* @param Data A pointer to the memory holding the property's data.
* @param ArrayIndex The index of the element to set (if the property is an array).
* @return true on success, false otherwise.
* @see ClearPropertyValue
*/
template<typename PropertyType, typename ValueType>
bool SetPropertyValue( PropertyType* Property, FProperty* Outer, void* Data, int32 ArrayIndex, const ValueType& Value )
{
if (void* Ptr = GetPropertyValuePtr(Property, Outer, Data, ArrayIndex))
{
*(ValueType*)Ptr = Value;
return true;
}
return false;
}
template<typename PropertyType, typename ContainerType, typename KeyType>
bool ReadNumericProperty(FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
{
typedef typename PropertyType::TCppType TCppType;
if (PropertyType* TypedProperty = CastField<PropertyType>(Property))
{
return SetPropertyValue(TypedProperty, Outer, Data, ArrayIndex, GetNumeric<TCppType>(Container, Key));
}
else
{
return false;
}
}
template<typename ContainerType, typename KeyType>
bool ReadBoolProperty(FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
{
if (FBoolProperty* BoolProperty = CastField<FBoolProperty>(Property))
{
return SetPropertyValue(BoolProperty, Outer, Data, ArrayIndex, GetNumeric<int>(Container, Key)!=0);
}
return false;
}
template<typename ContainerType, typename KeyType>
bool ReadJSFunctionProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
{
if (Container->GetType(Key) != VTYPE_DICTIONARY || !Property->IsA<FStructProperty>())
{
return false;
}
CefRefPtr<CefDictionaryValue> Dictionary = Container->GetDictionary(Key);
FStructProperty* StructProperty = CastField<FStructProperty>(Property);
if ( !StructProperty || StructProperty->Struct != FWebInterfaceJSFunction::StaticStruct())
{
return false;
}
FGuid CallbackID;
if (!FGuid::Parse(FString(WCHAR_TO_TCHAR(Dictionary->GetString("$id").ToWString().c_str())), CallbackID))
{
// Invalid GUID
return false;
}
FWebInterfaceJSFunction CallbackObject(Scripting, CallbackID);
return SetPropertyValue(StructProperty, Outer, Data, ArrayIndex, CallbackObject);
}
template<typename ContainerType, typename KeyType>
bool ReadStringProperty(FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
{
if (Container->GetType(Key) == VTYPE_STRING)
{
FString StringValue = WCHAR_TO_TCHAR(Container->GetString(Key).ToWString().c_str());
if (FStrProperty* StrProperty = CastField<FStrProperty>(Property))
{
return SetPropertyValue(StrProperty, Outer, Data, ArrayIndex, StringValue);
}
if (FNameProperty* NameProperty = CastField<FNameProperty>(Property))
{
return SetPropertyValue(NameProperty, Outer, Data, ArrayIndex, FName(*StringValue));
}
if (FTextProperty* TextProperty = CastField<FTextProperty>(Property))
{
return SetPropertyValue(TextProperty, Outer, Data, ArrayIndex, FText::FromString(StringValue));
}
if (FByteProperty* ByteProperty = CastField<FByteProperty>(Property))
{
if (!ByteProperty->Enum)
{
return false;
}
int32 Index = ByteProperty->Enum->GetIndexByNameString(StringValue);
if (Index == INDEX_NONE)
{
return false;
}
return SetPropertyValue(ByteProperty, Outer, Data, ArrayIndex, (uint8)ByteProperty->Enum->GetValueByIndex(Index));
}
if (FEnumProperty* EnumProperty = CastField<FEnumProperty>(Property))
{
int32 Index = EnumProperty->GetEnum()->GetIndexByNameString(StringValue);
if (Index == INDEX_NONE)
{
return false;
}
if (void* ElementPtr = GetPropertyValuePtr(EnumProperty, Outer, Data, ArrayIndex))
{
EnumProperty->GetUnderlyingProperty()->SetIntPropertyValue(ElementPtr, EnumProperty->GetEnum()->GetValueByIndex(Index));
return true;
}
return false;
}
}
return false;
}
template<typename ContainerType, typename KeyType>
bool ReadProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
{
return ReadBoolProperty(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadStringProperty(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FByteProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FInt8Property>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FInt16Property>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FIntProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FInt64Property>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FUInt16Property>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FUInt32Property>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FUInt64Property>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FFloatProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadNumericProperty<FDoubleProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
|| ReadJSFunctionProperty(Scripting, Property, Outer, Data, ArrayIndex, Container, Key);
}
}
TSharedPtr<ICefInterfaceContainerWalker> FCefInterfaceListValueWalker::GetNextToken(EStructDeserializerBackendTokens& OutToken, FString& PropertyName)
{
TSharedPtr<ICefInterfaceContainerWalker> Retval = SharedThis(this);
Index++;
if (Index == -1)
{
OutToken = EStructDeserializerBackendTokens::ArrayStart;
}
else if ( Index < List->GetSize() )
{
AssignTokenFromContainer(List, Index, OutToken, PropertyName, Retval);
PropertyName = FString();
}
else
{
OutToken = EStructDeserializerBackendTokens::ArrayEnd;
Retval = Parent;
}
return Retval;
}
bool FCefInterfaceListValueWalker::ReadProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex)
{
return ::ReadProperty(Scripting, Property, Outer, Data, ArrayIndex, List, Index);
}
TSharedPtr<ICefInterfaceContainerWalker> FCefInterfaceDictionaryValueWalker::GetNextToken(EStructDeserializerBackendTokens& OutToken, FString& PropertyName)
{
TSharedPtr<ICefInterfaceContainerWalker> Retval = SharedThis(this);
Index++;
if (Index == -1)
{
OutToken = EStructDeserializerBackendTokens::StructureStart;
}
else if ( Index < Keys.size() )
{
AssignTokenFromContainer(Dictionary, Keys[Index], OutToken, PropertyName, Retval);
PropertyName = WCHAR_TO_TCHAR(Keys[Index].ToWString().c_str());
}
else
{
OutToken = EStructDeserializerBackendTokens::StructureEnd;
Retval = Parent;
}
return Retval;
}
bool FCefInterfaceDictionaryValueWalker::ReadProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex)
{
return ::ReadProperty(Scripting, Property, Outer, Data, ArrayIndex, Dictionary, Keys[Index]);
}
/* IStructDeserializerBackend interface
*****************************************************************************/
const FString& FCEFInterfaceJSStructDeserializerBackend::GetCurrentPropertyName() const
{
return CurrentPropertyName;
}
FString FCEFInterfaceJSStructDeserializerBackend::GetDebugString() const
{
return CurrentPropertyName;
}
const FString& FCEFInterfaceJSStructDeserializerBackend::GetLastErrorMessage() const
{
return CurrentPropertyName;
}
bool FCEFInterfaceJSStructDeserializerBackend::GetNextToken( EStructDeserializerBackendTokens& OutToken )
{
if (Walker.IsValid())
{
Walker = Walker->GetNextToken(OutToken, CurrentPropertyName);
return true;
}
else
{
return false;
}
}
bool FCEFInterfaceJSStructDeserializerBackend::ReadProperty( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex )
{
return Walker->ReadProperty(Scripting, Property, Outer, Data, ArrayIndex);
}
void FCEFInterfaceJSStructDeserializerBackend::SkipArray()
{
EStructDeserializerBackendTokens Token;
int32 depth = 1;
while (GetNextToken(Token) && depth > 0)
{
switch (Token)
{
case EStructDeserializerBackendTokens::ArrayEnd:
depth --;
break;
case EStructDeserializerBackendTokens::ArrayStart:
depth ++;
break;
default:
break;
}
}
}
void FCEFInterfaceJSStructDeserializerBackend::SkipStructure()
{
EStructDeserializerBackendTokens Token;
int32 depth = 1;
while (GetNextToken(Token) && depth > 0)
{
switch (Token)
{
case EStructDeserializerBackendTokens::StructureEnd:
depth --;
break;
case EStructDeserializerBackendTokens::StructureStart:
depth ++;
break;
default:
break;
}
}
}
#endif