Tencent / puerts

PUER(普洱) Typescript. Let's write your game in UE or Unity with TypeScript.
Other
5.08k stars 707 forks source link

[Unity]xil2cpp的一些待改进点 #1838

Open chexiongsheng opened 2 months ago

chexiongsheng commented 2 months ago

detail | 详细描述

1、android下得静态库,有些unity版本支持的不好; 2、wrapper代码得编译到plugin,得用户自己出包,增加了使用成本; 3、plugin引用了一些需要生成的头文件(unityenv_for_puerts.h、UnityExports4Puerts.h)。 4、pesapi_adpt.c相比gen_pesapi_adpt.js生成的版本有调整 5、部分和ue共用的代码略微改动,不能统一

chexiongsheng commented 2 months ago

基础类型的Nullable可能没处理好:https://github.com/Tencent/puerts/blob/master/unity/native_src_il2cpp/Src/Puerts.cpp#L415

chexiongsheng commented 1 month ago

重构过程发现有些类型的参数(xil2cpp wrap)没测试到,需要补充用例

public Vector3? GetNullableVector3(bool n)
//值类型引用
UnityEngine.Vector3 SmoothDamp(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Vector3 ByRef, Single)
public void GetNullableVector3(ref Vector3? vector)
public void StringRef(ref string abc)
public void ObjRef(ref object abc)
public System.IntPtr IntPtrArgRet(System.IntPtr intPtr)
public void IntDefault(string a, int b = 100)
public void ObjDefault(string a, object b = null)
public void StrDefault(string a, string b = "hello")
public void ValueTypeDefault(string a, MyStruct b = default)
chexiongsheng commented 1 month ago

TODO: pesapi_get_env_from_ref,如果虚拟机析构应该返回nullptr。

chexiongsheng commented 1 month ago

JSObject释放优化 目前的方案 gc线程把信息放延迟队列:JSObject析构(c# gc线程)->releaseScriptObject(InternalCall) -> g_unityExports.UnrefJsObject(plugin) ->PendingReleaseObjects.push_back(加锁) 延迟队列清理: 1、Tick(c#) ->ReleasePendingJsObjects(plugin) ->CppObjectMapper.ClearPendingPersistentObject(加锁) 2、FunctionToDelegate->_GetRuntimeObjectFromPersistentObject->CppObjectMapper.ClearPendingPersistentObject(加锁) 3、JsValueToCSRef里的js函数转delegate

之前之所以在js转delegate clear一下,看这里:https://github.com/Tencent/puerts/pull/1131

chexiongsheng commented 1 month ago

通过$generic生成的泛型示例扫描不到,不能为其生成代码。 比如ut用例ListDictForofTest的执行,打开NOT_GEN_WARNING会报:

can't get static wrapper for Int32 BinarySearch(Int32, Int32, Int32, System.Collections.Generic.IComparer`1[System.Int32]) declare in System.Collections.Generic.List`1[System.Int32], signature:i4ti4i4i4o
can't get static wrapper for Int32 FindIndex(Int32, Int32, System.Predicate`1[System.Int32]) declare in System.Collections.Generic.List`1[System.Int32], signature:i4ti4i4o
can't get static wrapper for Enumerator GetEnumerator() declare in System.Collections.Generic.List`1[System.Int32], signature:S_oi4i4i4_t
can't get static wrapper for Enumerator GetEnumerator() declare in System.Collections.Generic.Dictionary`2[System.Int32,System.Int32], signature:S_oi4i4S_i4i4_i4_t
can't get static wrapper for Boolean Remove(Int32, Int32 ByRef) declare in System.Collections.Generic.Dictionary`2[System.Int32,System.Int32], signature:bti4Pi4
can't get static wrapper for Boolean TryGetValue(Int32, Int32 ByRef) declare in System.Collections.Generic.Dictionary`2[System.Int32,System.Int32], signature:bti4Pi4
can't get static wrapper for Boolean TryAdd(Int32, Int32) declare in System.Collections.Generic.Dictionary`2[System.Int32,System.Int32], signature:bti4i4
can't get static wrapper for System.Collections.DictionaryEntry System.Collections.IDictionaryEnumerator.get_Entry() declare in System.Collections.Generic.Dictionary`2+Enumerator[System.Int32,System.Int32], signature:S_OO_t
can't get static wrapper for Int32 BinarySearch(Int32, Int32, System.String, System.Collections.Generic.IComparer`1[System.String]) declare in System.Collections.Generic.List`1[System.String], signature:i4ti4i4so
can't get static wrapper for Int32 FindIndex(Int32, Int32, System.Predicate`1[System.String]) declare in System.Collections.Generic.List`1[System.String], signature:i4ti4i4o
can't get static wrapper for Enumerator GetEnumerator() declare in System.Collections.Generic.List`1[System.String], signature:S_oi4i4s_t
chexiongsheng commented 1 month ago

JSObject释放优化 目前的方案 gc线程把信息放延迟队列:JSObject析构(c# gc线程)->releaseScriptObject(InternalCall) -> g_unityExports.UnrefJsObject(plugin) ->PendingReleaseObjects.push_back(加锁) 延迟队列清理: 1、Tick(c#) ->ReleasePendingJsObjects(plugin) ->CppObjectMapper.ClearPendingPersistentObject(加锁) 2、FunctionToDelegate->_GetRuntimeObjectFromPersistentObject->CppObjectMapper.ClearPendingPersistentObject(加锁) 3、JsValueToCSRef里的js函数转delegate

之前之所以在js转delegate clear一下,看这里:#1131

已经优化:https://github.com/Tencent/puerts/issues/1861

chexiongsheng commented 1 month ago

多backend支持方案: 1、比较简单的是通过thread local存储p-api实现函数指针数组。进入脚本前设置该数组,过程中任意p-api调用都获取thread local上相应函数指针调用。但脚本本地存储的性能如何? 2、每次进入脚本前设置全局函数指针变量。但这有多线程问题。 3、不直接调用p-api,而是调用如下结构体:

struct PesapiImpl
{
      typedef pesapi_value (*pesapi_create_nullType)(pesapi_env env);
      pesapi_create_nullType pesapi_create_null_ptr;
      pesapi_value pesapi_create_null (pesapi_env env) {
          return pesapi_create_null_ptr(env);
      }

      typedef pesapi_value (*pesapi_create_undefinedType)(pesapi_env env);
      pesapi_create_undefinedType pesapi_create_undefined_ptr;
      pesapi_value pesapi_create_undefined (pesapi_env env) {
          return pesapi_create_undefined_ptr(env);
      }
      // ...
};

所有需要用到p-api的函数的地方参数1都是这个结构体的实例(实例指针由指针数组强转而成)