From eafd7aa6547fe88278985730b1d10209e6812711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=90=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Fri, 3 Jan 2025 23:14:49 +0400 Subject: [PATCH 1/3] Fix issue #140 (AssetBundles cannot be loaded at runtime) --- Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs | 8 ++++---- Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs | 10 +++++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs index ef7c40e4..8c73caaf 100644 --- a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs +++ b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs @@ -82,10 +82,10 @@ public T Unbox() where T : unmanaged } private static readonly Type[] _intPtrTypeArray = { typeof(IntPtr) }; - private static readonly MethodInfo _getUninitializedObject = typeof(RuntimeHelpers).GetMethod(nameof(RuntimeHelpers.GetUninitializedObject))!; + private static readonly MethodInfo _getUninitializedObject = typeof(RuntimeHelpers).GetMethod(nameof(FormatterServices.GetUninitializedObject))!; private static readonly MethodInfo _getTypeFromHandle = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle))!; - private static readonly MethodInfo _createGCHandle = typeof(Il2CppObjectBase).GetMethod(nameof(CreateGCHandle))!; - private static readonly FieldInfo _isWrapped = typeof(Il2CppObjectBase).GetField(nameof(isWrapped))!; + private static readonly MethodInfo _createGCHandle = typeof(Il2CppObjectBase).GetMethod(nameof(CreateGCHandle), BindingFlags.Instance | BindingFlags.NonPublic)!; + private static readonly FieldInfo _isWrapped = typeof(Il2CppObjectBase).GetField(nameof(isWrapped), BindingFlags.Instance | BindingFlags.NonPublic)!; internal static class InitializerStore { @@ -126,7 +126,7 @@ private static Func Create() // obj.isWrapped = true; il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4_1); - il.Emit(OpCodes.Stsfld, _isWrapped); + il.Emit(OpCodes.Stfld, _isWrapped); var parameterlessConstructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.EmptyTypes); if (parameterlessConstructor != null) diff --git a/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs b/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs index bd70a8ad..54a18d9a 100644 --- a/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs +++ b/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs @@ -40,10 +40,14 @@ public static T Get(IntPtr ptr) var newObj = Il2CppObjectBase.InitializerStore.Initializer(ptr); unsafe { - var nativeClassStruct = UnityVersionHandler.Wrap((Il2CppClass*)Il2CppClassPointerStore.NativeClassPtr); - if (!nativeClassStruct.HasFinalize) + var il2CppClass = (Il2CppClass*)Il2CppClassPointerStore.NativeClassPtr; + if (il2CppClass != null) { - Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); + var nativeClassStruct = UnityVersionHandler.Wrap(il2CppClass); + if (!nativeClassStruct.HasFinalize) + { + Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); + } } } From 78a13d94208843f5f5a4004237dd41df4cb1de5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=90=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sun, 5 Jan 2025 00:38:05 +0400 Subject: [PATCH 2/3] Revert Il2CppObjectPool.cs and change incorrect typeof --- Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs | 2 +- Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs index 8c73caaf..f6ca9478 100644 --- a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs +++ b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs @@ -82,7 +82,7 @@ public T Unbox() where T : unmanaged } private static readonly Type[] _intPtrTypeArray = { typeof(IntPtr) }; - private static readonly MethodInfo _getUninitializedObject = typeof(RuntimeHelpers).GetMethod(nameof(FormatterServices.GetUninitializedObject))!; + private static readonly MethodInfo _getUninitializedObject = typeof(FormatterServices).GetMethod(nameof(FormatterServices.GetUninitializedObject))!; private static readonly MethodInfo _getTypeFromHandle = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle))!; private static readonly MethodInfo _createGCHandle = typeof(Il2CppObjectBase).GetMethod(nameof(CreateGCHandle), BindingFlags.Instance | BindingFlags.NonPublic)!; private static readonly FieldInfo _isWrapped = typeof(Il2CppObjectBase).GetField(nameof(isWrapped), BindingFlags.Instance | BindingFlags.NonPublic)!; diff --git a/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs b/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs index 54a18d9a..bd70a8ad 100644 --- a/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs +++ b/Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs @@ -40,14 +40,10 @@ public static T Get(IntPtr ptr) var newObj = Il2CppObjectBase.InitializerStore.Initializer(ptr); unsafe { - var il2CppClass = (Il2CppClass*)Il2CppClassPointerStore.NativeClassPtr; - if (il2CppClass != null) + var nativeClassStruct = UnityVersionHandler.Wrap((Il2CppClass*)Il2CppClassPointerStore.NativeClassPtr); + if (!nativeClassStruct.HasFinalize) { - var nativeClassStruct = UnityVersionHandler.Wrap(il2CppClass); - if (!nativeClassStruct.HasFinalize) - { - Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); - } + Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); } } From 1395b4923abe7658625833fd95d16e53238c5465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=90=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sun, 5 Jan 2025 00:51:37 +0400 Subject: [PATCH 3/3] Revert typeof changes and correction IL code comment --- Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs index f6ca9478..08e09523 100644 --- a/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs +++ b/Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs @@ -82,7 +82,7 @@ public T Unbox() where T : unmanaged } private static readonly Type[] _intPtrTypeArray = { typeof(IntPtr) }; - private static readonly MethodInfo _getUninitializedObject = typeof(FormatterServices).GetMethod(nameof(FormatterServices.GetUninitializedObject))!; + private static readonly MethodInfo _getUninitializedObject = typeof(RuntimeHelpers).GetMethod(nameof(RuntimeHelpers.GetUninitializedObject))!; private static readonly MethodInfo _getTypeFromHandle = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle))!; private static readonly MethodInfo _createGCHandle = typeof(Il2CppObjectBase).GetMethod(nameof(CreateGCHandle), BindingFlags.Instance | BindingFlags.NonPublic)!; private static readonly FieldInfo _isWrapped = typeof(Il2CppObjectBase).GetField(nameof(isWrapped), BindingFlags.Instance | BindingFlags.NonPublic)!; @@ -112,7 +112,7 @@ private static Func Create() // However, it could be be user-made or implicit // In that case we set the GCHandle and then call the ctor and let GC destroy any objects created by DerivedConstructorPointer - // var obj = (T)FormatterServices.GetUninitializedObject(type); + // var obj = (T)RuntimeHelpers.GetUninitializedObject(type); il.Emit(OpCodes.Ldtoken, type); il.Emit(OpCodes.Call, _getTypeFromHandle); il.Emit(OpCodes.Call, _getUninitializedObject);