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