BepInEx / Il2CppInterop

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

InvalidOperationException when using delegates #71

Open PieKing1215 opened 1 year ago

PieKing1215 commented 1 year ago

I'm trying to add a listener to a delegate, but I get an exception when the event runs.

BepInEx 6.0.0-be.665 Game is Unity 2022.1, v29 metadata (BepInEx says Running under Unity 2021.2.0f1 though?)

Setup:

// GameEvent is `public delegate void GameEvent();` (class that extends `Il2CppSystem.MulticastDelegate`)
// Plugin.OnRoundBegin is `public static void OnRoundBegin() {}`

GameEvents.OnRoundBegin += (GameEvents.GameEvent)Plugin.OnRoundBegin;
// or
GameEvents.OnRoundBegin += (System.Action)Plugin.OnRoundBegin;

Error when the event happens:

[Error  :Il2CppInterop] Exception in IL2CPP-to-Managed trampoline, not passing it to il2cpp: System.InvalidOperationException: Handle is not initialized.
   at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
   at Il2CppInterop.Runtime.Runtime.ClassInjectorBase.GetMonoObjectFromIl2CppPointer(IntPtr pointer) in /home/runner/work/Il2CppInterop/Il2CppInterop/Il2CppInterop.Runtime/Runtime/ClassInjectorBase.cs:line 15
   at (il2cpp delegate trampoline) System.Void_System.Action(IntPtr , Il2CppMethodInfo* )

I could just be doing something wrong, but I've tried a bunch of variations and couldn't get it to work.

(I wasn't 100% sure if I should open here or in BepInEx/BepInEx so feel free to transfer)

ds5678 commented 1 month ago

Is this still an issue?