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 * @brief Finds the original entry of a virtual function pointer
541 * @param vfnptr The virtual function pointer
542 * @return The original entry if the virtual function pointer has been patched; NULL otherwise.
544 virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
547 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
548 // These have to be able to return references. If T is a reference, the pointers returned
549 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
550 template <class T> struct MacroRefHelpers
552 inline static const T* GetOrigRet(ISourceHook *shptr)
554 return reinterpret_cast<const T*>(shptr->GetOrigRet());
556 inline static const T* GetOverrideRet(ISourceHook *shptr)
558 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
562 template <class T> struct MacroRefHelpers<T&>
564 inline static T* GetOrigRet(ISourceHook *shptr)
566 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
569 inline static T* GetOverrideRet(ISourceHook *shptr)
571 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
576 // For source-level compatibility
577 template <class T> struct CallClass
581 CallClass(T *p) : ptr(p)
591 typedef CallClass<void> GenericCallClass;
592 typedef CallClass<EmptyClass> ManualCallClass;
595 CallClass<T> *GetCallClass(T *p)
597 return new CallClass<T>(p);
601 void ReleaseCallClass(CallClass<T> *p)
606 template <class X, class MFP>
607 void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
609 SourceHook::MemFuncInfo info = {true, -1, 0, 0};
610 SourceHook::GetFuncInfo(pInstance, mfp, info);
612 void *vfnptr = reinterpret_cast<void*>(
613 *reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
615 void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
617 return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
621 /************************************************************************/
622 /* High level interface */
623 /************************************************************************/
624 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
625 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
626 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
627 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
628 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
630 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
631 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
632 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
634 // If a hook on a function which returns a reference does not want to specify a return value,
635 // it can use this macro.
636 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
637 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
639 // only call these from the hook handlers directly!
640 // :TODO: enforce it ?
642 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
643 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
645 // SourceHook::SetOverrideRet is defined later.
646 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
648 SET_META_RESULT(result); \
649 SH_GLOB_SHPTR->DoRecall(); \
650 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
651 RETURN_META(MRES_SUPERCEDE); \
654 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
656 SET_META_RESULT(result); \
657 SH_GLOB_SHPTR->DoRecall(); \
658 if ((result) >= MRES_OVERRIDE) \
660 /* meh, set the override result here because we don't get a chance to return */ \
661 /* before continuing the hook loop through the recall */ \
662 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
664 RETURN_META_VALUE(MRES_SUPERCEDE, \
665 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
668 // :TODO: thisptroffs in MNEWPARAMS ??
670 #if SH_COMP == SH_COMP_MSVC
672 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
675 SH_MFHCls(hookname)::ECMFP mfp; \
678 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
679 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
680 SH_MFHCls(hookname)::ms_MFI.vtblindex];
682 #elif SH_COMP == SH_COMP_GCC
684 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
687 SH_MFHCls(hookname)::ECMFP mfp; \
694 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
695 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
696 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
701 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
703 SET_META_RESULT(result); \
704 SH_GLOB_SHPTR->DoRecall(); \
705 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
706 (thisptr->*(u.mfp)) newparams; \
707 RETURN_META(MRES_SUPERCEDE); \
710 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
712 SET_META_RESULT(result); \
713 SH_GLOB_SHPTR->DoRecall(); \
714 if ((result) >= MRES_OVERRIDE) \
716 /* see RETURN_META_VALUE_NEWPARAMS */ \
717 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
719 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
720 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
723 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
725 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
726 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
727 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
728 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
732 #define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
734 // For source-level compatibility
736 #define SH_GET_CALLCLASS(ptr) SourceHook::GetCallClass(ptr)
737 #define SH_GET_MCALLCLASS(ptr, size) SourceHook::GetCallClass(reinterpret_cast<SourceHook::EmptyClass*>(ptr))
738 #define SH_RELEASE_CALLCLASS(ptr) SourceHook::ReleaseCallClass(ptr)
740 // New ADD / REMOVE macros.
741 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
742 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
744 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
745 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
748 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
749 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
752 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
753 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
755 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
756 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
758 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
759 __SourceHook_FHVPAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
760 post, handler, false)
762 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, vtableptr, handler, post) \
763 __SourceHook_FHVPAdd##ifacetype##ifacefunc(reinterpret_cast<void*>(vtableptr), \
766 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
767 __SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler, false)
769 #define SH_ADD_MANUALDVPHOOK(hookname, vtableptr, handler, post) \
770 __SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(vtableptr), post, handler, true)
772 #define SH_REMOVE_HOOK_ID(hookid) \
773 (SH_GLOB_SHPTR->RemoveHookByID(SH_GLOB_PLUGPTR, hookid))
776 // !! These are now deprecated. Instead, use one of these:
777 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
778 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
780 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
781 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
782 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
783 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
785 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
786 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
788 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
789 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
790 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
791 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
793 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
794 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
796 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
797 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
798 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
799 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
801 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
802 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
804 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
805 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
806 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
807 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
814 #if SH_COMP == SH_COMP_MSVC
815 # define SH_SETUP_MFP(mfp) \
816 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
817 #elif SH_COMP == SH_COMP_GCC
818 # define SH_SETUP_MFP(mfp) \
819 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
820 reinterpret_cast<void**>(&mfp)[1] = 0;
822 # error Not supported yet.
825 //////////////////////////////////////////////////////////////////////////
826 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
827 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
829 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
830 SH_FHCls(ifacetype,ifacefunc,overload)() \
832 GetFuncInfo(funcptr, ms_MFI); \
835 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
837 using namespace ::SourceHook; \
838 GetFuncInfo(funcptr, ms_MFI); \
839 /* Verify interface version */ \
840 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
842 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
845 if (action == HA_GetInfo) \
847 param->SetVersion(SH_HOOKMAN_VERSION); \
848 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
849 reinterpret_cast<const char*>(&ms_Proto)); \
851 MemFuncInfo mfi = {true, -1, 0, 0}; \
852 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
853 param->SetHookfuncVfnptr( \
854 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
857 else if (action == HA_Register) \
862 else if (action == HA_Unregister) \
871 // It has to be possible to use the macros in namespaces
872 // -> So we need to access and extend the global SourceHook namespace
873 // We use a namespace alias for this
874 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
875 struct SH_FHCls(ifacetype,ifacefunc,overload) \
877 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
878 static ::SourceHook::MemFuncInfo ms_MFI; \
879 static ::SourceHook::IHookManagerInfo *ms_HI; \
880 static ::SourceHook::ProtoInfo ms_Proto; \
881 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
883 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
885 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
886 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
887 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
888 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
889 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
891 using namespace ::SourceHook; \
892 MemFuncInfo mfi = {true, -1, 0, 0}; \
893 GetFuncInfo(funcptr, mfi); \
894 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
895 return false; /* No non-virtual functions / virtual inheritance supported */ \
897 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, mfi.thisptroffs, \
898 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
899 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
901 int __SourceHook_FHVPAdd##ifacetype##ifacefunc(void *iface, bool post, \
902 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler, bool direct) \
904 using namespace ::SourceHook; \
905 MemFuncInfo mfi = {true, -1, 0, 0}; \
906 GetFuncInfo(funcptr, mfi); \
907 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
908 return false; /* No non-virtual functions / virtual inheritance supported */ \
910 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
911 direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
912 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
913 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
915 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
916 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
918 using namespace ::SourceHook; \
919 MemFuncInfo mfi = {true, -1, 0, 0}; \
920 GetFuncInfo(funcptr, mfi); \
921 if (mfi.thisptroffs < 0) \
922 return false; /* No virtual inheritance supported */ \
924 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
925 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
926 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
929 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
930 struct SH_MFHCls(hookname) \
932 static SH_MFHCls(hookname) ms_Inst; \
933 static ::SourceHook::MemFuncInfo ms_MFI; \
934 static ::SourceHook::IHookManagerInfo *ms_HI; \
935 static ::SourceHook::ProtoInfo ms_Proto; \
937 SH_MFHCls(hookname)() \
939 ms_MFI.isVirtual = true; \
940 ms_MFI.thisptroffs = pthisptroffs; \
941 ms_MFI.vtblindex = pvtblidx; \
942 ms_MFI.vtbloffs = pvtbloffs; \
944 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
946 using namespace ::SourceHook; \
947 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
948 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
949 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
950 /* Verify interface version */ \
951 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
953 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
956 if (action == HA_GetInfo) \
958 param->SetVersion(SH_HOOKMAN_VERSION); \
959 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
960 reinterpret_cast<const char*>(&ms_Proto)); \
962 MemFuncInfo mfi = {true, -1, 0, 0}; \
963 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
964 param->SetHookfuncVfnptr( \
965 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
968 else if (action == HA_Register) \
973 else if (action == HA_Unregister) \
982 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
984 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
985 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
986 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
987 int __SourceHook_FHMAdd##hookname(void *iface, bool post, \
988 SH_MFHCls(hookname)::FD handler) \
990 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, pthisptroffs, \
991 SH_MFHCls(hookname)::HookManPubFunc, \
992 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
994 int __SourceHook_FHMVPAdd##hookname(void *iface, bool post, \
995 SH_MFHCls(hookname)::FD handler, bool direct) \
997 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
998 direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
999 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
1000 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
1002 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
1003 SH_MFHCls(hookname)::FD handler) \
1005 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
1006 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
1007 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
1011 #define SH_SETUPCALLS(rettype, paramtypes, params) \
1012 /* 1) Find the vfnptr */ \
1013 using namespace ::SourceHook; \
1014 void *ourvfnptr = reinterpret_cast<void*>( \
1015 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
1016 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
1017 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
1019 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
1020 /* ... and the iface */ \
1021 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
1024 /* The iface info was not found. Redirect the call to the original function. */ \
1025 rettype (EmptyClass::*mfp)paramtypes; \
1026 SH_SETUP_MFP(mfp); \
1027 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1029 /* 2) Declare some vars and set it up */ \
1030 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
1031 IHookList *prelist = ifinfo->GetPreHooks(); \
1032 IHookList *postlist = ifinfo->GetPostHooks(); \
1033 META_RES status = MRES_IGNORED; \
1034 META_RES prev_res; \
1036 typedef ReferenceCarrier<rettype>::type my_rettype; \
1037 my_rettype orig_ret; \
1038 my_rettype override_ret; \
1039 my_rettype plugin_ret; \
1041 my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
1042 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
1044 #define SH_CALL_HOOKS(post, params) \
1045 if (SH_GLOB_SHPTR->ShouldContinue()) \
1047 prev_res = MRES_IGNORED; \
1048 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
1050 cur_res = MRES_IGNORED; \
1051 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
1052 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
1053 prev_res = cur_res; \
1054 if (cur_res > status) \
1056 if (cur_res >= MRES_OVERRIDE) \
1057 *pOverrideRet = plugin_ret; \
1058 if (!SH_GLOB_SHPTR->ShouldContinue()) \
1066 #define SH_CALL_ORIG(rettype, paramtypes, params) \
1067 if (status != MRES_SUPERCEDE) \
1069 rettype (EmptyClass::*mfp)paramtypes; \
1070 SH_SETUP_MFP(mfp); \
1071 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1074 orig_ret = override_ret; \
1076 #define SH_RETURN() \
1077 SH_GLOB_SHPTR->HookLoopEnd(); \
1078 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
1080 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
1081 SH_SETUPCALLS(rettype, paramtypes, params) \
1082 SH_CALL_HOOKS(pre, params) \
1083 SH_CALL_ORIG(rettype, paramtypes, params) \
1084 SH_CALL_HOOKS(post, params) \
1087 //////////////////////////////////////////////////////////////////////////
1088 #define SH_SETUPCALLS_void(paramtypes, params) \
1089 /* 1) Find the vfnptr */ \
1090 using namespace ::SourceHook; \
1091 void *ourvfnptr = reinterpret_cast<void*>( \
1092 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
1093 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
1094 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
1096 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
1097 /* ... and the iface */ \
1098 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
1101 /* The iface info was not found. Redirect the call to the original function. */ \
1102 void (EmptyClass::*mfp)paramtypes; \
1103 SH_SETUP_MFP(mfp); \
1104 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1107 /* 2) Declare some vars and set it up */ \
1108 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
1109 IHookList *prelist = ifinfo->GetPreHooks(); \
1110 IHookList *postlist = ifinfo->GetPostHooks(); \
1111 META_RES status = MRES_IGNORED; \
1112 META_RES prev_res; \
1115 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
1117 #define SH_CALL_HOOKS_void(post, params) \
1118 if (SH_GLOB_SHPTR->ShouldContinue()) \
1120 prev_res = MRES_IGNORED; \
1121 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
1123 cur_res = MRES_IGNORED; \
1124 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
1125 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
1126 prev_res = cur_res; \
1127 if (cur_res > status) \
1129 if (!SH_GLOB_SHPTR->ShouldContinue()) \
1137 #define SH_CALL_ORIG_void(paramtypes, params) \
1138 if (status != MRES_SUPERCEDE) \
1140 void (EmptyClass::*mfp)paramtypes; \
1141 SH_SETUP_MFP(mfp); \
1142 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1145 #define SH_RETURN_void() \
1146 SH_GLOB_SHPTR->HookLoopEnd();
1148 #define SH_HANDLEFUNC_void(paramtypes, params) \
1149 SH_SETUPCALLS_void(paramtypes, params) \
1150 SH_CALL_HOOKS_void(pre, params) \
1151 SH_CALL_ORIG_void(paramtypes, params) \
1152 SH_CALL_HOOKS_void(post, params) \
1156 // Special vafmt handlers
1158 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
1159 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
1160 SH_CALL_HOOKS(pre, params_plug) \
1161 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
1162 SH_CALL_HOOKS(post, params_plug) \
1165 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
1166 SH_SETUPCALLS_void(paramtypes, params_orig) \
1167 SH_CALL_HOOKS_void(pre, params_plug) \
1168 SH_CALL_ORIG_void(paramtypes, params_orig) \
1169 SH_CALL_HOOKS_void(post, params_plug) \
1172 //////////////////////////////////////////////////////////////////////////
1175 // sizeof on references returns the size of the datatype, NOT the pointer size or something
1176 // -> one should probably flag references in __SourceHook_ParamSizes_* !
1177 // or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
1180 // ********* Support for $1 arguments *********
1181 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1182 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1183 (&ifacetype::ifacefunc))) \
1184 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1185 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1186 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1187 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1188 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1190 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1191 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1192 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1194 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1195 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1196 (&ifacetype::ifacefunc))) \
1197 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1198 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1199 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1200 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1201 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1204 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1205 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1206 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1208 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1209 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1210 (&ifacetype::ifacefunc))) \
1211 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1212 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1214 char buf[::SourceHook::STRBUF_LEN]; \
1216 va_start(ap, fmt); \
1217 vsnprintf(buf, sizeof(buf), fmt, ap); \
1219 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); \
1221 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1222 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1224 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1225 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1226 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1228 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1229 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1230 (&ifacetype::ifacefunc))) \
1231 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1232 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1234 char buf[::SourceHook::STRBUF_LEN]; \
1236 va_start(ap, fmt); \
1237 vsnprintf(buf, sizeof(buf), fmt, ap); \
1239 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)); \
1241 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1242 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1244 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1245 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1246 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1248 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1249 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1250 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1251 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1252 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1253 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1254 typedef rettype RetType; \
1255 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1257 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1258 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
1259 $1, __SourceHook_ParamSizesM_##hookname); \
1261 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1262 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1263 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1264 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1265 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1266 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1267 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1269 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1270 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
1271 $1, __SourceHook_ParamSizesM_##hookname); \
1276 //////////////////////////////////////////////////////////////////////////
1279 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1281 using namespace ::SourceHook; \
1283 m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
1284 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1285 m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
1289 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1291 using namespace ::SourceHook; \
1293 m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
1294 (m_ThisPtr->*m_MFP)call; \
1295 m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
1298 namespace SourceHook
1300 // Call Class Wrapper!
1301 template <class T> struct CCW
1305 // Get Real Pointer!
1306 static inline T *GRP(T *p)
1312 template <class T> struct CCW< CallClass<T> >
1316 // Get Real Pointer!
1317 static inline T *GRP(CallClass<T> *p)
1324 // Support for $1 arguments
1325 template<class ObjType, class MFP, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1333 ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
1334 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
1336 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1337 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1340 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1341 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1345 template<class ObjType, class MFP@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFP, void@[$2,1,$1:, Param$2@]>
1353 ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
1354 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
1356 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1357 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1360 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1361 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1368 #define SH__CALL_GET_VFNPTR_NORMAL \
1369 using namespace ::SourceHook; \
1370 MemFuncInfo mfi = {true, -1, 0, 0}; \
1371 GetFuncInfo(CCW<Y>::GRP(ptr), mfp, mfi); \
1372 void *vfnptr = reinterpret_cast<void*>( \
1373 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1375 #define SH__CALL_GET_VFNPTR_MANUAL \
1376 using namespace ::SourceHook; \
1377 void *vfnptr = reinterpret_cast<void*>( \
1378 *reinterpret_cast<void***>( (reinterpret_cast<char*>(CCW<Y>::GRP(ptr)) + thisptroffs + vtbloffs) ) + vtblidx); \
1380 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1382 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1383 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1384 // The only purpose of the mfp2 parameter is to extract the return type
1387 // Support for $1 arguments
1388 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1389 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1390 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1392 SH__CALL_GET_VFNPTR_NORMAL
1393 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1394 mfp, vfnptr, shptr, plug);
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@])const, 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<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1408 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)
1410 SH__CALL_GET_VFNPTR_MANUAL
1411 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1412 reinterpret_cast<SourceHook::EmptyClass*>(CCW<Y>::GRP(ptr)), mfp, vfnptr, shptr, plug);
1416 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1417 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1420 // Support for $1 arguments
1421 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1422 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1423 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1425 SH__CALL_GET_VFNPTR_NORMAL
1426 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1427 mfp, vfnptr, shptr, plug);
1430 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1431 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1432 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1434 SH__CALL_GET_VFNPTR_NORMAL
1435 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1436 mfp, vfnptr, shptr, plug);
1443 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
1444 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
1445 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1446 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1448 #undef SH_MAKE_EXECUTABLECLASS_OB
1450 //////////////////////////////////////////////////////////////////////////
1451 // SetOverrideRet and RecallGetIface for recalls
1452 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1453 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1454 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1456 namespace SourceHook
1458 template <class RetType>
1459 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1461 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1464 // SetOverrideResult used to be implemented like this:
1465 // SetOverrideResult(shptr, memfuncptr, return);
1466 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1467 // (it thought it was ambigous - the ref and non-ref type)
1468 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1469 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1470 // This also allows us to create a special version for references which respects ReferenceCarrier.
1472 template <class T> struct OverrideFunctor
1474 void operator()(ISourceHook *shptr, T res)
1476 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1479 template <class T> struct OverrideFunctor<T&>
1481 void operator()(ISourceHook *shptr, T &res)
1483 // overrideretptr points to ReferenceCarrier<T&>
1484 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1488 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1489 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1491 return OverrideFunctor<RetType>();
1494 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1495 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1497 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1503 // The pope is dead. -> :(