Skip to content

Commit

Permalink
[unity]未完善的扩展函数支持以及废代码删除
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Jan 10, 2025
1 parent 51b9029 commit 415b68a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 394 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace PuertsIl2cpp
public static class ExtensionMethodInfos_Gen
{
[UnityEngine.Scripting.Preserve]
public static IEnumerable<MethodInfo> TryLoadExtensionMethod(Type type)
public static MethodInfo[] TryLoadExtensionMethod(string assemblyQualifiedName)
{
if (false) {}${FOR(getExtendedTypeToExtensionTypeInfo(rawInfo), e => `
else if (type == typeof(${e.extendedType}))
else if (typeof(${e.extendedType}).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(${e.extendedType})${FOR(e.extensionTypes, extensionType => `, typeof(${extensionType})`)});
}`)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static Il2CppClass *g_typeofPersistentObjectInfo;
static Il2CppClass *g_typeofArrayBuffer;
static Il2CppClass *g_typeofTypedValue;

static MethodInfoHelper<void(const void* typeId, bool includeNonPublic)> g_RegisterNoThrowHelper;
static MethodInfoHelper<Il2CppArray*(Il2CppString*)> g_ExtensionMethodGetHelper;

static void RegisterType(Il2CppClass* klass);
static bool ClassNotFoundCallback(const void* typeId)
Expand All @@ -163,10 +163,9 @@ static bool ClassNotFoundCallback(const void* typeId)
return true;
}

static void SetRegisterNoThrow(Il2CppReflectionMethod* method)
static void SetExtensionMethodGet(Il2CppReflectionMethod* method)
{
g_RegisterNoThrowHelper = MethodInfoHelper<void(const void* typeId, bool includeNonPublic)>(method);
pesapi_on_class_not_found(ClassNotFoundCallback);
g_ExtensionMethodGetHelper = MethodInfoHelper<Il2CppArray*(Il2CppString*)>(method);
}

static std::string GetTypeSignature(const Il2CppType* type);
Expand Down Expand Up @@ -519,7 +518,8 @@ static void RegisterType(Il2CppClass* klass)
return true;
};

try {
try
{
if (isDelegate)
{
MethodInfo* invoke = (MethodInfo*)il2cpp::vm::Class::GetMethodFromName(klass, "Invoke", -1);
Expand Down Expand Up @@ -578,6 +578,26 @@ static void RegisterType(Il2CppClass* klass)

classInfo->Fields.push_back({ std::string(field->name), isStatic, fieldData });
}

std::string assemblyQualifiedName(Type::GetName(&klass->byval_arg, IL2CPP_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED));
if (!assemblyQualifiedName.empty())
{
Il2CppArray* extensionMethods = g_ExtensionMethodGetHelper.Call(il2cpp::vm::String::NewWrapper(assemblyQualifiedName.c_str()));
if (extensionMethods)
{
uint32_t len = Array::GetLength(extensionMethods);
PLog("%s extensionMethods count: %d", klass->name, len);
if (len == 2) //TODO: for test only
{
Il2CppReflectionMethod** arr = reinterpret_cast<Il2CppReflectionMethod**>(il2cpp::vm::Array::GetFirstElementAddress(extensionMethods));
for (uint32_t i = 0; i < len; ++i)
{
PLog("add extension %d %s", i, arr[i]->method->name);
AddMethod(arr[i]->method, false, false, arr[i]->method->name);
}
}
}
}
}
}
catch (Il2CppExceptionWrapper& exception)
Expand Down Expand Up @@ -2599,8 +2619,12 @@ static WrapData* CreateOverloadData(const MethodInfo* method)
std::string signature = GetMethodSignature(method, false);
auto wrap = FindWrapFunc(signature.c_str());
overloadData->Wrap = wrap ? wrap: &ReflectionWrapper;
overloadData->IsStatic = isStatic;
overloadData->IsExtensionMethod = false;
overloadData->IsExtensionMethod = IsExtensionMethod(method);
overloadData->IsStatic = isStatic && !overloadData->IsExtensionMethod;
if (overloadData->IsExtensionMethod)
{
PLog("CreateOverloadData IsExtensionMethod %s , static:%d", method->name, overloadData->IsStatic);
}
memcpy(overloadData->TypeInfos, usedTypes.data(), sizeof(void*) * usedTypes.size());
return overloadData;
}
Expand Down Expand Up @@ -2667,6 +2691,7 @@ puerts::JsEnvPrivate* InitialPapiEnvRef(struct pesapi_ffi* apis, pesapi_env_ref
{
apis->set_property(env, global, "loadType", loadType);
apis->set_property(env, global, "createFunction", createFunction);
pesapi_on_class_not_found(ClassNotFoundCallback);
return jsEnvPrivate;
}
}
Expand Down Expand Up @@ -2723,7 +2748,7 @@ void InitialPuerts(pesapi_func_ptr* func_array)
InternalCalls::Add("Puerts.NativeAPI::CleanupPapiEnvRef(System.IntPtr,System.IntPtr)", (Il2CppMethodPointer)puerts::CleanupPapiEnvRef);
InternalCalls::Add("Puerts.NativeAPI::DestroyJSEnvPrivate(System.IntPtr)", (Il2CppMethodPointer)puerts::DestroyJSEnvPrivate);
InternalCalls::Add("Puerts.JSObject::GetJSObjectValue(System.IntPtr,System.String,System.Type)", (Il2CppMethodPointer)puerts::GetJSObjectValue);
InternalCalls::Add("Puerts.NativeAPI::SetRegisterNoThrow(System.Reflection.MethodBase)", (Il2CppMethodPointer)puerts::SetRegisterNoThrow);
InternalCalls::Add("Puerts.NativeAPI::SetExtensionMethodGet(System.Reflection.MethodBase)", (Il2CppMethodPointer)puerts::SetExtensionMethodGet);
InternalCalls::Add("Puerts.NativeAPI::CppGetMethodSignature(System.Reflection.MethodBase,System.Boolean,System.Boolean)", (Il2CppMethodPointer)puerts::CppGetMethodSignature);
pesapi_init(func_array);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,62 @@ namespace PuertsIl2cpp
public static class ExtensionMethodInfos_Gen_Internal
{
[UnityEngine.Scripting.Preserve]
public static IEnumerable<MethodInfo> TryLoadExtensionMethod(Type type)
public static MethodInfo[] TryLoadExtensionMethod(string assemblyQualifiedName)
{
if (false) {}
else if (type == typeof(System.Int32[]))
else if (typeof(System.Int32[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Int32[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Single[]))
else if (typeof(System.Single[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Single[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Double[]))
else if (typeof(System.Double[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Double[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Boolean[]))
else if (typeof(System.Boolean[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Boolean[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Int64[]))
else if (typeof(System.Int64[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Int64[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.UInt64[]))
else if (typeof(System.UInt64[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.UInt64[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.SByte[]))
else if (typeof(System.SByte[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.SByte[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Byte[]))
else if (typeof(System.Byte[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Byte[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.UInt16[]))
else if (typeof(System.UInt16[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.UInt16[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Int16[]))
else if (typeof(System.Int16[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Int16[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Char[]))
else if (typeof(System.Char[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Char[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.UInt32[]))
else if (typeof(System.UInt32[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.UInt32[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.String[]))
else if (typeof(System.String[]).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.String[]), typeof(PuertsIl2cpp.ArrayExtension));
}
else if (type == typeof(System.Array))
else if (typeof(System.Array).AssemblyQualifiedName == assemblyQualifiedName)
{
return ExtensionMethodInfo.GetExtensionMethods(typeof(System.Array), typeof(PuertsIl2cpp.ArrayExtension));
}
Expand Down
8 changes: 3 additions & 5 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class JsEnv : IDisposable
// TypeRegister TypeRegister;

Type persistentObjectInfoType;
MethodInfo objectPoolAddMethodInfo;
MethodInfo objectPoolRemoveMethodInfo;
MethodInfo tryLoadTypeMethodInfo;
MethodInfo extensionMethodGetMethodInfo;

private Func<string, JSObject> moduleExecutor;

Expand Down Expand Up @@ -66,8 +64,8 @@ public JsEnv(ILoader loader, int debugPort = -1)
Puerts.NativeAPI.SetLogCallback(LogCallback, LogWarningCallback, LogErrorCallback);
Puerts.NativeAPI.InitialPuerts(Puerts.NativeAPI.GetRegsterApi());
apis = Puerts.NativeAPI.GetFFIApi();
tryLoadTypeMethodInfo = typeof(TypeRegister).GetMethod("RegisterNoThrow");
Puerts.NativeAPI.SetRegisterNoThrow(tryLoadTypeMethodInfo);
extensionMethodGetMethodInfo = typeof(PuertsIl2cpp.ExtensionMethodInfo).GetMethod("Get");
Puerts.NativeAPI.SetExtensionMethodGet(extensionMethodGetMethodInfo);

persistentObjectInfoType = typeof(Puerts.JSObject);
Puerts.NativeAPI.SetGlobalType_TypedValue(typeof(TypedValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static void DestroyJSEnvPrivate(IntPtr jsEnvPrivate)
}

[MethodImpl(MethodImplOptions.InternalCall)]
public static void SetRegisterNoThrow(MethodBase methodInfo)
public static void SetExtensionMethodGet(MethodBase methodInfo)
{
}

Expand Down
Loading

0 comments on commit 415b68a

Please sign in to comment.