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;
303 * @brief The main SourceHook interface
309 * @brief Return interface version
311 virtual int GetIfaceVersion() = 0;
314 * @brief Return implementation version
316 virtual int GetImplVersion() = 0;
321 * @return True if the function succeeded, false otherwise
323 * @param plug The unique identifier of the plugin that calls this function
324 * @param iface The interface pointer
325 * @param ifacesize The size of the class iface points to
326 * @param myHookMan A hook manager function that should be capable of handling the function
327 * @param handler A pointer to a FastDelegate containing the hook handler
328 * @param post Set to true if you want a post handler
330 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
331 ISHDelegate *handler, bool post) = 0;
334 * @brief Removes a hook.
336 * @return True if the function succeeded, false otherwise
338 * @param plug The unique identifier of the plugin that calls this function
339 * @param iface The interface pointer
340 * @param myHookMan A hook manager function that should be capable of handling the function
341 * @param handler A pointer to a FastDelegate containing the hook handler
342 * @param post Set to true if you want a post handler
344 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
345 ISHDelegate *handler, bool post) = 0;
348 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
350 * @param plug The unique identifier of the plugin in question
352 virtual bool IsPluginInUse(Plugin plug) = 0;
355 * @brief Return a pointer to a callclass. Generate a new one if required.
357 * @param iface The interface pointer
358 * @param size Size of the class instance
360 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
363 * @brief Release a callclass
365 * @param ptr Pointer to the callclass
367 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
369 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
370 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
371 //!< previously calledhandler
372 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
373 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
374 //!< If not in post function, undefined
375 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
376 //!< If none is specified, NULL
377 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
378 //////////////////////////////////////////////////////////////////////////
380 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
381 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
382 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
383 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
384 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
385 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
386 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
387 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
388 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
391 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
393 * @param plug The owner of the hook manager
394 * @param pubFunc The hook manager's info function
396 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
398 virtual void DoRecall() = 0; //!< Initiates a recall sequence
403 Users want the ability to change parameters of the called function
404 from inside their handler.
407 2) Recall the function
408 3) => SH's hook func gets called:
409 4) The first iterator points at the first hook the last hookfunc didn't execute yet
410 5) does all iteration and returns normally
411 6) The user's handler returns immediately
412 7) The hook func returns immediately as well
414 Also note that the recalled hookfunc starts with the status the recalling hookfunc
415 ended with. The last handler (doing the recall) is also able to specify its own
419 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
422 * @brief Set up the hook loop. Equivalent to calling:
423 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
425 * @param statusPtr pointer to status variable
426 * @param prevResPtr pointer to previous result variable
427 * @param curResPtr pointer to current result variable
428 * @param ifacePtrPtr pointer to interface this pointer variable
429 * @param origRetPr pointer to original return value variable. NULL for void funcs
430 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
432 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
433 * when the hook func is being called as part of a recall
435 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
436 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
442 /************************************************************************/
443 /* High level interface */
444 /************************************************************************/
445 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
446 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
447 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
448 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
449 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
451 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
452 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
453 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
456 // NEVER-EVER call these from post hooks!
457 // also, only call it from the hook handlers directly!
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); \
487 * @brief Get/generate callclass for an interface pointer
489 * @param ifaceptr The interface pointer
491 template<class ifacetype>
492 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
494 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
495 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
498 template<class ifacetype>
499 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
501 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
504 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
506 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
507 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
508 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
509 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
512 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
513 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
515 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
516 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
518 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
519 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
520 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
521 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
523 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
524 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
526 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
527 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
528 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
529 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
532 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
533 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
534 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
535 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
536 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
537 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
539 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
540 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
541 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
542 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
543 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
544 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
552 #if SH_COMP == SH_COMP_MSVC
553 # define SH_SETUP_MFP(mfp) \
554 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
555 #elif SH_COMP == SH_COMP_GCC
556 # define SH_SETUP_MFP(mfp) \
557 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
558 reinterpret_cast<void**>(&mfp)[1] = 0;
560 # error Not supported yet.
563 //////////////////////////////////////////////////////////////////////////
564 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
565 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
567 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
568 SH_FHCls(ifacetype,ifacefunc,overload)() \
570 GetFuncInfo(funcptr, ms_MFI); \
573 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
575 using namespace ::SourceHook; \
576 GetFuncInfo(funcptr, ms_MFI); \
577 /* Verify interface version */ \
578 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
581 if (action == HA_GetInfo) \
583 param->SetVersion(SH_HOOKMAN_VERSION); \
584 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
585 reinterpret_cast<const char*>(&ms_Proto)); \
588 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
589 param->SetHookfuncVfnptr( \
590 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
593 else if (action == HA_Register) \
598 else if (action == HA_Unregister) \
607 // It has to be possible to use the macros in namespaces
608 // -> So we need to access and extend the global SourceHook namespace
609 // We use a namespace alias for this
610 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
611 struct SH_FHCls(ifacetype,ifacefunc,overload) \
613 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
614 static ::SourceHook::MemFuncInfo ms_MFI; \
615 static ::SourceHook::IHookManagerInfo *ms_HI; \
616 static ::SourceHook::ProtoInfo ms_Proto; \
617 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
619 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
621 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
622 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
623 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
624 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
625 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
627 using namespace ::SourceHook; \
629 GetFuncInfo(funcptr, mfi); \
630 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
631 return false; /* No non-virtual functions / virtual inheritance supported */ \
633 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
634 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
635 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
637 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
638 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
640 using namespace ::SourceHook; \
642 GetFuncInfo(funcptr, mfi); \
643 if (mfi.thisptroffs < 0) \
644 return false; /* No virtual inheritance supported */ \
646 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
647 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
648 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
651 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
652 struct SH_MFHCls(hookname) \
654 static SH_MFHCls(hookname) ms_Inst; \
655 static ::SourceHook::MemFuncInfo ms_MFI; \
656 static ::SourceHook::IHookManagerInfo *ms_HI; \
657 static ::SourceHook::ProtoInfo ms_Proto; \
659 SH_MFHCls(hookname)() \
661 ms_MFI.isVirtual = true; \
662 ms_MFI.thisptroffs = pthisptroffs; \
663 ms_MFI.vtblindex = pvtblidx; \
664 ms_MFI.vtbloffs = pvtbloffs; \
666 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
668 using namespace ::SourceHook; \
669 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
670 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
671 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
672 /* Verify interface version */ \
673 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
676 if (action == HA_GetInfo) \
678 param->SetVersion(SH_HOOKMAN_VERSION); \
679 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
680 reinterpret_cast<const char*>(&ms_Proto)); \
683 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
684 param->SetHookfuncVfnptr( \
685 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
688 else if (action == HA_Register) \
693 else if (action == HA_Unregister) \
702 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
704 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
705 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
706 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
707 bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
708 SH_MFHCls(hookname)::FD handler) \
710 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
711 SH_MFHCls(hookname)::HookManPubFunc, \
712 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
714 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
715 SH_MFHCls(hookname)::FD handler) \
717 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
718 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
719 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
723 #define SH_SETUPCALLS(rettype, paramtypes, params) \
724 /* 1) Find the vfnptr */ \
725 using namespace ::SourceHook; \
726 void *ourvfnptr = reinterpret_cast<void*>( \
727 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
728 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
729 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
731 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
732 /* ... and the iface */ \
733 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
736 /* The iface info was not found. Redirect the call to the original function. */ \
737 rettype (EmptyClass::*mfp)paramtypes; \
739 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
741 /* 2) Declare some vars and set it up */ \
742 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
743 IHookList *prelist = ifinfo->GetPreHooks(); \
744 IHookList *postlist = ifinfo->GetPostHooks(); \
745 META_RES status = MRES_IGNORED; \
749 rettype override_ret; \
750 rettype plugin_ret; \
752 rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
753 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
755 #define SH_CALL_HOOKS(post, params) \
756 if (SH_GLOB_SHPTR->ShouldContinue()) \
758 prev_res = MRES_IGNORED; \
759 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
761 cur_res = MRES_IGNORED; \
762 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
763 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
764 prev_res = cur_res; \
765 if (cur_res > status) \
767 if (cur_res >= MRES_OVERRIDE) \
768 *pOverrideRet = plugin_ret; \
769 if (!SH_GLOB_SHPTR->ShouldContinue()) \
777 #define SH_CALL_ORIG(rettype, paramtypes, params) \
778 if (status != MRES_SUPERCEDE) \
780 rettype (EmptyClass::*mfp)paramtypes; \
782 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
785 orig_ret = override_ret; \
787 #define SH_RETURN() \
788 SH_GLOB_SHPTR->HookLoopEnd(); \
789 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
791 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
792 SH_SETUPCALLS(rettype, paramtypes, params) \
793 SH_CALL_HOOKS(pre, params) \
794 SH_CALL_ORIG(rettype, paramtypes, params) \
795 SH_CALL_HOOKS(post, params) \
798 //////////////////////////////////////////////////////////////////////////
799 #define SH_SETUPCALLS_void(paramtypes, params) \
800 /* 1) Find the vfnptr */ \
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 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
805 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
807 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
808 /* ... and the iface */ \
809 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
812 /* The iface info was not found. Redirect the call to the original function. */ \
813 void (EmptyClass::*mfp)paramtypes; \
815 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
818 /* 2) Declare some vars and set it up */ \
819 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
820 IHookList *prelist = ifinfo->GetPreHooks(); \
821 IHookList *postlist = ifinfo->GetPostHooks(); \
822 META_RES status = MRES_IGNORED; \
826 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
828 #define SH_CALL_HOOKS_void(post, params) \
829 if (SH_GLOB_SHPTR->ShouldContinue()) \
831 prev_res = MRES_IGNORED; \
832 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
834 cur_res = MRES_IGNORED; \
835 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
836 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
837 prev_res = cur_res; \
838 if (cur_res > status) \
840 if (!SH_GLOB_SHPTR->ShouldContinue()) \
848 #define SH_CALL_ORIG_void(paramtypes, params) \
849 if (status != MRES_SUPERCEDE) \
851 void (EmptyClass::*mfp)paramtypes; \
853 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
856 #define SH_RETURN_void() \
857 SH_GLOB_SHPTR->HookLoopEnd();
859 #define SH_HANDLEFUNC_void(paramtypes, params) \
860 SH_SETUPCALLS_void(paramtypes, params) \
861 SH_CALL_HOOKS_void(pre, params) \
862 SH_CALL_ORIG_void(paramtypes, params) \
863 SH_CALL_HOOKS_void(post, params) \
867 // Special vafmt handlers
869 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
870 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
871 SH_CALL_HOOKS(pre, params_plug) \
872 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
873 SH_CALL_HOOKS(post, params_plug) \
876 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
877 SH_SETUPCALLS_void(paramtypes, params_orig) \
878 SH_CALL_HOOKS_void(pre, params_plug) \
879 SH_CALL_ORIG_void(paramtypes, params_orig) \
880 SH_CALL_HOOKS_void(post, params_plug) \
883 //////////////////////////////////////////////////////////////////////////
886 // ********* Support for @$@ arguments *********
887 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
888 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
889 (&ifacetype::ifacefunc))) \
890 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
891 virtual rettype Func(@param%% p%%|, @) \
892 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
893 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
894 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
896 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
897 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
898 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
900 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
901 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
902 (&ifacetype::ifacefunc))) \
903 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
904 virtual void Func(@param%% p%%|, @) \
905 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
906 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
907 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
910 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
911 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
912 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
914 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
915 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
916 (&ifacetype::ifacefunc))) \
917 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
918 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
920 char buf[::SourceHook::STRBUF_LEN]; \
923 vsnprintf(buf, sizeof(buf), fmt, ap); \
925 SH_HANDLEFUNC_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
927 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
928 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
930 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
931 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
932 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
934 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
935 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
936 (&ifacetype::ifacefunc))) \
937 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
938 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
940 char buf[::SourceHook::STRBUF_LEN]; \
943 vsnprintf(buf, sizeof(buf), fmt, ap); \
945 SH_HANDLEFUNC_void_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
947 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
948 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
950 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
951 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
952 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
954 #define SH_DECL_MANUALHOOK@$@(hookname, vtblidx, vtbloffs, thisptroffs, rettype@, param%%@) \
955 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
956 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
957 virtual rettype Func(@param%% p%%|, @) \
958 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
959 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
961 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
962 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
963 @$@, __SourceHook_ParamSizesM_##hookname);
965 #define SH_DECL_MANUALHOOK@$@_void(hookname, vtblidx, vtbloffs, thisptroffs@, param%%@) \
966 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
967 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
968 virtual void Func(@param%% p%%|, @) \
969 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
970 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
972 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
973 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
974 @$@, __SourceHook_ParamSizesM_##hookname);
979 //////////////////////////////////////////////////////////////////////////
982 #if SH_COMP == SH_COMP_MSVC
984 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
986 using namespace ::SourceHook; \
988 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
989 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
991 return (m_CC->GetThisPtr()->*m_MFP)call; \
993 /* It's hooked. Call the original function. */ \
996 RetType(EmptyClass::*mfpnew)prms; \
1001 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
1002 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
1005 #elif SH_COMP == SH_COMP_GCC
1007 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1009 using namespace ::SourceHook; \
1011 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
1012 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
1014 return (m_CC->GetThisPtr()->*m_MFP)call; \
1016 /* It's hooked. Call the original function. */ \
1019 RetType(EmptyClass::*mfpnew)prms; \
1023 intptr_t adjustor; \
1026 u.s.addr = origfunc; \
1027 u.s.adjustor = mfi.thisptroffs; \
1029 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
1034 namespace SourceHook
1036 template<class CCType, class RetType, class MFPType> class ExecutableClass
1041 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
1046 // Support for @$@ arguments
1047 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
1048 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
1054 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1055 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1056 // The only purpose of the mfp2 parameter is to extract the return type
1059 // Support for @$@ arguments
1060 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1061 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1062 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
1064 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1067 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1068 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1069 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
1071 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1076 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1077 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1080 // Support for @$@ arguments
1081 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1082 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1083 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
1085 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1088 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1089 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1090 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
1092 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1099 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
1101 #undef SH_MAKE_EXECUTABLECLASS_OB
1103 //////////////////////////////////////////////////////////////////////////
1104 // SetOverrideRet and RecallGetIface for recalls
1105 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1106 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1107 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1109 namespace SourceHook
1112 template <class Iface, class RetType@, class Param%%@>
1113 void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@Param%%|, @), const RetType res)
1115 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1118 template <class Iface, class RetType@, class Param%%@>
1119 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@Param%%|, @))
1121 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1127 // The pope is dead. -> :(