1 /* ======== SourceHook ========
3 * No warranties of any kind
4 * ============================
9 * @brief Contains the public SourceHook API
12 #ifndef __SOURCEHOOK_H__
13 #define __SOURCEHOOK_H__
15 #define SH_ASSERT(x) if (!(x)) __asm { int 3 }
18 #define SH_SYS_WIN32 1
19 #define SH_SYS_LINUX 2
22 # define SH_SYS SH_SYS_WIN32
23 #elif defined __linux__
24 # define SH_SYS SH_SYS_LINUX
26 # error Unsupported system
31 #define SH_COMP_MSVC 2
34 # define SH_COMP SH_COMP_MSVC
35 #elif defined __GNUC__
36 # define SH_COMP SH_COMP_GCC
38 # error Unsupported compiler
41 #if SH_COMP==SH_COMP_MSVC
42 # define vsnprintf _vsnprintf
45 #include "FastDelegate.h"
46 #include "sh_memfuncinfo.h"
47 #include "sh_memory.h"
53 // Flags returned by a plugin's api function.
54 // NOTE: order is crucial, as greater/less comparisons are made.
57 MRES_IGNORED=0, // plugin didn't take any action
58 MRES_HANDLED, // plugin did something, but real function should still be called
59 MRES_OVERRIDE, // call real function, but use my return value
60 MRES_SUPERCEDE, // skip real function; use my return value
66 const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions
69 * @brief A plugin typedef
71 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
72 * and every plugin has a unique identifier, everything is ok.
78 HA_GetInfo = 0, // -> Only store info
79 HA_Register, // -> Save the pointer for future reference
80 HA_Unregister, // -> Clear the saved pointer
81 HA_IfaceAdded, // -> Request call class
82 HA_IfaceRemoved // -> Release call class
88 * @brief Pointer to hooker type
90 * A "hooker" is a the only thing that knows the actual protoype of the function at compile time.
92 * @param hi A pointer to a HookerInfo structure. The hooker should fill it and store it for
93 * future reference (mainly if something should get hooked to its hookfunc)
95 typedef int (*Hooker)(HookerAction ha, HookerInfo *hi);
100 virtual void DeleteThis() = 0; // Ugly, I know
101 virtual bool IsEqual(ISHDelegate *other) = 0;
104 template <class T> class CSHDelegate : public ISHDelegate
108 CSHDelegate(T deleg) : m_Deleg(deleg)
117 bool IsEqual(ISHDelegate *other)
119 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
129 * @brief This structure contains information about a hooker (hook manager)
137 ISHDelegate *handler; //!< Pointer to the handler
138 Plugin plug; //!< The owner plugin
140 void *callclass; //!< Stores a call class for this interface
141 void *ptr; //!< Pointer to the interface instance
142 void *orig_entry; //!< The original vtable entry
143 std::list<Hook> hooks_pre; //!< A list of pre-hooks
144 std::list<Hook> hooks_post; //!< A list of post-hooks
145 bool operator ==(void *other) const
150 Plugin plug; //!< The owner plugin
151 const char *proto; //!< The prototype of the function the hooker is responsible for
152 int vtbl_idx; //!< The vtable index
153 int vtbl_offs; //!< The vtable offset
154 int thisptr_offs; //!< The this-pointer-adjuster
155 Hooker func; //!< The interface to the hooker
157 int hookfunc_vtbl_idx; //!< the vtable index of the hookfunc
158 int hookfunc_vtbl_offs; //!< the vtable offset of the hookfunc
159 void *hookfunc_inst; //!< Instance of the class the hookfunc is in
161 std::list<Iface> ifaces; //!< List of hooked interfaces
165 * @brief Structure describing a callclass
173 std::list<int> patches; //!< Already patched entries
174 bool operator ==(void *other) const
179 void *iface; //!< The iface pointer this callclass belongs to
180 size_t size; //!< The size of the callclass
181 void *ptr; //!< The actual "object"
182 typedef std::list<VTable> VTableList;
183 VTableList vtables; //!< Already known vtables
184 int refcounter; //!< How many times it was requested
186 bool operator ==(void *other) const
193 * @brief The main SourceHook interface
201 * @return True if the function succeeded, false otherwise
203 * @param plug The unique identifier of the plugin that calls this function
204 * @param iface The interface pointer
205 * @param ifacesize The size of the class iface points to
206 * @param myHooker A hooker function that should be capable of handling the function
207 * @param handler A pointer to a FastDelegate containing the hook handler
208 * @param post Set to true if you want a post handler
210 virtual bool AddHook(Plugin plug, void *iface, int ifacesize, Hooker myHooker, ISHDelegate *handler, bool post) = 0;
213 * @brief Removes a hook.
215 * @return True if the function succeeded, false otherwise
217 * @param plug The unique identifier of the plugin that calls this function
218 * @param iface The interface pointer
219 * @param myHooker A hooker function that should be capable of handling the function
220 * @param handler A pointer to a FastDelegate containing the hook handler
221 * @param post Set to true if you want a post handler
223 virtual bool RemoveHook(Plugin plug, void *iface, Hooker myHooker, ISHDelegate *handler, bool post) = 0;
226 * @brief Checks whether a plugin has (a) hooker(s) that is/are currently used by other plugins
228 * @param plug The unique identifier of the plugin in question
230 virtual bool IsPluginInUse(Plugin plug) = 0;
233 * @brief Return a pointer to a callclass. Generate a new one if required.
235 * @param iface The interface pointer
236 * @param size Size of the class
238 virtual void *GetCallClass(void *iface, size_t size) = 0;
241 * @brief Release a callclass
243 * @param ptr Pointer to the callclass
245 virtual void ReleaseCallClass(void *ptr) = 0;
247 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
248 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
249 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
250 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
251 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
252 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
253 //////////////////////////////////////////////////////////////////////////
255 virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
256 virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
257 virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
258 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
259 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
260 virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
264 //////////////////////////////////////////////////////////////////////////
266 #define SET_META_RESULT(result) g_SHPtr->SetRes(result)
267 #define RETURN_META(result) do { g_SHPtr->SetRes(result); return; } while(0)
268 #define RETURN_META_VALUE(result, value) do { g_SHPtr->SetRes(result); return (value); } while(0)
270 #define META_RESULT_STATUS g_SHPtr->GetStatus()
271 #define META_RESULT_PREVIOUS g_SHPtr->GetPrevRes()
272 #define META_RESULT_ORIG_RET(type) *(const type *)g_SHPtr->GetOrigRet()
273 #define META_RESULT_OVERRIDE_RET(type) *(const type *)g_SHPtr->GetOverrideRet()
274 #define META_IFACEPTR g_SHPtr->GetIfacePtr()
278 * @brief Get/generate callclass for an interface pointer
280 * @param ifacetype The type of the interface
281 * @param ifaceptr The interface pointer
283 #define SH_GET_CALLCLASS(ifacetype, ifaceptr) reinterpret_cast<ifacetype*>(g_SHPtr->GetCallClass(ifaceptr, sizeof(ifacetype)))
284 #define SH_RELEASE_CALLCLASS(ptr) g_SHPtr->ReleaseCallClass(reinterpret_cast<void*>(ptr))
286 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
287 SourceHook::SH_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
288 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
289 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
290 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
291 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
293 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
294 SourceHook::SH_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
295 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
296 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
297 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
298 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
303 //////////////////////////////////////////////////////////////////////////
304 #define SH_FHCls(ift, iff, ov) FHCls_##ift##iff##ov
306 #define SHINT_MAKE_HOOKERPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
307 static int Hooker(HookerAction action, HookerInfo *param) \
309 if (action == HA_GetInfo) \
311 param->proto = ms_Proto; \
313 GetFuncInfo(funcptr, mfi); \
314 param->vtbl_idx = mfi.vtblindex; \
315 param->vtbl_offs = mfi.vtbloffs; \
316 param->thisptr_offs = mfi.thisptroffs; \
317 if (param->thisptr_offs) \
318 return 2; /*No virtual inheritance supported*/ \
319 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
320 param->hookfunc_vtbl_idx = mfi.vtblindex; \
321 param->hookfunc_vtbl_offs = mfi.vtbloffs; \
322 param->hookfunc_inst = (void*)&ms_Inst; \
325 else if (action == HA_Register) \
330 else if (action == HA_Unregister) \
339 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
340 namespace SourceHook \
342 struct SH_FHCls(ifacetype,ifacefunc,overload) \
344 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
345 static HookerInfo *ms_HI; \
346 static const char *ms_Proto; \
347 SHINT_MAKE_HOOKERPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
349 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto) \
351 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
352 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
353 HookerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
354 bool SH_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
355 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
357 return g_SHPtr->AddHook(g_Plug, iface, sizeof(ifacetype), \
358 SH_FHCls(ifacetype,ifacefunc,overload)::Hooker, \
359 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
361 bool SH_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
362 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
364 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
365 return g_SHPtr->RemoveHook(g_Plug, iface, \
366 SH_FHCls(ifacetype,ifacefunc,overload)::Hooker, &tmp, post); \
370 #define SH_SETUPCALLS(rettype) \
371 /* 1) Find the iface ptr */ \
372 /* 1.1) Adjust to original this pointer */ \
373 void *origthis = this - ms_HI->thisptr_offs; \
374 std::list<HookerInfo::Iface>::iterator ifaceiter = std::find(ms_HI->ifaces.begin(), \
375 ms_HI->ifaces.end(), origthis); \
376 SH_ASSERT(ifaceiter != ms_HI->ifaces.end()); \
377 HookerInfo::Iface &ci = *ifaceiter; \
378 /* 2) Declare some vars and set it up */ \
379 std::list<HookerInfo::Iface::Hook> &prelist = ci.hooks_pre; \
380 std::list<HookerInfo::Iface::Hook> &postlist = ci.hooks_post; \
381 rettype orig_ret, override_ret, plugin_ret; \
382 META_RES &cur_res = g_SHPtr->GetCurResRef(); \
383 META_RES &prev_res = g_SHPtr->GetPrevResRef(); \
384 META_RES &status = g_SHPtr->GetStatusRef(); \
385 status = MRES_IGNORED; \
386 g_SHPtr->SetIfacePtr(ci.ptr); \
387 g_SHPtr->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
388 g_SHPtr->SetOverrideRet(NULL);
390 #define SH_CALL_HOOKS(post, params) \
391 prev_res = MRES_IGNORED; \
392 for (std::list<HookerInfo::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
394 cur_res = MRES_IGNORED; \
395 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
396 prev_res = cur_res; \
397 if (cur_res > status) \
399 if (cur_res >= MRES_OVERRIDE) \
401 override_ret = plugin_ret; \
402 g_SHPtr->SetOverrideRet(&override_ret); \
406 #define SH_CALL_ORIG(ifacetype, ifacefunc, params) \
407 if (status != MRES_SUPERCEDE) \
408 orig_ret = reinterpret_cast<ifacetype*>(ci.callclass)->ifacefunc params; \
410 orig_ret = override_ret;
412 #define SH_RETURN() \
413 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
415 #define SH_HANDLEFUNC(ifacetype, ifacefunc, params, rettype) \
416 SH_SETUPCALLS(rettype) \
417 SH_CALL_HOOKS(pre, params) \
418 SH_CALL_ORIG(ifacetype, ifacefunc, params) \
419 SH_CALL_HOOKS(post, params) \
422 //////////////////////////////////////////////////////////////////////////
423 #define SH_SETUPCALLS_void() \
424 /* 1) Find the iface ptr */ \
425 /* 1.1) Adjust to original this pointer */ \
426 void *origthis = this - ms_HI->thisptr_offs; \
427 std::list<HookerInfo::Iface>::iterator ifaceiter = std::find(ms_HI->ifaces.begin(), \
428 ms_HI->ifaces.end(), origthis); \
429 SH_ASSERT(ifaceiter != ms_HI->ifaces.end()); \
430 HookerInfo::Iface &ci = *ifaceiter; \
431 /* 2) Declare some vars and set it up */ \
432 std::list<HookerInfo::Iface::Hook> &prelist = ci.hooks_pre; \
433 std::list<HookerInfo::Iface::Hook> &postlist = ci.hooks_post; \
434 META_RES &cur_res = g_SHPtr->GetCurResRef(); \
435 META_RES &prev_res = g_SHPtr->GetPrevResRef(); \
436 META_RES &status = g_SHPtr->GetStatusRef(); \
437 status = MRES_IGNORED; \
438 g_SHPtr->SetIfacePtr(ci.ptr); \
439 g_SHPtr->SetOverrideRet(NULL);
441 #define SH_CALL_HOOKS_void(post, params) \
442 prev_res = MRES_IGNORED; \
443 for (std::list<HookerInfo::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
445 cur_res = MRES_IGNORED; \
446 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
447 prev_res = cur_res; \
448 if (cur_res > status) \
452 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, params) \
453 if (status != MRES_SUPERCEDE) \
454 reinterpret_cast<ifacetype*>(ci.callclass)->ifacefunc params;
456 #define SH_RETURN_void()
458 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, params) \
459 SH_SETUPCALLS_void() \
460 SH_CALL_HOOKS_void(pre, params) \
461 SH_CALL_ORIG_void(ifacetype, ifacefunc, params) \
462 SH_CALL_HOOKS_void(post, params) \
466 // Special vafmt handlers
467 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, params_orig, params_plug, rettype) \
468 SH_SETUPCALLS(rettype) \
469 SH_CALL_HOOKS(pre, params_plug) \
470 SH_CALL_ORIG(ifacetype, ifacefunc, params_orig) \
471 SH_CALL_HOOKS(post, params_plug) \
474 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, params_orig, params_plug) \
475 SH_SETUPCALLS_void() \
476 SH_CALL_HOOKS_void(pre, params_plug) \
477 SH_CALL_ORIG_void(ifacetype, ifacefunc, params_orig) \
478 SH_CALL_HOOKS_void(post, params_plug) \
481 //////////////////////////////////////////////////////////////////////////
485 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
486 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @)> \
487 (&ifacetype::ifacefunc))) \
488 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
489 virtual rettype Func(@param%% p%%|, @) attr \
490 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@p%%|, @), rettype); } \
491 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @)
493 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
494 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @)> \
495 (&ifacetype::ifacefunc))) \
496 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
497 virtual void Func(@param%% p%%|, @) attr \
498 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@p%%|, @)); } \
499 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @)
501 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
502 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
503 (&ifacetype::ifacefunc))) \
504 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
505 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) attr \
507 char buf[STRBUF_LEN]; \
510 vsnprintf(buf, sizeof(buf), fmt, ap); \
512 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
514 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...")
516 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
517 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
518 (&ifacetype::ifacefunc))) \
519 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
520 virtual void Func(@param%% p%%|, @@, @const char *, ...) attr \
522 char buf[STRBUF_LEN]; \
525 vsnprintf(buf, sizeof(buf), fmt, ap); \
527 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
529 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...")
534 #define SH_DECL_HOOK1(ifacetype, ifacefunc, attr, overload, rettype, param1) \
535 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload) \
536 typedef fastdelegate::FastDelegate1<param1, rettype> FD; \
537 virtual rettype Func(param1 p1) \
538 { SH_HANDLEFUNC(ifacetype, ifacefunc, (p1), rettype); } \
539 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype "|" #param1)
545 // The pope is dead. -> :(