sourcehook/generate/sourcehook.hxx
author David Anderson <dvander@alliedmods.net>
Sun May 01 18:46:06 2005 +0000 (2005-05-01)
changeset 53 797ecc5394d6
parent 50 4b6bbaf375dc
child 58 71016f952503
permissions -rwxr-xr-x
Switched from MEMFUNC to STATICFUNC for simplicity
     1 /* ======== SourceHook ========
     2 * By PM
     3 * No warranties of any kind
     4 * ============================
     5 */
     6 
     7 /**
     8 *	@file sourcehook.h
     9 *	@brief Contains the public SourceHook API
    10 */
    11 
    12 #ifndef __SOURCEHOOK_H__
    13 #define __SOURCEHOOK_H__
    14 
    15 #ifndef SH_GLOB_SHPTR
    16 #define SH_GLOB_SHPTR g_SHPtr
    17 #endif
    18 
    19 #ifndef SH_GLOB_PLUGPTR
    20 #define SH_GLOB_PLUGPTR g_PLID
    21 #endif
    22 
    23 #ifdef SH_DEBUG
    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__)
    27 #else
    28 # define SH_ASSERT(x, info)
    29 #endif
    30 
    31 // System
    32 #define SH_SYS_WIN32 1
    33 #define SH_SYS_LINUX 2
    34 
    35 #ifdef _WIN32
    36 # define SH_SYS SH_SYS_WIN32
    37 #elif defined __linux__
    38 # define SH_SYS SH_SYS_LINUX
    39 #else
    40 # error Unsupported system
    41 #endif
    42 
    43 // Compiler
    44 #define SH_COMP_GCC 1
    45 #define SH_COMP_MSVC 2
    46 
    47 #ifdef _MSC_VER
    48 # define SH_COMP SH_COMP_MSVC
    49 #elif defined __GNUC__
    50 # define SH_COMP SH_COMP_GCC
    51 #else
    52 # error Unsupported compiler
    53 #endif
    54 
    55 #if SH_COMP==SH_COMP_MSVC
    56 # define vsnprintf _vsnprintf
    57 #endif
    58 
    59 #include "FastDelegate.h"
    60 #include "sh_memfuncinfo.h"
    61 #include "sh_memory.h"
    62 #include <list>
    63 #include <vector>
    64 #include <map>
    65 #include <algorithm>
    66 
    67 // Good old metamod!
    68 
    69 // Flags returned by a plugin's api function.
    70 // NOTE: order is crucial, as greater/less comparisons are made.
    71 enum META_RES
    72 {
    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
    77 };
    78 
    79 
    80 namespace SourceHook
    81 {
    82 	const int STRBUF_LEN=8192;		// In bytes, for "vafmt" functions
    83 
    84 	/**
    85 	*	@brief An empty class. No inheritance used. Used for original-function-call hacks
    86 	*/
    87 	class EmptyClass
    88 	{
    89 	};
    90 
    91 	/**
    92 	*	@brief A plugin typedef
    93 	*
    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.
    96 	*/
    97 	typedef int Plugin;
    98 
    99 	enum HookManagerAction
   100 	{
   101 		HA_GetInfo = 0,			// -> Only store info
   102 		HA_Register,			// -> Save the pointer for future reference
   103 		HA_Unregister			// -> Clear the saved pointer
   104 	};
   105 
   106 	struct HookManagerInfo;
   107 
   108 	/**
   109 	*	@brief Pointer to hook manager type
   110 	*
   111 	*	A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
   112 	*	
   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)
   115 	*/
   116 	typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
   117 
   118 	class ISHDelegate
   119 	{
   120 	public:
   121 		virtual void DeleteThis() = 0;				// Ugly, I know
   122 		virtual bool IsEqual(ISHDelegate *other) = 0;
   123 	};
   124 
   125 	template <class T> class CSHDelegate : public ISHDelegate
   126 	{
   127 		T m_Deleg;
   128 	public:
   129 		CSHDelegate(T deleg) : m_Deleg(deleg)
   130 		{
   131 		}
   132 
   133 		CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
   134 		{
   135 		}
   136 
   137 		void DeleteThis()
   138 		{
   139 			delete this;
   140 		}
   141 
   142 		bool IsEqual(ISHDelegate *other)
   143 		{
   144 			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();				
   145 		}
   146 
   147 		T &GetDeleg()
   148 		{
   149 			return m_Deleg;
   150 		}
   151 	};
   152 
   153 	/**
   154 	*	@brief This structure contains information about a hook manager (hook manager)
   155 	*/
   156 	struct HookManagerInfo
   157 	{
   158 		struct VfnPtr
   159 		{
   160 			struct Iface
   161 			{
   162 				struct Hook
   163 				{
   164 					ISHDelegate *handler;			//!< Pointer to the handler
   165 					bool paused;					//!< If true, the hook should not be executed
   166 					Plugin plug;					//!< The owner plugin
   167 				};
   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
   172 				{
   173 					return ptr == other;
   174 				}
   175 			};
   176 
   177 			void *vfnptr;							//!< Pointer to the function
   178 			void *orig_entry;						//!< The original vtable entry
   179 
   180 			typedef std::list<Iface> IfaceList;
   181 			typedef IfaceList::iterator IfaceListIter;
   182 			IfaceList ifaces;						//!< List of interface pointers
   183 
   184 			bool operator ==(void *other)
   185 			{
   186 				return vfnptr == other;
   187 			}
   188 		};
   189 
   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
   195 
   196 		void *hookfunc_vfnptr;					//!< Pointer to the hookfunc impl
   197 		
   198 		typedef std::list<VfnPtr> VfnPtrList;
   199 		typedef VfnPtrList::iterator VfnPtrListIter;
   200 		VfnPtrList vfnptrs;				//!< List of hooked interfaces
   201 	};
   202 
   203 	typedef std::vector<void*> OrigFuncs;
   204 	typedef std::map<int, OrigFuncs> OrigVTables;
   205 
   206 	template<class B> struct CallClass
   207 	{
   208 		B *ptr;
   209 		OrigVTables vt;
   210 	};
   211 
   212 	typedef CallClass<void> GenericCallClass;
   213 
   214 	/**
   215 	*	@brief The main SourceHook interface
   216 	*/
   217 	class ISourceHook
   218 	{
   219 	public:
   220 		/**
   221 		*	@brief Add a hook.
   222 		*
   223 		*	@return True if the function succeeded, false otherwise
   224 		*
   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
   231 		*/
   232 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
   233 
   234 		/**
   235 		*	@brief Removes a hook.
   236 		*
   237 		*	@return True if the function succeeded, false otherwise
   238 		*
   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
   244 		*/
   245 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
   246 
   247 		/**
   248 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   249 		*
   250 		*	@param plug The unique identifier of the plugin in question
   251 		*/
   252 		virtual bool IsPluginInUse(Plugin plug) = 0;
   253 
   254 		/**
   255 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   256 		*
   257 		*	@param iface The interface pointer
   258 		*	@param size Size of the class
   259 		*/
   260 		virtual GenericCallClass *GetCallClass(void *iface) = 0;
   261 
   262 		/**
   263 		*	@brief Release a callclass
   264 		*
   265 		*	@param ptr Pointer to the callclass
   266 		*/
   267 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   268 
   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 		//////////////////////////////////////////////////////////////////////////
   276 		// For hook managers
   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
   283 	};
   284 }
   285 
   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)
   292 
   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()
   298 
   299 
   300 /**
   301 *	@brief Get/generate callclass for an interface pointer
   302 *
   303 *	@param ifaceptr The interface pointer
   304 */
   305 template<class ifacetype>
   306 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   307 {
   308 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   309 		shptr->GetCallClass(reinterpret_cast<void*>(ptr)));
   310 }
   311 
   312 template<class ifacetype>
   313 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   314 {
   315 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   316 }
   317 
   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)
   320 
   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)
   327 
   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)
   334 
   335 #define SH_NOATTRIB
   336 
   337 
   338 
   339 
   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;
   347 #else
   348 # error Not supported yet.
   349 #endif
   350 
   351 //////////////////////////////////////////////////////////////////////////
   352 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   353 
   354 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   355 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
   356 	{ \
   357 		using namespace ::SourceHook; \
   358 		if (action == ::SourceHook::HA_GetInfo) \
   359 		{ \
   360 			param->proto = ms_Proto; \
   361 			MemFuncInfo mfi; \
   362 			GetFuncInfo(funcptr, mfi); \
   363 			param->vtbl_idx = mfi.vtblindex; \
   364 			param->vtbl_offs = mfi.vtbloffs; \
   365 			\
   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]; \
   369 			return 0; \
   370 		} \
   371 		else if (action == ::SourceHook::HA_Register) \
   372 		{ \
   373 			ms_HI = param; \
   374 			return 0; \
   375 		} \
   376 		else if (action == ::SourceHook::HA_Unregister) \
   377 		{ \
   378 			ms_HI = NULL; \
   379 			return 0; \
   380 		} \
   381 		else \
   382 			return 1; \
   383 	}
   384 
   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) \
   390 	{ \
   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)
   395 
   396 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
   397 	}; \
   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) \
   403 	{ \
   404 		using namespace ::SourceHook; \
   405 		MemFuncInfo mfi; \
   406 		GetFuncInfo(funcptr, mfi); \
   407 		if (mfi.thisptroffs < 0) \
   408 			return false; /* No virtual inheritance supported */ \
   409 		\
   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); \
   413 	} \
   414 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   415 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   416 	{ \
   417 		using namespace ::SourceHook; \
   418 		MemFuncInfo mfi; \
   419 		GetFuncInfo(funcptr, mfi); \
   420 		if (mfi.thisptroffs < 0) \
   421 			return false; /* No virtual inheritance supported */ \
   422 		\
   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); \
   426 	} \
   427 
   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); \
   433 	\
   434 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   435 		ms_HI->vfnptrs.end(), ourvfnptr); \
   436 	if (vfptriter == ms_HI->vfnptrs.end()) \
   437 	{ \
   438 		/* Bleh? Should be impossible! */ \
   439 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   440 	} \
   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()) \
   445 	{ \
   446 		/* The iface info was not found. Redirect the call to the original function. */ \
   447 		rettype (EmptyClass::*mfp)paramtypes; \
   448 		SH_SETUP_MFP(mfp); \
   449 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   450 	} \
   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; \
   455 	rettype orig_ret; \
   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);
   465 
   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) \
   469 	{ \
   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) \
   475 			status = cur_res; \
   476 		if (cur_res >= MRES_OVERRIDE) \
   477 		{ \
   478 			override_ret = plugin_ret; \
   479 			SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   480 		} \
   481 	}
   482 
   483 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   484 	if (status != MRES_SUPERCEDE) \
   485 	{ \
   486 		rettype (EmptyClass::*mfp)paramtypes; \
   487 		SH_SETUP_MFP(mfp); \
   488 		orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   489 	} \
   490 	else \
   491 		orig_ret = override_ret;
   492 
   493 #define SH_RETURN() \
   494 	return status >= MRES_OVERRIDE ? override_ret : orig_ret;
   495 
   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) \
   501 	SH_RETURN()
   502 
   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); \
   509 	\
   510 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   511 		ms_HI->vfnptrs.end(), ourvfnptr); \
   512 	if (vfptriter == ms_HI->vfnptrs.end()) \
   513 	{ \
   514 		/* Bleh? Should be impossible! */ \
   515 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   516 	} \
   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()) \
   521 	{ \
   522 		/* The iface info was not found. Redirect the call to the original function. */ \
   523 		void (EmptyClass::*mfp)paramtypes; \
   524 		SH_SETUP_MFP(mfp); \
   525 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   526 		return; \
   527 	} \
   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);
   538 
   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) \
   542 	{ \
   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) \
   548 			status = cur_res; \
   549 	}
   550 
   551 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   552 	if (status != MRES_SUPERCEDE) \
   553 	{ \
   554 		void (EmptyClass::*mfp)paramtypes; \
   555 		SH_SETUP_MFP(mfp); \
   556 		(reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   557 	}
   558 
   559 #define SH_RETURN_void()
   560 
   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) \
   566 	SH_RETURN_void()
   567 
   568 
   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) \
   575 	SH_RETURN()
   576 
   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) \
   582 	SH_RETURN_void()
   583 
   584 //////////////////////////////////////////////////////////////////////////
   585 
   586 @VARARGS@
   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)))
   596 
   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)))
   605 
   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, ...) \
   611 		{ \
   612 			char buf[::SourceHook::STRBUF_LEN]; \
   613 			va_list ap; \
   614 			va_start(ap, fmt); \
   615 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   616 			va_end(ap); \
   617 			SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
   618 		} \
   619 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
   620 	(static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   621 
   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, ...) \
   627 		{ \
   628 			char buf[::SourceHook::STRBUF_LEN]; \
   629 			va_list ap; \
   630 			va_start(ap, fmt); \
   631 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   632 			va_end(ap); \
   633 			SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
   634 		} \
   635 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
   636 	(static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   637 
   638 @ENDARGS@
   639 
   640 
   641 //////////////////////////////////////////////////////////////////////////
   642 // SH_CALL
   643 
   644 
   645 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   646 { \
   647 	using namespace ::SourceHook; \
   648 	MemFuncInfo mfi; \
   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; \
   653 	\
   654 	/* It's hooked. Call the original function. */ \
   655 	union \
   656 	{ \
   657 		RetType(EmptyClass::*mfpnew)prms; \
   658 		void *addr; \
   659 	} u; \
   660 	u.addr = iter->second[mfi.vtblindex]; \
   661 	\
   662 	return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
   663 }
   664 
   665 
   666 namespace SourceHook
   667 {
   668 	template<class CCType, class RetType, class MFPType> class ExecutableClass
   669 	{
   670 		CCType *m_CC;
   671 		MFPType m_MFP;
   672 	public:
   673 		ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
   674 		{
   675 		}
   676 
   677 @VARARGS@
   678 		// Support for @$@ arguments
   679 		@template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const 
   680 			SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
   681 
   682 @ENDARGS@
   683 	};
   684 }
   685 
   686 @VARARGS@
   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%%|, @))
   691 {
   692 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
   693 }
   694 
   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)
   698 {
   699 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
   700 }
   701 
   702 template <class X, class Y, class RetType@, @@class Param%%|, @>
   703 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...)>
   704 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @@, @...))
   705 {
   706 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...)>(ptr, mfp);
   707 }
   708 
   709 template <class X, class Y, class RetType@, @@class Param%%|, @>
   710 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...) const>
   711 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @@, @...)const)
   712 {
   713 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @@, @...) const>(ptr, mfp);
   714 }
   715 
   716 @ENDARGS@
   717 
   718 #undef SH_MAKE_EXECUTABLECLASS_BODY
   719 
   720 #endif
   721 	// The pope is dead. -> :(