knah/Il2CppAssemblyUnhollower

System.ArgumentException: Base class UnityEngine.MonoBehaviour is generic and can't be inherited from

spacehamster opened this issue · 2 comments

When trying to inherit from MonoBehaviours in Unity v2018.3.7, I get the following exception

[Error  :   BepInEx] Error loading [Fast Mode] : Base class UnityEngine.MonoBehaviour is generic and can't be inherited from
[Debug  :   BepInEx] System.ArgumentException: Base class UnityEngine.MonoBehaviour is generic and can't be inherited from
  at UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2Cpp[T] () [0x000c5] in <78b0a74ed8474311860460a2b2541509>:0 
  at FastMode.MyMod.Load () [0x00017] in <c635291483104ba6bcd486771a7e2830>:0 
  at BepInEx.IL2CPP.IL2CPPChainloader.LoadPlugin (BepInEx.PluginInfo pluginInfo, System.Reflection.Assembly pluginAssembly) [0x00019] in <25d17e9887f345fb8db7fda2eb27a374>:0 
  at BepInEx.Bootstrap.BaseChainloader`1[TPlugin].Execute () [0x00223] in <43cd6c7a3ac44c01acb29606479d8d71>:0 

Example code: Main.cs.txt

I believe the issue is caused by incorrect Il2CppClass field alignment, the field naturalAligment does not exist in unity versions 2018.3 or lower, and commenting out the field fixes the issue.

public byte naturalAligment; // Alignment of this type without accounting for packing

I also found another issue with the struct layout.

/*ALIGN_TYPE(8)*/ulong cctor_thread;

cctor_thread is a uint64_t in versions less then 2019, but with 2019 and newer, cctor_thread was changed to a size_t, so the field should be sized like this. (ALIGN_TYPE(8) has no effect from what I can tell)

2018 x86: 8 bytes
2018 x64: 8 bytes
2019 x86: 4 bytes
2019 x64: 8 bytes
knah commented

99a080d includes a rewrite of struct handling and uint64 -> size_t change for U2019, so this one should be fixed in 0.4.10.0. In general, native struct handling is a bit more streamlined now, so PRs for other specific version support would be welcome.