26 return KHook::Action::Override;
28 return KHook::Action::Supersede;
30 return KHook::Action::Ignore;
54template <
template <
class,
class,
class...>
class HookKind,
class Object,
class Ret,
class...
Args>
59 template <
class BeforeHandler,
class AfterHandler>
61 : _before(WrapBefore(std::forward<BeforeHandler>(before))), _after(std::forward<AfterHandler>(after))
63 static_assert(!std::is_polymorphic_v<InstalledHook>,
"KHook dispatches through a raw member address");
65 !std::is_null_pointer_v<std::decay_t<BeforeHandler>> || !std::is_null_pointer_v<std::decay_t<AfterHandler>>,
66 "a hook with neither handler would install nothing");
69 Hook.AddContext(
this, &InstalledHook::RunBefore, &InstalledHook::RunAfter);
76 using Before = std::move_only_function<HookResult<Ret>(Object&,
Args...)>;
77 using After = std::move_only_function<void(Object&,
Args...)>;
80 template <
class Handler>
81 static Before WrapBefore(Handler&& handler)
83 if constexpr (std::is_null_pointer_v<std::decay_t<Handler>>)
87 else if constexpr (std::is_void_v<std::invoke_result_t<std::decay_t<Handler>&, Object&,
Args...>>)
89 return [handler = std::forward<Handler>(handler)](Object& self,
Args... args)
mutable {
90 handler(self, std::forward<Args>(args)...);
96 return std::forward<Handler>(handler);
100 KHook::Return<Ret> RunBefore(Object* self,
Args... args)
105 KHook::Return<Ret> RunAfter(Object* self, Args... args)
109 _after(*self, std::forward<Args>(args)...);
111 return ToKHook(HookResult<Ret>{});
125 return Subscription([hook = std::move(hook)]()
mutable { hook.reset(); });
141template <
class Iface,
class Ret,
class... Args,
class Before,
class After = std::nullptr_t>
143 After&& after =
nullptr)
146 auto hook = std::make_unique<Installed>(std::forward<Before>(before), std::forward<After>(after));
147 hook->Hook.Configure(method);
150 hook->Hook.Add(instance);
167template <
class Object,
class Ret,
class... Args,
class Before,
class After = std::nullptr_t>
169 Before&& before, After&& after =
nullptr)
173 return std::unexpected(
Error::Unsupported(std::format(
"the {} vtable slot did not bind", name)));
177 auto hook = std::make_unique<Installed>(std::forward<Before>(before), std::forward<After>(after));
178 hook->Hook.Configure(function.Index());
181 void* asObject = function.Table();
182 hook->Hook.AddGlobal(
reinterpret_cast<Object*
>(&asObject));
184 Log::Info(
"{} hook installed (vtable index {}).", name, function.Index());
199template <
class Object,
class Ret,
class... Args,
class Before,
class After = std::nullptr_t>
201 const ClassTable& table, Before&& before, After&& after =
nullptr)
205 return std::unexpected(
Error::Unsupported(std::format(
"the {} class table did not bind", name)));
209 auto hook = std::make_unique<Installed>(std::forward<Before>(before), std::forward<After>(after));
210 hook->Hook.Configure(method);
213 void* asObject = table.
Ptr();
214 hook->Hook.AddGlobal(
reinterpret_cast<Object*
>(&asObject));
216 Log::Info(
"{} hook installed (vtable index {}).", name, KHook::GetVtableIndex(method));
231template <
class Object,
class Ret,
class... Args,
class Before,
class After = std::nullptr_t>
233 Before&& before, After&& after =
nullptr)
237 return std::unexpected(
Error::Unsupported(std::format(
"the {} signature did not bind", name)));
241 auto hook = std::make_unique<Installed>(std::forward<Before>(before), std::forward<After>(after));
242 hook->Hook.Configure(
static_cast<const void*
>(function.Ptr()));
249template <
class Iface,
class Ret,
class... Args,
class... Passed>
252 return KHook::CallOriginal(method, instance, std::forward<Passed>(args)...);
void * Ptr() const noexcept
A hook before-handler's verdict.
HookAction Action() const noexcept
InstalledHook(BeforeHandler &&before, AfterHandler &&after)
HookKind< Object, Ret, Args... > Hook
InstalledHook(const InstalledHook &)=delete
InstalledHook & operator=(const InstalledHook &)=delete
Owns one registration and releases it on destruction.
The argument types a command handler's parameter list is written in.
KHook::Action ToKHookAction(HookAction action)
Subscription ToSubscription(std::unique_ptr< Hook > hook)
KHook::Return< Ret > ToKHook(HookResult< Ret > result)
void Info(std::format_string< Args... > fmt, Args &&... args)
Result< Subscription > HookVirtual(std::string_view name, const VirtualFn< Ret(Object *, Args...)> &function, Before &&before, After &&after=nullptr)
Hook a gamedata-bound virtual function on objects sharing its class vtable.
Ret CallOriginal(Ret(Iface::*method)(Args...), Iface *instance, Passed &&... args)
Result< Subscription > HookFunction(std::string_view name, const Fn< Ret(Object *, Args...)> &function, Before &&before, After &&after=nullptr)
Hook a signature-bound function at its entry.
Result< Subscription > HookTable(std::string_view name, Ret(Object::*method)(Args...), const ClassTable &table, Before &&before, After &&after=nullptr)
Hook an SDK-declared virtual on every object sharing a gamedata-found class table.
std::expected< T, Error > Result
@ Replace
Let it run, but return this value instead of its own.
@ Block
Skip it entirely and return this value.
Subscription HookInterface(Ret(Iface::*method)(Args...), Iface *instance, Before &&before, After &&after=nullptr)
Hook one virtual method of an engine interface for a subscription's lifetime.
static InstalledScan Installed()
static Error Unsupported(std::string detail)