1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2005 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;
304 * @brief The main SourceHook interface
310 * @brief Return interface version
312 virtual int GetIfaceVersion() = 0;
315 * @brief Return implementation version
317 virtual int GetImplVersion() = 0;
322 * @return True if the function succeeded, false otherwise
324 * @param plug The unique identifier of the plugin that calls this function
325 * @param iface The interface pointer
326 * @param ifacesize The size of the class iface points to
327 * @param myHookMan A hook manager function that should be capable of handling the function
328 * @param handler A pointer to a FastDelegate containing the hook handler
329 * @param post Set to true if you want a post handler
331 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
332 ISHDelegate *handler, bool post) = 0;
335 * @brief Removes a hook.
337 * @return True if the function succeeded, false otherwise
339 * @param plug The unique identifier of the plugin that calls this function
340 * @param iface The interface pointer
341 * @param myHookMan A hook manager function that should be capable of handling the function
342 * @param handler A pointer to a FastDelegate containing the hook handler
343 * @param post Set to true if you want a post handler
345 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
346 ISHDelegate *handler, bool post) = 0;
349 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
351 * @param plug The unique identifier of the plugin in question
353 virtual bool IsPluginInUse(Plugin plug) = 0;
356 * @brief Return a pointer to a callclass. Generate a new one if required.
358 * @param iface The interface pointer
359 * @param size Size of the class instance
361 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
364 * @brief Release a callclass
366 * @param ptr Pointer to the callclass
368 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
370 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
371 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
372 //!< previously calledhandler
373 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
374 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
375 //!< If not in post function, undefined
376 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
377 //!< If none is specified, NULL
378 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
379 //////////////////////////////////////////////////////////////////////////
381 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
382 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
383 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
384 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
385 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
386 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
387 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
388 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
389 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
392 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
394 * @param plug The owner of the hook manager
395 * @param pubFunc The hook manager's info function
397 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
399 virtual void DoRecall() = 0; //!< Initiates a recall sequence
404 Users want the ability to change parameters of the called function
405 from inside their handler.
408 2) Recall the function
409 3) => SH's hook func gets called:
410 4) The first iterator points at the first hook the last hookfunc didn't execute yet
411 5) does all iteration and returns normally
412 6) The user's handler returns immediately
413 7) The hook func returns immediately as well
415 Also note that the recalled hookfunc starts with the status the recalling hookfunc
416 ended with. The last handler (doing the recall) is also able to specify its own
420 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
423 * @brief Set up the hook loop. Equivalent to calling:
424 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
426 * @param statusPtr pointer to status variable
427 * @param prevResPtr pointer to previous result variable
428 * @param curResPtr pointer to current result variable
429 * @param ifacePtrPtr pointer to interface this pointer variable
430 * @param origRetPr pointer to original return value variable. NULL for void funcs
431 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
433 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
434 * when the hook func is being called as part of a recall
436 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
437 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
443 /************************************************************************/
444 /* High level interface */
445 /************************************************************************/
446 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
447 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
448 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
449 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
450 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
452 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
453 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
454 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
457 // only call these from the hook handlers directly!
458 // :TODO: enforce it ?
460 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
461 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
463 // SourceHook::SetOverrideRet is defined later.
464 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
466 SET_META_RESULT(result); \
467 SH_GLOB_SHPTR->DoRecall(); \
468 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
469 RETURN_META(MRES_SUPERCEDE); \
472 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
474 SET_META_RESULT(result); \
475 SH_GLOB_SHPTR->DoRecall(); \
476 if ((result) >= MRES_OVERRIDE) \
478 /* meh, set the override result here because we don't get a chance to return */ \
479 /* before continuing the hook loop through the recall */ \
480 SourceHook::SetOverrideResult(SH_GLOB_SHPTR, memfuncptr, value); \
482 RETURN_META_VALUE(MRES_SUPERCEDE, \
483 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
486 // :TODO: thisptroffs in MNEWPARAMS ??
488 #if SH_COMP == SH_COMP_MSVC
490 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
493 SH_MFHCls(hookname)::ECMFP mfp; \
496 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
497 u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
498 SH_MFHCls(hookname)::ms_MFI.vtblindex];
500 #elif SH_COMP == SH_COMP_GCC
502 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
505 SH_MFHCls(hookname)::ECMFP mfp; \
512 SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
513 u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
514 SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
519 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
521 SET_META_RESULT(result); \
522 SH_GLOB_SHPTR->DoRecall(); \
523 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
524 (thisptr->*(u.mfp)) newparams; \
525 RETURN_META(MRES_SUPERCEDE); \
528 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
530 SET_META_RESULT(result); \
531 SH_GLOB_SHPTR->DoRecall(); \
532 if ((result) >= MRES_OVERRIDE) \
534 /* see RETURN_META_VALUE_NEWPARAMS */ \
535 SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
537 SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
538 RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
542 * @brief Get/generate callclass for an interface pointer
544 * @param ifaceptr The interface pointer
546 template<class ifacetype>
547 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
549 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
550 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
553 template<class ifacetype>
554 inline SourceHook::CallClass<ifacetype> *SH_GET_MCALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr, int ifacesize)
556 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
557 shptr->GetCallClass(reinterpret_cast<void*>(ptr), ifacesize));
560 template<class ifacetype>
561 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
563 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
566 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
568 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
569 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
570 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
571 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
574 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
575 #define SH_GET_MCALLCLASS(ptr, size) SH_GET_MCALLCLASS_R(SH_GLOB_SHPTR, reinterpret_cast<SourceHook::EmptyClass*>(ptr), size)
576 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
578 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
579 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
581 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
582 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
583 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
584 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
586 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
587 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
589 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
590 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
591 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
592 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
595 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
596 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
597 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
598 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
599 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
600 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
602 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
603 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
604 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
605 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
606 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
607 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
615 #if SH_COMP == SH_COMP_MSVC
616 # define SH_SETUP_MFP(mfp) \
617 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
618 #elif SH_COMP == SH_COMP_GCC
619 # define SH_SETUP_MFP(mfp) \
620 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
621 reinterpret_cast<void**>(&mfp)[1] = 0;
623 # error Not supported yet.
626 //////////////////////////////////////////////////////////////////////////
627 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
628 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
630 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
631 SH_FHCls(ifacetype,ifacefunc,overload)() \
633 GetFuncInfo(funcptr, ms_MFI); \
636 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
638 using namespace ::SourceHook; \
639 GetFuncInfo(funcptr, ms_MFI); \
640 /* Verify interface version */ \
641 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
644 if (action == HA_GetInfo) \
646 param->SetVersion(SH_HOOKMAN_VERSION); \
647 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
648 reinterpret_cast<const char*>(&ms_Proto)); \
651 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
652 param->SetHookfuncVfnptr( \
653 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
656 else if (action == HA_Register) \
661 else if (action == HA_Unregister) \
670 // It has to be possible to use the macros in namespaces
671 // -> So we need to access and extend the global SourceHook namespace
672 // We use a namespace alias for this
673 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
674 struct SH_FHCls(ifacetype,ifacefunc,overload) \
676 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
677 static ::SourceHook::MemFuncInfo ms_MFI; \
678 static ::SourceHook::IHookManagerInfo *ms_HI; \
679 static ::SourceHook::ProtoInfo ms_Proto; \
680 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
682 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
684 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
685 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
686 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
687 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
688 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
690 using namespace ::SourceHook; \
692 GetFuncInfo(funcptr, mfi); \
693 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
694 return false; /* No non-virtual functions / virtual inheritance supported */ \
696 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
697 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
698 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
700 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
701 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
703 using namespace ::SourceHook; \
705 GetFuncInfo(funcptr, mfi); \
706 if (mfi.thisptroffs < 0) \
707 return false; /* No virtual inheritance supported */ \
709 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
710 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
711 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
714 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
715 struct SH_MFHCls(hookname) \
717 static SH_MFHCls(hookname) ms_Inst; \
718 static ::SourceHook::MemFuncInfo ms_MFI; \
719 static ::SourceHook::IHookManagerInfo *ms_HI; \
720 static ::SourceHook::ProtoInfo ms_Proto; \
722 SH_MFHCls(hookname)() \
724 ms_MFI.isVirtual = true; \
725 ms_MFI.thisptroffs = pthisptroffs; \
726 ms_MFI.vtblindex = pvtblidx; \
727 ms_MFI.vtbloffs = pvtbloffs; \
729 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
731 using namespace ::SourceHook; \
732 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
733 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
734 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
735 /* Verify interface version */ \
736 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
739 if (action == HA_GetInfo) \
741 param->SetVersion(SH_HOOKMAN_VERSION); \
742 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
743 reinterpret_cast<const char*>(&ms_Proto)); \
746 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
747 param->SetHookfuncVfnptr( \
748 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
751 else if (action == HA_Register) \
756 else if (action == HA_Unregister) \
765 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
767 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
768 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
769 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
770 bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
771 SH_MFHCls(hookname)::FD handler) \
773 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
774 SH_MFHCls(hookname)::HookManPubFunc, \
775 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
777 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
778 SH_MFHCls(hookname)::FD handler) \
780 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
781 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
782 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
786 #define SH_SETUPCALLS(rettype, paramtypes, params) \
787 /* 1) Find the vfnptr */ \
788 using namespace ::SourceHook; \
789 void *ourvfnptr = reinterpret_cast<void*>( \
790 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
791 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
792 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
794 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
795 /* ... and the iface */ \
796 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
799 /* The iface info was not found. Redirect the call to the original function. */ \
800 rettype (EmptyClass::*mfp)paramtypes; \
802 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
804 /* 2) Declare some vars and set it up */ \
805 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
806 IHookList *prelist = ifinfo->GetPreHooks(); \
807 IHookList *postlist = ifinfo->GetPostHooks(); \
808 META_RES status = MRES_IGNORED; \
812 rettype override_ret; \
813 rettype plugin_ret; \
815 rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
816 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
818 #define SH_CALL_HOOKS(post, params) \
819 if (SH_GLOB_SHPTR->ShouldContinue()) \
821 prev_res = MRES_IGNORED; \
822 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
824 cur_res = MRES_IGNORED; \
825 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
826 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
827 prev_res = cur_res; \
828 if (cur_res > status) \
830 if (cur_res >= MRES_OVERRIDE) \
831 *pOverrideRet = plugin_ret; \
832 if (!SH_GLOB_SHPTR->ShouldContinue()) \
840 #define SH_CALL_ORIG(rettype, paramtypes, params) \
841 if (status != MRES_SUPERCEDE) \
843 rettype (EmptyClass::*mfp)paramtypes; \
845 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
848 orig_ret = override_ret; \
850 #define SH_RETURN() \
851 SH_GLOB_SHPTR->HookLoopEnd(); \
852 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
854 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
855 SH_SETUPCALLS(rettype, paramtypes, params) \
856 SH_CALL_HOOKS(pre, params) \
857 SH_CALL_ORIG(rettype, paramtypes, params) \
858 SH_CALL_HOOKS(post, params) \
861 //////////////////////////////////////////////////////////////////////////
862 #define SH_SETUPCALLS_void(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 void (EmptyClass::*mfp)paramtypes; \
878 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
881 /* 2) Declare some vars and set it up */ \
882 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
883 IHookList *prelist = ifinfo->GetPreHooks(); \
884 IHookList *postlist = ifinfo->GetPostHooks(); \
885 META_RES status = MRES_IGNORED; \
889 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
891 #define SH_CALL_HOOKS_void(post, params) \
892 if (SH_GLOB_SHPTR->ShouldContinue()) \
894 prev_res = MRES_IGNORED; \
895 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
897 cur_res = MRES_IGNORED; \
898 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
899 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
900 prev_res = cur_res; \
901 if (cur_res > status) \
903 if (!SH_GLOB_SHPTR->ShouldContinue()) \
911 #define SH_CALL_ORIG_void(paramtypes, params) \
912 if (status != MRES_SUPERCEDE) \
914 void (EmptyClass::*mfp)paramtypes; \
916 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
919 #define SH_RETURN_void() \
920 SH_GLOB_SHPTR->HookLoopEnd();
922 #define SH_HANDLEFUNC_void(paramtypes, params) \
923 SH_SETUPCALLS_void(paramtypes, params) \
924 SH_CALL_HOOKS_void(pre, params) \
925 SH_CALL_ORIG_void(paramtypes, params) \
926 SH_CALL_HOOKS_void(post, params) \
930 // Special vafmt handlers
932 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
933 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
934 SH_CALL_HOOKS(pre, params_plug) \
935 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
936 SH_CALL_HOOKS(post, params_plug) \
939 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
940 SH_SETUPCALLS_void(paramtypes, params_orig) \
941 SH_CALL_HOOKS_void(pre, params_plug) \
942 SH_CALL_ORIG_void(paramtypes, params_orig) \
943 SH_CALL_HOOKS_void(post, params_plug) \
946 //////////////////////////////////////////////////////////////////////////
949 // ********* Support for $1 arguments *********
950 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
951 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
952 (&ifacetype::ifacefunc))) \
953 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
954 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
955 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
956 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
957 (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
959 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
960 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
961 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
963 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
964 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
965 (&ifacetype::ifacefunc))) \
966 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
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))) \
973 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
974 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
975 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
977 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
978 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
979 (&ifacetype::ifacefunc))) \
980 typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
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 int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
994 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
995 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
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 virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
1003 char buf[::SourceHook::STRBUF_LEN]; \
1005 va_start(ap, fmt); \
1006 vsnprintf(buf, sizeof(buf), fmt, ap); \
1008 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)); \
1010 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
1011 (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
1013 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
1014 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
1015 $1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
1017 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
1018 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1019 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
1020 virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
1021 { SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
1022 typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1023 typedef rettype RetType; \
1024 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1026 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1027 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
1028 $1, __SourceHook_ParamSizesM_##hookname); \
1030 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
1031 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1032 typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
1033 virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
1034 { SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
1035 typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
1036 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
1038 const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
1039 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
1040 $1, __SourceHook_ParamSizesM_##hookname); \
1045 //////////////////////////////////////////////////////////////////////////
1048 #if SH_COMP == SH_COMP_MSVC
1050 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1052 using namespace ::SourceHook; \
1054 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
1055 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
1057 return (m_CC->GetThisPtr()->*m_MFP)call; \
1059 /* It's hooked. Call the original function. */ \
1062 RetType(EmptyClass::*mfpnew)prms; \
1065 u.addr = origfunc; \
1067 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
1068 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
1071 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
1073 using namespace ::SourceHook; \
1074 char *adjustedthisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()) + m_ThisPtrOffs; \
1077 RetType(EmptyClass::*mfpnew)prms; \
1080 u.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
1082 u.addr = (*reinterpret_cast<void***>(adjustedthisptr + m_VtblOffs))[m_VtblIdx]; \
1084 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
1087 #elif SH_COMP == SH_COMP_GCC
1089 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1091 using namespace ::SourceHook; \
1093 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
1094 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
1096 return (m_CC->GetThisPtr()->*m_MFP)call; \
1098 /* It's hooked. Call the original function. */ \
1101 RetType(EmptyClass::*mfpnew)prms; \
1105 intptr_t adjustor; \
1108 u.s.addr = origfunc; \
1109 u.s.adjustor = mfi.thisptroffs; \
1111 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
1114 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
1116 using namespace ::SourceHook; \
1117 char *thisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()); \
1120 RetType(EmptyClass::*mfpnew)prms; \
1123 intptr_t adjustor; \
1126 u.s.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
1128 u.s.addr = (*reinterpret_cast<void***>(thisptr + m_ThisPtrOffs + m_VtblOffs))[m_VtblIdx]; \
1130 u.s.adjustor = m_ThisPtrOffs; \
1131 return (reinterpret_cast<EmptyClass*>(thisptr)->*u.mfpnew)call; \
1136 namespace SourceHook
1139 // Support for $1 arguments
1140 template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
1145 ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
1147 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1148 SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1151 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1152 SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1156 template <class RetType@[$2,1,$1:, class Param$2@]> class MExecutableClass$1
1158 ManualCallClass *m_CC;
1163 MExecutableClass$1(ManualCallClass *cc, int vtbloffs, int vtblidx, int thisptroffs) : m_CC(cc),
1164 m_ThisPtrOffs(thisptroffs), m_VtblIdx(vtblidx), m_VtblOffs(vtbloffs) { }
1166 RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
1167 SH_MAKE_MEXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
1170 template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
1171 SH_MAKE_MEXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
1178 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1179 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1180 // The only purpose of the mfp2 parameter is to extract the return type
1183 // Support for $1 arguments
1184 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1185 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1186 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
1188 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1191 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1192 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1193 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
1195 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1198 template <class X, class RetType@[$2,1,$1:, class Param$2@]>
1199 SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>
1200 SH_MCALL2(SourceHook::ManualCallClass *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
1202 return SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>(ptr, vtbloffs, vtblidx, thisptroffs);
1206 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1207 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1210 // Support for $1 arguments
1211 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1212 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1213 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
1215 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1218 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
1219 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
1220 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
1222 return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
1229 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
1230 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
1231 SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
1233 #undef SH_MAKE_EXECUTABLECLASS_OB
1235 //////////////////////////////////////////////////////////////////////////
1236 // SetOverrideRet and RecallGetIface for recalls
1237 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1238 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1239 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1241 namespace SourceHook
1243 template <class RetType>
1244 void SetOverrideResult(ISourceHook *shptr, const RetType res)
1246 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1250 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1251 void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]), const RetType res)
1253 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1256 template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
1257 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
1259 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1265 // The pope is dead. -> :(