Added code for proper handling of thisptr adjustors, added new test, version info, more options for GetFuncInfo
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 1
20 #define SH_IMPL_VERSION 1
23 #define SH_GLOB_SHPTR g_SHPtr
26 #ifndef SH_GLOB_PLUGPTR
27 #define SH_GLOB_PLUGPTR g_PLID
31 # define SH_ASSERT__(x, info, file, line, func) \
32 ((printf("SOURCEHOOK DEBUG ASSERTION FAILED:\n %s:%u(%s): %s\n", file, line, func, info), true) ? (abort(), 0) : 0)
33 # define SH_ASSERT(x, info) if (!(x)) SH_ASSERT__(x, info, __FILE__, __LINE__, __FUNCTION__)
35 # define SH_ASSERT(x, info)
39 #define SH_SYS_WIN32 1
40 #define SH_SYS_LINUX 2
43 # define SH_SYS SH_SYS_WIN32
44 #elif defined __linux__
45 # define SH_SYS SH_SYS_LINUX
47 # error Unsupported system
52 #define SH_COMP_MSVC 2
55 # define SH_COMP SH_COMP_MSVC
56 #elif defined __GNUC__
57 # define SH_COMP SH_COMP_GCC
59 # error Unsupported compiler
62 #if SH_COMP==SH_COMP_MSVC
63 # define vsnprintf _vsnprintf
66 #include "FastDelegate.h"
67 #include "sh_memfuncinfo.h"
68 #include "sh_memory.h"
76 // Flags returned by a plugin's api function.
77 // NOTE: order is crucial, as greater/less comparisons are made.
80 MRES_IGNORED=0, // plugin didn't take any action
81 MRES_HANDLED, // plugin did something, but real function should still be called
82 MRES_OVERRIDE, // call real function, but use my return value
83 MRES_SUPERCEDE // skip real function; use my return value
89 const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions
92 * @brief An empty class. No inheritance used. Used for original-function-call hacks
99 * @brief Implicit cast.
101 template <class In, class Out>
102 inline Out implicit_cast(In input)
108 * @brief A plugin typedef
110 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
111 * and every plugin has a unique identifier, everything is ok.
115 enum HookManagerAction
117 HA_GetInfo = 0, // -> Only store info
118 HA_Register, // -> Save the pointer for future reference
119 HA_Unregister // -> Clear the saved pointer
122 struct HookManagerInfo;
125 * @brief Pointer to hook manager type
127 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
129 * @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
130 * future reference (mainly if something should get hooked to its hookfunc)
132 typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
137 virtual void DeleteThis() = 0; // Ugly, I know
138 virtual bool IsEqual(ISHDelegate *other) = 0;
141 template <class T> class CSHDelegate : public ISHDelegate
145 CSHDelegate(T deleg) : m_Deleg(deleg)
149 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
158 bool IsEqual(ISHDelegate *other)
160 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
170 * @brief This structure contains information about a hook manager (hook manager)
172 struct HookManagerInfo
180 ISHDelegate *handler; //!< Pointer to the handler
181 bool paused; //!< If true, the hook should not be executed
182 Plugin plug; //!< The owner plugin
183 int thisptr_offs; //!< This pointer offset
185 void *ptr; //!< Pointer to the interface instance
186 std::list<Hook> hooks_pre; //!< A list of pre-hooks
187 std::list<Hook> hooks_post; //!< A list of post-hooks
188 bool operator ==(void *other) const
194 void *vfnptr; //!< Pointer to the function
195 void *orig_entry; //!< The original vtable entry
197 typedef std::list<Iface> IfaceList;
198 typedef IfaceList::iterator IfaceListIter;
199 IfaceList ifaces; //!< List of interface pointers
201 bool operator ==(void *other)
203 return vfnptr == other;
207 Plugin plug; //!< The owner plugin
208 const char *proto; //!< The prototype of the function the hook manager is responsible for
209 int vtbl_idx; //!< The vtable index
210 int vtbl_offs; //!< The vtable offset
211 HookManagerPubFunc func; //!< The interface to the hook manager
213 void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
215 typedef std::list<VfnPtr> VfnPtrList;
216 typedef VfnPtrList::iterator VfnPtrListIter;
217 VfnPtrList vfnptrs; //!< List of hooked interfaces
220 typedef std::vector<void*> OrigFuncs;
221 typedef std::map<int, OrigFuncs> OrigVTables;
223 template<class B> struct CallClass
225 B *ptr; //!< Pointer to the actual object
226 size_t objsize; //!< Size of the instance
227 OrigVTables vt; //!< Info about vtables & functions
230 typedef CallClass<void> GenericCallClass;
233 * @brief The main SourceHook interface
239 * @brief Return interface version
241 virtual int GetIfaceVersion() = 0;
244 * @brief Return implementation version
246 virtual int GetImplVersion() = 0;
251 * @return True if the function succeeded, false otherwise
253 * @param plug The unique identifier of the plugin that calls this function
254 * @param iface The interface pointer
255 * @param ifacesize The size of the class iface points to
256 * @param myHookMan A hook manager function that should be capable of handling the function
257 * @param handler A pointer to a FastDelegate containing the hook handler
258 * @param post Set to true if you want a post handler
260 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
263 * @brief Removes a hook.
265 * @return True if the function succeeded, false otherwise
267 * @param plug The unique identifier of the plugin that calls this function
268 * @param iface The interface pointer
269 * @param myHookMan A hook manager function that should be capable of handling the function
270 * @param handler A pointer to a FastDelegate containing the hook handler
271 * @param post Set to true if you want a post handler
273 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
276 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
278 * @param plug The unique identifier of the plugin in question
280 virtual bool IsPluginInUse(Plugin plug) = 0;
283 * @brief Return a pointer to a callclass. Generate a new one if required.
285 * @param iface The interface pointer
286 * @param size Size of the class instance
288 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
291 * @brief Release a callclass
293 * @param ptr Pointer to the callclass
295 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
297 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
298 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
299 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
300 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
301 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
302 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
303 //////////////////////////////////////////////////////////////////////////
305 virtual META_RES &GetCurResRef() = 0; //!< Gets the reference to the current meta result
306 virtual META_RES &GetPrevResRef() = 0; //!< Gets the reference to the previous meta result
307 virtual META_RES &GetStatusRef() = 0; //!< Gets the reference to the status variable
308 virtual void* &GetIfacePtrRef() = 0; //!< Gets the reference to the interface this pointer
309 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
310 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
314 /************************************************************************/
315 /* High level interface */
316 /************************************************************************/
317 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
318 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
319 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
321 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
322 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
323 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
324 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
325 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
329 * @brief Get/generate callclass for an interface pointer
331 * @param ifaceptr The interface pointer
333 template<class ifacetype>
334 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
336 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
337 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
340 template<class ifacetype>
341 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
343 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
346 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
347 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
349 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
350 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
352 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
353 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
354 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
355 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
357 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
358 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
360 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
361 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
362 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
363 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
370 #if SH_COMP == SH_COMP_MSVC
371 # define SH_SETUP_MFP(mfp) \
372 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
373 #elif SH_COMP == SH_COMP_GCC
374 # define SH_SETUP_MFP(mfp) \
375 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
376 reinterpret_cast<void**>(&mfp)[1] = 0;
378 # error Not supported yet.
381 //////////////////////////////////////////////////////////////////////////
382 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
384 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
385 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
387 using namespace ::SourceHook; \
388 /* Verify interface version */ \
389 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
392 if (action == ::SourceHook::HA_GetInfo) \
394 param->proto = ms_Proto; \
396 GetFuncInfo(funcptr, mfi); \
397 param->vtbl_idx = mfi.vtblindex; \
398 param->vtbl_offs = mfi.vtbloffs; \
400 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
401 param->hookfunc_vfnptr = \
402 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
405 else if (action == ::SourceHook::HA_Register) \
410 else if (action == ::SourceHook::HA_Unregister) \
419 // It has to be possible to use the macros in namespaces
420 // -> So we need to access and extend the global SourceHook namespace
421 // We use a namespace alias for this
422 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
423 struct SH_FHCls(ifacetype,ifacefunc,overload) \
425 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
426 static ::SourceHook::HookManagerInfo *ms_HI; \
427 static const char *ms_Proto; \
428 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
430 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
432 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
433 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
434 ::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
435 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
436 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
438 using namespace ::SourceHook; \
440 GetFuncInfo(funcptr, mfi); \
441 if (mfi.thisptroffs < 0) \
442 return false; /* No virtual inheritance supported */ \
444 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
445 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
446 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
448 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
449 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
451 using namespace ::SourceHook; \
453 GetFuncInfo(funcptr, mfi); \
454 if (mfi.thisptroffs < 0) \
455 return false; /* No virtual inheritance supported */ \
457 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
458 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
459 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
462 #define SH_SETUPCALLS(rettype, paramtypes, params) \
463 /* 1) Find the vfnptr */ \
464 using namespace ::SourceHook; \
465 void *ourvfnptr = reinterpret_cast<void*>( \
466 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
468 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
469 ms_HI->vfnptrs.end(), ourvfnptr); \
470 if (vfptriter == ms_HI->vfnptrs.end()) \
472 /* Bleh? Should be impossible! */ \
473 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
475 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
476 /* 2) Find the iface */ \
477 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
478 if (ifiter == vfnptr.ifaces.end()) \
480 /* The iface info was not found. Redirect the call to the original function. */ \
481 rettype (EmptyClass::*mfp)paramtypes; \
483 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
485 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
486 /* 2) Declare some vars and set it up */ \
487 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
488 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
490 rettype override_ret; \
491 rettype plugin_ret; \
492 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
493 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
494 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
495 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
496 status = MRES_IGNORED; \
497 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
498 SH_GLOB_SHPTR->SetOverrideRet(NULL);
500 #define SH_CALL_HOOKS(post, params) \
501 prev_res = MRES_IGNORED; \
502 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
504 if (hiter->paused) continue; \
505 cur_res = MRES_IGNORED; \
506 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - hiter->thisptr_offs); \
507 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
508 prev_res = cur_res; \
509 if (cur_res > status) \
511 if (cur_res >= MRES_OVERRIDE) \
513 override_ret = plugin_ret; \
514 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
518 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
519 if (status != MRES_SUPERCEDE) \
521 rettype (EmptyClass::*mfp)paramtypes; \
523 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
526 orig_ret = override_ret;
528 #define SH_RETURN() \
529 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
531 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
532 SH_SETUPCALLS(rettype, paramtypes, params) \
533 SH_CALL_HOOKS(pre, params) \
534 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
535 SH_CALL_HOOKS(post, params) \
538 //////////////////////////////////////////////////////////////////////////
539 #define SH_SETUPCALLS_void(paramtypes, params) \
540 using namespace ::SourceHook; \
541 /* 1) Find the vfnptr */ \
542 void *ourvfnptr = reinterpret_cast<void*>( \
543 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
545 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
546 ms_HI->vfnptrs.end(), ourvfnptr); \
547 if (vfptriter == ms_HI->vfnptrs.end()) \
549 /* Bleh? Should be impossible! */ \
550 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
552 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
553 /* 2) Find the iface */ \
554 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
555 if (ifiter == vfnptr.ifaces.end()) \
557 /* The iface info was not found. Redirect the call to the original function. */ \
558 void (EmptyClass::*mfp)paramtypes; \
560 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
563 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
564 /* 2) Declare some vars and set it up */ \
565 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
566 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
567 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
568 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
569 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
570 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
571 status = MRES_IGNORED; \
572 SH_GLOB_SHPTR->SetOverrideRet(NULL);
574 #define SH_CALL_HOOKS_void(post, params) \
575 prev_res = MRES_IGNORED; \
576 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
578 if (hiter->paused) continue; \
579 cur_res = MRES_IGNORED; \
580 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - hiter->thisptr_offs); \
581 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
582 prev_res = cur_res; \
583 if (cur_res > status) \
587 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
588 if (status != MRES_SUPERCEDE) \
590 void (EmptyClass::*mfp)paramtypes; \
592 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
595 #define SH_RETURN_void()
597 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
598 SH_SETUPCALLS_void(paramtypes, params) \
599 SH_CALL_HOOKS_void(pre, params) \
600 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
601 SH_CALL_HOOKS_void(post, params) \
605 // Special vafmt handlers
606 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
607 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
608 SH_CALL_HOOKS(pre, params_plug) \
609 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
610 SH_CALL_HOOKS(post, params_plug) \
613 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
614 SH_SETUPCALLS_void(paramtypes, params_orig) \
615 SH_CALL_HOOKS_void(pre, params_plug) \
616 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
617 SH_CALL_HOOKS_void(post, params_plug) \
620 //////////////////////////////////////////////////////////////////////////
623 // ********* Support for @$@ arguments *********
624 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
625 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
626 (&ifacetype::ifacefunc))) \
627 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
628 virtual rettype Func(@param%% p%%|, @) \
629 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
630 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
631 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
633 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
634 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
635 (&ifacetype::ifacefunc))) \
636 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
637 virtual void Func(@param%% p%%|, @) \
638 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
639 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
640 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
642 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
643 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
644 (&ifacetype::ifacefunc))) \
645 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
646 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
648 char buf[::SourceHook::STRBUF_LEN]; \
651 vsnprintf(buf, sizeof(buf), fmt, ap); \
653 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
655 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
656 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
658 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
659 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
660 (&ifacetype::ifacefunc))) \
661 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
662 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
664 char buf[::SourceHook::STRBUF_LEN]; \
667 vsnprintf(buf, sizeof(buf), fmt, ap); \
669 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
671 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
672 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
677 //////////////////////////////////////////////////////////////////////////
681 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
683 using namespace ::SourceHook; \
685 GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
686 OrigVTables::const_iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
687 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
688 return (m_CC->ptr->*m_MFP)call; \
690 /* It's hooked. Call the original function. */ \
693 RetType(EmptyClass::*mfpnew)prms; \
696 u.addr = iter->second[mfi.vtblindex]; \
698 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->ptr) + mfi.thisptroffs); \
699 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
705 template<class CCType, class RetType, class MFPType> class ExecutableClass
710 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
715 // Support for @$@ arguments
716 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
717 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
723 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
724 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
725 // The only purpose of the mfp2 parameter is to extract the return type
728 // Support for @$@ arguments
729 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
730 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
731 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
733 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
736 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
737 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
738 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
740 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
745 #if SH_COMP != SH_COMP_MSVC
747 // **** MSVC doesn't like these, GCC needs them ****
750 // Support for @$@ arguments
751 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
752 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
753 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
755 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
758 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
759 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
760 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
762 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
769 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
771 #undef SH_MAKE_EXECUTABLECLASS_BODY
774 // The pope is dead. -> :(