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