Introduction
MonoDetour is a .NET detouring library powered by MonoMod.RuntimeDetour. What this means is that MonoDetour can be used to insert hooks before and after methods, and modify their CIL instructions at runtime.
MonoDetour is mainly designed to be used with HookGen. That is, MonoDetour generates helpers hooks to make hooking easy.
You can use the generated hooks like so:
// Generate helper hooks for the target type.[MonoDetourTargets(typeof(SomeType))]class SomeTypeHooks{ // MonoDetourManager.InvokeHookInitializers will // call methods marked with this attribute in types // which have the MonoDetourTargetsAttribute. [MonoDetourHookInitialize] internal static void InitHooks() { // Note: this is using the default generated MonoDetourManager // MonoDetour.HookGen.DefaultMonoDetourManager.Instance by default. // Use it for managing your hooks. On.SomeNamespace.SomeType.SomeMethod.Prefix(Prefix_SomeMethod); }
private static void Prefix_SomeMethod(SomeType self, ref int number) { Console.WriteLine("Hello from Prefix hook!"); }}
MonoDetour entirely relies on ILHook
s for hooking similar to HarmonyX. But instead of having monolithic ILHook
methods like in HarmonyX, MonoDetour maps every hook to a unique ILHook
.
MonoDetour achieves this by creating a small framework for ILHook wrappers, which it then uses to implement 3 basic detour types.
Detour Types Learn more about MonoDetour's detour type system.