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 #define SH_IFACE_VERSION 2
20 #define SH_IMPL_VERSION 2
23 #define SH_GLOB_SHPTR g_SHPtr
26 #ifndef SH_GLOB_PLUGPTR
27 #define SH_GLOB_PLUGPTR g_PLID
34 # define SH_ASSERT(x, info) \
38 printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
46 # define SH_ASSERT(x, info)
50 #define SH_SYS_WIN32 1
51 #define SH_SYS_LINUX 2
54 # define SH_SYS SH_SYS_WIN32
55 #elif defined __linux__
56 # define SH_SYS SH_SYS_LINUX
58 # error Unsupported system
63 #define SH_COMP_MSVC 2
66 # define SH_COMP SH_COMP_MSVC
67 #elif defined __GNUC__
68 # define SH_COMP SH_COMP_GCC
70 # error Unsupported compiler
73 #if SH_COMP==SH_COMP_MSVC
74 # define vsnprintf _vsnprintf
77 #define SH_PTRSIZE sizeof(void*)
79 #include "FastDelegate.h"
80 #include "sh_memfuncinfo.h"
84 // Flags returned by a plugin's api function.
85 // NOTE: order is crucial, as greater/less comparisons are made.
88 MRES_IGNORED=0, // plugin didn't take any action
89 MRES_HANDLED, // plugin did something, but real function should still be called
90 MRES_OVERRIDE, // call real function, but use my return value
91 MRES_SUPERCEDE // skip real function; use my return value
98 * @brief Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
100 const int STRBUF_LEN=4096;
103 * @brief An empty class. No inheritance used. Used for original-function-call hacks
110 * @brief Implicit cast.
112 template <class In, class Out>
113 inline Out implicit_cast(In input)
119 * @brief A plugin typedef
121 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
122 * plugin has a unique identifier, everything is ok.
123 * It should also be possible to set it to 0.
128 * @brief Specifies the actions for hook managers
130 enum HookManagerAction
132 HA_GetInfo = 0, //!< Store info about the hook manager
133 HA_Register, //!< Save the IHookManagerInfo pointer for future reference
134 HA_Unregister //!< Clear the saved pointer
137 class IHookManagerInfo;
140 * @brief Pointer to hook manager interface function
142 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
144 * @param ha What the hook manager should do
145 * @param hi A pointer to IHookManagerInfo
147 typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
152 virtual void DeleteThis() = 0; // Ugly, I know
153 virtual bool IsEqual(ISHDelegate *other) = 0;
156 template <class T> class CSHDelegate : public ISHDelegate
160 CSHDelegate(T deleg) : m_Deleg(deleg)
164 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
173 bool IsEqual(ISHDelegate *other)
175 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
188 virtual bool End() = 0;
189 virtual void Next() = 0;
190 virtual ISHDelegate *Handler() = 0;
191 virtual int ThisPtrOffs() = 0;
193 virtual IIter *GetIter() = 0;
194 virtual void ReleaseIter(IIter *pIter) = 0;
199 virtual void *GetPtr() = 0;
200 virtual IHookList *GetPreHooks() = 0;
201 virtual IHookList *GetPostHooks() = 0;
206 virtual void *GetVfnPtr() = 0;
207 virtual void *GetOrigEntry() = 0;
209 virtual IIface *FindIface(void *ptr) = 0;
212 struct IHookManagerInfo
214 virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
216 virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
217 virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
223 IHookList::IIter *m_pIter;
225 AutoHookIter(IHookList *pList)
226 : m_pList(pList), m_pIter(pList->GetIter())
232 m_pList->ReleaseIter(m_pIter);
237 return m_pIter->End();
245 ISHDelegate *Handler()
247 return m_pIter->Handler();
252 return m_pIter->ThisPtrOffs();
257 template<class B> struct CallClass
259 virtual B *GetThisPtr() = 0;
260 virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
263 typedef CallClass<void> GenericCallClass;
266 * @brief The main SourceHook interface
272 * @brief Return interface version
274 virtual int GetIfaceVersion() = 0;
277 * @brief Return implementation version
279 virtual int GetImplVersion() = 0;
284 * @return True if the function succeeded, false otherwise
286 * @param plug The unique identifier of the plugin that calls this function
287 * @param iface The interface pointer
288 * @param ifacesize The size of the class iface points to
289 * @param myHookMan A hook manager function that should be capable of handling the function
290 * @param handler A pointer to a FastDelegate containing the hook handler
291 * @param post Set to true if you want a post handler
293 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
294 ISHDelegate *handler, bool post) = 0;
297 * @brief Removes a hook.
299 * @return True if the function succeeded, false otherwise
301 * @param plug The unique identifier of the plugin that calls this function
302 * @param iface The interface pointer
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 RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
308 ISHDelegate *handler, bool post) = 0;
311 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
313 * @param plug The unique identifier of the plugin in question
315 virtual bool IsPluginInUse(Plugin plug) = 0;
318 * @brief Return a pointer to a callclass. Generate a new one if required.
320 * @param iface The interface pointer
321 * @param size Size of the class instance
323 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
326 * @brief Release a callclass
328 * @param ptr Pointer to the callclass
330 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
332 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
333 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the
334 //!< previously calledhandler
335 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
336 virtual const void *GetOrigRet() = 0; //!< Gets the original result.
337 //!< If not in post function, undefined
338 virtual const void *GetOverrideRet() = 0; //!< Gets the override result.
339 //!< If none is specified, NULL
340 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
341 //////////////////////////////////////////////////////////////////////////
343 virtual META_RES &GetCurResRef() = 0; //!< Gets the reference to the current meta result
344 virtual META_RES &GetPrevResRef() = 0; //!< Gets the reference to the previous meta result
345 virtual META_RES &GetStatusRef() = 0; //!< Gets the reference to the status variable
346 virtual void* &GetIfacePtrRef() = 0; //!< Gets the reference to the interface this pointer
347 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
348 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
352 /************************************************************************/
353 /* High level interface */
354 /************************************************************************/
355 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
356 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
357 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
359 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
360 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
361 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
362 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
363 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
367 * @brief Get/generate callclass for an interface pointer
369 * @param ifaceptr The interface pointer
371 template<class ifacetype>
372 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
374 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
375 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
378 template<class ifacetype>
379 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
381 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
384 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
385 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
387 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
388 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
390 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
391 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
392 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
393 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
395 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
396 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
398 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
399 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
400 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
401 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
408 #if SH_COMP == SH_COMP_MSVC
409 # define SH_SETUP_MFP(mfp) \
410 reinterpret_cast<void**>(&mfp)[0] = vfnptr->GetOrigEntry();
411 #elif SH_COMP == SH_COMP_GCC
412 # define SH_SETUP_MFP(mfp) \
413 reinterpret_cast<void**>(&mfp)[0] = vfnptr->GetOrigEntry(); \
414 reinterpret_cast<void**>(&mfp)[1] = 0;
416 # error Not supported yet.
419 //////////////////////////////////////////////////////////////////////////
420 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
422 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
423 SH_FHCls(ifacetype,ifacefunc,overload)() \
425 GetFuncInfo(funcptr, ms_MFI); \
428 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
430 using namespace ::SourceHook; \
431 GetFuncInfo(funcptr, ms_MFI); \
432 /* Verify interface version */ \
433 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
436 if (action == ::SourceHook::HA_GetInfo) \
438 param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, ms_Proto); \
441 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
442 param->SetHookfuncVfnptr( \
443 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
446 else if (action == ::SourceHook::HA_Register) \
451 else if (action == ::SourceHook::HA_Unregister) \
460 // It has to be possible to use the macros in namespaces
461 // -> So we need to access and extend the global SourceHook namespace
462 // We use a namespace alias for this
463 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
464 struct SH_FHCls(ifacetype,ifacefunc,overload) \
466 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
467 static ::SourceHook::MemFuncInfo ms_MFI; \
468 static ::SourceHook::IHookManagerInfo *ms_HI; \
469 static const char *ms_Proto; \
470 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
472 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
474 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
475 ::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
476 ::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
477 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
478 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
479 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
481 using namespace ::SourceHook; \
483 GetFuncInfo(funcptr, mfi); \
484 if (mfi.thisptroffs < 0) \
485 return false; /* No virtual inheritance supported */ \
487 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
488 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
489 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
491 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
492 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
494 using namespace ::SourceHook; \
496 GetFuncInfo(funcptr, mfi); \
497 if (mfi.thisptroffs < 0) \
498 return false; /* No virtual inheritance supported */ \
500 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
501 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
502 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
505 #define SH_SETUPCALLS(rettype, paramtypes, params) \
506 /* 1) Find the vfnptr */ \
507 using namespace ::SourceHook; \
508 void *ourvfnptr = reinterpret_cast<void*>( \
509 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
510 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
511 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
513 /* ... and the iface */ \
514 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
517 /* The iface info was not found. Redirect the call to the original function. */ \
518 rettype (EmptyClass::*mfp)paramtypes; \
520 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
522 /* 2) Declare some vars and set it up */ \
523 IHookList *prelist = ifinfo->GetPreHooks(); \
524 IHookList *postlist = ifinfo->GetPostHooks(); \
526 rettype override_ret; \
527 rettype plugin_ret; \
528 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
529 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
530 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
531 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
532 status = MRES_IGNORED; \
533 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
534 SH_GLOB_SHPTR->SetOverrideRet(NULL);
536 #define SH_CALL_HOOKS(post, params) \
537 prev_res = MRES_IGNORED; \
538 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
540 cur_res = MRES_IGNORED; \
541 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
542 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
543 prev_res = cur_res; \
544 if (cur_res > status) \
546 if (cur_res >= MRES_OVERRIDE) \
548 override_ret = plugin_ret; \
549 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
553 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
554 if (status != MRES_SUPERCEDE) \
556 rettype (EmptyClass::*mfp)paramtypes; \
558 orig_ret = (reinterpret_cast<EmptyClass*>(ifinfo->GetPtr())->*mfp)params; \
561 orig_ret = override_ret;
563 #define SH_RETURN() \
564 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
566 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
567 SH_SETUPCALLS(rettype, paramtypes, params) \
568 SH_CALL_HOOKS(pre, params) \
569 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
570 SH_CALL_HOOKS(post, params) \
573 //////////////////////////////////////////////////////////////////////////
574 #define SH_SETUPCALLS_void(paramtypes, params) \
575 /* 1) Find the vfnptr */ \
576 using namespace ::SourceHook; \
577 void *ourvfnptr = reinterpret_cast<void*>( \
578 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
579 IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
580 SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
582 /* ... and the iface */ \
583 IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
586 /* The iface info was not found. Redirect the call to the original function. */ \
587 void (EmptyClass::*mfp)paramtypes; \
589 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
592 /* 2) Declare some vars and set it up */ \
593 IHookList *prelist = ifinfo->GetPreHooks(); \
594 IHookList *postlist = ifinfo->GetPostHooks(); \
595 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
596 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
597 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
598 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
599 status = MRES_IGNORED; \
600 SH_GLOB_SHPTR->SetOverrideRet(NULL); \
601 SH_GLOB_SHPTR->SetOrigRet(NULL);
603 #define SH_CALL_HOOKS_void(post, params) \
604 prev_res = MRES_IGNORED; \
605 for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
607 cur_res = MRES_IGNORED; \
608 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
609 reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
610 prev_res = cur_res; \
611 if (cur_res > status) \
615 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
616 if (status != MRES_SUPERCEDE) \
618 void (EmptyClass::*mfp)paramtypes; \
620 (reinterpret_cast<EmptyClass*>(ifinfo->GetPtr())->*mfp)params; \
623 #define SH_RETURN_void()
625 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
626 SH_SETUPCALLS_void(paramtypes, params) \
627 SH_CALL_HOOKS_void(pre, params) \
628 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
629 SH_CALL_HOOKS_void(post, params) \
633 // Special vafmt handlers
634 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
635 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
636 SH_CALL_HOOKS(pre, params_plug) \
637 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
638 SH_CALL_HOOKS(post, params_plug) \
641 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
642 SH_SETUPCALLS_void(paramtypes, params_orig) \
643 SH_CALL_HOOKS_void(pre, params_plug) \
644 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
645 SH_CALL_HOOKS_void(post, params_plug) \
648 //////////////////////////////////////////////////////////////////////////
651 // ********* Support for @$@ arguments *********
652 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
653 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
654 (&ifacetype::ifacefunc))) \
655 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
656 virtual rettype Func(@param%% p%%|, @) \
657 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
658 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
659 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
661 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
662 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
663 (&ifacetype::ifacefunc))) \
664 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
665 virtual void Func(@param%% p%%|, @) \
666 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
667 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
668 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
670 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
671 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
672 (&ifacetype::ifacefunc))) \
673 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
674 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
676 char buf[::SourceHook::STRBUF_LEN]; \
679 vsnprintf(buf, sizeof(buf), fmt, ap); \
681 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
683 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
684 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
686 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
687 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
688 (&ifacetype::ifacefunc))) \
689 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
690 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
692 char buf[::SourceHook::STRBUF_LEN]; \
695 vsnprintf(buf, sizeof(buf), fmt, ap); \
697 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
699 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
700 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
705 //////////////////////////////////////////////////////////////////////////
708 #if SH_COMP == SH_COMP_MSVC
710 // :TODO: TEST THIS ON MSVC
712 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
714 using namespace ::SourceHook; \
716 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
717 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
719 return (m_CC->GetThisPtr()->*m_MFP)call; \
721 /* It's hooked. Call the original function. */ \
724 RetType(EmptyClass::*mfpnew)prms; \
729 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
730 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
733 #elif SH_COMP == SH_COMP_GCC
735 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
737 using namespace ::SourceHook; \
739 GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
740 void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
742 return (m_CC->GetThisPtr()->*m_MFP)call; \
744 /* It's hooked. Call the original function. */ \
747 RetType(EmptyClass::*mfpnew)prms; \
754 u.s.addr = origfunc; \
755 u.s.adjustor = mfi.thisptroffs; \
757 return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
764 template<class CCType, class RetType, class MFPType> class ExecutableClass
769 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
774 // Support for @$@ arguments
775 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
776 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
782 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
783 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
784 // The only purpose of the mfp2 parameter is to extract the return type
787 // Support for @$@ arguments
788 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
789 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
790 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
792 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
795 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
796 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
797 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
799 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
804 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
805 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
808 // Support for @$@ arguments
809 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
810 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
811 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
813 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
816 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
817 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
818 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
820 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
827 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
829 #undef SH_MAKE_EXECUTABLECLASS_OB
832 // The pope is dead. -> :(