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
25 #define SH_IMPL_VERSION 3
28 // 1 - Support for recalls, performance optimisations
29 #define SH_HOOKMAN_VERSION 1
31 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
32 // It's used in various macros
34 #define SH_GLOB_SHPTR g_SHPtr
37 // Used to identify the current plugin
38 #ifndef SH_GLOB_PLUGPTR
39 #define SH_GLOB_PLUGPTR g_PLID
46 # define SH_ASSERT(x, info) \
50 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
58 # define SH_ASSERT(x, info)
62 #define SH_SYS_WIN32 1
63 #define SH_SYS_LINUX 2
66 # define SH_SYS SH_SYS_WIN32
67 #elif defined __linux__
68 # define SH_SYS SH_SYS_LINUX
70 # error Unsupported system
75 #define SH_COMP_MSVC 2
78 # define SH_COMP SH_COMP_MSVC
79 #elif defined __GNUC__
80 # define SH_COMP SH_COMP_GCC
82 # error Unsupported compiler
85 #if SH_COMP==SH_COMP_MSVC
86 # define vsnprintf _vsnprintf
89 #if SH_SYS != SH_SYS_WIN32
93 #define SH_PTRSIZE sizeof(void*)
95 #include "FastDelegate.h"
96 #include "sh_memfuncinfo.h"
100 // Flags returned by a plugin's api function.
101 // NOTE: order is crucial, as greater/less comparisons are made.
104 MRES_IGNORED=0, // plugin didn't take any action
105 MRES_HANDLED, // plugin did something, but real function should still be called
106 MRES_OVERRIDE, // call real function, but use my return value
107 MRES_SUPERCEDE // skip real function; use my return value
114 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
116 const int STRBUF_LEN=4096;
119 * @brief An empty class. No inheritance used. Used for original-function-call hacks
126 * @brief Implicit cast.
128 template <class In, class Out>
129 inline Out implicit_cast(In input)
135 * @brief A plugin typedef
137 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
138 * plugin has a unique identifier, everything is ok.
139 * It should also be possible to set it to 0.
145 ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
148 int beginningNull; //!< To distinguish from old protos (which never begin with 0)
149 int retTypeSize; //!< 0 if void
150 int numOfParams; //!< number of parameters
151 const int *params; //!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
155 * @brief Specifies the actions for hook managers
157 enum HookManagerAction
159 HA_GetInfo = 0, //!< Store info about the hook manager
160 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
161 HA_Unregister //!< Clear the saved pointer
164 struct IHookManagerInfo;
167 * @brief Pointer to hook manager interface function
169 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
171 * @param ha What the hook manager should do
172 * @param hi A pointer to IHookManagerInfo
174 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
179 virtual void DeleteThis() = 0; // Ugly, I know
180 virtual bool IsEqual(ISHDelegate *other) = 0;
183 template <class T> class CSHDelegate : public ISHDelegate
187 CSHDelegate(T deleg) : m_Deleg(deleg)
191 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
200 bool IsEqual(ISHDelegate *other)
202 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
215 virtual bool End() = 0;
216 virtual void Next() = 0;
217 virtual ISHDelegate *Handler() = 0;
218 virtual int ThisPtrOffs() = 0;
220 virtual IIter *GetIter() = 0;
221 virtual void ReleaseIter(IIter *pIter) = 0;
226 virtual void *GetPtr() = 0;
227 virtual IHookList *GetPreHooks() = 0;
228 virtual IHookList *GetPostHooks() = 0;
233 virtual void *GetVfnPtr() = 0;
234 virtual void *GetOrigEntry() = 0;
236 virtual IIface *FindIface(void *ptr) = 0;
239 struct IHookManagerInfo
241 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
243 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
244 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
246 // Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
247 // If the hookman doesn't set this, it defaults 0
248 // SourceHook prefers hookmans with higher version numbers
249 virtual void SetVersion(int version) = 0;
255 IHookList::IIter *m_pIter;
257 AutoHookIter(IHookList *pList)
258 : m_pList(pList), m_pIter(pList->GetIter())
265 m_pList->ReleaseIter(m_pIter);
270 return m_pIter->End();
278 ISHDelegate *Handler()
280 return m_pIter->Handler();
285 return m_pIter->ThisPtrOffs();
294 template<class B> struct CallClass
296 virtual B *GetThisPtr() = 0;
297 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
300 typedef CallClass<void> GenericCallClass;
301 typedef CallClass<EmptyClass> ManualCallClass;
303 // 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
304 // - Today is an important day.
305 // I'm adding support for functions which return references.
308 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
309 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
310 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
311 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
312 // special cases were needed for getoverrideret / getorigret; these are implemented through the
313 // SourceHook::MacroRefHelpers structs.
314 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
315 template <class T> struct ReferenceCarrier
320 template <class T> struct ReferenceCarrier<T&>
326 type() : m_StoredRef(NULL)
329 type(T& ref) : m_StoredRef(&ref)
333 T& operator= (T& ref)
347 * @brief The main SourceHook interface
353 * @brief Return interface version
355 virtual int GetIfaceVersion() = 0;
358 * @brief Return implementation version
360 virtual int GetImplVersion() = 0;
365 * @return True if the function succeeded, false otherwise
367 * @param plug The unique identifier of the plugin that calls this function
368 * @param iface The interface pointer
369 * @param ifacesize The size of the class iface points to
370 * @param myHookMan A hook manager function that should be capable of handling the function
371 * @param handler A pointer to a FastDelegate containing the hook handler
372 * @param post Set to true if you want a post handler
374 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
375 ISHDelegate *handler, bool post) = 0;
378 * @brief Removes a hook.
380 * @return True if the function succeeded, false otherwise
382 * @param plug The unique identifier of the plugin that calls this function
383 * @param iface The interface pointer
384 * @param myHookMan A hook manager function that should be capable of handling the function
385 * @param handler A pointer to a FastDelegate containing the hook handler
386 * @param post Set to true if you want a post handler
388 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
389 ISHDelegate *handler, bool post) = 0;
392 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
394 * @param plug The unique identifier of the plugin in question
396 virtual bool IsPluginInUse(Plugin plug) = 0;
399 * @brief Return a pointer to a callclass. Generate a new one if required.
401 * @param iface The interface pointer
402 * @param size Size of the class instance
404 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
407 * @brief Release a callclass
409 * @param ptr Pointer to the callclass
411 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
413 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
414 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
415 //!< previously calledhandler
416 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
417 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
418 //!< If not in post function, undefined
419 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
420 //!< If none is specified, NULL
421 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
422 //////////////////////////////////////////////////////////////////////////
424 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
425 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
426 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
427 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
428 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
429 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
430 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
431 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
432 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
435 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
437 * @param plug The owner of the hook manager
438 * @param pubFunc The hook manager's info function
440 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
442 virtual void DoRecall() = 0; //!< Initiates a recall sequence
447 Users want the ability to change parameters of the called function
448 from inside their handler.
451 2) Recall the function
452 3) => SH's hook func gets called:
453 4) The first iterator points at the first hook the last hookfunc didn't execute yet
454 5) does all iteration and returns normally
455 6) The user's handler returns immediately
456 7) The hook func returns immediately as well
458 Also note that the recalled hookfunc starts with the status the recalling hookfunc
459 ended with. The last handler (doing the recall) is also able to specify its own
463 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
466 * @brief Set up the hook loop. Equivalent to calling:
467 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
469 * @param statusPtr pointer to status variable
470 * @param prevResPtr pointer to previous result variable
471 * @param curResPtr pointer to current result variable
472 * @param ifacePtrPtr pointer to interface this pointer variable
473 * @param origRetPr pointer to original return value variable. NULL for void funcs
474 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
476 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
477 * when the hook func is being called as part of a recall
479 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
480 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
485 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
486 // These have to be able to return references. If T is a reference, the pointers returned
487 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
488 template <class T> struct MacroRefHelpers
490 inline static const T* GetOrigRet(ISourceHook *shptr)
492 return reinterpret_cast<const T*>(shptr->GetOrigRet());
494 inline static const T* GetOverrideRet(ISourceHook *shptr)
496 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
500 template <class T> struct MacroRefHelpers<T&>
502 inline static T* GetOrigRet(ISourceHook *shptr)
504 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
507 inline static T* GetOverrideRet(ISourceHook *shptr)
509 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
515 /************************************************************************/
516 /* High level interface */
517 /************************************************************************/
518 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
519 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
520 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
521 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
522 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
524 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
525 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
526 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
528 // If a hook on a function which returns a reference does not want to specify a return value,
529 // it can use this macro.
530 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
531 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
533 // only call these from the hook handlers directly!
534 // :TODO: enforce it ?
536 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
537 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
539 // SourceHook::SetOverrideRet is defined later.
540 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
542 SET_META_RESULT(result); \
543 SH_GLOB_SHPTR->DoRecall(); \
544 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
545 RETURN_META(MRES_SUPERCEDE); \
548 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
550 SET_META_RESULT(result); \
551 SH_GLOB_SHPTR->DoRecall(); \
552 if ((result) >= MRES_OVERRIDE) \
554 /* meh, set the override result here because we don't get a chance to return */ \
555 /* before continuing the hook loop through the recall */ \
556 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
558 RETURN_META_VALUE(MRES_SUPERCEDE, \
559 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
562 // :TODO: thisptroffs in MNEWPARAMS ??
564 #if SH_COMP == SH_COMP_MSVC
566 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
569 SH_MFHCls(hookname)::ECMFP mfp; \
572 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
573 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
574 SH_MFHCls(hookname)::ms_MFI.vtblindex];
576 #elif SH_COMP == SH_COMP_GCC
578 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
581 SH_MFHCls(hookname)::ECMFP mfp; \
588 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
589 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
590 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
595 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
597 SET_META_RESULT(result); \
598 SH_GLOB_SHPTR->DoRecall(); \
599 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
600 (thisptr->*(u.mfp)) newparams; \
601 RETURN_META(MRES_SUPERCEDE); \
604 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
606 SET_META_RESULT(result); \
607 SH_GLOB_SHPTR->DoRecall(); \
608 if ((result) >= MRES_OVERRIDE) \
610 /* see RETURN_META_VALUE_NEWPARAMS */ \
611 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
613 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
614 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
618 * @brief Get/generate callclass for an interface pointer
620 * @param ifaceptr The interface pointer
622 template<class ifacetype>
623 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
625 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
626 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
629 template<class ifacetype>
630 inline SourceHook::CallClass<ifacetype> *SH_GET_MCALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr, int ifacesize)
632 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
633 shptr->GetCallClass(reinterpret_cast<void*>(ptr), ifacesize));
636 template<class ifacetype>
637 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
639 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
642 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
644 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
645 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
646 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
647 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
650 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
651 #define SH_GET_MCALLCLASS(ptr, size) SH_GET_MCALLCLASS_R(SH_GLOB_SHPTR, reinterpret_cast<SourceHook::EmptyClass*>(ptr), size)
652 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
654 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
655 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
657 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
658 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
659 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
660 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
662 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
663 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
665 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
666 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
667 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
668 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
671 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
672 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
673 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
674 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
675 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
676 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
678 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
679 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
680 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
681 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
682 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
683 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
691 #if SH_COMP == SH_COMP_MSVC
692 # define SH_SETUP_MFP(mfp) \
693 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
694 #elif SH_COMP == SH_COMP_GCC
695 # define SH_SETUP_MFP(mfp) \
696 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
697 reinterpret_cast<void**>(&mfp)[1] = 0;
699 # error Not supported yet.
702 //////////////////////////////////////////////////////////////////////////
703 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
704 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
706 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
707 SH_FHCls(ifacetype,ifacefunc,overload)() \
709 GetFuncInfo(funcptr, ms_MFI); \
712 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
714 using namespace ::SourceHook; \
715 GetFuncInfo(funcptr, ms_MFI); \
716 /* Verify interface version */ \
717 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
720 if (action == HA_GetInfo) \
722 param->SetVersion(SH_HOOKMAN_VERSION); \
723 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
724 reinterpret_cast<const char*>(&ms_Proto)); \
726 MemFuncInfo mfi = {true, -1, 0, 0}; \
727 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
728 param->SetHookfuncVfnptr( \
729 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
732 else if (action == HA_Register) \
737 else if (action == HA_Unregister) \
746 // It has to be possible to use the macros in namespaces
747 // -> So we need to access and extend the global SourceHook namespace
748 // We use a namespace alias for this
749 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
750 struct SH_FHCls(ifacetype,ifacefunc,overload) \
752 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
753 static ::SourceHook::MemFuncInfo ms_MFI; \
754 static ::SourceHook::IHookManagerInfo *ms_HI; \
755 static ::SourceHook::ProtoInfo ms_Proto; \
756 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
758 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
760 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
761 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
762 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
763 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
764 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
766 using namespace ::SourceHook; \
767 MemFuncInfo mfi = {true, -1, 0, 0}; \
768 GetFuncInfo(funcptr, mfi); \
769 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
770 return false; /* No non-virtual functions / virtual inheritance supported */ \
772 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
773 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
774 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
776 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
777 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
779 using namespace ::SourceHook; \
780 MemFuncInfo mfi = {true, -1, 0, 0}; \
781 GetFuncInfo(funcptr, mfi); \
782 if (mfi.thisptroffs < 0) \
783 return false; /* No virtual inheritance supported */ \
785 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
786 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
787 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
790 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
791 struct SH_MFHCls(hookname) \
793 static SH_MFHCls(hookname) ms_Inst; \
794 static ::SourceHook::MemFuncInfo ms_MFI; \
795 static ::SourceHook::IHookManagerInfo *ms_HI; \
796 static ::SourceHook::ProtoInfo ms_Proto; \
798 SH_MFHCls(hookname)() \
800 ms_MFI.isVirtual = true; \
801 ms_MFI.thisptroffs = pthisptroffs; \
802 ms_MFI.vtblindex = pvtblidx; \
803 ms_MFI.vtbloffs = pvtbloffs; \
805 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
807 using namespace ::SourceHook; \
808 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
809 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
810 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
811 /* Verify interface version */ \
812 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
815 if (action == HA_GetInfo) \
817 param->SetVersion(SH_HOOKMAN_VERSION); \
818 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
819 reinterpret_cast<const char*>(&ms_Proto)); \
821 MemFuncInfo mfi = {true, -1, 0, 0}; \
822 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
823 param->SetHookfuncVfnptr( \
824 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
827 else if (action == HA_Register) \
832 else if (action == HA_Unregister) \
841 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
843 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
844 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
845 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
846 bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
847 SH_MFHCls(hookname)::FD handler) \
849 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
850 SH_MFHCls(hookname)::HookManPubFunc, \
851 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
853 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
854 SH_MFHCls(hookname)::FD handler) \
856 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
857 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
858 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
862 #define SH_SETUPCALLS(rettype, paramtypes, params) \
863 /* 1) Find the vfnptr */ \
864 using namespace ::SourceHook; \
865 void *ourvfnptr = reinterpret_cast<void*>( \
866 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
867 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
868 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
870 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
871 /* ... and the iface */ \
872 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
875 /* The iface info was not found. Redirect the call to the original function. */ \
876 rettype (EmptyClass::*mfp)paramtypes; \
878 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
880 /* 2) Declare some vars and set it up */ \
881 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
882 IHookList *prelist = ifinfo->GetPreHooks(); \
883 IHookList *postlist = ifinfo->GetPostHooks(); \
884 META_RES status = MRES_IGNORED; \
887 typedef ReferenceCarrier<rettype>::type my_rettype; \
888 my_rettype orig_ret; \
889 my_rettype override_ret; \
890 my_rettype plugin_ret; \
892 my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
893 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
895 #define SH_CALL_HOOKS(post, params) \
896 if (SH_GLOB_SHPTR->ShouldContinue()) \
898 prev_res = MRES_IGNORED; \
899 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
901 cur_res = MRES_IGNORED; \
902 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
903 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
904 prev_res = cur_res; \
905 if (cur_res > status) \
907 if (cur_res >= MRES_OVERRIDE) \
908 *pOverrideRet = plugin_ret; \
909 if (!SH_GLOB_SHPTR->ShouldContinue()) \
917 #define SH_CALL_ORIG(rettype, paramtypes, params) \
918 if (status != MRES_SUPERCEDE) \
920 rettype (EmptyClass::*mfp)paramtypes; \
922 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
925 orig_ret = override_ret; \
927 #define SH_RETURN() \
928 SH_GLOB_SHPTR->HookLoopEnd(); \
929 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
931 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
932 SH_SETUPCALLS(rettype, paramtypes, params) \
933 SH_CALL_HOOKS(pre, params) \
934 SH_CALL_ORIG(rettype, paramtypes, params) \
935 SH_CALL_HOOKS(post, params) \
938 //////////////////////////////////////////////////////////////////////////
939 #define SH_SETUPCALLS_void(paramtypes, params) \
940 /* 1) Find the vfnptr */ \
941 using namespace ::SourceHook; \
942 void *ourvfnptr = reinterpret_cast<void*>( \
943 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
944 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
945 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
947 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
948 /* ... and the iface */ \
949 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
952 /* The iface info was not found. Redirect the call to the original function. */ \
953 void (EmptyClass::*mfp)paramtypes; \
955 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
958 /* 2) Declare some vars and set it up */ \
959 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
960 IHookList *prelist = ifinfo->GetPreHooks(); \
961 IHookList *postlist = ifinfo->GetPostHooks(); \
962 META_RES status = MRES_IGNORED; \
966 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
968 #define SH_CALL_HOOKS_void(post, params) \
969 if (SH_GLOB_SHPTR->ShouldContinue()) \
971 prev_res = MRES_IGNORED; \
972 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
974 cur_res = MRES_IGNORED; \
975 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
976 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
977 prev_res = cur_res; \
978 if (cur_res > status) \
980 if (!SH_GLOB_SHPTR->ShouldContinue()) \
988 #define SH_CALL_ORIG_void(paramtypes, params) \
989 if (status != MRES_SUPERCEDE) \
991 void (EmptyClass::*mfp)paramtypes; \
993 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
996 #define SH_RETURN_void() \
997 SH_GLOB_SHPTR->HookLoopEnd();
999 #define SH_HANDLEFUNC_void(paramtypes, params) \
1000 SH_SETUPCALLS_void(paramtypes, params) \
1001 SH_CALL_HOOKS_void(pre, params) \
1002 SH_CALL_ORIG_void(paramtypes, params) \
1003 SH_CALL_HOOKS_void(post, params) \
1007 // Special vafmt handlers
1009 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
1010 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
1011 SH_CALL_HOOKS(pre, params_plug) \
1012 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
1013 SH_CALL_HOOKS(post, params_plug) \
1016 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
1017 SH_SETUPCALLS_void(paramtypes, params_orig) \
1018 SH_CALL_HOOKS_void(pre, params_plug) \
1019 SH_CALL_ORIG_void(paramtypes, params_orig) \
1020 SH_CALL_HOOKS_void(post, params_plug) \
1023 //////////////////////////////////////////////////////////////////////////
1026 // sizeof on references returns the size of the datatype, NOT the pointer size or something
1027 // -> one should probably flag references in __SourceHook_ParamSizes_* !
1028 // or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
1031 // ********* Support for $1 arguments *********
1032 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1033 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1034 (&ifacetype::ifacefunc))) \
1035 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1036 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1037 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1038 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1039 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1041 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1042 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1043 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1045 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1046 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
1047 (&ifacetype::ifacefunc))) \
1048 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1049 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1050 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1051 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1052 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
1055 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1056 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1057 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1059 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
1060 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1061 (&ifacetype::ifacefunc))) \
1062 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
1063 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1065 char buf[::SourceHook::STRBUF_LEN]; \
1067 va_start(ap, fmt); \
1068 vsnprintf(buf, sizeof(buf), fmt, ap); \
1070 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); \
1072 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1073 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1075 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1076 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
1077 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1079 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
1080 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
1081 (&ifacetype::ifacefunc))) \
1082 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1083 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1085 char buf[::SourceHook::STRBUF_LEN]; \
1087 va_start(ap, fmt); \
1088 vsnprintf(buf, sizeof(buf), fmt, ap); \
1090 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)); \
1092 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1093 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1095 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1096 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1097 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1099 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1100 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1101 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1102 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1103 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1104 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1105 typedef rettype RetType; \
1106 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1108 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1109 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
1110 $1, __SourceHook_ParamSizesM_##hookname); \
1112 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1113 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1114 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1115 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1116 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1117 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1118 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1120 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1121 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
1122 $1, __SourceHook_ParamSizesM_##hookname); \
1127 //////////////////////////////////////////////////////////////////////////
1130 #if SH_COMP == SH_COMP_MSVC
1132 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1134 using namespace ::SourceHook; \
1135 MemFuncInfo mfi = {true, -1, 0, 0}; \
1136 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
1137 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
1139 return (m_CC->GetThisPtr()->*m_MFP)call; \
1141 /* It's hooked. Call the original function. */ \
1144 RetType(EmptyClass::*mfpnew)prms; \
1147 u.addr = origfunc; \
1149 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
1150 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
1153 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
1155 using namespace ::SourceHook; \
1156 char *adjustedthisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()) + m_ThisPtrOffs; \
1159 RetType(EmptyClass::*mfpnew)prms; \
1162 u.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
1164 u.addr = (*reinterpret_cast<void***>(adjustedthisptr + m_VtblOffs))[m_VtblIdx]; \
1166 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
1169 #elif SH_COMP == SH_COMP_GCC
1171 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1173 using namespace ::SourceHook; \
1174 MemFuncInfo mfi = {true, -1, 0, 0}; \
1175 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
1176 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
1178 return (m_CC->GetThisPtr()->*m_MFP)call; \
1180 /* It's hooked. Call the original function. */ \
1183 RetType(EmptyClass::*mfpnew)prms; \
1187 intptr_t adjustor; \
1190 u.s.addr = origfunc; \
1191 u.s.adjustor = mfi.thisptroffs; \
1193 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
1196 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
1198 using namespace ::SourceHook; \
1199 char *thisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()); \
1202 RetType(EmptyClass::*mfpnew)prms; \
1205 intptr_t adjustor; \
1208 u.s.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
1210 u.s.addr = (*reinterpret_cast<void***>(thisptr + m_ThisPtrOffs + m_VtblOffs))[m_VtblIdx]; \
1212 u.s.adjustor = m_ThisPtrOffs; \
1213 return (reinterpret_cast<EmptyClass*>(thisptr)->*u.mfpnew)call; \
1218 namespace SourceHook
1221 // Support for $1 arguments
1222 template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1227 ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
1229 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1230 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1233 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1234 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1238 template <class RetType@[$2,1,$1:, class Param$2@]> class MExecutableClass$1
1240 ManualCallClass *m_CC;
1245 MExecutableClass$1(ManualCallClass *cc, int vtbloffs, int vtblidx, int thisptroffs) : m_CC(cc),
1246 m_ThisPtrOffs(thisptroffs), m_VtblIdx(vtblidx), m_VtblOffs(vtbloffs) { }
1248 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1249 SH_MAKE_MEXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1252 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1253 SH_MAKE_MEXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1260 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1261 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1262 // The only purpose of the mfp2 parameter is to extract the return type
1265 // Support for $1 arguments
1266 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1267 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1268 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
1270 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1273 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1274 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1275 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
1277 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1280 template <class X, class RetType@[$2,1,$1:, class Param$2@]>
1281 SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>
1282 SH_MCALL2(SourceHook::ManualCallClass *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
1284 return SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>(ptr, vtbloffs, vtblidx, thisptroffs);
1288 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1289 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1292 // Support for $1 arguments
1293 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1294 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1295 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
1297 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1300 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1301 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1302 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
1304 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1311 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
1312 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1313 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1315 #undef SH_MAKE_EXECUTABLECLASS_OB
1317 //////////////////////////////////////////////////////////////////////////
1318 // SetOverrideRet and RecallGetIface for recalls
1319 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1320 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1321 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1323 namespace SourceHook
1325 template <class RetType>
1326 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1328 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1331 // SetOverrideResult used to be implemented like this:
1332 // SetOverrideResult(shptr, memfuncptr, return);
1333 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1334 // (it thought it was ambigous - the ref and non-ref type)
1335 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1336 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1337 // This also allows us to create a special version for references which respects ReferenceCarrier.
1339 template <class T> struct OverrideFunctor
1341 void operator()(ISourceHook *shptr, T res)
1343 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1346 template <class T> struct OverrideFunctor<T&>
1348 void operator()(ISourceHook *shptr, T &res)
1350 // overrideretptr points to ReferenceCarrier<T&>
1351 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1355 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1356 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1358 return OverrideFunctor<RetType>();
1361 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1362 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1364 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1370 // The pope is dead. -> :(