sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Fri Apr 29 20:29:46 2005 +0000 (2005-04-29)
changeset 41 534eac4c53ef
parent 8 07e339563a28
child 45 04770941f5a9
permissions -rwxr-xr-x
Completly redone templates, added "vfnptrs"
     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 #define SH_ASSERT(x, info) if (!(x)) __asm { int 3 }
    24 
    25 // System
    26 #define SH_SYS_WIN32 1
    27 #define SH_SYS_LINUX 2
    28 
    29 #ifdef _WIN32
    30 # define SH_SYS SH_SYS_WIN32
    31 #elif defined __linux__
    32 # define SH_SYS SH_SYS_LINUX
    33 #else
    34 # error Unsupported system
    35 #endif
    36 
    37 // Compiler
    38 #define SH_COMP_GCC 1
    39 #define SH_COMP_MSVC 2
    40 
    41 #ifdef _MSC_VER
    42 # define SH_COMP SH_COMP_MSVC
    43 #elif defined __GNUC__
    44 # define SH_COMP SH_COMP_GCC
    45 #else
    46 # error Unsupported compiler
    47 #endif
    48 
    49 #if SH_COMP==SH_COMP_MSVC
    50 # define vsnprintf _vsnprintf
    51 #endif
    52 
    53 #include "FastDelegate.h"
    54 #include "sh_memfuncinfo.h"
    55 #include "sh_memory.h"
    56 #include <list>
    57 #include <vector>
    58 #include <map>
    59 #include <algorithm>
    60 
    61 // Good old metamod!
    62 
    63 // Flags returned by a plugin's api function.
    64 // NOTE: order is crucial, as greater/less comparisons are made.
    65 enum META_RES
    66 {
    67 	MRES_IGNORED=0,		// plugin didn't take any action
    68 	MRES_HANDLED,		// plugin did something, but real function should still be called
    69 	MRES_OVERRIDE,		// call real function, but use my return value
    70 	MRES_SUPERCEDE,		// skip real function; use my return value
    71 };
    72 
    73 
    74 namespace SourceHook
    75 {
    76 	const int STRBUF_LEN=8192;		// In bytes, for "vafmt" functions
    77 
    78 	/**
    79 	*	@brief An empty class. No inheritance used. Used for original-function-call hacks
    80 	*/
    81 	class EmptyClass
    82 	{
    83 	};
    84 
    85 	/**
    86 	*	@brief A plugin typedef
    87 	*
    88 	*	SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it
    89 	*	and every plugin has a unique identifier, everything is ok.
    90 	*/
    91 	typedef int Plugin;
    92 
    93 	enum HookManagerAction
    94 	{
    95 		HA_GetInfo = 0,			// -> Only store info
    96 		HA_Register,			// -> Save the pointer for future reference
    97 		HA_Unregister			// -> Clear the saved pointer
    98 	};
    99 
   100 	struct HookManagerInfo;
   101 
   102 	/**
   103 	*	@brief Pointer to hook manager type
   104 	*
   105 	*	A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
   106 	*	
   107 	*	@param hi A pointer to a HookManagerInfo structure. The hook manager should fill it and store it for
   108 	*				future reference (mainly if something should get hooked to its hookfunc)
   109 	*/
   110 	typedef int (*HookManagerPubFunc)(HookManagerAction ha, HookManagerInfo *hi);
   111 
   112 	class ISHDelegate
   113 	{
   114 	public:
   115 		virtual void DeleteThis() = 0;				// Ugly, I know
   116 		virtual bool IsEqual(ISHDelegate *other) = 0;
   117 	};
   118 
   119 	template <class T> class CSHDelegate : public ISHDelegate
   120 	{
   121 		T m_Deleg;
   122 	public:
   123 		CSHDelegate(T deleg) : m_Deleg(deleg)
   124 		{
   125 		}
   126 
   127 		void DeleteThis()
   128 		{
   129 			delete this;
   130 		}
   131 
   132 		bool IsEqual(ISHDelegate *other)
   133 		{
   134 			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();				
   135 		}
   136 
   137 		T &GetDeleg()
   138 		{
   139 			return m_Deleg;
   140 		}
   141 	};
   142 
   143 	/**
   144 	*	@brief This structure contains information about a hook manager (hook manager)
   145 	*/
   146 	struct HookManagerInfo
   147 	{
   148 		struct VfnPtr
   149 		{
   150 			struct Iface
   151 			{
   152 				struct Hook
   153 				{
   154 					ISHDelegate *handler;			//!< Pointer to the handler
   155 					bool paused;					//!< If true, the hook should not be executed
   156 					Plugin plug;					//!< The owner plugin
   157 				};
   158 				void *ptr;							//!< Pointer to the interface instance
   159 				std::list<Hook> hooks_pre;			//!< A list of pre-hooks
   160 				std::list<Hook> hooks_post;			//!< A list of post-hooks
   161 				bool operator ==(void *other) const
   162 				{
   163 					return ptr == other;
   164 				}
   165 			};
   166 
   167 			void *vfnptr;							//!< Pointer to the function
   168 			void *orig_entry;					//!< The original vtable entry
   169 
   170 			typedef std::list<Iface> IfaceList;
   171 			typedef IfaceList::iterator IfaceListIter;
   172 			IfaceList ifaces;
   173 
   174 			bool operator ==(void *other)
   175 			{
   176 				return vfnptr == other;
   177 			}
   178 		};
   179 		Plugin plug;							//!< The owner plugin
   180 		const char *proto;						//!< The prototype of the function the hook manager is responsible for
   181 		int vtbl_idx;							//!< The vtable index
   182 		int vtbl_offs;							//!< The vtable offset
   183 		HookManagerPubFunc func;				//!< The interface to the hook manager
   184 
   185 		void *hookfunc_vfnptr;					//!< Pointer to the hookfunc impl
   186 		
   187 		typedef std::list<VfnPtr> VfnPtrList;
   188 		typedef VfnPtrList::iterator VfnPtrListIter;
   189 		VfnPtrList vfnptrs;				//!< List of hooked interfaces
   190 	};
   191 
   192 	typedef std::vector<void*> OrigFuncs;
   193 	typedef std::map<int, OrigFuncs> OrigVTables;
   194 
   195 	template<class B> struct CallClass
   196 	{
   197 		B *ptr;
   198 		OrigVTables vt;
   199 	};
   200 
   201 	typedef CallClass<void> GenericCallClass;
   202 
   203 	/**
   204 	*	@brief The main SourceHook interface
   205 	*/
   206 	class ISourceHook
   207 	{
   208 	public:
   209 		/**
   210 		*	@brief Add a hook.
   211 		*
   212 		*	@return True if the function succeeded, false otherwise
   213 		*
   214 		*	@param plug The unique identifier of the plugin that calls this function
   215 		*	@param iface The interface pointer
   216 		*	@param ifacesize The size of the class iface points to
   217 		*	@param myHookMan A hook manager function that should be capable of handling the function
   218 		*	@param handler A pointer to a FastDelegate containing the hook handler
   219 		*	@param post Set to true if you want a post handler
   220 		*/
   221 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
   222 
   223 		/**
   224 		*	@brief Removes a hook.
   225 		*
   226 		*	@return True if the function succeeded, false otherwise
   227 		*
   228 		*	@param plug The unique identifier of the plugin that calls this function
   229 		*	@param iface The interface pointer
   230 		*	@param myHookMan A hook manager function that should be capable of handling the function
   231 		*	@param handler A pointer to a FastDelegate containing the hook handler
   232 		*	@param post Set to true if you want a post handler
   233 		*/
   234 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
   235 
   236 		/**
   237 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   238 		*
   239 		*	@param plug The unique identifier of the plugin in question
   240 		*/
   241 		virtual bool IsPluginInUse(Plugin plug) = 0;
   242 
   243 		/**
   244 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   245 		*
   246 		*	@param iface The interface pointer
   247 		*	@param size Size of the class
   248 		*/
   249 		virtual GenericCallClass *GetCallClass(void *iface) = 0;
   250 
   251 		/**
   252 		*	@brief Release a callclass
   253 		*
   254 		*	@param ptr Pointer to the callclass
   255 		*/
   256 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   257 
   258 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   259 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the previously called handler
   260 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   261 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result. If not in post function, undefined
   262 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result. If none is specified, NULL
   263 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   264 		//////////////////////////////////////////////////////////////////////////
   265 		// For hook managers
   266 		virtual META_RES &GetCurResRef() = 0;				//!< Gets the pointer to the current meta result
   267 		virtual META_RES &GetPrevResRef() = 0;				//!< Gets the pointer to the previous meta result
   268 		virtual META_RES &GetStatusRef() = 0;				//!< Gets the pointer to the status variable
   269 		virtual void SetOrigRet(const void *ptr) = 0;		//!< Sets the original return pointer
   270 		virtual void SetOverrideRet(const void *ptr) = 0;	//!< Sets the override result pointer
   271 		virtual void SetIfacePtr(void *ptr) = 0;			//!< Sets the interface this pointer
   272 	};
   273 }
   274 
   275 //////////////////////////////////////////////////////////////////////////
   276 // Macro interface
   277 #define SET_META_RESULT(result)		SH_GLOB_SHPTR->SetRes(result)
   278 #define RETURN_META(result)			do { SH_GLOB_SHPTR->SetRes(result); return; } while(0)
   279 #define RETURN_META_VALUE(result, value)	do { SH_GLOB_SHPTR->SetRes(result); return (value); } while(0)
   280 
   281 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   282 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   283 #define META_RESULT_ORIG_RET(type)			*(const type *)SH_GLOB_SHPTR->GetOrigRet()
   284 #define META_RESULT_OVERRIDE_RET(type)		*(const type *)SH_GLOB_SHPTR->GetOverrideRet()
   285 #define META_IFACEPTR						SH_GLOB_SHPTR->GetIfacePtr()
   286 
   287 
   288 /**
   289 *	@brief Get/generate callclass for an interface pointer
   290 *
   291 *	@param ifaceptr The interface pointer
   292 */
   293 template<class ifacetype>
   294 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS(ifacetype *ptr)
   295 {
   296 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   297 		SH_GLOB_SHPTR->GetCallClass(reinterpret_cast<void*>(ptr)));
   298 }
   299 
   300 #define SH_RELEASE_CALLCLASS(ptr) SH_GLOB_SHPTR->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr))
   301 
   302 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   303 	SourceHook::SH_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
   304 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   305 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
   306 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   307 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   308 
   309 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   310 	SourceHook::SH_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
   311 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   312 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post)
   313 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   314 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   315 
   316 #define SH_NOATTRIB
   317 
   318 
   319 
   320 #if SH_COMP == SH_COMP_MSVC
   321 # define SH_SETUP_MFP(mfp) \
   322 	reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
   323 #elif SH_COMP == SH_COMP_GCC
   324 # define SH_SETUP_MFP(mfp) \
   325 	reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
   326 	reinterpret_cast<void**>(&mfp)[1] = 0;
   327 #else
   328 # error Not supported yet.
   329 #endif
   330 
   331 //////////////////////////////////////////////////////////////////////////
   332 #define SH_FHCls(ift, iff, ov) FHCls_##ift##iff##ov
   333 
   334 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   335 	static int HookManPubFunc(HookManagerAction action, HookManagerInfo *param) \
   336 	{ \
   337 		if (action == HA_GetInfo) \
   338 		{ \
   339 			param->proto = ms_Proto; \
   340 			MemFuncInfo mfi; \
   341 			GetFuncInfo(funcptr, mfi); \
   342 			param->vtbl_idx = mfi.vtblindex; \
   343 			param->vtbl_offs = mfi.vtbloffs; \
   344 			\
   345 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   346 			param->hookfunc_vfnptr = \
   347 			reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
   348 			return 0; \
   349 		} \
   350 		else if (action == HA_Register) \
   351 		{ \
   352 			ms_HI = param; \
   353 			return 0; \
   354 		} \
   355 		else if (action == HA_Unregister) \
   356 		{ \
   357 			ms_HI = NULL; \
   358 			return 0; \
   359 		} \
   360 		else \
   361 			return 1; \
   362 	}
   363 
   364 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   365 	namespace SourceHook \
   366 	{ \
   367 		struct SH_FHCls(ifacetype,ifacefunc,overload) \
   368 		{ \
   369 			static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   370 			static HookManagerInfo *ms_HI; \
   371 			static const char *ms_Proto; \
   372 			SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   373 
   374 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
   375 		}; \
   376 		const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
   377 		SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   378 		HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   379 		bool SH_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   380 			SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   381 		{ \
   382 			MemFuncInfo mfi; \
   383 			GetFuncInfo(funcptr, mfi); \
   384 			if (mfi.thisptroffs < 0) \
   385 				return false; /* No virtual inheritance supported */ \
   386 			\
   387 			return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   388 				SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   389 				new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   390 		} \
   391 		bool SH_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   392 			SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   393 		{ \
   394 			MemFuncInfo mfi; \
   395 			GetFuncInfo(funcptr, mfi); \
   396 			if (mfi.thisptroffs < 0) \
   397 				return false; /* No virtual inheritance supported */ \
   398 			\
   399 			CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   400 			return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   401 				SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   402 		} \
   403 	}
   404 
   405 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   406 	/* 1) Find the vfnptr */ \
   407 	void *ourvfnptr = reinterpret_cast<void*>( \
   408 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
   409 	\
   410 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   411 		ms_HI->vfnptrs.end(), ourvfnptr); \
   412 	if (vfptriter == ms_HI->vfnptrs.end()) \
   413 	{ \
   414 		/* Bleh? Should be impossible! */ \
   415 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   416 	} \
   417 	HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
   418 	/* 2) Find the iface */ \
   419 	HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
   420 	if (ifiter == vfnptr.ifaces.end()) \
   421 	{ \
   422 		/* The iface info was not found. Redirect the call to the original function. */ \
   423 		rettype (EmptyClass::*mfp)paramtypes; \
   424 		SH_SETUP_MFP(mfp); \
   425 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   426 	} \
   427 	HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
   428 	/* 2) Declare some vars and set it up */ \
   429 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
   430 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
   431 	rettype orig_ret, override_ret, plugin_ret; \
   432 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   433 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   434 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   435 	status = MRES_IGNORED; \
   436 	SH_GLOB_SHPTR->SetIfacePtr(this); \
   437 	SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
   438 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   439 
   440 #define SH_CALL_HOOKS(post, params) \
   441 	prev_res = MRES_IGNORED; \
   442 	for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
   443 	{ \
   444 		if (hiter->paused) continue; \
   445 		cur_res = MRES_IGNORED; \
   446 		plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
   447 		prev_res = cur_res; \
   448 		if (cur_res > status) \
   449 			status = cur_res; \
   450 		if (cur_res >= MRES_OVERRIDE) \
   451 		{ \
   452 			override_ret = plugin_ret; \
   453 			SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   454 		} \
   455 	}
   456 
   457 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   458 	if (status != MRES_SUPERCEDE) \
   459 	{ \
   460 		rettype (EmptyClass::*mfp)paramtypes; \
   461 		SH_SETUP_MFP(mfp); \
   462 		orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   463 	} \
   464 	else \
   465 		orig_ret = override_ret;
   466 
   467 #define SH_RETURN() \
   468 	return status >= MRES_OVERRIDE ? override_ret : orig_ret;
   469 
   470 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
   471 	SH_SETUPCALLS(rettype, paramtypes, params) \
   472 	SH_CALL_HOOKS(pre, params) \
   473 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   474 	SH_CALL_HOOKS(post, params) \
   475 	SH_RETURN()
   476 
   477 //////////////////////////////////////////////////////////////////////////
   478 #define SH_SETUPCALLS_void(paramtypes, params) \
   479 	/* 1) Find the vfnptr */ \
   480 	void *ourvfnptr = reinterpret_cast<void*>( \
   481 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
   482 	\
   483 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   484 		ms_HI->vfnptrs.end(), ourvfnptr); \
   485 	if (vfptriter == ms_HI->vfnptrs.end()) \
   486 	{ \
   487 		/* Bleh? Should be impossible! */ \
   488 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   489 	} \
   490 	HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
   491 	/* 2) Find the iface */ \
   492 	HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
   493 	if (ifiter == vfnptr.ifaces.end()) \
   494 	{ \
   495 		/* The iface info was not found. Redirect the call to the original function. */ \
   496 		void (EmptyClass::*mfp)paramtypes; \
   497 		SH_SETUP_MFP(mfp); \
   498 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   499 		return; \
   500 	} \
   501 	HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
   502 	/* 2) Declare some vars and set it up */ \
   503 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
   504 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
   505 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   506 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   507 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   508 	status = MRES_IGNORED; \
   509 	SH_GLOB_SHPTR->SetIfacePtr(this); \
   510 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   511 
   512 #define SH_CALL_HOOKS_void(post, params) \
   513 	prev_res = MRES_IGNORED; \
   514 	for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
   515 	{ \
   516 		if (hiter->paused) continue; \
   517 		cur_res = MRES_IGNORED; \
   518 		reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
   519 		prev_res = cur_res; \
   520 		if (cur_res > status) \
   521 			status = cur_res; \
   522 	}
   523 
   524 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   525 	if (status != MRES_SUPERCEDE) \
   526 	{ \
   527 		void (EmptyClass::*mfp)paramtypes; \
   528 		SH_SETUP_MFP(mfp); \
   529 		(reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   530 	}
   531 
   532 #define SH_RETURN_void()
   533 
   534 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
   535 	SH_SETUPCALLS_void(paramtypes, params) \
   536 	SH_CALL_HOOKS_void(pre, params) \
   537 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   538 	SH_CALL_HOOKS_void(post, params) \
   539 	SH_RETURN_void()
   540 
   541 
   542 // Special vafmt handlers
   543 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
   544 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   545 	SH_CALL_HOOKS(pre, params_plug) \
   546 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
   547 	SH_CALL_HOOKS(post, params_plug) \
   548 	SH_RETURN()
   549 
   550 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
   551 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   552 	SH_CALL_HOOKS_void(pre, params_plug) \
   553 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
   554 	SH_CALL_HOOKS_void(post, params_plug) \
   555 	SH_RETURN_void()
   556 
   557 //////////////////////////////////////////////////////////////////////////
   558 
   559 @VARARGS@
   560 // ********* Support for @$@ arguments *********
   561 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   562 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @)> \
   563 		(&ifacetype::ifacefunc))) \
   564 		typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
   565 		virtual rettype Func(@param%% p%%|, @) attr \
   566 		{ SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
   567 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
   568 	(static_cast<rettype (ifacetype::*)(@param%%|, @)>(&ifacetype::ifacefunc)))
   569 
   570 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
   571 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @)> \
   572 		(&ifacetype::ifacefunc))) \
   573 		typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
   574 		virtual void Func(@param%% p%%|, @) attr \
   575 		{ SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
   576 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
   577 	(static_cast<void (ifacetype::*)(@param%%|, @)>(&ifacetype::ifacefunc)))
   578 
   579 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   580 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
   581 		(&ifacetype::ifacefunc))) \
   582 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
   583 		virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) attr \
   584 		{ \
   585 			char buf[STRBUF_LEN]; \
   586 			va_list ap; \
   587 			va_start(ap, fmt); \
   588 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   589 			va_end(ap); \
   590 			SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
   591 		} \
   592 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
   593 	(static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...)>(&ifacetype::ifacefunc)))
   594 
   595 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   596 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)> \
   597 		(&ifacetype::ifacefunc))) \
   598 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
   599 		virtual void Func(@param%% p%%|, @@, @const char *, ...) attr \
   600 		{ \
   601 			char buf[STRBUF_LEN]; \
   602 			va_list ap; \
   603 			va_start(ap, fmt); \
   604 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   605 			va_end(ap); \
   606 			SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
   607 		} \
   608 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
   609 	(static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...)>(&ifacetype::ifacefunc)))
   610 
   611 @ENDARGS@
   612 
   613 
   614 //////////////////////////////////////////////////////////////////////////
   615 // SH_CALL
   616 
   617 
   618 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   619 { \
   620 	MemFuncInfo mfi; \
   621 	MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
   622 	OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
   623 	if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
   624 		return (m_CC->ptr->*m_MFP)call; \
   625 	\
   626 	/* It's hooked. Call the original function. */ \
   627 	union \
   628 	{ \
   629 		RetType(EmptyClass::*mfpnew)prms; \
   630 		void *addr; \
   631 	} u; \
   632 	u.addr = iter->second[mfi.vtblindex]; \
   633 	\
   634 	return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
   635 }
   636 
   637 
   638 namespace SourceHook
   639 {
   640 	template<class CCType, class RetType, class MFPType> class ExecutableClass
   641 	{
   642 		CCType *m_CC;
   643 		MFPType m_MFP;
   644 	public:
   645 		ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
   646 		{
   647 		}
   648 
   649 @VARARGS@
   650 		// Support for @$@ arguments
   651 		template<@class Param%%|, @> RetType operator()(@Param%% p%%|, @) const 
   652 			SH_MAKE_EXECUTABLECLASS_OB((@p%%@), (@Param%%@))
   653 
   654 @ENDARGS@
   655 	};
   656 }
   657 
   658 @VARARGS@
   659 // Support for @$@ arguments
   660 template <class X, class Y, class RetType@, @@class Param%%|, @>
   661 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
   662 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
   663 {
   664 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
   665 }
   666 
   667 @ENDARGS@
   668 
   669 #undef SH_MAKE_EXECUTABLECLASS_BODY
   670 
   671 #endif
   672 	// The pope is dead. -> :(