sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sun May 01 09:55:51 2005 +0000 (2005-05-01)
changeset 47 0d8950ea695b
parent 45 04770941f5a9
child 48 a52dba4f8ed9
permissions -rwxr-xr-x
Fixed RemoveHook + minor changes
     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 		CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
   128 		{
   129 		}
   130 
   131 		void DeleteThis()
   132 		{
   133 			delete this;
   134 		}
   135 
   136 		bool IsEqual(ISHDelegate *other)
   137 		{
   138 			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();				
   139 		}
   140 
   141 		T &GetDeleg()
   142 		{
   143 			return m_Deleg;
   144 		}
   145 	};
   146 
   147 	/**
   148 	*	@brief This structure contains information about a hook manager (hook manager)
   149 	*/
   150 	struct HookManagerInfo
   151 	{
   152 		struct VfnPtr
   153 		{
   154 			struct Iface
   155 			{
   156 				struct Hook
   157 				{
   158 					ISHDelegate *handler;			//!< Pointer to the handler
   159 					bool paused;					//!< If true, the hook should not be executed
   160 					Plugin plug;					//!< The owner plugin
   161 				};
   162 				void *ptr;							//!< Pointer to the interface instance
   163 				std::list<Hook> hooks_pre;			//!< A list of pre-hooks
   164 				std::list<Hook> hooks_post;			//!< A list of post-hooks
   165 				bool operator ==(void *other) const
   166 				{
   167 					return ptr == other;
   168 				}
   169 			};
   170 
   171 			void *vfnptr;							//!< Pointer to the function
   172 			void *orig_entry;						//!< The original vtable entry
   173 
   174 			typedef std::list<Iface> IfaceList;
   175 			typedef IfaceList::iterator IfaceListIter;
   176 			IfaceList ifaces;						//!< List of interface pointers
   177 
   178 			bool operator ==(void *other)
   179 			{
   180 				return vfnptr == other;
   181 			}
   182 		};
   183 
   184 		Plugin plug;							//!< The owner plugin
   185 		const char *proto;						//!< The prototype of the function the hook manager is responsible for
   186 		int vtbl_idx;							//!< The vtable index
   187 		int vtbl_offs;							//!< The vtable offset
   188 		HookManagerPubFunc func;				//!< The interface to the hook manager
   189 
   190 		void *hookfunc_vfnptr;					//!< Pointer to the hookfunc impl
   191 		
   192 		typedef std::list<VfnPtr> VfnPtrList;
   193 		typedef VfnPtrList::iterator VfnPtrListIter;
   194 		VfnPtrList vfnptrs;				//!< List of hooked interfaces
   195 	};
   196 
   197 	typedef std::vector<void*> OrigFuncs;
   198 	typedef std::map<int, OrigFuncs> OrigVTables;
   199 
   200 	template<class B> struct CallClass
   201 	{
   202 		B *ptr;
   203 		OrigVTables vt;
   204 	};
   205 
   206 	typedef CallClass<void> GenericCallClass;
   207 
   208 	/**
   209 	*	@brief The main SourceHook interface
   210 	*/
   211 	class ISourceHook
   212 	{
   213 	public:
   214 		/**
   215 		*	@brief Add a hook.
   216 		*
   217 		*	@return True if the function succeeded, false otherwise
   218 		*
   219 		*	@param plug The unique identifier of the plugin that calls this function
   220 		*	@param iface The interface pointer
   221 		*	@param ifacesize The size of the class iface points to
   222 		*	@param myHookMan A hook manager function that should be capable of handling the function
   223 		*	@param handler A pointer to a FastDelegate containing the hook handler
   224 		*	@param post Set to true if you want a post handler
   225 		*/
   226 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
   227 
   228 		/**
   229 		*	@brief Removes a hook.
   230 		*
   231 		*	@return True if the function succeeded, false otherwise
   232 		*
   233 		*	@param plug The unique identifier of the plugin that calls this function
   234 		*	@param iface The interface pointer
   235 		*	@param myHookMan A hook manager function that should be capable of handling the function
   236 		*	@param handler A pointer to a FastDelegate containing the hook handler
   237 		*	@param post Set to true if you want a post handler
   238 		*/
   239 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post) = 0;
   240 
   241 		/**
   242 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   243 		*
   244 		*	@param plug The unique identifier of the plugin in question
   245 		*/
   246 		virtual bool IsPluginInUse(Plugin plug) = 0;
   247 
   248 		/**
   249 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   250 		*
   251 		*	@param iface The interface pointer
   252 		*	@param size Size of the class
   253 		*/
   254 		virtual GenericCallClass *GetCallClass(void *iface) = 0;
   255 
   256 		/**
   257 		*	@brief Release a callclass
   258 		*
   259 		*	@param ptr Pointer to the callclass
   260 		*/
   261 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   262 
   263 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   264 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the previously called handler
   265 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   266 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result. If not in post function, undefined
   267 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result. If none is specified, NULL
   268 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   269 		//////////////////////////////////////////////////////////////////////////
   270 		// For hook managers
   271 		virtual META_RES &GetCurResRef() = 0;				//!< Gets the pointer to the current meta result
   272 		virtual META_RES &GetPrevResRef() = 0;				//!< Gets the pointer to the previous meta result
   273 		virtual META_RES &GetStatusRef() = 0;				//!< Gets the pointer to the status variable
   274 		virtual void SetOrigRet(const void *ptr) = 0;		//!< Sets the original return pointer
   275 		virtual void SetOverrideRet(const void *ptr) = 0;	//!< Sets the override result pointer
   276 		virtual void SetIfacePtr(void *ptr) = 0;			//!< Sets the interface this pointer
   277 	};
   278 }
   279 
   280 /************************************************************************/
   281 /* High level interface                                                 */
   282 /************************************************************************/
   283 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   284 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   285 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   286 
   287 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   288 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   289 #define META_RESULT_ORIG_RET(type)			*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
   290 #define META_RESULT_OVERRIDE_RET(type)		*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
   291 #define META_IFACEPTR						SH_GLOB_SHPTR->GetIfacePtr()
   292 
   293 
   294 /**
   295 *	@brief Get/generate callclass for an interface pointer
   296 *
   297 *	@param ifaceptr The interface pointer
   298 */
   299 template<class ifacetype>
   300 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   301 {
   302 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   303 		shptr->GetCallClass(reinterpret_cast<void*>(ptr)));
   304 }
   305 
   306 template<class ifacetype>
   307 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   308 {
   309 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   310 }
   311 
   312 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   313 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   314 
   315 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   316 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
   317 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   318 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   319 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   320 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   321 
   322 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   323 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)ifaceptr, post, handler)
   324 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   325 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   326 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   327 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   328 
   329 #define SH_NOATTRIB
   330 
   331 
   332 
   333 
   334 #if SH_COMP == SH_COMP_MSVC
   335 # define SH_SETUP_MFP(mfp) \
   336 	reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry;
   337 #elif SH_COMP == SH_COMP_GCC
   338 # define SH_SETUP_MFP(mfp) \
   339 	reinterpret_cast<void**>(&mfp)[0] = vfnptr.orig_entry; \
   340 	reinterpret_cast<void**>(&mfp)[1] = 0;
   341 #else
   342 # error Not supported yet.
   343 #endif
   344 
   345 //////////////////////////////////////////////////////////////////////////
   346 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   347 
   348 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   349 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::HookManagerInfo *param) \
   350 	{ \
   351 		using namespace ::SourceHook; \
   352 		if (action == ::SourceHook::HA_GetInfo) \
   353 		{ \
   354 			param->proto = ms_Proto; \
   355 			MemFuncInfo mfi; \
   356 			GetFuncInfo(funcptr, mfi); \
   357 			param->vtbl_idx = mfi.vtblindex; \
   358 			param->vtbl_offs = mfi.vtbloffs; \
   359 			\
   360 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   361 			param->hookfunc_vfnptr = \
   362 			reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]; \
   363 			return 0; \
   364 		} \
   365 		else if (action == ::SourceHook::HA_Register) \
   366 		{ \
   367 			ms_HI = param; \
   368 			return 0; \
   369 		} \
   370 		else if (action == ::SourceHook::HA_Unregister) \
   371 		{ \
   372 			ms_HI = NULL; \
   373 			return 0; \
   374 		} \
   375 		else \
   376 			return 1; \
   377 	}
   378 
   379 // It has to be possible to use the macros in namespaces
   380 // -> So we need to access and extend the global SourceHook namespace
   381 // We use a namespace alias for this
   382 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   383 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   384 	{ \
   385 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   386 		static ::SourceHook::HookManagerInfo *ms_HI; \
   387 		static const char *ms_Proto; \
   388 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   389 
   390 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
   391 	}; \
   392 	const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
   393 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   394 	::SourceHook::HookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   395 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   396 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   397 	{ \
   398 		using namespace ::SourceHook; \
   399 		MemFuncInfo mfi; \
   400 		GetFuncInfo(funcptr, mfi); \
   401 		if (mfi.thisptroffs < 0) \
   402 			return false; /* No virtual inheritance supported */ \
   403 		\
   404 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   405 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   406 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   407 	} \
   408 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   409 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   410 	{ \
   411 		using namespace ::SourceHook; \
   412 		MemFuncInfo mfi; \
   413 		GetFuncInfo(funcptr, mfi); \
   414 		if (mfi.thisptroffs < 0) \
   415 			return false; /* No virtual inheritance supported */ \
   416 		\
   417 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   418 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   419 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   420 	} \
   421 
   422 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   423 	/* 1) Find the vfnptr */ \
   424 	using namespace ::SourceHook; \
   425 	void *ourvfnptr = reinterpret_cast<void*>( \
   426 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
   427 	\
   428 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   429 		ms_HI->vfnptrs.end(), ourvfnptr); \
   430 	if (vfptriter == ms_HI->vfnptrs.end()) \
   431 	{ \
   432 		/* Bleh? Should be impossible! */ \
   433 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   434 	} \
   435 	HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
   436 	/* 2) Find the iface */ \
   437 	HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
   438 	if (ifiter == vfnptr.ifaces.end()) \
   439 	{ \
   440 		/* The iface info was not found. Redirect the call to the original function. */ \
   441 		rettype (EmptyClass::*mfp)paramtypes; \
   442 		SH_SETUP_MFP(mfp); \
   443 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   444 	} \
   445 	HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
   446 	/* 2) Declare some vars and set it up */ \
   447 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
   448 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
   449 	rettype orig_ret; \
   450 	rettype override_ret; \
   451 	rettype plugin_ret; \
   452 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   453 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   454 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   455 	status = MRES_IGNORED; \
   456 	SH_GLOB_SHPTR->SetIfacePtr(this); \
   457 	SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
   458 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   459 
   460 #define SH_CALL_HOOKS(post, params) \
   461 	prev_res = MRES_IGNORED; \
   462 	for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
   463 	{ \
   464 		if (hiter->paused) continue; \
   465 		cur_res = MRES_IGNORED; \
   466 		plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
   467 		prev_res = cur_res; \
   468 		if (cur_res > status) \
   469 			status = cur_res; \
   470 		if (cur_res >= MRES_OVERRIDE) \
   471 		{ \
   472 			override_ret = plugin_ret; \
   473 			SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   474 		} \
   475 	}
   476 
   477 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   478 	if (status != MRES_SUPERCEDE) \
   479 	{ \
   480 		rettype (EmptyClass::*mfp)paramtypes; \
   481 		SH_SETUP_MFP(mfp); \
   482 		orig_ret = (reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   483 	} \
   484 	else \
   485 		orig_ret = override_ret;
   486 
   487 #define SH_RETURN() \
   488 	return status >= MRES_OVERRIDE ? override_ret : orig_ret;
   489 
   490 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
   491 	SH_SETUPCALLS(rettype, paramtypes, params) \
   492 	SH_CALL_HOOKS(pre, params) \
   493 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   494 	SH_CALL_HOOKS(post, params) \
   495 	SH_RETURN()
   496 
   497 //////////////////////////////////////////////////////////////////////////
   498 #define SH_SETUPCALLS_void(paramtypes, params) \
   499 	using namespace ::SourceHook; \
   500 	/* 1) Find the vfnptr */ \
   501 	void *ourvfnptr = reinterpret_cast<void*>( \
   502 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_HI->vtbl_offs) + ms_HI->vtbl_idx); \
   503 	\
   504 	HookManagerInfo::VfnPtrListIter vfptriter = std::find(ms_HI->vfnptrs.begin(), \
   505 		ms_HI->vfnptrs.end(), ourvfnptr); \
   506 	if (vfptriter == ms_HI->vfnptrs.end()) \
   507 	{ \
   508 		/* Bleh? Should be impossible! */ \
   509 		SH_ASSERT(0, "Called with vfnptr 0x%p which couldn't be found in the list"); \
   510 	} \
   511 	HookManagerInfo::VfnPtr &vfnptr = *vfptriter; \
   512 	/* 2) Find the iface */ \
   513 	HookManagerInfo::VfnPtr::IfaceListIter ifiter = std::find(vfnptr.ifaces.begin(), vfnptr.ifaces.end(), this); \
   514 	if (ifiter == vfnptr.ifaces.end()) \
   515 	{ \
   516 		/* The iface info was not found. Redirect the call to the original function. */ \
   517 		void (EmptyClass::*mfp)paramtypes; \
   518 		SH_SETUP_MFP(mfp); \
   519 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   520 		return; \
   521 	} \
   522 	HookManagerInfo::VfnPtr::Iface &ci = *ifiter; \
   523 	/* 2) Declare some vars and set it up */ \
   524 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &prelist = ci.hooks_pre; \
   525 	std::list<HookManagerInfo::VfnPtr::Iface::Hook> &postlist = ci.hooks_post; \
   526 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   527 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   528 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   529 	status = MRES_IGNORED; \
   530 	SH_GLOB_SHPTR->SetIfacePtr(this); \
   531 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   532 
   533 #define SH_CALL_HOOKS_void(post, params) \
   534 	prev_res = MRES_IGNORED; \
   535 	for (std::list<HookManagerInfo::VfnPtr::Iface::Hook>::iterator hiter = post##list.begin(); hiter != post##list.end(); ++hiter) \
   536 	{ \
   537 		if (hiter->paused) continue; \
   538 		cur_res = MRES_IGNORED; \
   539 		reinterpret_cast<CSHDelegate<FD>*>(hiter->handler)->GetDeleg() params; \
   540 		prev_res = cur_res; \
   541 		if (cur_res > status) \
   542 			status = cur_res; \
   543 	}
   544 
   545 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   546 	if (status != MRES_SUPERCEDE) \
   547 	{ \
   548 		void (EmptyClass::*mfp)paramtypes; \
   549 		SH_SETUP_MFP(mfp); \
   550 		(reinterpret_cast<EmptyClass*>(ci.ptr)->*mfp)params; \
   551 	}
   552 
   553 #define SH_RETURN_void()
   554 
   555 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
   556 	SH_SETUPCALLS_void(paramtypes, params) \
   557 	SH_CALL_HOOKS_void(pre, params) \
   558 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   559 	SH_CALL_HOOKS_void(post, params) \
   560 	SH_RETURN_void()
   561 
   562 
   563 // Special vafmt handlers
   564 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
   565 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   566 	SH_CALL_HOOKS(pre, params_plug) \
   567 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
   568 	SH_CALL_HOOKS(post, params_plug) \
   569 	SH_RETURN()
   570 
   571 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
   572 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   573 	SH_CALL_HOOKS_void(pre, params_plug) \
   574 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
   575 	SH_CALL_HOOKS_void(post, params_plug) \
   576 	SH_RETURN_void()
   577 
   578 //////////////////////////////////////////////////////////////////////////
   579 
   580 @VARARGS@
   581 // ********* Support for @$@ arguments *********
   582 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   583 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
   584 		(&ifacetype::ifacefunc))) \
   585 		typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
   586 		virtual rettype Func(@param%% p%%|, @) \
   587 		{ SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
   588 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
   589 	(static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
   590 
   591 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
   592 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
   593 		(&ifacetype::ifacefunc))) \
   594 		typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
   595 		virtual void Func(@param%% p%%|, @) \
   596 		{ SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
   597 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
   598 	(static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
   599 
   600 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   601 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
   602 		(&ifacetype::ifacefunc))) \
   603 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
   604 		virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
   605 		{ \
   606 			char buf[STRBUF_LEN]; \
   607 			va_list ap; \
   608 			va_start(ap, fmt); \
   609 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   610 			va_end(ap); \
   611 			SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
   612 		} \
   613 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
   614 	(static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   615 
   616 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   617 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
   618 		(&ifacetype::ifacefunc))) \
   619 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
   620 		virtual void Func(@param%% p%%|, @@, @const char *, ...) \
   621 		{ \
   622 			char buf[STRBUF_LEN]; \
   623 			va_list ap; \
   624 			va_start(ap, fmt); \
   625 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   626 			va_end(ap); \
   627 			SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
   628 		} \
   629 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
   630 	(static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   631 
   632 @ENDARGS@
   633 
   634 
   635 //////////////////////////////////////////////////////////////////////////
   636 // SH_CALL
   637 
   638 
   639 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   640 { \
   641 	using namespace ::SourceHook; \
   642 	MemFuncInfo mfi; \
   643 	MFI_Impl<sizeof(m_MFP)>::GetFuncInfo(m_MFP, mfi); \
   644 	OrigVTables::const_iterator iter = m_CC->vt.find(mfi.vtbloffs); \
   645 	if (iter == m_CC->vt.end() || mfi.vtblindex >= (int)iter->second.size() || iter->second[mfi.vtblindex] == NULL) \
   646 		return (m_CC->ptr->*m_MFP)call; \
   647 	\
   648 	/* It's hooked. Call the original function. */ \
   649 	union \
   650 	{ \
   651 		RetType(EmptyClass::*mfpnew)prms; \
   652 		void *addr; \
   653 	} u; \
   654 	u.addr = iter->second[mfi.vtblindex]; \
   655 	\
   656 	return (reinterpret_cast<EmptyClass*>(m_CC->ptr)->*u.mfpnew)call; \
   657 }
   658 
   659 
   660 namespace SourceHook
   661 {
   662 	template<class CCType, class RetType, class MFPType> class ExecutableClass
   663 	{
   664 		CCType *m_CC;
   665 		MFPType m_MFP;
   666 	public:
   667 		ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
   668 		{
   669 		}
   670 
   671 @VARARGS@
   672 		// Support for @$@ arguments
   673 		@template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const 
   674 			SH_MAKE_EXECUTABLECLASS_OB((@p%%@), (@Param%%@))
   675 
   676 @ENDARGS@
   677 	};
   678 }
   679 
   680 @VARARGS@
   681 // Support for @$@ arguments
   682 template <class X, class Y, class RetType@, @@class Param%%|, @>
   683 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>
   684 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @))
   685 {
   686 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @)>(ptr, mfp);
   687 }
   688 
   689 template <class X, class Y, class RetType@, @@class Param%%|, @>
   690 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>
   691 SH_CALL(SourceHook::CallClass<Y> *ptr, RetType(X::*mfp)(@Param%%|, @)const)
   692 {
   693 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, RetType (X::*)(@Param%%|, @) const>(ptr, mfp);
   694 }
   695 
   696 @ENDARGS@
   697 
   698 #undef SH_MAKE_EXECUTABLECLASS_BODY
   699 
   700 #endif
   701 	// The pope is dead. -> :(