Home

Awesome

DetourUtility: A Lightweight C# Method Redirection Tool

DetourUtility is a lightweight utility in C# designed to dynamically redirect method calls at runtime using unsafe code for low-level memory manipulation. This technique is commonly known as "detouring" and is useful for modifying method behavior in scenarios where source code access is unavailable, such as in Unity or other compiled assemblies.

Key Features

Use Cases

Detouring is typically used in the following scenarios:

  1. Modding: Especially useful in game modding where detouring allows the replacement or augmentation of game functions.
  2. Unity Editor Workarounds: Unity has become more open to modification, but certain engine behaviors may still need fixing. Detouring enables custom editor bugfixes, tools, and workarounds.
  3. Runtime Engine Modifications: While not ideal for production, detouring can resolve issues during development when other fixes are unavailable.
  4. Assembly Patching: Useful for patching code from assemblies where the source code is unavailable.

Caveats and Risks

Using detours comes with significant risks, including:

Make sure to fully understand the impact before using detours.

Example Usage

using System.Reflection;
using DetourUtility;

// Example of redirecting (detouring) method calls
MethodInfo originalMethod = typeof(SomeClass).GetMethod("OriginalMethod");
MethodInfo newMethod = typeof(SomeClass).GetMethod("NewMethod");

DetourUtility.TryDetourFromTo(originalMethod, newMethod);

How It Works

DetourUtility rewrites function pointers in memory to redirect method calls. Depending on whether the system is 32-bit or 64-bit, the class adjusts its logic to handle different jump instructions at the assembly level.

This technique is based on tried-and-tested methods used in the modding and software patching communities, particularly in environments that require runtime code modification.

Credits

Detours: Redirecting C# Methods at Runtime