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
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 The main SourceHook interface
283 * @brief Return interface version
285 virtual int GetIfaceVersion() = 0;
288 * @brief Return implementation version
290 virtual int GetImplVersion() = 0;
295 * @return True if the function succeeded, false otherwise
297 * @param plug The unique identifier of the plugin that calls this function
298 * @param iface The interface pointer
299 * @param ifacesize The size of the class iface points to
300 * @param myHookMan A hook manager function that should be capable of handling the function
301 * @param handler A pointer to a FastDelegate containing the hook handler
302 * @param post Set to true if you want a post handler
304 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
305 ISHDelegate *handler, bool post) = 0;
308 * @brief Removes a hook.
310 * @return True if the function succeeded, false otherwise
312 * @param plug The unique identifier of the plugin that calls this function
313 * @param iface The interface pointer
314 * @param myHookMan A hook manager function that should be capable of handling the function
315 * @param handler A pointer to a FastDelegate containing the hook handler
316 * @param post Set to true if you want a post handler
318 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
319 ISHDelegate *handler, bool post) = 0;
322 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
324 * @param plug The unique identifier of the plugin in question
326 virtual bool IsPluginInUse(Plugin plug) = 0;
329 * @brief Return a pointer to a callclass. Generate a new one if required.
331 * @param iface The interface pointer
332 * @param size Size of the class instance
334 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
337 * @brief Release a callclass
339 * @param ptr Pointer to the callclass
341 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
343 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
344 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
345 //!< previously calledhandler
346 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
347 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
348 //!< If not in post function, undefined
349 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
350 //!< If none is specified, NULL
351 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
352 //////////////////////////////////////////////////////////////////////////
354 virtual void HookLoopBegin(IIface *pIface) = 0; //!< Should be called when a hook loop begins
355 virtual void HookLoopEnd() = 0; //!< Should be called when a hook loop exits
356 virtual void SetCurResPtr(META_RES *mres) = 0; //!< Sets pointer to the current meta result
357 virtual void SetPrevResPtr(META_RES *mres) = 0; //!< Sets pointer to previous meta result
358 virtual void SetStatusPtr(META_RES *mres) = 0; //!< Sets pointer to the status variable
359 virtual void SetIfacePtrPtr(void **pp) = 0; //!< Sets pointer to the interface this pointer
360 virtual void SetOrigRetPtr(const void *ptr) = 0; //!< Sets the original return pointer
361 virtual void SetOverrideRetPtr(const void *ptr) = 0; //!< Sets the override result pointer
362 virtual bool ShouldContinue() = 0; //!< Returns false if the hook loop should exit
366 /************************************************************************/
367 /* High level interface */
368 /************************************************************************/
369 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
370 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
371 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
373 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
374 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
375 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
376 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
377 #define META_IFACEPTR(type) reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
381 * @brief Get/generate callclass for an interface pointer
383 * @param ifaceptr The interface pointer
385 template<class ifacetype>
386 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
388 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
389 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
392 template<class ifacetype>
393 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
395 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
398 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
399 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
401 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
402 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
404 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
405 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
406 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
407 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
409 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
410 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
412 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
413 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
414 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
415 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
422 #if SH_COMP == SH_COMP_MSVC
423 # define SH_SETUP_MFP(mfp) \
424 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
425 #elif SH_COMP == SH_COMP_GCC
426 # define SH_SETUP_MFP(mfp) \
427 reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
428 reinterpret_cast<void**>(&mfp)[1] = 0;
430 # error Not supported yet.
433 //////////////////////////////////////////////////////////////////////////
434 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
436 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
437 SH_FHCls(ifacetype,ifacefunc,overload)() \
439 GetFuncInfo(funcptr, ms_MFI); \
442 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
444 using namespace ::SourceHook; \
445 GetFuncInfo(funcptr, ms_MFI); \
446 /* Verify interface version */ \
447 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
450 if (action == ::SourceHook::HA_GetInfo) \
452 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, ms_Proto); \
455 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
456 param->SetHookfuncVfnptr( \
457 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
460 else if (action == ::SourceHook::HA_Register) \
465 else if (action == ::SourceHook::HA_Unregister) \
474 // It has to be possible to use the macros in namespaces
475 // -> So we need to access and extend the global SourceHook namespace
476 // We use a namespace alias for this
477 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
478 struct SH_FHCls(ifacetype,ifacefunc,overload) \
480 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
481 static ::SourceHook::MemFuncInfo ms_MFI; \
482 static ::SourceHook::IHookManagerInfo *ms_HI; \
483 static const char *ms_Proto; \
484 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
486 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
488 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
489 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
490 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
491 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
492 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
493 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
495 using namespace ::SourceHook; \
497 GetFuncInfo(funcptr, mfi); \
498 if (mfi.thisptroffs < 0) \
499 return false; /* No virtual inheritance supported */ \
501 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
502 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
503 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
505 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
506 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
508 using namespace ::SourceHook; \
510 GetFuncInfo(funcptr, mfi); \
511 if (mfi.thisptroffs < 0) \
512 return false; /* No virtual inheritance supported */ \
514 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
515 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
516 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
519 #define SH_SETUPCALLS(rettype, paramtypes, params) \
520 /* 1) Find the vfnptr */ \
521 using namespace ::SourceHook; \
522 void *ourvfnptr = reinterpret_cast<void*>( \
523 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
524 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
525 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
527 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
528 /* ... and the iface */ \
529 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
532 /* The iface info was not found. Redirect the call to the original function. */ \
533 rettype (EmptyClass::*mfp)paramtypes; \
535 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
537 /* 2) Declare some vars and set it up */ \
538 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
539 IHookList *prelist = ifinfo->GetPreHooks(); \
540 IHookList *postlist = ifinfo->GetPostHooks(); \
541 META_RES status = MRES_IGNORED; \
544 SH_GLOB_SHPTR->SetStatusPtr(&status); \
545 SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
546 SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
548 rettype override_ret; \
549 rettype plugin_ret; \
551 SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
552 SH_GLOB_SHPTR->SetOrigRetPtr(reinterpret_cast<void*>(&orig_ret)); \
553 SH_GLOB_SHPTR->SetOverrideRetPtr(NULL);
555 #define SH_CALL_HOOKS(post, params) \
556 if (SH_GLOB_SHPTR->ShouldContinue()) \
558 prev_res = MRES_IGNORED; \
559 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
561 cur_res = MRES_IGNORED; \
562 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
563 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
564 prev_res = cur_res; \
565 if (cur_res > status) \
567 if (cur_res >= MRES_OVERRIDE) \
569 override_ret = plugin_ret; \
570 SH_GLOB_SHPTR->SetOverrideRetPtr(&override_ret); \
572 if (!SH_GLOB_SHPTR->ShouldContinue()) \
580 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
581 if (status != MRES_SUPERCEDE) \
583 rettype (EmptyClass::*mfp)paramtypes; \
585 orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
588 orig_ret = override_ret;
590 #define SH_RETURN() \
591 SH_GLOB_SHPTR->HookLoopEnd(); \
592 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
594 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
595 SH_SETUPCALLS(rettype, paramtypes, params) \
596 SH_CALL_HOOKS(pre, params) \
597 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
598 SH_CALL_HOOKS(post, params) \
601 //////////////////////////////////////////////////////////////////////////
602 #define SH_SETUPCALLS_void(paramtypes, params) \
603 /* 1) Find the vfnptr */ \
604 using namespace ::SourceHook; \
605 void *ourvfnptr = reinterpret_cast<void*>( \
606 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
607 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
608 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
610 void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
611 /* ... and the iface */ \
612 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
615 /* The iface info was not found. Redirect the call to the original function. */ \
616 void (EmptyClass::*mfp)paramtypes; \
618 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
621 /* 2) Declare some vars and set it up */ \
622 SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
623 IHookList *prelist = ifinfo->GetPreHooks(); \
624 IHookList *postlist = ifinfo->GetPostHooks(); \
625 META_RES status = MRES_IGNORED; \
628 SH_GLOB_SHPTR->SetStatusPtr(&status); \
629 SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
630 SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
632 SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
633 SH_GLOB_SHPTR->SetOverrideRetPtr(NULL); \
634 SH_GLOB_SHPTR->SetOrigRetPtr(NULL);
636 #define SH_CALL_HOOKS_void(post, params) \
637 if (SH_GLOB_SHPTR->ShouldContinue()) \
639 prev_res = MRES_IGNORED; \
640 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
642 cur_res = MRES_IGNORED; \
643 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
644 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
645 prev_res = cur_res; \
646 if (cur_res > status) \
648 if (!SH_GLOB_SHPTR->ShouldContinue()) \
656 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
657 if (status != MRES_SUPERCEDE) \
659 void (EmptyClass::*mfp)paramtypes; \
661 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
664 #define SH_RETURN_void() \
665 SH_GLOB_SHPTR->HookLoopEnd();
667 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
668 SH_SETUPCALLS_void(paramtypes, params) \
669 SH_CALL_HOOKS_void(pre, params) \
670 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
671 SH_CALL_HOOKS_void(post, params) \
675 // Special vafmt handlers
676 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
677 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
678 SH_CALL_HOOKS(pre, params_plug) \
679 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
680 SH_CALL_HOOKS(post, params_plug) \
683 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
684 SH_SETUPCALLS_void(paramtypes, params_orig) \
685 SH_CALL_HOOKS_void(pre, params_plug) \
686 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
687 SH_CALL_HOOKS_void(post, params_plug) \
690 //////////////////////////////////////////////////////////////////////////
693 // ********* Support for @$@ arguments *********
694 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
695 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
696 (&ifacetype::ifacefunc))) \
697 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
698 virtual rettype Func(@param%% p%%|, @) \
699 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
700 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
701 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
703 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
704 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
705 (&ifacetype::ifacefunc))) \
706 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
707 virtual void Func(@param%% p%%|, @) \
708 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
709 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
710 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
712 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
713 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
714 (&ifacetype::ifacefunc))) \
715 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
716 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
718 char buf[::SourceHook::STRBUF_LEN]; \
721 vsnprintf(buf, sizeof(buf), fmt, ap); \
723 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
725 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
726 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
728 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
729 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
730 (&ifacetype::ifacefunc))) \
731 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
732 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
734 char buf[::SourceHook::STRBUF_LEN]; \
737 vsnprintf(buf, sizeof(buf), fmt, ap); \
739 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
741 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
742 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
747 //////////////////////////////////////////////////////////////////////////
750 #if SH_COMP == SH_COMP_MSVC
752 // :TODO: TEST THIS ON MSVC
754 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
756 using namespace ::SourceHook; \
758 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
759 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
761 return (m_CC->GetThisPtr()->*m_MFP)call; \
763 /* It's hooked. Call the original function. */ \
766 RetType(EmptyClass::*mfpnew)prms; \
771 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
772 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
775 #elif SH_COMP == SH_COMP_GCC
777 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
779 using namespace ::SourceHook; \
781 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
782 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
784 return (m_CC->GetThisPtr()->*m_MFP)call; \
786 /* It's hooked. Call the original function. */ \
789 RetType(EmptyClass::*mfpnew)prms; \
796 u.s.addr = origfunc; \
797 u.s.adjustor = mfi.thisptroffs; \
799 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
806 template<class CCType, class RetType, class MFPType> class ExecutableClass
811 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
816 // Support for @$@ arguments
817 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
818 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
824 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
825 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
826 // The only purpose of the mfp2 parameter is to extract the return type
829 // Support for @$@ arguments
830 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
831 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
832 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
834 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
837 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
838 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
839 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
841 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
846 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
847 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
850 // Support for @$@ arguments
851 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
852 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
853 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
855 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
858 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
859 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
860 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
862 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
869 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
871 #undef SH_MAKE_EXECUTABLECLASS_OB
874 // The pope is dead. -> :(