1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
13 * @brief Contains the public SourceHook API
16 #ifndef __SOURCEHOOK_H__
17 #define __SOURCEHOOK_H__
19 #define SH_IFACE_VERSION 1
20 #define SH_IMPL_VERSION 1
23 #define SH_GLOB_SHPTR g_SHPtr
26 #ifndef SH_GLOB_PLUGPTR
27 #define SH_GLOB_PLUGPTR g_PLID
34 # define SH_ASSERT__(x, info, file, line, func) \
35 ((printf("SOURCEHOOK DEBUG ASSERTION FAILED:\n %s:%u(%s): %s\n", file, line, func, info), true) ? (abort(), 0) : 0)
36 # define SH_ASSERT(x, info) if (!(x)) SH_ASSERT__(x, info, __FILE__, __LINE__, __FUNCTION__)
38 # define SH_ASSERT(x, info)
42 #define SH_SYS_WIN32 1
43 #define SH_SYS_LINUX 2
46 # define SH_SYS SH_SYS_WIN32
47 #elif defined __linux__
48 # define SH_SYS SH_SYS_LINUX
50 # error Unsupported system
55 #define SH_COMP_MSVC 2
58 # define SH_COMP SH_COMP_MSVC
59 #elif defined __GNUC__
60 # define SH_COMP SH_COMP_GCC
62 # error Unsupported compiler
65 #if SH_COMP==SH_COMP_MSVC
66 # define vsnprintf _vsnprintf
69 #define SH_PTRSIZE sizeof(void*)
71 #include "FastDelegate.h"
72 #include "sh_memfuncinfo.h"
73 #include "sh_memory.h"
75 #include "sh_vector.h"
76 #include "sh_tinyhash.h"
80 // Flags returned by a plugin's api function.
81 // NOTE: order is crucial, as greater/less comparisons are made.
84 MRES_IGNORED=0, // plugin didn't take any action
85 MRES_HANDLED, // plugin did something, but real function should still be called
86 MRES_OVERRIDE, // call real function, but use my return value
87 MRES_SUPERCEDE // skip real function; use my return value
93 const int STRBUF_LEN=4096; // In bytes, for "vafmt" functions
96 * @brief An empty class. No inheritance used. Used for original-function-call hacks
103 * @brief Implicit cast.
105 template <class In, class Out>
106 inline Out implicit_cast(In input)
112 * @brief A plugin typedef
114 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
115 * and every plugin has a unique identifier, everything is ok.
119 enum HookManagerAction
121 HA_GetInfo = 0, // -> Only store info
122 HA_Register, // -> Save the pointer for future reference
123 HA_Unregister // -> Clear the saved pointer
126 struct HookManagerInfo;
129 * @brief Pointer to hook manager type
131 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
133 * @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
134 * future reference (mainly if something should get hooked to its hookfunc)
136 typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
141 virtual void DeleteThis() = 0; // Ugly, I know
142 virtual bool IsEqual(ISHDelegate *other) = 0;
145 template <class T> class CSHDelegate : public ISHDelegate
149 CSHDelegate(T deleg) : m_Deleg(deleg)
153 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
162 bool IsEqual(ISHDelegate *other)
164 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
174 * @brief This structure contains information about a hook manager (hook manager)
176 struct HookManagerInfo
184 ISHDelegate *handler; //!< Pointer to the handler
185 bool paused; //!< If true, the hook should not be executed
186 Plugin plug; //!< The owner plugin
187 int thisptr_offs; //!< This pointer offset
189 void *ptr; //!< Pointer to the interface instance
190 List<Hook> hooks_pre; //!< A list of pre-hooks
191 List<Hook> hooks_post; //!< A list of post-hooks
192 bool operator ==(void *other) const
198 void *vfnptr; //!< Pointer to the function
199 void *orig_entry; //!< The original vtable entry
201 typedef List<Iface> IfaceList;
202 typedef IfaceList::iterator IfaceListIter;
203 IfaceList ifaces; //!< List of interface pointers
205 bool operator ==(void *other)
207 return vfnptr == other;
211 Plugin plug; //!< The owner plugin
212 const char *proto; //!< The prototype of the function the hook manager is responsible for
213 int vtbl_idx; //!< The vtable index
214 int vtbl_offs; //!< The vtable offset
215 HookManagerPubFunc func; //!< The interface to the hook manager
217 void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
219 typedef List<VfnPtr> VfnPtrList;
220 typedef VfnPtrList::iterator VfnPtrListIter;
221 VfnPtrList vfnptrs; //!< List of hooked interfaces
224 typedef SourceHook::CVector<void*> OrigFuncs;
225 typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
227 template<class B> struct CallClass
229 B *ptr; //!< Pointer to the actual object
230 size_t objsize; //!< Size of the instance
231 OrigVTables vt; //!< Info about vtables & functions
234 typedef CallClass<void> GenericCallClass;
237 * @brief The main SourceHook interface
242 virtual ~ISourceHook()
245 * @brief Return interface version
247 virtual int GetIfaceVersion() = 0;
250 * @brief Return implementation version
252 virtual int GetImplVersion() = 0;
257 * @return True if the function succeeded, false otherwise
259 * @param plug The unique identifier of the plugin that calls this function
260 * @param iface The interface pointer
261 * @param ifacesize The size of the class iface points to
262 * @param myHookMan A hook manager function that should be capable of handling the function
263 * @param handler A pointer to a FastDelegate containing the hook handler
264 * @param post Set to true if you want a post handler
266 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
269 * @brief Removes a hook.
271 * @return True if the function succeeded, false otherwise
273 * @param plug The unique identifier of the plugin that calls this function
274 * @param iface The interface pointer
275 * @param myHookMan A hook manager function that should be capable of handling the function
276 * @param handler A pointer to a FastDelegate containing the hook handler
277 * @param post Set to true if you want a post handler
279 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
282 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
284 * @param plug The unique identifier of the plugin in question
286 virtual bool IsPluginInUse(Plugin plug) = 0;
289 * @brief Return a pointer to a callclass. Generate a new one if required.
291 * @param iface The interface pointer
292 * @param size Size of the class instance
294 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
297 * @brief Release a callclass
299 * @param ptr Pointer to the callclass
301 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
303 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
304 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
305 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
306 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
307 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
308 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
309 //////////////////////////////////////////////////////////////////////////
311 virtual META_RES &GetCurResRef() = 0; //!< Gets the reference to the current meta result
312 virtual META_RES &GetPrevResRef() = 0; //!< Gets the reference to the previous meta result
313 virtual META_RES &GetStatusRef() = 0; //!< Gets the reference to the status variable
314 virtual void* &GetIfacePtrRef() = 0; //!< Gets the reference to the interface this pointer
315 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
316 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
320 /************************************************************************/
321 /* High level interface */
322 /************************************************************************/
323 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
324 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
325 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
327 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
328 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
329 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
330 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
331 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
335 * @brief Get/generate callclass for an interface pointer
337 * @param ifaceptr The interface pointer
339 template<class ifacetype>
340 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
342 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
343 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
346 template<class ifacetype>
347 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
349 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
352 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
353 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
355 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
356 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
358 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
359 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
360 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
361 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
363 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
364 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
366 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
367 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
368 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
369 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
376 #if SH_COMP == SH_COMP_MSVC
377 # define SH_SETUP_MFP(mfp) \
378 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
379 #elif SH_COMP == SH_COMP_GCC
380 # define SH_SETUP_MFP(mfp) \
381 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
382 reinterpret_cast<void**>(&mfp)[1] = 0;
384 # error Not supported yet.
387 //////////////////////////////////////////////////////////////////////////
388 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
390 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
391 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
393 using namespace ::SourceHook; \
394 /* Verify interface version */ \
395 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
398 if (action == ::SourceHook::HA_GetInfo) \
400 param->proto = ms_Proto; \
402 GetFuncInfo(funcptr, mfi); \
403 param->vtbl_idx = mfi.vtblindex; \
404 param->vtbl_offs = mfi.vtbloffs; \
406 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
407 param->hookfunc_vfnptr = \
408 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
411 else if (action == ::SourceHook::HA_Register) \
416 else if (action == ::SourceHook::HA_Unregister) \
425 // It has to be possible to use the macros in namespaces
426 // -> So we need to access and extend the global SourceHook namespace
427 // We use a namespace alias for this
428 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
429 struct SH_FHCls(ifacetype,ifacefunc,overload) \
431 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
432 static ::SourceHook::HookManagerInfo *ms_HI; \
433 static const char *ms_Proto; \
434 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
436 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
438 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
439 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
440 ::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
441 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
442 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
444 using namespace ::SourceHook; \
446 GetFuncInfo(funcptr, mfi); \
447 if (mfi.thisptroffs < 0) \
448 return false; /* No virtual inheritance supported */ \
450 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
451 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
452 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
454 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
455 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
457 using namespace ::SourceHook; \
459 GetFuncInfo(funcptr, mfi); \
460 if (mfi.thisptroffs < 0) \
461 return false; /* No virtual inheritance supported */ \
463 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
464 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
465 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
468 #define SH_SETUPCALLS(rettype, paramtypes, params) \
469 /* 1) Find the vfnptr */ \
470 using namespace ::SourceHook; \
471 void *ourvfnptr = reinterpret_cast<void*>( \
472 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
474 HookManagerInfo::VfnPtrListIter vfptriter = ms_HI->vfnptrs.find(ourvfnptr); \
475 if (vfptriter == ms_HI->vfnptrs.end()) \
477 /* Bleh? Should be impossible! */ \
478 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
480 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
481 /* 2) Find the iface */ \
482 HookManagerInfo::VfnPtr::IfaceListIter ifiter = vfnptr.ifaces.find(this); \
483 if (ifiter == vfnptr.ifaces.end()) \
485 /* The iface info was not found. Redirect the call to the original function. */ \
486 rettype (EmptyClass::*mfp)paramtypes; \
488 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
490 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
491 /* 2) Declare some vars and set it up */ \
492 List<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
493 List<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
495 rettype override_ret; \
496 rettype plugin_ret; \
497 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
498 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
499 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
500 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
501 status = MRES_IGNORED; \
502 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
503 SH_GLOB_SHPTR->SetOverrideRet(NULL);
505 #define SH_CALL_HOOKS(post, params) \
506 prev_res = MRES_IGNORED; \
507 for (List<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
509 if (hiter->paused) continue; \
510 cur_res = MRES_IGNORED; \
511 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - hiter->thisptr_offs); \
512 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
513 prev_res = cur_res; \
514 if (cur_res > status) \
516 if (cur_res >= MRES_OVERRIDE) \
518 override_ret = plugin_ret; \
519 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
523 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
524 if (status != MRES_SUPERCEDE) \
526 rettype (EmptyClass::*mfp)paramtypes; \
528 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
531 orig_ret = override_ret;
533 #define SH_RETURN() \
534 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
536 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
537 SH_SETUPCALLS(rettype, paramtypes, params) \
538 SH_CALL_HOOKS(pre, params) \
539 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
540 SH_CALL_HOOKS(post, params) \
543 //////////////////////////////////////////////////////////////////////////
544 #define SH_SETUPCALLS_void(paramtypes, params) \
545 using namespace ::SourceHook; \
546 /* 1) Find the vfnptr */ \
547 void *ourvfnptr = reinterpret_cast<void*>( \
548 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
550 HookManagerInfo::VfnPtrListIter vfptriter = ms_HI->vfnptrs.find(ourvfnptr); \
551 if (vfptriter == ms_HI->vfnptrs.end()) \
553 /* Bleh? Should be impossible! */ \
554 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
556 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
557 /* 2) Find the iface */ \
558 HookManagerInfo::VfnPtr::IfaceListIter ifiter = vfnptr.ifaces.find(this); \
559 if (ifiter == vfnptr.ifaces.end()) \
561 /* The iface info was not found. Redirect the call to the original function. */ \
562 void (EmptyClass::*mfp)paramtypes; \
564 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
567 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
568 /* 2) Declare some vars and set it up */ \
569 List<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
570 List<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
571 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
572 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
573 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
574 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
575 status = MRES_IGNORED; \
576 SH_GLOB_SHPTR->SetOverrideRet(NULL);
578 #define SH_CALL_HOOKS_void(post, params) \
579 prev_res = MRES_IGNORED; \
580 for (List<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
582 if (hiter->paused) continue; \
583 cur_res = MRES_IGNORED; \
584 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - hiter->thisptr_offs); \
585 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
586 prev_res = cur_res; \
587 if (cur_res > status) \
591 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
592 if (status != MRES_SUPERCEDE) \
594 void (EmptyClass::*mfp)paramtypes; \
596 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
599 #define SH_RETURN_void()
601 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
602 SH_SETUPCALLS_void(paramtypes, params) \
603 SH_CALL_HOOKS_void(pre, params) \
604 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
605 SH_CALL_HOOKS_void(post, params) \
609 // Special vafmt handlers
610 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
611 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
612 SH_CALL_HOOKS(pre, params_plug) \
613 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
614 SH_CALL_HOOKS(post, params_plug) \
617 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
618 SH_SETUPCALLS_void(paramtypes, params_orig) \
619 SH_CALL_HOOKS_void(pre, params_plug) \
620 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
621 SH_CALL_HOOKS_void(post, params_plug) \
624 //////////////////////////////////////////////////////////////////////////
627 // ********* Support for @$@ arguments *********
628 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
629 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
630 (&ifacetype::ifacefunc))) \
631 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
632 virtual rettype Func(@param%% p%%|, @) \
633 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
634 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
635 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
637 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
638 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
639 (&ifacetype::ifacefunc))) \
640 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
641 virtual void Func(@param%% p%%|, @) \
642 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
643 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
644 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
646 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
647 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
648 (&ifacetype::ifacefunc))) \
649 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
650 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
652 char buf[::SourceHook::STRBUF_LEN]; \
655 vsnprintf(buf, sizeof(buf), fmt, ap); \
657 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
659 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
660 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
662 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
663 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
664 (&ifacetype::ifacefunc))) \
665 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
666 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
668 char buf[::SourceHook::STRBUF_LEN]; \
671 vsnprintf(buf, sizeof(buf), fmt, ap); \
673 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
675 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
676 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
681 //////////////////////////////////////////////////////////////////////////
684 #if SH_COMP == SH_COMP_MSVC
686 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
688 using namespace ::SourceHook; \
690 GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
691 OrigVTables::iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
692 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->val.size() || iter->val[mfi.vtblindex] == NULL) \
693 return (m_CC->ptr->*m_MFP)call; \
695 /* It's hooked. Call the original function. */ \
698 RetType(EmptyClass::*mfpnew)prms; \
701 u.addr = iter->val[mfi.vtblindex]; \
703 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->ptr) + mfi.thisptroffs); \
704 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
707 #elif SH_COMP == SH_COMP_GCC
709 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
711 using namespace ::SourceHook; \
713 GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
714 OrigVTables::iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
715 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->val.size() || iter->val[mfi.vtblindex] == NULL) \
716 return (m_CC->ptr->*m_MFP)call; \
718 /* It's hooked. Call the original function. */ \
721 RetType(EmptyClass::*mfpnew)prms; \
728 u.s.addr = iter->val[mfi.vtblindex]; \
729 u.s.adjustor = mfi.thisptroffs; \
731 return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
738 template<class CCType, class RetType, class MFPType> class ExecutableClass
743 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
748 // Support for @$@ arguments
749 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
750 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
756 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
757 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
758 // The only purpose of the mfp2 parameter is to extract the return type
761 // Support for @$@ arguments
762 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
763 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
764 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
766 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
769 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
770 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
771 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
773 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
778 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
779 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
782 // Support for @$@ arguments
783 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
784 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
785 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
787 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
790 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
791 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
792 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
794 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
801 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
803 #undef SH_MAKE_EXECUTABLECLASS_BODY
806 // The pope is dead. -> :(