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
31 # define SH_ASSERT__(x, info, file, line, func) \
32 ((printf("SOURCEHOOK DEBUG ASSERTION FAILED:\n %s:%u(%s): %s\n", file, line, func, info), true) ? (abort(), 0) : 0)
33 # define SH_ASSERT(x, info) if (!(x)) SH_ASSERT__(x, info, __FILE__, __LINE__, __FUNCTION__)
35 # define SH_ASSERT(x, info)
39 #define SH_SYS_WIN32 1
40 #define SH_SYS_LINUX 2
43 # define SH_SYS SH_SYS_WIN32
44 #elif defined __linux__
45 # define SH_SYS SH_SYS_LINUX
47 # error Unsupported system
52 #define SH_COMP_MSVC 2
55 # define SH_COMP SH_COMP_MSVC
56 #elif defined __GNUC__
57 # define SH_COMP SH_COMP_GCC
59 # error Unsupported compiler
62 #if SH_COMP==SH_COMP_MSVC
63 # define vsnprintf _vsnprintf
66 #define SH_PTRSIZE sizeof(void*)
68 #include "FastDelegate.h"
69 #include "sh_memfuncinfo.h"
70 #include "sh_memory.h"
78 // Flags returned by a plugin's api function.
79 // NOTE: order is crucial, as greater/less comparisons are made.
82 MRES_IGNORED=0, // plugin didn't take any action
83 MRES_HANDLED, // plugin did something, but real function should still be called
84 MRES_OVERRIDE, // call real function, but use my return value
85 MRES_SUPERCEDE // skip real function; use my return value
91 const int STRBUF_LEN=4096; // In bytes, for "vafmt" functions
94 * @brief An empty class. No inheritance used. Used for original-function-call hacks
101 * @brief Implicit cast.
103 template <class In, class Out>
104 inline Out implicit_cast(In input)
110 * @brief A plugin typedef
112 * SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
113 * and every plugin has a unique identifier, everything is ok.
117 enum HookManagerAction
119 HA_GetInfo = 0, // -> Only store info
120 HA_Register, // -> Save the pointer for future reference
121 HA_Unregister // -> Clear the saved pointer
124 struct HookManagerInfo;
127 * @brief Pointer to hook manager type
129 * A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
131 * @param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
132 * future reference (mainly if something should get hooked to its hookfunc)
134 typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
139 virtual void DeleteThis() = 0; // Ugly, I know
140 virtual bool IsEqual(ISHDelegate *other) = 0;
143 template <class T> class CSHDelegate : public ISHDelegate
147 CSHDelegate(T deleg) : m_Deleg(deleg)
151 CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
160 bool IsEqual(ISHDelegate *other)
162 return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
172 * @brief This structure contains information about a hook manager (hook manager)
174 struct HookManagerInfo
182 ISHDelegate *handler; //!< Pointer to the handler
183 bool paused; //!< If true, the hook should not be executed
184 Plugin plug; //!< The owner plugin
185 int thisptr_offs; //!< This pointer offset
187 void *ptr; //!< Pointer to the interface instance
188 SourceHook::List<Hook> hooks_pre; //!< A list of pre-hooks
189 SourceHook::List<Hook> hooks_post; //!< A list of post-hooks
190 bool operator ==(void *other) const
196 void *vfnptr; //!< Pointer to the function
197 void *orig_entry; //!< The original vtable entry
199 typedef SourceHook::List<Iface> IfaceList;
200 typedef IfaceList::iterator IfaceListIter;
201 IfaceList ifaces; //!< List of interface pointers
203 bool operator ==(void *other)
205 return vfnptr == other;
209 Plugin plug; //!< The owner plugin
210 const char *proto; //!< The prototype of the function the hook manager is responsible for
211 int vtbl_idx; //!< The vtable index
212 int vtbl_offs; //!< The vtable offset
213 HookManagerPubFunc func; //!< The interface to the hook manager
215 void *hookfunc_vfnptr; //!< Pointer to the hookfunc impl
217 typedef SourceHook::List<VfnPtr> VfnPtrList;
218 typedef VfnPtrList::iterator VfnPtrListIter;
219 VfnPtrList vfnptrs; //!< List of hooked interfaces
222 typedef SourceHook::CVector<void*> OrigFuncs;
223 typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
225 template<class B> struct CallClass
227 B *ptr; //!< Pointer to the actual object
228 size_t objsize; //!< Size of the instance
229 OrigVTables vt; //!< Info about vtables & functions
232 typedef CallClass<void> GenericCallClass;
235 * @brief The main SourceHook interface
241 * @brief Return interface version
243 virtual int GetIfaceVersion() = 0;
246 * @brief Return implementation version
248 virtual int GetImplVersion() = 0;
253 * @return True if the function succeeded, false otherwise
255 * @param plug The unique identifier of the plugin that calls this function
256 * @param iface The interface pointer
257 * @param ifacesize The size of the class iface points to
258 * @param myHookMan A hook manager function that should be capable of handling the function
259 * @param handler A pointer to a FastDelegate containing the hook handler
260 * @param post Set to true if you want a post handler
262 virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
265 * @brief Removes a hook.
267 * @return True if the function succeeded, false otherwise
269 * @param plug The unique identifier of the plugin that calls this function
270 * @param iface The interface pointer
271 * @param myHookMan A hook manager function that should be capable of handling the function
272 * @param handler A pointer to a FastDelegate containing the hook handler
273 * @param post Set to true if you want a post handler
275 virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
278 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
280 * @param plug The unique identifier of the plugin in question
282 virtual bool IsPluginInUse(Plugin plug) = 0;
285 * @brief Return a pointer to a callclass. Generate a new one if required.
287 * @param iface The interface pointer
288 * @param size Size of the class instance
290 virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
293 * @brief Release a callclass
295 * @param ptr Pointer to the callclass
297 virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
299 virtual void SetRes(META_RES res) = 0; //!< Sets the meta result
300 virtual META_RES GetPrevRes() = 0; //!< Gets the meta result of the previously called handler
301 virtual META_RES GetStatus() = 0; //!< Gets the highest meta result
302 virtual const void *GetOrigRet() = 0; //!< Gets the original result. If not in post function, undefined
303 virtual const void *GetOverrideRet() = 0; //!< Gets the override result. If none is specified, NULL
304 virtual void *GetIfacePtr() = 0; //!< Gets the interface pointer
305 //////////////////////////////////////////////////////////////////////////
307 virtual META_RES &GetCurResRef() = 0; //!< Gets the reference to the current meta result
308 virtual META_RES &GetPrevResRef() = 0; //!< Gets the reference to the previous meta result
309 virtual META_RES &GetStatusRef() = 0; //!< Gets the reference to the status variable
310 virtual void* &GetIfacePtrRef() = 0; //!< Gets the reference to the interface this pointer
311 virtual void SetOrigRet(const void *ptr) = 0; //!< Sets the original return pointer
312 virtual void SetOverrideRet(const void *ptr) = 0; //!< Sets the override result pointer
316 /************************************************************************/
317 /* High level interface */
318 /************************************************************************/
319 #define SET_META_RESULT(result) SH_GLOB_SHPTR->SetRes(result)
320 #define RETURN_META(result) do { SET_META_RESULT(result); return; } while(0)
321 #define RETURN_META_VALUE(result, value) do { SET_META_RESULT(result); return (value); } while(0)
323 #define META_RESULT_STATUS SH_GLOB_SHPTR->GetStatus()
324 #define META_RESULT_PREVIOUS SH_GLOB_SHPTR->GetPrevRes()
325 #define META_RESULT_ORIG_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
326 #define META_RESULT_OVERRIDE_RET(type) *reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
327 #define META_IFACEPTR SH_GLOB_SHPTR->GetIfacePtr()
331 * @brief Get/generate callclass for an interface pointer
333 * @param ifaceptr The interface pointer
335 template<class ifacetype>
336 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
338 return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
339 shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
342 template<class ifacetype>
343 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
345 shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
348 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
349 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
351 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
352 __SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
354 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
355 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
356 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
357 SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
359 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
360 __SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
362 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
363 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
364 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
365 SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
372 #if SH_COMP == SH_COMP_MSVC
373 # define SH_SETUP_MFP(mfp) \
374 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
375 #elif SH_COMP == SH_COMP_GCC
376 # define SH_SETUP_MFP(mfp) \
377 reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
378 reinterpret_cast<void**>(&mfp)[1] = 0;
380 # error Not supported yet.
383 //////////////////////////////////////////////////////////////////////////
384 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
386 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
387 static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
389 using namespace ::SourceHook; \
390 /* Verify interface version */ \
391 if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
394 if (action == ::SourceHook::HA_GetInfo) \
396 param->proto = ms_Proto; \
398 GetFuncInfo(funcptr, mfi); \
399 param->vtbl_idx = mfi.vtblindex; \
400 param->vtbl_offs = mfi.vtbloffs; \
402 GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
403 param->hookfunc_vfnptr = \
404 reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
407 else if (action == ::SourceHook::HA_Register) \
412 else if (action == ::SourceHook::HA_Unregister) \
421 // It has to be possible to use the macros in namespaces
422 // -> So we need to access and extend the global SourceHook namespace
423 // We use a namespace alias for this
424 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
425 struct SH_FHCls(ifacetype,ifacefunc,overload) \
427 static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
428 static ::SourceHook::HookManagerInfo *ms_HI; \
429 static const char *ms_Proto; \
430 SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
432 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
434 const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
435 SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
436 ::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
437 bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
438 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
440 using namespace ::SourceHook; \
442 GetFuncInfo(funcptr, mfi); \
443 if (mfi.thisptroffs < 0) \
444 return false; /* No virtual inheritance supported */ \
446 return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
447 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
448 new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
450 bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
451 SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
453 using namespace ::SourceHook; \
455 GetFuncInfo(funcptr, mfi); \
456 if (mfi.thisptroffs < 0) \
457 return false; /* No virtual inheritance supported */ \
459 CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
460 return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
461 SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
464 #define SH_SETUPCALLS(rettype, paramtypes, params) \
465 /* 1) Find the vfnptr */ \
466 using namespace ::SourceHook; \
467 void *ourvfnptr = reinterpret_cast<void*>( \
468 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
470 HookManagerInfo::VfnPtrListIter vfptriter = ms_HI->find(ms_HI->vfnptrs.begin(), \
471 ms_HI->vfnptrs.end(), ourvfnptr); \
472 if (vfptriter == ms_HI->vfnptrs.end()) \
474 /* Bleh? Should be impossible! */ \
475 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
477 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
478 /* 2) Find the iface */ \
479 HookManagerInfo::VfnPtr::IfaceListIter ifiter = ms_HI->find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
480 if (ifiter == vfnptr.ifaces.end()) \
482 /* The iface info was not found. Redirect the call to the original function. */ \
483 rettype (EmptyClass::*mfp)paramtypes; \
485 return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
487 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
488 /* 2) Declare some vars and set it up */ \
489 SourceHook::List<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
490 SourceHook::List<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
492 rettype override_ret; \
493 rettype plugin_ret; \
494 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
495 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
496 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
497 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
498 status = MRES_IGNORED; \
499 SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
500 SH_GLOB_SHPTR->SetOverrideRet(NULL);
502 #define SH_CALL_HOOKS(post, params) \
503 prev_res = MRES_IGNORED; \
504 for (SourceHook::List<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
506 if (hiter->paused) continue; \
507 cur_res = MRES_IGNORED; \
508 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - hiter->thisptr_offs); \
509 plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
510 prev_res = cur_res; \
511 if (cur_res > status) \
513 if (cur_res >= MRES_OVERRIDE) \
515 override_ret = plugin_ret; \
516 SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
520 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
521 if (status != MRES_SUPERCEDE) \
523 rettype (EmptyClass::*mfp)paramtypes; \
525 orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
528 orig_ret = override_ret;
530 #define SH_RETURN() \
531 return status >= MRES_OVERRIDE ? override_ret : orig_ret;
533 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
534 SH_SETUPCALLS(rettype, paramtypes, params) \
535 SH_CALL_HOOKS(pre, params) \
536 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
537 SH_CALL_HOOKS(post, params) \
540 //////////////////////////////////////////////////////////////////////////
541 #define SH_SETUPCALLS_void(paramtypes, params) \
542 using namespace ::SourceHook; \
543 /* 1) Find the vfnptr */ \
544 void *ourvfnptr = reinterpret_cast<void*>( \
545 *reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
547 HookManagerInfo::VfnPtrListIter vfptriter = ms_HI->find(ms_HI->vfnptrs.begin(), \
548 ms_HI->vfnptrs.end(), ourvfnptr); \
549 if (vfptriter == ms_HI->vfnptrs.end()) \
551 /* Bleh? Should be impossible! */ \
552 SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
554 HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
555 /* 2) Find the iface */ \
556 HookManagerInfo::VfnPtr::IfaceListIter ifiter = ms_HI->find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
557 if (ifiter == vfnptr.ifaces.end()) \
559 /* The iface info was not found. Redirect the call to the original function. */ \
560 void (EmptyClass::*mfp)paramtypes; \
562 (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
565 HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
566 /* 2) Declare some vars and set it up */ \
567 SourceHook::List<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
568 SourceHook::List<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
569 META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
570 META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
571 META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
572 void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
573 status = MRES_IGNORED; \
574 SH_GLOB_SHPTR->SetOverrideRet(NULL);
576 #define SH_CALL_HOOKS_void(post, params) \
577 prev_res = MRES_IGNORED; \
578 for (SourceHook::List<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
580 if (hiter->paused) continue; \
581 cur_res = MRES_IGNORED; \
582 ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - hiter->thisptr_offs); \
583 reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
584 prev_res = cur_res; \
585 if (cur_res > status) \
589 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
590 if (status != MRES_SUPERCEDE) \
592 void (EmptyClass::*mfp)paramtypes; \
594 (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
597 #define SH_RETURN_void()
599 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
600 SH_SETUPCALLS_void(paramtypes, params) \
601 SH_CALL_HOOKS_void(pre, params) \
602 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
603 SH_CALL_HOOKS_void(post, params) \
607 // Special vafmt handlers
608 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
609 SH_SETUPCALLS(rettype, paramtypes, params_orig) \
610 SH_CALL_HOOKS(pre, params_plug) \
611 SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
612 SH_CALL_HOOKS(post, params_plug) \
615 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
616 SH_SETUPCALLS_void(paramtypes, params_orig) \
617 SH_CALL_HOOKS_void(pre, params_plug) \
618 SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
619 SH_CALL_HOOKS_void(post, params_plug) \
622 //////////////////////////////////////////////////////////////////////////
625 // ********* Support for @$@ arguments *********
626 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
627 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
628 (&ifacetype::ifacefunc))) \
629 typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
630 virtual rettype Func(@param%% p%%|, @) \
631 { SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
632 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
633 (static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
635 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
636 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
637 (&ifacetype::ifacefunc))) \
638 typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
639 virtual void Func(@param%% p%%|, @) \
640 { SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
641 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
642 (static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
644 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
645 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
646 (&ifacetype::ifacefunc))) \
647 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
648 virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
650 char buf[::SourceHook::STRBUF_LEN]; \
653 vsnprintf(buf, sizeof(buf), fmt, ap); \
655 SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
657 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
658 (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
660 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
661 SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
662 (&ifacetype::ifacefunc))) \
663 typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
664 virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
666 char buf[::SourceHook::STRBUF_LEN]; \
669 vsnprintf(buf, sizeof(buf), fmt, ap); \
671 SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
673 SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
674 (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
679 //////////////////////////////////////////////////////////////////////////
682 #if SH_COMP == SH_COMP_MSVC
684 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
686 using namespace ::SourceHook; \
688 GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
689 OrigVTables::iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
690 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->val.size() || iter->val[mfi.vtblindex] == NULL) \
691 return (m_CC->ptr->*m_MFP)call; \
693 /* It's hooked. Call the original function. */ \
696 RetType(EmptyClass::*mfpnew)prms; \
699 u.addr = iter->val[mfi.vtblindex]; \
701 void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->ptr) + mfi.thisptroffs); \
702 return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
705 #elif SH_COMP == SH_COMP_GCC
707 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
709 using namespace ::SourceHook; \
711 GetFuncInfo(m_CC->ptr, m_MFP, mfi); \
712 OrigVTables::iterator iter = m_CC->vt.find(mfi.thisptroffs + mfi.vtbloffs); \
713 if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->val.size() || iter->val[mfi.vtblindex] == NULL) \
714 return (m_CC->ptr->*m_MFP)call; \
716 /* It's hooked. Call the original function. */ \
719 RetType(EmptyClass::*mfpnew)prms; \
726 u.s.addr = iter->val[mfi.vtblindex]; \
727 u.s.adjustor = mfi.thisptroffs; \
729 return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
736 template<class CCType, class RetType, class MFPType> class ExecutableClass
741 ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
746 // Support for @$@ arguments
747 @template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
748 SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
754 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
755 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
756 // The only purpose of the mfp2 parameter is to extract the return type
759 // Support for @$@ arguments
760 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
761 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
762 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
764 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
767 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
768 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
769 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
771 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
776 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
777 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
780 // Support for @$@ arguments
781 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
782 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
783 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
785 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
788 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
789 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
790 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
792 return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
799 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
801 #undef SH_MAKE_EXECUTABLECLASS_BODY
804 // The pope is dead. -> :(