It seems to work, but it's not reentrant yet.
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"
23 // Future: Thread safe interface?
25 #define SH_IFACE_VERSION 3
26 #define SH_IMPL_VERSION 2
29 #define SH_GLOB_SHPTR g_SHPtr
32 #ifndef SH_GLOB_PLUGPTR
33 #define SH_GLOB_PLUGPTR g_PLID
40 # define SH_ASSERT(x, info) \
44 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
52 # define SH_ASSERT(x, info)
56 #define SH_SYS_WIN32 1
57 #define SH_SYS_LINUX 2
60 # define SH_SYS SH_SYS_WIN32
61 #elif defined __linux__
62 # define SH_SYS SH_SYS_LINUX
64 # error Unsupported system
69 #define SH_COMP_MSVC 2
72 # define SH_COMP SH_COMP_MSVC
73 #elif defined __GNUC__
74 # define SH_COMP SH_COMP_GCC
76 # error Unsupported compiler
79 #if SH_COMP==SH_COMP_MSVC
80 # define vsnprintf _vsnprintf
83 #define SH_PTRSIZE sizeof(void*)
85 #include "FastDelegate.h"
86 #include "sh_memfuncinfo.h"
90 // Flags returned by a plugin's api function.
91 // NOTE: order is crucial, as greater/less comparisons are made.
94 MRES_IGNORED=0, // plugin didn't take any action
95 MRES_HANDLED, // plugin did something, but real function should still be called
96 MRES_OVERRIDE, // call real function, but use my return value
97 MRES_SUPERCEDE // skip real function; use my return value
104 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
106 const int STRBUF_LEN=4096;
109 * @brief An empty class. No inheritance used. Used for original-function-call hacks
116 * @brief Implicit cast.
118 template <class In, class Out>
119 inline Out implicit_cast(In input)
125 * @brief A plugin typedef
127 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
128 * plugin has a unique identifier, everything is ok.
129 * It should also be possible to set it to 0.
134 * @brief Specifies the actions for hook managers
136 enum HookManagerAction
138 HA_GetInfo = 0, //!< Store info about the hook manager
139 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
140 HA_Unregister //!< Clear the saved pointer
143 struct IHookManagerInfo;
146 * @brief Pointer to hook manager interface function
148 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
150 * @param ha What the hook manager should do
151 * @param hi A pointer to IHookManagerInfo
153 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
158 virtual void DeleteThis() = 0; // Ugly, I know
159 virtual bool IsEqual(ISHDelegate *other) = 0;
162 template <class T> class CSHDelegate : public ISHDelegate
166 CSHDelegate(T deleg) : m_Deleg(deleg)
170 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
179 bool IsEqual(ISHDelegate *other)
181 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
194 virtual bool End() = 0;
195 virtual void Next() = 0;
196 virtual ISHDelegate *Handler() = 0;
197 virtual int ThisPtrOffs() = 0;
199 virtual IIter *GetIter() = 0;
200 virtual void ReleaseIter(IIter *pIter) = 0;
205 virtual void *GetPtr() = 0;
206 virtual IHookList *GetPreHooks() = 0;
207 virtual IHookList *GetPostHooks() = 0;
212 virtual void *GetVfnPtr() = 0;
213 virtual void *GetOrigEntry() = 0;
215 virtual IIface *FindIface(void *ptr) = 0;
218 struct IHookManagerInfo
220 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
222 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
223 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
229 IHookList::IIter *m_pIter;
231 AutoHookIter(IHookList *pList)
232 : m_pList(pList), m_pIter(pList->GetIter())
239 m_pList->ReleaseIter(m_pIter);
244 return m_pIter->End();
252 ISHDelegate *Handler()
254 return m_pIter->Handler();
259 return m_pIter->ThisPtrOffs();
268 template<class B> struct CallClass
270 virtual B *GetThisPtr() = 0;
271 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
274 typedef CallClass<void> GenericCallClass;
277 * @brief SH informs the loop in the hook manager about its status through this
286 * @brief The main SourceHook interface
292 * @brief Return interface version
294 virtual int GetIfaceVersion() = 0;
297 * @brief Return implementation version
299 virtual int GetImplVersion() = 0;
304 * @return True if the function succeeded, false otherwise
306 * @param plug The unique identifier of the plugin that calls this function
307 * @param iface The interface pointer
308 * @param ifacesize The size of the class iface points to
309 * @param myHookMan A hook manager function that should be capable of handling the function
310 * @param handler A pointer to a FastDelegate containing the hook handler
311 * @param post Set to true if you want a post handler
313 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
314 ISHDelegate *handler, bool post) = 0;
317 * @brief Removes a hook.
319 * @return True if the function succeeded, false otherwise
321 * @param plug The unique identifier of the plugin that calls this function
322 * @param iface The interface pointer
323 * @param myHookMan A hook manager function that should be capable of handling the function
324 * @param handler A pointer to a FastDelegate containing the hook handler
325 * @param post Set to true if you want a post handler
327 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
328 ISHDelegate *handler, bool post) = 0;
331 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
333 * @param plug The unique identifier of the plugin in question
335 virtual bool IsPluginInUse(Plugin plug) = 0;
338 * @brief Return a pointer to a callclass. Generate a new one if required.
340 * @param iface The interface pointer
341 * @param size Size of the class instance
343 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
346 * @brief Release a callclass
348 * @param ptr Pointer to the callclass
350 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
352 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
353 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
354 //!< previously calledhandler
355 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
356 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
357 //!< If not in post function, undefined
358 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
359 //!< If none is specified, NULL
360 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
361 //////////////////////////////////////////////////////////////////////////
363 virtual META_RES &GetCurResRef() = 0; //!< Gets the reference to the current meta result
364 virtual META_RES &GetPrevResRef() = 0; //!< Gets the reference to the previous meta result
365 virtual META_RES &GetStatusRef() = 0; //!< Gets the reference to the status variable
366 virtual void* &GetIfacePtrRef() = 0; //!< Gets the reference to the interface this pointer
367 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
368 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
369 virtual HookLoopStatus &GetStatusVarRef(IIface *pIface) = 0; //!< gets the reference to the hook status loop variable
370 virtual void HookLoopDone() = 0;
374 /************************************************************************/
375 /* High level interface */
376 /************************************************************************/
377 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
378 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
379 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
381 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
382 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
383 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
384 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
385 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
389 * @brief Get/generate callclass for an interface pointer
391 * @param ifaceptr The interface pointer
393 template<class ifacetype>
394 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
396 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
397 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
400 template<class ifacetype>
401 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
403 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
406 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
407 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
409 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
410 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
412 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
413 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
414 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
415 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
417 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
418 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
420 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
421 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
422 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
423 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
430 #if SH_COMP == SH_COMP_MSVC
431 # define SH_SETUP_MFP(mfp) \
432 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
433 #elif SH_COMP == SH_COMP_GCC
434 # define SH_SETUP_MFP(mfp) \
435 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
436 reinterpret_cast<void**>(&mfp)[1] = 0;
438 # error Not supported yet.
441 //////////////////////////////////////////////////////////////////////////
442 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
444 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
445 SH_FHCls(ifacetype,ifacefunc,overload)() \
447 GetFuncInfo(funcptr, ms_MFI); \
450 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
452 using namespace ::SourceHook; \
453 GetFuncInfo(funcptr, ms_MFI); \
454 /* Verify interface version */ \
455 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
458 if (action == ::SourceHook::HA_GetInfo) \
460 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, ms_Proto); \
463 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
464 param->SetHookfuncVfnptr( \
465 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
468 else if (action == ::SourceHook::HA_Register) \
473 else if (action == ::SourceHook::HA_Unregister) \
482 // It has to be possible to use the macros in namespaces
483 // -> So we need to access and extend the global SourceHook namespace
484 // We use a namespace alias for this
485 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
486 struct SH_FHCls(ifacetype,ifacefunc,overload) \
488 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
489 static ::SourceHook::MemFuncInfo ms_MFI; \
490 static ::SourceHook::IHookManagerInfo *ms_HI; \
491 static const char *ms_Proto; \
492 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
494 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
496 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
497 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
498 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
499 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
500 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
501 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
503 using namespace ::SourceHook; \
505 GetFuncInfo(funcptr, mfi); \
506 if (mfi.thisptroffs < 0) \
507 return false; /* No virtual inheritance supported */ \
509 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
510 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
511 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
513 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
514 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
516 using namespace ::SourceHook; \
518 GetFuncInfo(funcptr, mfi); \
519 if (mfi.thisptroffs < 0) \
520 return false; /* No virtual inheritance supported */ \
522 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
523 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
524 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
527 #define SH_SETUPCALLS(rettype, paramtypes, params) \
528 /* 1) Find the vfnptr */ \
529 using namespace ::SourceHook; \
530 void *ourvfnptr = reinterpret_cast<void*>( \
531 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
532 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
533 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
535 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
536 /* ... and the iface */ \
537 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
540 /* The iface info was not found. Redirect the call to the original function. */ \
541 rettype (EmptyClass::*mfp)paramtypes; \
543 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
545 /* 2) Declare some vars and set it up */ \
546 IHookList *prelist = ifinfo->GetPreHooks(); \
547 IHookList *postlist = ifinfo->GetPostHooks(); \
548 HookLoopStatus &hls = SH_GLOB_SHPTR->GetStatusVarRef(ifinfo); \
549 hls = HLS_Continue; \
551 rettype override_ret; \
552 rettype plugin_ret; \
553 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
554 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
555 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
556 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
557 status = MRES_IGNORED; \
558 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
559 SH_GLOB_SHPTR->SetOverrideRet(NULL);
561 #define SH_CALL_HOOKS(post, params) \
562 if (hls == HLS_Continue) \
564 prev_res = MRES_IGNORED; \
565 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
567 cur_res = MRES_IGNORED; \
568 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
569 hls = HLS_Continue; \
570 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
571 prev_res = cur_res; \
572 if (cur_res > status) \
574 if (cur_res >= MRES_OVERRIDE) \
576 override_ret = plugin_ret; \
577 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
579 if (hls == HLS_Stop) \
587 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
588 if (status != MRES_SUPERCEDE) \
590 rettype (EmptyClass::*mfp)paramtypes; \
592 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
595 orig_ret = override_ret;
597 #define SH_RETURN() \
598 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
600 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
601 SH_SETUPCALLS(rettype, paramtypes, params) \
602 SH_CALL_HOOKS(pre, params) \
603 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
604 SH_CALL_HOOKS(post, params) \
607 //////////////////////////////////////////////////////////////////////////
608 #define SH_SETUPCALLS_void(paramtypes, params) \
609 /* 1) Find the vfnptr */ \
610 using namespace ::SourceHook; \
611 void *ourvfnptr = reinterpret_cast<void*>( \
612 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
613 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
614 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
616 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
617 /* ... and the iface */ \
618 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
621 /* The iface info was not found. Redirect the call to the original function. */ \
622 void (EmptyClass::*mfp)paramtypes; \
624 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
627 /* 2) Declare some vars and set it up */ \
628 IHookList *prelist = ifinfo->GetPreHooks(); \
629 IHookList *postlist = ifinfo->GetPostHooks(); \
630 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
631 HookLoopStatus &hls = SH_GLOB_SHPTR->GetStatusVarRef(ifinfo); \
632 hls = HLS_Continue; \
633 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
634 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
635 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
636 status = MRES_IGNORED; \
637 SH_GLOB_SHPTR->SetOverrideRet(NULL); \
638 SH_GLOB_SHPTR->SetOrigRet(NULL);
640 #define SH_CALL_HOOKS_void(post, params) \
641 if (hls == HLS_Continue) \
643 prev_res = MRES_IGNORED; \
644 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
646 cur_res = MRES_IGNORED; \
647 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
648 hls = HLS_Continue; \
649 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
650 prev_res = cur_res; \
651 if (cur_res > status) \
653 if (hls == HLS_Stop) \
661 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
662 if (status != MRES_SUPERCEDE) \
664 void (EmptyClass::*mfp)paramtypes; \
666 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
669 #define SH_RETURN_void()
671 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
672 SH_SETUPCALLS_void(paramtypes, params) \
673 SH_CALL_HOOKS_void(pre, params) \
674 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
675 SH_CALL_HOOKS_void(post, params) \
679 // Special vafmt handlers
680 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
681 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
682 SH_CALL_HOOKS(pre, params_plug) \
683 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
684 SH_CALL_HOOKS(post, params_plug) \
687 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
688 SH_SETUPCALLS_void(paramtypes, params_orig) \
689 SH_CALL_HOOKS_void(pre, params_plug) \
690 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
691 SH_CALL_HOOKS_void(post, params_plug) \
694 //////////////////////////////////////////////////////////////////////////
697 // ********* Support for @$@ arguments *********
698 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
699 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
700 (&ifacetype::ifacefunc))) \
701 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
702 virtual rettype Func(@param%% p%%|, @) \
703 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
704 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
705 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
707 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
708 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
709 (&ifacetype::ifacefunc))) \
710 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
711 virtual void Func(@param%% p%%|, @) \
712 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
713 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
714 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
716 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
717 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
718 (&ifacetype::ifacefunc))) \
719 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
720 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
722 char buf[::SourceHook::STRBUF_LEN]; \
725 vsnprintf(buf, sizeof(buf), fmt, ap); \
727 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
729 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
730 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
732 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
733 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
734 (&ifacetype::ifacefunc))) \
735 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
736 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
738 char buf[::SourceHook::STRBUF_LEN]; \
741 vsnprintf(buf, sizeof(buf), fmt, ap); \
743 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
745 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
746 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
751 //////////////////////////////////////////////////////////////////////////
754 #if SH_COMP == SH_COMP_MSVC
756 // :TODO: TEST THIS ON MSVC
758 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
760 using namespace ::SourceHook; \
762 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
763 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
765 return (m_CC->GetThisPtr()->*m_MFP)call; \
767 /* It's hooked. Call the original function. */ \
770 RetType(EmptyClass::*mfpnew)prms; \
775 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
776 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
779 #elif SH_COMP == SH_COMP_GCC
781 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
783 using namespace ::SourceHook; \
785 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
786 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
788 return (m_CC->GetThisPtr()->*m_MFP)call; \
790 /* It's hooked. Call the original function. */ \
793 RetType(EmptyClass::*mfpnew)prms; \
800 u.s.addr = origfunc; \
801 u.s.adjustor = mfi.thisptroffs; \
803 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
810 template<class CCType, class RetType, class MFPType> class ExecutableClass
815 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
820 // Support for @$@ arguments
821 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
822 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
828 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
829 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
830 // The only purpose of the mfp2 parameter is to extract the return type
833 // Support for @$@ arguments
834 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
835 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
836 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
838 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
841 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
842 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
843 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
845 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
850 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
851 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
854 // Support for @$@ arguments
855 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
856 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
857 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
859 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
862 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
863 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
864 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
866 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
873 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
875 #undef SH_MAKE_EXECUTABLECLASS_OB
878 // The pope is dead. -> :(