sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Thu Oct 06 20:46:49 2005 +0000 (2005-10-06)
changeset 123 53e819ac032a
parent 117 e322da4af2d2
child 128 63f95eed16b6
permissions -rwxr-xr-x
It seems to work, but it's not reentrant yet.
     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 //  Future: Thread safe interface?
    24 
    25 #define SH_IFACE_VERSION 3
    26 #define SH_IMPL_VERSION 2
    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 SH informs the loop in the hook manager about its status through this
   278 	*/
   279 	enum HookLoopStatus
   280 	{
   281 		HLS_Continue=0,
   282 		HLS_Stop
   283 	};
   284 
   285 	/**
   286 	*	@brief The main SourceHook interface
   287 	*/
   288 	class ISourceHook
   289 	{
   290 	public:
   291 		/**
   292 		*	@brief Return interface version
   293 		*/
   294 		virtual int GetIfaceVersion() = 0;
   295 
   296 		/**
   297 		*	@brief Return implementation version
   298 		*/
   299 		virtual int GetImplVersion() = 0;
   300 
   301 		/**
   302 		*	@brief Add a hook.
   303 		*
   304 		*	@return True if the function succeeded, false otherwise
   305 		*
   306 		*	@param plug The unique identifier of the plugin that calls this function
   307 		*	@param iface The interface pointer
   308 		*	@param ifacesize The size of the class iface points to
   309 		*	@param myHookMan A hook manager function that should be capable of handling the function
   310 		*	@param handler A pointer to a FastDelegate containing the hook handler
   311 		*	@param post Set to true if you want a post handler
   312 		*/
   313 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   314 			ISHDelegate *handler, bool post) = 0;
   315 
   316 		/**
   317 		*	@brief Removes a hook.
   318 		*
   319 		*	@return True if the function succeeded, false otherwise
   320 		*
   321 		*	@param plug The unique identifier of the plugin that calls this function
   322 		*	@param iface The interface pointer
   323 		*	@param myHookMan A hook manager function that should be capable of handling the function
   324 		*	@param handler A pointer to a FastDelegate containing the hook handler
   325 		*	@param post Set to true if you want a post handler
   326 		*/
   327 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   328 			ISHDelegate *handler, bool post) = 0;
   329 
   330 		/**
   331 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   332 		*
   333 		*	@param plug The unique identifier of the plugin in question
   334 		*/
   335 		virtual bool IsPluginInUse(Plugin plug) = 0;
   336 
   337 		/**
   338 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   339 		*
   340 		*	@param iface The interface pointer
   341 		*	@param size Size of the class instance
   342 		*/
   343 		virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
   344 
   345 		/**
   346 		*	@brief Release a callclass
   347 		*
   348 		*	@param ptr Pointer to the callclass
   349 		*/
   350 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   351 
   352 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   353 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   354 															//!<  previously calledhandler
   355 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   356 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   357 															//!<  If not in post function, undefined
   358 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   359 															//!<  If none is specified, NULL
   360 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   361 		//////////////////////////////////////////////////////////////////////////
   362 		// For hook managers
   363 		virtual META_RES &GetCurResRef() = 0;				//!< Gets the reference to the current meta result
   364 		virtual META_RES &GetPrevResRef() = 0;				//!< Gets the reference to the previous meta result
   365 		virtual META_RES &GetStatusRef() = 0;				//!< Gets the reference to the status variable
   366 		virtual void* &GetIfacePtrRef() = 0;				//!< Gets the reference to the interface this pointer
   367 		virtual void SetOrigRet(const void *ptr) = 0;		//!< Sets the original return pointer
   368 		virtual void SetOverrideRet(const void *ptr) = 0;	//!< Sets the override result pointer
   369 		virtual HookLoopStatus &GetStatusVarRef(IIface *pIface) = 0;			//!< gets the reference to the hook status loop variable
   370 		virtual void HookLoopDone() = 0;
   371 	};
   372 }
   373 
   374 /************************************************************************/
   375 /* High level interface                                                 */
   376 /************************************************************************/
   377 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   378 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   379 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   380 
   381 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   382 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   383 #define META_RESULT_ORIG_RET(type)			*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
   384 #define META_RESULT_OVERRIDE_RET(type)		*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
   385 #define META_IFACEPTR						SH_GLOB_SHPTR->GetIfacePtr()
   386 
   387 
   388 /**
   389 *	@brief Get/generate callclass for an interface pointer
   390 *
   391 *	@param ifaceptr The interface pointer
   392 */
   393 template<class ifacetype>
   394 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   395 {
   396 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   397 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
   398 }
   399 
   400 template<class ifacetype>
   401 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   402 {
   403 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   404 }
   405 
   406 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   407 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   408 
   409 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   410 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   411 	post, handler)
   412 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   413 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   414 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   415 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   416 
   417 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   418 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   419 	post, handler)
   420 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   421 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   422 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   423 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   424 
   425 #define SH_NOATTRIB
   426 
   427 
   428 
   429 
   430 #if SH_COMP == SH_COMP_MSVC
   431 # define SH_SETUP_MFP(mfp) \
   432 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   433 #elif SH_COMP == SH_COMP_GCC
   434 # define SH_SETUP_MFP(mfp) \
   435 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   436 	reinterpret_cast<void**>(&mfp)[1] = 0;
   437 #else
   438 # error Not supported yet.
   439 #endif
   440 
   441 //////////////////////////////////////////////////////////////////////////
   442 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   443 
   444 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   445 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   446 	{ \
   447 		GetFuncInfo(funcptr, ms_MFI); \
   448 	} \
   449 	\
   450 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   451 	{ \
   452 		using namespace ::SourceHook; \
   453 		GetFuncInfo(funcptr, ms_MFI); \
   454 		/* Verify interface version */ \
   455 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   456 			return 1; \
   457 		\
   458 		if (action == ::SourceHook::HA_GetInfo) \
   459 		{ \
   460 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, ms_Proto); \
   461 			\
   462 			MemFuncInfo mfi; \
   463 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   464 			param->SetHookfuncVfnptr( \
   465 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   466 			return 0; \
   467 		} \
   468 		else if (action == ::SourceHook::HA_Register) \
   469 		{ \
   470 			ms_HI = param; \
   471 			return 0; \
   472 		} \
   473 		else if (action == ::SourceHook::HA_Unregister) \
   474 		{ \
   475 			ms_HI = NULL; \
   476 			return 0; \
   477 		} \
   478 		else \
   479 			return 1; \
   480 	}
   481 
   482 // It has to be possible to use the macros in namespaces
   483 // -> So we need to access and extend the global SourceHook namespace
   484 // We use a namespace alias for this
   485 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   486 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   487 	{ \
   488 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   489 		static ::SourceHook::MemFuncInfo ms_MFI; \
   490 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   491 		static const char *ms_Proto; \
   492 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   493 
   494 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, proto, funcptr) \
   495 	}; \
   496 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   497 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   498 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   499 	const char *SH_FHCls(ifacetype,ifacefunc,overload)::ms_Proto = proto; \
   500 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   501 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   502 	{ \
   503 		using namespace ::SourceHook; \
   504 		MemFuncInfo mfi; \
   505 		GetFuncInfo(funcptr, mfi); \
   506 		if (mfi.thisptroffs < 0) \
   507 			return false; /* No virtual inheritance supported */ \
   508 		\
   509 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   510 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   511 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   512 	} \
   513 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   514 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   515 	{ \
   516 		using namespace ::SourceHook; \
   517 		MemFuncInfo mfi; \
   518 		GetFuncInfo(funcptr, mfi); \
   519 		if (mfi.thisptroffs < 0) \
   520 			return false; /* No virtual inheritance supported */ \
   521 		\
   522 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   523 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   524 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   525 	} \
   526 
   527 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   528 	/* 1) Find the vfnptr */ \
   529 	using namespace ::SourceHook; \
   530 	void *ourvfnptr = reinterpret_cast<void*>( \
   531 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   532 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   533 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   534 	\
   535 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   536 	/* ... and the iface */ \
   537 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   538 	if (!ifinfo) \
   539 	{ \
   540 		/* The iface info was not found. Redirect the call to the original function. */ \
   541 		rettype (EmptyClass::*mfp)paramtypes; \
   542 		SH_SETUP_MFP(mfp); \
   543 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   544 	} \
   545 	/* 2) Declare some vars and set it up */ \
   546 	IHookList *prelist = ifinfo->GetPreHooks(); \
   547 	IHookList *postlist = ifinfo->GetPostHooks(); \
   548 	HookLoopStatus &hls = SH_GLOB_SHPTR->GetStatusVarRef(ifinfo); \
   549 	hls = HLS_Continue; \
   550 	rettype orig_ret; \
   551 	rettype override_ret; \
   552 	rettype plugin_ret; \
   553 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   554 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   555 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   556 	void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
   557 	status = MRES_IGNORED; \
   558 	SH_GLOB_SHPTR->SetOrigRet(reinterpret_cast<void*>(&orig_ret)); \
   559 	SH_GLOB_SHPTR->SetOverrideRet(NULL);
   560 
   561 #define SH_CALL_HOOKS(post, params) \
   562 	if (hls == HLS_Continue) \
   563 	{ \
   564 		prev_res = MRES_IGNORED; \
   565 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   566 		{ \
   567 			cur_res = MRES_IGNORED; \
   568 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   569 			hls = HLS_Continue; \
   570 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   571 			prev_res = cur_res; \
   572 			if (cur_res > status) \
   573 				status = cur_res; \
   574 			if (cur_res >= MRES_OVERRIDE) \
   575 			{ \
   576 				override_ret = plugin_ret; \
   577 				SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   578 			} \
   579 			if (hls == HLS_Stop) \
   580 			{ \
   581 				iter.SetToZero(); \
   582 				break; \
   583 			} \
   584 		} \
   585 	}
   586 
   587 #define SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   588 	if (status != MRES_SUPERCEDE) \
   589 	{ \
   590 		rettype (EmptyClass::*mfp)paramtypes; \
   591 		SH_SETUP_MFP(mfp); \
   592 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   593 	} \
   594 	else \
   595 		orig_ret = override_ret;
   596 
   597 #define SH_RETURN() \
   598 	return status >= MRES_OVERRIDE ? override_ret : orig_ret;
   599 
   600 #define SH_HANDLEFUNC(ifacetype, ifacefunc, paramtypes, params, rettype) \
   601 	SH_SETUPCALLS(rettype, paramtypes, params) \
   602 	SH_CALL_HOOKS(pre, params) \
   603 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params) \
   604 	SH_CALL_HOOKS(post, params) \
   605 	SH_RETURN()
   606 
   607 //////////////////////////////////////////////////////////////////////////
   608 #define SH_SETUPCALLS_void(paramtypes, params) \
   609 	/* 1) Find the vfnptr */ \
   610 	using namespace ::SourceHook; \
   611 	void *ourvfnptr = reinterpret_cast<void*>( \
   612 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   613 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   614 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   615 	\
   616 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   617 	/* ... and the iface */ \
   618 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   619 	if (!ifinfo) \
   620 	{ \
   621 		/* The iface info was not found. Redirect the call to the original function. */ \
   622 		void (EmptyClass::*mfp)paramtypes; \
   623 		SH_SETUP_MFP(mfp); \
   624 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   625 		return; \
   626 	} \
   627 	/* 2) Declare some vars and set it up */ \
   628 	IHookList *prelist = ifinfo->GetPreHooks(); \
   629 	IHookList *postlist = ifinfo->GetPostHooks(); \
   630 	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   631 	HookLoopStatus &hls = SH_GLOB_SHPTR->GetStatusVarRef(ifinfo); \
   632 	hls = HLS_Continue; \
   633 	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   634 	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   635 	void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
   636 	status = MRES_IGNORED; \
   637 	SH_GLOB_SHPTR->SetOverrideRet(NULL); \
   638 	SH_GLOB_SHPTR->SetOrigRet(NULL);
   639 
   640 #define SH_CALL_HOOKS_void(post, params) \
   641 	if (hls == HLS_Continue) \
   642 	{ \
   643 		prev_res = MRES_IGNORED; \
   644 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   645 		{ \
   646 			cur_res = MRES_IGNORED; \
   647 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   648 			hls = HLS_Continue; \
   649 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   650 			prev_res = cur_res; \
   651 			if (cur_res > status) \
   652 				status = cur_res; \
   653 			if (hls == HLS_Stop) \
   654 			{ \
   655 				iter.SetToZero(); \
   656 				break; \
   657 			} \
   658 		} \
   659 	}
   660 
   661 #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   662 	if (status != MRES_SUPERCEDE) \
   663 	{ \
   664 		void (EmptyClass::*mfp)paramtypes; \
   665 		SH_SETUP_MFP(mfp); \
   666 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   667 	}
   668 
   669 #define SH_RETURN_void()
   670 
   671 #define SH_HANDLEFUNC_void(ifacetype, ifacefunc, paramtypes, params) \
   672 	SH_SETUPCALLS_void(paramtypes, params) \
   673 	SH_CALL_HOOKS_void(pre, params) \
   674 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   675 	SH_CALL_HOOKS_void(post, params) \
   676 	SH_RETURN_void()
   677 
   678 
   679 // Special vafmt handlers
   680 #define SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug, rettype) \
   681 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   682 	SH_CALL_HOOKS(pre, params_plug) \
   683 	SH_CALL_ORIG(ifacetype, ifacefunc, rettype, paramtypes, params_orig) \
   684 	SH_CALL_HOOKS(post, params_plug) \
   685 	SH_RETURN()
   686 
   687 #define SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, paramtypes, params_orig, params_plug) \
   688 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   689 	SH_CALL_HOOKS_void(pre, params_plug) \
   690 	SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params_orig) \
   691 	SH_CALL_HOOKS_void(post, params_plug) \
   692 	SH_RETURN_void()
   693 
   694 //////////////////////////////////////////////////////////////////////////
   695 
   696 @VARARGS@
   697 // ********* Support for @$@ arguments *********
   698 #define SH_DECL_HOOK@$@(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   699 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @) attr> \
   700 		(&ifacetype::ifacefunc))) \
   701 		typedef fastdelegate::FastDelegate@$@<@param%%|, @@, @rettype> FD; \
   702 		virtual rettype Func(@param%% p%%|, @) \
   703 		{ SH_HANDLEFUNC(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @), rettype); } \
   704 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @, \
   705 	(static_cast<rettype (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
   706 
   707 #define SH_DECL_HOOK@$@_void(ifacetype, ifacefunc, attr, overload@, param%%@) \
   708 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @) attr> \
   709 		(&ifacetype::ifacefunc))) \
   710 		typedef fastdelegate::FastDelegate@$@<@param%%|, @> FD; \
   711 		virtual void Func(@param%% p%%|, @) \
   712 		{ SH_HANDLEFUNC_void(ifacetype, ifacefunc, (@param%%|, @), (@p%%|, @)); } \
   713 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @, \
   714 	(static_cast<void (ifacetype::*)(@param%%|, @) attr>(&ifacetype::ifacefunc)))
   715 
   716 #define SH_DECL_HOOK@$@_vafmt(ifacetype, ifacefunc, attr, overload, rettype@, param%%@) \
   717 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
   718 		(&ifacetype::ifacefunc))) \
   719 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *, rettype> FD; \
   720 		virtual rettype Func(@param%% p%%|, @@, @const char *fmt, ...) \
   721 		{ \
   722 			char buf[::SourceHook::STRBUF_LEN]; \
   723 			va_list ap; \
   724 			va_start(ap, fmt); \
   725 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   726 			va_end(ap); \
   727 			SH_HANDLEFUNC_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf), rettype); \
   728 		} \
   729 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr "|" #rettype @"|" #param%%| @ "|const char*|...", \
   730 	(static_cast<rettype (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   731 
   732 #define SH_DECL_HOOK@$@_void_vafmt(ifacetype, ifacefunc, attr, overload@, param%%@) \
   733 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr> \
   734 		(&ifacetype::ifacefunc))) \
   735 		typedef fastdelegate::FastDelegate@$+1@<@param%%|, @@, @const char *> FD; \
   736 		virtual void Func(@param%% p%%|, @@, @const char *fmt, ...) \
   737 		{ \
   738 			char buf[::SourceHook::STRBUF_LEN]; \
   739 			va_list ap; \
   740 			va_start(ap, fmt); \
   741 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   742 			va_end(ap); \
   743 			SH_HANDLEFUNC_void_vafmt(ifacetype, ifacefunc, (@param%%|, @@, @...), (@p%%|, @@, @"%s", buf), (@p%%|, @@, @buf)); \
   744 		} \
   745 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, #attr @"|" #param%%| @ "|const char*|...", \
   746 	(static_cast<void (ifacetype::*)(@param%%|, @@, @const char *, ...) attr>(&ifacetype::ifacefunc)))
   747 
   748 @ENDARGS@
   749 
   750 
   751 //////////////////////////////////////////////////////////////////////////
   752 // SH_CALL
   753 
   754 #if SH_COMP == SH_COMP_MSVC
   755 
   756 // :TODO: TEST THIS ON MSVC
   757 
   758 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   759 { \
   760 	using namespace ::SourceHook; \
   761 	MemFuncInfo mfi; \
   762 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
   763 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
   764 	if (!origfunc) \
   765 		return (m_CC->GetThisPtr()->*m_MFP)call; \
   766 	\
   767 	/* It's hooked. Call the original function. */ \
   768 	union \
   769 	{ \
   770 		RetType(EmptyClass::*mfpnew)prms; \
   771 		void *addr; \
   772 	} u; \
   773 	u.addr = origfunc; \
   774 	\
   775 	void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
   776 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
   777 }
   778 
   779 #elif SH_COMP == SH_COMP_GCC
   780 
   781 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   782 { \
   783 	using namespace ::SourceHook; \
   784 	MemFuncInfo mfi; \
   785 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
   786 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
   787 	if (!origfunc) \
   788 		return (m_CC->GetThisPtr()->*m_MFP)call; \
   789 	\
   790 	/* It's hooked. Call the original function. */ \
   791 	union \
   792 	{ \
   793 		RetType(EmptyClass::*mfpnew)prms; \
   794 		struct \
   795 		{ \
   796 			void *addr; \
   797 			intptr_t adjustor; \
   798 		} s; \
   799 	} u; \
   800 	u.s.addr = origfunc; \
   801 	u.s.adjustor = mfi.thisptroffs; \
   802 	\
   803 	return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
   804 }
   805 
   806 #endif
   807 
   808 namespace SourceHook
   809 {
   810 	template<class CCType, class RetType, class MFPType> class ExecutableClass
   811 	{
   812 		CCType *m_CC;
   813 		MFPType m_MFP;
   814 	public:
   815 		ExecutableClass(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp)
   816 		{
   817 		}
   818 
   819 @VARARGS@
   820 		// Support for @$@ arguments
   821 		@template<@@class Param%%|, @@> @RetType operator()(@Param%% p%%|, @) const
   822 			SH_MAKE_EXECUTABLECLASS_OB((@p%%|, @), (@Param%%|, @))
   823 
   824 @ENDARGS@
   825 	};
   826 }
   827 
   828 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
   829 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
   830 // The only purpose of the mfp2 parameter is to extract the return type
   831 
   832 @VARARGS@
   833 // Support for @$@ arguments
   834 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
   835 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
   836 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @))
   837 {
   838 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
   839 }
   840 
   841 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
   842 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
   843 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @)const)
   844 {
   845 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
   846 }
   847 
   848 @ENDARGS@
   849 
   850 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
   851 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
   852 
   853 @VARARGS@
   854 // Support for @$@ arguments
   855 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
   856 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
   857 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...))
   858 {
   859 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
   860 }
   861 
   862 template <class X, class Y, class MFP, class RetType@, @@class Param%%|, @>
   863 SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>
   864 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@Param%%|, @@, @...)const)
   865 {
   866 	return SourceHook::ExecutableClass<SourceHook::CallClass<Y>, RetType, MFP>(ptr, mfp);
   867 }
   868 
   869 @ENDARGS@
   870 
   871 #endif
   872 
   873 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
   874 
   875 #undef SH_MAKE_EXECUTABLECLASS_OB
   876 
   877 #endif
   878 	// The pope is dead. -> :(