Removed "trust the prototype" model which could lead to problems. Instead, every CVfnPtr now has a list of hook managers.
-> Removed CHookManagerContainer
Also removed listcat (unnecessary since "V2" interface)
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 normal non-trivial constructor */
175 PassFlag_AssignOp = (1<<4), /**< Object has a non-trivial assignment operator */
176 PassFlag_CCtor = (1<<5) /**< Object has a copy constructor (which takes const Object& as only parameter) */
179 size_t size; //!< Size of the data being passed
182 // (might be used in future versions for automatic hookfunc generation)
183 int type; //!< PassType value
184 unsigned int flags; //!< Pass/return flags
191 void *pAssignOperator;
199 CallConv_Unknown, /**< Unknown -- no extra info available */
200 CallConv_ThisCall, /**< This call (object pointer required) */
201 CallConv_Cdecl, /**< Standard C call */
204 int numOfParams; //!< number of parameters
205 PassInfo retPassInfo; //!< PassInfo for the return value. size=0 -> no retval
206 const PassInfo *paramsPassInfo; //!< PassInfos for the parameters
208 // paramsPassInfo[0] is basically a dummy parameter.
209 // However, paramsPassInfo[0].size stores the version of the ProtoInfo structure.
215 PassInfo::V2Info retPassInfo2;
216 const PassInfo::V2Info *paramsPassInfo2;
219 struct IHookManagerInfo;
222 * @brief Pointer to hook manager interface function
224 * The hook manager should store hi for later use if store==true. It should then call hi->SetInfo(...) if hi
225 * is non-null. The hook manager can return 0 for success or a non-zero value if it doesn't want to be used.
227 * @param hi A pointer to IHookManagerInfo
229 typedef int (*HookManagerPubFunc)(bool store, IHookManagerInfo *hi);
234 virtual bool IsEqual(ISHDelegate *pOtherDeleg) = 0; // pOtherDeleg is from the same plugin and hookman
235 virtual void DeleteThis() = 0;
238 struct IHookManagerInfo
240 virtual void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
241 ProtoInfo *proto, void *hookfunc_vfnptr) = 0;
244 // I'm adding support for functions which return references.
247 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
248 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
249 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
250 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
251 // special cases were needed for getoverrideret / getorigret; these are implemented through the
252 // SourceHook::MacroRefHelpers structs.
253 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
254 template <class T> struct ReferenceCarrier
259 template <class T> struct ReferenceCarrier<T&>
265 type() : m_StoredRef(NULL)
268 type(T& ref) : m_StoredRef(&ref)
272 T& operator= (T& ref)
287 virtual ISHDelegate *GetNext() = 0;
288 virtual void *GetOverrideRetPtr() = 0;
289 virtual const void *GetOrigRetPtr() = 0;
290 virtual bool ShouldCallOrig() = 0;
294 * @brief The main SourceHook interface
300 * @brief Return interface version
302 virtual int GetIfaceVersion() = 0;
305 * @brief Return implementation version
307 virtual int GetImplVersion() = 0;
310 * @brief Modes for the new AddHook
320 * @brief Add a (VP) hook.
322 * @return non-zero hook id on success, 0 otherwise
324 * @param plug The unique identifier of the plugin that calls this function
325 * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
326 * @param iface The interface pointer
327 * @param ifacesize The size of the class iface points to
328 * @param myHookMan A hook manager function that should be capable of handling the function
329 * @param handler A pointer to the hook handler something
330 * @param post Set to true if you want a post handler
332 virtual int AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
333 ISHDelegate *handler, bool post) = 0;
335 // Source backwarts compat (only for normal hooks)
336 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
337 ISHDelegate *handler, bool post) = 0;
340 * @brief Remove a hook by ID.
342 * @return true on success, false otherwise
344 * @param hookid The hook id (returned by AddHook)
346 virtual bool RemoveHookByID(int hookid) = 0;
349 * @brief Pause a hook by ID.
351 * @return true on success, false otherwise
353 * @param hookid The hook id (returned by AddHook)
355 virtual bool PauseHookByID(int hookid) = 0;
358 * @brief Unpause a hook by ID.
360 * @return true on success, false otherwise
362 * @param hookid The hook id (returned by AddHook)
364 virtual bool UnpauseHookByID(int hookid) = 0;
367 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
369 * @param plug The unique identifier of the plugin in question
372 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
373 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
374 //!< previously calledhandler
375 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
376 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
377 //!< If not in post function, undefined
378 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
379 //!< If none is specified, NULL
380 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
382 virtual void *GetOverrideRetPtr() = 0; //!< Used for setting the override return value
385 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
387 * @param plug The owner of the hook manager
388 * @param pubFunc The hook manager's info function
390 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
395 * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
397 * @param plug The unique identifier of the plugin that calls this function
398 * @param vfnptr The virtual function pointer of the function in question
400 virtual void SetIgnoreHooks(void *vfnptr) = 0;
403 * @brief Reverses SetIgnoreHooks' effect
405 * @param plug The unique identifier of the plugin that calls this function
406 * @param vfnptr The virtual function pointer of the function in question
408 virtual void ResetIgnoreHooks(void *vfnptr) = 0;
411 * @brief Finds the original entry of a virtual function pointer
413 * @param vfnptr The virtual function pointer
414 * @return The original entry if the virtual function pointer has been patched; NULL otherwise.
416 virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
418 //////////////////////////////////////////////////////////////////////////
422 // Returns the current iface ptr
423 virtual void DoRecall() = 0; //!< Initiates a recall sequence
429 Users want the ability to change parameters of the called function
430 from inside their handler.
433 2) Recall the function
434 3) => SH's hook func gets called:
435 4) The first iterator points at the first hook the last hookfunc didn't execute yet
436 5) does all iteration and returns normally
437 6) The user's handler returns immediately
438 7) The hook func returns immediately as well
440 Also note that the recalled hookfunc starts with the status the recalling hookfunc
441 ended with. The last handler (doing the recall) is also able to specify its own
446 * @brief Set up the hook loop.
448 * @param statusPtr pointer to status variable
449 * @param prevResPtr pointer to previous result variable
450 * @param curResPtr pointer to current result variable
451 * @param ifacePtrPtr pointer to interface this pointer variable
452 * @param origRetPr pointer to original return value variable. NULL for void funcs
453 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
455 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
456 * when the hook func is being called as part of a recall
458 virtual IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry,
459 META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
460 const void *origRetPtr, void *overrideRetPtr) = 0;
462 virtual void EndContext(IHookContext *pCtx) = 0;
465 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
466 // These have to be able to return references. If T is a reference, the pointers returned
467 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
468 template <class T> struct MacroRefHelpers
470 inline static const T* GetOrigRet(ISourceHook *shptr)
472 return reinterpret_cast<const T*>(shptr->GetOrigRet());
474 inline static const T* GetOverrideRet(ISourceHook *shptr)
476 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
480 template <class T> struct MacroRefHelpers<T&>
482 inline static T* GetOrigRet(ISourceHook *shptr)
484 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
487 inline static T* GetOverrideRet(ISourceHook *shptr)
489 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
494 template <class X, class MFP>
495 void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
497 SourceHook::MemFuncInfo info = {true, -1, 0, 0};
498 SourceHook::GetFuncInfo(pInstance, mfp, info);
500 void *vfnptr = reinterpret_cast<void*>(
501 *reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
503 void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
505 return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
508 template <class T> struct GetPassInfo
510 static const int type = 0;
511 static const unsigned int flags = PassInfo::PassFlag_ByVal;
514 template <class T> struct GetPassInfo<T&>
516 static const int type = 0;
517 static const unsigned int flags = PassInfo::PassFlag_ByRef;
521 /************************************************************************/
522 /* High level interface */
523 /************************************************************************/
524 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
525 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
526 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
527 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
528 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
530 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
531 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
532 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
536 SourceHook::CallClass<T> *SH_GET_CALLCLASS(T *p)
538 return static_cast< SourceHook::CallClass<T> *>(p);
540 #define SH_RELEASE_CALLCLASS(p)
542 #define SH_GET_MCALLCLASS(p, size) reinterpret_cast<void*>(p)
544 // only call these from the hook handlers directly!
546 // If a hook on a function which returns a reference does not want to specify a return value,
547 // it can use this macro.
548 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
549 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
551 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
552 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
554 // SourceHook::SetOverrideRet is defined later.
555 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
557 SET_META_RESULT(result); \
558 SH_GLOB_SHPTR->DoRecall(); \
559 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
560 RETURN_META(MRES_SUPERCEDE); \
563 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
565 SET_META_RESULT(result); \
566 SH_GLOB_SHPTR->DoRecall(); \
567 if ((result) >= MRES_OVERRIDE) \
569 /* meh, set the override result here because we don't get a chance to return */ \
570 /* before continuing the hook loop through the recall */ \
571 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
573 RETURN_META_VALUE(MRES_SUPERCEDE, \
574 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
577 // :TODO: thisptroffs in MNEWPARAMS ??
579 #if SH_COMP == SH_COMP_MSVC
581 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
584 SH_MFHCls(hookname)::ECMFP mfp; \
587 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
588 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
589 SH_MFHCls(hookname)::ms_MFI.vtblindex];
591 #elif SH_COMP == SH_COMP_GCC
593 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
596 SH_MFHCls(hookname)::ECMFP mfp; \
603 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
604 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
605 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
610 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
612 SET_META_RESULT(result); \
613 SH_GLOB_SHPTR->DoRecall(); \
614 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
615 (thisptr->*(u.mfp)) newparams; \
616 RETURN_META(MRES_SUPERCEDE); \
619 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
621 SET_META_RESULT(result); \
622 SH_GLOB_SHPTR->DoRecall(); \
623 if ((result) >= MRES_OVERRIDE) \
625 /* see RETURN_META_VALUE_NEWPARAMS */ \
626 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
628 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
629 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
633 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
635 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
636 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
637 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
638 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
641 #define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
643 // New ADD / REMOVE macros.
644 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
645 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
647 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
648 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
649 SourceHook::ISourceHook::Hook_Normal, post, handler)
651 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
652 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
655 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
656 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), \
657 SourceHook::ISourceHook::Hook_Normal, post, handler)
659 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
660 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
662 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
663 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
664 SourceHook::ISourceHook::Hook_VP, post, handler)
666 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
667 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
668 SourceHook::ISourceHook::Hook_DVP, post, handler)
670 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
671 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_VP, post, handler)
673 #define SH_ADD_MANUALDVPHOOK(hookname, ifaceptr, handler, post) \
674 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_DVP, post, handler)
676 #define SH_REMOVE_HOOK_ID(hookid) \
677 (SH_GLOB_SHPTR->RemoveHookByID(hookid))
680 // !! These are now deprecated. Instead, use one of these:
681 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
682 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
684 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
685 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
686 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
687 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
689 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
690 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
692 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
693 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
694 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
695 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
697 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
698 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
700 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
701 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
702 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
703 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
705 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
706 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
708 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
709 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
710 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
711 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
718 #if SH_COMP == SH_COMP_MSVC
719 # define SH_SETUP_MFP(mfp) \
720 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
721 #elif SH_COMP == SH_COMP_GCC
722 # define SH_SETUP_MFP(mfp) \
723 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
724 reinterpret_cast<void**>(&mfp)[1] = 0;
726 # error Not supported yet.
729 //////////////////////////////////////////////////////////////////////////
730 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
731 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
733 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
734 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
736 using namespace ::SourceHook; \
737 GetFuncInfo(funcptr, ms_MFI); \
738 /* Verify interface version */ \
739 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
741 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
747 MemFuncInfo mfi = {true, -1, 0, 0}; \
748 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
749 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
750 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
755 // It has to be possible to use the macros in namespaces
756 // -> So we need to access and extend the global SourceHook namespace
757 // We use a namespace alias for this
758 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
759 struct SH_FHCls(ifacetype,ifacefunc,overload) \
761 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
762 static ::SourceHook::MemFuncInfo ms_MFI; \
763 static ::SourceHook::IHookManagerInfo *ms_HI; \
764 static ::SourceHook::ProtoInfo ms_Proto; \
765 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
767 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
769 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
770 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
771 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
772 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
773 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
775 using namespace ::SourceHook; \
776 MemFuncInfo mfi = {true, -1, 0, 0}; \
777 GetFuncInfo(funcptr, mfi); \
778 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
779 return false; /* No non-virtual functions / virtual inheritance supported */ \
781 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
782 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
783 new SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl(handler), post); \
785 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
786 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
788 using namespace ::SourceHook; \
789 MemFuncInfo mfi = {true, -1, 0, 0}; \
790 GetFuncInfo(funcptr, mfi); \
791 SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl tmp(handler); \
792 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
793 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
796 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
797 struct SH_MFHCls(hookname) \
799 static SH_MFHCls(hookname) ms_Inst; \
800 static ::SourceHook::MemFuncInfo ms_MFI; \
801 static ::SourceHook::IHookManagerInfo *ms_HI; \
802 static ::SourceHook::ProtoInfo ms_Proto; \
804 SH_MFHCls(hookname)() \
806 ms_MFI.isVirtual = true; \
807 ms_MFI.thisptroffs = pthisptroffs; \
808 ms_MFI.vtblindex = pvtblidx; \
809 ms_MFI.vtbloffs = pvtbloffs; \
811 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
813 using namespace ::SourceHook; \
814 /* Verify interface version */ \
815 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
817 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
823 MemFuncInfo mfi = {true, -1, 0, 0}; \
824 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
825 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
826 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
831 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
833 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
834 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
835 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
836 int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
837 SH_MFHCls(hookname)::FD handler) \
839 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
840 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
841 new SH_MFHCls(hookname)::CMyDelegateImpl(handler), post); \
843 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
844 SH_MFHCls(hookname)::FD handler) \
846 SH_MFHCls(hookname)::CMyDelegateImpl tmp(handler); \
847 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
848 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
851 #define SH_SETUPCALLS(rettype, paramtypes, params) \
853 using namespace ::SourceHook; \
854 void *ourvfnptr = reinterpret_cast<void*>( \
855 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
856 void *vfnptr_origentry; \
858 META_RES status = MRES_IGNORED; \
862 typedef ReferenceCarrier< rettype >::type my_rettype; \
863 my_rettype orig_ret; \
864 my_rettype override_ret; \
865 my_rettype plugin_ret; \
867 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
868 &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
870 #define SH_CALL_HOOKS(post, params) \
871 prev_res = MRES_IGNORED; \
872 while ( (iter = static_cast<IMyDelegate*>(pContext->GetNext())) ) \
874 cur_res = MRES_IGNORED; \
875 plugin_ret = iter->Call params; \
876 prev_res = cur_res; \
877 if (cur_res > status) \
879 if (cur_res >= MRES_OVERRIDE) \
880 *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret; \
883 #define SH_CALL_ORIG(rettype, paramtypes, params) \
884 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
886 rettype (EmptyClass::*mfp)paramtypes; \
888 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
891 orig_ret = override_ret; /* :TODO: ??? : use pContext->GetOverrideRetPtr() or not? */
893 #define SH_RETURN() \
894 const my_rettype *retptr = reinterpret_cast<const my_rettype*>( \
895 (status >= MRES_OVERRIDE) ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()); \
896 SH_GLOB_SHPTR->EndContext(pContext); \
899 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
900 SH_SETUPCALLS(rettype, paramtypes, params) \
901 SH_CALL_HOOKS(pre, params) \
902 SH_CALL_ORIG(rettype, paramtypes, params) \
903 SH_CALL_HOOKS(post, params) \
906 //////////////////////////////////////////////////////////////////////////
907 #define SH_SETUPCALLS_void(paramtypes, params) \
909 using namespace ::SourceHook; \
910 void *ourvfnptr = reinterpret_cast<void*>( \
911 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
912 void *vfnptr_origentry; \
914 META_RES status = MRES_IGNORED; \
919 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
920 &vfnptr_origentry, &status, &prev_res, &cur_res, NULL, NULL);
922 #define SH_CALL_HOOKS_void(post, params) \
923 prev_res = MRES_IGNORED; \
924 while ( (iter = static_cast<IMyDelegate*>(pContext->GetNext())) ) \
926 cur_res = MRES_IGNORED; \
928 prev_res = cur_res; \
929 if (cur_res > status) \
933 #define SH_CALL_ORIG_void(paramtypes, params) \
934 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
936 void (EmptyClass::*mfp)paramtypes; \
938 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
941 #define SH_RETURN_void() \
942 SH_GLOB_SHPTR->EndContext(pContext);
944 #define SH_HANDLEFUNC_void(paramtypes, params) \
945 SH_SETUPCALLS_void(paramtypes, params) \
946 SH_CALL_HOOKS_void(pre, params) \
947 SH_CALL_ORIG_void(paramtypes, params) \
948 SH_CALL_HOOKS_void(post, params) \
952 // Special vafmt handlers
954 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
955 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
956 SH_CALL_HOOKS(pre, params_plug) \
957 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
958 SH_CALL_HOOKS(post, params_plug) \
961 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
962 SH_SETUPCALLS_void(paramtypes, params_orig) \
963 SH_CALL_HOOKS_void(pre, params_plug) \
964 SH_CALL_ORIG_void(paramtypes, params_orig) \
965 SH_CALL_HOOKS_void(post, params_plug) \
968 //////////////////////////////////////////////////////////////////////////
970 #define MAKE_DELEG(ret_type, params_decl, params_pass) \
971 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual ret_type Call params_decl = 0; }; \
972 struct CMyDelegateImpl : IMyDelegate \
975 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
976 ret_type Call params_decl { return m_Deleg params_pass; } \
977 void DeleteThis() { delete this; } \
978 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
981 #define MAKE_DELEG_void(params_decl, params_pass) \
982 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual void Call params_decl = 0; }; \
983 struct CMyDelegateImpl : IMyDelegate \
986 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
987 void Call params_decl { m_Deleg params_pass; } \
988 void DeleteThis() { delete this; } \
989 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
992 // GetPassInfo -> easier access. __SH_GPI generates a SourceHook::PassInfo instance.
993 #define __SH_GPI(tt) { sizeof(tt), ::SourceHook::GetPassInfo< tt >::type, ::SourceHook::GetPassInfo< tt >::flags }
994 // Extra pass info V2
995 #define __SH_EPI { 0, 0, 0, 0 }
998 // ********* Support for $1 arguments *********
999 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1000 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1001 (&ifacetype::ifacefunc))) \
1002 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1003 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1004 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1005 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1006 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1007 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1009 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1010 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1011 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, __SH_GPI(rettype), \
1012 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1015 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1016 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1017 (&ifacetype::ifacefunc))) \
1018 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1019 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1020 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1021 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1022 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1023 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1025 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:,__SH_GPI(param$2)@] }; \
1026 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1027 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
1028 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1030 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1031 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1032 (&ifacetype::ifacefunc))) \
1033 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1034 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1035 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@] @[$1!=0:, @]const char *fmt, ...) \
1037 char buf[::SourceHook::STRBUF_LEN]; \
1039 va_start(ap, fmt); \
1040 vsnprintf(buf, sizeof(buf), fmt, ap); \
1042 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); \
1044 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1045 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1047 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1048 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1049 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, __SH_GPI(rettype), \
1050 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1052 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1053 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1054 (&ifacetype::ifacefunc))) \
1055 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1056 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1057 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1059 char buf[::SourceHook::STRBUF_LEN]; \
1061 va_start(ap, fmt); \
1062 vsnprintf(buf, sizeof(buf), fmt, ap); \
1064 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)); \
1066 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1067 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1069 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1070 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1071 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
1072 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
1074 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1075 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1076 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1077 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1078 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1079 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1080 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1081 typedef rettype RetType; \
1082 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1084 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1085 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1086 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, __SH_GPI(rettype), \
1087 __SourceHook_ParamInfosM_##hookname, 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname };
1089 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1090 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1091 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1092 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1093 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1094 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1095 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1096 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1098 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
1099 const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
1100 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
1101 __SourceHook_ParamInfosM_##hookname , 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname };
1106 //////////////////////////////////////////////////////////////////////////
1109 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1111 using namespace ::SourceHook; \
1113 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1114 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1115 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1119 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1121 using namespace ::SourceHook; \
1123 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1124 (m_ThisPtr->*m_MFP)call; \
1125 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1128 namespace SourceHook
1131 // Support for $1 arguments
1132 template<class ObjType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1139 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1140 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1142 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1143 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1146 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1147 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1151 template<class ObjType, class MFPType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFPType, void@[$2,1,$1:, Param$2@]>
1158 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1159 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1161 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1162 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1165 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1166 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1173 #define SH__CALL_GET_VFNPTR_NORMAL \
1174 using namespace ::SourceHook; \
1175 MemFuncInfo mfi = {true, -1, 0, 0}; \
1176 GetFuncInfo(ptr, mfp, mfi); \
1177 void *vfnptr = reinterpret_cast<void*>( \
1178 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1180 #define SH__CALL_GET_VFNPTR_MANUAL \
1181 using namespace ::SourceHook; \
1182 void *vfnptr = reinterpret_cast<void*>( \
1183 *reinterpret_cast<void***>( (reinterpret_cast<char*>(ptr) + thisptroffs + vtbloffs) ) + vtblidx); \
1185 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1187 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1188 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1189 // The only purpose of the mfp2 parameter is to extract the return type
1192 // Support for $1 arguments
1193 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1194 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1195 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr)
1197 SH__CALL_GET_VFNPTR_NORMAL
1198 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1201 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1202 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1203 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr)
1205 SH__CALL_GET_VFNPTR_NORMAL
1206 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1209 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1210 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1211 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr)
1213 SH__CALL_GET_VFNPTR_MANUAL
1214 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1215 reinterpret_cast<SourceHook::EmptyClass*>(ptr), mfp, vfnptr, shptr);
1219 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1220 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1223 // Support for $1 arguments
1224 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1225 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1226 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr)
1228 SH__CALL_GET_VFNPTR_NORMAL
1229 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1232 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1233 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1234 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr)
1236 SH__CALL_GET_VFNPTR_NORMAL
1237 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1244 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR)
1245 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR)
1246 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1247 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1249 #undef SH_MAKE_EXECUTABLECLASS_OB
1250 #undef SH_MAKE_EXECUTABLECLASS_OB_void
1252 //////////////////////////////////////////////////////////////////////////
1253 // SetOverrideRet and RecallGetIface for recalls
1254 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1255 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1256 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1258 namespace SourceHook
1260 template <class RetType>
1261 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1263 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1266 // SetOverrideResult used to be implemented like this:
1267 // SetOverrideResult(shptr, memfuncptr, return);
1268 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1269 // (it thought it was ambigous - the ref and non-ref type)
1270 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1271 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1272 // This also allows us to create a special version for references which respects ReferenceCarrier.
1274 template <class T> struct OverrideFunctor
1276 void operator()(ISourceHook *shptr, T res)
1278 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1281 template <class T> struct OverrideFunctor<T&>
1283 void operator()(ISourceHook *shptr, T &res)
1285 // overrideretptr points to ReferenceCarrier<T&>
1286 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1290 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1291 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1293 return OverrideFunctor<RetType>();
1296 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1297 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1299 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1305 // The pope is dead. -> :(