Skip to content

Commit

Permalink
Fix issue #140 (AssetBundles cannot be loaded at runtime)
Browse files Browse the repository at this point in the history
  • Loading branch information
aceman1209 committed Jan 3, 2025
1 parent ba67c11 commit eafd7aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Il2CppInterop.Runtime/InteropTypes/Il2CppObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public T Unbox<T>() 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<T>
{
Expand Down Expand Up @@ -126,7 +126,7 @@ private static Func<IntPtr, T> 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)
Expand Down
10 changes: 7 additions & 3 deletions Il2CppInterop.Runtime/Runtime/Il2CppObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ public static T Get<T>(IntPtr ptr)
var newObj = Il2CppObjectBase.InitializerStore<T>.Initializer(ptr);
unsafe
{
var nativeClassStruct = UnityVersionHandler.Wrap((Il2CppClass*)Il2CppClassPointerStore<T>.NativeClassPtr);
if (!nativeClassStruct.HasFinalize)
var il2CppClass = (Il2CppClass*)Il2CppClassPointerStore<T>.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));
}
}
}

Expand Down

0 comments on commit eafd7aa

Please sign in to comment.