1 /* ======== SourceHook ========
3 * No warranties of any kind
4 * ============================
9 * @brief Contains the public SourceHook API
12 #ifndef __SOURCEHOOK_H__
13 #define __SOURCEHOOK_H__
16 #define SH_GLOB_SHPTR g_SHPtr
19 #ifndef SH_GLOB_PLUGPTR
20 #define SH_GLOB_PLUGPTR g_PLID
23 #define SH_ASSERT(x, info) if (!(x)) __asm { int 3 }
26 #define SH_SYS_WIN32 1
27 #define SH_SYS_LINUX 2
30 # define SH_SYS SH_SYS_WIN32
31 #elif defined __linux__
32 # define SH_SYS SH_SYS_LINUX
34 # error Unsupported system
39 #define SH_COMP_MSVC 2
42 # define SH_COMP SH_COMP_MSVC
43 #elif defined __GNUC__
44 # define SH_COMP SH_COMP_GCC
46 # error Unsupported compiler
49 #if SH_COMP==SH_COMP_MSVC
50 # define vsnprintf _vsnprintf
53 #include "FastDelegate.h"
54 #include "sh_memfuncinfo.h"
55 #include "sh_memory.h"
63 // Flags returned by a plugin's api function.
64 // NOTE: order is crucial, as greater/less comparisons are made.
67 MRES_IGNORED=0, // plugin didn't take any action
68 MRES_HANDLED, // plugin did something, but real function should still be called
69 MRES_OVERRIDE, // call real function, but use my return value
70 MRES_SUPERCEDE, // skip real function; use my return value
76 const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions
79 * @brief An empty class. No inheritance used. Used for original-function-call hacks
86 * @brief A plugin typedef
88 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
89 * and every plugin has a unique identifier, everything is ok.
93 enum HookManagerAction
95 HA_GetInfo = 0, // -> Only store info
96 HA_Register, // -> Save the pointer for future reference
97 HA_Unregister // -> Clear the saved pointer
100 struct HookManagerInfo;
103 * @brief Pointer to hook manager type
105 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
107 * @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
108 * future reference (mainly if something should get hooked to its hookfunc)
110 typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
115 virtual void DeleteThis() = 0; // Ugly, I know
116 virtual bool IsEqual(ISHDelegate *other) = 0;
119 template <class T> class CSHDelegate : public ISHDelegate
123 CSHDelegate(T deleg) : m_Deleg(deleg)
127 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
136 bool IsEqual(ISHDelegate *other)
138 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
148 * @brief This structure contains information about a hook manager (hook manager)
150 struct HookManagerInfo
158 ISHDelegate *handler; //!< Pointer to the handler
159 bool paused; //!< If true, the hook should not be executed
160 Plugin plug; //!< The owner plugin
162 void *ptr; //!< Pointer to the interface instance
163 std::list<Hook> hooks_pre; //!< A list of pre-hooks
164 std::list<Hook> hooks_post; //!< A list of post-hooks
165 bool operator ==(void *other) const
171 void *vfnptr; //!< Pointer to the function
172 void *orig_entry; //!< The original vtable entry
174 typedef std::list<Iface> IfaceList;
175 typedef IfaceList::iterator IfaceListIter;
176 IfaceList ifaces; //!< List of interface pointers
178 bool operator ==(void *other)
180 return vfnptr == other;
184 Plugin plug; //!< The owner plugin
185 const char *proto; //!< The prototype of the function the hook manager is responsible for
186 int vtbl_idx; //!< The vtable index
187 int vtbl_offs; //!< The vtable offset
188 HookManagerPubFunc func; //!< The interface to the hook manager
190 void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
192 typedef std::list<VfnPtr> VfnPtrList;
193 typedef VfnPtrList::iterator VfnPtrListIter;
194 VfnPtrList vfnptrs; //!< List of hooked interfaces
197 typedef std::vector<void*> OrigFuncs;
198 typedef std::map<int, OrigFuncs> OrigVTables;
200 template<class B> struct CallClass
206 typedef CallClass<void> GenericCallClass;
209 * @brief The main SourceHook interface
217 * @return True if the function succeeded, false otherwise
219 * @param plug The unique identifier of the plugin that calls this function
220 * @param iface The interface pointer
221 * @param ifacesize The size of the class iface points to
222 * @param myHookMan A hook manager function that should be capable of handling the function
223 * @param handler A pointer to a FastDelegate containing the hook handler
224 * @param post Set to true if you want a post handler
226 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
229 * @brief Removes a hook.
231 * @return True if the function succeeded, false otherwise
233 * @param plug The unique identifier of the plugin that calls this function
234 * @param iface The interface pointer
235 * @param myHookMan A hook manager function that should be capable of handling the function
236 * @param handler A pointer to a FastDelegate containing the hook handler
237 * @param post Set to true if you want a post handler
239 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
242 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
244 * @param plug The unique identifier of the plugin in question
246 virtual bool IsPluginInUse(Plugin plug) = 0;
249 * @brief Return a pointer to a callclass. Generate a new one if required.
251 * @param iface The interface pointer
252 * @param size Size of the class
254 virtual GenericCallClass *GetCallClass(void *iface) = 0;
257 * @brief Release a callclass
259 * @param ptr Pointer to the callclass
261 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
263 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
264 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
265 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
266 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
267 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
268 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
269 //////////////////////////////////////////////////////////////////////////
271 virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
272 virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
273 virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
274 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
275 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
276 virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
280 /************************************************************************/
281 /* High level interface */
282 /************************************************************************/
283 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
284 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
285 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
287 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
288 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
289 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
290 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
291 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
295 * @brief Get/generate callclass for an interface pointer
297 * @param ifaceptr The interface pointer
299 template<class ifacetype>
300 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
302 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
303 shptr->GetCallClass(reinterpret_cast<void*>(ptr)));
306 template<class ifacetype>
307 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
309 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
312 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
313 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
315 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
316 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
317 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
318 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
319 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
320 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
322 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
323 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
324 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
325 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
326 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
327 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
334 #if SH_COMP == SH_COMP_MSVC
335 # define SH_SETUP_MFP(mfp) \
336 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
337 #elif SH_COMP == SH_COMP_GCC
338 # define SH_SETUP_MFP(mfp) \
339 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
340 reinterpret_cast<void**>(&mfp)[1] = 0;
342 # error Not supported yet.
345 //////////////////////////////////////////////////////////////////////////
346 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
348 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
349 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
351 using namespace ::SourceHook; \
352 if (action == ::SourceHook::HA_GetInfo) \
354 param->proto = ms_Proto; \
356 GetFuncInfo(funcptr, mfi); \
357 param->vtbl_idx = mfi.vtblindex; \
358 param->vtbl_offs = mfi.vtbloffs; \
360 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
361 param->hookfunc_vfnptr = \
362 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
365 else if (action == ::SourceHook::HA_Register) \
370 else if (action == ::SourceHook::HA_Unregister) \
379 // It has to be possible to use the macros in namespaces
380 // -> So we need to access and extend the global SourceHook namespace
381 // We use a namespace alias for this
382 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
383 struct SH_FHCls(ifacetype,ifacefunc,overload) \
385 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
386 static ::SourceHook::HookManagerInfo *ms_HI; \
387 static const char *ms_Proto; \
388 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
390 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
392 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
393 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
394 ::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
395 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
396 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
398 using namespace ::SourceHook; \
400 GetFuncInfo(funcptr, mfi); \
401 if (mfi.thisptroffs < 0) \
402 return false; /* No virtual inheritance supported */ \
404 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
405 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
406 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
408 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
409 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
411 using namespace ::SourceHook; \
413 GetFuncInfo(funcptr, mfi); \
414 if (mfi.thisptroffs < 0) \
415 return false; /* No virtual inheritance supported */ \
417 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
418 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
419 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
422 #define SH_SETUPCALLS(rettype, paramtypes, params) \
423 /* 1) Find the vfnptr */ \
424 using namespace ::SourceHook; \
425 void *ourvfnptr = reinterpret_cast<void*>( \
426 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
428 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
429 ms_HI->vfnptrs.end(), ourvfnptr); \
430 if (vfptriter == ms_HI->vfnptrs.end()) \
432 /* Bleh? Should be impossible! */ \
433 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
435 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
436 /* 2) Find the iface */ \
437 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
438 if (ifiter == vfnptr.ifaces.end()) \
440 /* The iface info was not found. Redirect the call to the original function. */ \
441 rettype (EmptyClass::*mfp)paramtypes; \
443 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
445 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
446 /* 2) Declare some vars and set it up */ \
447 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
448 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
450 rettype override_ret; \
451 rettype plugin_ret; \
452 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
453 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
454 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
455 status = MRES_IGNORED; \
456 SH_GLOB_SHPTR->SetIfacePtr(this); \
457 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
458 SH_GLOB_SHPTR->SetOverrideRet(NULL);
460 #define SH_CALL_HOOKS(post, params) \
461 prev_res = MRES_IGNORED; \
462 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
464 if (hiter->paused) continue; \
465 cur_res = MRES_IGNORED; \
466 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
467 prev_res = cur_res; \
468 if (cur_res > status) \
470 if (cur_res >= MRES_OVERRIDE) \
472 override_ret = plugin_ret; \
473 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
477 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
478 if (status != MRES_SUPERCEDE) \
480 rettype (EmptyClass::*mfp)paramtypes; \
482 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
485 orig_ret = override_ret;
487 #define SH_RETURN() \
488 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
490 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
491 SH_SETUPCALLS(rettype, paramtypes, params) \
492 SH_CALL_HOOKS(pre, params) \
493 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
494 SH_CALL_HOOKS(post, params) \
497 //////////////////////////////////////////////////////////////////////////
498 #define SH_SETUPCALLS_void(paramtypes, params) \
499 using namespace ::SourceHook; \
500 /* 1) Find the vfnptr */ \
501 void *ourvfnptr = reinterpret_cast<void*>( \
502 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
504 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
505 ms_HI->vfnptrs.end(), ourvfnptr); \
506 if (vfptriter == ms_HI->vfnptrs.end()) \
508 /* Bleh? Should be impossible! */ \
509 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
511 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
512 /* 2) Find the iface */ \
513 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
514 if (ifiter == vfnptr.ifaces.end()) \
516 /* The iface info was not found. Redirect the call to the original function. */ \
517 void (EmptyClass::*mfp)paramtypes; \
519 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
522 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
523 /* 2) Declare some vars and set it up */ \
524 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
525 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
526 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
527 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
528 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
529 status = MRES_IGNORED; \
530 SH_GLOB_SHPTR->SetIfacePtr(this); \
531 SH_GLOB_SHPTR->SetOverrideRet(NULL);
533 #define SH_CALL_HOOKS_void(post, params) \
534 prev_res = MRES_IGNORED; \
535 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
537 if (hiter->paused) continue; \
538 cur_res = MRES_IGNORED; \
539 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
540 prev_res = cur_res; \
541 if (cur_res > status) \
545 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
546 if (status != MRES_SUPERCEDE) \
548 void (EmptyClass::*mfp)paramtypes; \
550 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
553 #define SH_RETURN_void()
555 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
556 SH_SETUPCALLS_void(paramtypes, params) \
557 SH_CALL_HOOKS_void(pre, params) \
558 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
559 SH_CALL_HOOKS_void(post, params) \
563 // Special vafmt handlers
564 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
565 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
566 SH_CALL_HOOKS(pre, params_plug) \
567 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
568 SH_CALL_HOOKS(post, params_plug) \
571 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
572 SH_SETUPCALLS_void(paramtypes, params_orig) \
573 SH_CALL_HOOKS_void(pre, params_plug) \
574 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
575 SH_CALL_HOOKS_void(post, params_plug) \
578 //////////////////////////////////////////////////////////////////////////
581 // ********* Support for @$@ arguments *********
582 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
583 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
584 (&ifacetype::ifacefunc))) \
585 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
586 virtual rettype Func(@param%% p%%|, @) \
587 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
588 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
589 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
591 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
592 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
593 (&ifacetype::ifacefunc))) \
594 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
595 virtual void Func(@param%% p%%|, @) \
596 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
597 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
598 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
600 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
601 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
602 (&ifacetype::ifacefunc))) \
603 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
604 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
606 char buf[STRBUF_LEN]; \
609 vsnprintf(buf, sizeof(buf), fmt, ap); \
611 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
613 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
614 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
616 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
617 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
618 (&ifacetype::ifacefunc))) \
619 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
620 virtual void Func(@param%% p%%|, @@, @const char *, ...) \
622 char buf[STRBUF_LEN]; \
625 vsnprintf(buf, sizeof(buf), fmt, ap); \
627 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
629 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
630 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
635 //////////////////////////////////////////////////////////////////////////
639 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
641 using namespace ::SourceHook; \
643 MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
644 OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
645 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
646 return (m_CC->ptr->*m_MFP)call; \
648 /* It's hooked. Call the original function. */ \
651 RetType(EmptyClass::*mfpnew)prms; \
654 u.addr = iter->second[mfi.vtblindex]; \
656 return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
662 template<class CCType, class RetType, class MFPType> class ExecutableClass
667 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
672 // Support for @$@ arguments
673 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
674 SH_MAKE_EXECUTABLECLASS_OB((@p%%@), (@Param%%@))
681 // Support for @$@ arguments
682 template <class X, class Y, class RetType@, @@class Param%%|, @>
683 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
684 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
686 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
689 template <class X, class Y, class RetType@, @@class Param%%|, @>
690 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>
691 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @)const)
693 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
698 #undef SH_MAKE_EXECUTABLECLASS_BODY
701 // The pope is dead. -> :(