using System;
using System.Collections.Generic;
namespace WhiteMagic.Hooking;
///
/// Manages named inline detours against a .
/// Detours only work when operating in-process; applying a detour to an
/// external target will fail because the hook delegate lives in the host process.
///
public sealed class DetourManager
{
private readonly MemoryBase _memory;
private readonly Dictionary _detours = new();
/// Creates a detour manager bound to the supplied memory reader.
public DetourManager(MemoryBase memory)
{
_memory = memory;
}
///
/// Creates a new detour and registers it with the manager.
/// The delegate's type must match the native signature of
/// .
///
public Detour Create(string name, IntPtr target, Delegate hook)
{
var detour = new Detour(_memory, name, target, hook);
_detours[name] = detour;
return detour;
}
/// Looks up a detour by name.
public Detour? this[string name]
{
get
{
_detours.TryGetValue(name, out Detour? detour);
return detour;
}
}
/// All detours registered in this manager.
public IEnumerable All => _detours.Values;
/// Removes every applied detour, restoring original bytes.
public void RemoveAll()
{
foreach (Detour detour in _detours.Values)
{
detour.Remove();
}
}
}