One bugfix, addition of META_RETURN_(VALUE_)NEWPARAMS (implemented using recalls), tests now support compiling against an older version of sourcehook.h (through the sourcehook_test.h file) while main.cpp which now also acts as a CSourceHookImpl factory compiles against the new version.
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"
25 #define SH_IFACE_VERSION 4
26 #define SH_IMPL_VERSION 3
28 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
29 // It's used in various macros
31 #define SH_GLOB_SHPTR g_SHPtr
34 // Used to identify the current plugin
35 #ifndef SH_GLOB_PLUGPTR
36 #define SH_GLOB_PLUGPTR g_PLID
43 # define SH_ASSERT(x, info) \
47 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
55 # define SH_ASSERT(x, info)
59 #define SH_SYS_WIN32 1
60 #define SH_SYS_LINUX 2
63 # define SH_SYS SH_SYS_WIN32
64 #elif defined __linux__
65 # define SH_SYS SH_SYS_LINUX
67 # error Unsupported system
72 #define SH_COMP_MSVC 2
75 # define SH_COMP SH_COMP_MSVC
76 #elif defined __GNUC__
77 # define SH_COMP SH_COMP_GCC
79 # error Unsupported compiler
82 #if SH_COMP==SH_COMP_MSVC
83 # define vsnprintf _vsnprintf
86 #define SH_PTRSIZE sizeof(void*)
88 #include "FastDelegate.h"
89 #include "sh_memfuncinfo.h"
93 // Flags returned by a plugin's api function.
94 // NOTE: order is crucial, as greater/less comparisons are made.
97 MRES_IGNORED=0, // plugin didn't take any action
98 MRES_HANDLED, // plugin did something, but real function should still be called
99 MRES_OVERRIDE, // call real function, but use my return value
100 MRES_SUPERCEDE // skip real function; use my return value
107 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
109 const int STRBUF_LEN=4096;
112 * @brief An empty class. No inheritance used. Used for original-function-call hacks
119 * @brief Implicit cast.
121 template <class In, class Out>
122 inline Out implicit_cast(In input)
128 * @brief A plugin typedef
130 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
131 * plugin has a unique identifier, everything is ok.
132 * It should also be possible to set it to 0.
138 ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
141 int beginningNull; //!< To distinguish from old protos (which never begin with 0)
142 int retTypeSize; //!< 0 if void
143 int numOfParams; //!< number of parameters
144 const int *params; //!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
148 * @brief Specifies the actions for hook managers
150 enum HookManagerAction
152 HA_GetInfo = 0, //!< Store info about the hook manager
153 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
154 HA_Unregister //!< Clear the saved pointer
157 struct IHookManagerInfo;
160 * @brief Pointer to hook manager interface function
162 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
164 * @param ha What the hook manager should do
165 * @param hi A pointer to IHookManagerInfo
167 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
172 virtual void DeleteThis() = 0; // Ugly, I know
173 virtual bool IsEqual(ISHDelegate *other) = 0;
176 template <class T> class CSHDelegate : public ISHDelegate
180 CSHDelegate(T deleg) : m_Deleg(deleg)
184 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
193 bool IsEqual(ISHDelegate *other)
195 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
208 virtual bool End() = 0;
209 virtual void Next() = 0;
210 virtual ISHDelegate *Handler() = 0;
211 virtual int ThisPtrOffs() = 0;
213 virtual IIter *GetIter() = 0;
214 virtual void ReleaseIter(IIter *pIter) = 0;
219 virtual void *GetPtr() = 0;
220 virtual IHookList *GetPreHooks() = 0;
221 virtual IHookList *GetPostHooks() = 0;
226 virtual void *GetVfnPtr() = 0;
227 virtual void *GetOrigEntry() = 0;
229 virtual IIface *FindIface(void *ptr) = 0;
232 struct IHookManagerInfo
234 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
236 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
237 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
243 IHookList::IIter *m_pIter;
245 AutoHookIter(IHookList *pList)
246 : m_pList(pList), m_pIter(pList->GetIter())
253 m_pList->ReleaseIter(m_pIter);
258 return m_pIter->End();
266 ISHDelegate *Handler()
268 return m_pIter->Handler();
273 return m_pIter->ThisPtrOffs();
282 template<class B> struct CallClass
284 virtual B *GetThisPtr() = 0;
285 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
288 typedef CallClass<void> GenericCallClass;
291 * @brief The main SourceHook interface
297 * @brief Return interface version
299 virtual int GetIfaceVersion() = 0;
302 * @brief Return implementation version
304 virtual int GetImplVersion() = 0;
309 * @return True if the function succeeded, false otherwise
311 * @param plug The unique identifier of the plugin that calls this function
312 * @param iface The interface pointer
313 * @param ifacesize The size of the class iface points to
314 * @param myHookMan A hook manager function that should be capable of handling the function
315 * @param handler A pointer to a FastDelegate containing the hook handler
316 * @param post Set to true if you want a post handler
318 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
319 ISHDelegate *handler, bool post) = 0;
322 * @brief Removes a hook.
324 * @return True if the function succeeded, false otherwise
326 * @param plug The unique identifier of the plugin that calls this function
327 * @param iface The interface pointer
328 * @param myHookMan A hook manager function that should be capable of handling the function
329 * @param handler A pointer to a FastDelegate containing the hook handler
330 * @param post Set to true if you want a post handler
332 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
333 ISHDelegate *handler, bool post) = 0;
336 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
338 * @param plug The unique identifier of the plugin in question
340 virtual bool IsPluginInUse(Plugin plug) = 0;
343 * @brief Return a pointer to a callclass. Generate a new one if required.
345 * @param iface The interface pointer
346 * @param size Size of the class instance
348 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
351 * @brief Release a callclass
353 * @param ptr Pointer to the callclass
355 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
357 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
358 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
359 //!< previously calledhandler
360 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
361 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
362 //!< If not in post function, undefined
363 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
364 //!< If none is specified, NULL
365 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
366 //////////////////////////////////////////////////////////////////////////
368 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
369 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
370 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
371 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
372 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
373 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
374 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
375 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
376 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
379 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
381 * @param plug The owner of the hook manager
382 * @param pubFunc The hook manager's info function
384 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
386 virtual void DoRecall() = 0; //!< Initiates a recall sequence
391 Users want the ability to change parameters of the called function
392 from inside their handler.
395 2) Recall the function
396 3) => SH's hook func gets called:
397 4) The first iterator points at the first hook the last hookfunc didn't execute yet
398 5) does all iteration and returns normally
399 6) The user's handler returns immediately
400 7) The hook func returns immediately as well
402 Also note that the recalled hookfunc starts with the status the recalling hookfunc
403 ended with. The last handler (doing the recall) is also able to specify its own
407 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
410 * @brief Set up the hook loop. Equivalent to calling:
411 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
413 * @param statusPtr pointer to status variable
414 * @param prevResPtr pointer to previous result variable
415 * @param curResPtr pointer to current result variable
416 * @param ifacePtrPtr pointer to interface this pointer variable
417 * @param origRetPr pointer to original return value variable. NULL for void funcs
418 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
420 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
421 * when the hook func is being called as part of a recall
423 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
424 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
428 /************************************************************************/
429 /* High level interface */
430 /************************************************************************/
431 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
432 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
433 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
434 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
435 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
437 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
438 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
439 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
442 // NEVER-EVER call these from post hooks!
443 // also, only call it from the hook handlers directly!
445 // :TODO: problems with SetOverrideResult and overloaded iface::func ?
447 // SourceHook::SetOverrideRet is defined later.
448 #define RETURN_META_NEWPARAMS(result, iface, func, newparams) \
450 SET_META_RESULT(result); \
451 SH_GLOB_SHPTR->DoRecall(); \
452 META_IFACEPTR(iface)->func newparams; \
453 RETURN_META(MRES_SUPERCEDE); \
456 #define RETURN_META_VALUE_NEWPARAMS(result, value, iface, func, newparams) \
458 SET_META_RESULT(result); \
459 SH_GLOB_SHPTR->DoRecall(); \
460 if ((result) >= MRES_OVERRIDE) \
462 /* meh, set the override result here because we don't get a chance to return */ \
463 /* before continuing the hook loop through the recall */ \
464 SourceHook::SetOverrideResult(SH_GLOB_SHPTR, &iface::func, value); \
466 RETURN_META_VALUE(MRES_SUPERCEDE, META_IFACEPTR(iface)->func newparams); \
470 * @brief Get/generate callclass for an interface pointer
472 * @param ifaceptr The interface pointer
474 template<class ifacetype>
475 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
477 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
478 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
481 template<class ifacetype>
482 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
484 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
487 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
489 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
490 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
491 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
492 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
495 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
496 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
498 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
499 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
501 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
502 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
503 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
504 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
506 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
507 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
509 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
510 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
511 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
512 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
515 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
516 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
517 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
518 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
519 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
520 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
522 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
523 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
524 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
525 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
526 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
527 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
535 #if SH_COMP == SH_COMP_MSVC
536 # define SH_SETUP_MFP(mfp) \
537 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
538 #elif SH_COMP == SH_COMP_GCC
539 # define SH_SETUP_MFP(mfp) \
540 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
541 reinterpret_cast<void**>(&mfp)[1] = 0;
543 # error Not supported yet.
546 //////////////////////////////////////////////////////////////////////////
547 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
548 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
550 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
551 SH_FHCls(ifacetype,ifacefunc,overload)() \
553 GetFuncInfo(funcptr, ms_MFI); \
556 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
558 using namespace ::SourceHook; \
559 GetFuncInfo(funcptr, ms_MFI); \
560 /* Verify interface version */ \
561 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
564 if (action == HA_GetInfo) \
566 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
567 reinterpret_cast<const char*>(&ms_Proto)); \
570 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
571 param->SetHookfuncVfnptr( \
572 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
575 else if (action == HA_Register) \
580 else if (action == HA_Unregister) \
589 // It has to be possible to use the macros in namespaces
590 // -> So we need to access and extend the global SourceHook namespace
591 // We use a namespace alias for this
592 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
593 struct SH_FHCls(ifacetype,ifacefunc,overload) \
595 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
596 static ::SourceHook::MemFuncInfo ms_MFI; \
597 static ::SourceHook::IHookManagerInfo *ms_HI; \
598 static ::SourceHook::ProtoInfo ms_Proto; \
599 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
601 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
603 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
604 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
605 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
606 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
607 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
609 using namespace ::SourceHook; \
611 GetFuncInfo(funcptr, mfi); \
612 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
613 return false; /* No non-virtual functions / virtual inheritance supported */ \
615 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
616 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
617 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
619 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
620 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
622 using namespace ::SourceHook; \
624 GetFuncInfo(funcptr, mfi); \
625 if (mfi.thisptroffs < 0) \
626 return false; /* No virtual inheritance supported */ \
628 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
629 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
630 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
633 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
634 struct SH_MFHCls(hookname) \
636 static SH_MFHCls(hookname) ms_Inst; \
637 static ::SourceHook::MemFuncInfo ms_MFI; \
638 static ::SourceHook::IHookManagerInfo *ms_HI; \
639 static ::SourceHook::ProtoInfo ms_Proto; \
641 SH_MFHCls(hookname)() \
643 ms_MFI.isVirtual = true; \
644 ms_MFI.thisptroffs = pthisptroffs; \
645 ms_MFI.vtblindex = pvtblidx; \
646 ms_MFI.vtbloffs = pvtbloffs; \
648 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
650 using namespace ::SourceHook; \
651 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
652 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
653 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
654 /* Verify interface version */ \
655 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
658 if (action == HA_GetInfo) \
660 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
661 reinterpret_cast<const char*>(&ms_Proto)); \
664 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
665 param->SetHookfuncVfnptr( \
666 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
669 else if (action == HA_Register) \
674 else if (action == HA_Unregister) \
683 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
685 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
686 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
687 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
688 bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
689 SH_MFHCls(hookname)::FD handler) \
691 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
692 SH_MFHCls(hookname)::HookManPubFunc, \
693 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
695 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
696 SH_MFHCls(hookname)::FD handler) \
698 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
699 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
700 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
704 #define SH_SETUPCALLS(rettype, paramtypes, params) \
705 /* 1) Find the vfnptr */ \
706 using namespace ::SourceHook; \
707 void *ourvfnptr = reinterpret_cast<void*>( \
708 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
709 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
710 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
712 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
713 /* ... and the iface */ \
714 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
717 /* The iface info was not found. Redirect the call to the original function. */ \
718 rettype (EmptyClass::*mfp)paramtypes; \
720 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
722 /* 2) Declare some vars and set it up */ \
723 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
724 IHookList *prelist = ifinfo->GetPreHooks(); \
725 IHookList *postlist = ifinfo->GetPostHooks(); \
726 META_RES status = MRES_IGNORED; \
730 rettype override_ret; \
731 rettype plugin_ret; \
733 rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
734 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
736 #define SH_CALL_HOOKS(post, params) \
737 if (SH_GLOB_SHPTR->ShouldContinue()) \
739 prev_res = MRES_IGNORED; \
740 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
742 cur_res = MRES_IGNORED; \
743 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
744 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
745 prev_res = cur_res; \
746 if (cur_res > status) \
748 if (cur_res >= MRES_OVERRIDE) \
749 *pOverrideRet = plugin_ret; \
750 if (!SH_GLOB_SHPTR->ShouldContinue()) \
758 #define SH_CALL_ORIG(rettype, paramtypes, params) \
759 if (status != MRES_SUPERCEDE) \
761 rettype (EmptyClass::*mfp)paramtypes; \
763 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
766 orig_ret = override_ret; \
768 #define SH_RETURN() \
769 SH_GLOB_SHPTR->HookLoopEnd(); \
770 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
772 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
773 SH_SETUPCALLS(rettype, paramtypes, params) \
774 SH_CALL_HOOKS(pre, params) \
775 SH_CALL_ORIG(rettype, paramtypes, params) \
776 SH_CALL_HOOKS(post, params) \
779 //////////////////////////////////////////////////////////////////////////
780 #define SH_SETUPCALLS_void(paramtypes, params) \
781 /* 1) Find the vfnptr */ \
782 using namespace ::SourceHook; \
783 void *ourvfnptr = reinterpret_cast<void*>( \
784 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
785 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
786 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
788 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
789 /* ... and the iface */ \
790 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
793 /* The iface info was not found. Redirect the call to the original function. */ \
794 void (EmptyClass::*mfp)paramtypes; \
796 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
799 /* 2) Declare some vars and set it up */ \
800 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
801 IHookList *prelist = ifinfo->GetPreHooks(); \
802 IHookList *postlist = ifinfo->GetPostHooks(); \
803 META_RES status = MRES_IGNORED; \
807 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
809 #define SH_CALL_HOOKS_void(post, params) \
810 if (SH_GLOB_SHPTR->ShouldContinue()) \
812 prev_res = MRES_IGNORED; \
813 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
815 cur_res = MRES_IGNORED; \
816 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
817 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
818 prev_res = cur_res; \
819 if (cur_res > status) \
821 if (!SH_GLOB_SHPTR->ShouldContinue()) \
829 #define SH_CALL_ORIG_void(paramtypes, params) \
830 if (status != MRES_SUPERCEDE) \
832 void (EmptyClass::*mfp)paramtypes; \
834 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
837 #define SH_RETURN_void() \
838 SH_GLOB_SHPTR->HookLoopEnd();
840 #define SH_HANDLEFUNC_void(paramtypes, params) \
841 SH_SETUPCALLS_void(paramtypes, params) \
842 SH_CALL_HOOKS_void(pre, params) \
843 SH_CALL_ORIG_void(paramtypes, params) \
844 SH_CALL_HOOKS_void(post, params) \
848 // Special vafmt handlers
850 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
851 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
852 SH_CALL_HOOKS(pre, params_plug) \
853 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
854 SH_CALL_HOOKS(post, params_plug) \
857 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
858 SH_SETUPCALLS_void(paramtypes, params_orig) \
859 SH_CALL_HOOKS_void(pre, params_plug) \
860 SH_CALL_ORIG_void(paramtypes, params_orig) \
861 SH_CALL_HOOKS_void(post, params_plug) \
864 //////////////////////////////////////////////////////////////////////////
867 // ********* Support for @$@ arguments *********
868 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
869 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
870 (&ifacetype::ifacefunc))) \
871 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
872 virtual rettype Func(@param%% p%%|, @) \
873 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
874 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
875 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
877 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
878 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
879 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
881 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
882 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
883 (&ifacetype::ifacefunc))) \
884 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
885 virtual void Func(@param%% p%%|, @) \
886 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
887 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
888 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
891 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
892 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
893 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
895 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
896 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
897 (&ifacetype::ifacefunc))) \
898 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
899 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
901 char buf[::SourceHook::STRBUF_LEN]; \
904 vsnprintf(buf, sizeof(buf), fmt, ap); \
906 SH_HANDLEFUNC_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
908 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
909 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
911 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
912 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
913 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
915 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
916 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
917 (&ifacetype::ifacefunc))) \
918 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
919 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
921 char buf[::SourceHook::STRBUF_LEN]; \
924 vsnprintf(buf, sizeof(buf), fmt, ap); \
926 SH_HANDLEFUNC_void_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
928 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
929 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
931 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
932 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
933 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
935 #define SH_DECL_MANUALHOOK@$@(hookname, vtblidx, vtbloffs, thisptroffs, rettype@, param%%@) \
936 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
937 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
938 virtual rettype Func(@param%% p%%|, @) \
939 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
940 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
942 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
943 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
944 @$@, __SourceHook_ParamSizesM_##hookname);
946 #define SH_DECL_MANUALHOOK@$@_void(hookname, vtblidx, vtbloffs, thisptroffs@, param%%@) \
947 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
948 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
949 virtual void Func(@param%% p%%|, @) \
950 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
951 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
953 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
954 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
955 @$@, __SourceHook_ParamSizesM_##hookname);
960 //////////////////////////////////////////////////////////////////////////
963 #if SH_COMP == SH_COMP_MSVC
965 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
967 using namespace ::SourceHook; \
969 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
970 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
972 return (m_CC->GetThisPtr()->*m_MFP)call; \
974 /* It's hooked. Call the original function. */ \
977 RetType(EmptyClass::*mfpnew)prms; \
982 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
983 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
986 #elif SH_COMP == SH_COMP_GCC
988 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
990 using namespace ::SourceHook; \
992 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
993 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
995 return (m_CC->GetThisPtr()->*m_MFP)call; \
997 /* It's hooked. Call the original function. */ \
1000 RetType(EmptyClass::*mfpnew)prms; \
1004 intptr_t adjustor; \
1007 u.s.addr = origfunc; \
1008 u.s.adjustor = mfi.thisptroffs; \
1010 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
1015 namespace SourceHook
1017 template<class CCType, class RetType, class MFPType> class ExecutableClass
1022 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
1027 // Support for @$@ arguments
1028 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
1029 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
1035 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1036 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1037 // The only purpose of the mfp2 parameter is to extract the return type
1040 // Support for @$@ arguments
1041 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1042 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1043 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
1045 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1048 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1049 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1050 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
1052 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1057 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1058 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1061 // Support for @$@ arguments
1062 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1063 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1064 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
1066 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1069 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1070 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1071 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
1073 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1080 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
1082 #undef SH_MAKE_EXECUTABLECLASS_OB
1084 //////////////////////////////////////////////////////////////////////////
1085 // SetOverrideRet for recalls
1086 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1087 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1088 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1090 namespace SourceHook
1093 template <class Iface, class RetType@, class Param%%@>
1094 void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@Param%%|, @), const RetType res)
1096 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1102 // The pope is dead. -> :(