Skip to content

Commit

Permalink
[unity]unity6的gchandle类型从int改为了void*,il2cpp优化这做下兼容,fix #1912
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Nov 14, 2024
1 parent f5cc8d7 commit 82783ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,11 @@ static pesapi_value GetPObjectRefInfoValue(struct pesapi_ffi* apis, pesapi_env e
return apis->get_value_from_ref(env, objectInfo->ValueRef);
}

static int32_t* FindOrCreateHandleStoreOfValue(struct pesapi_ffi* apis, pesapi_env env, pesapi_value value, pesapi_value_ref* out_value_ref, Il2CppObject** out_object)
#ifndef UNITY_6000_0_OR_NEWER
typedef int32_t Il2CppGCHandle;
#endif

static Il2CppGCHandle* FindOrCreateHandleStoreOfValue(struct pesapi_ffi* apis, pesapi_env env, pesapi_value value, pesapi_value_ref* out_value_ref, Il2CppObject** out_object)
{
void* out_ptr;
if(!apis->get_private(env, value, &out_ptr))
Expand All @@ -530,11 +534,11 @@ static int32_t* FindOrCreateHandleStoreOfValue(struct pesapi_ffi* apis, pesapi_e
}

pesapi_value_ref value_ref = static_cast<pesapi_value_ref>(out_ptr);
int32_t* res = nullptr;
Il2CppGCHandle* res = nullptr;
uint32_t field_count;
if (value_ref)
{
res = reinterpret_cast<int32_t*>(apis->get_ref_internal_fields(value_ref, &field_count));
res = reinterpret_cast<Il2CppGCHandle*>(apis->get_ref_internal_fields(value_ref, &field_count));
if(field_count != 1 || !res)
{
PLog("invalid internal_fields size: %u, ptr:%p", field_count, res);
Expand All @@ -548,7 +552,7 @@ static int32_t* FindOrCreateHandleStoreOfValue(struct pesapi_ffi* apis, pesapi_e
{
value_ref = apis->create_value_ref(env, value, (sizeof(std::atomic<Il2CppObject*>) + sizeof(void*) - 1) / sizeof(void*));

res = reinterpret_cast<int32_t*>(apis->get_ref_internal_fields(value_ref, &field_count));
res = reinterpret_cast<Il2CppGCHandle*>(apis->get_ref_internal_fields(value_ref, &field_count));
apis->set_private(env, value, value_ref);
*out_object = nullptr;
}
Expand All @@ -566,7 +570,7 @@ static Il2CppObject* FunctionToDelegate(struct pesapi_ffi* apis, pesapi_env env,
{
pesapi_value_ref value_ref;
Il2CppObject* ret = nullptr;
int32_t* handle_store = FindOrCreateHandleStoreOfValue(apis, env, jsval, &value_ref, &ret);
Il2CppGCHandle* handle_store = FindOrCreateHandleStoreOfValue(apis, env, jsval, &value_ref, &ret);
if (!handle_store) return nullptr;

if (ret == nullptr)
Expand Down Expand Up @@ -907,7 +911,7 @@ handle_underlying:

pesapi_value_ref value_ref;
Il2CppObject* ret = nullptr;
int32_t* handle_store = FindOrCreateHandleStoreOfValue(apis, env, jsval, &value_ref, &ret);
Il2CppGCHandle* handle_store = FindOrCreateHandleStoreOfValue(apis, env, jsval, &value_ref, &ret);
if (!handle_store) return nullptr;

if (ret == nullptr)
Expand Down Expand Up @@ -1892,8 +1896,8 @@ struct JsEnvPrivate
// insert前已经对有效性进行判断
uint32_t fieldCount;
//PLog("CleanupPendingKillScriptObjects: %p", valueRef);
int32_t* store = reinterpret_cast<int32_t*>(apis->get_ref_internal_fields(valueRef, &fieldCount));
int32_t handle = *store;
Il2CppGCHandle* store = reinterpret_cast<Il2CppGCHandle*>(apis->get_ref_internal_fields(valueRef, &fieldCount));
Il2CppGCHandle handle = *store;
if (nullptr == il2cpp::gc::GCHandle::GetTarget(handle)) //假如不为空代表已经重新被绑定了
{
il2cpp::gc::GCHandle::Free(handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { default as $, IF, ELSE, ELSEIF, ENDIF, FOR } from '../templates/tte.mjs'

export default function unityenv_for_puerts(newerthan2021, shared) {
export default function unityenv_for_puerts(newerthan2021, newerthan6000, shared) {
return $`
${IF(newerthan2021)}
#ifndef UNITY_2021_1_OR_NEWER
#define UNITY_2021_1_OR_NEWER
#endif
${ENDIF()}
${IF(newerthan6000)}
#ifndef UNITY_6000_0_OR_NEWER
#define UNITY_6000_0_OR_NEWER
#endif
${ENDIF()}
${IF(shared)}
#ifndef PUERTS_SHARED
#define PUERTS_SHARED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,18 @@ public static void GenMarcoHeader(string outDir)

using (var jsEnv = new Puerts.JsEnv())
{
var macroHeaderRender = jsEnv.ExecuteModule<Func<bool, bool, string>>("puerts/xil2cpp/unityenv_for_puerts.h.tpl.mjs", "default");
var macroHeaderRender = jsEnv.ExecuteModule<Func<bool, bool, bool, string>>("puerts/xil2cpp/unityenv_for_puerts.h.tpl.mjs", "default");
string macroHeaderContent = macroHeaderRender(
#if !UNITY_2021_1_OR_NEWER
false,
#else
true,
#endif
#if !UNITY_6000_0_OR_NEWER
false,
#else
true,
#endif
#if UNITY_IPHONE
false
#else
Expand Down

0 comments on commit 82783ae

Please sign in to comment.