BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
195 stars 61 forks source link

Non-blittable structs have invalid constructors #76

Closed yukieiji closed 1 year ago

yukieiji commented 1 year ago

What happened?

Can't construct Il2cppSystem.Nullable. such as Il2cppSystem.Nullable(RoleTypes.Crewmate)

Steps to reproduce

  1. build https://github.com/yukieiji/issueProject/tree/main/BeplnEx%20%23550 (for be.565) or https://github.com/yukieiji/issueProject/tree/main/BeplnEx%20%23550_be663 (for be.663)
  2. install this mod
  3. lunch AmongUs

BepInEx Distribution

Bleeding Edge from BepisBuilds

Log outputs

# be565 roleTypeNullAble();
[Error  :    Detour] UnhollowerBaseLib.ObjectCollectedException: Object was garbage collected in IL2CPP domain
  at UnhollowerBaseLib.Il2CppObjectBase.get_Pointer () [0x00022] in <8940b609035b40debac5624d167973bc>:0 
  at UnhollowerBaseLib.IL2CPP.Il2CppObjectBaseToPtrNotNull (UnhollowerBaseLib.Il2CppObjectBase obj) [0x00009] in <8940b609035b40debac5624d167973bc>:0 
  at Il2CppSystem.Nullable`1[T]..ctor (AmongUs.GameOptions.RoleTypes value) [0x00070] in <1bb700d2b5ae4713bb576809c77ea30d>:0 
  at ExtremeRoles.MainMenuManagerStartPatch.roleTypeNullAble () [0x00001] in <961aad5c5a424480a4da462ca4fe3908>:0 
  at ExtremeRoles.MainMenuManagerStartPatch.Postfix () [0x00001] in <961aad5c5a424480a4da462ca4fe3908>:0 
  at (wrapper dynamic-method) MainMenuManager.DMD<MainMenuManager::Start>(MainMenuManager)
  at (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.(il2cpp -> managed) Start(intptr,UnhollowerBaseLib.Runtime.Il2CppMethodInfo*)

# be565 testClassNullAble();
[Error  :    Detour] System.TypeInitializationException: The type initializer for 'Il2CppSystem.Nullable`1' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object
  at UnhollowerRuntimeLib.Injection.InjectorHelpers.hkClassFromIl2CppType (UnhollowerBaseLib.Runtime.Il2CppTypeStruct* type) [0x0000d] in <8940b609035b40debac5624d167973bc>:0 
  at (wrapper native-to-managed) UnhollowerRuntimeLib.Injection.InjectorHelpers.hkClassFromIl2CppType(UnhollowerBaseLib.Runtime.Il2CppTypeStruct*)
  at (wrapper managed-to-native) UnhollowerBaseLib.IL2CPP.il2cpp_runtime_invoke(intptr,intptr,void**,intptr&)
  at Il2CppSystem.Type.internal_from_handle (System.IntPtr handle) [0x00018] in <1bb700d2b5ae4713bb576809c77ea30d>:0 
  at Il2CppSystem.Nullable`1[T]..cctor () [0x00030] in <1bb700d2b5ae4713bb576809c77ea30d>:0 
   --- End of inner exception stack trace ---
  at ExtremeRoles.MainMenuManagerStartPatch.testClassNullAble () [0x00001] in <df29f3a2e0af485eaff31005c228be0a>:0 
  at ExtremeRoles.MainMenuManagerStartPatch.Postfix () [0x00001] in <df29f3a2e0af485eaff31005c228be0a>:0 
  at (wrapper dynamic-method) MainMenuManager.DMD<MainMenuManager::Start>(MainMenuManager)
  at (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.(il2cpp -> managed) Start(intptr,UnhollowerBaseLib.Runtime.Il2CppMethodInfo*)

# be663 roleTypeNullAble();
[Error  :Il2CppInterop] During invoking native->managed trampoline
Exception: System.TypeLoadException: Could not load type 'AmongUs.GameOptions.RoleTypes' from assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at ExtremeRoles.MainMenuManagerStartPatch.roleTypeNullAble()
   at ExtremeRoles.MainMenuManagerStartPatch.Postfix() in F:\ドキュメント\VisualStudioProject\AmongUs\issueProject\BeplnEx #550_be663\BeplnEx 550\BeplnEx 550Plugin.cs:line 40
   at DMD<MainMenuManager::Start>(MainMenuManager this)
   at (il2cpp -> managed) Start(IntPtr , Il2CppMethodInfo* )

# be663 testClassNullAble();
[Error  :Il2CppInterop] During invoking native->managed trampoline
Exception: System.TypeInitializationException: The type initializer for 'Il2CppSystem.Nullable`1' threw an exception.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Il2CppInterop.Runtime.Injection.InjectorHelpers.hkClassFromIl2CppType(Il2CppType* type, Boolean throwOnError) in /home/runner/work/Il2CppInterop/Il2CppInterop/Il2CppInterop.Runtime/Injection/InjectorHelpers.cs:line 319
   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.Nullable`1..cctor()
   --- End of inner exception stack trace ---
   at Il2CppSystem.Nullable`1..ctor(T value)
   at ExtremeRoles.MainMenuManagerStartPatch.testClassNullAble() in F:\ドキュメント\VisualStudioProject\AmongUs\issueProject\BeplnEx #550_be663\BeplnEx 550\BeplnEx 550Plugin.cs:line 52
   at ExtremeRoles.MainMenuManagerStartPatch.Postfix() in F:\ドキュメント\VisualStudioProject\AmongUs\issueProject\BeplnEx #550_be663\BeplnEx 550\BeplnEx 550Plugin.cs:line 40
   at DMD<MainMenuManager::Start>(MainMenuManager this)
   at (il2cpp -> managed) Start(IntPtr , Il2CppMethodInfo* )

Environment

- OS: Windows10
- BepInEx: 6.0.0-be565 and 6.0.0-be663
- Game: AmongUs
js6pak commented 1 year ago

roleTypeNullAble will get fixed by #77, you can use new Il2CppSystem.Nullable<RoleTypes>() { value = RoleTypes.Shapeshifter, has_value = true } as a workaround in the meantime

testClassNullAble is expected not to work because you are trying to use an il2cpp generic with a managed type (and you should inject it first), which won't work because il2cpp generics are AOT compiled, but I guess we should have a better error message