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 CallClass
303 virtual B *GetThisPtr() = 0;
304 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
307 typedef CallClass<void> GenericCallClass;
308 typedef CallClass<EmptyClass> ManualCallClass;
310 // 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
311 // - Today is an important day.
312 // I'm adding support for functions which return references.
315 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
316 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
317 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
318 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
319 // special cases were needed for getoverrideret / getorigret; these are implemented through the
320 // SourceHook::MacroRefHelpers structs.
321 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
322 template <class T> struct ReferenceCarrier
327 template <class T> struct ReferenceCarrier<T&>
333 type() : m_StoredRef(NULL)
336 type(T& ref) : m_StoredRef(&ref)
340 T& operator= (T& ref)
354 * @brief The main SourceHook interface
360 * @brief Return interface version
362 virtual int GetIfaceVersion() = 0;
365 * @brief Return implementation version
367 virtual int GetImplVersion() = 0;
372 * @return True if the function succeeded, false otherwise
374 * @param plug The unique identifier of the plugin that calls this function
375 * @param iface The interface pointer
376 * @param ifacesize The size of the class iface points to
377 * @param myHookMan A hook manager function that should be capable of handling the function
378 * @param handler A pointer to a FastDelegate containing the hook handler
379 * @param post Set to true if you want a post handler
381 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
382 ISHDelegate *handler, bool post) = 0;
385 * @brief Removes a hook.
387 * @return True if the function succeeded, false otherwise
389 * @param plug The unique identifier of the plugin that calls this function
390 * @param iface The interface pointer
391 * @param myHookMan A hook manager function that should be capable of handling the function
392 * @param handler A pointer to a FastDelegate containing the hook handler
393 * @param post Set to true if you want a post handler
395 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
396 ISHDelegate *handler, bool post) = 0;
399 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
401 * @param plug The unique identifier of the plugin in question
403 virtual bool IsPluginInUse(Plugin plug) = 0;
406 * @brief Return a pointer to a callclass. Generate a new one if required.
408 * @param iface The interface pointer
409 * @param size Size of the class instance
411 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
414 * @brief Release a callclass
416 * @param ptr Pointer to the callclass
418 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
420 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
421 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
422 //!< previously calledhandler
423 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
424 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
425 //!< If not in post function, undefined
426 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
427 //!< If none is specified, NULL
428 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
429 //////////////////////////////////////////////////////////////////////////
431 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
432 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
433 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
434 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
435 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
436 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
437 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
438 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
439 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
442 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
444 * @param plug The owner of the hook manager
445 * @param pubFunc The hook manager's info function
447 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
449 virtual void DoRecall() = 0; //!< Initiates a recall sequence
454 Users want the ability to change parameters of the called function
455 from inside their handler.
458 2) Recall the function
459 3) => SH's hook func gets called:
460 4) The first iterator points at the first hook the last hookfunc didn't execute yet
461 5) does all iteration and returns normally
462 6) The user's handler returns immediately
463 7) The hook func returns immediately as well
465 Also note that the recalled hookfunc starts with the status the recalling hookfunc
466 ended with. The last handler (doing the recall) is also able to specify its own
470 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
473 * @brief Set up the hook loop. Equivalent to calling:
474 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
476 * @param statusPtr pointer to status variable
477 * @param prevResPtr pointer to previous result variable
478 * @param curResPtr pointer to current result variable
479 * @param ifacePtrPtr pointer to interface this pointer variable
480 * @param origRetPr pointer to original return value variable. NULL for void funcs
481 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
483 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
484 * when the hook func is being called as part of a recall
486 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
487 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
490 * @brief Modes for the new AddHook
500 * @brief Add a (VP) hook.
502 * @return non-zero hook id on success, 0 otherwise
504 * @param plug The unique identifier of the plugin that calls this function
505 * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
506 * @param iface The interface pointer
507 * @param ifacesize The size of the class iface points to
508 * @param myHookMan A hook manager function that should be capable of handling the function
509 * @param handler A pointer to a FastDelegate containing the hook handler
510 * @param post Set to true if you want a post handler
512 virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
513 ISHDelegate *handler, bool post) = 0;
516 * @brief Remove a VP hook by ID.
518 * @return true on success, false otherwise
520 * @param plug The unique identifier of the plugin that calls this function
521 * @param hookid The hook id (returned by AddHookNew)
523 virtual bool RemoveHookByID(Plugin plug, int hookid) = 0;
526 * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
528 * @param plug The unique identifier of the plugin that calls this function
529 * @param vfnptr The virtual function pointer of the function in question
531 virtual void SetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
534 * @brief Reverses SetIgnoreHooks' effect
536 * @param plug The unique identifier of the plugin that calls this function
537 * @param vfnptr The virtual function pointer of the function in question
539 virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
542 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
543 // These have to be able to return references. If T is a reference, the pointers returned
544 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
545 template <class T> struct MacroRefHelpers
547 inline static const T* GetOrigRet(ISourceHook *shptr)
549 return reinterpret_cast<const T*>(shptr->GetOrigRet());
551 inline static const T* GetOverrideRet(ISourceHook *shptr)
553 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
557 template <class T> struct MacroRefHelpers<T&>
559 inline static T* GetOrigRet(ISourceHook *shptr)
561 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
564 inline static T* GetOverrideRet(ISourceHook *shptr)
566 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
571 // For source-level compatibility
572 template <class T> struct LegacyCallClass
576 LegacyCallClass(T *p) : ptr(p)
587 LegacyCallClass<T> *GetLegacyCallClass(T *p)
589 return new LegacyCallClass<T>(p);
593 void ReleaseLegacyCallClass(LegacyCallClass<T> *p)
599 /************************************************************************/
600 /* High level interface */
601 /************************************************************************/
602 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
603 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
604 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
605 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
606 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
608 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
609 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
610 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
612 // If a hook on a function which returns a reference does not want to specify a return value,
613 // it can use this macro.
614 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
615 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
617 // only call these from the hook handlers directly!
618 // :TODO: enforce it ?
620 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
621 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
623 // SourceHook::SetOverrideRet is defined later.
624 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
626 SET_META_RESULT(result); \
627 SH_GLOB_SHPTR->DoRecall(); \
628 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
629 RETURN_META(MRES_SUPERCEDE); \
632 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
634 SET_META_RESULT(result); \
635 SH_GLOB_SHPTR->DoRecall(); \
636 if ((result) >= MRES_OVERRIDE) \
638 /* meh, set the override result here because we don't get a chance to return */ \
639 /* before continuing the hook loop through the recall */ \
640 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
642 RETURN_META_VALUE(MRES_SUPERCEDE, \
643 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
646 // :TODO: thisptroffs in MNEWPARAMS ??
648 #if SH_COMP == SH_COMP_MSVC
650 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
653 SH_MFHCls(hookname)::ECMFP mfp; \
656 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
657 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
658 SH_MFHCls(hookname)::ms_MFI.vtblindex];
660 #elif SH_COMP == SH_COMP_GCC
662 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
665 SH_MFHCls(hookname)::ECMFP mfp; \
672 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
673 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
674 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
679 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
681 SET_META_RESULT(result); \
682 SH_GLOB_SHPTR->DoRecall(); \
683 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
684 (thisptr->*(u.mfp)) newparams; \
685 RETURN_META(MRES_SUPERCEDE); \
688 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
690 SET_META_RESULT(result); \
691 SH_GLOB_SHPTR->DoRecall(); \
692 if ((result) >= MRES_OVERRIDE) \
694 /* see RETURN_META_VALUE_NEWPARAMS */ \
695 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
697 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
698 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
701 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
703 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
704 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
705 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
706 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
709 // For source-level compatibility
710 #define CallClass LegacyCallClass
711 #define ManualCallClass LegacyCallClass<SourceHook::EmptyClass>
713 #define SH_GET_CALLCLASS(ptr) SourceHook::GetLegacyCallClass(ptr)
714 #define SH_GET_MCALLCLASS(ptr, size) SourceHook::GetLegacyCallClass(reinterpret_cast<SourceHook::EmptyClass*>(ptr))
715 #define SH_RELEASE_CALLCLASS(ptr) SourceHook::ReleaseLegacyCallClass(ptr)
717 // New ADD / REMOVE macros.
718 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
719 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
721 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
722 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
725 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
726 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
729 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
730 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
732 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
733 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
735 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
736 __SourceHook_FHVPAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
737 post, handler, false)
739 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, vtableptr, handler, post) \
740 __SourceHook_FHVPAdd##ifacetype##ifacefunc(reinterpret_cast<void*>(vtableptr), \
743 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
744 __SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler, false)
746 #define SH_ADD_MANUALDVPHOOK(hookname, vtableptr, handler, post) \
747 __SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(vtableptr), post, handler, true)
749 #define SH_REMOVE_HOOK_ID(hookid) \
750 (SH_GLOB_SHPTR->RemoveHookByID(SH_GLOB_PLUGPTR, hookid))
753 // !! These are now deprecated. Instead, use one of these:
754 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
755 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
757 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
758 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
759 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
760 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
762 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
763 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
765 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
766 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
767 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
768 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
770 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
771 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
773 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
774 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
775 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
776 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
778 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
779 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
781 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
782 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
783 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
784 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
791 #if SH_COMP == SH_COMP_MSVC
792 # define SH_SETUP_MFP(mfp) \
793 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
794 #elif SH_COMP == SH_COMP_GCC
795 # define SH_SETUP_MFP(mfp) \
796 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
797 reinterpret_cast<void**>(&mfp)[1] = 0;
799 # error Not supported yet.
802 //////////////////////////////////////////////////////////////////////////
803 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
804 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
806 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
807 SH_FHCls(ifacetype,ifacefunc,overload)() \
809 GetFuncInfo(funcptr, ms_MFI); \
812 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
814 using namespace ::SourceHook; \
815 GetFuncInfo(funcptr, ms_MFI); \
816 /* Verify interface version */ \
817 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
819 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
822 if (action == HA_GetInfo) \
824 param->SetVersion(SH_HOOKMAN_VERSION); \
825 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
826 reinterpret_cast<const char*>(&ms_Proto)); \
828 MemFuncInfo mfi = {true, -1, 0, 0}; \
829 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
830 param->SetHookfuncVfnptr( \
831 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
834 else if (action == HA_Register) \
839 else if (action == HA_Unregister) \
848 // It has to be possible to use the macros in namespaces
849 // -> So we need to access and extend the global SourceHook namespace
850 // We use a namespace alias for this
851 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
852 struct SH_FHCls(ifacetype,ifacefunc,overload) \
854 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
855 static ::SourceHook::MemFuncInfo ms_MFI; \
856 static ::SourceHook::IHookManagerInfo *ms_HI; \
857 static ::SourceHook::ProtoInfo ms_Proto; \
858 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
860 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
862 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
863 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
864 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
865 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
866 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
868 using namespace ::SourceHook; \
869 MemFuncInfo mfi = {true, -1, 0, 0}; \
870 GetFuncInfo(funcptr, mfi); \
871 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
872 return false; /* No non-virtual functions / virtual inheritance supported */ \
874 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, mfi.thisptroffs, \
875 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
876 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
878 int __SourceHook_FHVPAdd##ifacetype##ifacefunc(void *iface, bool post, \
879 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler, bool direct) \
881 using namespace ::SourceHook; \
882 MemFuncInfo mfi = {true, -1, 0, 0}; \
883 GetFuncInfo(funcptr, mfi); \
884 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
885 return false; /* No non-virtual functions / virtual inheritance supported */ \
887 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
888 direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
889 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
890 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
892 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
893 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
895 using namespace ::SourceHook; \
896 MemFuncInfo mfi = {true, -1, 0, 0}; \
897 GetFuncInfo(funcptr, mfi); \
898 if (mfi.thisptroffs < 0) \
899 return false; /* No virtual inheritance supported */ \
901 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
902 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
903 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
906 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
907 struct SH_MFHCls(hookname) \
909 static SH_MFHCls(hookname) ms_Inst; \
910 static ::SourceHook::MemFuncInfo ms_MFI; \
911 static ::SourceHook::IHookManagerInfo *ms_HI; \
912 static ::SourceHook::ProtoInfo ms_Proto; \
914 SH_MFHCls(hookname)() \
916 ms_MFI.isVirtual = true; \
917 ms_MFI.thisptroffs = pthisptroffs; \
918 ms_MFI.vtblindex = pvtblidx; \
919 ms_MFI.vtbloffs = pvtbloffs; \
921 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
923 using namespace ::SourceHook; \
924 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
925 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
926 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
927 /* Verify interface version */ \
928 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
930 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
933 if (action == HA_GetInfo) \
935 param->SetVersion(SH_HOOKMAN_VERSION); \
936 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
937 reinterpret_cast<const char*>(&ms_Proto)); \
939 MemFuncInfo mfi = {true, -1, 0, 0}; \
940 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
941 param->SetHookfuncVfnptr( \
942 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
945 else if (action == HA_Register) \
950 else if (action == HA_Unregister) \
959 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
961 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
962 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
963 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
964 int __SourceHook_FHMAdd##hookname(void *iface, bool post, \
965 SH_MFHCls(hookname)::FD handler) \
967 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, pthisptroffs, \
968 SH_MFHCls(hookname)::HookManPubFunc, \
969 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
971 int __SourceHook_FHMVPAdd##hookname(void *iface, bool post, \
972 SH_MFHCls(hookname)::FD handler, bool direct) \
974 return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
975 direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
976 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
977 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
979 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
980 SH_MFHCls(hookname)::FD handler) \
982 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
983 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
984 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
988 #define SH_SETUPCALLS(rettype, paramtypes, params) \
989 /* 1) Find the vfnptr */ \
990 using namespace ::SourceHook; \
991 void *ourvfnptr = reinterpret_cast<void*>( \
992 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
993 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
994 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
996 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
997 /* ... and the iface */ \
998 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
1001 /* The iface info was not found. Redirect the call to the original function. */ \
1002 rettype (EmptyClass::*mfp)paramtypes; \
1003 SH_SETUP_MFP(mfp); \
1004 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1006 /* 2) Declare some vars and set it up */ \
1007 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
1008 IHookList *prelist = ifinfo->GetPreHooks(); \
1009 IHookList *postlist = ifinfo->GetPostHooks(); \
1010 META_RES status = MRES_IGNORED; \
1011 META_RES prev_res; \
1013 typedef ReferenceCarrier<rettype>::type my_rettype; \
1014 my_rettype orig_ret; \
1015 my_rettype override_ret; \
1016 my_rettype plugin_ret; \
1018 my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
1019 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
1021 #define SH_CALL_HOOKS(post, params) \
1022 if (SH_GLOB_SHPTR->ShouldContinue()) \
1024 prev_res = MRES_IGNORED; \
1025 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
1027 cur_res = MRES_IGNORED; \
1028 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
1029 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
1030 prev_res = cur_res; \
1031 if (cur_res > status) \
1033 if (cur_res >= MRES_OVERRIDE) \
1034 *pOverrideRet = plugin_ret; \
1035 if (!SH_GLOB_SHPTR->ShouldContinue()) \
1043 #define SH_CALL_ORIG(rettype, paramtypes, params) \
1044 if (status != MRES_SUPERCEDE) \
1046 rettype (EmptyClass::*mfp)paramtypes; \
1047 SH_SETUP_MFP(mfp); \
1048 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1051 orig_ret = override_ret; \
1053 #define SH_RETURN() \
1054 SH_GLOB_SHPTR->HookLoopEnd(); \
1055 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
1057 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
1058 SH_SETUPCALLS(rettype, paramtypes, params) \
1059 SH_CALL_HOOKS(pre, params) \
1060 SH_CALL_ORIG(rettype, paramtypes, params) \
1061 SH_CALL_HOOKS(post, params) \
1064 //////////////////////////////////////////////////////////////////////////
1065 #define SH_SETUPCALLS_void(paramtypes, params) \
1066 /* 1) Find the vfnptr */ \
1067 using namespace ::SourceHook; \
1068 void *ourvfnptr = reinterpret_cast<void*>( \
1069 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
1070 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
1071 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
1073 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
1074 /* ... and the iface */ \
1075 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
1078 /* The iface info was not found. Redirect the call to the original function. */ \
1079 void (EmptyClass::*mfp)paramtypes; \
1080 SH_SETUP_MFP(mfp); \
1081 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1084 /* 2) Declare some vars and set it up */ \
1085 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
1086 IHookList *prelist = ifinfo->GetPreHooks(); \
1087 IHookList *postlist = ifinfo->GetPostHooks(); \
1088 META_RES status = MRES_IGNORED; \
1089 META_RES prev_res; \
1092 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
1094 #define SH_CALL_HOOKS_void(post, params) \
1095 if (SH_GLOB_SHPTR->ShouldContinue()) \
1097 prev_res = MRES_IGNORED; \
1098 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
1100 cur_res = MRES_IGNORED; \
1101 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
1102 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
1103 prev_res = cur_res; \
1104 if (cur_res > status) \
1106 if (!SH_GLOB_SHPTR->ShouldContinue()) \
1114 #define SH_CALL_ORIG_void(paramtypes, params) \
1115 if (status != MRES_SUPERCEDE) \
1117 void (EmptyClass::*mfp)paramtypes; \
1118 SH_SETUP_MFP(mfp); \
1119 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
1122 #define SH_RETURN_void() \
1123 SH_GLOB_SHPTR->HookLoopEnd();
1125 #define SH_HANDLEFUNC_void(paramtypes, params) \
1126 SH_SETUPCALLS_void(paramtypes, params) \
1127 SH_CALL_HOOKS_void(pre, params) \
1128 SH_CALL_ORIG_void(paramtypes, params) \
1129 SH_CALL_HOOKS_void(post, params) \
1133 // Special vafmt handlers
1135 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
1136 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
1137 SH_CALL_HOOKS(pre, params_plug) \
1138 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
1139 SH_CALL_HOOKS(post, params_plug) \
1142 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
1143 SH_SETUPCALLS_void(paramtypes, params_orig) \
1144 SH_CALL_HOOKS_void(pre, params_plug) \
1145 SH_CALL_ORIG_void(paramtypes, params_orig) \
1146 SH_CALL_HOOKS_void(post, params_plug) \
1149 //////////////////////////////////////////////////////////////////////////
1152 // sizeof on references returns the size of the datatype, NOT the pointer size or something
1153 // -> one should probably flag references in __SourceHook_ParamSizes_* !
1154 // or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
1157 // ********* Support for $1 arguments *********
1158 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1159 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1160 (&ifacetype::ifacefunc))) \
1161 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1162 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1163 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1164 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1165 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1167 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1168 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1169 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1171 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1172 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1173 (&ifacetype::ifacefunc))) \
1174 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1175 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1176 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1177 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1178 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1181 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1182 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1183 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1185 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1186 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1187 (&ifacetype::ifacefunc))) \
1188 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1189 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1191 char buf[::SourceHook::STRBUF_LEN]; \
1193 va_start(ap, fmt); \
1194 vsnprintf(buf, sizeof(buf), fmt, ap); \
1196 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); \
1198 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1199 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1201 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1202 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1203 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1205 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1206 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1207 (&ifacetype::ifacefunc))) \
1208 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1209 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1211 char buf[::SourceHook::STRBUF_LEN]; \
1213 va_start(ap, fmt); \
1214 vsnprintf(buf, sizeof(buf), fmt, ap); \
1216 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)); \
1218 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1219 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1221 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1222 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1223 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1225 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1226 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1227 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1228 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1229 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1230 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1231 typedef rettype RetType; \
1232 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1234 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1235 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
1236 $1, __SourceHook_ParamSizesM_##hookname); \
1238 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1239 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1240 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1241 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1242 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1243 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1244 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1246 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1247 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
1248 $1, __SourceHook_ParamSizesM_##hookname); \
1253 //////////////////////////////////////////////////////////////////////////
1256 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1258 using namespace ::SourceHook; \
1260 m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
1261 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1262 m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
1266 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1268 using namespace ::SourceHook; \
1270 m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
1271 (m_ThisPtr->*m_MFP)call; \
1272 m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
1275 namespace SourceHook
1277 // Call Class Wrapper!
1278 template <class T> struct CCW
1282 // Get Real Pointer!
1283 static inline T *GRP(T *p)
1289 template <class T> struct CCW< LegacyCallClass<T> >
1293 // Get Real Pointer!
1294 static inline T *GRP(LegacyCallClass<T> *p)
1301 // Support for $1 arguments
1302 template<class ObjType, class MFP, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1310 ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
1311 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
1313 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1314 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1317 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1318 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1322 template<class ObjType, class MFP@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFP, void@[$2,1,$1:, Param$2@]>
1330 ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
1331 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
1333 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1334 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1337 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1338 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1345 #define SH__CALL_GET_VFNPTR_NORMAL \
1346 using namespace ::SourceHook; \
1347 MemFuncInfo mfi = {true, -1, 0, 0}; \
1348 GetFuncInfo(CCW<Y>::GRP(ptr), mfp, mfi); \
1349 void *vfnptr = reinterpret_cast<void*>( \
1350 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1352 #define SH__CALL_GET_VFNPTR_MANUAL \
1353 using namespace ::SourceHook; \
1354 void *vfnptr = reinterpret_cast<void*>( \
1355 *reinterpret_cast<void***>( (reinterpret_cast<char*>(CCW<Y>::GRP(ptr)) + thisptroffs + vtbloffs) ) + vtblidx); \
1357 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1359 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1360 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1361 // The only purpose of the mfp2 parameter is to extract the return type
1364 // Support for $1 arguments
1365 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1366 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1367 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1369 SH__CALL_GET_VFNPTR_NORMAL
1370 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1371 mfp, vfnptr, shptr, plug);
1374 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1375 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1376 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1378 SH__CALL_GET_VFNPTR_NORMAL
1379 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1380 mfp, vfnptr, shptr, plug);
1383 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1384 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1385 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)
1387 SH__CALL_GET_VFNPTR_MANUAL
1388 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1389 reinterpret_cast<SourceHook::EmptyClass*>(CCW<Y>::GRP(ptr)), mfp, vfnptr, shptr, plug);
1393 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1394 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1397 // Support for $1 arguments
1398 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1399 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1400 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1402 SH__CALL_GET_VFNPTR_NORMAL
1403 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1404 mfp, vfnptr, shptr, plug);
1407 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1408 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
1409 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
1411 SH__CALL_GET_VFNPTR_NORMAL
1412 return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
1413 mfp, vfnptr, shptr, plug);
1420 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
1421 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
1422 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1423 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1425 #undef SH_MAKE_EXECUTABLECLASS_OB
1427 //////////////////////////////////////////////////////////////////////////
1428 // SetOverrideRet and RecallGetIface for recalls
1429 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1430 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1431 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1433 namespace SourceHook
1435 template <class RetType>
1436 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1438 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1441 // SetOverrideResult used to be implemented like this:
1442 // SetOverrideResult(shptr, memfuncptr, return);
1443 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1444 // (it thought it was ambigous - the ref and non-ref type)
1445 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1446 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1447 // This also allows us to create a special version for references which respects ReferenceCarrier.
1449 template <class T> struct OverrideFunctor
1451 void operator()(ISourceHook *shptr, T res)
1453 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1456 template <class T> struct OverrideFunctor<T&>
1458 void operator()(ISourceHook *shptr, T &res)
1460 // overrideretptr points to ReferenceCarrier<T&>
1461 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1465 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1466 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1468 return OverrideFunctor<RetType>();
1471 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1472 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1474 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1480 // The pope is dead. -> :(