Skip to content

Commit

Permalink
Update BootstrapInterop.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Nov 22, 2023
1 parent b69fff0 commit ec7c9b6
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions MelonLoader/MelonLoader.Bootstrap/BootstrapInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,29 @@ public static unsafe class BootstrapInterop
public static delegate* unmanaged<void*, void*, void*> NativeHookAttach;
public static delegate* unmanaged<void*, void> NativeHookDetach;

public unsafe static T HookAttach<T>(T target, T detour) where T : Delegate
=> Marshal.GetDelegateForFunctionPointer<T>(
new IntPtr(
NativeHookAttach(
target.GetFunctionPointer().ToPointer(),
detour.GetFunctionPointer().ToPointer())));
public static T HookAttach<T>(T target, T detour) where T : Delegate
=> Marshal.GetDelegateForFunctionPointer<T>(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>(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>(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());
}
}
}

0 comments on commit ec7c9b6

Please sign in to comment.