sourcehook/generate/sourcehook.hxx
branchsourcemm-1.6.0
changeset 347 e82968c21606
child 361 f34718303c8f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/generate/sourcehook.hxx	Tue Sep 18 20:03:47 2007 +0000
     1.3 @@ -0,0 +1,1370 @@
     1.4 +/* ======== SourceHook ========
     1.5 +* Copyright (C) 2004-2007 Metamod:Source Development Team
     1.6 +* No warranties of any kind
     1.7 +*
     1.8 +* License: zlib/libpng
     1.9 +*
    1.10 +* Author(s): Pavol "PM OnoTo" Marko
    1.11 +* ============================
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +*	@file sourcehook.h
    1.16 +*	@brief Contains the public SourceHook API
    1.17 +*/
    1.18 +
    1.19 +#ifndef __SOURCEHOOK_H__
    1.20 +#define __SOURCEHOOK_H__
    1.21 +
    1.22 +// Interface revisions:
    1.23 +//  1 - Initial revision
    1.24 +//  2 - Changed to virtual functions for iterators and all queries
    1.25 +//  3 - Added "hook loop status variable"
    1.26 +//  4 - Reentrant
    1.27 +#define SH_IFACE_VERSION 4
    1.28 +#define SH_IMPL_VERSION 3
    1.29 +
    1.30 +// Hookman version:
    1.31 +//  1 - Support for recalls, performance optimisations
    1.32 +#define SH_HOOKMAN_VERSION 1
    1.33 +
    1.34 +// The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
    1.35 +// It's used in various macros
    1.36 +#ifndef SH_GLOB_SHPTR
    1.37 +#define SH_GLOB_SHPTR g_SHPtr
    1.38 +#endif
    1.39 +
    1.40 +// Used to identify the current plugin
    1.41 +#ifndef SH_GLOB_PLUGPTR
    1.42 +#define SH_GLOB_PLUGPTR g_PLID
    1.43 +#endif
    1.44 +
    1.45 +#ifdef SH_DEBUG
    1.46 +# include <stdio.h>
    1.47 +# include <stdlib.h>
    1.48 +
    1.49 +# define SH_ASSERT(x, info) \
    1.50 +	do { \
    1.51 +		if (!(x)) \
    1.52 +		{ \
    1.53 +			printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
    1.54 +			printf info; \
    1.55 +			putchar('\n'); \
    1.56 +			abort(); \
    1.57 +		} \
    1.58 +	} while(0)
    1.59 +
    1.60 +#else
    1.61 +# define SH_ASSERT(x, info)
    1.62 +#endif
    1.63 +
    1.64 +// System
    1.65 +#define SH_SYS_WIN32 1
    1.66 +#define SH_SYS_LINUX 2
    1.67 +
    1.68 +#ifdef _WIN32
    1.69 +# define SH_SYS SH_SYS_WIN32
    1.70 +#elif defined __linux__
    1.71 +# define SH_SYS SH_SYS_LINUX
    1.72 +#else
    1.73 +# error Unsupported system
    1.74 +#endif
    1.75 +
    1.76 +// Compiler
    1.77 +#define SH_COMP_GCC 1
    1.78 +#define SH_COMP_MSVC 2
    1.79 +
    1.80 +#ifdef _MSC_VER
    1.81 +# define SH_COMP SH_COMP_MSVC
    1.82 +#elif defined __GNUC__
    1.83 +# define SH_COMP SH_COMP_GCC
    1.84 +#else
    1.85 +# error Unsupported compiler
    1.86 +#endif
    1.87 +
    1.88 +#if SH_COMP==SH_COMP_MSVC
    1.89 +# define vsnprintf _vsnprintf
    1.90 +#endif
    1.91 +
    1.92 +#if SH_SYS != SH_SYS_WIN32
    1.93 +# include <unistd.h>
    1.94 +#endif
    1.95 +
    1.96 +#define SH_PTRSIZE sizeof(void*)
    1.97 +
    1.98 +#include "FastDelegate.h"
    1.99 +#include "sh_memfuncinfo.h"
   1.100 +
   1.101 +// Good old metamod!
   1.102 +
   1.103 +// Flags returned by a plugin's api function.
   1.104 +// NOTE: order is crucial, as greater/less comparisons are made.
   1.105 +enum META_RES
   1.106 +{
   1.107 +	MRES_IGNORED=0,		// plugin didn't take any action
   1.108 +	MRES_HANDLED,		// plugin did something, but real function should still be called
   1.109 +	MRES_OVERRIDE,		// call real function, but use my return value
   1.110 +	MRES_SUPERCEDE		// skip real function; use my return value
   1.111 +};
   1.112 +
   1.113 +
   1.114 +namespace SourceHook
   1.115 +{
   1.116 +	/**
   1.117 +	*	@brief	Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
   1.118 +	*/
   1.119 +	const int STRBUF_LEN=4096;
   1.120 +
   1.121 +	/**
   1.122 +	*	@brief	An empty class. No inheritance used. Used for original-function-call hacks
   1.123 +	*/
   1.124 +	class EmptyClass
   1.125 +	{
   1.126 +	};
   1.127 +
   1.128 +	/**
   1.129 +	*	@brief Implicit cast.
   1.130 +	*/
   1.131 +	template <class In, class Out>
   1.132 +		inline Out implicit_cast(In input)
   1.133 +		{
   1.134 +			return input;
   1.135 +		}
   1.136 +
   1.137 +	/**
   1.138 +	*	@brief A plugin typedef
   1.139 +	*
   1.140 +	*	SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
   1.141 +	*	plugin has a unique identifier, everything is ok.
   1.142 +	*	It should also be possible to set it to 0.
   1.143 +	*/
   1.144 +	typedef int Plugin;
   1.145 +
   1.146 +	struct ProtoInfo
   1.147 +	{
   1.148 +		ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
   1.149 +		{
   1.150 +		}
   1.151 +		int beginningNull;		//!< To distinguish from old protos (which never begin with 0)
   1.152 +		int retTypeSize;		//!< 0 if void
   1.153 +		int numOfParams;		//!< number of parameters
   1.154 +		const int *params;		//!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
   1.155 +	};
   1.156 +
   1.157 +	/**
   1.158 +	*	@brief Specifies the actions for hook managers
   1.159 +	*/
   1.160 +	enum HookManagerAction
   1.161 +	{
   1.162 +		HA_GetInfo = 0,			//!< Store info about the hook manager
   1.163 +		HA_Register,			//!< Save the IHookManagerInfo pointer for future reference
   1.164 +		HA_Unregister			//!< Clear the saved pointer
   1.165 +	};
   1.166 +
   1.167 +	struct IHookManagerInfo;
   1.168 +
   1.169 +	/**
   1.170 +	*	@brief Pointer to hook manager interface function
   1.171 +	*
   1.172 +	*	A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
   1.173 +	*
   1.174 +	*   @param ha What the hook manager should do
   1.175 +	*	@param hi A pointer to IHookManagerInfo
   1.176 +	*/
   1.177 +	typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
   1.178 +
   1.179 +	class ISHDelegate
   1.180 +	{
   1.181 +	public:
   1.182 +		virtual void DeleteThis() = 0;				// Ugly, I know
   1.183 +		virtual bool IsEqual(ISHDelegate *other) = 0;
   1.184 +	};
   1.185 +
   1.186 +	template <class T> class CSHDelegate : public ISHDelegate
   1.187 +	{
   1.188 +		T m_Deleg;
   1.189 +	public:
   1.190 +		CSHDelegate(T deleg) : m_Deleg(deleg)
   1.191 +		{
   1.192 +		}
   1.193 +
   1.194 +		CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
   1.195 +		{
   1.196 +		}
   1.197 +
   1.198 +		void DeleteThis()
   1.199 +		{
   1.200 +			delete this;
   1.201 +		}
   1.202 +
   1.203 +		bool IsEqual(ISHDelegate *other)
   1.204 +		{
   1.205 +			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
   1.206 +		}
   1.207 +
   1.208 +		T &GetDeleg()
   1.209 +		{
   1.210 +			return m_Deleg;
   1.211 +		}
   1.212 +	};
   1.213 +
   1.214 +	struct IHookList
   1.215 +	{
   1.216 +		struct IIter
   1.217 +		{
   1.218 +			virtual bool End() = 0;
   1.219 +			virtual void Next() = 0;
   1.220 +			virtual ISHDelegate *Handler() = 0;
   1.221 +			virtual int ThisPtrOffs() = 0;
   1.222 +		};
   1.223 +		virtual IIter *GetIter() = 0;
   1.224 +		virtual void ReleaseIter(IIter *pIter) = 0;
   1.225 +	};
   1.226 +
   1.227 +	struct IIface
   1.228 +	{
   1.229 +		virtual void *GetPtr() = 0;
   1.230 +		virtual IHookList *GetPreHooks() = 0;
   1.231 +		virtual IHookList *GetPostHooks() = 0;
   1.232 +	};
   1.233 +
   1.234 +	struct IVfnPtr
   1.235 +	{
   1.236 +		virtual void *GetVfnPtr() = 0;
   1.237 +		virtual void *GetOrigEntry() = 0;
   1.238 +
   1.239 +		virtual IIface *FindIface(void *ptr) = 0;
   1.240 +	};
   1.241 +
   1.242 +	struct IHookManagerInfo
   1.243 +	{
   1.244 +		virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
   1.245 +
   1.246 +		virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
   1.247 +		virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
   1.248 +
   1.249 +		// Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
   1.250 +		// If the hookman doesn't set this, it defaults 0
   1.251 +		// SourceHook prefers hookmans with higher version numbers
   1.252 +		virtual void SetVersion(int version) = 0;
   1.253 +	};
   1.254 +
   1.255 +	class AutoHookIter
   1.256 +	{
   1.257 +		IHookList *m_pList;
   1.258 +		IHookList::IIter *m_pIter;
   1.259 +	public:
   1.260 +		AutoHookIter(IHookList *pList)
   1.261 +			: m_pList(pList), m_pIter(pList->GetIter())
   1.262 +		{
   1.263 +		}
   1.264 +
   1.265 +		~AutoHookIter()
   1.266 +		{
   1.267 +			if (m_pList)
   1.268 +				m_pList->ReleaseIter(m_pIter);
   1.269 +		}
   1.270 +
   1.271 +		bool End()
   1.272 +		{
   1.273 +			return m_pIter->End();
   1.274 +		}
   1.275 +
   1.276 +		void Next()
   1.277 +		{
   1.278 +			m_pIter->Next();
   1.279 +		}
   1.280 +
   1.281 +		ISHDelegate *Handler()
   1.282 +		{
   1.283 +			return m_pIter->Handler();
   1.284 +		}
   1.285 +
   1.286 +		int ThisPtrOffs()
   1.287 +		{
   1.288 +			return m_pIter->ThisPtrOffs();
   1.289 +		}
   1.290 +
   1.291 +		void SetToZero()
   1.292 +		{
   1.293 +			m_pList = 0;
   1.294 +		}
   1.295 +	};
   1.296 +
   1.297 +	template<class B> struct CallClass
   1.298 +	{
   1.299 +		virtual B *GetThisPtr() = 0;
   1.300 +		virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
   1.301 +	};
   1.302 +
   1.303 +	typedef CallClass<void> GenericCallClass;
   1.304 +	typedef CallClass<EmptyClass> ManualCallClass;
   1.305 +
   1.306 +	// 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
   1.307 +	// - Today is an important day.
   1.308 +	// I'm adding support for functions which return references.
   1.309 +
   1.310 +	// How it works:
   1.311 +	//  SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
   1.312 +	//  override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
   1.313 +	//  this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
   1.314 +	//  which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
   1.315 +	//  special cases were needed for getoverrideret / getorigret; these are implemented through the
   1.316 +	//  SourceHook::MacroRefHelpers structs.
   1.317 +	//  Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
   1.318 +	template <class T> struct ReferenceCarrier
   1.319 +	{
   1.320 +		typedef T type;
   1.321 +	};
   1.322 +
   1.323 +	template <class T> struct ReferenceCarrier<T&>
   1.324 +	{
   1.325 +		class type
   1.326 +		{
   1.327 +			T *m_StoredRef;
   1.328 +		public:
   1.329 +			type() : m_StoredRef(NULL)
   1.330 +			{
   1.331 +			}
   1.332 +			type(T& ref) : m_StoredRef(&ref)
   1.333 +			{
   1.334 +			}
   1.335 +
   1.336 +			T& operator= (T& ref)
   1.337 +			{
   1.338 +				m_StoredRef = &ref;
   1.339 +				return ref;
   1.340 +			}
   1.341 +
   1.342 +			operator T& () const
   1.343 +			{
   1.344 +				return *m_StoredRef;
   1.345 +			}
   1.346 +		};
   1.347 +	};
   1.348 +
   1.349 +	/**
   1.350 +	*	@brief The main SourceHook interface
   1.351 +	*/
   1.352 +	class ISourceHook
   1.353 +	{
   1.354 +	public:
   1.355 +		/**
   1.356 +		*	@brief Return interface version
   1.357 +		*/
   1.358 +		virtual int GetIfaceVersion() = 0;
   1.359 +
   1.360 +		/**
   1.361 +		*	@brief Return implementation version
   1.362 +		*/
   1.363 +		virtual int GetImplVersion() = 0;
   1.364 +
   1.365 +		/**
   1.366 +		*	@brief Add a hook.
   1.367 +		*
   1.368 +		*	@return True if the function succeeded, false otherwise
   1.369 +		*
   1.370 +		*	@param plug The unique identifier of the plugin that calls this function
   1.371 +		*	@param iface The interface pointer
   1.372 +		*	@param ifacesize The size of the class iface points to
   1.373 +		*	@param myHookMan A hook manager function that should be capable of handling the function
   1.374 +		*	@param handler A pointer to a FastDelegate containing the hook handler
   1.375 +		*	@param post Set to true if you want a post handler
   1.376 +		*/
   1.377 +		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   1.378 +			ISHDelegate *handler, bool post) = 0;
   1.379 +
   1.380 +		/**
   1.381 +		*	@brief Removes a hook.
   1.382 +		*
   1.383 +		*	@return True if the function succeeded, false otherwise
   1.384 +		*
   1.385 +		*	@param plug The unique identifier of the plugin that calls this function
   1.386 +		*	@param iface The interface pointer
   1.387 +		*	@param myHookMan A hook manager function that should be capable of handling the function
   1.388 +		*	@param handler A pointer to a FastDelegate containing the hook handler
   1.389 +		*	@param post Set to true if you want a post handler
   1.390 +		*/
   1.391 +		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   1.392 +			ISHDelegate *handler, bool post) = 0;
   1.393 +
   1.394 +		/**
   1.395 +		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   1.396 +		*
   1.397 +		*	@param plug The unique identifier of the plugin in question
   1.398 +		*/
   1.399 +		virtual bool IsPluginInUse(Plugin plug) = 0;
   1.400 +
   1.401 +		/**
   1.402 +		*	@brief Return a pointer to a callclass. Generate a new one if required.
   1.403 +		*
   1.404 +		*	@param iface The interface pointer
   1.405 +		*	@param size Size of the class instance
   1.406 +		*/
   1.407 +		virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
   1.408 +
   1.409 +		/**
   1.410 +		*	@brief Release a callclass
   1.411 +		*
   1.412 +		*	@param ptr Pointer to the callclass
   1.413 +		*/
   1.414 +		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   1.415 +
   1.416 +		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   1.417 +		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   1.418 +															//!<  previously calledhandler
   1.419 +		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   1.420 +		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   1.421 +															//!<  If not in post function, undefined
   1.422 +		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   1.423 +															//!<  If none is specified, NULL
   1.424 +		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   1.425 +		//////////////////////////////////////////////////////////////////////////
   1.426 +		// For hook managers
   1.427 +		virtual void HookLoopBegin(IIface *pIface) = 0;			//!< Should be called when a hook loop begins
   1.428 +		virtual void HookLoopEnd() = 0;							//!< Should be called when a hook loop exits
   1.429 +		virtual void SetCurResPtr(META_RES *mres) = 0;			//!< Sets pointer to the current meta result
   1.430 +		virtual void SetPrevResPtr(META_RES *mres) = 0;			//!< Sets pointer to previous meta result
   1.431 +		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
   1.432 +		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
   1.433 +		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
   1.434 +		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
   1.435 +		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
   1.436 +
   1.437 +		/**
   1.438 +		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
   1.439 +		*
   1.440 +		*	@param plug The owner of the hook manager
   1.441 +		*   @param pubFunc The hook manager's info function
   1.442 +		*/
   1.443 +		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
   1.444 +
   1.445 +		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   1.446 +		/*
   1.447 +			HOW RECALLS WORK:
   1.448 +			
   1.449 +			The problem:
   1.450 +				Users want the ability to change parameters of the called function
   1.451 +				from inside their handler.
   1.452 +			The solution:
   1.453 +				1) Mark as "recall"
   1.454 +				2) Recall the function
   1.455 +				3) => SH's hook func gets called:
   1.456 +				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   1.457 +				  5) does all iteration and returns normally
   1.458 +		        6) The user's handler returns immediately
   1.459 +				7) The hook func returns immediately as well
   1.460 +
   1.461 +				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   1.462 +				ended with. The last handler (doing the recall) is also able to specify its own
   1.463 +				META_RES.
   1.464 +		*/
   1.465 +
   1.466 +		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
   1.467 +
   1.468 +		/**
   1.469 +		*	@brief Set up the hook loop. Equivalent to calling:
   1.470 +		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
   1.471 +		*
   1.472 +		*	@param statusPtr		pointer to status variable
   1.473 +		*	@param prevResPtr		pointer to previous result variable
   1.474 +		*	@param curResPtr		pointer to current result variable
   1.475 +		*	@param ifacePtrPtr		pointer to interface this pointer variable
   1.476 +		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   1.477 +		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   1.478 +		*	
   1.479 +		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   1.480 +		*		when the hook func is being called as part of a recall
   1.481 +		*/
   1.482 +		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   1.483 +			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
   1.484 +
   1.485 +		//!< 
   1.486 +	};
   1.487 +
   1.488 +	// For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
   1.489 +	//  These have to be able to return references. If T is a reference, the pointers returned
   1.490 +	//  from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
   1.491 +	template <class T> struct MacroRefHelpers
   1.492 +	{
   1.493 +		inline static const T* GetOrigRet(ISourceHook *shptr)
   1.494 +		{
   1.495 +			return reinterpret_cast<const T*>(shptr->GetOrigRet());
   1.496 +		}
   1.497 +		inline static const T* GetOverrideRet(ISourceHook *shptr)
   1.498 +		{
   1.499 +			return reinterpret_cast<const T*>(shptr->GetOverrideRet());
   1.500 +		}
   1.501 +	};
   1.502 +
   1.503 +	template <class T> struct MacroRefHelpers<T&>
   1.504 +	{
   1.505 +		inline static T* GetOrigRet(ISourceHook *shptr)
   1.506 +		{
   1.507 +			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
   1.508 +			return &ref;
   1.509 +		}
   1.510 +		inline static T* GetOverrideRet(ISourceHook *shptr)
   1.511 +		{
   1.512 +			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
   1.513 +			return &ref;
   1.514 +		}
   1.515 +	};
   1.516 +}
   1.517 +
   1.518 +/************************************************************************/
   1.519 +/* High level interface                                                 */
   1.520 +/************************************************************************/
   1.521 +#define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   1.522 +#define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   1.523 +#define META_RESULT_ORIG_RET(type)			*SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
   1.524 +#define META_RESULT_OVERRIDE_RET(type)		*SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
   1.525 +#define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   1.526 +
   1.527 +#define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   1.528 +#define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   1.529 +#define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   1.530 +
   1.531 +// If a hook on a function which returns a reference does not want to specify a return value,
   1.532 +// it can use this macro.
   1.533 +//   ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
   1.534 +#define RETURN_META_NOREF(result, rettype)	do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
   1.535 +
   1.536 +// only call these from the hook handlers directly!
   1.537 +// :TODO: enforce it ?
   1.538 +
   1.539 +// Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   1.540 +// Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   1.541 +
   1.542 +// SourceHook::SetOverrideRet is defined later.
   1.543 +#define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   1.544 +	do { \
   1.545 +		SET_META_RESULT(result); \
   1.546 +		SH_GLOB_SHPTR->DoRecall(); \
   1.547 +		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   1.548 +		RETURN_META(MRES_SUPERCEDE); \
   1.549 +	} while (0)
   1.550 +
   1.551 +#define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   1.552 +	do { \
   1.553 +		SET_META_RESULT(result); \
   1.554 +		SH_GLOB_SHPTR->DoRecall(); \
   1.555 +		if ((result) >= MRES_OVERRIDE) \
   1.556 +		{ \
   1.557 +			/* meh, set the override result here because we don't get a chance to return */ \
   1.558 +			/* before continuing the hook loop through the recall */ \
   1.559 +			SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
   1.560 +		} \
   1.561 +		RETURN_META_VALUE(MRES_SUPERCEDE, \
   1.562 +			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   1.563 +	} while (0)
   1.564 +
   1.565 +// :TODO: thisptroffs in MNEWPARAMS ??
   1.566 +
   1.567 +#if SH_COMP == SH_COMP_MSVC
   1.568 +
   1.569 +#define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   1.570 +	union \
   1.571 +	{ \
   1.572 +		SH_MFHCls(hookname)::ECMFP mfp; \
   1.573 +		void *addr; \
   1.574 +	} u; \
   1.575 +	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   1.576 +	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   1.577 +		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   1.578 +
   1.579 +#elif SH_COMP == SH_COMP_GCC
   1.580 +
   1.581 +#define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   1.582 +	union \
   1.583 +	{ \
   1.584 +		SH_MFHCls(hookname)::ECMFP mfp; \
   1.585 +		struct \
   1.586 +		{ \
   1.587 +			void *addr; \
   1.588 +			intptr_t adjustor; \
   1.589 +		} s; \
   1.590 +	} u; \
   1.591 +	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   1.592 +	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   1.593 +		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   1.594 +	u.s.adjustor = 0;
   1.595 +
   1.596 +#endif
   1.597 +
   1.598 +#define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   1.599 +	do { \
   1.600 +		SET_META_RESULT(result); \
   1.601 +		SH_GLOB_SHPTR->DoRecall(); \
   1.602 +		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   1.603 +		(thisptr->*(u.mfp)) newparams; \
   1.604 +		RETURN_META(MRES_SUPERCEDE); \
   1.605 +	} while (0)
   1.606 +
   1.607 +#define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   1.608 +	do { \
   1.609 +		SET_META_RESULT(result); \
   1.610 +		SH_GLOB_SHPTR->DoRecall(); \
   1.611 +		if ((result) >= MRES_OVERRIDE) \
   1.612 +		{ \
   1.613 +			/* see RETURN_META_VALUE_NEWPARAMS */ \
   1.614 +			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   1.615 +		} \
   1.616 +		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   1.617 +		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   1.618 +	} while (0)
   1.619 +
   1.620 +/**
   1.621 +*	@brief Get/generate callclass for an interface pointer
   1.622 +*
   1.623 +*	@param ifaceptr The interface pointer
   1.624 +*/
   1.625 +template<class ifacetype>
   1.626 +inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   1.627 +{
   1.628 +	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   1.629 +		shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
   1.630 +}
   1.631 +
   1.632 +template<class ifacetype>
   1.633 +inline SourceHook::CallClass<ifacetype> *SH_GET_MCALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr, int ifacesize)
   1.634 +{
   1.635 +	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   1.636 +		shptr->GetCallClass(reinterpret_cast<void*>(ptr), ifacesize));
   1.637 +}
   1.638 +
   1.639 +template<class ifacetype>
   1.640 +inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   1.641 +{
   1.642 +	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   1.643 +}
   1.644 +
   1.645 +#define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   1.646 +	do { \
   1.647 +		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   1.648 +		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   1.649 +		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   1.650 +		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   1.651 +	} while (0)
   1.652 +
   1.653 +#define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   1.654 +#define SH_GET_MCALLCLASS(ptr, size) SH_GET_MCALLCLASS_R(SH_GLOB_SHPTR, reinterpret_cast<SourceHook::EmptyClass*>(ptr), size)
   1.655 +#define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   1.656 +
   1.657 +#define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   1.658 +	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   1.659 +	post, handler)
   1.660 +#define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   1.661 +	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   1.662 +#define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   1.663 +	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   1.664 +
   1.665 +#define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   1.666 +	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   1.667 +	post, handler)
   1.668 +#define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   1.669 +	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   1.670 +#define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   1.671 +	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   1.672 +
   1.673 +
   1.674 +#define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   1.675 +	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   1.676 +#define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   1.677 +	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   1.678 +#define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   1.679 +	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   1.680 +
   1.681 +#define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   1.682 +	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   1.683 +#define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   1.684 +	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   1.685 +#define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   1.686 +	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   1.687 +
   1.688 +
   1.689 +#define SH_NOATTRIB
   1.690 +
   1.691 +
   1.692 +
   1.693 +
   1.694 +#if SH_COMP == SH_COMP_MSVC
   1.695 +# define SH_SETUP_MFP(mfp) \
   1.696 +	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   1.697 +#elif SH_COMP == SH_COMP_GCC
   1.698 +# define SH_SETUP_MFP(mfp) \
   1.699 +	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   1.700 +	reinterpret_cast<void**>(&mfp)[1] = 0;
   1.701 +#else
   1.702 +# error Not supported yet.
   1.703 +#endif
   1.704 +
   1.705 +//////////////////////////////////////////////////////////////////////////
   1.706 +#define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   1.707 +#define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   1.708 +
   1.709 +#define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   1.710 +	SH_FHCls(ifacetype,ifacefunc,overload)() \
   1.711 +	{ \
   1.712 +		GetFuncInfo(funcptr, ms_MFI); \
   1.713 +	} \
   1.714 +	\
   1.715 +	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   1.716 +	{ \
   1.717 +		using namespace ::SourceHook; \
   1.718 +		GetFuncInfo(funcptr, ms_MFI); \
   1.719 +		/* Verify interface version */ \
   1.720 +		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   1.721 +			return 1; \
   1.722 +		\
   1.723 +		if (action == HA_GetInfo) \
   1.724 +		{ \
   1.725 +			param->SetVersion(SH_HOOKMAN_VERSION); \
   1.726 +			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   1.727 +				reinterpret_cast<const char*>(&ms_Proto)); \
   1.728 +			\
   1.729 +			MemFuncInfo mfi = {true, -1, 0, 0}; \
   1.730 +			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   1.731 +			param->SetHookfuncVfnptr( \
   1.732 +				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   1.733 +			return 0; \
   1.734 +		} \
   1.735 +		else if (action == HA_Register) \
   1.736 +		{ \
   1.737 +			ms_HI = param; \
   1.738 +			return 0; \
   1.739 +		} \
   1.740 +		else if (action == HA_Unregister) \
   1.741 +		{ \
   1.742 +			ms_HI = NULL; \
   1.743 +			return 0; \
   1.744 +		} \
   1.745 +		else \
   1.746 +			return 1; \
   1.747 +	}
   1.748 +
   1.749 +// It has to be possible to use the macros in namespaces
   1.750 +// -> So we need to access and extend the global SourceHook namespace
   1.751 +// We use a namespace alias for this
   1.752 +#define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   1.753 +	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   1.754 +	{ \
   1.755 +		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   1.756 +		static ::SourceHook::MemFuncInfo ms_MFI; \
   1.757 +		static ::SourceHook::IHookManagerInfo *ms_HI; \
   1.758 +		static ::SourceHook::ProtoInfo ms_Proto; \
   1.759 +		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   1.760 +
   1.761 +#define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   1.762 +	}; \
   1.763 +	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   1.764 +	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   1.765 +	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   1.766 +	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   1.767 +		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   1.768 +	{ \
   1.769 +		using namespace ::SourceHook; \
   1.770 +		MemFuncInfo mfi = {true, -1, 0, 0}; \
   1.771 +		GetFuncInfo(funcptr, mfi); \
   1.772 +		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   1.773 +			return false; /* No non-virtual functions / virtual inheritance supported */ \
   1.774 +		\
   1.775 +		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   1.776 +			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   1.777 +			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   1.778 +	} \
   1.779 +	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   1.780 +		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   1.781 +	{ \
   1.782 +		using namespace ::SourceHook; \
   1.783 +		MemFuncInfo mfi = {true, -1, 0, 0}; \
   1.784 +		GetFuncInfo(funcptr, mfi); \
   1.785 +		if (mfi.thisptroffs < 0) \
   1.786 +			return false; /* No virtual inheritance supported */ \
   1.787 +		\
   1.788 +		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   1.789 +		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   1.790 +			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   1.791 +	} \
   1.792 +
   1.793 +#define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   1.794 +	struct SH_MFHCls(hookname) \
   1.795 +	{ \
   1.796 +		static SH_MFHCls(hookname) ms_Inst; \
   1.797 +		static ::SourceHook::MemFuncInfo ms_MFI; \
   1.798 +		static ::SourceHook::IHookManagerInfo *ms_HI; \
   1.799 +		static ::SourceHook::ProtoInfo ms_Proto; \
   1.800 +		\
   1.801 +		SH_MFHCls(hookname)() \
   1.802 +		{ \
   1.803 +			ms_MFI.isVirtual = true; \
   1.804 +			ms_MFI.thisptroffs = pthisptroffs; \
   1.805 +			ms_MFI.vtblindex = pvtblidx; \
   1.806 +			ms_MFI.vtbloffs = pvtbloffs; \
   1.807 +		} \
   1.808 +		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   1.809 +		{ \
   1.810 +			using namespace ::SourceHook; \
   1.811 +			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   1.812 +			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   1.813 +			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   1.814 +			/* Verify interface version */ \
   1.815 +			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   1.816 +				return 1; \
   1.817 +			\
   1.818 +			if (action == HA_GetInfo) \
   1.819 +			{ \
   1.820 +				param->SetVersion(SH_HOOKMAN_VERSION); \
   1.821 +				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   1.822 +					reinterpret_cast<const char*>(&ms_Proto)); \
   1.823 +				\
   1.824 +				MemFuncInfo mfi = {true, -1, 0, 0}; \
   1.825 +				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   1.826 +				param->SetHookfuncVfnptr( \
   1.827 +					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   1.828 +				return 0; \
   1.829 +			} \
   1.830 +			else if (action == HA_Register) \
   1.831 +			{ \
   1.832 +				ms_HI = param; \
   1.833 +				return 0; \
   1.834 +			} \
   1.835 +			else if (action == HA_Unregister) \
   1.836 +			{ \
   1.837 +				ms_HI = NULL; \
   1.838 +				return 0; \
   1.839 +			} \
   1.840 +			else \
   1.841 +				return 1; \
   1.842 +		}
   1.843 +
   1.844 +#define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   1.845 +	}; \
   1.846 +	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   1.847 +	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   1.848 +	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   1.849 +	bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   1.850 +		SH_MFHCls(hookname)::FD handler) \
   1.851 +	{ \
   1.852 +		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   1.853 +			SH_MFHCls(hookname)::HookManPubFunc, \
   1.854 +			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   1.855 +	} \
   1.856 +	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   1.857 +		SH_MFHCls(hookname)::FD handler) \
   1.858 +	{ \
   1.859 +		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   1.860 +		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   1.861 +			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   1.862 +	} \
   1.863 +
   1.864 +
   1.865 +#define SH_SETUPCALLS(rettype, paramtypes, params) \
   1.866 +	/* 1) Find the vfnptr */ \
   1.867 +	using namespace ::SourceHook; \
   1.868 +	void *ourvfnptr = reinterpret_cast<void*>( \
   1.869 +		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   1.870 +	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   1.871 +	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   1.872 +	\
   1.873 +	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   1.874 +	/* ... and the iface */ \
   1.875 +	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   1.876 +	if (!ifinfo) \
   1.877 +	{ \
   1.878 +		/* The iface info was not found. Redirect the call to the original function. */ \
   1.879 +		rettype (EmptyClass::*mfp)paramtypes; \
   1.880 +		SH_SETUP_MFP(mfp); \
   1.881 +		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.882 +	} \
   1.883 +	/* 2) Declare some vars and set it up */ \
   1.884 +	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   1.885 +	IHookList *prelist = ifinfo->GetPreHooks(); \
   1.886 +	IHookList *postlist = ifinfo->GetPostHooks(); \
   1.887 +	META_RES status = MRES_IGNORED; \
   1.888 +	META_RES prev_res; \
   1.889 +	META_RES cur_res; \
   1.890 +	typedef ReferenceCarrier<rettype>::type my_rettype; \
   1.891 +	my_rettype orig_ret; \
   1.892 +	my_rettype override_ret; \
   1.893 +	my_rettype plugin_ret; \
   1.894 +	void* ifptr; \
   1.895 +	my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
   1.896 +		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
   1.897 +
   1.898 +#define SH_CALL_HOOKS(post, params) \
   1.899 +	if (SH_GLOB_SHPTR->ShouldContinue()) \
   1.900 +	{ \
   1.901 +		prev_res = MRES_IGNORED; \
   1.902 +		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   1.903 +		{ \
   1.904 +			cur_res = MRES_IGNORED; \
   1.905 +			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   1.906 +			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   1.907 +			prev_res = cur_res; \
   1.908 +			if (cur_res > status) \
   1.909 +				status = cur_res; \
   1.910 +			if (cur_res >= MRES_OVERRIDE) \
   1.911 +				*pOverrideRet = plugin_ret; \
   1.912 +			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   1.913 +			{ \
   1.914 +				iter.SetToZero(); \
   1.915 +				break; \
   1.916 +			} \
   1.917 +		} \
   1.918 +	}
   1.919 +
   1.920 +#define SH_CALL_ORIG(rettype, paramtypes, params) \
   1.921 +	if (status != MRES_SUPERCEDE) \
   1.922 +	{ \
   1.923 +		rettype (EmptyClass::*mfp)paramtypes; \
   1.924 +		SH_SETUP_MFP(mfp); \
   1.925 +		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.926 +	} \
   1.927 +	else \
   1.928 +		orig_ret = override_ret; \
   1.929 +
   1.930 +#define SH_RETURN() \
   1.931 +	SH_GLOB_SHPTR->HookLoopEnd(); \
   1.932 +	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
   1.933 +
   1.934 +#define SH_HANDLEFUNC(paramtypes, params, rettype) \
   1.935 +	SH_SETUPCALLS(rettype, paramtypes, params) \
   1.936 +	SH_CALL_HOOKS(pre, params) \
   1.937 +	SH_CALL_ORIG(rettype, paramtypes, params) \
   1.938 +	SH_CALL_HOOKS(post, params) \
   1.939 +	SH_RETURN()
   1.940 +
   1.941 +//////////////////////////////////////////////////////////////////////////
   1.942 +#define SH_SETUPCALLS_void(paramtypes, params) \
   1.943 +	/* 1) Find the vfnptr */ \
   1.944 +	using namespace ::SourceHook; \
   1.945 +	void *ourvfnptr = reinterpret_cast<void*>( \
   1.946 +		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   1.947 +	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   1.948 +	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   1.949 +	\
   1.950 +	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   1.951 +	/* ... and the iface */ \
   1.952 +	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   1.953 +	if (!ifinfo) \
   1.954 +	{ \
   1.955 +		/* The iface info was not found. Redirect the call to the original function. */ \
   1.956 +		void (EmptyClass::*mfp)paramtypes; \
   1.957 +		SH_SETUP_MFP(mfp); \
   1.958 +		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.959 +		return; \
   1.960 +	} \
   1.961 +	/* 2) Declare some vars and set it up */ \
   1.962 +	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   1.963 +	IHookList *prelist = ifinfo->GetPreHooks(); \
   1.964 +	IHookList *postlist = ifinfo->GetPostHooks(); \
   1.965 +	META_RES status = MRES_IGNORED; \
   1.966 +	META_RES prev_res; \
   1.967 +	META_RES cur_res; \
   1.968 +	void* ifptr; \
   1.969 +	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
   1.970 +
   1.971 +#define SH_CALL_HOOKS_void(post, params) \
   1.972 +	if (SH_GLOB_SHPTR->ShouldContinue()) \
   1.973 +	{ \
   1.974 +		prev_res = MRES_IGNORED; \
   1.975 +		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   1.976 +		{ \
   1.977 +			cur_res = MRES_IGNORED; \
   1.978 +			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   1.979 +			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   1.980 +			prev_res = cur_res; \
   1.981 +			if (cur_res > status) \
   1.982 +				status = cur_res; \
   1.983 +			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   1.984 +			{ \
   1.985 +				iter.SetToZero(); \
   1.986 +				break; \
   1.987 +			} \
   1.988 +		} \
   1.989 +	}
   1.990 +
   1.991 +#define SH_CALL_ORIG_void(paramtypes, params) \
   1.992 +	if (status != MRES_SUPERCEDE) \
   1.993 +	{ \
   1.994 +		void (EmptyClass::*mfp)paramtypes; \
   1.995 +		SH_SETUP_MFP(mfp); \
   1.996 +		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.997 +	}
   1.998 +
   1.999 +#define SH_RETURN_void() \
  1.1000 +	SH_GLOB_SHPTR->HookLoopEnd();
  1.1001 +
  1.1002 +#define SH_HANDLEFUNC_void(paramtypes, params) \
  1.1003 +	SH_SETUPCALLS_void(paramtypes, params) \
  1.1004 +	SH_CALL_HOOKS_void(pre, params) \
  1.1005 +	SH_CALL_ORIG_void(paramtypes, params) \
  1.1006 +	SH_CALL_HOOKS_void(post, params) \
  1.1007 +	SH_RETURN_void()
  1.1008 +
  1.1009 +
  1.1010 +// Special vafmt handlers
  1.1011 +// :TODO: what
  1.1012 +#define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
  1.1013 +	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
  1.1014 +	SH_CALL_HOOKS(pre, params_plug) \
  1.1015 +	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
  1.1016 +	SH_CALL_HOOKS(post, params_plug) \
  1.1017 +	SH_RETURN()
  1.1018 +
  1.1019 +#define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
  1.1020 +	SH_SETUPCALLS_void(paramtypes, params_orig) \
  1.1021 +	SH_CALL_HOOKS_void(pre, params_plug) \
  1.1022 +	SH_CALL_ORIG_void(paramtypes, params_orig) \
  1.1023 +	SH_CALL_HOOKS_void(post, params_plug) \
  1.1024 +	SH_RETURN_void()
  1.1025 +
  1.1026 +//////////////////////////////////////////////////////////////////////////
  1.1027 +
  1.1028 +// :FIXME:
  1.1029 +//  sizeof on references returns the size of the datatype, NOT the pointer size or something
  1.1030 +//  -> one should probably flag references in __SourceHook_ParamSizes_* !
  1.1031 +//		or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
  1.1032 +
  1.1033 +@[$1,0,$a:
  1.1034 +// ********* Support for $1 arguments *********
  1.1035 +#define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1.1036 +	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1.1037 +		(&ifacetype::ifacefunc))) \
  1.1038 +		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1.1039 +		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1.1040 +		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1.1041 +	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1.1042 +		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1.1043 +	\
  1.1044 +	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1.1045 +	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1.1046 +		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1.1047 +
  1.1048 +#define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1.1049 +	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1.1050 +		(&ifacetype::ifacefunc))) \
  1.1051 +		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1.1052 +		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1.1053 +		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1.1054 +	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1.1055 +		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1.1056 +	\
  1.1057 +	\
  1.1058 +	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1.1059 +	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1.1060 +		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1.1061 +
  1.1062 +#define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1.1063 +	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1.1064 +		(&ifacetype::ifacefunc))) \
  1.1065 +		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
  1.1066 +		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1.1067 +		{ \
  1.1068 +			char buf[::SourceHook::STRBUF_LEN]; \
  1.1069 +			va_list ap; \
  1.1070 +			va_start(ap, fmt); \
  1.1071 +			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1.1072 +			va_end(ap); \
  1.1073 +			SH_HANDLEFUNC_vafmt((@[$2,1,$1|, :param$2@]@[$1!=0:, @]...), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]"%s", buf), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]buf), rettype); \
  1.1074 +		} \
  1.1075 +	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1.1076 +		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1.1077 +	\
  1.1078 +	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1.1079 +	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1.1080 +		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1.1081 +
  1.1082 +#define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1.1083 +	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1.1084 +		(&ifacetype::ifacefunc))) \
  1.1085 +		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1.1086 +		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1.1087 +		{ \
  1.1088 +			char buf[::SourceHook::STRBUF_LEN]; \
  1.1089 +			va_list ap; \
  1.1090 +			va_start(ap, fmt); \
  1.1091 +			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1.1092 +			va_end(ap); \
  1.1093 +			SH_HANDLEFUNC_void_vafmt((@[$2,1,$1|, :param$2@]@[$1!=0:, @]...), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]"%s", buf), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]buf)); \
  1.1094 +		} \
  1.1095 +	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1.1096 +		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1.1097 +	\
  1.1098 +	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1.1099 +	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1.1100 +		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1.1101 +
  1.1102 +#define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1.1103 +	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1.1104 +		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1.1105 +		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1.1106 +		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1.1107 +		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1.1108 +		typedef rettype RetType; \
  1.1109 +	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1.1110 +	\
  1.1111 +	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1.1112 +	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
  1.1113 +		$1, __SourceHook_ParamSizesM_##hookname); \
  1.1114 +
  1.1115 +#define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1.1116 +	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1.1117 +		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1.1118 +		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1.1119 +		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1.1120 +		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1.1121 +	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1.1122 +	\
  1.1123 +	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1.1124 +	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
  1.1125 +		$1, __SourceHook_ParamSizesM_##hookname); \
  1.1126 +
  1.1127 +@]
  1.1128 +
  1.1129 +
  1.1130 +//////////////////////////////////////////////////////////////////////////
  1.1131 +// SH_CALL
  1.1132 +
  1.1133 +#if SH_COMP == SH_COMP_MSVC
  1.1134 +
  1.1135 +# define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1.1136 +{ \
  1.1137 +	using namespace ::SourceHook; \
  1.1138 +	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1.1139 +	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1.1140 +	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1.1141 +	if (!origfunc) \
  1.1142 +		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1.1143 +	\
  1.1144 +	/* It's hooked. Call the original function. */ \
  1.1145 +	union \
  1.1146 +	{ \
  1.1147 +		RetType(EmptyClass::*mfpnew)prms; \
  1.1148 +		void *addr; \
  1.1149 +	} u; \
  1.1150 +	u.addr = origfunc; \
  1.1151 +	\
  1.1152 +	void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
  1.1153 +	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1.1154 +}
  1.1155 +
  1.1156 +# define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1.1157 +{ \
  1.1158 +	using namespace ::SourceHook; \
  1.1159 +	char *adjustedthisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()) + m_ThisPtrOffs; \
  1.1160 +	union \
  1.1161 +	{ \
  1.1162 +		RetType(EmptyClass::*mfpnew)prms; \
  1.1163 +		void *addr; \
  1.1164 +	} u; \
  1.1165 +	u.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1.1166 +	if (!u.addr) \
  1.1167 +		u.addr = (*reinterpret_cast<void***>(adjustedthisptr + m_VtblOffs))[m_VtblIdx]; \
  1.1168 +	\
  1.1169 +	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1.1170 +}
  1.1171 +
  1.1172 +#elif SH_COMP == SH_COMP_GCC
  1.1173 +
  1.1174 +# define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1.1175 +{ \
  1.1176 +	using namespace ::SourceHook; \
  1.1177 +	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1.1178 +	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1.1179 +	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1.1180 +	if (!origfunc) \
  1.1181 +		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1.1182 +	\
  1.1183 +	/* It's hooked. Call the original function. */ \
  1.1184 +	union \
  1.1185 +	{ \
  1.1186 +		RetType(EmptyClass::*mfpnew)prms; \
  1.1187 +		struct \
  1.1188 +		{ \
  1.1189 +			void *addr; \
  1.1190 +			intptr_t adjustor; \
  1.1191 +		} s; \
  1.1192 +	} u; \
  1.1193 +	u.s.addr = origfunc; \
  1.1194 +	u.s.adjustor = mfi.thisptroffs; \
  1.1195 +	\
  1.1196 +	return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
  1.1197 +}
  1.1198 +
  1.1199 +# define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1.1200 +{ \
  1.1201 +	using namespace ::SourceHook; \
  1.1202 +	char *thisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()); \
  1.1203 +	union \
  1.1204 +	{ \
  1.1205 +		RetType(EmptyClass::*mfpnew)prms; \
  1.1206 +		struct { \
  1.1207 +			void *addr; \
  1.1208 +			intptr_t adjustor; \
  1.1209 +		} s; \
  1.1210 +	} u; \
  1.1211 +	u.s.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1.1212 +	if (!u.s.addr) \
  1.1213 +		u.s.addr = (*reinterpret_cast<void***>(thisptr + m_ThisPtrOffs + m_VtblOffs))[m_VtblIdx]; \
  1.1214 +	\
  1.1215 +	u.s.adjustor = m_ThisPtrOffs; \
  1.1216 +	return (reinterpret_cast<EmptyClass*>(thisptr)->*u.mfpnew)call; \
  1.1217 +}
  1.1218 +
  1.1219 +#endif
  1.1220 +
  1.1221 +namespace SourceHook
  1.1222 +{
  1.1223 +@[$1,0,$a:
  1.1224 +	// Support for $1 arguments
  1.1225 +	template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1.1226 +	{
  1.1227 +	   CCType *m_CC;
  1.1228 +	   MFPType m_MFP;
  1.1229 +	public:
  1.1230 +	   ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
  1.1231 +	
  1.1232 +	   RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1.1233 +	      SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1.1234 +	         
  1.1235 +	   @[$2,$1+1,$a:
  1.1236 +	   template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1.1237 +	      SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1.1238 +	   @]
  1.1239 +	};
  1.1240 +
  1.1241 +	template <class RetType@[$2,1,$1:, class Param$2@]> class MExecutableClass$1
  1.1242 +	{
  1.1243 +		ManualCallClass *m_CC;
  1.1244 +		int m_ThisPtrOffs;
  1.1245 +		int m_VtblIdx;
  1.1246 +		int m_VtblOffs;
  1.1247 +	public:
  1.1248 +		MExecutableClass$1(ManualCallClass *cc, int vtbloffs, int vtblidx, int thisptroffs) : m_CC(cc),
  1.1249 +			m_ThisPtrOffs(thisptroffs), m_VtblIdx(vtblidx), m_VtblOffs(vtbloffs) { }
  1.1250 +
  1.1251 +		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1.1252 +			SH_MAKE_MEXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1.1253 +
  1.1254 +		@[$2,$1+1,$a:
  1.1255 +		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1.1256 +			SH_MAKE_MEXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1.1257 +		@]
  1.1258 +	};
  1.1259 +@]
  1.1260 +
  1.1261 +}
  1.1262 +
  1.1263 +// SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1.1264 +// That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1.1265 +// The only purpose of the mfp2 parameter is to extract the return type
  1.1266 +
  1.1267 +@[$1,0,$a:
  1.1268 +// Support for $1 arguments
  1.1269 +template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1.1270 +SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1.1271 +SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
  1.1272 +{
  1.1273 +	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1.1274 +}
  1.1275 +
  1.1276 +template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1.1277 +SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1.1278 +SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
  1.1279 +{
  1.1280 +	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1.1281 +}
  1.1282 +
  1.1283 +template <class X, class RetType@[$2,1,$1:, class Param$2@]>
  1.1284 +SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>
  1.1285 +SH_MCALL2(SourceHook::ManualCallClass *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
  1.1286 +{
  1.1287 +	return SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>(ptr, vtbloffs, vtblidx, thisptroffs);
  1.1288 +}
  1.1289 +@]
  1.1290 +
  1.1291 +#if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1.1292 +// GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1.1293 +
  1.1294 +@[$1,0,$a:
  1.1295 +// Support for $1 arguments
  1.1296 +template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1.1297 +SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1.1298 +SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
  1.1299 +{
  1.1300 +	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1.1301 +}
  1.1302 +
  1.1303 +template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1.1304 +SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1.1305 +SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
  1.1306 +{
  1.1307 +	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1.1308 +}
  1.1309 +
  1.1310 +@]
  1.1311 +
  1.1312 +#endif
  1.1313 +
  1.1314 +#define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
  1.1315 +#define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1.1316 +	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1.1317 +
  1.1318 +#undef SH_MAKE_EXECUTABLECLASS_OB
  1.1319 +
  1.1320 +//////////////////////////////////////////////////////////////////////////
  1.1321 +// SetOverrideRet and RecallGetIface for recalls
  1.1322 +// These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1.1323 +// The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1.1324 +// (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1.1325 +
  1.1326 +namespace SourceHook
  1.1327 +{
  1.1328 +	template <class RetType>
  1.1329 +	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1.1330 +	{
  1.1331 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1.1332 +	}
  1.1333 +
  1.1334 +	// SetOverrideResult used to be implemented like this:
  1.1335 +	//  SetOverrideResult(shptr, memfuncptr, return);
  1.1336 +	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1.1337 +	//  (it thought it was ambigous - the ref and non-ref type)
  1.1338 +	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1.1339 +	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1.1340 +	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1.1341 +
  1.1342 +	template <class T> struct OverrideFunctor
  1.1343 +	{
  1.1344 +		void operator()(ISourceHook *shptr, T res)
  1.1345 +		{
  1.1346 +			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1.1347 +		}
  1.1348 +	};
  1.1349 +	template <class T> struct OverrideFunctor<T&>
  1.1350 +	{
  1.1351 +		void operator()(ISourceHook *shptr, T &res)
  1.1352 +		{
  1.1353 +			// overrideretptr points to ReferenceCarrier<T&>
  1.1354 +			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1.1355 +		}
  1.1356 +	};
  1.1357 +@[$1,0,$a:
  1.1358 +	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1.1359 +	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1.1360 +	{
  1.1361 +		return OverrideFunctor<RetType>();
  1.1362 +	}
  1.1363 +
  1.1364 +	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1.1365 +	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1.1366 +	{
  1.1367 +		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1.1368 +	}
  1.1369 +@]
  1.1370 +}
  1.1371 +
  1.1372 +#endif
  1.1373 +	// The pope is dead. -> :(