From 1585c04ff5e20ea6440ea3a3fc8f177343af4a3c Mon Sep 17 00:00:00 2001 From: johnche Date: Tue, 15 Oct 2024 16:28:55 +0800 Subject: [PATCH] =?UTF-8?q?[unity]=E7=94=A8pesapi=5Fon=5Fclass=5Fnot=5Ffou?= =?UTF-8?q?nd=E5=AE=9E=E7=8E=B0lazyload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../puerts/xil2cpp/Puerts_il2cpp.cpp.txt | 18 +++++++++++++ .../core/upm/Runtime/Src/IL2Cpp/JsEnv.cs | 2 +- .../Runtime/Src/IL2Cpp/Native/NativeAPI.cs | 6 +++-- unity/native_src_il2cpp/Inc/JSClassRegister.h | 5 +--- .../native_src_il2cpp/Src/CppObjectMapper.cpp | 4 +-- .../native_src_il2cpp/Src/JSClassRegister.cpp | 25 ++++------------- unity/native_src_il2cpp/Src/PesapiV8Impl.cpp | 2 +- unity/native_src_il2cpp/Src/Puerts.cpp | 27 ------------------- 8 files changed, 32 insertions(+), 57 deletions(-) diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt index 0adddf5511..f8627a4763 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt @@ -158,6 +158,23 @@ static Il2CppClass *g_typeofPersistentObjectInfo; static Il2CppClass *g_typeofArrayBuffer; static Il2CppClass *g_typeofTypedValue; +static const MethodInfo *g_methodInfoRegisterNoThrow; +typedef void (*RegisterNoThrowFunc) (const void* typeId, bool includeNonPublic, const MethodInfo* methodInfo); +static RegisterNoThrowFunc g_registerNoThrow; + +static bool ClassNotFoundCallback(const void* typeId) +{ + g_registerNoThrow(typeId, false, g_methodInfoRegisterNoThrow); + return true; +} + +static void SetRegisterNoThrow(Il2CppReflectionMethod* method) +{ + g_registerNoThrow = (RegisterNoThrowFunc)GetMethodPointer(method); + g_methodInfoRegisterNoThrow = (const MethodInfo *)GetMethodInfoPointer(method); + pesapi_on_class_not_found(ClassNotFoundCallback); +} + Il2CppClass* GetReturnType(const MethodInfo* method) { if (kInvalidIl2CppMethodSlot != method->slot) { Class::Init(method->klass); @@ -1953,6 +1970,7 @@ void InitialPuerts(pesapi_func_ptr* func_array) InternalCalls::Add("PuertsIl2cpp.NativeAPI::InitialPapiEnvRef(System.IntPtr)", (Il2CppMethodPointer)puerts::InitialPapiEnvRef); InternalCalls::Add("PuertsIl2cpp.NativeAPI::CleanupPapiEnvRef(System.IntPtr)", (Il2CppMethodPointer)puerts::CleanupPapiEnvRef); InternalCalls::Add("Puerts.JSObject::GetJSObjectValue(System.String,System.Type)", (Il2CppMethodPointer)puerts::GetJSObjectValue); + InternalCalls::Add("PuertsIl2cpp.NativeAPI::SetRegisterNoThrow(System.Reflection.MethodBase)", (Il2CppMethodPointer)puerts::SetRegisterNoThrow); pesapi_init(func_array); } diff --git a/unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs b/unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs index 9244af08a4..9320c81b36 100644 --- a/unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs +++ b/unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs @@ -64,7 +64,7 @@ public JsEnv(ILoader loader, int debugPort = -1) PuertsIl2cpp.NativeAPI.SetLogCallback(PuertsIl2cpp.NativeAPI.Log); PuertsIl2cpp.NativeAPI.InitialPuerts(PuertsIl2cpp.NativeAPI.GetPesapiImpl()); tryLoadTypeMethodInfo = typeof(TypeRegister).GetMethod("RegisterNoThrow"); - PuertsIl2cpp.NativeAPI.SetTryLoadCallback(PuertsIl2cpp.NativeAPI.GetMethodInfoPointer(tryLoadTypeMethodInfo), PuertsIl2cpp.NativeAPI.GetMethodPointer(tryLoadTypeMethodInfo)); + PuertsIl2cpp.NativeAPI.SetRegisterNoThrow(tryLoadTypeMethodInfo); persistentObjectInfoType = typeof(Puerts.JSObject); PuertsIl2cpp.NativeAPI.SetGlobalType_TypedValue(typeof(TypedValue)); diff --git a/unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs b/unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs index d146e03af1..8849e0123f 100644 --- a/unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs +++ b/unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs @@ -95,8 +95,10 @@ public static void CleanupPapiEnvRef(IntPtr envRef) [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void SetObjectPool(IntPtr jsEnv, IntPtr objectPoolAddMethodInfo, IntPtr objectPoolAdd, IntPtr objectPoolRemoveMethodInfo, IntPtr objectPoolRemove, IntPtr objectPoolInstance); - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetTryLoadCallback(IntPtr tryLoadMethodInfo, IntPtr tryLoad); + [MethodImpl(MethodImplOptions.InternalCall)] + public static void SetRegisterNoThrow(MethodBase methodInfo) + { + } [MethodImpl(MethodImplOptions.InternalCall)] public static void SetObjectToGlobal(IntPtr envRef, string key, Object obj) diff --git a/unity/native_src_il2cpp/Inc/JSClassRegister.h b/unity/native_src_il2cpp/Inc/JSClassRegister.h index 17caa1543e..2008fe55e9 100644 --- a/unity/native_src_il2cpp/Inc/JSClassRegister.h +++ b/unity/native_src_il2cpp/Inc/JSClassRegister.h @@ -100,10 +100,7 @@ void JSENV_API SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* Con void JSENV_API ForeachRegisterClass(std::function); -JSENV_API const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad = false); - -typedef void (*LoadTypeFunc) (const void* typeId); -JSENV_API void SetLazyLoadCallback(LoadTypeFunc Callback); +JSENV_API const JSClassDefinition* FindClassByID(const void* TypeId); JSENV_API void OnClassNotFound(ClassNotFoundCallback Callback); diff --git a/unity/native_src_il2cpp/Src/CppObjectMapper.cpp b/unity/native_src_il2cpp/Src/CppObjectMapper.cpp index bb3d0ec49e..6688435154 100644 --- a/unity/native_src_il2cpp/Src/CppObjectMapper.cpp +++ b/unity/native_src_il2cpp/Src/CppObjectMapper.cpp @@ -118,7 +118,7 @@ v8::Local FCppObjectMapper::FindOrAddCppObject( if (!Template.IsEmpty()) { auto Result = Template->InstanceTemplate()->NewInstance(Context).ToLocalChecked(); - BindCppObject(Isolate, const_cast(FindClassByID(TypeId, true)), Ptr, Result, PassByPointer); + BindCppObject(Isolate, const_cast(LoadClassByID(TypeId)), Ptr, Result, PassByPointer); return Result; } else @@ -181,7 +181,7 @@ v8::Local FCppObjectMapper::GetTemplateOfClass(v8::Isolate auto Iter = TypeIdToTemplateMap.find(TypeId); if (Iter == TypeIdToTemplateMap.end()) { - auto ClassDefinition = FindClassByID(TypeId, true); + auto ClassDefinition = LoadClassByID(TypeId); if (!ClassDefinition) { return v8::Local(); diff --git a/unity/native_src_il2cpp/Src/JSClassRegister.cpp b/unity/native_src_il2cpp/Src/JSClassRegister.cpp index 27e32cdefd..cb81f8ef02 100644 --- a/unity/native_src_il2cpp/Src/JSClassRegister.cpp +++ b/unity/native_src_il2cpp/Src/JSClassRegister.cpp @@ -81,7 +81,7 @@ class JSClassRegister void ForeachRegisterClass(std::function); - const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad = false); + const JSClassDefinition* FindClassByID(const void* TypeId); void OnClassNotFound(ClassNotFoundCallback InCallback) { @@ -101,11 +101,6 @@ class JSClassRegister } return clsDef; } - - void SetLazyLoadCallback(LoadTypeFunc InCallback) - { - LazyLoad = InCallback; - } const JSClassDefinition* FindCppTypeClassByName(const std::string& Name); @@ -120,7 +115,6 @@ class JSClassRegister private: std::map CDataIdToClassDefinition; std::map CDataNameToClassDefinition; - LoadTypeFunc LazyLoad = nullptr; std::recursive_mutex RegInfoMutex; ClassNotFoundCallback ClassNotFoundCallback = nullptr; #if USING_IN_UNREAL_ENGINE @@ -226,15 +220,10 @@ void JSClassRegister::SetClassTypeInfo(const void* TypeId, const NamedFunctionIn } } -const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId, bool TryLazyLoad) +const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId) { std::lock_guard guard(RegInfoMutex); auto Iter = CDataIdToClassDefinition.find(TypeId); - if (Iter == CDataIdToClassDefinition.end() && TryLazyLoad && LazyLoad) - { - LazyLoad(TypeId); - Iter = CDataIdToClassDefinition.find(TypeId); - } if (Iter == CDataIdToClassDefinition.end()) { return nullptr; @@ -334,25 +323,21 @@ void ForeachRegisterClass(std::functionForeachRegisterClass(Callback); } -const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad) +const JSClassDefinition* FindClassByID(const void* TypeId) { - return GetJSClassRegister()->FindClassByID(TypeId, TryLazyLoad); + return GetJSClassRegister()->FindClassByID(TypeId); } void OnClassNotFound(ClassNotFoundCallback Callback) { GetJSClassRegister()->OnClassNotFound(Callback); } + const JSClassDefinition* LoadClassByID(const void* TypeId) { return GetJSClassRegister()->LoadClassByID(TypeId); } -void SetLazyLoadCallback(LoadTypeFunc Callback) -{ - GetJSClassRegister()->SetLazyLoadCallback(Callback); -} - const JSClassDefinition* FindCppTypeClassByName(const std::string& Name) { return GetJSClassRegister()->FindCppTypeClassByName(Name); diff --git a/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp b/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp index 4f9bde957d..271b10a643 100644 --- a/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp +++ b/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp @@ -1017,7 +1017,7 @@ MSVC_PRAGMA(warning(pop)) void* pesapi_load_class_data(const void* type_id, bool callback_if_not_found) { - auto clsDef = puerts::FindClassByID(type_id, callback_if_not_found); + auto clsDef = callback_if_not_found ? puerts::LoadClassByID(type_id) : puerts::FindClassByID(type_id); return clsDef ? clsDef->Data : nullptr; } diff --git a/unity/native_src_il2cpp/Src/Puerts.cpp b/unity/native_src_il2cpp/Src/Puerts.cpp index 1f400991a8..b1fd05dbed 100644 --- a/unity/native_src_il2cpp/Src/Puerts.cpp +++ b/unity/native_src_il2cpp/Src/Puerts.cpp @@ -42,15 +42,6 @@ static LogCallback GLogCallback = nullptr; typedef void (*LazyLoadTypeFunc) (const void* typeId, bool includeNonPublic, void* method); -void* GTryLoadTypeMethodInfo = nullptr; - -LazyLoadTypeFunc GTryLazyLoadType = nullptr; - -static void LazyLoad(const void* typeId) -{ - GTryLazyLoadType(typeId, false, GTryLoadTypeMethodInfo); -} - void PLog(LogLevel Level, const std::string Fmt, ...) { static char SLogBuffer[1024]; @@ -65,17 +56,6 @@ void PLog(LogLevel Level, const std::string Fmt, ...) } } -static void* GetJsClassInfo(const void* TypeId, bool TryLazyLoad) -{ - auto ClassDefinition = FindClassByID(TypeId, TryLazyLoad); - if (!ClassDefinition) - { - return nullptr; - } - - return ClassDefinition->Data; -} - struct JSEnv { JSEnv() @@ -204,13 +184,6 @@ V8_EXPORT void SetObjectPool(puerts::JSEnv* jsEnv, void* ObjectPoolAddMethodInfo jsEnv->CppObjectMapper.ObjectPoolInstance = ObjectPoolInstance; } -V8_EXPORT void SetTryLoadCallback(void* tryLoadMethodInfo, puerts::LazyLoadTypeFunc tryLoad) -{ - puerts::GTryLoadTypeMethodInfo = tryLoadMethodInfo; - puerts::GTryLazyLoadType = tryLoad; - puerts::SetLazyLoadCallback(puerts::LazyLoad); -} - V8_EXPORT void CreateInspector(puerts::JSEnv* jsEnv, int32_t Port) { jsEnv->BackendEnv.CreateInspector(jsEnv->MainIsolate, &jsEnv->MainContext, Port);