1 /* ======== SourceHook ========
3 * No warranties of any kind
4 * ============================
9 * @brief Contains the public SourceHook API
12 #ifndef __SOURCEHOOK_H__
13 #define __SOURCEHOOK_H__
16 #define SH_GLOB_SHPTR g_SHPtr
19 #ifndef SH_GLOB_PLUGPTR
20 #define SH_GLOB_PLUGPTR g_PLID
23 #define SH_ASSERT(x) if (!(x)) __asm { int 3 }
26 #define SH_SYS_WIN32 1
27 #define SH_SYS_LINUX 2
30 # define SH_SYS SH_SYS_WIN32
31 #elif defined __linux__
32 # define SH_SYS SH_SYS_LINUX
34 # error Unsupported system
39 #define SH_COMP_MSVC 2
42 # define SH_COMP SH_COMP_MSVC
43 #elif defined __GNUC__
44 # define SH_COMP SH_COMP_GCC
46 # error Unsupported compiler
49 #if SH_COMP==SH_COMP_MSVC
50 # define vsnprintf _vsnprintf
53 #include "FastDelegate.h"
54 #include "sh_memfuncinfo.h"
55 #include "sh_memory.h"
61 // Flags returned by a plugin's api function.
62 // NOTE: order is crucial, as greater/less comparisons are made.
65 MRES_IGNORED=0, // plugin didn't take any action
66 MRES_HANDLED, // plugin did something, but real function should still be called
67 MRES_OVERRIDE, // call real function, but use my return value
68 MRES_SUPERCEDE, // skip real function; use my return value
74 const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions
77 * @brief A plugin typedef
79 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
80 * and every plugin has a unique identifier, everything is ok.
84 enum HookManagerAction
86 HA_GetInfo = 0, // -> Only store info
87 HA_Register, // -> Save the pointer for future reference
88 HA_Unregister // -> Clear the saved pointer
91 struct HookManagerInfo;
94 * @brief Pointer to hook manager type
96 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
98 * @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
99 * future reference (mainly if something should get hooked to its hookfunc)
101 typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
106 virtual void DeleteThis() = 0; // Ugly, I know
107 virtual bool IsEqual(ISHDelegate *other) = 0;
110 template <class T> class CSHDelegate : public ISHDelegate
114 CSHDelegate(T deleg) : m_Deleg(deleg)
123 bool IsEqual(ISHDelegate *other)
125 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
135 * @brief This structure contains information about a hook manager (hook manager)
137 struct HookManagerInfo
143 ISHDelegate *handler; //!< Pointer to the handler
144 bool paused; //!< If true, the hook should not be executed
145 Plugin plug; //!< The owner plugin
147 void *callclass; //!< Stores a call class for this interface
148 void *ptr; //!< Pointer to the interface instance
149 void *orig_entry; //!< The original vtable entry
150 std::list<Hook> hooks_pre; //!< A list of pre-hooks
151 std::list<Hook> hooks_post; //!< A list of post-hooks
152 bool operator ==(void *other) const
157 Plugin plug; //!< The owner plugin
158 const char *proto; //!< The prototype of the function the hook manager is responsible for
159 int vtbl_idx; //!< The vtable index
160 int vtbl_offs; //!< The vtable offset
161 int thisptr_offs; //!< The this-pointer-adjuster
162 HookManagerPubFunc func; //!< The interface to the hook manager
164 int hookfunc_vtbl_idx; //!< the vtable index of the hookfunc
165 int hookfunc_vtbl_offs; //!< the vtable offset of the hookfunc
166 void *hookfunc_inst; //!< Instance of the class the hookfunc is in
168 std::list<Iface> ifaces; //!< List of hooked interfaces
172 * @brief Structure describing a callclass
180 std::list<int> patches; //!< Already patched entries
181 bool operator ==(void *other) const
186 void *iface; //!< The iface pointer this callclass belongs to
187 size_t size; //!< The size of the callclass
188 void *ptr; //!< The actual "object"
189 typedef std::list<VTable> VTableList;
190 VTableList vtables; //!< Already known vtables
191 int refcounter; //!< How many times it was requested
193 bool operator ==(void *other) const
200 * @brief The main SourceHook interface
208 * @return True if the function succeeded, false otherwise
210 * @param plug The unique identifier of the plugin that calls this function
211 * @param iface The interface pointer
212 * @param ifacesize The size of the class iface points to
213 * @param myHookMan A hook manager function that should be capable of handling the function
214 * @param handler A pointer to a FastDelegate containing the hook handler
215 * @param post Set to true if you want a post handler
217 virtual bool AddHook(Plugin plug, void *iface, int ifacesize, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
220 * @brief Removes a hook.
222 * @return True if the function succeeded, false otherwise
224 * @param plug The unique identifier of the plugin that calls this function
225 * @param iface The interface pointer
226 * @param myHookMan A hook manager function that should be capable of handling the function
227 * @param handler A pointer to a FastDelegate containing the hook handler
228 * @param post Set to true if you want a post handler
230 virtual bool RemoveHook(Plugin plug, void *iface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
233 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
235 * @param plug The unique identifier of the plugin in question
237 virtual bool IsPluginInUse(Plugin plug) = 0;
240 * @brief Return a pointer to a callclass. Generate a new one if required.
242 * @param iface The interface pointer
243 * @param size Size of the class
245 virtual void *GetCallClass(void *iface, size_t size) = 0;
248 * @brief Release a callclass
250 * @param ptr Pointer to the callclass
252 virtual void ReleaseCallClass(void *ptr) = 0;
254 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
255 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
256 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
257 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
258 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
259 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
260 //////////////////////////////////////////////////////////////////////////
262 virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
263 virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
264 virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
265 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
266 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
267 virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
271 //////////////////////////////////////////////////////////////////////////
273 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
274 #define RETURN_META(result) do { SH_GLOB_SHPTR->SetRes(result); return; } while(0)
275 #define RETURN_META_VALUE(result, value) do { SH_GLOB_SHPTR->SetRes(result); return (value); } while(0)
277 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
278 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
279 #define META_RESULT_ORIG_RET(type) *(const type *)SH_GLOB_SHPTR->GetOrigRet()
280 #define META_RESULT_OVERRIDE_RET(type) *(const type *)SH_GLOB_SHPTR->GetOverrideRet()
281 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
285 * @brief Get/generate callclass for an interface pointer
287 * @param ifacetype The type of the interface
288 * @param ifaceptr The interface pointer
290 #define SH_GET_CALLCLASS(ifacetype, ifaceptr) reinterpret_cast<ifacetype*>(SH_GLOB_SHPTR->GetCallClass(ifaceptr, sizeof(ifacetype)))
291 #define SH_RELEASE_CALLCLASS(ptr) SH_GLOB_SHPTR->ReleaseCallClass(reinterpret_cast<void*>(ptr))
293 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
294 SourceHook::SH_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
295 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
296 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
297 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
298 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
300 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
301 SourceHook::SH_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
302 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
303 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
304 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
305 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
310 //////////////////////////////////////////////////////////////////////////
311 #define SH_FHCls(ift, iff, ov) FHCls_##ift##iff##ov
313 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
314 static int HookManPubFunc(HookManagerAction action, HookManagerInfo *param) \
316 if (action == HA_GetInfo) \
318 param->proto = ms_Proto; \
320 GetFuncInfo(funcptr, mfi); \
321 param->vtbl_idx = mfi.vtblindex; \
322 param->vtbl_offs = mfi.vtbloffs; \
323 param->thisptr_offs = mfi.thisptroffs; \
324 if (param->thisptr_offs < 0) \
325 return 2; /*No virtual inheritance supported*/ \
326 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
327 param->hookfunc_vtbl_idx = mfi.vtblindex; \
328 param->hookfunc_vtbl_offs = mfi.vtbloffs; \
329 param->hookfunc_inst = (void*)&ms_Inst; \
332 else if (action == HA_Register) \
337 else if (action == HA_Unregister) \
346 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
347 namespace SourceHook \
349 struct SH_FHCls(ifacetype,ifacefunc,overload) \
351 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
352 static HookManagerInfo *ms_HI; \
353 static const char *ms_Proto; \
354 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
356 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto) \
358 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
359 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
360 HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
361 bool SH_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
362 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
364 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, sizeof(ifacetype), \
365 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
366 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
368 bool SH_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
369 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
371 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
372 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, \
373 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
377 #define SH_SETUPCALLS(rettype) \
378 /* 1) Find the iface ptr */ \
379 /* 1.1) Adjust to original this pointer */ \
380 void *origthis = this - ms_HI->thisptr_offs; \
381 std::list<HookManagerInfo::Iface>::iterator ifaceiter = std::find(ms_HI->ifaces.begin(), \
382 ms_HI->ifaces.end(), origthis); \
383 SH_ASSERT(ifaceiter != ms_HI->ifaces.end()); \
384 HookManagerInfo::Iface &ci = *ifaceiter; \
385 /* 2) Declare some vars and set it up */ \
386 std::list<HookManagerInfo::Iface::Hook> &prelist = ci.hooks_pre; \
387 std::list<HookManagerInfo::Iface::Hook> &postlist = ci.hooks_post; \
388 rettype orig_ret, override_ret, plugin_ret; \
389 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
390 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
391 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
392 status = MRES_IGNORED; \
393 SH_GLOB_SHPTR->SetIfacePtr(ci.ptr); \
394 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
395 SH_GLOB_SHPTR->SetOverrideRet(NULL);
397 #define SH_CALL_HOOKS(post, params) \
398 prev_res = MRES_IGNORED; \
399 for (std::list<HookManagerInfo::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
401 if (hiter->paused) continue; \
402 cur_res = MRES_IGNORED; \
403 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
404 prev_res = cur_res; \
405 if (cur_res > status) \
407 if (cur_res >= MRES_OVERRIDE) \
409 override_ret = plugin_ret; \
410 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
414 #define SH_CALL_ORIG(ifacetype, ifacefunc, params) \
415 if (status != MRES_SUPERCEDE) \
416 orig_ret = reinterpret_cast<ifacetype*>(ci.callclass)->ifacefunc params; \
418 orig_ret = override_ret;
420 #define SH_RETURN() \
421 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
423 #define SH_HANDLEFUNC(ifacetype, ifacefunc, params, rettype) \
424 SH_SETUPCALLS(rettype) \
425 SH_CALL_HOOKS(pre, params) \
426 SH_CALL_ORIG(ifacetype, ifacefunc, params) \
427 SH_CALL_HOOKS(post, params) \
430 //////////////////////////////////////////////////////////////////////////
431 #define SH_SETUPCALLS_void() \
432 /* 1) Find the iface ptr */ \
433 /* 1.1) Adjust to original this pointer */ \
434 void *origthis = this - ms_HI->thisptr_offs; \
435 std::list<HookManagerInfo::Iface>::iterator ifaceiter = std::find(ms_HI->ifaces.begin(), \
436 ms_HI->ifaces.end(), origthis); \
437 SH_ASSERT(ifaceiter != ms_HI->ifaces.end()); \
438 HookManagerInfo::Iface &ci = *ifaceiter; \
439 /* 2) Declare some vars and set it up */ \
440 std::list<HookManagerInfo::Iface::Hook> &prelist = ci.hooks_pre; \
441 std::list<HookManagerInfo::Iface::Hook> &postlist = ci.hooks_post; \
442 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
443 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
444 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
445 status = MRES_IGNORED; \
446 SH_GLOB_SHPTR->SetIfacePtr(ci.ptr); \
447 SH_GLOB_SHPTR->SetOverrideRet(NULL);
449 #define SH_CALL_HOOKS_void(post, params) \
450 prev_res = MRES_IGNORED; \
451 for (std::list<HookManagerInfo::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
453 if (hiter->paused) continue; \
454 cur_res = MRES_IGNORED; \
455 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
456 prev_res = cur_res; \
457 if (cur_res > status) \
461 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, params) \
462 if (status != MRES_SUPERCEDE) \
463 reinterpret_cast<ifacetype*>(ci.callclass)->ifacefunc params;
465 #define SH_RETURN_void()
467 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, params) \
468 SH_SETUPCALLS_void() \
469 SH_CALL_HOOKS_void(pre, params) \
470 SH_CALL_ORIG_void(ifacetype, ifacefunc, params) \
471 SH_CALL_HOOKS_void(post, params) \
475 // Special vafmt handlers
476 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, params_orig, params_plug, rettype) \
477 SH_SETUPCALLS(rettype) \
478 SH_CALL_HOOKS(pre, params_plug) \
479 SH_CALL_ORIG(ifacetype, ifacefunc, params_orig) \
480 SH_CALL_HOOKS(post, params_plug) \
483 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, params_orig, params_plug) \
484 SH_SETUPCALLS_void() \
485 SH_CALL_HOOKS_void(pre, params_plug) \
486 SH_CALL_ORIG_void(ifacetype, ifacefunc, params_orig) \
487 SH_CALL_HOOKS_void(post, params_plug) \
490 //////////////////////////////////////////////////////////////////////////
494 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
495 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @)> \
496 (&ifacetype::ifacefunc))) \
497 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
498 virtual rettype Func(@param%% p%%|, @) attr \
499 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@p%%|, @), rettype); } \
500 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @)
502 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
503 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @)> \
504 (&ifacetype::ifacefunc))) \
505 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
506 virtual void Func(@param%% p%%|, @) attr \
507 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@p%%|, @)); } \
508 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @)
510 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
511 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
512 (&ifacetype::ifacefunc))) \
513 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
514 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) attr \
516 char buf[STRBUF_LEN]; \
519 vsnprintf(buf, sizeof(buf), fmt, ap); \
521 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
523 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...")
525 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
526 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
527 (&ifacetype::ifacefunc))) \
528 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
529 virtual void Func(@param%% p%%|, @@, @const char *, ...) attr \
531 char buf[STRBUF_LEN]; \
534 vsnprintf(buf, sizeof(buf), fmt, ap); \
536 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
538 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...")
543 #define SH_DECL_HOOK1(ifacetype, ifacefunc, attr, overload, rettype, param1) \
544 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload) \
545 typedef fastdelegate::FastDelegate1<param1, rettype> FD; \
546 virtual rettype Func(param1 p1) \
547 { SH_HANDLEFUNC(ifacetype, ifacefunc, (p1), rettype); } \
548 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype "|" #param1)
554 // The pope is dead. -> :(