using System.Collections.Generic;
namespace WhiteMagic.Hooking;
///
/// Manages named, reversible byte patches against a .
/// Every patch records the bytes it replaced and can restore them later.
///
public sealed class PatchManager
{
private readonly MemoryBase _memory;
private readonly Dictionary _patches = new();
/// Creates a patch manager bound to the supplied memory reader.
public PatchManager(MemoryBase memory)
{
_memory = memory;
}
/// Creates a new patch and registers it with the manager.
public Patch Create(string name, IntPtr address, byte[] patchBytes)
{
var patch = new Patch(_memory, name, address, patchBytes);
_patches[name] = patch;
return patch;
}
/// Looks up a patch by name.
public Patch? this[string name]
{
get
{
_patches.TryGetValue(name, out Patch? patch);
return patch;
}
}
/// All patches registered in this manager.
public IEnumerable All => _patches.Values;
/// Removes every applied patch.
public void RestoreAll()
{
foreach (Patch patch in _patches.Values)
{
patch.Remove();
}
}
}