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