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