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 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
39 // It's used in various macros
41 #define SH_GLOB_SHPTR g_SHPtr
44 // Used to identify the current plugin
45 #ifndef SH_GLOB_PLUGPTR
46 #define SH_GLOB_PLUGPTR g_PLID
53 # define SH_ASSERT(x, info) \
57 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
65 # define SH_ASSERT(x, info)
69 #define SH_SYS_WIN32 1
70 #define SH_SYS_LINUX 2
73 # define SH_SYS SH_SYS_WIN32
74 #elif defined __linux__
75 # define SH_SYS SH_SYS_LINUX
77 # error Unsupported system
82 #define SH_COMP_MSVC 2
85 # define SH_COMP SH_COMP_MSVC
86 #elif defined __GNUC__
87 # define SH_COMP SH_COMP_GCC
89 # error Unsupported compiler
92 #if SH_COMP==SH_COMP_MSVC
93 # define vsnprintf _vsnprintf
96 #if SH_SYS != SH_SYS_WIN32
100 #define SH_PTRSIZE sizeof(void*)
102 #include "sh_memfuncinfo.h"
103 #include "FastDelegate.h"
107 // Flags returned by a plugin's api function.
108 // NOTE: order is crucial, as greater/less comparisons are made.
111 MRES_IGNORED=0, // plugin didn't take any action
112 MRES_HANDLED, // plugin did something, but real function should still be called
113 MRES_OVERRIDE, // call real function, but use my return value
114 MRES_SUPERCEDE // skip real function; use my return value
121 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
123 const int STRBUF_LEN=4096;
126 * @brief An empty class. No inheritance used. Used for original-function-call hacks
133 * @brief Implicit cast.
135 template <class In, class Out>
136 inline Out implicit_cast(In input)
142 * @brief A plugin typedef
144 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
145 * plugin has a unique identifier, everything is ok.
146 * It should also be possible to set it to 0.
151 template <class T> class CallClass : public T
154 typedef void ManualCallClass;
157 * @brief Describes the way a parameter/retval is passed.
163 PassType_Unknown=0, /**< Unknown -- no extra info available */
164 PassType_Basic, /**< Plain old register data (pointers, integers) */
165 PassType_Float, /**< Floating point data */
166 PassType_Object, /**< Object or structure */
171 PassFlag_ByVal = (1<<0), /**< Passing by value */
172 PassFlag_ByRef = (1<<1), /**< Passing by reference */
173 PassFlag_ODtor = (1<<2), /**< Object has a destructor */
174 PassFlag_OCtor = (1<<3), /**< Object has a constructor */
175 PassFlag_AssignOp = (1<<4) /**< Object has an assignment operator */
178 size_t size; //!< Size of the data being passed
181 // (might be used in future versions for automatic hookfunc generation)
182 int type; //!< PassType value
183 unsigned int flags; //!< Pass/return flags
190 CallConv_Unknown, /**< Unknown -- no extra info available */
191 CallConv_ThisCall, /**< This call (object pointer required) */
192 CallConv_Cdecl, /**< Standard C call */
195 int numOfParams; //!< number of parameters
196 PassInfo retPassInfo; //!< PassInfo for the return value. size=0 -> no retval
197 const PassInfo *paramsPassInfo; //!< PassInfos for the parameters
203 struct IHookManagerInfo;
206 * @brief Pointer to hook manager interface function
208 * The hook manager should store hi for later use if store==true. It should then call hi->SetInfo(...) if hi
209 * is non-null. The hook manager can return 0 for success or a non-zero value if it doesn't want to be used.
211 * @param hi A pointer to IHookManagerInfo
213 typedef int (*HookManagerPubFunc)(bool store, IHookManagerInfo *hi);
218 virtual bool IsEqual(ISHDelegate *pOtherDeleg) = 0; // pOtherDeleg is from the same plugin and hookman
219 virtual void DeleteThis() = 0;
222 struct IHookManagerInfo
224 virtual void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
225 ProtoInfo *proto, void *hookfunc_vfnptr) = 0;
228 // I'm adding support for functions which return references.
231 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
232 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
233 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
234 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
235 // special cases were needed for getoverrideret / getorigret; these are implemented through the
236 // SourceHook::MacroRefHelpers structs.
237 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
238 template <class T> struct ReferenceCarrier
243 template <class T> struct ReferenceCarrier<T&>
249 type() : m_StoredRef(NULL)
252 type(T& ref) : m_StoredRef(&ref)
256 T& operator= (T& ref)
271 virtual ISHDelegate *GetNext() = 0;
272 virtual void *GetOverrideRetPtr() = 0;
273 virtual const void *GetOrigRetPtr() = 0;
274 virtual bool ShouldCallOrig() = 0;
278 * @brief The main SourceHook interface
284 * @brief Return interface version
286 virtual int GetIfaceVersion() = 0;
289 * @brief Return implementation version
291 virtual int GetImplVersion() = 0;
294 * @brief Modes for the new AddHook
304 * @brief Add a (VP) hook.
306 * @return non-zero hook id on success, 0 otherwise
308 * @param plug The unique identifier of the plugin that calls this function
309 * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
310 * @param iface The interface pointer
311 * @param ifacesize The size of the class iface points to
312 * @param myHookMan A hook manager function that should be capable of handling the function
313 * @param handler A pointer to the hook handler something
314 * @param post Set to true if you want a post handler
316 virtual int AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
317 ISHDelegate *handler, bool post) = 0;
319 // Source backwarts compat (only for normal hooks)
320 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
321 ISHDelegate *handler, bool post) = 0;
324 * @brief Remove a hook by ID.
326 * @return true on success, false otherwise
328 * @param hookid The hook id (returned by AddHook)
330 virtual bool RemoveHookByID(int hookid) = 0;
333 * @brief Pause a hook by ID.
335 * @return true on success, false otherwise
337 * @param hookid The hook id (returned by AddHook)
339 virtual bool PauseHookByID(int hookid) = 0;
342 * @brief Unpause a hook by ID.
344 * @return true on success, false otherwise
346 * @param hookid The hook id (returned by AddHook)
348 virtual bool UnpauseHookByID(int hookid) = 0;
351 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
353 * @param plug The unique identifier of the plugin in question
356 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
357 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
358 //!< previously calledhandler
359 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
360 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
361 //!< If not in post function, undefined
362 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
363 //!< If none is specified, NULL
364 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
366 virtual void *GetOverrideRetPtr() = 0; //!< Used for setting the override return value
369 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
371 * @param plug The owner of the hook manager
372 * @param pubFunc The hook manager's info function
374 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
379 * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
381 * @param plug The unique identifier of the plugin that calls this function
382 * @param vfnptr The virtual function pointer of the function in question
384 virtual void SetIgnoreHooks(void *vfnptr) = 0;
387 * @brief Reverses SetIgnoreHooks' effect
389 * @param plug The unique identifier of the plugin that calls this function
390 * @param vfnptr The virtual function pointer of the function in question
392 virtual void ResetIgnoreHooks(void *vfnptr) = 0;
395 * @brief Finds the original entry of a virtual function pointer
397 * @param vfnptr The virtual function pointer
398 * @return The original entry if the virtual function pointer has been patched; NULL otherwise.
400 virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
402 //////////////////////////////////////////////////////////////////////////
406 // Returns the current iface ptr
407 virtual void DoRecall() = 0; //!< Initiates a recall sequence
413 Users want the ability to change parameters of the called function
414 from inside their handler.
417 2) Recall the function
418 3) => SH's hook func gets called:
419 4) The first iterator points at the first hook the last hookfunc didn't execute yet
420 5) does all iteration and returns normally
421 6) The user's handler returns immediately
422 7) The hook func returns immediately as well
424 Also note that the recalled hookfunc starts with the status the recalling hookfunc
425 ended with. The last handler (doing the recall) is also able to specify its own
430 * @brief Set up the hook loop.
432 * @param statusPtr pointer to status variable
433 * @param prevResPtr pointer to previous result variable
434 * @param curResPtr pointer to current result variable
435 * @param ifacePtrPtr pointer to interface this pointer variable
436 * @param origRetPr pointer to original return value variable. NULL for void funcs
437 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
439 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
440 * when the hook func is being called as part of a recall
442 virtual IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry,
443 META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
444 const void *origRetPtr, void *overrideRetPtr) = 0;
446 virtual void EndContext(IHookContext *pCtx) = 0;
449 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
450 // These have to be able to return references. If T is a reference, the pointers returned
451 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
452 template <class T> struct MacroRefHelpers
454 inline static const T* GetOrigRet(ISourceHook *shptr)
456 return reinterpret_cast<const T*>(shptr->GetOrigRet());
458 inline static const T* GetOverrideRet(ISourceHook *shptr)
460 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
464 template <class T> struct MacroRefHelpers<T&>
466 inline static T* GetOrigRet(ISourceHook *shptr)
468 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
471 inline static T* GetOverrideRet(ISourceHook *shptr)
473 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
478 template <class X, class MFP>
479 void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
481 SourceHook::MemFuncInfo info = {true, -1, 0, 0};
482 SourceHook::GetFuncInfo(pInstance, mfp, info);
484 void *vfnptr = reinterpret_cast<void*>(
485 *reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
487 void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
489 return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
493 /************************************************************************/
494 /* High level interface */
495 /************************************************************************/
496 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
497 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
498 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
499 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
500 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
502 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
503 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
504 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
508 SourceHook::CallClass<T> *SH_GET_CALLCLASS(T *p)
510 return static_cast< SourceHook::CallClass<T> *>(p);
512 #define SH_RELEASE_CALLCLASS(p)
514 #define SH_GET_MCALLCLASS(p, size) reinterpret_cast<void*>(p)
516 // only call these from the hook handlers directly!
518 // If a hook on a function which returns a reference does not want to specify a return value,
519 // it can use this macro.
520 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
521 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
523 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
524 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
526 // SourceHook::SetOverrideRet is defined later.
527 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
529 SET_META_RESULT(result); \
530 SH_GLOB_SHPTR->DoRecall(); \
531 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
532 RETURN_META(MRES_SUPERCEDE); \
535 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
537 SET_META_RESULT(result); \
538 SH_GLOB_SHPTR->DoRecall(); \
539 if ((result) >= MRES_OVERRIDE) \
541 /* meh, set the override result here because we don't get a chance to return */ \
542 /* before continuing the hook loop through the recall */ \
543 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
545 RETURN_META_VALUE(MRES_SUPERCEDE, \
546 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
549 // :TODO: thisptroffs in MNEWPARAMS ??
551 #if SH_COMP == SH_COMP_MSVC
553 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
556 SH_MFHCls(hookname)::ECMFP mfp; \
559 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
560 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
561 SH_MFHCls(hookname)::ms_MFI.vtblindex];
563 #elif SH_COMP == SH_COMP_GCC
565 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
568 SH_MFHCls(hookname)::ECMFP mfp; \
575 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
576 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
577 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
582 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
584 SET_META_RESULT(result); \
585 SH_GLOB_SHPTR->DoRecall(); \
586 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
587 (thisptr->*(u.mfp)) newparams; \
588 RETURN_META(MRES_SUPERCEDE); \
591 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
593 SET_META_RESULT(result); \
594 SH_GLOB_SHPTR->DoRecall(); \
595 if ((result) >= MRES_OVERRIDE) \
597 /* see RETURN_META_VALUE_NEWPARAMS */ \
598 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
600 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
601 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
605 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
607 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
608 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
609 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
610 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
613 #define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
615 // New ADD / REMOVE macros.
616 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
617 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
619 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
620 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
621 SourceHook::ISourceHook::Hook_Normal, post, handler)
623 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
624 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
627 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
628 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), \
629 SourceHook::ISourceHook::Hook_Normal, post, handler)
631 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
632 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
634 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
635 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
636 SourceHook::ISourceHook::Hook_VP, post, handler)
638 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
639 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
640 SourceHook::ISourceHook::Hook_DVP, post, handler)
642 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
643 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_VP, post, handler)
645 #define SH_ADD_MANUALDVPHOOK(hookname, ifaceptr, handler, post) \
646 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_DVP, post, handler)
648 #define SH_REMOVE_HOOK_ID(hookid) \
649 (SH_GLOB_SHPTR->RemoveHookByID(hookid))
652 // !! These are now deprecated. Instead, use one of these:
653 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
654 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
656 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
657 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
658 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
659 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
661 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
662 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
664 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
665 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
666 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
667 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
669 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
670 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
672 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
673 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
674 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
675 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
677 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
678 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
680 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
681 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
682 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
683 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
690 #if SH_COMP == SH_COMP_MSVC
691 # define SH_SETUP_MFP(mfp) \
692 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
693 #elif SH_COMP == SH_COMP_GCC
694 # define SH_SETUP_MFP(mfp) \
695 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
696 reinterpret_cast<void**>(&mfp)[1] = 0;
698 # error Not supported yet.
701 //////////////////////////////////////////////////////////////////////////
702 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
703 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
705 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
706 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
708 using namespace ::SourceHook; \
709 GetFuncInfo(funcptr, ms_MFI); \
710 /* Verify interface version */ \
711 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
713 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
719 MemFuncInfo mfi = {true, -1, 0, 0}; \
720 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
721 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
722 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
727 // It has to be possible to use the macros in namespaces
728 // -> So we need to access and extend the global SourceHook namespace
729 // We use a namespace alias for this
730 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
731 struct SH_FHCls(ifacetype,ifacefunc,overload) \
733 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
734 static ::SourceHook::MemFuncInfo ms_MFI; \
735 static ::SourceHook::IHookManagerInfo *ms_HI; \
736 static ::SourceHook::ProtoInfo ms_Proto; \
737 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
739 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
741 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
742 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
743 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
744 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
745 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
747 using namespace ::SourceHook; \
748 MemFuncInfo mfi = {true, -1, 0, 0}; \
749 GetFuncInfo(funcptr, mfi); \
750 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
751 return false; /* No non-virtual functions / virtual inheritance supported */ \
753 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
754 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
755 new SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl(handler), post); \
757 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
758 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
760 using namespace ::SourceHook; \
761 MemFuncInfo mfi = {true, -1, 0, 0}; \
762 GetFuncInfo(funcptr, mfi); \
763 SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl tmp(handler); \
764 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
765 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
768 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
769 struct SH_MFHCls(hookname) \
771 static SH_MFHCls(hookname) ms_Inst; \
772 static ::SourceHook::MemFuncInfo ms_MFI; \
773 static ::SourceHook::IHookManagerInfo *ms_HI; \
774 static ::SourceHook::ProtoInfo ms_Proto; \
776 SH_MFHCls(hookname)() \
778 ms_MFI.isVirtual = true; \
779 ms_MFI.thisptroffs = pthisptroffs; \
780 ms_MFI.vtblindex = pvtblidx; \
781 ms_MFI.vtbloffs = pvtbloffs; \
783 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
785 using namespace ::SourceHook; \
786 /* Verify interface version */ \
787 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
789 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
795 MemFuncInfo mfi = {true, -1, 0, 0}; \
796 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
797 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
798 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
803 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
805 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
806 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
807 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
808 int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
809 SH_MFHCls(hookname)::FD handler) \
811 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
812 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
813 new SH_MFHCls(hookname)::CMyDelegateImpl(handler), post); \
815 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
816 SH_MFHCls(hookname)::FD handler) \
818 SH_MFHCls(hookname)::CMyDelegateImpl tmp(handler); \
819 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
820 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
823 #define SH_SETUPCALLS(rettype, paramtypes, params) \
825 using namespace ::SourceHook; \
826 void *ourvfnptr = reinterpret_cast<void*>( \
827 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
828 void *vfnptr_origentry; \
830 META_RES status = MRES_IGNORED; \
834 typedef ReferenceCarrier<rettype>::type my_rettype; \
835 my_rettype orig_ret; \
836 my_rettype override_ret; \
837 my_rettype plugin_ret; \
839 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
840 &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
842 #define SH_CALL_HOOKS(post, params) \
843 prev_res = MRES_IGNORED; \
844 while ( iter = static_cast<IMyDelegate*>(pContext->GetNext()) ) \
846 cur_res = MRES_IGNORED; \
847 plugin_ret = iter->Call params; \
848 prev_res = cur_res; \
849 if (cur_res > status) \
851 if (cur_res >= MRES_OVERRIDE) \
852 *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret; \
855 #define SH_CALL_ORIG(rettype, paramtypes, params) \
856 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
858 rettype (EmptyClass::*mfp)paramtypes; \
860 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
863 orig_ret = override_ret; /* :TODO: ??? : use pContext->GetOverrideRetPtr() or not? */
865 #define SH_RETURN() \
866 const my_rettype *retptr = reinterpret_cast<const my_rettype*>( \
867 (status >= MRES_OVERRIDE) ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()); \
868 SH_GLOB_SHPTR->EndContext(pContext); \
871 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
872 SH_SETUPCALLS(rettype, paramtypes, params) \
873 SH_CALL_HOOKS(pre, params) \
874 SH_CALL_ORIG(rettype, paramtypes, params) \
875 SH_CALL_HOOKS(post, params) \
878 //////////////////////////////////////////////////////////////////////////
879 #define SH_SETUPCALLS_void(paramtypes, params) \
881 using namespace ::SourceHook; \
882 void *ourvfnptr = reinterpret_cast<void*>( \
883 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
884 void *vfnptr_origentry; \
886 META_RES status = MRES_IGNORED; \
891 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
892 &vfnptr_origentry, &status, &prev_res, &cur_res, NULL, NULL);
894 #define SH_CALL_HOOKS_void(post, params) \
895 prev_res = MRES_IGNORED; \
896 while ( iter = static_cast<IMyDelegate*>(pContext->GetNext()) ) \
898 cur_res = MRES_IGNORED; \
900 prev_res = cur_res; \
901 if (cur_res > status) \
905 #define SH_CALL_ORIG_void(paramtypes, params) \
906 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
908 void (EmptyClass::*mfp)paramtypes; \
910 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
913 #define SH_RETURN_void() \
914 SH_GLOB_SHPTR->EndContext(pContext);
916 #define SH_HANDLEFUNC_void(paramtypes, params) \
917 SH_SETUPCALLS_void(paramtypes, params) \
918 SH_CALL_HOOKS_void(pre, params) \
919 SH_CALL_ORIG_void(paramtypes, params) \
920 SH_CALL_HOOKS_void(post, params) \
924 // Special vafmt handlers
926 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
927 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
928 SH_CALL_HOOKS(pre, params_plug) \
929 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
930 SH_CALL_HOOKS(post, params_plug) \
933 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
934 SH_SETUPCALLS_void(paramtypes, params_orig) \
935 SH_CALL_HOOKS_void(pre, params_plug) \
936 SH_CALL_ORIG_void(paramtypes, params_orig) \
937 SH_CALL_HOOKS_void(post, params_plug) \
940 //////////////////////////////////////////////////////////////////////////
942 #define MAKE_DELEG(ret_type, params_decl, params_pass) \
943 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual ret_type Call params_decl = 0; }; \
944 struct CMyDelegateImpl : IMyDelegate \
947 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
948 ret_type Call params_decl { return m_Deleg params_pass; } \
949 void DeleteThis() { delete this; } \
950 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
953 #define MAKE_DELEG_void(params_decl, params_pass) \
954 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual void Call params_decl = 0; }; \
955 struct CMyDelegateImpl : IMyDelegate \
958 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
959 void Call params_decl { m_Deleg params_pass; } \
960 void DeleteThis() { delete this; } \
961 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
965 // sizeof on references returns the size of the datatype, NOT the pointer size or something
966 // -> one should probably flag references in __SourceHook_ParamSizes_* !
967 // or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
970 // ********* Support for $1 arguments *********
971 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
972 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
973 (&ifacetype::ifacefunc))) \
974 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
975 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
976 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
977 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
978 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
979 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
981 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
982 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
983 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
986 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
987 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
988 (&ifacetype::ifacefunc))) \
989 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
990 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
991 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
992 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
993 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
994 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
996 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
997 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
998 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
1000 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1001 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1002 (&ifacetype::ifacefunc))) \
1003 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1004 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1005 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@] @[$1!=0:, @]const char *fmt, ...) \
1007 char buf[::SourceHook::STRBUF_LEN]; \
1009 va_start(ap, fmt); \
1010 vsnprintf(buf, sizeof(buf), fmt, ap); \
1012 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); \
1014 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1015 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1017 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1018 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
1019 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
1021 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1022 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1023 (&ifacetype::ifacefunc))) \
1024 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1025 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1026 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1028 char buf[::SourceHook::STRBUF_LEN]; \
1030 va_start(ap, fmt); \
1031 vsnprintf(buf, sizeof(buf), fmt, ap); \
1033 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)); \
1035 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1036 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1038 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1039 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
1040 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
1042 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1043 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1044 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1045 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1046 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1047 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1048 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1049 typedef rettype RetType; \
1050 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1052 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1053 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
1054 __SourceHook_ParamInfosM_##hookname, 0 };
1056 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1057 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1058 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1059 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1060 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1061 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1062 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1063 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1065 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1066 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
1067 __SourceHook_ParamInfosM_##hookname , 0 };
1072 //////////////////////////////////////////////////////////////////////////
1075 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1077 using namespace ::SourceHook; \
1079 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1080 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1081 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1085 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1087 using namespace ::SourceHook; \
1089 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1090 (m_ThisPtr->*m_MFP)call; \
1091 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1094 namespace SourceHook
1097 // Support for $1 arguments
1098 template<class ObjType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1105 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1106 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1108 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1109 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1112 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1113 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1117 template<class ObjType, class MFPType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFPType, void@[$2,1,$1:, Param$2@]>
1124 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1125 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1127 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1128 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1131 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1132 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1139 #define SH__CALL_GET_VFNPTR_NORMAL \
1140 using namespace ::SourceHook; \
1141 MemFuncInfo mfi = {true, -1, 0, 0}; \
1142 GetFuncInfo(ptr, mfp, mfi); \
1143 void *vfnptr = reinterpret_cast<void*>( \
1144 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1146 #define SH__CALL_GET_VFNPTR_MANUAL \
1147 using namespace ::SourceHook; \
1148 void *vfnptr = reinterpret_cast<void*>( \
1149 *reinterpret_cast<void***>( (reinterpret_cast<char*>(ptr) + thisptroffs + vtbloffs) ) + vtblidx); \
1151 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1153 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1154 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1155 // The only purpose of the mfp2 parameter is to extract the return type
1158 // Support for $1 arguments
1159 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1160 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1161 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr)
1163 SH__CALL_GET_VFNPTR_NORMAL
1164 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1167 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1168 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1169 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr)
1171 SH__CALL_GET_VFNPTR_NORMAL
1172 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1175 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1176 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1177 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr)
1179 SH__CALL_GET_VFNPTR_MANUAL
1180 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1181 reinterpret_cast<SourceHook::EmptyClass*>(ptr), mfp, vfnptr, shptr);
1185 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1186 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1189 // Support for $1 arguments
1190 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1191 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1192 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr)
1194 SH__CALL_GET_VFNPTR_NORMAL
1195 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1198 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1199 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1200 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr)
1202 SH__CALL_GET_VFNPTR_NORMAL
1203 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1210 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR)
1211 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR)
1212 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1213 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1215 #undef SH_MAKE_EXECUTABLECLASS_OB
1216 #undef SH_MAKE_EXECUTABLECLASS_OB_void
1218 //////////////////////////////////////////////////////////////////////////
1219 // SetOverrideRet and RecallGetIface for recalls
1220 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1221 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1222 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1224 namespace SourceHook
1226 template <class RetType>
1227 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1229 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1232 // SetOverrideResult used to be implemented like this:
1233 // SetOverrideResult(shptr, memfuncptr, return);
1234 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1235 // (it thought it was ambigous - the ref and non-ref type)
1236 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1237 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1238 // This also allows us to create a special version for references which respects ReferenceCarrier.
1240 template <class T> struct OverrideFunctor
1242 void operator()(ISourceHook *shptr, T res)
1244 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1247 template <class T> struct OverrideFunctor<T&>
1249 void operator()(ISourceHook *shptr, T &res)
1251 // overrideretptr points to ReferenceCarrier<T&>
1252 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1256 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1257 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1259 return OverrideFunctor<RetType>();
1262 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1263 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1265 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1271 // The pope is dead. -> :(