BepInEx / Il2CppInterop

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

Don't crash on failed delegate creation #49

Closed UserUnknownFactor closed 1 year ago

UserUnknownFactor commented 1 year ago

Otherwise Il2CppInterop crashes in combination of BepInEx-Unity.IL2CPP-win-x64-6.0.0-be.656 and Unity 2020.3.38f1 on UnityEngine.Input+IsJoystickPreconfiguredDelegate with something like this:

System.NotSupportedException: Wrong MethodAttributes or CallingConventions for DynamicMethod. Only public, static, standard supported
   at System.Reflection.Emit.DynamicMethod.CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention)
   at System.Reflection.Emit.DynamicMethod.Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, Boolean skipVisibility, Boolean transparentMethod)
   at System.Reflection.Emit.DynamicMethod..ctor(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility)
   at Il2CppInterop.Runtime.IL2CPP.GenerateDelegateForMissingICall[T](String signature) in /Il2CppInterop/Il2CppInterop.Runtime/IL2CPP.cs:line 272
   at Il2CppInterop.Runtime.IL2CPP.ResolveICall[T](String signature) in /Il2CppInterop/Il2CppInterop.Runtime/IL2CPP.cs:line 262
   at UnityEngine.Input..cctor()
   --- End of inner exception stack trace ---
   at UnityEngine.Input.GetKeyDown(KeyCode key)
   at MyPlugin.PluginClass.DoUpdate()
js6pak commented 1 year ago

This is not the correct fix, the actual problem is that no one actually checked if GenerateDelegateForMissingICall works after switching to coreclr, all you need to fix this is to add MethodAttributes.Public to the attributes. Or just don't specify attributes and calling convention and leave the defaults which are the same.

UserUnknownFactor commented 1 year ago

@js6pak: thank you for fixing it.