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::SH_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, 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::SH_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, 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) FHCls_##ift##iff##ov
334 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
335 static int HookManPubFunc(HookManagerAction action, HookManagerInfo *param) \
337 if (action == HA_GetInfo) \
339 param->proto = ms_Proto; \
341 GetFuncInfo(funcptr, mfi); \
342 param->vtbl_idx = mfi.vtblindex; \
343 param->vtbl_offs = mfi.vtbloffs; \
345 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
346 param->hookfunc_vfnptr = \
347 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
350 else if (action == HA_Register) \
355 else if (action == HA_Unregister) \
364 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
365 namespace SourceHook \
367 struct SH_FHCls(ifacetype,ifacefunc,overload) \
369 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
370 static HookManagerInfo *ms_HI; \
371 static const char *ms_Proto; \
372 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
374 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
376 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
377 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
378 HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
379 bool SH_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
380 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
383 GetFuncInfo(funcptr, mfi); \
384 if (mfi.thisptroffs < 0) \
385 return false; /* No virtual inheritance supported */ \
387 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
388 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
389 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
391 bool SH_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
392 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
395 GetFuncInfo(funcptr, mfi); \
396 if (mfi.thisptroffs < 0) \
397 return false; /* No virtual inheritance supported */ \
399 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
400 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
401 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
405 #define SH_SETUPCALLS(rettype, paramtypes, params) \
406 /* 1) Find the vfnptr */ \
407 void *ourvfnptr = reinterpret_cast<void*>( \
408 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
410 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
411 ms_HI->vfnptrs.end(), ourvfnptr); \
412 if (vfptriter == ms_HI->vfnptrs.end()) \
414 /* Bleh? Should be impossible! */ \
415 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
417 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
418 /* 2) Find the iface */ \
419 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
420 if (ifiter == vfnptr.ifaces.end()) \
422 /* The iface info was not found. Redirect the call to the original function. */ \
423 rettype (EmptyClass::*mfp)paramtypes; \
425 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
427 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
428 /* 2) Declare some vars and set it up */ \
429 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
430 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
431 rettype orig_ret, override_ret, plugin_ret; \
432 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
433 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
434 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
435 status = MRES_IGNORED; \
436 SH_GLOB_SHPTR->SetIfacePtr(this); \
437 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
438 SH_GLOB_SHPTR->SetOverrideRet(NULL);
440 #define SH_CALL_HOOKS(post, params) \
441 prev_res = MRES_IGNORED; \
442 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
444 if (hiter->paused) continue; \
445 cur_res = MRES_IGNORED; \
446 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
447 prev_res = cur_res; \
448 if (cur_res > status) \
450 if (cur_res >= MRES_OVERRIDE) \
452 override_ret = plugin_ret; \
453 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
457 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
458 if (status != MRES_SUPERCEDE) \
460 rettype (EmptyClass::*mfp)paramtypes; \
462 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
465 orig_ret = override_ret;
467 #define SH_RETURN() \
468 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
470 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
471 SH_SETUPCALLS(rettype, paramtypes, params) \
472 SH_CALL_HOOKS(pre, params) \
473 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
474 SH_CALL_HOOKS(post, params) \
477 //////////////////////////////////////////////////////////////////////////
478 #define SH_SETUPCALLS_void(paramtypes, params) \
479 /* 1) Find the vfnptr */ \
480 void *ourvfnptr = reinterpret_cast<void*>( \
481 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
483 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
484 ms_HI->vfnptrs.end(), ourvfnptr); \
485 if (vfptriter == ms_HI->vfnptrs.end()) \
487 /* Bleh? Should be impossible! */ \
488 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
490 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
491 /* 2) Find the iface */ \
492 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
493 if (ifiter == vfnptr.ifaces.end()) \
495 /* The iface info was not found. Redirect the call to the original function. */ \
496 void (EmptyClass::*mfp)paramtypes; \
498 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
501 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
502 /* 2) Declare some vars and set it up */ \
503 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
504 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
505 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
506 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
507 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
508 status = MRES_IGNORED; \
509 SH_GLOB_SHPTR->SetIfacePtr(this); \
510 SH_GLOB_SHPTR->SetOverrideRet(NULL);
512 #define SH_CALL_HOOKS_void(post, params) \
513 prev_res = MRES_IGNORED; \
514 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
516 if (hiter->paused) continue; \
517 cur_res = MRES_IGNORED; \
518 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
519 prev_res = cur_res; \
520 if (cur_res > status) \
524 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
525 if (status != MRES_SUPERCEDE) \
527 void (EmptyClass::*mfp)paramtypes; \
529 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
532 #define SH_RETURN_void()
534 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
535 SH_SETUPCALLS_void(paramtypes, params) \
536 SH_CALL_HOOKS_void(pre, params) \
537 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
538 SH_CALL_HOOKS_void(post, params) \
542 // Special vafmt handlers
543 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
544 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
545 SH_CALL_HOOKS(pre, params_plug) \
546 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
547 SH_CALL_HOOKS(post, params_plug) \
550 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
551 SH_SETUPCALLS_void(paramtypes, params_orig) \
552 SH_CALL_HOOKS_void(pre, params_plug) \
553 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
554 SH_CALL_HOOKS_void(post, params_plug) \
557 //////////////////////////////////////////////////////////////////////////
560 // ********* Support for @$@ arguments *********
561 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
562 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @)> \
563 (&ifacetype::ifacefunc))) \
564 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
565 virtual rettype Func(@param%% p%%|, @) attr \
566 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
567 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
568 (static_cast<rettype (ifacetype::*)(@param%%|, @)>(&ifacetype::ifacefunc)))
570 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
571 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @)> \
572 (&ifacetype::ifacefunc))) \
573 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
574 virtual void Func(@param%% p%%|, @) attr \
575 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
576 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
577 (static_cast<void (ifacetype::*)(@param%%|, @)>(&ifacetype::ifacefunc)))
579 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
580 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
581 (&ifacetype::ifacefunc))) \
582 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
583 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) attr \
585 char buf[STRBUF_LEN]; \
588 vsnprintf(buf, sizeof(buf), fmt, ap); \
590 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
592 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
593 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)>(&ifacetype::ifacefunc)))
595 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
596 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
597 (&ifacetype::ifacefunc))) \
598 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
599 virtual void Func(@param%% p%%|, @@, @const char *, ...) attr \
601 char buf[STRBUF_LEN]; \
604 vsnprintf(buf, sizeof(buf), fmt, ap); \
606 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
608 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
609 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)>(&ifacetype::ifacefunc)))
614 //////////////////////////////////////////////////////////////////////////
618 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
621 MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
622 OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
623 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
624 return (m_CC->ptr->*m_MFP)call; \
626 /* It's hooked. Call the original function. */ \
629 RetType(EmptyClass::*mfpnew)prms; \
632 u.addr = iter->second[mfi.vtblindex]; \
634 return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
640 template<class CCType, class RetType, class MFPType> class ExecutableClass
645 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
650 // Support for @$@ arguments
651 template<@class Param%%|, @> RetType operator()(@Param%% p%%|, @) const
652 SH_MAKE_EXECUTABLECLASS_OB((@p%%@), (@Param%%@))
659 // Support for @$@ arguments
660 template <class X, class Y, class RetType@, @@class Param%%|, @>
661 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
662 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
664 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
669 #undef SH_MAKE_EXECUTABLECLASS_BODY
672 // The pope is dead. -> :(