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
24 # define SH_ASSERT__(x, info, file, line, func) \
25 ((printf("SOURCEHOOK DEBUG ASSERTION FAILED:\n %s:%u(%s): %s\n", file, line, func, info), true) ? (abort(), 0) : 0)
26 # define SH_ASSERT(x, info) if (!(x)) SH_ASSERT__(x, info, __FILE__, __LINE__, __FUNCTION__)
28 # define SH_ASSERT(x, info)
32 #define SH_SYS_WIN32 1
33 #define SH_SYS_LINUX 2
36 # define SH_SYS SH_SYS_WIN32
37 #elif defined __linux__
38 # define SH_SYS SH_SYS_LINUX
40 # error Unsupported system
45 #define SH_COMP_MSVC 2
48 # define SH_COMP SH_COMP_MSVC
49 #elif defined __GNUC__
50 # define SH_COMP SH_COMP_GCC
52 # error Unsupported compiler
55 #if SH_COMP==SH_COMP_MSVC
56 # define vsnprintf _vsnprintf
59 #include "FastDelegate.h"
60 #include "sh_memfuncinfo.h"
61 #include "sh_memory.h"
69 // Flags returned by a plugin's api function.
70 // NOTE: order is crucial, as greater/less comparisons are made.
73 MRES_IGNORED=0, // plugin didn't take any action
74 MRES_HANDLED, // plugin did something, but real function should still be called
75 MRES_OVERRIDE, // call real function, but use my return value
76 MRES_SUPERCEDE // skip real function; use my return value
82 const int STRBUF_LEN=8192; // In bytes, for "vafmt" functions
85 * @brief An empty class. No inheritance used. Used for original-function-call hacks
92 * @brief A plugin typedef
94 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
95 * and every plugin has a unique identifier, everything is ok.
99 enum HookManagerAction
101 HA_GetInfo = 0, // -> Only store info
102 HA_Register, // -> Save the pointer for future reference
103 HA_Unregister // -> Clear the saved pointer
106 struct HookManagerInfo;
109 * @brief Pointer to hook manager type
111 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
113 * @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
114 * future reference (mainly if something should get hooked to its hookfunc)
116 typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
121 virtual void DeleteThis() = 0; // Ugly, I know
122 virtual bool IsEqual(ISHDelegate *other) = 0;
125 template <class T> class CSHDelegate : public ISHDelegate
129 CSHDelegate(T deleg) : m_Deleg(deleg)
133 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
142 bool IsEqual(ISHDelegate *other)
144 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
154 * @brief This structure contains information about a hook manager (hook manager)
156 struct HookManagerInfo
164 ISHDelegate *handler; //!< Pointer to the handler
165 bool paused; //!< If true, the hook should not be executed
166 Plugin plug; //!< The owner plugin
168 void *ptr; //!< Pointer to the interface instance
169 std::list<Hook> hooks_pre; //!< A list of pre-hooks
170 std::list<Hook> hooks_post; //!< A list of post-hooks
171 bool operator ==(void *other) const
177 void *vfnptr; //!< Pointer to the function
178 void *orig_entry; //!< The original vtable entry
180 typedef std::list<Iface> IfaceList;
181 typedef IfaceList::iterator IfaceListIter;
182 IfaceList ifaces; //!< List of interface pointers
184 bool operator ==(void *other)
186 return vfnptr == other;
190 Plugin plug; //!< The owner plugin
191 const char *proto; //!< The prototype of the function the hook manager is responsible for
192 int vtbl_idx; //!< The vtable index
193 int vtbl_offs; //!< The vtable offset
194 HookManagerPubFunc func; //!< The interface to the hook manager
196 void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
198 typedef std::list<VfnPtr> VfnPtrList;
199 typedef VfnPtrList::iterator VfnPtrListIter;
200 VfnPtrList vfnptrs; //!< List of hooked interfaces
203 typedef std::vector<void*> OrigFuncs;
204 typedef std::map<int, OrigFuncs> OrigVTables;
206 template<class B> struct CallClass
212 typedef CallClass<void> GenericCallClass;
215 * @brief The main SourceHook interface
223 * @return True if the function succeeded, false otherwise
225 * @param plug The unique identifier of the plugin that calls this function
226 * @param iface The interface pointer
227 * @param ifacesize The size of the class iface points to
228 * @param myHookMan A hook manager function that should be capable of handling the function
229 * @param handler A pointer to a FastDelegate containing the hook handler
230 * @param post Set to true if you want a post handler
232 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
235 * @brief Removes a hook.
237 * @return True if the function succeeded, false otherwise
239 * @param plug The unique identifier of the plugin that calls this function
240 * @param iface The interface pointer
241 * @param myHookMan A hook manager function that should be capable of handling the function
242 * @param handler A pointer to a FastDelegate containing the hook handler
243 * @param post Set to true if you want a post handler
245 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
248 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
250 * @param plug The unique identifier of the plugin in question
252 virtual bool IsPluginInUse(Plugin plug) = 0;
255 * @brief Return a pointer to a callclass. Generate a new one if required.
257 * @param iface The interface pointer
258 * @param size Size of the class
260 virtual GenericCallClass *GetCallClass(void *iface) = 0;
263 * @brief Release a callclass
265 * @param ptr Pointer to the callclass
267 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
269 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
270 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
271 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
272 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
273 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
274 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
275 //////////////////////////////////////////////////////////////////////////
277 virtual META_RES &GetCurResRef() = 0; //!< Gets the pointer to the current meta result
278 virtual META_RES &GetPrevResRef() = 0; //!< Gets the pointer to the previous meta result
279 virtual META_RES &GetStatusRef() = 0; //!< Gets the pointer to the status variable
280 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
281 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
282 virtual void SetIfacePtr(void *ptr) = 0; //!< Sets the interface this pointer
286 /************************************************************************/
287 /* High level interface */
288 /************************************************************************/
289 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
290 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
291 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
293 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
294 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
295 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
296 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
297 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
301 * @brief Get/generate callclass for an interface pointer
303 * @param ifaceptr The interface pointer
305 template<class ifacetype>
306 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
308 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
309 shptr->GetCallClass(reinterpret_cast<void*>(ptr)));
312 template<class ifacetype>
313 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
315 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
318 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
319 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
321 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
322 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
323 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
324 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
325 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
326 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
328 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
329 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
330 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
331 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
332 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
333 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
340 #if SH_COMP == SH_COMP_MSVC
341 # define SH_SETUP_MFP(mfp) \
342 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
343 #elif SH_COMP == SH_COMP_GCC
344 # define SH_SETUP_MFP(mfp) \
345 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
346 reinterpret_cast<void**>(&mfp)[1] = 0;
348 # error Not supported yet.
351 //////////////////////////////////////////////////////////////////////////
352 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
354 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
355 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
357 using namespace ::SourceHook; \
358 if (action == ::SourceHook::HA_GetInfo) \
360 param->proto = ms_Proto; \
362 GetFuncInfo(funcptr, mfi); \
363 param->vtbl_idx = mfi.vtblindex; \
364 param->vtbl_offs = mfi.vtbloffs; \
366 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
367 param->hookfunc_vfnptr = \
368 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
371 else if (action == ::SourceHook::HA_Register) \
376 else if (action == ::SourceHook::HA_Unregister) \
385 // It has to be possible to use the macros in namespaces
386 // -> So we need to access and extend the global SourceHook namespace
387 // We use a namespace alias for this
388 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
389 struct SH_FHCls(ifacetype,ifacefunc,overload) \
391 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
392 static ::SourceHook::HookManagerInfo *ms_HI; \
393 static const char *ms_Proto; \
394 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
396 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
398 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
399 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
400 ::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
401 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
402 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
404 using namespace ::SourceHook; \
406 GetFuncInfo(funcptr, mfi); \
407 if (mfi.thisptroffs < 0) \
408 return false; /* No virtual inheritance supported */ \
410 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
411 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
412 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
414 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
415 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
417 using namespace ::SourceHook; \
419 GetFuncInfo(funcptr, mfi); \
420 if (mfi.thisptroffs < 0) \
421 return false; /* No virtual inheritance supported */ \
423 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
424 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
425 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
428 #define SH_SETUPCALLS(rettype, paramtypes, params) \
429 /* 1) Find the vfnptr */ \
430 using namespace ::SourceHook; \
431 void *ourvfnptr = reinterpret_cast<void*>( \
432 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
434 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
435 ms_HI->vfnptrs.end(), ourvfnptr); \
436 if (vfptriter == ms_HI->vfnptrs.end()) \
438 /* Bleh? Should be impossible! */ \
439 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
441 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
442 /* 2) Find the iface */ \
443 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
444 if (ifiter == vfnptr.ifaces.end()) \
446 /* The iface info was not found. Redirect the call to the original function. */ \
447 rettype (EmptyClass::*mfp)paramtypes; \
449 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
451 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
452 /* 2) Declare some vars and set it up */ \
453 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
454 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
456 rettype override_ret; \
457 rettype plugin_ret; \
458 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
459 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
460 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
461 status = MRES_IGNORED; \
462 SH_GLOB_SHPTR->SetIfacePtr(this); \
463 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
464 SH_GLOB_SHPTR->SetOverrideRet(NULL);
466 #define SH_CALL_HOOKS(post, params) \
467 prev_res = MRES_IGNORED; \
468 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
470 if (hiter->paused) continue; \
471 cur_res = MRES_IGNORED; \
472 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
473 prev_res = cur_res; \
474 if (cur_res > status) \
476 if (cur_res >= MRES_OVERRIDE) \
478 override_ret = plugin_ret; \
479 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
483 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
484 if (status != MRES_SUPERCEDE) \
486 rettype (EmptyClass::*mfp)paramtypes; \
488 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
491 orig_ret = override_ret;
493 #define SH_RETURN() \
494 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
496 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
497 SH_SETUPCALLS(rettype, paramtypes, params) \
498 SH_CALL_HOOKS(pre, params) \
499 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
500 SH_CALL_HOOKS(post, params) \
503 //////////////////////////////////////////////////////////////////////////
504 #define SH_SETUPCALLS_void(paramtypes, params) \
505 using namespace ::SourceHook; \
506 /* 1) Find the vfnptr */ \
507 void *ourvfnptr = reinterpret_cast<void*>( \
508 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
510 HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
511 ms_HI->vfnptrs.end(), ourvfnptr); \
512 if (vfptriter == ms_HI->vfnptrs.end()) \
514 /* Bleh? Should be impossible! */ \
515 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
517 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
518 /* 2) Find the iface */ \
519 HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
520 if (ifiter == vfnptr.ifaces.end()) \
522 /* The iface info was not found. Redirect the call to the original function. */ \
523 void (EmptyClass::*mfp)paramtypes; \
525 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
528 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
529 /* 2) Declare some vars and set it up */ \
530 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
531 std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
532 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
533 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
534 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
535 status = MRES_IGNORED; \
536 SH_GLOB_SHPTR->SetIfacePtr(this); \
537 SH_GLOB_SHPTR->SetOverrideRet(NULL);
539 #define SH_CALL_HOOKS_void(post, params) \
540 prev_res = MRES_IGNORED; \
541 for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
543 if (hiter->paused) continue; \
544 cur_res = MRES_IGNORED; \
545 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
546 prev_res = cur_res; \
547 if (cur_res > status) \
551 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
552 if (status != MRES_SUPERCEDE) \
554 void (EmptyClass::*mfp)paramtypes; \
556 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
559 #define SH_RETURN_void()
561 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
562 SH_SETUPCALLS_void(paramtypes, params) \
563 SH_CALL_HOOKS_void(pre, params) \
564 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
565 SH_CALL_HOOKS_void(post, params) \
569 // Special vafmt handlers
570 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
571 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
572 SH_CALL_HOOKS(pre, params_plug) \
573 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
574 SH_CALL_HOOKS(post, params_plug) \
577 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
578 SH_SETUPCALLS_void(paramtypes, params_orig) \
579 SH_CALL_HOOKS_void(pre, params_plug) \
580 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
581 SH_CALL_HOOKS_void(post, params_plug) \
584 //////////////////////////////////////////////////////////////////////////
587 // ********* Support for @$@ arguments *********
588 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
589 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
590 (&ifacetype::ifacefunc))) \
591 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
592 virtual rettype Func(@param%% p%%|, @) \
593 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
594 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
595 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
597 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
598 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
599 (&ifacetype::ifacefunc))) \
600 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
601 virtual void Func(@param%% p%%|, @) \
602 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
603 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
604 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
606 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
607 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
608 (&ifacetype::ifacefunc))) \
609 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
610 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
612 char buf[::SourceHook::STRBUF_LEN]; \
615 vsnprintf(buf, sizeof(buf), fmt, ap); \
617 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
619 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
620 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
622 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
623 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
624 (&ifacetype::ifacefunc))) \
625 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
626 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
628 char buf[::SourceHook::STRBUF_LEN]; \
631 vsnprintf(buf, sizeof(buf), fmt, ap); \
633 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
635 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
636 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
641 //////////////////////////////////////////////////////////////////////////
645 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
647 using namespace ::SourceHook; \
649 MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
650 OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
651 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
652 return (m_CC->ptr->*m_MFP)call; \
654 /* It's hooked. Call the original function. */ \
657 RetType(EmptyClass::*mfpnew)prms; \
660 u.addr = iter->second[mfi.vtblindex]; \
662 return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
668 template<class CCType, class RetType, class MFPType> class ExecutableClass
673 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
678 // Support for @$@ arguments
679 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
680 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
687 // Support for @$@ arguments
688 template <class X, class Y, class RetType@, @@class Param%%|, @>
689 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
690 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
692 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
695 template <class X, class Y, class RetType@, @@class Param%%|, @>
696 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>
697 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @)const)
699 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
704 #undef SH_MAKE_EXECUTABLECLASS_BODY
707 // The pope is dead. -> :(