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.
137 * @brief Specifies the actions for hook managers
139 enum HookManagerAction
141 HA_GetInfo = 0, //!< Store info about the hook manager
142 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
143 HA_Unregister //!< Clear the saved pointer
146 struct IHookManagerInfo;
149 * @brief Pointer to hook manager interface function
151 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
153 * @param ha What the hook manager should do
154 * @param hi A pointer to IHookManagerInfo
156 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
161 virtual void DeleteThis() = 0; // Ugly, I know
162 virtual bool IsEqual(ISHDelegate *other) = 0;
165 template <class T> class CSHDelegate : public ISHDelegate
169 CSHDelegate(T deleg) : m_Deleg(deleg)
173 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
182 bool IsEqual(ISHDelegate *other)
184 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
197 virtual bool End() = 0;
198 virtual void Next() = 0;
199 virtual ISHDelegate *Handler() = 0;
200 virtual int ThisPtrOffs() = 0;
202 virtual IIter *GetIter() = 0;
203 virtual void ReleaseIter(IIter *pIter) = 0;
208 virtual void *GetPtr() = 0;
209 virtual IHookList *GetPreHooks() = 0;
210 virtual IHookList *GetPostHooks() = 0;
215 virtual void *GetVfnPtr() = 0;
216 virtual void *GetOrigEntry() = 0;
218 virtual IIface *FindIface(void *ptr) = 0;
221 struct IHookManagerInfo
223 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
225 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
226 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
232 IHookList::IIter *m_pIter;
234 AutoHookIter(IHookList *pList)
235 : m_pList(pList), m_pIter(pList->GetIter())
242 m_pList->ReleaseIter(m_pIter);
247 return m_pIter->End();
255 ISHDelegate *Handler()
257 return m_pIter->Handler();
262 return m_pIter->ThisPtrOffs();
271 template<class B> struct CallClass
273 virtual B *GetThisPtr() = 0;
274 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
277 typedef CallClass<void> GenericCallClass;
280 * @brief The main SourceHook interface
286 * @brief Return interface version
288 virtual int GetIfaceVersion() = 0;
291 * @brief Return implementation version
293 virtual int GetImplVersion() = 0;
298 * @return True if the function succeeded, false otherwise
300 * @param plug The unique identifier of the plugin that calls this function
301 * @param iface The interface pointer
302 * @param ifacesize The size of the class iface points to
303 * @param myHookMan A hook manager function that should be capable of handling the function
304 * @param handler A pointer to a FastDelegate containing the hook handler
305 * @param post Set to true if you want a post handler
307 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
308 ISHDelegate *handler, bool post) = 0;
311 * @brief Removes a hook.
313 * @return True if the function succeeded, false otherwise
315 * @param plug The unique identifier of the plugin that calls this function
316 * @param iface The interface pointer
317 * @param myHookMan A hook manager function that should be capable of handling the function
318 * @param handler A pointer to a FastDelegate containing the hook handler
319 * @param post Set to true if you want a post handler
321 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
322 ISHDelegate *handler, bool post) = 0;
325 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
327 * @param plug The unique identifier of the plugin in question
329 virtual bool IsPluginInUse(Plugin plug) = 0;
332 * @brief Return a pointer to a callclass. Generate a new one if required.
334 * @param iface The interface pointer
335 * @param size Size of the class instance
337 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
340 * @brief Release a callclass
342 * @param ptr Pointer to the callclass
344 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
346 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
347 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
348 //!< previously calledhandler
349 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
350 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
351 //!< If not in post function, undefined
352 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
353 //!< If none is specified, NULL
354 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
355 //////////////////////////////////////////////////////////////////////////
357 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
358 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
359 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
360 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
361 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
362 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
363 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
364 virtual void SetOverrideRetPtr(const void *ptr) = 0; //!< Sets the override result pointer
365 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
369 /************************************************************************/
370 /* High level interface */
371 /************************************************************************/
372 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
373 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
374 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
376 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
377 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
378 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
379 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
380 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
384 * @brief Get/generate callclass for an interface pointer
386 * @param ifaceptr The interface pointer
388 template<class ifacetype>
389 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
391 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
392 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
395 template<class ifacetype>
396 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
398 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
401 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
402 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
404 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
405 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
407 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
408 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
409 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
410 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
412 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
413 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
415 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
416 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
417 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
418 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
425 #if SH_COMP == SH_COMP_MSVC
426 # define SH_SETUP_MFP(mfp) \
427 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
428 #elif SH_COMP == SH_COMP_GCC
429 # define SH_SETUP_MFP(mfp) \
430 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
431 reinterpret_cast<void**>(&mfp)[1] = 0;
433 # error Not supported yet.
436 //////////////////////////////////////////////////////////////////////////
437 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
439 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
440 SH_FHCls(ifacetype,ifacefunc,overload)() \
442 GetFuncInfo(funcptr, ms_MFI); \
445 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
447 using namespace ::SourceHook; \
448 GetFuncInfo(funcptr, ms_MFI); \
449 /* Verify interface version */ \
450 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
453 if (action == HA_GetInfo) \
455 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, ms_Proto); \
458 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
459 param->SetHookfuncVfnptr( \
460 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
463 else if (action == HA_Register) \
468 else if (action == HA_Unregister) \
477 // It has to be possible to use the macros in namespaces
478 // -> So we need to access and extend the global SourceHook namespace
479 // We use a namespace alias for this
480 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
481 struct SH_FHCls(ifacetype,ifacefunc,overload) \
483 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
484 static ::SourceHook::MemFuncInfo ms_MFI; \
485 static ::SourceHook::IHookManagerInfo *ms_HI; \
486 static const char *ms_Proto; \
487 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
489 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
491 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
492 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
493 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
494 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
495 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
496 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
498 using namespace ::SourceHook; \
500 GetFuncInfo(funcptr, mfi); \
501 if (mfi.thisptroffs < 0) \
502 return false; /* No virtual inheritance supported */ \
504 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
505 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
506 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
508 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
509 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
511 using namespace ::SourceHook; \
513 GetFuncInfo(funcptr, mfi); \
514 if (mfi.thisptroffs < 0) \
515 return false; /* No virtual inheritance supported */ \
517 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
518 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
519 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
522 #define SH_SETUPCALLS(rettype, paramtypes, params) \
523 /* 1) Find the vfnptr */ \
524 using namespace ::SourceHook; \
525 void *ourvfnptr = reinterpret_cast<void*>( \
526 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
527 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
528 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
530 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
531 /* ... and the iface */ \
532 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
535 /* The iface info was not found. Redirect the call to the original function. */ \
536 rettype (EmptyClass::*mfp)paramtypes; \
538 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
540 /* 2) Declare some vars and set it up */ \
541 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
542 IHookList *prelist = ifinfo->GetPreHooks(); \
543 IHookList *postlist = ifinfo->GetPostHooks(); \
544 META_RES status = MRES_IGNORED; \
547 SH_GLOB_SHPTR->SetStatusPtr(&status); \
548 SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
549 SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
551 rettype override_ret; \
552 rettype plugin_ret; \
554 SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
555 SH_GLOB_SHPTR->SetOrigRetPtr(reinterpret_cast<void*>(&orig_ret)); \
556 SH_GLOB_SHPTR->SetOverrideRetPtr(NULL);
558 #define SH_CALL_HOOKS(post, params) \
559 if (SH_GLOB_SHPTR->ShouldContinue()) \
561 prev_res = MRES_IGNORED; \
562 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
564 cur_res = MRES_IGNORED; \
565 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
566 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
567 prev_res = cur_res; \
568 if (cur_res > status) \
570 if (cur_res >= MRES_OVERRIDE) \
572 override_ret = plugin_ret; \
573 SH_GLOB_SHPTR->SetOverrideRetPtr(&override_ret); \
575 if (!SH_GLOB_SHPTR->ShouldContinue()) \
583 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
584 if (status != MRES_SUPERCEDE) \
586 rettype (EmptyClass::*mfp)paramtypes; \
588 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
591 orig_ret = override_ret;
593 #define SH_RETURN() \
594 SH_GLOB_SHPTR->HookLoopEnd(); \
595 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
597 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
598 SH_SETUPCALLS(rettype, paramtypes, params) \
599 SH_CALL_HOOKS(pre, params) \
600 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
601 SH_CALL_HOOKS(post, params) \
604 //////////////////////////////////////////////////////////////////////////
605 #define SH_SETUPCALLS_void(paramtypes, params) \
606 /* 1) Find the vfnptr */ \
607 using namespace ::SourceHook; \
608 void *ourvfnptr = reinterpret_cast<void*>( \
609 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
610 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
611 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
613 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
614 /* ... and the iface */ \
615 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
618 /* The iface info was not found. Redirect the call to the original function. */ \
619 void (EmptyClass::*mfp)paramtypes; \
621 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
624 /* 2) Declare some vars and set it up */ \
625 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
626 IHookList *prelist = ifinfo->GetPreHooks(); \
627 IHookList *postlist = ifinfo->GetPostHooks(); \
628 META_RES status = MRES_IGNORED; \
631 SH_GLOB_SHPTR->SetStatusPtr(&status); \
632 SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
633 SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
635 SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
636 SH_GLOB_SHPTR->SetOverrideRetPtr(NULL); \
637 SH_GLOB_SHPTR->SetOrigRetPtr(NULL);
639 #define SH_CALL_HOOKS_void(post, params) \
640 if (SH_GLOB_SHPTR->ShouldContinue()) \
642 prev_res = MRES_IGNORED; \
643 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
645 cur_res = MRES_IGNORED; \
646 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
647 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
648 prev_res = cur_res; \
649 if (cur_res > status) \
651 if (!SH_GLOB_SHPTR->ShouldContinue()) \
659 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
660 if (status != MRES_SUPERCEDE) \
662 void (EmptyClass::*mfp)paramtypes; \
664 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
667 #define SH_RETURN_void() \
668 SH_GLOB_SHPTR->HookLoopEnd();
670 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
671 SH_SETUPCALLS_void(paramtypes, params) \
672 SH_CALL_HOOKS_void(pre, params) \
673 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
674 SH_CALL_HOOKS_void(post, params) \
678 // Special vafmt handlers
679 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
680 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
681 SH_CALL_HOOKS(pre, params_plug) \
682 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
683 SH_CALL_HOOKS(post, params_plug) \
686 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
687 SH_SETUPCALLS_void(paramtypes, params_orig) \
688 SH_CALL_HOOKS_void(pre, params_plug) \
689 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
690 SH_CALL_HOOKS_void(post, params_plug) \
693 //////////////////////////////////////////////////////////////////////////
696 // ********* Support for @$@ arguments *********
697 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
698 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
699 (&ifacetype::ifacefunc))) \
700 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
701 virtual rettype Func(@param%% p%%|, @) \
702 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
703 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
704 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
706 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
707 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
708 (&ifacetype::ifacefunc))) \
709 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
710 virtual void Func(@param%% p%%|, @) \
711 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
712 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
713 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
715 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
716 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
717 (&ifacetype::ifacefunc))) \
718 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
719 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
721 char buf[::SourceHook::STRBUF_LEN]; \
724 vsnprintf(buf, sizeof(buf), fmt, ap); \
726 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
728 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
729 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
731 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
732 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
733 (&ifacetype::ifacefunc))) \
734 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
735 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
737 char buf[::SourceHook::STRBUF_LEN]; \
740 vsnprintf(buf, sizeof(buf), fmt, ap); \
742 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
744 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
745 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
750 //////////////////////////////////////////////////////////////////////////
753 #if SH_COMP == SH_COMP_MSVC
755 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
757 using namespace ::SourceHook; \
759 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
760 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
762 return (m_CC->GetThisPtr()->*m_MFP)call; \
764 /* It's hooked. Call the original function. */ \
767 RetType(EmptyClass::*mfpnew)prms; \
772 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
773 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
776 #elif SH_COMP == SH_COMP_GCC
778 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
780 using namespace ::SourceHook; \
782 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
783 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
785 return (m_CC->GetThisPtr()->*m_MFP)call; \
787 /* It's hooked. Call the original function. */ \
790 RetType(EmptyClass::*mfpnew)prms; \
797 u.s.addr = origfunc; \
798 u.s.adjustor = mfi.thisptroffs; \
800 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
807 template<class CCType, class RetType, class MFPType> class ExecutableClass
812 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
817 // Support for @$@ arguments
818 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
819 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
825 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
826 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
827 // The only purpose of the mfp2 parameter is to extract the return type
830 // Support for @$@ arguments
831 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
832 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
833 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
835 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
838 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
839 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
840 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
842 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
847 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
848 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
851 // Support for @$@ arguments
852 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
853 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
854 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
856 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
859 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
860 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
861 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
863 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
870 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
872 #undef SH_MAKE_EXECUTABLECLASS_OB
875 // The pope is dead. -> :(