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