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