sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sat Apr 30 19:09:51 2005 +0000 (2005-04-30)
changeset 45 04770941f5a9
parent 41 534eac4c53ef
child 47 0d8950ea695b
permissions -rwxr-xr-x
Fixes
     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_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, fastdelegate::MakeDelegate(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_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, fastdelegate::MakeDelegate(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) __SourceHook_FHCls_##ift##iff##ov
   333 
   334 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   335 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
   336 	{ \
   337 		using namespace ::SourceHook; \
   338 		if (action == ::SourceHook::HA_GetInfo) \
   339 		{ \
   340 			param->proto = ms_Proto; \
   341 			MemFuncInfo mfi; \
   342 			GetFuncInfo(funcptr, mfi); \
   343 			param->vtbl_idx = mfi.vtblindex; \
   344 			param->vtbl_offs = mfi.vtbloffs; \
   345 			\
   346 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   347 			param->hookfunc_vfnptr = \
   348 			reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
   349 			return 0; \
   350 		} \
   351 		else if (action == ::SourceHook::HA_Register) \
   352 		{ \
   353 			ms_HI = param; \
   354 			return 0; \
   355 		} \
   356 		else if (action == ::SourceHook::HA_Unregister) \
   357 		{ \
   358 			ms_HI = NULL; \
   359 			return 0; \
   360 		} \
   361 		else \
   362 			return 1; \
   363 	}
   364 
   365 // It has to be possible to use the macros in namespaces
   366 // -> So we need to access and extend the global SourceHook namespace
   367 // We use a namespace alias for this
   368 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   369 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   370 	{ \
   371 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   372 		static ::SourceHook::HookManagerInfo *ms_HI; \
   373 		static const char *ms_Proto; \
   374 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   375 
   376 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
   377 	}; \
   378 	const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
   379 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   380 	::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   381 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   382 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   383 	{ \
   384 		using namespace ::SourceHook; \
   385 		MemFuncInfo mfi; \
   386 		GetFuncInfo(funcptr, mfi); \
   387 		if (mfi.thisptroffs < 0) \
   388 			return false; /* No virtual inheritance supported */ \
   389 		\
   390 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   391 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   392 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   393 	} \
   394 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   395 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   396 	{ \
   397 		using namespace ::SourceHook; \
   398 		MemFuncInfo mfi; \
   399 		GetFuncInfo(funcptr, mfi); \
   400 		if (mfi.thisptroffs < 0) \
   401 			return false; /* No virtual inheritance supported */ \
   402 		\
   403 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   404 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   405 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   406 	} \
   407 
   408 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   409 	/* 1) Find the vfnptr */ \
   410 	using namespace ::SourceHook; \
   411 	void *ourvfnptr = reinterpret_cast<void*>( \
   412 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
   413 	\
   414 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   415 		ms_HI->vfnptrs.end(), ourvfnptr); \
   416 	if (vfptriter == ms_HI->vfnptrs.end()) \
   417 	{ \
   418 		/* Bleh? Should be impossible! */ \
   419 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   420 	} \
   421 	HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
   422 	/* 2) Find the iface */ \
   423 	HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
   424 	if (ifiter == vfnptr.ifaces.end()) \
   425 	{ \
   426 		/* The iface info was not found. Redirect the call to the original function. */ \
   427 		rettype (EmptyClass::*mfp)paramtypes; \
   428 		SH_SETUP_MFP(mfp); \
   429 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   430 	} \
   431 	HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
   432 	/* 2) Declare some vars and set it up */ \
   433 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
   434 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
   435 	rettype orig_ret; \
   436 	rettype override_ret; \
   437 	rettype plugin_ret; \
   438 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   439 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   440 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   441 	status = MRES_IGNORED; \
   442 	SH_GLOB_SHPTR->SetIfacePtr(this); \
   443 	SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
   444 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   445 
   446 #define SH_CALL_HOOKS(post, params) \
   447 	prev_res = MRES_IGNORED; \
   448 	for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
   449 	{ \
   450 		if (hiter->paused) continue; \
   451 		cur_res = MRES_IGNORED; \
   452 		plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
   453 		prev_res = cur_res; \
   454 		if (cur_res > status) \
   455 			status = cur_res; \
   456 		if (cur_res >= MRES_OVERRIDE) \
   457 		{ \
   458 			override_ret = plugin_ret; \
   459 			SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   460 		} \
   461 	}
   462 
   463 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   464 	if (status != MRES_SUPERCEDE) \
   465 	{ \
   466 		rettype (EmptyClass::*mfp)paramtypes; \
   467 		SH_SETUP_MFP(mfp); \
   468 		orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   469 	} \
   470 	else \
   471 		orig_ret = override_ret;
   472 
   473 #define SH_RETURN() \
   474 	return status >= MRES_OVERRIDE ? override_ret : orig_ret;
   475 
   476 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
   477 	SH_SETUPCALLS(rettype, paramtypes, params) \
   478 	SH_CALL_HOOKS(pre, params) \
   479 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   480 	SH_CALL_HOOKS(post, params) \
   481 	SH_RETURN()
   482 
   483 //////////////////////////////////////////////////////////////////////////
   484 #define SH_SETUPCALLS_void(paramtypes, params) \
   485 	using namespace ::SourceHook; \
   486 	/* 1) Find the vfnptr */ \
   487 	void *ourvfnptr = reinterpret_cast<void*>( \
   488 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
   489 	\
   490 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   491 		ms_HI->vfnptrs.end(), ourvfnptr); \
   492 	if (vfptriter == ms_HI->vfnptrs.end()) \
   493 	{ \
   494 		/* Bleh? Should be impossible! */ \
   495 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   496 	} \
   497 	HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
   498 	/* 2) Find the iface */ \
   499 	HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
   500 	if (ifiter == vfnptr.ifaces.end()) \
   501 	{ \
   502 		/* The iface info was not found. Redirect the call to the original function. */ \
   503 		void (EmptyClass::*mfp)paramtypes; \
   504 		SH_SETUP_MFP(mfp); \
   505 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   506 		return; \
   507 	} \
   508 	HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
   509 	/* 2) Declare some vars and set it up */ \
   510 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
   511 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
   512 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   513 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   514 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   515 	status = MRES_IGNORED; \
   516 	SH_GLOB_SHPTR->SetIfacePtr(this); \
   517 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   518 
   519 #define SH_CALL_HOOKS_void(post, params) \
   520 	prev_res = MRES_IGNORED; \
   521 	for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
   522 	{ \
   523 		if (hiter->paused) continue; \
   524 		cur_res = MRES_IGNORED; \
   525 		reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
   526 		prev_res = cur_res; \
   527 		if (cur_res > status) \
   528 			status = cur_res; \
   529 	}
   530 
   531 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   532 	if (status != MRES_SUPERCEDE) \
   533 	{ \
   534 		void (EmptyClass::*mfp)paramtypes; \
   535 		SH_SETUP_MFP(mfp); \
   536 		(reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   537 	}
   538 
   539 #define SH_RETURN_void()
   540 
   541 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
   542 	SH_SETUPCALLS_void(paramtypes, params) \
   543 	SH_CALL_HOOKS_void(pre, params) \
   544 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   545 	SH_CALL_HOOKS_void(post, params) \
   546 	SH_RETURN_void()
   547 
   548 
   549 // Special vafmt handlers
   550 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
   551 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   552 	SH_CALL_HOOKS(pre, params_plug) \
   553 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
   554 	SH_CALL_HOOKS(post, params_plug) \
   555 	SH_RETURN()
   556 
   557 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
   558 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   559 	SH_CALL_HOOKS_void(pre, params_plug) \
   560 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
   561 	SH_CALL_HOOKS_void(post, params_plug) \
   562 	SH_RETURN_void()
   563 
   564 //////////////////////////////////////////////////////////////////////////
   565 
   566 @VARARGS@
   567 // ********* Support for @$@ arguments *********
   568 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   569 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
   570 		(&ifacetype::ifacefunc))) \
   571 		typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
   572 		virtual rettype Func(@param%% p%%|, @) \
   573 		{ SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
   574 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
   575 	(static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
   576 
   577 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
   578 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
   579 		(&ifacetype::ifacefunc))) \
   580 		typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
   581 		virtual void Func(@param%% p%%|, @) \
   582 		{ SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
   583 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
   584 	(static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
   585 
   586 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   587 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
   588 		(&ifacetype::ifacefunc))) \
   589 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
   590 		virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
   591 		{ \
   592 			char buf[STRBUF_LEN]; \
   593 			va_list ap; \
   594 			va_start(ap, fmt); \
   595 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   596 			va_end(ap); \
   597 			SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
   598 		} \
   599 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
   600 	(static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   601 
   602 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   603 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
   604 		(&ifacetype::ifacefunc))) \
   605 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
   606 		virtual void Func(@param%% p%%|, @@, @const char *, ...) \
   607 		{ \
   608 			char buf[STRBUF_LEN]; \
   609 			va_list ap; \
   610 			va_start(ap, fmt); \
   611 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   612 			va_end(ap); \
   613 			SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
   614 		} \
   615 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
   616 	(static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   617 
   618 @ENDARGS@
   619 
   620 
   621 //////////////////////////////////////////////////////////////////////////
   622 // SH_CALL
   623 
   624 
   625 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   626 { \
   627 	using namespace ::SourceHook; \
   628 	MemFuncInfo mfi; \
   629 	MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
   630 	OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
   631 	if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
   632 		return (m_CC->ptr->*m_MFP)call; \
   633 	\
   634 	/* It's hooked. Call the original function. */ \
   635 	union \
   636 	{ \
   637 		RetType(EmptyClass::*mfpnew)prms; \
   638 		void *addr; \
   639 	} u; \
   640 	u.addr = iter->second[mfi.vtblindex]; \
   641 	\
   642 	return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
   643 }
   644 
   645 
   646 namespace SourceHook
   647 {
   648 	template<class CCType, class RetType, class MFPType> class ExecutableClass
   649 	{
   650 		CCType *m_CC;
   651 		MFPType m_MFP;
   652 	public:
   653 		ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
   654 		{
   655 		}
   656 
   657 @VARARGS@
   658 		// Support for @$@ arguments
   659 		@template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const 
   660 			SH_MAKE_EXECUTABLECLASS_OB((@p%%@), (@Param%%@))
   661 
   662 @ENDARGS@
   663 	};
   664 }
   665 
   666 @VARARGS@
   667 // Support for @$@ arguments
   668 template <class X, class Y, class RetType@, @@class Param%%|, @>
   669 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
   670 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
   671 {
   672 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
   673 }
   674 
   675 template <class X, class Y, class RetType@, @@class Param%%|, @>
   676 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>
   677 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @)const)
   678 {
   679 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
   680 }
   681 
   682 @ENDARGS@
   683 
   684 #undef SH_MAKE_EXECUTABLECLASS_BODY
   685 
   686 #endif
   687 	// The pope is dead. -> :(