1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
13 * @brief Contains the public SourceHook API
16 #ifndef __SOURCEHOOK_H__
17 #define __SOURCEHOOK_H__
19 // Interface revisions:
20 // 1 - Initial revision
21 // 2 - Changed to virtual functions for iterators and all queries
22 // 3 - Added "hook loop status variable"
25 #define SH_IFACE_VERSION 4
26 #define SH_IMPL_VERSION 3
28 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
29 // It's used in various macros
31 #define SH_GLOB_SHPTR g_SHPtr
34 // Used to identify the current plugin
35 #ifndef SH_GLOB_PLUGPTR
36 #define SH_GLOB_PLUGPTR g_PLID
43 # define SH_ASSERT(x, info) \
47 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
55 # define SH_ASSERT(x, info)
59 #define SH_SYS_WIN32 1
60 #define SH_SYS_LINUX 2
63 # define SH_SYS SH_SYS_WIN32
64 #elif defined __linux__
65 # define SH_SYS SH_SYS_LINUX
67 # error Unsupported system
72 #define SH_COMP_MSVC 2
75 # define SH_COMP SH_COMP_MSVC
76 #elif defined __GNUC__
77 # define SH_COMP SH_COMP_GCC
79 # error Unsupported compiler
82 #if SH_COMP==SH_COMP_MSVC
83 # define vsnprintf _vsnprintf
86 #define SH_PTRSIZE sizeof(void*)
88 #include "FastDelegate.h"
89 #include "sh_memfuncinfo.h"
93 // Flags returned by a plugin's api function.
94 // NOTE: order is crucial, as greater/less comparisons are made.
97 MRES_IGNORED=0, // plugin didn't take any action
98 MRES_HANDLED, // plugin did something, but real function should still be called
99 MRES_OVERRIDE, // call real function, but use my return value
100 MRES_SUPERCEDE // skip real function; use my return value
107 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
109 const int STRBUF_LEN=4096;
112 * @brief An empty class. No inheritance used. Used for original-function-call hacks
119 * @brief Implicit cast.
121 template <class In, class Out>
122 inline Out implicit_cast(In input)
128 * @brief A plugin typedef
130 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
131 * plugin has a unique identifier, everything is ok.
132 * It should also be possible to set it to 0.
138 ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
141 int beginningNull; //!< To distinguish from old protos (which never begin with 0)
142 int retTypeSize; //!< 0 if void
143 int numOfParams; //!< number of parameters
144 const int *params; //!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
148 * @brief Specifies the actions for hook managers
150 enum HookManagerAction
152 HA_GetInfo = 0, //!< Store info about the hook manager
153 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
154 HA_Unregister //!< Clear the saved pointer
157 struct IHookManagerInfo;
160 * @brief Pointer to hook manager interface function
162 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
164 * @param ha What the hook manager should do
165 * @param hi A pointer to IHookManagerInfo
167 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
172 virtual void DeleteThis() = 0; // Ugly, I know
173 virtual bool IsEqual(ISHDelegate *other) = 0;
176 template <class T> class CSHDelegate : public ISHDelegate
180 CSHDelegate(T deleg) : m_Deleg(deleg)
184 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
193 bool IsEqual(ISHDelegate *other)
195 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
208 virtual bool End() = 0;
209 virtual void Next() = 0;
210 virtual ISHDelegate *Handler() = 0;
211 virtual int ThisPtrOffs() = 0;
213 virtual IIter *GetIter() = 0;
214 virtual void ReleaseIter(IIter *pIter) = 0;
219 virtual void *GetPtr() = 0;
220 virtual IHookList *GetPreHooks() = 0;
221 virtual IHookList *GetPostHooks() = 0;
226 virtual void *GetVfnPtr() = 0;
227 virtual void *GetOrigEntry() = 0;
229 virtual IIface *FindIface(void *ptr) = 0;
232 struct IHookManagerInfo
234 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
236 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
237 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
243 IHookList::IIter *m_pIter;
245 AutoHookIter(IHookList *pList)
246 : m_pList(pList), m_pIter(pList->GetIter())
253 m_pList->ReleaseIter(m_pIter);
258 return m_pIter->End();
266 ISHDelegate *Handler()
268 return m_pIter->Handler();
273 return m_pIter->ThisPtrOffs();
282 template<class B> struct CallClass
284 virtual B *GetThisPtr() = 0;
285 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
288 typedef CallClass<void> GenericCallClass;
291 * @brief The main SourceHook interface
297 * @brief Return interface version
299 virtual int GetIfaceVersion() = 0;
302 * @brief Return implementation version
304 virtual int GetImplVersion() = 0;
309 * @return True if the function succeeded, false otherwise
311 * @param plug The unique identifier of the plugin that calls this function
312 * @param iface The interface pointer
313 * @param ifacesize The size of the class iface points to
314 * @param myHookMan A hook manager function that should be capable of handling the function
315 * @param handler A pointer to a FastDelegate containing the hook handler
316 * @param post Set to true if you want a post handler
318 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
319 ISHDelegate *handler, bool post) = 0;
322 * @brief Removes a hook.
324 * @return True if the function succeeded, false otherwise
326 * @param plug The unique identifier of the plugin that calls this function
327 * @param iface The interface pointer
328 * @param myHookMan A hook manager function that should be capable of handling the function
329 * @param handler A pointer to a FastDelegate containing the hook handler
330 * @param post Set to true if you want a post handler
332 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
333 ISHDelegate *handler, bool post) = 0;
336 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
338 * @param plug The unique identifier of the plugin in question
340 virtual bool IsPluginInUse(Plugin plug) = 0;
343 * @brief Return a pointer to a callclass. Generate a new one if required.
345 * @param iface The interface pointer
346 * @param size Size of the class instance
348 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
351 * @brief Release a callclass
353 * @param ptr Pointer to the callclass
355 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
357 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
358 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
359 //!< previously calledhandler
360 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
361 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
362 //!< If not in post function, undefined
363 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
364 //!< If none is specified, NULL
365 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
366 //////////////////////////////////////////////////////////////////////////
368 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
369 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
370 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
371 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
372 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
373 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
374 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
375 virtual void SetOverrideRetPtr(const void *ptr) = 0; //!< Sets the override result pointer
376 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
379 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
381 * @param plug The owner of the hook manager
382 * @param pubFunc The hook manager's info function
384 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
388 /************************************************************************/
389 /* High level interface */
390 /************************************************************************/
391 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
392 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
393 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
395 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
396 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
397 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
398 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
399 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
403 * @brief Get/generate callclass for an interface pointer
405 * @param ifaceptr The interface pointer
407 template<class ifacetype>
408 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
410 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
411 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
414 template<class ifacetype>
415 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
417 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
420 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
422 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
423 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
424 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
425 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
428 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
429 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
431 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
432 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
434 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
435 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
436 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
437 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
439 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
440 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
442 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
443 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
444 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
445 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
448 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
449 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
450 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
451 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
452 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
453 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
455 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
456 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
457 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
458 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
459 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
460 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
468 #if SH_COMP == SH_COMP_MSVC
469 # define SH_SETUP_MFP(mfp) \
470 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
471 #elif SH_COMP == SH_COMP_GCC
472 # define SH_SETUP_MFP(mfp) \
473 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
474 reinterpret_cast<void**>(&mfp)[1] = 0;
476 # error Not supported yet.
479 //////////////////////////////////////////////////////////////////////////
480 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
481 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
483 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
484 SH_FHCls(ifacetype,ifacefunc,overload)() \
486 GetFuncInfo(funcptr, ms_MFI); \
489 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
491 using namespace ::SourceHook; \
492 GetFuncInfo(funcptr, ms_MFI); \
493 /* Verify interface version */ \
494 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
497 if (action == HA_GetInfo) \
499 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
500 reinterpret_cast<const char*>(&ms_Proto)); \
503 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
504 param->SetHookfuncVfnptr( \
505 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
508 else if (action == HA_Register) \
513 else if (action == HA_Unregister) \
522 // It has to be possible to use the macros in namespaces
523 // -> So we need to access and extend the global SourceHook namespace
524 // We use a namespace alias for this
525 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
526 struct SH_FHCls(ifacetype,ifacefunc,overload) \
528 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
529 static ::SourceHook::MemFuncInfo ms_MFI; \
530 static ::SourceHook::IHookManagerInfo *ms_HI; \
531 static ::SourceHook::ProtoInfo ms_Proto; \
532 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
534 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
536 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
537 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
538 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
539 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
540 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
542 using namespace ::SourceHook; \
544 GetFuncInfo(funcptr, mfi); \
545 if (mfi.thisptroffs < 0) \
546 return false; /* No virtual inheritance supported */ \
548 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
549 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
550 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
552 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
553 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
555 using namespace ::SourceHook; \
557 GetFuncInfo(funcptr, mfi); \
558 if (mfi.thisptroffs < 0) \
559 return false; /* No virtual inheritance supported */ \
561 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
562 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
563 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
566 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
567 struct SH_MFHCls(hookname) \
569 static SH_MFHCls(hookname) ms_Inst; \
570 static ::SourceHook::MemFuncInfo ms_MFI; \
571 static ::SourceHook::IHookManagerInfo *ms_HI; \
572 static ::SourceHook::ProtoInfo ms_Proto; \
574 SH_MFHCls(hookname)() \
576 ms_MFI.isVirtual = true; \
577 ms_MFI.thisptroffs = pthisptroffs; \
578 ms_MFI.vtblindex = pvtblidx; \
579 ms_MFI.vtbloffs = pvtbloffs; \
581 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
583 using namespace ::SourceHook; \
584 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
585 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
586 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
587 /* Verify interface version */ \
588 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
591 if (action == HA_GetInfo) \
593 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
594 reinterpret_cast<const char*>(&ms_Proto)); \
597 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
598 param->SetHookfuncVfnptr( \
599 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
602 else if (action == HA_Register) \
607 else if (action == HA_Unregister) \
616 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
618 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
619 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
620 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
621 bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
622 SH_MFHCls(hookname)::FD handler) \
624 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
625 SH_MFHCls(hookname)::HookManPubFunc, \
626 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
628 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
629 SH_MFHCls(hookname)::FD handler) \
631 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
632 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
633 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
637 #define SH_SETUPCALLS(rettype, paramtypes, params) \
638 /* 1) Find the vfnptr */ \
639 using namespace ::SourceHook; \
640 void *ourvfnptr = reinterpret_cast<void*>( \
641 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
642 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
643 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
645 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
646 /* ... and the iface */ \
647 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
650 /* The iface info was not found. Redirect the call to the original function. */ \
651 rettype (EmptyClass::*mfp)paramtypes; \
653 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
655 /* 2) Declare some vars and set it up */ \
656 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
657 IHookList *prelist = ifinfo->GetPreHooks(); \
658 IHookList *postlist = ifinfo->GetPostHooks(); \
659 META_RES status = MRES_IGNORED; \
662 SH_GLOB_SHPTR->SetStatusPtr(&status); \
663 SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
664 SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
666 rettype override_ret; \
667 rettype plugin_ret; \
669 SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
670 SH_GLOB_SHPTR->SetOrigRetPtr(reinterpret_cast<void*>(&orig_ret)); \
671 SH_GLOB_SHPTR->SetOverrideRetPtr(NULL);
673 #define SH_CALL_HOOKS(post, params) \
674 if (SH_GLOB_SHPTR->ShouldContinue()) \
676 prev_res = MRES_IGNORED; \
677 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
679 cur_res = MRES_IGNORED; \
680 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
681 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
682 prev_res = cur_res; \
683 if (cur_res > status) \
685 if (cur_res >= MRES_OVERRIDE) \
687 override_ret = plugin_ret; \
688 SH_GLOB_SHPTR->SetOverrideRetPtr(&override_ret); \
690 if (!SH_GLOB_SHPTR->ShouldContinue()) \
698 #define SH_CALL_ORIG(rettype, paramtypes, params) \
699 if (status != MRES_SUPERCEDE) \
701 rettype (EmptyClass::*mfp)paramtypes; \
703 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
706 orig_ret = override_ret;
708 #define SH_RETURN() \
709 SH_GLOB_SHPTR->HookLoopEnd(); \
710 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
712 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
713 SH_SETUPCALLS(rettype, paramtypes, params) \
714 SH_CALL_HOOKS(pre, params) \
715 SH_CALL_ORIG(rettype, paramtypes, params) \
716 SH_CALL_HOOKS(post, params) \
719 //////////////////////////////////////////////////////////////////////////
720 #define SH_SETUPCALLS_void(paramtypes, params) \
721 /* 1) Find the vfnptr */ \
722 using namespace ::SourceHook; \
723 void *ourvfnptr = reinterpret_cast<void*>( \
724 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
725 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
726 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
728 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
729 /* ... and the iface */ \
730 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
733 /* The iface info was not found. Redirect the call to the original function. */ \
734 void (EmptyClass::*mfp)paramtypes; \
736 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
739 /* 2) Declare some vars and set it up */ \
740 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
741 IHookList *prelist = ifinfo->GetPreHooks(); \
742 IHookList *postlist = ifinfo->GetPostHooks(); \
743 META_RES status = MRES_IGNORED; \
746 SH_GLOB_SHPTR->SetStatusPtr(&status); \
747 SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
748 SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
750 SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
751 SH_GLOB_SHPTR->SetOverrideRetPtr(NULL); \
752 SH_GLOB_SHPTR->SetOrigRetPtr(NULL);
754 #define SH_CALL_HOOKS_void(post, params) \
755 if (SH_GLOB_SHPTR->ShouldContinue()) \
757 prev_res = MRES_IGNORED; \
758 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
760 cur_res = MRES_IGNORED; \
761 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
762 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
763 prev_res = cur_res; \
764 if (cur_res > status) \
766 if (!SH_GLOB_SHPTR->ShouldContinue()) \
774 #define SH_CALL_ORIG_void(paramtypes, params) \
775 if (status != MRES_SUPERCEDE) \
777 void (EmptyClass::*mfp)paramtypes; \
779 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
782 #define SH_RETURN_void() \
783 SH_GLOB_SHPTR->HookLoopEnd();
785 #define SH_HANDLEFUNC_void(paramtypes, params) \
786 SH_SETUPCALLS_void(paramtypes, params) \
787 SH_CALL_HOOKS_void(pre, params) \
788 SH_CALL_ORIG_void(paramtypes, params) \
789 SH_CALL_HOOKS_void(post, params) \
793 // Special vafmt handlers
794 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
795 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
796 SH_CALL_HOOKS(pre, params_plug) \
797 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
798 SH_CALL_HOOKS(post, params_plug) \
801 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
802 SH_SETUPCALLS_void(paramtypes, params_orig) \
803 SH_CALL_HOOKS_void(pre, params_plug) \
804 SH_CALL_ORIG_void(paramtypes, params_orig) \
805 SH_CALL_HOOKS_void(post, params_plug) \
808 //////////////////////////////////////////////////////////////////////////
811 // ********* Support for @$@ arguments *********
812 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
813 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
814 (&ifacetype::ifacefunc))) \
815 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
816 virtual rettype Func(@param%% p%%|, @) \
817 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
818 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
819 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
821 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
822 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
823 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
825 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
826 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
827 (&ifacetype::ifacefunc))) \
828 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
829 virtual void Func(@param%% p%%|, @) \
830 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
831 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
832 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
835 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
836 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
837 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
839 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
840 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
841 (&ifacetype::ifacefunc))) \
842 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
843 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
845 char buf[::SourceHook::STRBUF_LEN]; \
848 vsnprintf(buf, sizeof(buf), fmt, ap); \
850 SH_HANDLEFUNC_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
852 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
853 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
855 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
856 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
857 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
859 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
860 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
861 (&ifacetype::ifacefunc))) \
862 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
863 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
865 char buf[::SourceHook::STRBUF_LEN]; \
868 vsnprintf(buf, sizeof(buf), fmt, ap); \
870 SH_HANDLEFUNC_void_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
872 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
873 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
875 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
876 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
877 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
879 #define SH_DECL_MANUALHOOK@$@(hookname, vtblidx, vtbloffs, thisptroffs, rettype@, param%%@) \
880 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
881 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
882 virtual rettype Func(@param%% p%%|, @) \
883 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
884 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
886 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
887 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
888 @$@, __SourceHook_ParamSizesM_##hookname);
890 #define SH_DECL_MANUALHOOK@$@_void(hookname, vtblidx, vtbloffs, thisptroffs@, param%%@) \
891 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
892 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
893 virtual void Func(@param%% p%%|, @) \
894 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
895 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
897 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
898 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
899 @$@, __SourceHook_ParamSizesM_##hookname);
904 //////////////////////////////////////////////////////////////////////////
907 #if SH_COMP == SH_COMP_MSVC
909 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
911 using namespace ::SourceHook; \
913 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
914 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
916 return (m_CC->GetThisPtr()->*m_MFP)call; \
918 /* It's hooked. Call the original function. */ \
921 RetType(EmptyClass::*mfpnew)prms; \
926 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
927 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
930 #elif SH_COMP == SH_COMP_GCC
932 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
934 using namespace ::SourceHook; \
936 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
937 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
939 return (m_CC->GetThisPtr()->*m_MFP)call; \
941 /* It's hooked. Call the original function. */ \
944 RetType(EmptyClass::*mfpnew)prms; \
951 u.s.addr = origfunc; \
952 u.s.adjustor = mfi.thisptroffs; \
954 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
961 template<class CCType, class RetType, class MFPType> class ExecutableClass
966 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
971 // Support for @$@ arguments
972 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
973 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
979 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
980 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
981 // The only purpose of the mfp2 parameter is to extract the return type
984 // Support for @$@ arguments
985 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
986 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
987 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
989 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
992 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
993 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
994 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
996 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1001 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1002 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1005 // Support for @$@ arguments
1006 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1007 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1008 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
1010 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1013 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1014 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1015 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
1017 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1024 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
1026 #undef SH_MAKE_EXECUTABLECLASS_OB
1029 // The pope is dead. -> :(