123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #include "CEF/CEFInterfaceJSStructSerializerBackend.h"
- #if WITH_CEF3
- #include "UObject/EnumProperty.h"
- #include "UObject/TextProperty.h"
- #include "UObject/PropertyPortFlags.h"
- #include "Misc/CommandLine.h"
- static FString GetBindingName(const TSharedPtr<FCEFInterfaceJSScripting>& Scripting, const FProperty* ValueProperty)
- {
-
- static const bool bIsKairos = FParse::Param(FCommandLine::Get(), TEXT("KairosOnly"));
- if (bIsKairos)
- {
-
- return ValueProperty->GetName();
- }
- else
- {
- return Scripting->GetBindingName(ValueProperty);
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::AddNull(const FStructSerializerState& State)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetNull(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)));
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetNull(Current.ListValue->GetSize());
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, bool Value)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetBool(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Value);
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetBool(Current.ListValue->GetSize(), Value);
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, int32 Value)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetInt(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Value);
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetInt(Current.ListValue->GetSize(), Value);
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, double Value)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetDouble(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Value);
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetDouble(Current.ListValue->GetSize(), Value);
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, FString Value)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetString(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), TCHAR_TO_WCHAR(*Value));
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetString(Current.ListValue->GetSize(), TCHAR_TO_WCHAR(*Value));
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::Add(const FStructSerializerState& State, UObject* Value)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetDictionary(TCHAR_TO_WCHAR(*GetBindingName(Scripting, State.ValueProperty)), Scripting->ConvertObject(Value));
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetDictionary(Current.ListValue->GetSize(), Scripting->ConvertObject(Value));
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::BeginArray(const FStructSerializerState& State)
- {
- CefRefPtr<CefListValue> ListValue = CefListValue::Create();
- Stack.Push(StackItem(GetBindingName(Scripting, State.ValueProperty), ListValue));
- }
- void FCEFInterfaceJSStructSerializerBackend::BeginStructure(const FStructSerializerState& State)
- {
- if (State.KeyProperty != nullptr)
- {
- FString KeyString;
- State.KeyProperty->ExportTextItem_Direct(KeyString, State.KeyData, nullptr, nullptr, PPF_None);
- CefRefPtr<CefDictionaryValue> DictionaryValue = CefDictionaryValue::Create();
- Stack.Push(StackItem(KeyString, DictionaryValue));
- }
- else if (State.ValueProperty != nullptr)
- {
- CefRefPtr<CefDictionaryValue> DictionaryValue = CefDictionaryValue::Create();
- Stack.Push(StackItem(GetBindingName(Scripting, State.ValueProperty), DictionaryValue));
- }
- else
- {
- Result = CefDictionaryValue::Create();
- Stack.Push(StackItem(FString(), Result));
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::EndArray(const FStructSerializerState& )
- {
- StackItem Previous = Stack.Pop();
- check(Previous.Kind == StackItem::STYPE_LIST);
- check(Stack.Num() > 0);
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetList(TCHAR_TO_WCHAR(*Previous.Name), Previous.ListValue);
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetList(Current.ListValue->GetSize(), Previous.ListValue);
- break;
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::EndStructure(const FStructSerializerState& )
- {
- StackItem Previous = Stack.Pop();
- check(Previous.Kind == StackItem::STYPE_DICTIONARY);
- if (Stack.Num() > 0)
- {
- StackItem& Current = Stack.Top();
- switch (Current.Kind) {
- case StackItem::STYPE_DICTIONARY:
- Current.DictionaryValue->SetDictionary(TCHAR_TO_WCHAR(*Previous.Name), Previous.DictionaryValue);
- break;
- case StackItem::STYPE_LIST:
- Current.ListValue->SetDictionary(Current.ListValue->GetSize(), Previous.DictionaryValue);
- break;
- }
- }
- else
- {
- check(Result == Previous.DictionaryValue);
- }
- }
- void FCEFInterfaceJSStructSerializerBackend::WriteComment(const FString& Comment)
- {
-
- }
- void FCEFInterfaceJSStructSerializerBackend::WriteProperty(const FStructSerializerState& State, int32 ArrayIndex)
- {
-
- if (State.FieldType == FBoolProperty::StaticClass())
- {
- Add(State, CastFieldChecked<FBoolProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
-
- else if (State.FieldType == FEnumProperty::StaticClass())
- {
- FEnumProperty* EnumProperty = CastFieldChecked<FEnumProperty>(State.ValueProperty);
- Add(State, EnumProperty->GetEnum()->GetNameStringByValue(EnumProperty->GetUnderlyingProperty()->GetSignedIntPropertyValue(EnumProperty->ContainerPtrToValuePtr<void>(State.ValueData, ArrayIndex))));
- }
- else if (State.FieldType == FByteProperty::StaticClass())
- {
- FByteProperty* ByteProperty = CastFieldChecked<FByteProperty>(State.ValueProperty);
- if (ByteProperty->IsEnum())
- {
- Add(State, ByteProperty->Enum->GetNameStringByValue(ByteProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)));
- }
- else
- {
- Add(State, (double)ByteProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- }
-
- else if (State.FieldType == FDoubleProperty::StaticClass())
- {
- Add(State, CastFieldChecked<FDoubleProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FFloatProperty::StaticClass())
- {
- Add(State, CastFieldChecked<FFloatProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
-
- else if (State.FieldType == FIntProperty::StaticClass())
- {
- Add(State, (int32)CastFieldChecked<FIntProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FInt8Property::StaticClass())
- {
- Add(State, (int32)CastFieldChecked<FInt8Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FInt16Property::StaticClass())
- {
- Add(State, (int32)CastFieldChecked<FInt16Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FInt64Property::StaticClass())
- {
- Add(State, (double)CastFieldChecked<FInt64Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
-
- else if (State.FieldType == FUInt16Property::StaticClass())
- {
- Add(State, (int32)CastFieldChecked<FUInt16Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FUInt32Property::StaticClass())
- {
- Add(State, (double)CastFieldChecked<FUInt32Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FUInt64Property::StaticClass())
- {
- Add(State, (double)CastFieldChecked<FUInt64Property>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
-
- else if (State.FieldType == FNameProperty::StaticClass())
- {
- Add(State, CastFieldChecked<FNameProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex).ToString());
- }
- else if (State.FieldType == FStrProperty::StaticClass())
- {
- Add(State, CastFieldChecked<FStrProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
- else if (State.FieldType == FTextProperty::StaticClass())
- {
- Add(State, CastFieldChecked<FTextProperty>(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex).ToString());
- }
-
- else if (FClassProperty* ClassProperty = CastField<FClassProperty>(State.ValueProperty))
- {
- Add(State, ClassProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)->GetPathName());
- }
- else if (FObjectProperty* ObjectProperty = CastField<FObjectProperty>(State.ValueProperty))
- {
- Add(State, ObjectProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex));
- }
-
- else
- {
- GLog->Logf(ELogVerbosity::Warning, TEXT("FCEFJSStructSerializerBackend: Property %s cannot be serialized, because its type (%s) is not supported"), *State.ValueProperty->GetName(), *State.ValueType->GetName());
- }
- }
- #endif
|