correct version now... i guess i'm just tired :)
1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
13 * @brief Contains the public SourceHook API
16 #ifndef __SOURCEHOOK_H__
17 #define __SOURCEHOOK_H__
19 // Interface revisions:
20 // 1 - Initial revision
21 // 2 - Changed to virtual functions for iterators and all queries
22 // 3 - Added "hook loop status variable"
24 // 5 - New "V2" interface
25 #define SH_IFACE_VERSION 5
29 // 4 - addition of hook ids and vp hooks (with them, AddHookNew and RemoveHookNew)
30 // This is not a SH_IFACE_VERSION change so that old plugins continue working!
31 // 5 - implementation of the new "V2" interface
32 #define SH_IMPL_VERSION 5
36 #define SH_HOOKMAN_VERSION 1
38 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
39 // It's used in various macros
41 #define SH_GLOB_SHPTR g_SHPtr
44 // Used to identify the current plugin
45 #ifndef SH_GLOB_PLUGPTR
46 #define SH_GLOB_PLUGPTR g_PLID
53 # define SH_ASSERT(x, info) \
57 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
65 # define SH_ASSERT(x, info)
69 #define SH_SYS_WIN32 1
70 #define SH_SYS_LINUX 2
73 # define SH_SYS SH_SYS_WIN32
74 #elif defined __linux__
75 # define SH_SYS SH_SYS_LINUX
77 # error Unsupported system
82 #define SH_COMP_MSVC 2
85 # define SH_COMP SH_COMP_MSVC
86 #elif defined __GNUC__
87 # define SH_COMP SH_COMP_GCC
89 # error Unsupported compiler
92 #if SH_COMP==SH_COMP_MSVC
93 # define vsnprintf _vsnprintf
96 #if SH_SYS != SH_SYS_WIN32
100 #define SH_PTRSIZE sizeof(void*)
102 #include "sh_memfuncinfo.h"
103 #include "FastDelegate.h"
107 // Flags returned by a plugin's api function.
108 // NOTE: order is crucial, as greater/less comparisons are made.
111 MRES_IGNORED=0, // plugin didn't take any action
112 MRES_HANDLED, // plugin did something, but real function should still be called
113 MRES_OVERRIDE, // call real function, but use my return value
114 MRES_SUPERCEDE // skip real function; use my return value
121 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
123 const int STRBUF_LEN=4096;
126 * @brief An empty class. No inheritance used. Used for original-function-call hacks
133 * @brief Implicit cast.
135 template <class In, class Out>
136 inline Out implicit_cast(In input)
142 * @brief A plugin typedef
144 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
145 * plugin has a unique identifier, everything is ok.
146 * It should also be possible to set it to 0.
151 template <class T> class CallClass : public T
154 typedef void ManualCallClass;
157 * @brief Describes the way a parameter/retval is passed.
163 PassType_Unknown=0, /**< Unknown -- no extra info available */
164 PassType_Basic, /**< Plain old register data (pointers, integers) */
165 PassType_Float, /**< Floating point data */
166 PassType_Object, /**< Object or structure */
171 PassFlag_ByVal = (1<<0), /**< Passing by value */
172 PassFlag_ByRef = (1<<1), /**< Passing by reference */
173 PassFlag_ODtor = (1<<2), /**< Object has a destructor */
174 PassFlag_OCtor = (1<<3), /**< Object has a constructor */
175 PassFlag_AssignOp = (1<<4) /**< Object has an assignment operator */
178 size_t size; //!< Size of the data being passed
181 // (might be used in future versions for automatic hookfunc generation)
182 int type; //!< PassType value
183 unsigned int flags; //!< Pass/return flags
190 CallConv_Unknown, /**< Unknown -- no extra info available */
191 CallConv_ThisCall, /**< This call (object pointer required) */
192 CallConv_Cdecl, /**< Standard C call */
195 int numOfParams; //!< number of parameters
196 PassInfo retPassInfo; //!< PassInfo for the return value. size=0 -> no retval
197 const PassInfo *paramsPassInfo; //!< PassInfos for the parameters
203 struct IHookManagerInfo;
206 * @brief Pointer to hook manager interface function
208 * The hook manager should store hi for later use if store==true. It should then call hi->SetInfo(...) if hi
209 * is non-null. The hook manager can return 0 for success or a non-zero value if it doesn't want to be used.
211 * @param hi A pointer to IHookManagerInfo
213 typedef int (*HookManagerPubFunc)(bool store, IHookManagerInfo *hi);
218 virtual bool IsEqual(ISHDelegate *pOtherDeleg) = 0; // pOtherDeleg is from the same plugin and hookman
219 virtual void DeleteThis() = 0;
222 struct IHookManagerInfo
224 virtual void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
225 ProtoInfo *proto, void *hookfunc_vfnptr) = 0;
228 // I'm adding support for functions which return references.
231 // SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
232 // override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
233 // this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
234 // which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
235 // special cases were needed for getoverrideret / getorigret; these are implemented through the
236 // SourceHook::MacroRefHelpers structs.
237 // Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
238 template <class T> struct ReferenceCarrier
243 template <class T> struct ReferenceCarrier<T&>
249 type() : m_StoredRef(NULL)
252 type(T& ref) : m_StoredRef(&ref)
256 T& operator= (T& ref)
271 virtual ISHDelegate *GetNext() = 0;
272 virtual void *GetOverrideRetPtr() = 0;
273 virtual const void *GetOrigRetPtr() = 0;
274 virtual bool ShouldCallOrig() = 0;
278 * @brief The main SourceHook interface
284 * @brief Return interface version
286 virtual int GetIfaceVersion() = 0;
289 * @brief Return implementation version
291 virtual int GetImplVersion() = 0;
294 * @brief Modes for the new AddHook
304 * @brief Add a (VP) hook.
306 * @return non-zero hook id on success, 0 otherwise
308 * @param plug The unique identifier of the plugin that calls this function
309 * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
310 * @param iface The interface pointer
311 * @param ifacesize The size of the class iface points to
312 * @param myHookMan A hook manager function that should be capable of handling the function
313 * @param handler A pointer to the hook handler something
314 * @param post Set to true if you want a post handler
316 virtual int AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
317 ISHDelegate *handler, bool post) = 0;
319 // Source backwarts compat (only for normal hooks)
320 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
321 ISHDelegate *handler, bool post) = 0;
324 * @brief Remove a hook by ID.
326 * @return true on success, false otherwise
328 * @param hookid The hook id (returned by AddHook)
330 virtual bool RemoveHookByID(int hookid) = 0;
333 * @brief Pause a hook by ID.
335 * @return true on success, false otherwise
337 * @param hookid The hook id (returned by AddHook)
339 virtual bool PauseHookByID(int hookid) = 0;
342 * @brief Unpause a hook by ID.
344 * @return true on success, false otherwise
346 * @param hookid The hook id (returned by AddHook)
348 virtual bool UnpauseHookByID(int hookid) = 0;
351 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
353 * @param plug The unique identifier of the plugin in question
356 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
357 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
358 //!< previously calledhandler
359 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
360 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
361 //!< If not in post function, undefined
362 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
363 //!< If none is specified, NULL
364 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
366 virtual void *GetOverrideRetPtr() = 0; //!< Used for setting the override return value
369 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
371 * @param plug The owner of the hook manager
372 * @param pubFunc The hook manager's info function
374 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
379 * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
381 * @param plug The unique identifier of the plugin that calls this function
382 * @param vfnptr The virtual function pointer of the function in question
384 virtual void SetIgnoreHooks(void *vfnptr) = 0;
387 * @brief Reverses SetIgnoreHooks' effect
389 * @param plug The unique identifier of the plugin that calls this function
390 * @param vfnptr The virtual function pointer of the function in question
392 virtual void ResetIgnoreHooks(void *vfnptr) = 0;
394 //////////////////////////////////////////////////////////////////////////
398 // Returns the current iface ptr
399 virtual void DoRecall() = 0; //!< Initiates a recall sequence
405 Users want the ability to change parameters of the called function
406 from inside their handler.
409 2) Recall the function
410 3) => SH's hook func gets called:
411 4) The first iterator points at the first hook the last hookfunc didn't execute yet
412 5) does all iteration and returns normally
413 6) The user's handler returns immediately
414 7) The hook func returns immediately as well
416 Also note that the recalled hookfunc starts with the status the recalling hookfunc
417 ended with. The last handler (doing the recall) is also able to specify its own
422 * @brief Set up the hook loop.
424 * @param statusPtr pointer to status variable
425 * @param prevResPtr pointer to previous result variable
426 * @param curResPtr pointer to current result variable
427 * @param ifacePtrPtr pointer to interface this pointer variable
428 * @param origRetPr pointer to original return value variable. NULL for void funcs
429 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
431 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
432 * when the hook func is being called as part of a recall
434 virtual IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry,
435 META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
436 const void *origRetPtr, void *overrideRetPtr) = 0;
438 virtual void EndContext(IHookContext *pCtx) = 0;
441 // For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
442 // These have to be able to return references. If T is a reference, the pointers returned
443 // from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
444 template <class T> struct MacroRefHelpers
446 inline static const T* GetOrigRet(ISourceHook *shptr)
448 return reinterpret_cast<const T*>(shptr->GetOrigRet());
450 inline static const T* GetOverrideRet(ISourceHook *shptr)
452 return reinterpret_cast<const T*>(shptr->GetOverrideRet());
456 template <class T> struct MacroRefHelpers<T&>
458 inline static T* GetOrigRet(ISourceHook *shptr)
460 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
463 inline static T* GetOverrideRet(ISourceHook *shptr)
465 T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
471 /************************************************************************/
472 /* High level interface */
473 /************************************************************************/
474 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
475 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
476 #define META_RESULT_ORIG_RET(type) *SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
477 #define META_RESULT_OVERRIDE_RET(type) *SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
478 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
480 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
481 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
482 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
486 SourceHook::CallClass<T> *SH_GET_CALLCLASS(T *p)
488 return static_cast< SourceHook::CallClass<T> *>(p);
490 #define SH_RELEASE_CALLCLASS(p)
492 #define SH_GET_MCALLCLASS(p, size) reinterpret_cast<void*>(p)
494 // only call these from the hook handlers directly!
496 // If a hook on a function which returns a reference does not want to specify a return value,
497 // it can use this macro.
498 // ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
499 #define RETURN_META_NOREF(result, rettype) do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
501 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
502 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
504 // SourceHook::SetOverrideRet is defined later.
505 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
507 SET_META_RESULT(result); \
508 SH_GLOB_SHPTR->DoRecall(); \
509 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
510 RETURN_META(MRES_SUPERCEDE); \
513 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
515 SET_META_RESULT(result); \
516 SH_GLOB_SHPTR->DoRecall(); \
517 if ((result) >= MRES_OVERRIDE) \
519 /* meh, set the override result here because we don't get a chance to return */ \
520 /* before continuing the hook loop through the recall */ \
521 SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
523 RETURN_META_VALUE(MRES_SUPERCEDE, \
524 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
527 // :TODO: thisptroffs in MNEWPARAMS ??
529 #if SH_COMP == SH_COMP_MSVC
531 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
534 SH_MFHCls(hookname)::ECMFP mfp; \
537 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
538 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
539 SH_MFHCls(hookname)::ms_MFI.vtblindex];
541 #elif SH_COMP == SH_COMP_GCC
543 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
546 SH_MFHCls(hookname)::ECMFP mfp; \
553 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
554 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
555 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
560 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
562 SET_META_RESULT(result); \
563 SH_GLOB_SHPTR->DoRecall(); \
564 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
565 (thisptr->*(u.mfp)) newparams; \
566 RETURN_META(MRES_SUPERCEDE); \
569 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
571 SET_META_RESULT(result); \
572 SH_GLOB_SHPTR->DoRecall(); \
573 if ((result) >= MRES_OVERRIDE) \
575 /* see RETURN_META_VALUE_NEWPARAMS */ \
576 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
578 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
579 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
583 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
585 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
586 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
587 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
588 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
591 // New ADD / REMOVE macros.
592 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
593 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
595 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
596 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
597 SourceHook::ISourceHook::Hook_Normal, post, handler)
599 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
600 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
603 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
604 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), \
605 SourceHook::ISourceHook::Hook_Normal, post, handler)
607 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
608 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
610 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
611 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
612 SourceHook::ISourceHook::Hook_VP, post, handler)
614 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
615 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
616 SourceHook::ISourceHook::Hook_DVP, post, handler)
618 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
619 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_VP, post, handler)
621 #define SH_ADD_MANUALDVPHOOK(hookname, ifaceptr, handler, post) \
622 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_DVP, post, handler)
624 #define SH_REMOVE_HOOK_ID(hookid) \
625 (SH_GLOB_SHPTR->RemoveHookByID(hookid))
628 // !! These are now deprecated. Instead, use one of these:
629 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
630 // SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
632 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
633 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
634 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
635 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
637 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
638 // SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
640 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
641 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
642 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
643 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
645 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
646 // SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
648 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
649 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
650 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
651 SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
653 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
654 // SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
656 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
657 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
658 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
659 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
666 #if SH_COMP == SH_COMP_MSVC
667 # define SH_SETUP_MFP(mfp) \
668 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
669 #elif SH_COMP == SH_COMP_GCC
670 # define SH_SETUP_MFP(mfp) \
671 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
672 reinterpret_cast<void**>(&mfp)[1] = 0;
674 # error Not supported yet.
677 //////////////////////////////////////////////////////////////////////////
678 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
679 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
681 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
682 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
684 using namespace ::SourceHook; \
685 GetFuncInfo(funcptr, ms_MFI); \
686 /* Verify interface version */ \
687 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
689 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
695 MemFuncInfo mfi = {true, -1, 0, 0}; \
696 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
697 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
698 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
703 // It has to be possible to use the macros in namespaces
704 // -> So we need to access and extend the global SourceHook namespace
705 // We use a namespace alias for this
706 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
707 struct SH_FHCls(ifacetype,ifacefunc,overload) \
709 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
710 static ::SourceHook::MemFuncInfo ms_MFI; \
711 static ::SourceHook::IHookManagerInfo *ms_HI; \
712 static ::SourceHook::ProtoInfo ms_Proto; \
713 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
715 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
717 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
718 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
719 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
720 int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
721 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
723 using namespace ::SourceHook; \
724 MemFuncInfo mfi = {true, -1, 0, 0}; \
725 GetFuncInfo(funcptr, mfi); \
726 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
727 return false; /* No non-virtual functions / virtual inheritance supported */ \
729 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
730 iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
731 new SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl(handler), post); \
733 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
734 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
736 using namespace ::SourceHook; \
737 MemFuncInfo mfi = {true, -1, 0, 0}; \
738 GetFuncInfo(funcptr, mfi); \
739 SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl tmp(handler); \
740 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
741 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
744 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
745 struct SH_MFHCls(hookname) \
747 static SH_MFHCls(hookname) ms_Inst; \
748 static ::SourceHook::MemFuncInfo ms_MFI; \
749 static ::SourceHook::IHookManagerInfo *ms_HI; \
750 static ::SourceHook::ProtoInfo ms_Proto; \
752 SH_MFHCls(hookname)() \
754 ms_MFI.isVirtual = true; \
755 ms_MFI.thisptroffs = pthisptroffs; \
756 ms_MFI.vtblindex = pvtblidx; \
757 ms_MFI.vtbloffs = pvtbloffs; \
759 static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
761 using namespace ::SourceHook; \
762 /* Verify interface version */ \
763 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
765 if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
771 MemFuncInfo mfi = {true, -1, 0, 0}; \
772 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
773 hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
774 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
779 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
781 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
782 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
783 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
784 int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
785 SH_MFHCls(hookname)::FD handler) \
787 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
788 iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
789 new SH_MFHCls(hookname)::CMyDelegateImpl(handler), post); \
791 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
792 SH_MFHCls(hookname)::FD handler) \
794 SH_MFHCls(hookname)::CMyDelegateImpl tmp(handler); \
795 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
796 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
799 #define SH_SETUPCALLS(rettype, paramtypes, params) \
801 using namespace ::SourceHook; \
802 void *ourvfnptr = reinterpret_cast<void*>( \
803 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
804 void *vfnptr_origentry; \
806 META_RES status = MRES_IGNORED; \
810 typedef ReferenceCarrier<rettype>::type my_rettype; \
811 my_rettype orig_ret; \
812 my_rettype override_ret; \
813 my_rettype plugin_ret; \
815 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
816 &vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
818 #define SH_CALL_HOOKS(post, params) \
819 prev_res = MRES_IGNORED; \
820 while ( iter = static_cast<IMyDelegate*>(pContext->GetNext()) ) \
822 cur_res = MRES_IGNORED; \
823 plugin_ret = iter->Call params; \
824 prev_res = cur_res; \
825 if (cur_res > status) \
827 if (cur_res >= MRES_OVERRIDE) \
828 *reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret; \
831 #define SH_CALL_ORIG(rettype, paramtypes, params) \
832 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
834 rettype (EmptyClass::*mfp)paramtypes; \
836 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
839 orig_ret = override_ret; /* :TODO: ??? : use pContext->GetOverrideRetPtr() or not? */
841 #define SH_RETURN() \
842 const my_rettype *retptr = reinterpret_cast<const my_rettype*>( \
843 (status >= MRES_OVERRIDE) ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()); \
844 SH_GLOB_SHPTR->EndContext(pContext); \
847 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
848 SH_SETUPCALLS(rettype, paramtypes, params) \
849 SH_CALL_HOOKS(pre, params) \
850 SH_CALL_ORIG(rettype, paramtypes, params) \
851 SH_CALL_HOOKS(post, params) \
854 //////////////////////////////////////////////////////////////////////////
855 #define SH_SETUPCALLS_void(paramtypes, params) \
857 using namespace ::SourceHook; \
858 void *ourvfnptr = reinterpret_cast<void*>( \
859 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
860 void *vfnptr_origentry; \
862 META_RES status = MRES_IGNORED; \
867 IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
868 &vfnptr_origentry, &status, &prev_res, &cur_res, NULL, NULL);
870 #define SH_CALL_HOOKS_void(post, params) \
871 prev_res = MRES_IGNORED; \
872 while ( iter = static_cast<IMyDelegate*>(pContext->GetNext()) ) \
874 cur_res = MRES_IGNORED; \
876 prev_res = cur_res; \
877 if (cur_res > status) \
881 #define SH_CALL_ORIG_void(paramtypes, params) \
882 if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
884 void (EmptyClass::*mfp)paramtypes; \
886 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
889 #define SH_RETURN_void() \
890 SH_GLOB_SHPTR->EndContext(pContext);
892 #define SH_HANDLEFUNC_void(paramtypes, params) \
893 SH_SETUPCALLS_void(paramtypes, params) \
894 SH_CALL_HOOKS_void(pre, params) \
895 SH_CALL_ORIG_void(paramtypes, params) \
896 SH_CALL_HOOKS_void(post, params) \
900 // Special vafmt handlers
902 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
903 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
904 SH_CALL_HOOKS(pre, params_plug) \
905 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
906 SH_CALL_HOOKS(post, params_plug) \
909 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
910 SH_SETUPCALLS_void(paramtypes, params_orig) \
911 SH_CALL_HOOKS_void(pre, params_plug) \
912 SH_CALL_ORIG_void(paramtypes, params_orig) \
913 SH_CALL_HOOKS_void(post, params_plug) \
916 //////////////////////////////////////////////////////////////////////////
918 #define MAKE_DELEG(ret_type, params_decl, params_pass) \
919 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual ret_type Call params_decl = 0; }; \
920 struct CMyDelegateImpl : IMyDelegate \
923 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
924 ret_type Call params_decl { return m_Deleg params_pass; } \
925 void DeleteThis() { delete this; } \
926 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
929 #define MAKE_DELEG_void(params_decl, params_pass) \
930 struct IMyDelegate : ::SourceHook::ISHDelegate { virtual void Call params_decl = 0; }; \
931 struct CMyDelegateImpl : IMyDelegate \
934 CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
935 void Call params_decl { m_Deleg params_pass; } \
936 void DeleteThis() { delete this; } \
937 bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
941 // sizeof on references returns the size of the datatype, NOT the pointer size or something
942 // -> one should probably flag references in __SourceHook_ParamSizes_* !
943 // or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
946 // ********* Support for $1 arguments *********
947 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
948 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
949 (&ifacetype::ifacefunc))) \
950 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
951 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
952 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
953 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
954 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
955 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
957 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
958 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
959 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
962 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
963 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
964 (&ifacetype::ifacefunc))) \
965 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
966 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
967 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
968 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
969 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
970 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
972 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
973 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
974 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
976 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
977 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
978 (&ifacetype::ifacefunc))) \
979 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
980 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
981 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@] @[$1!=0:, @]const char *fmt, ...) \
983 char buf[::SourceHook::STRBUF_LEN]; \
986 vsnprintf(buf, sizeof(buf), fmt, ap); \
988 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); \
990 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
991 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
993 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
994 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
995 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
997 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
998 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
999 (&ifacetype::ifacefunc))) \
1000 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
1001 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
1002 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1004 char buf[::SourceHook::STRBUF_LEN]; \
1006 va_start(ap, fmt); \
1007 vsnprintf(buf, sizeof(buf), fmt, ap); \
1009 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)); \
1011 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1012 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1014 const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1015 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
1016 __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
1018 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1019 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1020 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1021 MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1022 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1023 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1024 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1025 typedef rettype RetType; \
1026 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1028 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1029 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
1030 __SourceHook_ParamInfosM_##hookname, 0 };
1032 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1033 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1034 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1035 MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
1036 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1037 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1038 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1039 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1041 const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
1042 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
1043 __SourceHook_ParamInfosM_##hookname , 0 };
1048 //////////////////////////////////////////////////////////////////////////
1051 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1053 using namespace ::SourceHook; \
1055 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1056 RetType tmpret = (m_ThisPtr->*m_MFP)call; \
1057 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1061 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
1063 using namespace ::SourceHook; \
1065 m_pSH->SetIgnoreHooks(m_VfnPtr); \
1066 (m_ThisPtr->*m_MFP)call; \
1067 m_pSH->ResetIgnoreHooks(m_VfnPtr); \
1070 namespace SourceHook
1073 // Support for $1 arguments
1074 template<class ObjType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1081 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1082 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1084 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1085 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1088 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1089 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1093 template<class ObjType, class MFPType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFPType, void@[$2,1,$1:, Param$2@]>
1100 ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
1101 m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
1103 void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1104 SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1107 template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1108 SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1115 #define SH__CALL_GET_VFNPTR_NORMAL \
1116 using namespace ::SourceHook; \
1117 MemFuncInfo mfi = {true, -1, 0, 0}; \
1118 GetFuncInfo(ptr, mfp, mfi); \
1119 void *vfnptr = reinterpret_cast<void*>( \
1120 *reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
1122 #define SH__CALL_GET_VFNPTR_MANUAL \
1123 using namespace ::SourceHook; \
1124 void *vfnptr = reinterpret_cast<void*>( \
1125 *reinterpret_cast<void***>( (reinterpret_cast<char*>(ptr) + thisptroffs + vtbloffs) ) + vtblidx); \
1127 *reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
1129 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1130 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1131 // The only purpose of the mfp2 parameter is to extract the return type
1134 // Support for $1 arguments
1135 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1136 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1137 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr)
1139 SH__CALL_GET_VFNPTR_NORMAL
1140 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1143 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1144 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1145 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr)
1147 SH__CALL_GET_VFNPTR_NORMAL
1148 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1151 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1152 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
1153 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr)
1155 SH__CALL_GET_VFNPTR_MANUAL
1156 return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
1157 reinterpret_cast<SourceHook::EmptyClass*>(ptr), mfp, vfnptr, shptr);
1161 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1162 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1165 // Support for $1 arguments
1166 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1167 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1168 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr)
1170 SH__CALL_GET_VFNPTR_NORMAL
1171 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1174 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1175 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
1176 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr)
1178 SH__CALL_GET_VFNPTR_NORMAL
1179 return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
1186 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR)
1187 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR)
1188 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1189 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1191 #undef SH_MAKE_EXECUTABLECLASS_OB
1192 #undef SH_MAKE_EXECUTABLECLASS_OB_void
1194 //////////////////////////////////////////////////////////////////////////
1195 // SetOverrideRet and RecallGetIface for recalls
1196 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1197 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1198 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1200 namespace SourceHook
1202 template <class RetType>
1203 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1205 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1208 // SetOverrideResult used to be implemented like this:
1209 // SetOverrideResult(shptr, memfuncptr, return);
1210 // normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
1211 // (it thought it was ambigous - the ref and non-ref type)
1212 // so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
1213 // new syntax: SetOverrideResult(memfuncptr)(shptr, return)
1214 // This also allows us to create a special version for references which respects ReferenceCarrier.
1216 template <class T> struct OverrideFunctor
1218 void operator()(ISourceHook *shptr, T res)
1220 *reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
1223 template <class T> struct OverrideFunctor<T&>
1225 void operator()(ISourceHook *shptr, T &res)
1227 // overrideretptr points to ReferenceCarrier<T&>
1228 *reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
1232 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1233 OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1235 return OverrideFunctor<RetType>();
1238 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1239 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1241 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1247 // The pope is dead. -> :(