Added insert(iterator where, const T &what) to sh_list and simplified it on the occasion, hook managers now have (optional) version numbers, everything somehow works! Also changed _NEWPARAMS macro syntax a bit
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 #define SH_PTRSIZE sizeof(void*)
91 #include "FastDelegate.h"
92 #include "sh_memfuncinfo.h"
96 // Flags returned by a plugin's api function.
97 // NOTE: order is crucial, as greater/less comparisons are made.
100 MRES_IGNORED=0, // plugin didn't take any action
101 MRES_HANDLED, // plugin did something, but real function should still be called
102 MRES_OVERRIDE, // call real function, but use my return value
103 MRES_SUPERCEDE // skip real function; use my return value
110 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
112 const int STRBUF_LEN=4096;
115 * @brief An empty class. No inheritance used. Used for original-function-call hacks
122 * @brief Implicit cast.
124 template <class In, class Out>
125 inline Out implicit_cast(In input)
131 * @brief A plugin typedef
133 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
134 * plugin has a unique identifier, everything is ok.
135 * It should also be possible to set it to 0.
141 ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
144 int beginningNull; //!< To distinguish from old protos (which never begin with 0)
145 int retTypeSize; //!< 0 if void
146 int numOfParams; //!< number of parameters
147 const int *params; //!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
151 * @brief Specifies the actions for hook managers
153 enum HookManagerAction
155 HA_GetInfo = 0, //!< Store info about the hook manager
156 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
157 HA_Unregister //!< Clear the saved pointer
160 struct IHookManagerInfo;
163 * @brief Pointer to hook manager interface function
165 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
167 * @param ha What the hook manager should do
168 * @param hi A pointer to IHookManagerInfo
170 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
175 virtual void DeleteThis() = 0; // Ugly, I know
176 virtual bool IsEqual(ISHDelegate *other) = 0;
179 template <class T> class CSHDelegate : public ISHDelegate
183 CSHDelegate(T deleg) : m_Deleg(deleg)
187 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
196 bool IsEqual(ISHDelegate *other)
198 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
211 virtual bool End() = 0;
212 virtual void Next() = 0;
213 virtual ISHDelegate *Handler() = 0;
214 virtual int ThisPtrOffs() = 0;
216 virtual IIter *GetIter() = 0;
217 virtual void ReleaseIter(IIter *pIter) = 0;
222 virtual void *GetPtr() = 0;
223 virtual IHookList *GetPreHooks() = 0;
224 virtual IHookList *GetPostHooks() = 0;
229 virtual void *GetVfnPtr() = 0;
230 virtual void *GetOrigEntry() = 0;
232 virtual IIface *FindIface(void *ptr) = 0;
235 struct IHookManagerInfo
237 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
239 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
240 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
242 // Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
243 // If the hookman doesn't set this, it defaults 0
244 // SourceHook prefers hookmans with higher version numbers
245 virtual void SetVersion(int version) = 0;
251 IHookList::IIter *m_pIter;
253 AutoHookIter(IHookList *pList)
254 : m_pList(pList), m_pIter(pList->GetIter())
261 m_pList->ReleaseIter(m_pIter);
266 return m_pIter->End();
274 ISHDelegate *Handler()
276 return m_pIter->Handler();
281 return m_pIter->ThisPtrOffs();
290 template<class B> struct CallClass
292 virtual B *GetThisPtr() = 0;
293 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
296 typedef CallClass<void> GenericCallClass;
299 * @brief The main SourceHook interface
305 * @brief Return interface version
307 virtual int GetIfaceVersion() = 0;
310 * @brief Return implementation version
312 virtual int GetImplVersion() = 0;
317 * @return True if the function succeeded, false otherwise
319 * @param plug The unique identifier of the plugin that calls this function
320 * @param iface The interface pointer
321 * @param ifacesize The size of the class iface points to
322 * @param myHookMan A hook manager function that should be capable of handling the function
323 * @param handler A pointer to a FastDelegate containing the hook handler
324 * @param post Set to true if you want a post handler
326 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
327 ISHDelegate *handler, bool post) = 0;
330 * @brief Removes a hook.
332 * @return True if the function succeeded, false otherwise
334 * @param plug The unique identifier of the plugin that calls this function
335 * @param iface The interface pointer
336 * @param myHookMan A hook manager function that should be capable of handling the function
337 * @param handler A pointer to a FastDelegate containing the hook handler
338 * @param post Set to true if you want a post handler
340 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
341 ISHDelegate *handler, bool post) = 0;
344 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
346 * @param plug The unique identifier of the plugin in question
348 virtual bool IsPluginInUse(Plugin plug) = 0;
351 * @brief Return a pointer to a callclass. Generate a new one if required.
353 * @param iface The interface pointer
354 * @param size Size of the class instance
356 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
359 * @brief Release a callclass
361 * @param ptr Pointer to the callclass
363 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
365 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
366 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
367 //!< previously calledhandler
368 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
369 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
370 //!< If not in post function, undefined
371 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
372 //!< If none is specified, NULL
373 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
374 //////////////////////////////////////////////////////////////////////////
376 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
377 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
378 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
379 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
380 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
381 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
382 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
383 virtual void SetOverrideRetPtr(void *ptr) = 0; //!< Sets the override result pointer
384 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
387 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
389 * @param plug The owner of the hook manager
390 * @param pubFunc The hook manager's info function
392 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
394 virtual void DoRecall() = 0; //!< Initiates a recall sequence
399 Users want the ability to change parameters of the called function
400 from inside their handler.
403 2) Recall the function
404 3) => SH's hook func gets called:
405 4) The first iterator points at the first hook the last hookfunc didn't execute yet
406 5) does all iteration and returns normally
407 6) The user's handler returns immediately
408 7) The hook func returns immediately as well
410 Also note that the recalled hookfunc starts with the status the recalling hookfunc
411 ended with. The last handler (doing the recall) is also able to specify its own
415 virtual void *GetOverrideRetPtr() = 0; //!< Returns the pointer set by SetOverrideRetPtr
418 * @brief Set up the hook loop. Equivalent to calling:
419 * SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
421 * @param statusPtr pointer to status variable
422 * @param prevResPtr pointer to previous result variable
423 * @param curResPtr pointer to current result variable
424 * @param ifacePtrPtr pointer to interface this pointer variable
425 * @param origRetPr pointer to original return value variable. NULL for void funcs
426 * @param overrideRetPtr pointer to override return value variable. NULL for void funcs
428 * @return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
429 * when the hook func is being called as part of a recall
431 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
432 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
438 /************************************************************************/
439 /* High level interface */
440 /************************************************************************/
441 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
442 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
443 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
444 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
445 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
447 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
448 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
449 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
452 // NEVER-EVER call these from post hooks!
453 // also, only call it from the hook handlers directly!
456 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
457 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
459 // SourceHook::SetOverrideRet is defined later.
460 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
462 SET_META_RESULT(result); \
463 SH_GLOB_SHPTR->DoRecall(); \
464 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
465 RETURN_META(MRES_SUPERCEDE); \
468 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
470 SET_META_RESULT(result); \
471 SH_GLOB_SHPTR->DoRecall(); \
472 if ((result) >= MRES_OVERRIDE) \
474 /* meh, set the override result here because we don't get a chance to return */ \
475 /* before continuing the hook loop through the recall */ \
476 SourceHook::SetOverrideResult(SH_GLOB_SHPTR, memfuncptr, value); \
478 RETURN_META_VALUE(MRES_SUPERCEDE, \
479 (SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
483 * @brief Get/generate callclass for an interface pointer
485 * @param ifaceptr The interface pointer
487 template<class ifacetype>
488 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
490 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
491 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
494 template<class ifacetype>
495 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
497 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
500 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
502 SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
503 SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
504 SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
505 SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
508 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
509 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
511 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
512 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
514 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
515 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
516 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
517 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
519 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
520 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
522 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
523 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
524 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
525 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
528 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
529 __SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
530 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
531 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
532 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
533 SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
535 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
536 __SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
537 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
538 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
539 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
540 SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
548 #if SH_COMP == SH_COMP_MSVC
549 # define SH_SETUP_MFP(mfp) \
550 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
551 #elif SH_COMP == SH_COMP_GCC
552 # define SH_SETUP_MFP(mfp) \
553 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
554 reinterpret_cast<void**>(&mfp)[1] = 0;
556 # error Not supported yet.
559 //////////////////////////////////////////////////////////////////////////
560 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
561 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
563 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
564 SH_FHCls(ifacetype,ifacefunc,overload)() \
566 GetFuncInfo(funcptr, ms_MFI); \
569 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
571 using namespace ::SourceHook; \
572 GetFuncInfo(funcptr, ms_MFI); \
573 /* Verify interface version */ \
574 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
577 if (action == HA_GetInfo) \
579 param->SetVersion(SH_HOOKMAN_VERSION); \
580 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
581 reinterpret_cast<const char*>(&ms_Proto)); \
584 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
585 param->SetHookfuncVfnptr( \
586 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
589 else if (action == HA_Register) \
594 else if (action == HA_Unregister) \
603 // It has to be possible to use the macros in namespaces
604 // -> So we need to access and extend the global SourceHook namespace
605 // We use a namespace alias for this
606 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
607 struct SH_FHCls(ifacetype,ifacefunc,overload) \
609 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
610 static ::SourceHook::MemFuncInfo ms_MFI; \
611 static ::SourceHook::IHookManagerInfo *ms_HI; \
612 static ::SourceHook::ProtoInfo ms_Proto; \
613 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
615 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
617 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
618 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
619 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
620 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
621 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
623 using namespace ::SourceHook; \
625 GetFuncInfo(funcptr, mfi); \
626 if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
627 return false; /* No non-virtual functions / virtual inheritance supported */ \
629 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
630 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
631 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
633 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
634 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
636 using namespace ::SourceHook; \
638 GetFuncInfo(funcptr, mfi); \
639 if (mfi.thisptroffs < 0) \
640 return false; /* No virtual inheritance supported */ \
642 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
643 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
644 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
647 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
648 struct SH_MFHCls(hookname) \
650 static SH_MFHCls(hookname) ms_Inst; \
651 static ::SourceHook::MemFuncInfo ms_MFI; \
652 static ::SourceHook::IHookManagerInfo *ms_HI; \
653 static ::SourceHook::ProtoInfo ms_Proto; \
655 SH_MFHCls(hookname)() \
657 ms_MFI.isVirtual = true; \
658 ms_MFI.thisptroffs = pthisptroffs; \
659 ms_MFI.vtblindex = pvtblidx; \
660 ms_MFI.vtbloffs = pvtbloffs; \
662 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
664 using namespace ::SourceHook; \
665 /* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
666 /* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
667 /* which is construced before SH_MFHCls(hookname)() gets called? */ \
668 /* Verify interface version */ \
669 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
672 if (action == HA_GetInfo) \
674 param->SetVersion(SH_HOOKMAN_VERSION); \
675 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
676 reinterpret_cast<const char*>(&ms_Proto)); \
679 GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
680 param->SetHookfuncVfnptr( \
681 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
684 else if (action == HA_Register) \
689 else if (action == HA_Unregister) \
698 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
700 SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
701 ::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
702 ::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
703 bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
704 SH_MFHCls(hookname)::FD handler) \
706 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
707 SH_MFHCls(hookname)::HookManPubFunc, \
708 new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
710 bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
711 SH_MFHCls(hookname)::FD handler) \
713 ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
714 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
715 SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
719 #define SH_SETUPCALLS(rettype, paramtypes, params) \
720 /* 1) Find the vfnptr */ \
721 using namespace ::SourceHook; \
722 void *ourvfnptr = reinterpret_cast<void*>( \
723 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
724 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
725 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
727 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
728 /* ... and the iface */ \
729 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
732 /* The iface info was not found. Redirect the call to the original function. */ \
733 rettype (EmptyClass::*mfp)paramtypes; \
735 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
737 /* 2) Declare some vars and set it up */ \
738 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
739 IHookList *prelist = ifinfo->GetPreHooks(); \
740 IHookList *postlist = ifinfo->GetPostHooks(); \
741 META_RES status = MRES_IGNORED; \
745 rettype override_ret; \
746 rettype plugin_ret; \
748 rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
749 &status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
751 #define SH_CALL_HOOKS(post, params) \
752 if (SH_GLOB_SHPTR->ShouldContinue()) \
754 prev_res = MRES_IGNORED; \
755 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
757 cur_res = MRES_IGNORED; \
758 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
759 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
760 prev_res = cur_res; \
761 if (cur_res > status) \
763 if (cur_res >= MRES_OVERRIDE) \
764 *pOverrideRet = plugin_ret; \
765 if (!SH_GLOB_SHPTR->ShouldContinue()) \
773 #define SH_CALL_ORIG(rettype, paramtypes, params) \
774 if (status != MRES_SUPERCEDE) \
776 rettype (EmptyClass::*mfp)paramtypes; \
778 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
781 orig_ret = override_ret; \
783 #define SH_RETURN() \
784 SH_GLOB_SHPTR->HookLoopEnd(); \
785 return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
787 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
788 SH_SETUPCALLS(rettype, paramtypes, params) \
789 SH_CALL_HOOKS(pre, params) \
790 SH_CALL_ORIG(rettype, paramtypes, params) \
791 SH_CALL_HOOKS(post, params) \
794 //////////////////////////////////////////////////////////////////////////
795 #define SH_SETUPCALLS_void(paramtypes, params) \
796 /* 1) Find the vfnptr */ \
797 using namespace ::SourceHook; \
798 void *ourvfnptr = reinterpret_cast<void*>( \
799 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
800 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
801 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
803 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
804 /* ... and the iface */ \
805 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
808 /* The iface info was not found. Redirect the call to the original function. */ \
809 void (EmptyClass::*mfp)paramtypes; \
811 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
814 /* 2) Declare some vars and set it up */ \
815 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
816 IHookList *prelist = ifinfo->GetPreHooks(); \
817 IHookList *postlist = ifinfo->GetPostHooks(); \
818 META_RES status = MRES_IGNORED; \
822 SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
824 #define SH_CALL_HOOKS_void(post, params) \
825 if (SH_GLOB_SHPTR->ShouldContinue()) \
827 prev_res = MRES_IGNORED; \
828 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
830 cur_res = MRES_IGNORED; \
831 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
832 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
833 prev_res = cur_res; \
834 if (cur_res > status) \
836 if (!SH_GLOB_SHPTR->ShouldContinue()) \
844 #define SH_CALL_ORIG_void(paramtypes, params) \
845 if (status != MRES_SUPERCEDE) \
847 void (EmptyClass::*mfp)paramtypes; \
849 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
852 #define SH_RETURN_void() \
853 SH_GLOB_SHPTR->HookLoopEnd();
855 #define SH_HANDLEFUNC_void(paramtypes, params) \
856 SH_SETUPCALLS_void(paramtypes, params) \
857 SH_CALL_HOOKS_void(pre, params) \
858 SH_CALL_ORIG_void(paramtypes, params) \
859 SH_CALL_HOOKS_void(post, params) \
863 // Special vafmt handlers
865 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
866 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
867 SH_CALL_HOOKS(pre, params_plug) \
868 SH_CALL_ORIG(rettype, paramtypes, params_orig) \
869 SH_CALL_HOOKS(post, params_plug) \
872 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
873 SH_SETUPCALLS_void(paramtypes, params_orig) \
874 SH_CALL_HOOKS_void(pre, params_plug) \
875 SH_CALL_ORIG_void(paramtypes, params_orig) \
876 SH_CALL_HOOKS_void(post, params_plug) \
879 //////////////////////////////////////////////////////////////////////////
882 // ********* Support for @$@ arguments *********
883 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
884 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
885 (&ifacetype::ifacefunc))) \
886 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
887 virtual rettype Func(@param%% p%%|, @) \
888 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
889 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
890 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
892 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
893 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
894 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
896 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
897 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
898 (&ifacetype::ifacefunc))) \
899 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
900 virtual void Func(@param%% p%%|, @) \
901 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
902 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
903 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc))) \
906 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@, sizeof(param%%)@ }; \
907 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
908 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
910 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
911 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
912 (&ifacetype::ifacefunc))) \
913 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
914 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
916 char buf[::SourceHook::STRBUF_LEN]; \
919 vsnprintf(buf, sizeof(buf), fmt, ap); \
921 SH_HANDLEFUNC_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
923 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
924 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
926 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
927 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
928 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
930 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
931 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
932 (&ifacetype::ifacefunc))) \
933 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
934 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
936 char buf[::SourceHook::STRBUF_LEN]; \
939 vsnprintf(buf, sizeof(buf), fmt, ap); \
941 SH_HANDLEFUNC_void_vafmt((@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
943 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
944 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc))) \
946 const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@, sizeof(param%%)@ }; \
947 ::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
948 @$@, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
950 #define SH_DECL_MANUALHOOK@$@(hookname, vtblidx, vtbloffs, thisptroffs, rettype@, param%%@) \
951 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
952 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
953 virtual rettype Func(@param%% p%%|, @) \
954 { SH_HANDLEFUNC((@param%%|, @), (@p%%|, @), rettype); } \
955 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
957 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
958 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
959 @$@, __SourceHook_ParamSizesM_##hookname);
961 #define SH_DECL_MANUALHOOK@$@_void(hookname, vtblidx, vtbloffs, thisptroffs@, param%%@) \
962 SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
963 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
964 virtual void Func(@param%% p%%|, @) \
965 { SH_HANDLEFUNC_void((@param%%|, @), (@p%%|, @)); } \
966 SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
968 const int __SourceHook_ParamSizesM_##hookname[] = { 0@, sizeof(param%%)@ }; \
969 ::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
970 @$@, __SourceHook_ParamSizesM_##hookname);
975 //////////////////////////////////////////////////////////////////////////
978 #if SH_COMP == SH_COMP_MSVC
980 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
982 using namespace ::SourceHook; \
984 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
985 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
987 return (m_CC->GetThisPtr()->*m_MFP)call; \
989 /* It's hooked. Call the original function. */ \
992 RetType(EmptyClass::*mfpnew)prms; \
997 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
998 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
1001 #elif SH_COMP == SH_COMP_GCC
1003 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
1005 using namespace ::SourceHook; \
1007 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
1008 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
1010 return (m_CC->GetThisPtr()->*m_MFP)call; \
1012 /* It's hooked. Call the original function. */ \
1015 RetType(EmptyClass::*mfpnew)prms; \
1019 intptr_t adjustor; \
1022 u.s.addr = origfunc; \
1023 u.s.adjustor = mfi.thisptroffs; \
1025 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
1030 namespace SourceHook
1032 template<class CCType, class RetType, class MFPType> class ExecutableClass
1037 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
1042 // Support for @$@ arguments
1043 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
1044 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
1050 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
1051 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
1052 // The only purpose of the mfp2 parameter is to extract the return type
1055 // Support for @$@ arguments
1056 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1057 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1058 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
1060 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1063 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1064 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1065 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
1067 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1072 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
1073 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
1076 // Support for @$@ arguments
1077 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1078 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1079 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
1081 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1084 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
1085 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
1086 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
1088 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
1095 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
1097 #undef SH_MAKE_EXECUTABLECLASS_OB
1099 //////////////////////////////////////////////////////////////////////////
1100 // SetOverrideRet and RecallGetIface for recalls
1101 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
1102 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
1103 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
1105 namespace SourceHook
1108 template <class Iface, class RetType@, class Param%%@>
1109 void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@Param%%|, @), const RetType res)
1111 *reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
1114 template <class Iface, class RetType@, class Param%%@>
1115 Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@Param%%|, @))
1117 return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
1123 // The pope is dead. -> :(