Skip to content

Commit

Permalink
[unity]SetObjectToGlobal放plugin代码会依赖il2cpp实现,改为放Puerts_il2cpp里实现
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 14, 2024
1 parent 59c297f commit 10657a5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,21 @@ Il2CppObject* EvalInternal(intptr_t ptr, Il2CppArray * __code, Il2CppString* __p
return nullptr;
}

void SetObjectToGlobal(pesapi_env_ref envRef, Il2CppString* __key, RuntimeObject *obj)
{
AutoValueScope ValueScope(envRef);
auto env = pesapi_get_env_from_ref(envRef);

const Il2CppChar* utf16 = il2cpp::utils::StringUtils::GetChars(__key);
std::string key = il2cpp::utils::StringUtils::Utf16ToUtf8(utf16);

pesapi_value global = pesapi_global(env);
if (global)
{
pesapi_set_property(env, global, key.c_str(), CSRefToJsValue(env, obj->klass, obj));
}
}

Il2CppObject* GetModuleExecutor(intptr_t ptr, Il2CppReflectionType* rtype)
{
pesapi_env_ref envRef = reinterpret_cast<pesapi_env_ref>(ptr);
Expand Down Expand Up @@ -1889,6 +1904,7 @@ void InitialPuerts(pesapi_func_ptr* func_array)
InternalCalls::Add("PuertsIl2cpp.NativeAPI::SetGlobalType_ArrayBuffer(System.Type)", (Il2CppMethodPointer)puerts::SetGlobalType_ArrayBuffer);
InternalCalls::Add("PuertsIl2cpp.NativeAPI::GetUnityExports()", (Il2CppMethodPointer)puerts::GetUnityExports);
InternalCalls::Add("PuertsIl2cpp.NativeAPI::EvalInternal(System.IntPtr,System.Byte[],System.String,System.Type)", (Il2CppMethodPointer)puerts::EvalInternal);
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("Puerts.JSObject::GetJSObjectValue(System.String,System.Type)", (Il2CppMethodPointer)puerts::GetJSObjectValue);
Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public JsEnv(ILoader loader, int debugPort = -1)
PuertsIl2cpp.NativeAPI.GetMethodInfoPointer(objectPoolRemoveMethodInfo), PuertsIl2cpp.NativeAPI.GetMethodPointer(objectPoolRemoveMethodInfo),
PuertsIl2cpp.NativeAPI.GetObjectPointer(objectPool));

PuertsIl2cpp.NativeAPI.SetObjectToGlobal(nativeJsEnv, "jsEnv", PuertsIl2cpp.NativeAPI.GetObjectPointer(this));
PuertsIl2cpp.NativeAPI.SetObjectToGlobal(nativePesapiEnv, "jsEnv", this);

//可以DISABLE掉自动注册,通过手动调用PuertsStaticWrap.AutoStaticCodeRegister.Register(jsEnv)来注册
#if !DISABLE_AUTO_REGISTER
Expand Down
7 changes: 5 additions & 2 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ public class NativeAPI
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetTryLoadCallback(IntPtr tryLoadMethodInfo, IntPtr tryLoad);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetObjectToGlobal(IntPtr jsEnv, string key, IntPtr objPtr);
[MethodImpl(MethodImplOptions.InternalCall)]
public static void SetObjectToGlobal(IntPtr envRef, string key, Object obj)
{
throw new NotImplementedException();
}

[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
public static extern void AddPendingKillScriptObjects(IntPtr jsEnv, IntPtr valueRef);
Expand Down
18 changes: 0 additions & 18 deletions unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,24 +272,6 @@ V8_EXPORT void SetTryLoadCallback(void* tryLoadMethodInfo, puerts::LazyLoadTypeF
puerts::SetLazyLoadCallback(puerts::LazyLoad);
}

V8_EXPORT void SetObjectToGlobal(puerts::JSEnv* jsEnv, const char* key, void *obj)
{
if (obj)
{
v8::Isolate* Isolate = jsEnv->MainIsolate;
#ifdef THREAD_SAFE
v8::Locker Locker(Isolate);
#endif
v8::Isolate::Scope IsolateScope(Isolate);
v8::HandleScope HandleScope(Isolate);
v8::Local<v8::Context> Context = jsEnv->MainContext.Get(Isolate);
v8::Context::Scope ContextScope(Context);

void* klass = *static_cast<void**>(obj); //TODO: 这是Il2cpp内部实现
Context->Global()->Set(Context, v8::String::NewFromUtf8(Isolate, key).ToLocalChecked(), puerts::DataTransfer::FindOrAddCData(Isolate, Context, klass, obj, true)).Check();
}
}

V8_EXPORT void CreateInspector(puerts::JSEnv* jsEnv, int32_t Port)
{
jsEnv->BackendEnv.CreateInspector(jsEnv->MainIsolate, &jsEnv->MainContext, Port);
Expand Down

0 comments on commit 10657a5

Please sign in to comment.