#include "SoftObjectWrapper.h" #include "V8Utils.h" #include "ObjectMapper.h" namespace PUERTS_NAMESPACE { v8::Local FSoftObjectWrapper::ToFunctionTemplate(v8::Isolate* Isolate) { auto Result = v8::FunctionTemplate::New(Isolate); Result->InstanceTemplate()->SetInternalFieldCount(6); Result->PrototypeTemplate()->Set( FV8Utils::ToV8String(Isolate, "LoadSynchronous"), v8::FunctionTemplate::New(Isolate, &LoadSynchronous)); Result->PrototypeTemplate()->Set(FV8Utils::ToV8String(Isolate, "Get"), v8::FunctionTemplate::New(Isolate, &Get)); return Result; } typedef UObject* (*FSoftObjectPtrObjectGetter)(FSoftObjectPtr* Ptr); static void GenericObjectGet(const v8::FunctionCallbackInfo& Info, FSoftObjectPtrObjectGetter Getter) { auto Isolate = Info.GetIsolate(); auto Context = Isolate->GetCurrentContext(); FSoftObjectPtr* Ptr = FV8Utils::GetPointerFast(Info.Holder()); if (!Ptr) { FV8Utils::ThrowException(Isolate, "passing a invalid object for FSoftObjectPtr"); return; } auto Obj = Getter(Ptr); if (Obj) { UClass* PropertyClass = nullptr; if (UObject* Object = FV8Utils::GetUObject(Info.Holder(), 1)) { if (FV8Utils::IsReleasedPtr(Object)) { FV8Utils::ThrowException(Isolate, "passing a invalid object"); return; } PropertyClass = Cast(Object); } UClass* MetaClass = nullptr; if (UObject* Object = FV8Utils::GetUObject(Info.Holder(), 2)) { if (FV8Utils::IsReleasedPtr(Object)) { FV8Utils::ThrowException(Isolate, "passing a invalid object"); return; } MetaClass = Cast(Object); } if (PropertyClass && !Obj->GetClass()->IsChildOf(PropertyClass)) { // FV8Utils::ThrowException(Isolate, "invalid type"); return; } if (MetaClass) { auto Class = Cast(Obj); if (!Class) { // FV8Utils::ThrowException(Isolate, "not a class"); return; } if (!Class->IsChildOf(MetaClass)) { // FV8Utils::ThrowException(Isolate, "not a expect class"); return; } } Info.GetReturnValue().Set(FV8Utils::IsolateData(Isolate)->FindOrAdd(Isolate, Context, Obj->GetClass(), Obj)); } } void FSoftObjectWrapper::LoadSynchronous(const v8::FunctionCallbackInfo& Info) { GenericObjectGet(Info, [](FSoftObjectPtr* Ptr) { return Ptr->LoadSynchronous(); }); } void FSoftObjectWrapper::Get(const v8::FunctionCallbackInfo& Info) { GenericObjectGet(Info, [](FSoftObjectPtr* Ptr) { return Ptr->Get(); }); } } // namespace PUERTS_NAMESPACE