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 #define SH_IFACE_VERSION 4
28 // 4 - addition of hook ids and vp hooks (with them, AddHookNew and RemoveHookNew)
29 // This is not a SH_IFACE_VERSION change so that old plugins continue working!
30 // 5 - addition of direct vp hooks (new hook mode; from now on AddHookNew checks for
31 // invalid hookmode -> impl version won't have to change because of things like this)
32 #define SH_IMPL_VERSION 5
35 // 1 - Support for recalls, performance optimisations
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 "FastDelegate.h"
103 #include "sh_memfuncinfo.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.
152 ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
155 int beginningNull; //!< To distinguish from old protos (which never begin with 0)
156 int retTypeSize; //!< 0 if void
157 int numOfParams; //!< number of parameters
158 const int *params; //!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
162 * @brief Specifies the actions for hook managers
164 enum HookManagerAction
166 HA_GetInfo = 0, //!< Store info about the hook manager
167 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
168 HA_Unregister //!< Clear the saved pointer
171 struct IHookManagerInfo;
174 * @brief Pointer to hook manager interface function
176 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
178 * @param ha What the hook manager should do
179 * @param hi A pointer to IHookManagerInfo
181 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
186 virtual void DeleteThis() = 0; // Ugly, I know
187 virtual bool IsEqual(ISHDelegate *other) = 0;
190 template <class T> class CSHDelegate : public ISHDelegate
194 CSHDelegate(T deleg) : m_Deleg(deleg)
198 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
207 bool IsEqual(ISHDelegate *other)
209 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
222 virtual bool End() = 0;
223 virtual void Next() = 0;
224 virtual ISHDelegate *Handler() = 0;
225 virtual int ThisPtrOffs() = 0;
227 virtual IIter *GetIter() = 0;
228 virtual void ReleaseIter(IIter *pIter) = 0;
233 virtual void *GetPtr() = 0;
234 virtual IHookList *GetPreHooks() = 0;
235 virtual IHookList *GetPostHooks() = 0;
240 virtual void *GetVfnPtr() = 0;
241 virtual void *GetOrigEntry() = 0;
243 virtual IIface *FindIface(void *ptr) = 0;
246 struct IHookManagerInfo
248 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
250 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
251 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
253 // Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
254 // If the hookman doesn't set this, it defaults 0
255 // SourceHook prefers hookmans with higher version numbers
256 virtual void SetVersion(int version) = 0;
262 IHookList::IIter *m_pIter;
264 AutoHookIter(IHookList *pList)
265 : m_pList(pList), m_pIter(pList->GetIter())
272 m_pList->ReleaseIter(m_pIter);
277 return m_pIter->End();
285 ISHDelegate *Handler()
287 return m_pIter->Handler();
292 return m_pIter->ThisPtrOffs();
301 template<class B> struct DeprecatedCallClass
303 virtual B *GetThisPtr() = 0;
304 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
307 // 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
308 // - Today is an important day.
309 // I'm adding support for functions which return references.
312 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
313 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
314 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
315 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
316 // special cases were needed for getoverrideret / getorigret; these are implemented through the
317 // SourceHook::MacroRefHelpers structs.
318 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
319 template <class T> struct ReferenceCarrier
324 template <class T> struct ReferenceCarrier<T&>
330 type() : m_StoredRef(NULL)
333 type(T& ref) : m_StoredRef(&ref)
337 T& operator= (T& ref)
351 * @brief The main SourceHook interface
357 * @brief Return interface version
359 virtual int GetIfaceVersion() = 0;
362 * @brief Return implementation version
364 virtual int GetImplVersion() = 0;
369 * @return True if the function succeeded, false otherwise
371 * @param plug The unique identifier of the plugin that calls this function
372 * @param iface The interface pointer
373 * @param ifacesize The size of the class iface points to
374 * @param myHookMan A hook manager function that should be capable of handling the function
375 * @param handler A pointer to a FastDelegate containing the hook handler
376 * @param post Set to true if you want a post handler
378 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
379 ISHDelegate *handler, bool post) = 0;
382 * @brief Removes a hook.
384 * @return True if the function succeeded, false otherwise
386 * @param plug The unique identifier of the plugin that calls this function
387 * @param iface The interface pointer
388 * @param myHookMan A hook manager function that should be capable of handling the function
389 * @param handler A pointer to a FastDelegate containing the hook handler
390 * @param post Set to true if you want a post handler
392 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
393 ISHDelegate *handler, bool post) = 0;
396 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
398 * @param plug The unique identifier of the plugin in question
400 virtual bool IsPluginInUse(Plugin plug) = 0;
403 * @brief Return a pointer to a callclass. Generate a new one if required.
405 * @param iface The interface pointer
406 * @param size Size of the class instance
408 virtual DeprecatedCallClass<void> *GetCallClass(void *iface, size_t size) = 0;
411 * @brief Release a callclass
413 * @param ptr Pointer to the callclass
415 virtual void ReleaseCallClass(DeprecatedCallClass<void> *ptr) = 0;
417 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
418 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
419 //!< previously calledhandler
420 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
421 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
422 //!< If not in post function, undefined
423 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
424 //!< If none is specified, NULL
425 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
426 //////////////////////////////////////////////////////////////////////////
428 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
429 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
430 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
431 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
432 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
433 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
434 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
435 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
436 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
439 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
441 * @param plug The owner of the hook manager
442 * @param pubFunc The hook manager's info function
444 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
446 virtual void DoRecall() = 0; //!< Initiates a recall sequence
451 Users want the ability to change parameters of the called function
452 from inside their handler.
455 2) Recall the function
456 3) => SH's hook func gets called:
457 4) The first iterator points at the first hook the last hookfunc didn't execute yet
458 5) does all iteration and returns normally
459 6) The user's handler returns immediately
460 7) The hook func returns immediately as well
462 Also note that the recalled hookfunc starts with the status the recalling hookfunc
463 ended with. The last handler (doing the recall) is also able to specify its own
467 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
470 * @brief Set up the hook loop. Equivalent to calling:
471 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
473 * @param statusPtr pointer to status variable
474 * @param prevResPtr pointer to previous result variable
475 * @param curResPtr pointer to current result variable
476 * @param ifacePtrPtr pointer to interface this pointer variable
477 * @param origRetPr pointer to original return value variable. NULL for void funcs
478 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
480 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
481 * when the hook func is being called as part of a recall
483 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
484 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
487 * @brief Modes for the new AddHook
497 * @brief Add a (VP) hook.
499 * @return non-zero hook id on success, 0 otherwise
501 * @param plug The unique identifier of the plugin that calls this function
502 * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
503 * @param iface The interface pointer
504 * @param ifacesize The size of the class iface points to
505 * @param myHookMan A hook manager function that should be capable of handling the function
506 * @param handler A pointer to a FastDelegate containing the hook handler
507 * @param post Set to true if you want a post handler
509 virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
510 ISHDelegate *handler, bool post) = 0;
513 * @brief Remove a VP hook by ID.
515 * @return true on success, false otherwise
517 * @param plug The unique identifier of the plugin that calls this function
518 * @param hookid The hook id (returned by AddHookNew)
520 virtual bool RemoveHookByID(Plugin plug, int hookid) = 0;
523 * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
525 * @param plug The unique identifier of the plugin that calls this function
526 * @param vfnptr The virtual function pointer of the function in question
528 virtual void SetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
531 * @brief Reverses SetIgnoreHooks' effect
533 * @param plug The unique identifier of the plugin that calls this function
534 * @param vfnptr The virtual function pointer of the function in question
536 virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
539 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
540 // These have to be able to return references. If T is a reference, the pointers returned
541 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
542 template <class T> struct MacroRefHelpers
544 inline static const T* GetOrigRet(ISourceHook *shptr)
546 return reinterpret_cast<const T*>(shptr->GetOrigRet());
548 inline static const T* GetOverrideRet(ISourceHook *shptr)
550 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
554 template <class T> struct MacroRefHelpers<T&>
556 inline static T* GetOrigRet(ISourceHook *shptr)
558 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
561 inline static T* GetOverrideRet(ISourceHook *shptr)
563 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
568 // For source-level compatibility
569 template <class T> struct CallClass
573 CallClass(T *p) : ptr(p)
583 typedef CallClass<void> GenericCallClass;
584 typedef CallClass<EmptyClass> ManualCallClass;
587 CallClass<T> *GetCallClass(T *p)
589 return new CallClass<T>(p);
593 void ReleaseCallClass(CallClass<T> *p)
600 /************************************************************************/
601 /* High level interface */
602 /************************************************************************/
603 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
604 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
605 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
606 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
607 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
609 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
610 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
611 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
613 // If a hook on a function which returns a reference does not want to specify a return value,
614 // it can use this macro.
615 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
616 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
618 // only call these from the hook handlers directly!
619 // :TODO: enforce it ?
621 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
622 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
624 // SourceHook::SetOverrideRet is defined later.
625 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
627 SET_META_RESULT(result); \
628 SH_GLOB_SHPTR->DoRecall(); \
629 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
630 RETURN_META(MRES_SUPERCEDE); \
633 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
635 SET_META_RESULT(result); \
636 SH_GLOB_SHPTR->DoRecall(); \
637 if ((result) >= MRES_OVERRIDE) \
639 /* meh, set the override result here because we don't get a chance to return */ \
640 /* before continuing the hook loop through the recall */ \
641 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
643 RETURN_META_VALUE(MRES_SUPERCEDE, \
644 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
647 // :TODO: thisptroffs in MNEWPARAMS ??
649 #if SH_COMP == SH_COMP_MSVC
651 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
654 SH_MFHCls(hookname)::ECMFP mfp; \
657 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
658 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
659 SH_MFHCls(hookname)::ms_MFI.vtblindex];
661 #elif SH_COMP == SH_COMP_GCC
663 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
666 SH_MFHCls(hookname)::ECMFP mfp; \
673 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
674 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
675 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
680 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
682 SET_META_RESULT(result); \
683 SH_GLOB_SHPTR->DoRecall(); \
684 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
685 (thisptr->*(u.mfp)) newparams; \
686 RETURN_META(MRES_SUPERCEDE); \
689 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
691 SET_META_RESULT(result); \
692 SH_GLOB_SHPTR->DoRecall(); \
693 if ((result) >= MRES_OVERRIDE) \
695 /* see RETURN_META_VALUE_NEWPARAMS */ \
696 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
698 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
699 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
702 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
704 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
705 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
706 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
707 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
710 // For source-level compatibility
712 #define SH_GET_CALLCLASS(ptr) SourceHook::GetCallClass(ptr)
713 #define SH_GET_MCALLCLASS(ptr, size) SourceHook::GetCallClass(reinterpret_cast<SourceHook::EmptyClass*>(ptr))
714 #define SH_RELEASE_CALLCLASS(ptr) SourceHook::ReleaseCallClass(ptr)
716 // New ADD / REMOVE macros.
717 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
718 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
720 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
721 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
724 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
725 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
728 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
729 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
731 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
732 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
734 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
735 __SourceHook_FHVPAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
736 post, handler, false)
738 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, vtableptr, handler, post) \
739 __SourceHook_FHVPAdd##ifacetype##ifacefunc(reinterpret_cast<void*>(vtableptr), \
742 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
743 __SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler, false)
745 #define SH_ADD_MANUALDVPHOOK(hookname, vtableptr, handler, post) \
746 __SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(vtableptr), post, handler, true)
748 #define SH_REMOVE_HOOK_ID(hookid) \
749 (SH_GLOB_SHPTR->RemoveHookByID(SH_GLOB_PLUGPTR, hookid))
752 // !! These are now deprecated. Instead, use one of these:
753 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
754 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
756 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
757 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
758 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
759 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
761 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
762 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
764 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
765 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
766 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
767 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
769 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
770 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
772 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
773 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
774 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
775 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
777 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
778 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
780 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
781 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
782 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
783 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
790 #if SH_COMP == SH_COMP_MSVC
791 # define SH_SETUP_MFP(mfp) \
792 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
793 #elif SH_COMP == SH_COMP_GCC
794 # define SH_SETUP_MFP(mfp) \
795 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
796 reinterpret_cast<void**>(&mfp)[1] = 0;
798 # error Not supported yet.
801 //////////////////////////////////////////////////////////////////////////
802 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
803 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
805 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
806 SH_FHCls(ifacetype,ifacefunc,overload)() \
808 GetFuncInfo(funcptr, ms_MFI); \
811 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
813 using namespace ::SourceHook; \
814 GetFuncInfo(funcptr, ms_MFI); \
815 /* Verify interface version */ \
816 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
818 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
821 if (action == HA_GetInfo) \
823 param->SetVersion(SH_HOOKMAN_VERSION); \
824 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
825 reinterpret_cast<const char*>(&ms_Proto)); \
827 MemFuncInfo mfi = {true, -1, 0, 0}; \
828 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
829 param->SetHookfuncVfnptr( \
830 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
833 else if (action == HA_Register) \
838 else if (action == HA_Unregister) \
847 // It has to be possible to use the macros in namespaces
848 // -> So we need to access and extend the global SourceHook namespace
849 // We use a namespace alias for this
850 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
851 struct SH_FHCls(ifacetype,ifacefunc,overload) \
853 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
854 static ::SourceHook::MemFuncInfo ms_MFI; \
855 static ::SourceHook::IHookManagerInfo *ms_HI; \
856 static ::SourceHook::ProtoInfo ms_Proto; \
857 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
859 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
861 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
862 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
863 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
864 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
865 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
867 using namespace ::SourceHook; \
868 MemFuncInfo mfi = {true, -1, 0, 0}; \
869 GetFuncInfo(funcptr, mfi); \
870 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
871 return false; /* No non-virtual functions / virtual inheritance supported */ \
873 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, mfi.thisptroffs, \
874 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
875 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
877 int __SourceHook_FHVPAdd##ifacetype##ifacefunc(void *iface, bool post, \
878 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler, bool direct) \
880 using namespace ::SourceHook; \
881 MemFuncInfo mfi = {true, -1, 0, 0}; \
882 GetFuncInfo(funcptr, mfi); \
883 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
884 return false; /* No non-virtual functions / virtual inheritance supported */ \
886 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
887 direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
888 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
889 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
891 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
892 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
894 using namespace ::SourceHook; \
895 MemFuncInfo mfi = {true, -1, 0, 0}; \
896 GetFuncInfo(funcptr, mfi); \
897 if (mfi.thisptroffs < 0) \
898 return false; /* No virtual inheritance supported */ \
900 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
901 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
902 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
905 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
906 struct SH_MFHCls(hookname) \
908 static SH_MFHCls(hookname) ms_Inst; \
909 static ::SourceHook::MemFuncInfo ms_MFI; \
910 static ::SourceHook::IHookManagerInfo *ms_HI; \
911 static ::SourceHook::ProtoInfo ms_Proto; \
913 SH_MFHCls(hookname)() \
915 ms_MFI.isVirtual = true; \
916 ms_MFI.thisptroffs = pthisptroffs; \
917 ms_MFI.vtblindex = pvtblidx; \
918 ms_MFI.vtbloffs = pvtbloffs; \
920 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
922 using namespace ::SourceHook; \
923 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
924 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
925 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
926 /* Verify interface version */ \
927 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
929 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
932 if (action == HA_GetInfo) \
934 param->SetVersion(SH_HOOKMAN_VERSION); \
935 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
936 reinterpret_cast<const char*>(&ms_Proto)); \
938 MemFuncInfo mfi = {true, -1, 0, 0}; \
939 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
940 param->SetHookfuncVfnptr( \
941 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
944 else if (action == HA_Register) \
949 else if (action == HA_Unregister) \
958 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
960 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
961 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
962 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
963 int __SourceHook_FHMAdd##hookname(void *iface, bool post, \
964 SH_MFHCls(hookname)::FD handler) \
966 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, pthisptroffs, \
967 SH_MFHCls(hookname)::HookManPubFunc, \
968 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
970 int __SourceHook_FHMVPAdd##hookname(void *iface, bool post, \
971 SH_MFHCls(hookname)::FD handler, bool direct) \
973 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
974 direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
975 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
976 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
978 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
979 SH_MFHCls(hookname)::FD handler) \
981 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
982 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
983 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
987 #define SH_SETUPCALLS(rettype, paramtypes, params) \
988 /* 1) Find the vfnptr */ \
989 using namespace ::SourceHook; \
990 void *ourvfnptr = reinterpret_cast<void*>( \
991 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
992 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
993 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
995 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
996 /* ... and the iface */ \
997 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
1000 /* The iface info was not found. Redirect the call to the original function. */ \
1001 rettype (EmptyClass::*mfp)paramtypes; \
1002 SH_SETUP_MFP(mfp); \
1003 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1005 /* 2) Declare some vars and set it up */ \
1006 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
1007 IHookList *prelist = ifinfo->GetPreHooks(); \
1008 IHookList *postlist = ifinfo->GetPostHooks(); \
1009 META_RES status = MRES_IGNORED; \
1010 META_RES prev_res; \
1012 typedef ReferenceCarrier<rettype>::type my_rettype; \
1013 my_rettype orig_ret; \
1014 my_rettype override_ret; \
1015 my_rettype plugin_ret; \
1017 my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
1018 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
1020 #define SH_CALL_HOOKS(post, params) \
1021 if (SH_GLOB_SHPTR->ShouldContinue()) \
1023 prev_res = MRES_IGNORED; \
1024 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
1026 cur_res = MRES_IGNORED; \
1027 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
1028 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
1029 prev_res = cur_res; \
1030 if (cur_res > status) \
1032 if (cur_res >= MRES_OVERRIDE) \
1033 *pOverrideRet = plugin_ret; \
1034 if (!SH_GLOB_SHPTR->ShouldContinue()) \
1042 #define SH_CALL_ORIG(rettype, paramtypes, params) \
1043 if (status != MRES_SUPERCEDE) \
1045 rettype (EmptyClass::*mfp)paramtypes; \
1046 SH_SETUP_MFP(mfp); \
1047 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1050 orig_ret = override_ret; \
1052 #define SH_RETURN() \
1053 SH_GLOB_SHPTR->HookLoopEnd(); \
1054 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
1056 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
1057 SH_SETUPCALLS(rettype, paramtypes, params) \
1058 SH_CALL_HOOKS(pre, params) \
1059 SH_CALL_ORIG(rettype, paramtypes, params) \
1060 SH_CALL_HOOKS(post, params) \
1063 //////////////////////////////////////////////////////////////////////////
1064 #define SH_SETUPCALLS_void(paramtypes, params) \
1065 /* 1) Find the vfnptr */ \
1066 using namespace ::SourceHook; \
1067 void *ourvfnptr = reinterpret_cast<void*>( \
1068 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
1069 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
1070 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
1072 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
1073 /* ... and the iface */ \
1074 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
1077 /* The iface info was not found. Redirect the call to the original function. */ \
1078 void (EmptyClass::*mfp)paramtypes; \
1079 SH_SETUP_MFP(mfp); \
1080 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1083 /* 2) Declare some vars and set it up */ \
1084 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
1085 IHookList *prelist = ifinfo->GetPreHooks(); \
1086 IHookList *postlist = ifinfo->GetPostHooks(); \
1087 META_RES status = MRES_IGNORED; \
1088 META_RES prev_res; \
1091 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
1093 #define SH_CALL_HOOKS_void(post, params) \
1094 if (SH_GLOB_SHPTR->ShouldContinue()) \
1096 prev_res = MRES_IGNORED; \
1097 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
1099 cur_res = MRES_IGNORED; \
1100 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
1101 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
1102 prev_res = cur_res; \
1103 if (cur_res > status) \
1105 if (!SH_GLOB_SHPTR->ShouldContinue()) \
1113 #define SH_CALL_ORIG_void(paramtypes, params) \
1114 if (status != MRES_SUPERCEDE) \
1116 void (EmptyClass::*mfp)paramtypes; \
1117 SH_SETUP_MFP(mfp); \
1118 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1121 #define SH_RETURN_void() \
1122 SH_GLOB_SHPTR->HookLoopEnd();
1124 #define SH_HANDLEFUNC_void(paramtypes, params) \
1125 SH_SETUPCALLS_void(paramtypes, params) \
1126 SH_CALL_HOOKS_void(pre, params) \
1127 SH_CALL_ORIG_void(paramtypes, params) \
1128 SH_CALL_HOOKS_void(post, params) \
1132 // Special vafmt handlers
1134 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
1135 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
1136 SH_CALL_HOOKS(pre, params_plug) \
1137 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
1138 SH_CALL_HOOKS(post, params_plug) \
1141 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
1142 SH_SETUPCALLS_void(paramtypes, params_orig) \
1143 SH_CALL_HOOKS_void(pre, params_plug) \
1144 SH_CALL_ORIG_void(paramtypes, params_orig) \
1145 SH_CALL_HOOKS_void(post, params_plug) \
1148 //////////////////////////////////////////////////////////////////////////
1151 // sizeof on references returns the size of the datatype, NOT the pointer size or something
1152 // -> one should probably flag references in __SourceHook_ParamSizes_* !
1153 // or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
1156 // ********* Support for $1 arguments *********
1157 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1158 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1159 (&ifacetype::ifacefunc))) \
1160 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1161 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1162 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1163 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1164 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1166 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1167 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1168 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1170 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1171 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1172 (&ifacetype::ifacefunc))) \
1173 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1174 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1175 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1176 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1177 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1180 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1181 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1182 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1184 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1185 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1186 (&ifacetype::ifacefunc))) \
1187 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1188 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1190 char buf[::SourceHook::STRBUF_LEN]; \
1192 va_start(ap, fmt); \
1193 vsnprintf(buf, sizeof(buf), fmt, ap); \
1195 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); \
1197 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1198 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1200 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1201 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1202 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1204 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1205 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1206 (&ifacetype::ifacefunc))) \
1207 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1208 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1210 char buf[::SourceHook::STRBUF_LEN]; \
1212 va_start(ap, fmt); \
1213 vsnprintf(buf, sizeof(buf), fmt, ap); \
1215 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)); \
1217 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1218 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1220 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1221 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1222 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1224 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1225 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1226 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1227 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1228 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1229 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1230 typedef rettype RetType; \
1231 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1233 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1234 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
1235 $1, __SourceHook_ParamSizesM_##hookname); \
1237 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1238 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1239 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1240 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1241 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1242 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1243 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1245 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1246 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
1247 $1, __SourceHook_ParamSizesM_##hookname); \
1252 //////////////////////////////////////////////////////////////////////////
1255 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1257 using namespace ::SourceHook; \
1259 m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
1260 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1261 m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
1265 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1267 using namespace ::SourceHook; \
1269 m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
1270 (m_ThisPtr->*m_MFP)call; \
1271 m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
1274 namespace SourceHook
1276 // Call Class Wrapper!
1277 template <class T> struct CCW
1281 // Get Real Pointer!
1282 static inline T *GRP(T *p)
1288 template <class T> struct CCW< CallClass<T> >
1292 // Get Real Pointer!
1293 static inline T *GRP(CallClass<T> *p)
1300 // Support for $1 arguments
1301 template<class ObjType, class MFP, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1309 ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
1310 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
1312 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1313 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1316 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1317 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1321 template<class ObjType, class MFP@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFP, void@[$2,1,$1:, Param$2@]>
1329 ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
1330 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
1332 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1333 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1336 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1337 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1344 #define SH__CALL_GET_VFNPTR_NORMAL \
1345 using namespace ::SourceHook; \
1346 MemFuncInfo mfi = {true, -1, 0, 0}; \
1347 GetFuncInfo(CCW<Y>::GRP(ptr), mfp, mfi); \
1348 void *vfnptr = reinterpret_cast<void*>( \
1349 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1351 #define SH__CALL_GET_VFNPTR_MANUAL \
1352 using namespace ::SourceHook; \
1353 void *vfnptr = reinterpret_cast<void*>( \
1354 *reinterpret_cast<void***>( (reinterpret_cast<char*>(CCW<Y>::GRP(ptr)) + thisptroffs + vtbloffs) ) + vtblidx); \
1356 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1358 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1359 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1360 // The only purpose of the mfp2 parameter is to extract the return type
1363 // Support for $1 arguments
1364 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1365 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1366 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1368 SH__CALL_GET_VFNPTR_NORMAL
1369 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1370 mfp, vfnptr, shptr, plug);
1373 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1374 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1375 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1377 SH__CALL_GET_VFNPTR_NORMAL
1378 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1379 mfp, vfnptr, shptr, plug);
1382 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1383 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1384 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1386 SH__CALL_GET_VFNPTR_MANUAL
1387 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1388 reinterpret_cast<SourceHook::EmptyClass*>(CCW<Y>::GRP(ptr)), mfp, vfnptr, shptr, plug);
1392 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1393 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1396 // Support for $1 arguments
1397 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1398 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1399 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1401 SH__CALL_GET_VFNPTR_NORMAL
1402 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1403 mfp, vfnptr, shptr, plug);
1406 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1407 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1408 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1410 SH__CALL_GET_VFNPTR_NORMAL
1411 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1412 mfp, vfnptr, shptr, plug);
1419 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
1420 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
1421 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1422 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1424 #undef SH_MAKE_EXECUTABLECLASS_OB
1426 //////////////////////////////////////////////////////////////////////////
1427 // SetOverrideRet and RecallGetIface for recalls
1428 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1429 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1430 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1432 namespace SourceHook
1434 template <class RetType>
1435 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1437 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1440 // SetOverrideResult used to be implemented like this:
1441 // SetOverrideResult(shptr, memfuncptr, return);
1442 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1443 // (it thought it was ambigous - the ref and non-ref type)
1444 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1445 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1446 // This also allows us to create a special version for references which respects ReferenceCarrier.
1448 template <class T> struct OverrideFunctor
1450 void operator()(ISourceHook *shptr, T res)
1452 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1455 template <class T> struct OverrideFunctor<T&>
1457 void operator()(ISourceHook *shptr, T &res)
1459 // overrideretptr points to ReferenceCarrier<T&>
1460 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1464 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1465 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1467 return OverrideFunctor<RetType>();
1470 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1471 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1473 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1479 // The pope is dead. -> :(