NativeInterfaceJSStructDeserializerBackend.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Engine/Source/Runtime/WebBrowser/Private/Native/NativeJSStructDeserializerBackend.cpp
  2. #include "NativeInterfaceJSStructDeserializerBackend.h"
  3. #include "NativeInterfaceJSScripting.h"
  4. #include "UObject/UnrealType.h"
  5. #include "Templates/Casts.h"
  6. namespace NativeInterfaceFuncs
  7. {
  8. // @todo: this function is copied from CEFJSStructDeserializerBackend.cpp. Move shared utility code to a common header file
  9. /**
  10. * Sets the value of the given property.
  11. *
  12. * @param Property The property to set.
  13. * @param Outer The property that contains the property to be set, if any.
  14. * @param Data A pointer to the memory holding the property's data.
  15. * @param ArrayIndex The index of the element to set (if the property is an array).
  16. * @return true on success, false otherwise.
  17. * @see ClearPropertyValue
  18. */
  19. template<typename FPropertyType, typename PropertyType>
  20. bool SetPropertyValue( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, const PropertyType& Value )
  21. {
  22. PropertyType* ValuePtr = nullptr;
  23. FArrayProperty* ArrayProperty = CastField<FArrayProperty>(Outer);
  24. if (ArrayProperty != nullptr)
  25. {
  26. if (ArrayProperty->Inner != Property)
  27. {
  28. return false;
  29. }
  30. FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->template ContainerPtrToValuePtr<void>(Data));
  31. int32 Index = ArrayHelper.AddValue();
  32. ValuePtr = (PropertyType*)ArrayHelper.GetRawPtr(Index);
  33. }
  34. else
  35. {
  36. FPropertyType* TypedProperty = CastField<FPropertyType>(Property);
  37. if (TypedProperty == nullptr || ArrayIndex >= TypedProperty->ArrayDim)
  38. {
  39. return false;
  40. }
  41. ValuePtr = TypedProperty->template ContainerPtrToValuePtr<PropertyType>(Data, ArrayIndex);
  42. }
  43. if (ValuePtr == nullptr)
  44. {
  45. return false;
  46. }
  47. *ValuePtr = Value;
  48. return true;
  49. }
  50. }
  51. bool FNativeInterfaceJSStructDeserializerBackend::ReadProperty( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex )
  52. {
  53. switch (GetLastNotation())
  54. {
  55. case EJsonNotation::String:
  56. {
  57. if (Property->IsA<FStructProperty>())
  58. {
  59. FStructProperty* StructProperty = CastField<FStructProperty>(Property);
  60. if ( StructProperty->Struct == FWebInterfaceJSFunction::StaticStruct())
  61. {
  62. FGuid CallbackID;
  63. if (!FGuid::Parse(GetReader()->GetValueAsString(), CallbackID))
  64. {
  65. return false;
  66. }
  67. FWebInterfaceJSFunction CallbackObject(Scripting, CallbackID);
  68. return NativeInterfaceFuncs::SetPropertyValue<FStructProperty, FWebInterfaceJSFunction>(Property, Outer, Data, ArrayIndex, CallbackObject);
  69. }
  70. }
  71. }
  72. break;
  73. }
  74. // If we reach this, default to parent class behavior
  75. return FJsonStructDeserializerBackend::ReadProperty(Property, Outer, Data, ArrayIndex);
  76. }
  77. FNativeInterfaceJSStructDeserializerBackend::FNativeInterfaceJSStructDeserializerBackend(FNativeInterfaceJSScriptingRef InScripting, FMemoryReader& Reader)
  78. : FJsonStructDeserializerBackend(Reader)
  79. , Scripting(InScripting)
  80. {
  81. }