Il2CppInterop icon indicating copy to clipboard operation
Il2CppInterop copied to clipboard

Unable to get NativeClassPtr in some classes

Open yukieiji opened this issue 1 year ago • 1 comments

When I try to get NativeClassPtr of a class like Il2cppSystem.Collection.Generic.Dictionary<Collider2D, IUsable[]>(in AmongUs), I get the following error.

  • error log
[Error  :Il2CppInterop] Exception in IL2CPP-to-Managed trampoline, not passing it to il2cpp: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeInitializationException: The type initializer for 'Il2CppInterop.Runtime.Il2CppClassPointerStore`1' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Il2CppSystem.Collections.Generic.Dictionary`2' threw an exception.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Il2CppInterop.Runtime.Injection.Hooks.Class_FromIl2CppType_Hook.Hook(Il2CppType* type, Boolean throwOnError) in /home/runner/work/Il2CppInterop/Il2CppInterop/Il2CppInterop.Runtime/Injection/Hooks/Class_FromIl2CppType_Hook.cs:line 33
   at Il2CppInterop.Runtime.IL2CPP.il2cpp_runtime_invoke(IntPtr method, IntPtr obj, Void** param, IntPtr& exc)
   at Il2CppSystem.Type.internal_from_handle(IntPtr handle)
   at Il2CppSystem.Collections.Generic.Dictionary`2..cctor()
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(QCallTypeHandle type)
   at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle type)
   at Il2CppInterop.Runtime.Il2CppClassPointerStore`1..cctor() in /home/runner/work/Il2CppInterop/Il2CppInterop/Il2CppInterop.Runtime/Il2CppClassPointerStore.cs:line 42
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized)
   at System.Reflection.RtFieldInfo.GetValue(Object obj)
   at UniverseLib.Il2CppReflection.Il2CppTypeNotNull(Type type, IntPtr& il2cppPtr)
   at UniverseLib.Il2CppReflection.Internal_TryCast(Object obj, Type toType)
   at UniverseLib.ReflectionExtensions.TryCast(Object obj)
   at UnityExplorer.CacheObject.CacheObjectBase.SetValueFromSource(Object value)
   at UnityExplorer.CacheObject.CacheMember.Evaluate()
   at UnityExplorer.CacheObject.CacheMember.TryAutoEvaluateIfUnitialized(CacheObjectCell objectcell)
   at UnityExplorer.CacheObject.CacheObjectBase.SetDataToCell(CacheObjectCell cell)
   at UnityExplorer.CacheObject.CacheObjectControllerHelper.SetCell(CacheObjectCell cell, Int32 index, IList cachedEntries, Action`1 onDataSetToCell)
   at UnityExplorer.Inspectors.ReflectionInspector.SetCell(CacheMemberCell cell, Int32 index)
   at UniverseLib.UI.Widgets.ScrollView.ScrollPool`1.SetCell(T cachedCell, Int32 dataIndex)
   at UniverseLib.UI.Widgets.ScrollView.ScrollPool`1.InitCoroutine(Action onHeightChangedListener)+MoveNext()
   at UniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumerator.MoveNext()
   at Trampoline_ByteThisUniverseLib.Runtime.Il2Cpp.Il2CppManagedEnumeratorMoveNext(IntPtr , Il2CppMethodInfo* )

This error is caused by the original method of retrieving NativeClassPtr with a function, but the same error occurs when using Il2CppClassPointerStore.GetNativeClassPointer

yukieiji avatar Feb 17 '24 13:02 yukieiji

Try Il2CppSystem.Collections.Generic.Dictionary<Collider2D, Il2CppReferenceArray<IUsable>> instead.

js6pak avatar Feb 25 '24 19:02 js6pak

@yukieiji did the advice from js6pak resolve your issue?

ds5678 avatar Jul 14 '24 15:07 ds5678

I have solved the problem, but I don't know how to find out if it is an Il2CppReferenceArray<T> from the Type.

yukieiji avatar Jul 14 '24 23:07 yukieiji

Interfaces are reference types.

ds5678 avatar Jul 14 '24 23:07 ds5678

For example, for Type Il2CppSystem.Collections.Generic.Dictionary<Collider2D, IUsable[]>, what code should I use?

yukieiji avatar Jul 15 '24 03:07 yukieiji

Try Il2CppSystem.Collections.Generic.Dictionary<Collider2D, Il2CppReferenceArray<IUsable>> instead.

ds5678 avatar Jul 15 '24 03:07 ds5678

How can I dynamically check that an Array is included and replace it with an Il2CppReferenceArray<T>? I need to dynamically detect and replace the Array contained in the Type for this function to work.

yukieiji avatar Jul 15 '24 04:07 yukieiji

GPT:

In C#, you can determine if a System.Type represents a value type by using the IsValueType property. This property returns a boolean value indicating whether the type is a value type. Value types include all primitive types (such as int, float, char, etc.), as well as struct types.

ds5678 avatar Jul 15 '24 06:07 ds5678