1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 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"
24 // 5 - New "V2" interface
25 #define SH_IFACE_VERSION 5
29 // 4 - addition of hook ids and vp hooks (with them, AddHookNew and RemoveHookNew)
30 // This is not a SH_IFACE_VERSION change so that old plugins continue working!
31 // 5 - implementation of the new "V2" interface
32 #define SH_IMPL_VERSION 5
36 #define SH_HOOKMAN_VERSION 1
38 // Hookmanautogen versions
40 #define SH_HOOKMANAUTOGEN_IFACE_VERSION 1
41 #define SH_HOOKMANAUTOGEN_IMPL_VERSION 1
43 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
44 // It's used in various macros
46 #define SH_GLOB_SHPTR g_SHPtr
49 // Used to identify the current plugin
50 #ifndef SH_GLOB_PLUGPTR
51 #define SH_GLOB_PLUGPTR g_PLID
58 # define SH_ASSERT(x, info) \
62 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
70 # define SH_ASSERT(x, info)
74 #define SH_SYS_WIN32 1
75 #define SH_SYS_LINUX 2
78 # define SH_SYS SH_SYS_WIN32
79 #elif defined __linux__
80 # define SH_SYS SH_SYS_LINUX
82 # error Unsupported system
87 #define SH_COMP_MSVC 2
90 # define SH_COMP SH_COMP_MSVC
91 #elif defined __GNUC__
92 # define SH_COMP SH_COMP_GCC
94 # error Unsupported compiler
97 #if SH_COMP==SH_COMP_MSVC
98 # define vsnprintf _vsnprintf
101 #if SH_SYS != SH_SYS_WIN32
105 #define SH_PTRSIZE sizeof(void*)
107 #include "sh_memfuncinfo.h"
108 #include "FastDelegate.h"
112 // Flags returned by a plugin's api function.
113 // NOTE: order is crucial, as greater/less comparisons are made.
116 MRES_IGNORED=0, // plugin didn't take any action
117 MRES_HANDLED, // plugin did something, but real function should still be called
118 MRES_OVERRIDE, // call real function, but use my return value
119 MRES_SUPERCEDE // skip real function; use my return value
126 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
128 const int STRBUF_LEN=4096;
131 * @brief An empty class. No inheritance used. Used for original-function-call hacks
138 * @brief Implicit cast.
140 template <class In, class Out>
141 inline Out implicit_cast(In input)
147 * @brief A plugin typedef
149 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
150 * plugin has a unique identifier, everything is ok.
151 * It should also be possible to set it to 0.
156 template <class T> class CallClass : public T
159 typedef void ManualCallClass;
162 * @brief Describes the way a parameter/retval is passed.
168 PassType_Unknown=0, /**< Unknown -- no extra info available */
169 PassType_Basic, /**< Plain old register data (pointers, integers) */
170 PassType_Float, /**< Floating point data */
171 PassType_Object, /**< Object or structure */
176 PassFlag_ByVal = (1<<0), /**< Passing by value */
177 PassFlag_ByRef = (1<<1), /**< Passing by reference */
178 PassFlag_ODtor = (1<<2), /**< Object has a destructor */
179 PassFlag_OCtor = (1<<3), /**< Object has a normal non-trivial constructor */
180 PassFlag_AssignOp = (1<<4), /**< Object has a non-trivial assignment operator */
181 PassFlag_CCtor = (1<<5), /**< Object has a copy constructor (which takes const Object& as only parameter) */
183 // The following two flags are only relevant for byval return types.
184 // SH tries to auto-detect these
185 // If you want to override SH's auto-detection, pass them in yourself
186 PassFlag_RetMem = (1<<6), /**< Object is returned in memory (through hidden first param */
187 PassFlag_RetReg = (1<<7) /**< Object is returned in EAX(:EDX) */
190 size_t size; //!< Size of the data being passed
193 // (might be used in future versions for automatic hookfunc generation)
194 int type; //!< PassType value
195 unsigned int flags; //!< Pass/return flags
202 void *pAssignOperator;
210 CallConv_Unknown, /**< Unknown -- no extra info available (0)*/
211 CallConv_ThisCall, /**< This call (object pointer required) (1)*/
212 CallConv_Cdecl, /**< C call (2)*/
213 CallConv_StdCall, /**< Windows "stdcall" (3)*/
215 CallConv_HasVarArgs = (1<<16), /**< Has variable arguments */
216 CallConv_HasVafmt = CallConv_HasVarArgs | (1<<17) /**< last params: const char*, ... */
219 int numOfParams; //!< number of parameters
220 PassInfo retPassInfo; //!< PassInfo for the return value. size=0 -> no retval
221 const PassInfo *paramsPassInfo; //!< PassInfos for the parameters
223 // paramsPassInfo[0] is basically a dummy parameter.
224 // However, paramsPassInfo[0].size stores the version of the ProtoInfo structure.
230 PassInfo::V2Info retPassInfo2;
231 const PassInfo::V2Info *paramsPassInfo2;
234 struct IHookManagerInfo;
237 * @brief Pointer to hook manager interface function
239 * The hook manager should store hi for later use if store==true. It should then call hi->SetInfo(...) if hi
240 * is non-null. The hook manager can return 0 for success or a non-zero value if it doesn't want to be used.
242 * @param hi A pointer to IHookManagerInfo
244 typedef int (*HookManagerPubFunc)(bool store, IHookManagerInfo *hi);
249 virtual bool IsEqual(ISHDelegate *pOtherDeleg) = 0; // pOtherDeleg is from the same plugin and hookman
250 virtual void DeleteThis() = 0;
253 struct IHookManagerInfo
255 virtual void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
256 ProtoInfo *proto, void *hookfunc_vfnptr) = 0;
259 // I'm adding support for functions which return references.
262 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
263 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
264 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
265 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
266 // special cases were needed for getoverrideret / getorigret; these are implemented through the
267 // SourceHook::MacroRefHelpers structs.
268 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
269 template <class T> struct ReferenceCarrier
274 template <class T> struct ReferenceCarrier<T&>
280 type() : m_StoredRef(NULL)
283 type(T& ref) : m_StoredRef(&ref)
287 T& operator= (T& ref)
302 virtual ISHDelegate *GetNext() = 0;
303 virtual void *GetOverrideRetPtr() = 0;
304 virtual const void *GetOrigRetPtr() = 0;
305 virtual bool ShouldCallOrig() = 0;
309 * @brief The main SourceHook interface
315 * @brief Return interface version
317 virtual int GetIfaceVersion() = 0;
320 * @brief Return implementation version
322 virtual int GetImplVersion() = 0;
325 * @brief Modes for the new AddHook
335 * @brief Add a (VP) hook.
337 * @return non-zero hook id on success, 0 otherwise
339 * @param plug The unique identifier of the plugin that calls this function
340 * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
341 * @param iface The interface pointer
342 * @param ifacesize The size of the class iface points to
343 * @param myHookMan A hook manager function that should be capable of handling the function
344 * @param handler A pointer to the hook handler something
345 * @param post Set to true if you want a post handler
347 virtual int AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
348 ISHDelegate *handler, bool post) = 0;
350 // Source backwarts compat (only for normal hooks)
351 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
352 ISHDelegate *handler, bool post) = 0;
355 * @brief Remove a hook by ID.
357 * @return true on success, false otherwise
359 * @param hookid The hook id (returned by AddHook)
361 virtual bool RemoveHookByID(int hookid) = 0;
364 * @brief Pause a hook by ID.
366 * @return true on success, false otherwise
368 * @param hookid The hook id (returned by AddHook)
370 virtual bool PauseHookByID(int hookid) = 0;
373 * @brief Unpause a hook by ID.
375 * @return true on success, false otherwise
377 * @param hookid The hook id (returned by AddHook)
379 virtual bool UnpauseHookByID(int hookid) = 0;
382 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
384 * @param plug The unique identifier of the plugin in question
387 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
388 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
389 //!< previously calledhandler
390 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
391 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
392 //!< If not in post function, undefined
393 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
394 //!< If none is specified, NULL
395 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
397 virtual void *GetOverrideRetPtr() = 0; //!< Used for setting the override return value
400 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
402 * @param plug The owner of the hook manager
403 * @param pubFunc The hook manager's info function
405 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
410 * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
412 * @param plug The unique identifier of the plugin that calls this function
413 * @param vfnptr The virtual function pointer of the function in question
415 virtual void SetIgnoreHooks(void *vfnptr) = 0;
418 * @brief Reverses SetIgnoreHooks' effect
420 * @param plug The unique identifier of the plugin that calls this function
421 * @param vfnptr The virtual function pointer of the function in question
423 virtual void ResetIgnoreHooks(void *vfnptr) = 0;
426 * @brief Finds the original entry of a virtual function pointer
428 * @param vfnptr The virtual function pointer
429 * @return The original entry if the virtual function pointer has been patched; NULL otherwise.
431 virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
433 //////////////////////////////////////////////////////////////////////////
437 // Returns the current iface ptr
438 virtual void DoRecall() = 0; //!< Initiates a recall sequence
444 Users want the ability to change parameters of the called function
445 from inside their handler.
448 2) Recall the function
449 3) => SH's hook func gets called:
450 4) The first iterator points at the first hook the last hookfunc didn't execute yet
451 5) does all iteration and returns normally
452 6) The user's handler returns immediately
453 7) The hook func returns immediately as well
455 Also note that the recalled hookfunc starts with the status the recalling hookfunc
456 ended with. The last handler (doing the recall) is also able to specify its own
461 * @brief Set up the hook loop.
463 * @param statusPtr pointer to status variable
464 * @param prevResPtr pointer to previous result variable
465 * @param curResPtr pointer to current result variable
466 * @param ifacePtrPtr pointer to interface this pointer variable
467 * @param origRetPr pointer to original return value variable. NULL for void funcs
468 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
470 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
471 * when the hook func is being called as part of a recall
473 virtual IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry,
474 META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
475 const void *origRetPtr, void *overrideRetPtr) = 0;
477 virtual void EndContext(IHookContext *pCtx) = 0;
481 class IHookManagerAutoGen
484 virtual int GetIfaceVersion() = 0;
485 virtual int GetImplVersion() = 0;
487 virtual HookManagerPubFunc MakeHookMan(const ProtoInfo *proto, int vtbl_offs, int vtbl_idx) = 0;
488 virtual void ReleaseHookMan(HookManagerPubFunc pubFunc) = 0;
491 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
492 // These have to be able to return references. If T is a reference, the pointers returned
493 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
494 template <class T> struct MacroRefHelpers
496 inline static const T* GetOrigRet(ISourceHook *shptr)
498 return reinterpret_cast<const T*>(shptr->GetOrigRet());
500 inline static const T* GetOverrideRet(ISourceHook *shptr)
502 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
506 template <class T> struct MacroRefHelpers<T&>
508 inline static T* GetOrigRet(ISourceHook *shptr)
510 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
513 inline static T* GetOverrideRet(ISourceHook *shptr)
515 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
520 template <class X, class MFP>
521 void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
523 SourceHook::MemFuncInfo info = {true, -1, 0, 0};
524 SourceHook::GetFuncInfo(pInstance, mfp, info);
526 void *vfnptr = reinterpret_cast<void*>(
527 *reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
529 void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
531 return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
534 template <class T> struct GetPassInfo
536 static const int type = 0;
537 static const unsigned int flags = PassInfo::PassFlag_ByVal;
540 template <class T> struct GetPassInfo<T&>
542 static const int type = 0;
543 static const unsigned int flags = PassInfo::PassFlag_ByRef;
547 /************************************************************************/
548 /* High level interface */
549 /************************************************************************/
550 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
551 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
552 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
553 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
554 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
556 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
557 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
558 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
562 SourceHook::CallClass<T> *SH_GET_CALLCLASS(T *p)
564 return static_cast< SourceHook::CallClass<T> *>(p);
566 #define SH_RELEASE_CALLCLASS(p)
568 #define SH_GET_MCALLCLASS(p, size) reinterpret_cast<void*>(p)
570 // only call these from the hook handlers directly!
572 // If a hook on a function which returns a reference does not want to specify a return value,
573 // it can use this macro.
574 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
575 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
577 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
578 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
580 // SourceHook::SetOverrideRet is defined later.
581 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
583 SET_META_RESULT(result); \
584 SH_GLOB_SHPTR->DoRecall(); \
585 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
586 RETURN_META(MRES_SUPERCEDE); \
589 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
591 SET_META_RESULT(result); \
592 SH_GLOB_SHPTR->DoRecall(); \
593 if ((result) >= MRES_OVERRIDE) \
595 /* meh, set the override result here because we don't get a chance to return */ \
596 /* before continuing the hook loop through the recall */ \
597 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
599 RETURN_META_VALUE(MRES_SUPERCEDE, \
600 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
603 // :TODO: thisptroffs in MNEWPARAMS ??
605 #if SH_COMP == SH_COMP_MSVC
607 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
610 SH_MFHCls(hookname)::ECMFP mfp; \
613 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
614 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
615 SH_MFHCls(hookname)::ms_MFI.vtblindex];
617 #elif SH_COMP == SH_COMP_GCC
619 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
622 SH_MFHCls(hookname)::ECMFP mfp; \
629 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
630 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
631 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
636 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
638 SET_META_RESULT(result); \
639 SH_GLOB_SHPTR->DoRecall(); \
640 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
641 (thisptr->*(u.mfp)) newparams; \
642 RETURN_META(MRES_SUPERCEDE); \
645 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
647 SET_META_RESULT(result); \
648 SH_GLOB_SHPTR->DoRecall(); \
649 if ((result) >= MRES_OVERRIDE) \
651 /* see RETURN_META_VALUE_NEWPARAMS */ \
652 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
654 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
655 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
659 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
661 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
662 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
663 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
664 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
667 #define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
669 // New ADD / REMOVE macros.
670 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
671 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
673 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
674 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
675 SourceHook::ISourceHook::Hook_Normal, post, handler)
677 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
678 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
681 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
682 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), \
683 SourceHook::ISourceHook::Hook_Normal, post, handler)
685 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
686 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
688 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
689 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
690 SourceHook::ISourceHook::Hook_VP, post, handler)
692 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
693 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
694 SourceHook::ISourceHook::Hook_DVP, post, handler)
696 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
697 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_VP, post, handler)
699 #define SH_ADD_MANUALDVPHOOK(hookname, ifaceptr, handler, post) \
700 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_DVP, post, handler)
702 #define SH_REMOVE_HOOK_ID(hookid) \
703 (SH_GLOB_SHPTR->RemoveHookByID(hookid))
706 // !! These are now deprecated. Instead, use one of these:
707 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
708 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
710 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
711 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
712 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
713 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
715 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
716 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
718 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
719 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
720 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
721 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
723 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
724 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
726 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
727 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
728 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
729 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
731 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
732 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
734 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
735 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
736 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
737 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
744 #if SH_COMP == SH_COMP_MSVC
745 # define SH_SETUP_MFP(mfp) \
746 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
747 #elif SH_COMP == SH_COMP_GCC
748 # define SH_SETUP_MFP(mfp) \
749 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
750 reinterpret_cast<void**>(&mfp)[1] = 0;
752 # error Not supported yet.
755 //////////////////////////////////////////////////////////////////////////
756 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
757 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
759 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
760 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
762 using namespace ::SourceHook; \
763 GetFuncInfo(funcptr, ms_MFI); \
764 /* Verify interface version */ \
765 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
767 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
773 MemFuncInfo mfi = {true, -1, 0, 0}; \
774 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
775 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
776 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
781 // It has to be possible to use the macros in namespaces
782 // -> So we need to access and extend the global SourceHook namespace
783 // We use a namespace alias for this
784 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
785 struct SH_FHCls(ifacetype,ifacefunc,overload) \
787 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
788 static ::SourceHook::MemFuncInfo ms_MFI; \
789 static ::SourceHook::IHookManagerInfo *ms_HI; \
790 static ::SourceHook::ProtoInfo ms_Proto; \
791 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
793 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
795 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
796 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
797 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
798 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
799 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
801 using namespace ::SourceHook; \
802 MemFuncInfo mfi = {true, -1, 0, 0}; \
803 GetFuncInfo(funcptr, mfi); \
804 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
805 return false; /* No non-virtual functions / virtual inheritance supported */ \
807 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
808 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
809 new SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl(handler), post); \
811 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
812 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
814 using namespace ::SourceHook; \
815 MemFuncInfo mfi = {true, -1, 0, 0}; \
816 GetFuncInfo(funcptr, mfi); \
817 SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl tmp(handler); \
818 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
819 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
822 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
823 struct SH_MFHCls(hookname) \
825 static SH_MFHCls(hookname) ms_Inst; \
826 static ::SourceHook::MemFuncInfo ms_MFI; \
827 static ::SourceHook::IHookManagerInfo *ms_HI; \
828 static ::SourceHook::ProtoInfo ms_Proto; \
830 SH_MFHCls(hookname)() \
832 ms_MFI.isVirtual = true; \
833 ms_MFI.thisptroffs = pthisptroffs; \
834 ms_MFI.vtblindex = pvtblidx; \
835 ms_MFI.vtbloffs = pvtbloffs; \
837 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
839 using namespace ::SourceHook; \
840 /* Verify interface version */ \
841 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
843 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
849 MemFuncInfo mfi = {true, -1, 0, 0}; \
850 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
851 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
852 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
857 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
859 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
860 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
861 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
862 int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
863 SH_MFHCls(hookname)::FD handler) \
865 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
866 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
867 new SH_MFHCls(hookname)::CMyDelegateImpl(handler), post); \
869 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
870 SH_MFHCls(hookname)::FD handler) \
872 SH_MFHCls(hookname)::CMyDelegateImpl tmp(handler); \
873 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
874 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
877 #define SH_SETUPCALLS(rettype, paramtypes, params) \
879 using namespace ::SourceHook; \
880 void *ourvfnptr = reinterpret_cast<void*>( \
881 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
882 void *vfnptr_origentry; \
884 META_RES status = MRES_IGNORED; \
888 typedef ReferenceCarrier< rettype >::type my_rettype; \
889 my_rettype orig_ret; \
890 my_rettype override_ret; \
891 my_rettype plugin_ret; \
893 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
894 &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
896 #define SH_CALL_HOOKS(post, params) \
897 prev_res = MRES_IGNORED; \
898 while ( (iter = static_cast<IMyDelegate*>(pContext->GetNext())) ) \
900 cur_res = MRES_IGNORED; \
901 plugin_ret = iter->Call params; \
902 prev_res = cur_res; \
903 if (cur_res > status) \
905 if (cur_res >= MRES_OVERRIDE) \
906 *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret; \
909 #define SH_CALL_ORIG(rettype, paramtypes, params) \
910 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
912 rettype (EmptyClass::*mfp)paramtypes; \
914 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
917 orig_ret = override_ret; /* :TODO: ??? : use pContext->GetOverrideRetPtr() or not? */
919 #define SH_RETURN() \
920 const my_rettype *retptr = reinterpret_cast<const my_rettype*>( \
921 (status >= MRES_OVERRIDE) ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()); \
922 SH_GLOB_SHPTR->EndContext(pContext); \
925 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
926 SH_SETUPCALLS(rettype, paramtypes, params) \
927 SH_CALL_HOOKS(pre, params) \
928 SH_CALL_ORIG(rettype, paramtypes, params) \
929 SH_CALL_HOOKS(post, params) \
932 //////////////////////////////////////////////////////////////////////////
933 #define SH_SETUPCALLS_void(paramtypes, params) \
935 using namespace ::SourceHook; \
936 void *ourvfnptr = reinterpret_cast<void*>( \
937 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
938 void *vfnptr_origentry; \
940 META_RES status = MRES_IGNORED; \
945 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
946 &vfnptr_origentry, &status, &prev_res, &cur_res, NULL, NULL);
948 #define SH_CALL_HOOKS_void(post, params) \
949 prev_res = MRES_IGNORED; \
950 while ( (iter = static_cast<IMyDelegate*>(pContext->GetNext())) ) \
952 cur_res = MRES_IGNORED; \
954 prev_res = cur_res; \
955 if (cur_res > status) \
959 #define SH_CALL_ORIG_void(paramtypes, params) \
960 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
962 void (EmptyClass::*mfp)paramtypes; \
964 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
967 #define SH_RETURN_void() \
968 SH_GLOB_SHPTR->EndContext(pContext);
970 #define SH_HANDLEFUNC_void(paramtypes, params) \
971 SH_SETUPCALLS_void(paramtypes, params) \
972 SH_CALL_HOOKS_void(pre, params) \
973 SH_CALL_ORIG_void(paramtypes, params) \
974 SH_CALL_HOOKS_void(post, params) \
978 // Special vafmt handlers
980 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
981 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
982 SH_CALL_HOOKS(pre, params_plug) \
983 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
984 SH_CALL_HOOKS(post, params_plug) \
987 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
988 SH_SETUPCALLS_void(paramtypes, params_orig) \
989 SH_CALL_HOOKS_void(pre, params_plug) \
990 SH_CALL_ORIG_void(paramtypes, params_orig) \
991 SH_CALL_HOOKS_void(post, params_plug) \
994 //////////////////////////////////////////////////////////////////////////
996 #define MAKE_DELEG(ret_type, params_decl, params_pass) \
997 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual ret_type Call params_decl = 0; }; \
998 struct CMyDelegateImpl : IMyDelegate \
1001 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
1002 ret_type Call params_decl { return m_Deleg params_pass; } \
1003 void DeleteThis() { delete this; } \
1004 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
1007 #define MAKE_DELEG_void(params_decl, params_pass) \
1008 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual void Call params_decl = 0; }; \
1009 struct CMyDelegateImpl : IMyDelegate \
1012 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
1013 void Call params_decl { m_Deleg params_pass; } \
1014 void DeleteThis() { delete this; } \
1015 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
1018 // GetPassInfo -> easier access. __SH_GPI generates a SourceHook::PassInfo instance.
1019 #define __SH_GPI(tt) { sizeof(tt), ::SourceHook::GetPassInfo< tt >::type, ::SourceHook::GetPassInfo< tt >::flags }
1020 // Extra pass info V2
1021 #define __SH_EPI { 0, 0, 0, 0 }
1024 // ********* Support for $1 arguments *********
1025 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1026 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1027 (&ifacetype::ifacefunc))) \
1028 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1029 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1030 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1031 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1032 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1033 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1035 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1036 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1037 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, __SH_GPI(rettype), \
1038 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1040 #define SH_DECL_EXTERN$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1041 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
1042 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> handler); \
1043 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
1044 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> handler);
1046 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1047 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1048 (&ifacetype::ifacefunc))) \
1049 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1050 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1051 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1052 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1053 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1054 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1056 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:,__SH_GPI(param$2)@] }; \
1057 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1058 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
1059 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1061 #define SH_DECL_EXTERN$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1062 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
1063 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler); \
1064 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
1065 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler);
1067 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1068 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1069 (&ifacetype::ifacefunc))) \
1070 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1071 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1072 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@] @[$1!=0:, @]const char *fmt, ...) \
1074 char buf[::SourceHook::STRBUF_LEN]; \
1076 va_start(ap, fmt); \
1077 vsnprintf(buf, sizeof(buf), fmt, ap); \
1079 SH_HANDLEFUNC_vafmt((@[$2,1,$1|, :param$2@]@[$1!=0:, @]...), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]"%s", buf), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]buf), rettype); \
1081 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1082 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1084 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1085 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1086 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, __SH_GPI(rettype), \
1087 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1089 #define SH_DECL_EXTERN$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1090 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
1091 fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> handler); \
1092 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
1093 fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> handler);
1095 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1096 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1097 (&ifacetype::ifacefunc))) \
1098 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1099 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1100 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1102 char buf[::SourceHook::STRBUF_LEN]; \
1104 va_start(ap, fmt); \
1105 vsnprintf(buf, sizeof(buf), fmt, ap); \
1107 SH_HANDLEFUNC_void_vafmt((@[$2,1,$1|, :param$2@]@[$1!=0:, @]...), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]"%s", buf), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]buf)); \
1109 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1110 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1112 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1113 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1114 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
1115 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1117 #define SH_DECL_EXTERN$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1118 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
1119 fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> handler); \
1120 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
1121 fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> handler);
1123 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1124 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1125 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1126 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1127 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1128 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1129 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1130 typedef rettype RetType; \
1131 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1133 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1134 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1135 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, __SH_GPI(rettype), \
1136 __SourceHook_ParamInfosM_##hookname, 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname };
1138 #define SH_DECL_MANUALEXTERN$1(hookname, rettype@[$2,1,$1:, param$2@]) \
1139 int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
1140 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> handler); \
1141 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
1142 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> handler);
1144 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1145 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1146 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1147 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1148 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1149 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1150 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1151 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1153 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1154 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1155 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
1156 __SourceHook_ParamInfosM_##hookname , 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname };
1158 #define SH_DECL_MANUALEXTERN$1_void(hookname@[$2,1,$1:, param$2@]) \
1159 int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
1160 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler); \
1161 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
1162 fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler);
1166 //////////////////////////////////////////////////////////////////////////
1169 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1171 using namespace ::SourceHook; \
1173 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1174 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1175 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1179 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1181 using namespace ::SourceHook; \
1183 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1184 (m_ThisPtr->*m_MFP)call; \
1185 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1188 namespace SourceHook
1191 // Support for $1 arguments
1192 template<class ObjType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1199 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1200 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1202 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1203 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1206 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1207 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1211 template<class ObjType, class MFPType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFPType, void@[$2,1,$1:, Param$2@]>
1218 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1219 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1221 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1222 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1225 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1226 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1233 #define SH__CALL_GET_VFNPTR_NORMAL \
1234 using namespace ::SourceHook; \
1235 MemFuncInfo mfi = {true, -1, 0, 0}; \
1236 GetFuncInfo(ptr, mfp, mfi); \
1237 void *vfnptr = reinterpret_cast<void*>( \
1238 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1240 #define SH__CALL_GET_VFNPTR_MANUAL \
1241 using namespace ::SourceHook; \
1242 void *vfnptr = reinterpret_cast<void*>( \
1243 *reinterpret_cast<void***>( (reinterpret_cast<char*>(ptr) + thisptroffs + vtbloffs) ) + vtblidx); \
1245 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1247 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1248 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1249 // The only purpose of the mfp2 parameter is to extract the return type
1252 // Support for $1 arguments
1253 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1254 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1255 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr)
1257 SH__CALL_GET_VFNPTR_NORMAL
1258 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1261 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1262 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1263 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr)
1265 SH__CALL_GET_VFNPTR_NORMAL
1266 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1269 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1270 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1271 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr)
1273 SH__CALL_GET_VFNPTR_MANUAL
1274 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1275 reinterpret_cast<SourceHook::EmptyClass*>(ptr), mfp, vfnptr, shptr);
1279 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1280 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1283 // Support for $1 arguments
1284 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1285 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1286 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr)
1288 SH__CALL_GET_VFNPTR_NORMAL
1289 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1292 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1293 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1294 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr)
1296 SH__CALL_GET_VFNPTR_NORMAL
1297 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1304 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR)
1305 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR)
1306 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1307 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1309 #undef SH_MAKE_EXECUTABLECLASS_OB
1310 #undef SH_MAKE_EXECUTABLECLASS_OB_void
1312 //////////////////////////////////////////////////////////////////////////
1313 // SetOverrideRet and RecallGetIface for recalls
1314 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1315 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1316 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1318 namespace SourceHook
1320 template <class RetType>
1321 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1323 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1326 // SetOverrideResult used to be implemented like this:
1327 // SetOverrideResult(shptr, memfuncptr, return);
1328 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1329 // (it thought it was ambigous - the ref and non-ref type)
1330 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1331 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1332 // This also allows us to create a special version for references which respects ReferenceCarrier.
1334 template <class T> struct OverrideFunctor
1336 void operator()(ISourceHook *shptr, T res)
1338 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1341 template <class T> struct OverrideFunctor<T&>
1343 void operator()(ISourceHook *shptr, T &res)
1345 // overrideretptr points to ReferenceCarrier<T&>
1346 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1350 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1351 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1353 return OverrideFunctor<RetType>();
1356 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1357 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1359 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1365 // The pope is dead. -> :(