CEFInterfaceJSStructSerializerBackend.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Engine/Source/Runtime/WebBrowser/Private/CEF/CEFJSStructSerializerBackend.cpp
  2. #include "CEF/CEFInterfaceJSStructSerializerBackend.h"
  3. #if WITH_CEF3
  4. #include "UObject/EnumProperty.h"
  5. #include "UObject/TextProperty.h"
  6. #include "UObject/PropertyPortFlags.h"
  7. #include "Misc/CommandLine.h"
  8. static FString GetBindingName(const TSharedPtr<FCEFInterfaceJSScripting>& Scripting, const FProperty* ValueProperty)
  9. {
  10. //@todo samz - HACK
  11. static const bool bIsKairos = FParse::Param(FCommandLine::Get(), TEXT("KairosOnly"));
  12. if (bIsKairos)
  13. {
  14. // skip lowercasing property field names for compatibility with FNativeJSStructSerializerBackend/FMobileJSStructSerializerBackend
  15. return ValueProperty->GetName();
  16. }
  17. else
  18. {
  19. return Scripting->GetBindingName(ValueProperty);
  20. }
  21. }
  22. /* Private methods
  23. *****************************************************************************/
  24. void FCEFInterfaceJSStructSerializerBackend::AddNull(const FStructSerializerState& State)
  25. {
  26. StackItem& Current = Stack.Top();
  27. switch (Current.Kind) {
  28. case StackItem::STYPE_DICTIONARY:
  29. Current.DictionaryValue->SetNull(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)));
  30. break;
  31. case StackItem::STYPE_LIST:
  32. Current.ListValue->SetNull(Current.ListValue->GetSize());
  33. break;
  34. }
  35. }
  36. void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, bool Value)
  37. {
  38. StackItem& Current = Stack.Top();
  39. switch (Current.Kind) {
  40. case StackItem::STYPE_DICTIONARY:
  41. Current.DictionaryValue->SetBool(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Value);
  42. break;
  43. case StackItem::STYPE_LIST:
  44. Current.ListValue->SetBool(Current.ListValue->GetSize(), Value);
  45. break;
  46. }
  47. }
  48. void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, int32 Value)
  49. {
  50. StackItem& Current = Stack.Top();
  51. switch (Current.Kind) {
  52. case StackItem::STYPE_DICTIONARY:
  53. Current.DictionaryValue->SetInt(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Value);
  54. break;
  55. case StackItem::STYPE_LIST:
  56. Current.ListValue->SetInt(Current.ListValue->GetSize(), Value);
  57. break;
  58. }
  59. }
  60. void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, double Value)
  61. {
  62. StackItem& Current = Stack.Top();
  63. switch (Current.Kind) {
  64. case StackItem::STYPE_DICTIONARY:
  65. Current.DictionaryValue->SetDouble(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Value);
  66. break;
  67. case StackItem::STYPE_LIST:
  68. Current.ListValue->SetDouble(Current.ListValue->GetSize(), Value);
  69. break;
  70. }
  71. }
  72. void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, FString Value)
  73. {
  74. StackItem& Current = Stack.Top();
  75. switch (Current.Kind) {
  76. case StackItem::STYPE_DICTIONARY:
  77. Current.DictionaryValue->SetString(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), TCHAR_TO_WCHAR(*Value));
  78. break;
  79. case StackItem::STYPE_LIST:
  80. Current.ListValue->SetString(Current.ListValue->GetSize(), TCHAR_TO_WCHAR(*Value));
  81. break;
  82. }
  83. }
  84. void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, UObject* Value)
  85. {
  86. StackItem& Current = Stack.Top();
  87. switch (Current.Kind) {
  88. case StackItem::STYPE_DICTIONARY:
  89. Current.DictionaryValue->SetDictionary(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Scripting->ConvertObject(Value));
  90. break;
  91. case StackItem::STYPE_LIST:
  92. Current.ListValue->SetDictionary(Current.ListValue->GetSize(), Scripting->ConvertObject(Value));
  93. break;
  94. }
  95. }
  96. /* IStructSerializerBackend interface
  97. *****************************************************************************/
  98. void FCEFInterfaceJSStructSerializerBackend::BeginArray(const FStructSerializerState& State)
  99. {
  100. CefRefPtr<CefListValue> ListValue = CefListValue::Create();
  101. Stack.Push(StackItem(GetBindingName(Scripting, State.ValueProperty), ListValue));
  102. }
  103. void FCEFInterfaceJSStructSerializerBackend::BeginStructure(const FStructSerializerState& State)
  104. {
  105. if (State.KeyProperty != nullptr)
  106. {
  107. FString KeyString;
  108. State.KeyProperty->ExportTextItem_Direct(KeyString, State.KeyData, nullptr, nullptr, PPF_None);
  109. CefRefPtr<CefDictionaryValue> DictionaryValue = CefDictionaryValue::Create();
  110. Stack.Push(StackItem(KeyString, DictionaryValue));
  111. }
  112. else if (State.ValueProperty != nullptr)
  113. {
  114. CefRefPtr<CefDictionaryValue> DictionaryValue = CefDictionaryValue::Create();
  115. Stack.Push(StackItem(GetBindingName(Scripting, State.ValueProperty), DictionaryValue));
  116. }
  117. else
  118. {
  119. Result = CefDictionaryValue::Create();
  120. Stack.Push(StackItem(FString(), Result));
  121. }
  122. }
  123. void FCEFInterfaceJSStructSerializerBackend::EndArray(const FStructSerializerState& /*State*/)
  124. {
  125. StackItem Previous = Stack.Pop();
  126. check(Previous.Kind == StackItem::STYPE_LIST);
  127. check(Stack.Num() > 0); // The root level object is always a struct
  128. StackItem& Current = Stack.Top();
  129. switch (Current.Kind) {
  130. case StackItem::STYPE_DICTIONARY:
  131. Current.DictionaryValue->SetList(TCHAR_TO_WCHAR(*Previous.Name), Previous.ListValue);
  132. break;
  133. case StackItem::STYPE_LIST:
  134. Current.ListValue->SetList(Current.ListValue->GetSize(), Previous.ListValue);
  135. break;
  136. }
  137. }
  138. void FCEFInterfaceJSStructSerializerBackend::EndStructure(const FStructSerializerState& /*State*/)
  139. {
  140. StackItem Previous = Stack.Pop();
  141. check(Previous.Kind == StackItem::STYPE_DICTIONARY);
  142. if (Stack.Num() > 0)
  143. {
  144. StackItem& Current = Stack.Top();
  145. switch (Current.Kind) {
  146. case StackItem::STYPE_DICTIONARY:
  147. Current.DictionaryValue->SetDictionary(TCHAR_TO_WCHAR(*Previous.Name), Previous.DictionaryValue);
  148. break;
  149. case StackItem::STYPE_LIST:
  150. Current.ListValue->SetDictionary(Current.ListValue->GetSize(), Previous.DictionaryValue);
  151. break;
  152. }
  153. }
  154. else
  155. {
  156. check(Result == Previous.DictionaryValue);
  157. }
  158. }
  159. void FCEFInterfaceJSStructSerializerBackend::WriteComment(const FString& Comment)
  160. {
  161. // Cef values do not support comments
  162. }
  163. void FCEFInterfaceJSStructSerializerBackend::WriteProperty(const FStructSerializerState& State, int32 ArrayIndex)
  164. {
  165. // booleans
  166. if (State.FieldType == FBoolProperty::StaticClass())
  167. {
  168. Add(State, CastFieldChecked<FBoolProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  169. }
  170. // unsigned bytes & enumerations
  171. else if (State.FieldType == FEnumProperty::StaticClass())
  172. {
  173. FEnumProperty* EnumProperty = CastFieldChecked<FEnumProperty>(State.ValueProperty);
  174. Add(State, EnumProperty->GetEnum()->GetNameStringByValue(EnumProperty->GetUnderlyingProperty()->GetSignedIntPropertyValue(EnumProperty->ContainerPtrToValuePtr<void>(State.ValueData, ArrayIndex))));
  175. }
  176. else if (State.FieldType == FByteProperty::StaticClass())
  177. {
  178. FByteProperty* ByteProperty = CastFieldChecked<FByteProperty>(State.ValueProperty);
  179. if (ByteProperty->IsEnum())
  180. {
  181. Add(State, ByteProperty->Enum->GetNameStringByValue(ByteProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)));
  182. }
  183. else
  184. {
  185. Add(State, (double)ByteProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  186. }
  187. }
  188. // floating point numbers
  189. else if (State.FieldType == FDoubleProperty::StaticClass())
  190. {
  191. Add(State, CastFieldChecked<FDoubleProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  192. }
  193. else if (State.FieldType == FFloatProperty::StaticClass())
  194. {
  195. Add(State, CastFieldChecked<FFloatProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  196. }
  197. // signed integers
  198. else if (State.FieldType == FIntProperty::StaticClass())
  199. {
  200. Add(State, (int32)CastFieldChecked<FIntProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  201. }
  202. else if (State.FieldType == FInt8Property::StaticClass())
  203. {
  204. Add(State, (int32)CastFieldChecked<FInt8Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  205. }
  206. else if (State.FieldType == FInt16Property::StaticClass())
  207. {
  208. Add(State, (int32)CastFieldChecked<FInt16Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  209. }
  210. else if (State.FieldType == FInt64Property::StaticClass())
  211. {
  212. Add(State, (double)CastFieldChecked<FInt64Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  213. }
  214. // unsigned integers
  215. else if (State.FieldType == FUInt16Property::StaticClass())
  216. {
  217. Add(State, (int32)CastFieldChecked<FUInt16Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  218. }
  219. else if (State.FieldType == FUInt32Property::StaticClass())
  220. {
  221. Add(State, (double)CastFieldChecked<FUInt32Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  222. }
  223. else if (State.FieldType == FUInt64Property::StaticClass())
  224. {
  225. Add(State, (double)CastFieldChecked<FUInt64Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  226. }
  227. // names & strings
  228. else if (State.FieldType == FNameProperty::StaticClass())
  229. {
  230. Add(State, CastFieldChecked<FNameProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex).ToString());
  231. }
  232. else if (State.FieldType == FStrProperty::StaticClass())
  233. {
  234. Add(State, CastFieldChecked<FStrProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  235. }
  236. else if (State.FieldType == FTextProperty::StaticClass())
  237. {
  238. Add(State, CastFieldChecked<FTextProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex).ToString());
  239. }
  240. // classes & objects
  241. else if (FClassProperty* ClassProperty = CastField<FClassProperty>(State.ValueProperty))
  242. {
  243. Add(State, ClassProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)->GetPathName());
  244. }
  245. else if (FObjectProperty* ObjectProperty = CastField<FObjectProperty>(State.ValueProperty))
  246. {
  247. Add(State, ObjectProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
  248. }
  249. // unsupported property type
  250. else
  251. {
  252. GLog->Logf(ELogVerbosity::Warning, TEXT("FCEFJSStructSerializerBackend: Property %s cannot be serialized, because its type (%s) is not supported"), *State.ValueProperty->GetName(), *State.ValueType->GetName());
  253. }
  254. }
  255. #endif