Added possibility to specify your own g_SHPtr / g_Plug, removed all occurencies of The Word, fixed UnloadPlugin and CompleteShutdown
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_Plug
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 Plugin plug; //!< The owner plugin
146 void *callclass; //!< Stores a call class for this interface
147 void *ptr; //!< Pointer to the interface instance
148 void *orig_entry; //!< The original vtable entry
149 std::list<Hook> hooks_pre; //!< A list of pre-hooks
150 std::list<Hook> hooks_post; //!< A list of post-hooks
151 bool operator ==(void *other) const
156 Plugin plug; //!< The owner plugin
157 const char *proto; //!< The prototype of the function the hook manager is responsible for
158 int vtbl_idx; //!< The vtable index
159 int vtbl_offs; //!< The vtable offset
160 int thisptr_offs; //!< The this-pointer-adjuster
161 HookManagerPubFunc func; //!< The interface to the hook manager
163 int hookfunc_vtbl_idx; //!< the vtable index of the hookfunc
164 int hookfunc_vtbl_offs; //!< the vtable offset of the hookfunc
165 void *hookfunc_inst; //!< Instance of the class the hookfunc is in
167 std::list<Iface> ifaces; //!< List of hooked interfaces
171 * @brief Structure describing a callclass
179 std::list<int> patches; //!< Already patched entries
180 bool operator ==(void *other) const
185 void *iface; //!< The iface pointer this callclass belongs to
186 size_t size; //!< The size of the callclass
187 void *ptr; //!< The actual "object"
188 typedef std::list<VTable> VTableList;
189 VTableList vtables; //!< Already known vtables
190 int refcounter; //!< How many times it was requested
192 bool operator ==(void *other) const
199 * @brief The main SourceHook interface
207 * @return True if the function succeeded, false otherwise
209 * @param plug The unique identifier of the plugin that calls this function
210 * @param iface The interface pointer
211 * @param ifacesize The size of the class iface points to
212 * @param myHookMan A hook manager function that should be capable of handling the function
213 * @param handler A pointer to a FastDelegate containing the hook handler
214 * @param post Set to true if you want a post handler
216 virtual bool AddHook(Plugin plug, void *iface, int ifacesize, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
219 * @brief Removes a hook.
221 * @return True if the function succeeded, false otherwise
223 * @param plug The unique identifier of the plugin that calls this function
224 * @param iface The interface pointer
225 * @param myHookMan A hook manager function that should be capable of handling the function
226 * @param handler A pointer to a FastDelegate containing the hook handler
227 * @param post Set to true if you want a post handler
229 virtual bool RemoveHook(Plugin plug, void *iface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
232 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
234 * @param plug The unique identifier of the plugin in question
236 virtual bool IsPluginInUse(Plugin plug) = 0;
239 * @brief Return a pointer to a callclass. Generate a new one if required.
241 * @param iface The interface pointer
242 * @param size Size of the class
244 virtual void *GetCallClass(void *iface, size_t size) = 0;
247 * @brief Release a callclass
249 * @param ptr Pointer to the callclass
251 virtual void ReleaseCallClass(void *ptr) = 0;
253 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
254 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
255 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
256 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
257 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
258 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
259 //////////////////////////////////////////////////////////////////////////
261 virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
262 virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
263 virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
264 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
265 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
266 virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
270 //////////////////////////////////////////////////////////////////////////
272 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
273 #define RETURN_META(result) do { SH_GLOB_SHPTR->SetRes(result); return; } while(0)
274 #define RETURN_META_VALUE(result, value) do { SH_GLOB_SHPTR->SetRes(result); return (value); } while(0)
276 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
277 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
278 #define META_RESULT_ORIG_RET(type) *(const type *)SH_GLOB_SHPTR->GetOrigRet()
279 #define META_RESULT_OVERRIDE_RET(type) *(const type *)SH_GLOB_SHPTR->GetOverrideRet()
280 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
284 * @brief Get/generate callclass for an interface pointer
286 * @param ifacetype The type of the interface
287 * @param ifaceptr The interface pointer
289 #define SH_GET_CALLCLASS(ifacetype, ifaceptr) reinterpret_cast<ifacetype*>(SH_GLOB_SHPTR->GetCallClass(ifaceptr, sizeof(ifacetype)))
290 #define SH_RELEASE_CALLCLASS(ptr) SH_GLOB_SHPTR->ReleaseCallClass(reinterpret_cast<void*>(ptr))
292 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
293 SourceHook::SH_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
294 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
295 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
296 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
297 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
299 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
300 SourceHook::SH_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
301 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
302 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
303 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
304 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
309 //////////////////////////////////////////////////////////////////////////
310 #define SH_FHCls(ift, iff, ov) FHCls_##ift##iff##ov
312 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
313 static int HookManPubFunc(HookManagerAction action, HookManagerInfo *param) \
315 if (action == HA_GetInfo) \
317 param->proto = ms_Proto; \
319 GetFuncInfo(funcptr, mfi); \
320 param->vtbl_idx = mfi.vtblindex; \
321 param->vtbl_offs = mfi.vtbloffs; \
322 param->thisptr_offs = mfi.thisptroffs; \
323 if (param->thisptr_offs < 0) \
324 return 2; /*No virtual inheritance supported*/ \
325 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
326 param->hookfunc_vtbl_idx = mfi.vtblindex; \
327 param->hookfunc_vtbl_offs = mfi.vtbloffs; \
328 param->hookfunc_inst = (void*)&ms_Inst; \
331 else if (action == HA_Register) \
336 else if (action == HA_Unregister) \
345 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
346 namespace SourceHook \
348 struct SH_FHCls(ifacetype,ifacefunc,overload) \
350 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
351 static HookManagerInfo *ms_HI; \
352 static const char *ms_Proto; \
353 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
355 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto) \
357 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
358 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
359 HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
360 bool SH_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
361 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
363 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, sizeof(ifacetype), \
364 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
365 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
367 bool SH_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
368 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
370 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
371 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, \
372 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
376 #define SH_SETUPCALLS(rettype) \
377 /* 1) Find the iface ptr */ \
378 /* 1.1) Adjust to original this pointer */ \
379 void *origthis = this - ms_HI->thisptr_offs; \
380 std::list<HookManagerInfo::Iface>::iterator ifaceiter = std::find(ms_HI->ifaces.begin(), \
381 ms_HI->ifaces.end(), origthis); \
382 SH_ASSERT(ifaceiter != ms_HI->ifaces.end()); \
383 HookManagerInfo::Iface &ci = *ifaceiter; \
384 /* 2) Declare some vars and set it up */ \
385 std::list<HookManagerInfo::Iface::Hook> &prelist = ci.hooks_pre; \
386 std::list<HookManagerInfo::Iface::Hook> &postlist = ci.hooks_post; \
387 rettype orig_ret, override_ret, plugin_ret; \
388 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
389 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
390 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
391 status = MRES_IGNORED; \
392 SH_GLOB_SHPTR->SetIfacePtr(ci.ptr); \
393 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
394 SH_GLOB_SHPTR->SetOverrideRet(NULL);
396 #define SH_CALL_HOOKS(post, params) \
397 prev_res = MRES_IGNORED; \
398 for (std::list<HookManagerInfo::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
400 cur_res = MRES_IGNORED; \
401 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
402 prev_res = cur_res; \
403 if (cur_res > status) \
405 if (cur_res >= MRES_OVERRIDE) \
407 override_ret = plugin_ret; \
408 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
412 #define SH_CALL_ORIG(ifacetype, ifacefunc, params) \
413 if (status != MRES_SUPERCEDE) \
414 orig_ret = reinterpret_cast<ifacetype*>(ci.callclass)->ifacefunc params; \
416 orig_ret = override_ret;
418 #define SH_RETURN() \
419 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
421 #define SH_HANDLEFUNC(ifacetype, ifacefunc, params, rettype) \
422 SH_SETUPCALLS(rettype) \
423 SH_CALL_HOOKS(pre, params) \
424 SH_CALL_ORIG(ifacetype, ifacefunc, params) \
425 SH_CALL_HOOKS(post, params) \
428 //////////////////////////////////////////////////////////////////////////
429 #define SH_SETUPCALLS_void() \
430 /* 1) Find the iface ptr */ \
431 /* 1.1) Adjust to original this pointer */ \
432 void *origthis = this - ms_HI->thisptr_offs; \
433 std::list<HookManagerInfo::Iface>::iterator ifaceiter = std::find(ms_HI->ifaces.begin(), \
434 ms_HI->ifaces.end(), origthis); \
435 SH_ASSERT(ifaceiter != ms_HI->ifaces.end()); \
436 HookManagerInfo::Iface &ci = *ifaceiter; \
437 /* 2) Declare some vars and set it up */ \
438 std::list<HookManagerInfo::Iface::Hook> &prelist = ci.hooks_pre; \
439 std::list<HookManagerInfo::Iface::Hook> &postlist = ci.hooks_post; \
440 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
441 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
442 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
443 status = MRES_IGNORED; \
444 SH_GLOB_SHPTR->SetIfacePtr(ci.ptr); \
445 SH_GLOB_SHPTR->SetOverrideRet(NULL);
447 #define SH_CALL_HOOKS_void(post, params) \
448 prev_res = MRES_IGNORED; \
449 for (std::list<HookManagerInfo::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
451 cur_res = MRES_IGNORED; \
452 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
453 prev_res = cur_res; \
454 if (cur_res > status) \
458 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, params) \
459 if (status != MRES_SUPERCEDE) \
460 reinterpret_cast<ifacetype*>(ci.callclass)->ifacefunc params;
462 #define SH_RETURN_void()
464 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, params) \
465 SH_SETUPCALLS_void() \
466 SH_CALL_HOOKS_void(pre, params) \
467 SH_CALL_ORIG_void(ifacetype, ifacefunc, params) \
468 SH_CALL_HOOKS_void(post, params) \
472 // Special vafmt handlers
473 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, params_orig, params_plug, rettype) \
474 SH_SETUPCALLS(rettype) \
475 SH_CALL_HOOKS(pre, params_plug) \
476 SH_CALL_ORIG(ifacetype, ifacefunc, params_orig) \
477 SH_CALL_HOOKS(post, params_plug) \
480 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, params_orig, params_plug) \
481 SH_SETUPCALLS_void() \
482 SH_CALL_HOOKS_void(pre, params_plug) \
483 SH_CALL_ORIG_void(ifacetype, ifacefunc, params_orig) \
484 SH_CALL_HOOKS_void(post, params_plug) \
487 //////////////////////////////////////////////////////////////////////////
491 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
492 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @)> \
493 (&ifacetype::ifacefunc))) \
494 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
495 virtual rettype Func(@param%% p%%|, @) attr \
496 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@p%%|, @), rettype); } \
497 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @)
499 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
500 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @)> \
501 (&ifacetype::ifacefunc))) \
502 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
503 virtual void Func(@param%% p%%|, @) attr \
504 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@p%%|, @)); } \
505 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @)
507 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
508 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
509 (&ifacetype::ifacefunc))) \
510 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
511 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) attr \
513 char buf[STRBUF_LEN]; \
516 vsnprintf(buf, sizeof(buf), fmt, ap); \
518 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
520 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...")
522 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
523 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
524 (&ifacetype::ifacefunc))) \
525 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
526 virtual void Func(@param%% p%%|, @@, @const char *, ...) attr \
528 char buf[STRBUF_LEN]; \
531 vsnprintf(buf, sizeof(buf), fmt, ap); \
533 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
535 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...")
540 #define SH_DECL_HOOK1(ifacetype, ifacefunc, attr, overload, rettype, param1) \
541 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload) \
542 typedef fastdelegate::FastDelegate1<param1, rettype> FD; \
543 virtual rettype Func(param1 p1) \
544 { SH_HANDLEFUNC(ifacetype, ifacefunc, (p1), rettype); } \
545 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype "|" #param1)
551 // The pope is dead. -> :(