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