Skip to content

Commit

Permalink
[unity]重构,函数名字更改,由于后续需要传C#对象,P-Invoke改为InternalCall
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 15, 2024
1 parent d894017 commit d443a37
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,25 @@ struct ScriptObjectsRefsMgr
}
};

puerts::ScriptObjectsRefsMgr* InitialPapiEnvRef(pesapi_env_ref envRef)
{
puerts::AutoValueScope ValueScope(envRef);
auto env = pesapi_get_env_from_ref(envRef);
auto mgr = new puerts::ScriptObjectsRefsMgr(envRef);
pesapi_set_env_private(env, mgr);
return mgr;
}

void CleanupPapiEnvRef(pesapi_env_ref envRef)
{
puerts::AutoValueScope ValueScope(envRef);
auto env = pesapi_get_env_from_ref(envRef);
auto scriptObjectsRefsMgr = (puerts::ScriptObjectsRefsMgr*)pesapi_get_env_private(env);
scriptObjectsRefsMgr->CleanupPendingKillScriptObjects();
delete scriptObjectsRefsMgr;
pesapi_release_env_ref(envRef);
}

}

#ifdef __cplusplus
Expand All @@ -1921,29 +1940,12 @@ void InitialPuerts(pesapi_func_ptr* func_array)
InternalCalls::Add("PuertsIl2cpp.NativeAPI::SetObjectToGlobal(System.IntPtr,System.String,System.Object)", (Il2CppMethodPointer)puerts::SetObjectToGlobal);
InternalCalls::Add("PuertsIl2cpp.NativeAPI::TypeIdToType(System.IntPtr)", (Il2CppMethodPointer)puerts::TypeIdToType);
InternalCalls::Add("PuertsIl2cpp.NativeAPI::GetModuleExecutor(System.IntPtr,System.Type)", (Il2CppMethodPointer)puerts::GetModuleExecutor);
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);
pesapi_init(func_array);
}

puerts::ScriptObjectsRefsMgr* CreateScriptObjectsRefsManager(pesapi_env_ref envRef)
{
puerts::AutoValueScope ValueScope(envRef);
auto env = pesapi_get_env_from_ref(envRef);
auto mgr = new puerts::ScriptObjectsRefsMgr(envRef);
pesapi_set_env_private(env, mgr);
return mgr;
}

void DestroyPapiEnvRefAndScriptObjectsRefsManager(pesapi_env_ref envRef)
{
puerts::AutoValueScope ValueScope(envRef);
auto env = pesapi_get_env_from_ref(envRef);
auto scriptObjectsRefsMgr = (puerts::ScriptObjectsRefsMgr*)pesapi_get_env_private(env);
scriptObjectsRefsMgr->CleanupPendingKillScriptObjects();
delete scriptObjectsRefsMgr;
pesapi_release_env_ref(envRef);
}

void AddPendingKillScriptObjects(puerts::ScriptObjectsRefsMgr* mgr, pesapi_value_ref valueRef)
{
pesapi_env_ref envRef = pesapi_get_ref_associated_env(valueRef);
Expand Down
4 changes: 2 additions & 2 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public JsEnv(ILoader loader, int debugPort = -1)

nativeJsEnv = PuertsIl2cpp.NativeAPI.CreateNativeJSEnv();
nativePesapiEnv = PuertsIl2cpp.NativeAPI.GetPapiEnvRef(nativeJsEnv);
nativeScriptObjectsRefsMgr = PuertsIl2cpp.NativeAPI.CreateScriptObjectsRefsManager(nativePesapiEnv);
nativeScriptObjectsRefsMgr = PuertsIl2cpp.NativeAPI.InitialPapiEnvRef(nativePesapiEnv);

//PuertsIl2cpp.NativeAPI.SetObjectPool(objectPool, typeof(PuertsIl2cpp.ObjectPool).GetMethod("Add")); //TODO: remove....
objectPoolAddMethodInfo = typeof(PuertsIl2cpp.ObjectPool).GetMethod("Add");
Expand Down Expand Up @@ -238,7 +238,7 @@ protected virtual void Dispose(bool dispose)
lock (this)
{
if (disposed) return;
PuertsIl2cpp.NativeAPI.DestroyPapiEnvRefAndScriptObjectsRefsManager(nativePesapiEnv);
PuertsIl2cpp.NativeAPI.CleanupPapiEnvRef(nativePesapiEnv);
PuertsIl2cpp.NativeAPI.DestroyNativeJSEnv(nativeJsEnv);
disposed = true;
}
Expand Down
14 changes: 10 additions & 4 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ public class NativeAPI
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetPapiEnvRef(IntPtr jsEnv);

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateScriptObjectsRefsManager(IntPtr envRef);
[MethodImpl(MethodImplOptions.InternalCall)]
public static IntPtr InitialPapiEnvRef(IntPtr envRef)
{
throw new NotImplementedException();
}

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyPapiEnvRefAndScriptObjectsRefsManager(IntPtr envRef);
[MethodImpl(MethodImplOptions.InternalCall)]
public static void CleanupPapiEnvRef(IntPtr envRef)
{
throw new NotImplementedException();
}

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateCSharpTypeInfo(string name, IntPtr type_id, IntPtr super_type_id, IntPtr klass, bool isValueType, bool isDelegate, string delegateSignature);
Expand Down

0 comments on commit d443a37

Please sign in to comment.