diff --git a/MelonLoader/MelonLoader.Bootstrap/BootstrapInterop.cs b/MelonLoader/MelonLoader.Bootstrap/BootstrapInterop.cs index 0cb0c1841..5b43e62f5 100644 --- a/MelonLoader/MelonLoader.Bootstrap/BootstrapInterop.cs +++ b/MelonLoader/MelonLoader.Bootstrap/BootstrapInterop.cs @@ -9,14 +9,29 @@ public static unsafe class BootstrapInterop public static delegate* unmanaged NativeHookAttach; public static delegate* unmanaged NativeHookDetach; - public unsafe static T HookAttach(T target, T detour) where T : Delegate - => Marshal.GetDelegateForFunctionPointer( - new IntPtr( - NativeHookAttach( - target.GetFunctionPointer().ToPointer(), - detour.GetFunctionPointer().ToPointer()))); + public static T HookAttach(T target, T detour) where T : Delegate + => Marshal.GetDelegateForFunctionPointer(HookAttach(target.GetFunctionPointer(), detour.GetFunctionPointer())); + public static IntPtr HookAttach(IntPtr target, IntPtr detour) + { + // Dereference Target Pointer to get Actual Pointer + IntPtr targetAddr = (IntPtr)(&target); - public unsafe static void HookDetach(T target) where T : Delegate - => NativeHookDetach(target.GetFunctionPointer().ToPointer()); + // Attach Hook + void* originalFunc = NativeHookAttach(targetAddr.ToPointer(), detour.ToPointer()); + + // Return Original Func as IntPtr + return new IntPtr(originalFunc); + } + + public static void HookDetach(T target) where T : Delegate + => HookDetach(target.GetFunctionPointer()); + public static void HookDetach(IntPtr target) + { + // Dereference Target Delegate to get Actual Pointer + IntPtr targetAddr = (IntPtr)(&target); + + // Detach Hook + NativeHookDetach(targetAddr.ToPointer()); + } } } \ No newline at end of file