dao::Detour
class
A detour on one of the game’s functions, placed with Microsoft Detours.
Fn is the function’s pointer type, and its calling convention must match the game’s:
- A free function or a static method is __cdecl, unless the game’s code says otherwise: a
ret Nat its end means __stdcall. - A member function is __thiscall, which a free function cannot declare. Write the hook as __fastcall with
thisfirst and an unused int second (it receives edx), and declareFnthe same way.
With the wrong convention the stack is off when the function returns, so the crash comes after the hook, not in it.
using HandleFn = bool(__fastcall*)(Controller* self, int, const Message& m);
dao::Detour<HandleFn> g_handle;
bool __fastcall Hooked(Controller* self, int edx, const Message& m) { ... }
g_handle.Attach(dao::addr::kHandle, &Hooked);
... g_handle.Original()(self, 0, m);
The destructor removes the detour. Exceptions thrown from a hook are not caught, so guard the hook’s body yourself (dev/PLUGINS.md).
Declared in dao/Hook.h.
Functions
Attach()
bool Attach(AddressId id, Fn hook)
Replaces the function with hook.
| Parameter | |
|---|---|
id |
the function’s ID from dao/Addresses.h |
hook |
the replacement, of the same type as the function |
Returns true if the detour was placed. False, with nothing changed, if this detour is already attached, the running build has no such function, or Detours rejected the address.
AttachTo()
bool AttachTo(Fn target, Fn hook)
Places the detour on a function by where it is: one that is not the game’s own, a system DLL’s export or a method read from an object’s vtable.
Returns false if target is null, a detour is already placed, or Detours refuses
Detach()
void Detach()
Removes the detour and restores the function. Does nothing if it is not attached.
Detour()
Detour()=default
Detour()
Detour(const Detour &)=delete
IsAttached()
bool IsAttached() const
Returns true while the detour is in place.
operator=()
Detour & operator=(const Detour &)=delete
Original()
Fn Original() const
Returns the original function, for the hook to call. Null until Attach succeeds.
~Detour()
~Detour()