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)
132 bool IsEqual(ISHDelegate *other)
134 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
144 * @brief This structure contains information about a hook manager (hook manager)
146 struct HookManagerInfo
154 ISHDelegate *handler; //!< Pointer to the handler
155 bool paused; //!< If true, the hook should not be executed
156 Plugin plug; //!< The owner plugin
158 void *ptr; //!< Pointer to the interface instance
159 std::list<Hook> hooks_pre; //!< A list of pre-hooks
160 std::list<Hook> hooks_post; //!< A list of post-hooks
161 bool operator ==(void *other) const
167 void *vfnptr; //!< Pointer to the function
168 void *orig_entry; //!< The original vtable entry
170 typedef std::list<Iface> IfaceList;
171 typedef IfaceList::iterator IfaceListIter;
174 bool operator ==(void *other)
176 return vfnptr == other;
179 Plugin plug; //!< The owner plugin
180 const char *proto; //!< The prototype of the function the hook manager is responsible for
181 int vtbl_idx; //!< The vtable index
182 int vtbl_offs; //!< The vtable offset
183 HookManagerPubFunc func; //!< The interface to the hook manager
185 void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
187 typedef std::list<VfnPtr> VfnPtrList;
188 typedef VfnPtrList::iterator VfnPtrListIter;
189 VfnPtrList vfnptrs; //!< List of hooked interfaces
192 typedef std::vector<void*> OrigFuncs;
193 typedef std::map<int, OrigFuncs> OrigVTables;
195 template<class B> struct CallClass
201 typedef CallClass<void> GenericCallClass;
204 * @brief The main SourceHook interface
212 * @return True if the function succeeded, false otherwise
214 * @param plug The unique identifier of the plugin that calls this function
215 * @param iface The interface pointer
216 * @param ifacesize The size of the class iface points to
217 * @param myHookMan A hook manager function that should be capable of handling the function
218 * @param handler A pointer to a FastDelegate containing the hook handler
219 * @param post Set to true if you want a post handler
221 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
224 * @brief Removes a hook.
226 * @return True if the function succeeded, false otherwise
228 * @param plug The unique identifier of the plugin that calls this function
229 * @param iface The interface pointer
230 * @param myHookMan A hook manager function that should be capable of handling the function
231 * @param handler A pointer to a FastDelegate containing the hook handler
232 * @param post Set to true if you want a post handler
234 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
237 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
239 * @param plug The unique identifier of the plugin in question
241 virtual bool IsPluginInUse(Plugin plug) = 0;
244 * @brief Return a pointer to a callclass. Generate a new one if required.
246 * @param iface The interface pointer
247 * @param size Size of the class
249 virtual GenericCallClass *GetCallClass(void *iface) = 0;
252 * @brief Release a callclass
254 * @param ptr Pointer to the callclass
256 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
258 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
259 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
260 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
261 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
262 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
263 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
264 //////////////////////////////////////////////////////////////////////////
266 virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
267 virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
268 virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
269 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
270 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
271 virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
275 //////////////////////////////////////////////////////////////////////////
277 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
278 #define RETURN_META(result) do { SH_GLOB_SHPTR->SetRes(result); return; } while(0)
279 #define RETURN_META_VALUE(result, value) do { SH_GLOB_SHPTR->SetRes(result); return (value); } while(0)
281 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
282 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
283 #define META_RESULT_ORIG_RET(type) *(const type *)SH_GLOB_SHPTR->GetOrigRet()
284 #define META_RESULT_OVERRIDE_RET(type) *(const type *)SH_GLOB_SHPTR->GetOverrideRet()
285 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
289 * @brief Get/generate callclass for an interface pointer
291 * @param ifaceptr The interface pointer
293 template<class ifacetype>
294 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS(ifacetype *ptr)
296 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
297 SH_GLOB_SHPTR->GetCallClass(reinterpret_cast<void*>(ptr)));
300 #define SH_RELEASE_CALLCLASS(ptr) SH_GLOB_SHPTR->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr))
302 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
303 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
304 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
305 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
306 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
307 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
309 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
310 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
311 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
312 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
313 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
314 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
320 #if SH_COMP == SH_COMP_MSVC
321 # define SH_SETUP_MFP(mfp) \
322 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
323 #elif SH_COMP == SH_COMP_GCC
324 # define SH_SETUP_MFP(mfp) \
325 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
326 reinterpret_cast<void**>(&mfp)[1] = 0;
328 # error Not supported yet.
331 //////////////////////////////////////////////////////////////////////////
332 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
334 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
335 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
337 using namespace ::SourceHook; \
338 if (action == ::SourceHook::HA_GetInfo) \
340 param->proto = ms_Proto; \
342 GetFuncInfo(funcptr, mfi); \
343 param->vtbl_idx = mfi.vtblindex; \
344 param->vtbl_offs = mfi.vtbloffs; \
346 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
347 param->hookfunc_vfnptr = \
348 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
351 else if (action == ::SourceHook::HA_Register) \
356 else if (action == ::SourceHook::HA_Unregister) \
365 // It has to be possible to use the macros in namespaces
366 // -> So we need to access and extend the global SourceHook namespace
367 // We use a namespace alias for this
368 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
369 struct SH_FHCls(ifacetype,ifacefunc,overload) \
371 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
372 static ::SourceHook::HookManagerInfo *ms_HI; \
373 static const char *ms_Proto; \
374 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
376 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
378 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
379 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
380 ::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
381 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
382 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
384 using namespace ::SourceHook; \
386 GetFuncInfo(funcptr, mfi); \
387 if (mfi.thisptroffs < 0) \
388 return false; /* No virtual inheritance supported */ \
390 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
391 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
392 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
394 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
395 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
397 using namespace ::SourceHook; \
399 GetFuncInfo(funcptr, mfi); \
400 if (mfi.thisptroffs < 0) \
401 return false; /* No virtual inheritance supported */ \
403 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
404 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
405 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
408 #define SH_SETUPCALLS(rettype, paramtypes, params) \
409 /* 1) Find the vfnptr */ \
410 using namespace ::SourceHook; \
411 void *ourvfnptr = reinterpret_cast<void*>( \
412 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
414 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
415 ms_HI->vfnptrs.end(), ourvfnptr); \
416 if (vfptriter == ms_HI->vfnptrs.end()) \
418 /* Bleh? Should be impossible! */ \
419 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
421 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
422 /* 2) Find the iface */ \
423 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
424 if (ifiter == vfnptr.ifaces.end()) \
426 /* The iface info was not found. Redirect the call to the original function. */ \
427 rettype (EmptyClass::*mfp)paramtypes; \
429 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
431 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
432 /* 2) Declare some vars and set it up */ \
433 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
434 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
436 rettype override_ret; \
437 rettype plugin_ret; \
438 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
439 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
440 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
441 status = MRES_IGNORED; \
442 SH_GLOB_SHPTR->SetIfacePtr(this); \
443 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
444 SH_GLOB_SHPTR->SetOverrideRet(NULL);
446 #define SH_CALL_HOOKS(post, params) \
447 prev_res = MRES_IGNORED; \
448 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
450 if (hiter->paused) continue; \
451 cur_res = MRES_IGNORED; \
452 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
453 prev_res = cur_res; \
454 if (cur_res > status) \
456 if (cur_res >= MRES_OVERRIDE) \
458 override_ret = plugin_ret; \
459 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
463 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
464 if (status != MRES_SUPERCEDE) \
466 rettype (EmptyClass::*mfp)paramtypes; \
468 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
471 orig_ret = override_ret;
473 #define SH_RETURN() \
474 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
476 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
477 SH_SETUPCALLS(rettype, paramtypes, params) \
478 SH_CALL_HOOKS(pre, params) \
479 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
480 SH_CALL_HOOKS(post, params) \
483 //////////////////////////////////////////////////////////////////////////
484 #define SH_SETUPCALLS_void(paramtypes, params) \
485 using namespace ::SourceHook; \
486 /* 1) Find the vfnptr */ \
487 void *ourvfnptr = reinterpret_cast<void*>( \
488 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
490 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
491 ms_HI->vfnptrs.end(), ourvfnptr); \
492 if (vfptriter == ms_HI->vfnptrs.end()) \
494 /* Bleh? Should be impossible! */ \
495 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
497 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
498 /* 2) Find the iface */ \
499 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
500 if (ifiter == vfnptr.ifaces.end()) \
502 /* The iface info was not found. Redirect the call to the original function. */ \
503 void (EmptyClass::*mfp)paramtypes; \
505 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
508 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
509 /* 2) Declare some vars and set it up */ \
510 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
511 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
512 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
513 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
514 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
515 status = MRES_IGNORED; \
516 SH_GLOB_SHPTR->SetIfacePtr(this); \
517 SH_GLOB_SHPTR->SetOverrideRet(NULL);
519 #define SH_CALL_HOOKS_void(post, params) \
520 prev_res = MRES_IGNORED; \
521 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
523 if (hiter->paused) continue; \
524 cur_res = MRES_IGNORED; \
525 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
526 prev_res = cur_res; \
527 if (cur_res > status) \
531 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
532 if (status != MRES_SUPERCEDE) \
534 void (EmptyClass::*mfp)paramtypes; \
536 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
539 #define SH_RETURN_void()
541 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
542 SH_SETUPCALLS_void(paramtypes, params) \
543 SH_CALL_HOOKS_void(pre, params) \
544 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
545 SH_CALL_HOOKS_void(post, params) \
549 // Special vafmt handlers
550 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
551 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
552 SH_CALL_HOOKS(pre, params_plug) \
553 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
554 SH_CALL_HOOKS(post, params_plug) \
557 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
558 SH_SETUPCALLS_void(paramtypes, params_orig) \
559 SH_CALL_HOOKS_void(pre, params_plug) \
560 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
561 SH_CALL_HOOKS_void(post, params_plug) \
564 //////////////////////////////////////////////////////////////////////////
567 // ********* Support for @$@ arguments *********
568 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
569 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
570 (&ifacetype::ifacefunc))) \
571 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
572 virtual rettype Func(@param%% p%%|, @) \
573 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
574 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
575 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
577 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
578 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
579 (&ifacetype::ifacefunc))) \
580 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
581 virtual void Func(@param%% p%%|, @) \
582 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
583 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
584 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
586 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
587 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
588 (&ifacetype::ifacefunc))) \
589 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
590 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
592 char buf[STRBUF_LEN]; \
595 vsnprintf(buf, sizeof(buf), fmt, ap); \
597 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
599 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
600 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
602 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
603 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
604 (&ifacetype::ifacefunc))) \
605 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
606 virtual void Func(@param%% p%%|, @@, @const char *, ...) \
608 char buf[STRBUF_LEN]; \
611 vsnprintf(buf, sizeof(buf), fmt, ap); \
613 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
615 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
616 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
621 //////////////////////////////////////////////////////////////////////////
625 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
627 using namespace ::SourceHook; \
629 MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
630 OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
631 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
632 return (m_CC->ptr->*m_MFP)call; \
634 /* It's hooked. Call the original function. */ \
637 RetType(EmptyClass::*mfpnew)prms; \
640 u.addr = iter->second[mfi.vtblindex]; \
642 return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
648 template<class CCType, class RetType, class MFPType> class ExecutableClass
653 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
658 // Support for @$@ arguments
659 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
660 SH_MAKE_EXECUTABLECLASS_OB((@p%%@), (@Param%%@))
667 // Support for @$@ arguments
668 template <class X, class Y, class RetType@, @@class Param%%|, @>
669 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
670 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
672 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
675 template <class X, class Y, class RetType@, @@class Param%%|, @>
676 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>
677 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @)const)
679 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
684 #undef SH_MAKE_EXECUTABLECLASS_BODY
687 // The pope is dead. -> :(