From 233f00057e1074f0fb5fe9de02d0ff3e4fcec944 Mon Sep 17 00:00:00 2001 From: dlbuhtig4096 <> Date: Thu, 28 Sep 2023 18:25:06 +0800 Subject: [PATCH] PR #103 changes & logging --- Il2CppInterop.Runtime/Injection/ClassInjector.cs | 11 +++++++++-- Il2CppInterop.Runtime/Injection/InjectorHelpers.cs | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Il2CppInterop.Runtime/Injection/ClassInjector.cs b/Il2CppInterop.Runtime/Injection/ClassInjector.cs index 9b50b1a7..e864ba69 100644 --- a/Il2CppInterop.Runtime/Injection/ClassInjector.cs +++ b/Il2CppInterop.Runtime/Injection/ClassInjector.cs @@ -412,6 +412,8 @@ static void FindAbstractMethods(List list, INativeClass var methodName = Marshal.PtrToStringAnsi(baseMethod.Name); + Logger.Instance.LogInformation("Processing {type}::{methodName}", type, methodName); + if (methodName == "Finalize") // slot number is not static { vTablePointer[i].method = methodPointerArray[0]; @@ -1138,8 +1140,13 @@ private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer) internal static Type SystemTypeFromIl2CppType(Il2CppTypeStruct* typePointer) { - var fullName = GetIl2CppTypeFullName(typePointer); - var type = Type.GetType(fullName) ?? throw new NullReferenceException($"Couldn't find System.Type for Il2Cpp type: {fullName}"); + var il2cppType = UnityVersionHandler.Wrap(typePointer); + + if (!InjectorHelpers.TryGetType((IntPtr)il2cppType.TypePointer, out var type)) + { + var fullName = GetIl2CppTypeFullName(typePointer); + type = Type.GetType(fullName) ?? throw new NullReferenceException($"Couldn't find System.Type for Il2Cpp type: {fullName}"); + } return RewriteType(type); } diff --git a/Il2CppInterop.Runtime/Injection/InjectorHelpers.cs b/Il2CppInterop.Runtime/Injection/InjectorHelpers.cs index 18038a78..2e19c804 100644 --- a/Il2CppInterop.Runtime/Injection/InjectorHelpers.cs +++ b/Il2CppInterop.Runtime/Injection/InjectorHelpers.cs @@ -98,6 +98,12 @@ internal static void AddTypeToLookup(Type type, IntPtr typePointer) { s_ClassNameLookup.Add((namespaze, klass, image), typePointer); } + s_TypeLookup[typePointer] = type; + } + + internal static bool TryGetType(IntPtr typePointer, out Type type) + { + return s_TypeLookup.TryGetValue(typePointer, out type); } internal static IntPtr GetIl2CppExport(string name) @@ -137,6 +143,7 @@ internal static IntPtr GetIl2CppMethodPointer(MethodBase proxyMethod) internal static readonly ConcurrentDictionary s_InjectedClasses = new(); /// (namespace, class, image) : class internal static readonly Dictionary<(string _namespace, string _class, IntPtr imagePtr), IntPtr> s_ClassNameLookup = new(); + internal static readonly Dictionary s_TypeLookup = new(); #region Class::Init [UnmanagedFunctionPointer(CallingConvention.Cdecl)]