sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 07 22:35:28 2007 +0000 (2007-10-07)
changeset 382 8b06ecc6e76d
parent 333 3813f1ac0cfe
child 384 264cab8ff2cc
permissions -rwxr-xr-x
Something :O
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2007 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 #define SH_IFACE_VERSION 4
    25 
    26 // Impl versions:
    27 // ???
    28 // 4 - addition of hook ids and vp hooks (with them, AddHookNew and RemoveHookNew)
    29 //     This is not a SH_IFACE_VERSION change so that old plugins continue working!
    30 // 5 - addition of direct vp hooks (new hook mode; from now on AddHookNew checks for
    31 //	   invalid hookmode -> impl version won't have to change because of things like this)
    32 #define SH_IMPL_VERSION 5
    33 
    34 // Hookman version:
    35 //  1 - Support for recalls, performance optimisations
    36 #define SH_HOOKMAN_VERSION 1
    37 
    38 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
    39 // It's used in various macros
    40 #ifndef SH_GLOB_SHPTR
    41 #define SH_GLOB_SHPTR g_SHPtr
    42 #endif
    43 
    44 // Used to identify the current plugin
    45 #ifndef SH_GLOB_PLUGPTR
    46 #define SH_GLOB_PLUGPTR g_PLID
    47 #endif
    48 
    49 #ifdef SH_DEBUG
    50 # include <stdio.h>
    51 # include <stdlib.h>
    52 
    53 # define SH_ASSERT(x, info) \
    54 	do { \
    55 		if (!(x)) \
    56 		{ \
    57 			printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
    58 			printf info; \
    59 			putchar('\n'); \
    60 			abort(); \
    61 		} \
    62 	} while(0)
    63 
    64 #else
    65 # define SH_ASSERT(x, info)
    66 #endif
    67 
    68 // System
    69 #define SH_SYS_WIN32 1
    70 #define SH_SYS_LINUX 2
    71 
    72 #ifdef _WIN32
    73 # define SH_SYS SH_SYS_WIN32
    74 #elif defined __linux__
    75 # define SH_SYS SH_SYS_LINUX
    76 #else
    77 # error Unsupported system
    78 #endif
    79 
    80 // Compiler
    81 #define SH_COMP_GCC 1
    82 #define SH_COMP_MSVC 2
    83 
    84 #ifdef _MSC_VER
    85 # define SH_COMP SH_COMP_MSVC
    86 #elif defined __GNUC__
    87 # define SH_COMP SH_COMP_GCC
    88 #else
    89 # error Unsupported compiler
    90 #endif
    91 
    92 #if SH_COMP==SH_COMP_MSVC
    93 # define vsnprintf _vsnprintf
    94 #endif
    95 
    96 #if SH_SYS != SH_SYS_WIN32
    97 # include <unistd.h>
    98 #endif
    99 
   100 #define SH_PTRSIZE sizeof(void*)
   101 
   102 #include "FastDelegate.h"
   103 #include "sh_memfuncinfo.h"
   104 
   105 // Good old metamod!
   106 
   107 // Flags returned by a plugin's api function.
   108 // NOTE: order is crucial, as greater/less comparisons are made.
   109 enum META_RES
   110 {
   111 	MRES_IGNORED=0,		// plugin didn't take any action
   112 	MRES_HANDLED,		// plugin did something, but real function should still be called
   113 	MRES_OVERRIDE,		// call real function, but use my return value
   114 	MRES_SUPERCEDE		// skip real function; use my return value
   115 };
   116 
   117 
   118 namespace SourceHook
   119 {
   120 	/**
   121 	*	@brief	Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
   122 	*/
   123 	const int STRBUF_LEN=4096;
   124 
   125 	/**
   126 	*	@brief	An empty class. No inheritance used. Used for original-function-call hacks
   127 	*/
   128 	class EmptyClass
   129 	{
   130 	};
   131 
   132 	/**
   133 	*	@brief Implicit cast.
   134 	*/
   135 	template <class In, class Out>
   136 		inline Out implicit_cast(In input)
   137 		{
   138 			return input;
   139 		}
   140 
   141 	/**
   142 	*	@brief A plugin typedef
   143 	*
   144 	*	SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
   145 	*	plugin has a unique identifier, everything is ok.
   146 	*	It should also be possible to set it to 0.
   147 	*/
   148 	typedef int Plugin;
   149 
   150 	struct ProtoInfo
   151 	{
   152 		ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
   153 		{
   154 		}
   155 		int beginningNull;		//!< To distinguish from old protos (which never begin with 0)
   156 		int retTypeSize;		//!< 0 if void
   157 		int numOfParams;		//!< number of parameters
   158 		const int *params;		//!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
   159 	};
   160 
   161 	/**
   162 	*	@brief Specifies the actions for hook managers
   163 	*/
   164 	enum HookManagerAction
   165 	{
   166 		HA_GetInfo = 0,			//!< Store info about the hook manager
   167 		HA_Register,			//!< Save the IHookManagerInfo pointer for future reference
   168 		HA_Unregister			//!< Clear the saved pointer
   169 	};
   170 
   171 	struct IHookManagerInfo;
   172 
   173 	/**
   174 	*	@brief Pointer to hook manager interface function
   175 	*
   176 	*	A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
   177 	*
   178 	*   @param ha What the hook manager should do
   179 	*	@param hi A pointer to IHookManagerInfo
   180 	*/
   181 	typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
   182 
   183 	class ISHDelegate
   184 	{
   185 	public:
   186 		virtual void DeleteThis() = 0;				// Ugly, I know
   187 		virtual bool IsEqual(ISHDelegate *other) = 0;
   188 	};
   189 
   190 	template <class T> class CSHDelegate : public ISHDelegate
   191 	{
   192 		T m_Deleg;
   193 	public:
   194 		CSHDelegate(T deleg) : m_Deleg(deleg)
   195 		{
   196 		}
   197 
   198 		CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
   199 		{
   200 		}
   201 
   202 		void DeleteThis()
   203 		{
   204 			delete this;
   205 		}
   206 
   207 		bool IsEqual(ISHDelegate *other)
   208 		{
   209 			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
   210 		}
   211 
   212 		T &GetDeleg()
   213 		{
   214 			return m_Deleg;
   215 		}
   216 	};
   217 
   218 	struct IHookList
   219 	{
   220 		struct IIter
   221 		{
   222 			virtual bool End() = 0;
   223 			virtual void Next() = 0;
   224 			virtual ISHDelegate *Handler() = 0;
   225 			virtual int ThisPtrOffs() = 0;
   226 		};
   227 		virtual IIter *GetIter() = 0;
   228 		virtual void ReleaseIter(IIter *pIter) = 0;
   229 	};
   230 
   231 	struct IIface
   232 	{
   233 		virtual void *GetPtr() = 0;
   234 		virtual IHookList *GetPreHooks() = 0;
   235 		virtual IHookList *GetPostHooks() = 0;
   236 	};
   237 
   238 	struct IVfnPtr
   239 	{
   240 		virtual void *GetVfnPtr() = 0;
   241 		virtual void *GetOrigEntry() = 0;
   242 
   243 		virtual IIface *FindIface(void *ptr) = 0;
   244 	};
   245 
   246 	struct IHookManagerInfo
   247 	{
   248 		virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
   249 
   250 		virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
   251 		virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
   252 
   253 		// Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
   254 		// If the hookman doesn't set this, it defaults 0
   255 		// SourceHook prefers hookmans with higher version numbers
   256 		virtual void SetVersion(int version) = 0;
   257 	};
   258 
   259 	class AutoHookIter
   260 	{
   261 		IHookList *m_pList;
   262 		IHookList::IIter *m_pIter;
   263 	public:
   264 		AutoHookIter(IHookList *pList)
   265 			: m_pList(pList), m_pIter(pList->GetIter())
   266 		{
   267 		}
   268 
   269 		~AutoHookIter()
   270 		{
   271 			if (m_pList)
   272 				m_pList->ReleaseIter(m_pIter);
   273 		}
   274 
   275 		bool End()
   276 		{
   277 			return m_pIter->End();
   278 		}
   279 
   280 		void Next()
   281 		{
   282 			m_pIter->Next();
   283 		}
   284 
   285 		ISHDelegate *Handler()
   286 		{
   287 			return m_pIter->Handler();
   288 		}
   289 
   290 		int ThisPtrOffs()
   291 		{
   292 			return m_pIter->ThisPtrOffs();
   293 		}
   294 
   295 		void SetToZero()
   296 		{
   297 			m_pList = 0;
   298 		}
   299 	};
   300 
   301 	template<class B> struct DeprecatedCallClass
   302 	{
   303 		virtual B *GetThisPtr() = 0;
   304 		virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
   305 	};
   306 
   307 	// 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
   308 	// - Today is an important day.
   309 	// I'm adding support for functions which return references.
   310 
   311 	// How it works:
   312 	//  SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
   313 	//  override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
   314 	//  this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
   315 	//  which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
   316 	//  special cases were needed for getoverrideret / getorigret; these are implemented through the
   317 	//  SourceHook::MacroRefHelpers structs.
   318 	//  Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
   319 	template <class T> struct ReferenceCarrier
   320 	{
   321 		typedef T type;
   322 	};
   323 
   324 	template <class T> struct ReferenceCarrier<T&>
   325 	{
   326 		class type
   327 		{
   328 			T *m_StoredRef;
   329 		public:
   330 			type() : m_StoredRef(NULL)
   331 			{
   332 			}
   333 			type(T& ref) : m_StoredRef(&ref)
   334 			{
   335 			}
   336 
   337 			T& operator= (T& ref)
   338 			{
   339 				m_StoredRef = &ref;
   340 				return ref;
   341 			}
   342 
   343 			operator T& () const
   344 			{
   345 				return *m_StoredRef;
   346 			}
   347 		};
   348 	};
   349 
   350 	/**
   351 	*	@brief The main SourceHook interface
   352 	*/
   353 	class ISourceHook
   354 	{
   355 	public:
   356 		/**
   357 		*	@brief Return interface version
   358 		*/
   359 		virtual int GetIfaceVersion() = 0;
   360 
   361 		/**
   362 		*	@brief Return implementation version
   363 		*/
   364 		virtual int GetImplVersion() = 0;
   365 
   366 		/**
   367 		*	@brief Add a hook.
   368 		*
   369 		*	@return True if the function succeeded, false otherwise
   370 		*
   371 		*	@param plug The unique identifier of the plugin that calls this function
   372 		*	@param iface The interface pointer
   373 		*	@param ifacesize The size of the class iface points to
   374 		*	@param myHookMan A hook manager function that should be capable of handling the function
   375 		*	@param handler A pointer to a FastDelegate containing the hook handler
   376 		*	@param post Set to true if you want a post handler
   377 		*/
   378 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   379 			ISHDelegate *handler, bool post) = 0;
   380 
   381 		/**
   382 		*	@brief Removes a hook.
   383 		*
   384 		*	@return True if the function succeeded, false otherwise
   385 		*
   386 		*	@param plug The unique identifier of the plugin that calls this function
   387 		*	@param iface The interface pointer
   388 		*	@param myHookMan A hook manager function that should be capable of handling the function
   389 		*	@param handler A pointer to a FastDelegate containing the hook handler
   390 		*	@param post Set to true if you want a post handler
   391 		*/
   392 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   393 			ISHDelegate *handler, bool post) = 0;
   394 
   395 		/**
   396 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   397 		*
   398 		*	@param plug The unique identifier of the plugin in question
   399 		*/
   400 		virtual bool IsPluginInUse(Plugin plug) = 0;
   401 
   402 		/**
   403 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   404 		*
   405 		*	@param iface The interface pointer
   406 		*	@param size Size of the class instance
   407 		*/
   408 		virtual DeprecatedCallClass<void> *GetCallClass(void *iface, size_t size) = 0;
   409 
   410 		/**
   411 		*	@brief Release a callclass
   412 		*
   413 		*	@param ptr Pointer to the callclass
   414 		*/
   415 		virtual void ReleaseCallClass(DeprecatedCallClass<void> *ptr) = 0;
   416 
   417 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   418 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   419 															//!<  previously calledhandler
   420 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   421 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   422 															//!<  If not in post function, undefined
   423 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   424 															//!<  If none is specified, NULL
   425 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   426 		//////////////////////////////////////////////////////////////////////////
   427 		// For hook managers
   428 		virtual void HookLoopBegin(IIface *pIface) = 0;			//!< Should be called when a hook loop begins
   429 		virtual void HookLoopEnd() = 0;							//!< Should be called when a hook loop exits
   430 		virtual void SetCurResPtr(META_RES *mres) = 0;			//!< Sets pointer to the current meta result
   431 		virtual void SetPrevResPtr(META_RES *mres) = 0;			//!< Sets pointer to previous meta result
   432 		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
   433 		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
   434 		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
   435 		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
   436 		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
   437 
   438 		/**
   439 		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
   440 		*
   441 		*	@param plug The owner of the hook manager
   442 		*   @param pubFunc The hook manager's info function
   443 		*/
   444 		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
   445 
   446 		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   447 		/*
   448 			HOW RECALLS WORK:
   449 			
   450 			The problem:
   451 				Users want the ability to change parameters of the called function
   452 				from inside their handler.
   453 			The solution:
   454 				1) Mark as "recall"
   455 				2) Recall the function
   456 				3) => SH's hook func gets called:
   457 				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   458 				  5) does all iteration and returns normally
   459 		        6) The user's handler returns immediately
   460 				7) The hook func returns immediately as well
   461 
   462 				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   463 				ended with. The last handler (doing the recall) is also able to specify its own
   464 				META_RES.
   465 		*/
   466 
   467 		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
   468 
   469 		/**
   470 		*	@brief Set up the hook loop. Equivalent to calling:
   471 		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
   472 		*
   473 		*	@param statusPtr		pointer to status variable
   474 		*	@param prevResPtr		pointer to previous result variable
   475 		*	@param curResPtr		pointer to current result variable
   476 		*	@param ifacePtrPtr		pointer to interface this pointer variable
   477 		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   478 		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   479 		*	
   480 		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   481 		*		when the hook func is being called as part of a recall
   482 		*/
   483 		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   484 			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
   485 
   486 		/**
   487 		*	@brief Modes for the new AddHook
   488 		*/
   489 		enum AddHookMode
   490 		{
   491 			Hook_Normal,
   492 			Hook_VP,
   493 			Hook_DVP
   494 		}; 
   495 
   496 		/**
   497 		*	@brief Add a (VP) hook.
   498 		*
   499 		*	@return non-zero hook id on success, 0 otherwise
   500 		*
   501 		*	@param plug The unique identifier of the plugin that calls this function
   502 		*	@param mode	Can be either Hook_Normal or Hook_VP (vtable-wide hook)
   503 		*	@param iface The interface pointer
   504 		*	@param ifacesize The size of the class iface points to
   505 		*	@param myHookMan A hook manager function that should be capable of handling the function
   506 		*	@param handler A pointer to a FastDelegate containing the hook handler
   507 		*	@param post Set to true if you want a post handler
   508 		*/
   509 		virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   510 			ISHDelegate *handler, bool post) = 0;
   511 
   512 		/**
   513 		*	@brief Remove a VP hook by ID.
   514 		*
   515 		*	@return true on success, false otherwise
   516 		*
   517 		*	@param plug The unique identifier of the plugin that calls this function
   518 		*	@param hookid The hook id (returned by AddHookNew)
   519 		*/
   520 		virtual bool RemoveHookByID(Plugin plug, int hookid) = 0;
   521 
   522 		/**
   523 		*	@brief Makes sure that hooks are going to be ignored on the next call of vfnptr
   524 		*
   525 		*	@param plug The unique identifier of the plugin that calls this function
   526 		*	@param vfnptr The virtual function pointer of the function in question
   527 		*/
   528 		virtual void SetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
   529 
   530 		/**
   531 		*	@brief Reverses SetIgnoreHooks' effect
   532 		*
   533 		*	@param plug The unique identifier of the plugin that calls this function
   534 		*	@param vfnptr The virtual function pointer of the function in question
   535 		*/
   536 		virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
   537 
   538 		/**
   539 		*	@brief Finds the original entry of a virtual function pointer
   540 		*
   541 		*	@param vfnptr The virtual function pointer
   542 		*	@return The original entry if the virtual function pointer has been patched; NULL otherwise.
   543 		*/
   544 		virtual void *GetOrigVfnPtrEntry(void *vfnptr) = 0;
   545 	};
   546 
   547 	// For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
   548 	//  These have to be able to return references. If T is a reference, the pointers returned
   549 	//  from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
   550 	template <class T> struct MacroRefHelpers
   551 	{
   552 		inline static const T* GetOrigRet(ISourceHook *shptr)
   553 		{
   554 			return reinterpret_cast<const T*>(shptr->GetOrigRet());
   555 		}
   556 		inline static const T* GetOverrideRet(ISourceHook *shptr)
   557 		{
   558 			return reinterpret_cast<const T*>(shptr->GetOverrideRet());
   559 		}
   560 	};
   561 
   562 	template <class T> struct MacroRefHelpers<T&>
   563 	{
   564 		inline static T* GetOrigRet(ISourceHook *shptr)
   565 		{
   566 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
   567 			return &ref;
   568 		}
   569 		inline static T* GetOverrideRet(ISourceHook *shptr)
   570 		{
   571 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
   572 			return &ref;
   573 		}
   574 	};
   575 
   576 	// For source-level compatibility
   577 	template <class T> struct CallClass
   578 	{
   579 		T *ptr;
   580 
   581 		CallClass(T *p) : ptr(p)
   582 		{
   583 		}
   584 
   585 		operator T*()
   586 		{
   587 			return ptr;
   588 		}
   589 	};
   590 
   591 	typedef CallClass<void> GenericCallClass;
   592 	typedef CallClass<EmptyClass> ManualCallClass;
   593 
   594 	template <class T>
   595 	CallClass<T> *GetCallClass(T *p)
   596 	{
   597 		return new CallClass<T>(p);
   598 	}
   599 
   600 	template <class T>
   601 	void ReleaseCallClass(CallClass<T> *p)
   602 	{
   603 		delete p;
   604 	}
   605 
   606 	template <class X, class MFP>
   607 	void *GetOrigVfnPtrEntry(X *pInstance, MFP mfp, ISourceHook *pSH)
   608 	{
   609 		SourceHook::MemFuncInfo info = {true, -1, 0, 0};
   610 		SourceHook::GetFuncInfo(pInstance, mfp, info);
   611 
   612 		void *vfnptr = reinterpret_cast<void*>(
   613 			*reinterpret_cast<void***>(reinterpret_cast<char*>(pInstance) + info.thisptroffs + info.vtbloffs) + info.vtblindex);
   614 
   615 		void *origentry = pSH->GetOrigVfnPtrEntry(vfnptr);
   616 		
   617 		return origentry ? origentry : *reinterpret_cast<void**>(vfnptr);
   618 	}
   619 }
   620 
   621 /************************************************************************/
   622 /* High level interface                                                 */
   623 /************************************************************************/
   624 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   625 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   626 #define META_RESULT_ORIG_RET(type)			*SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
   627 #define META_RESULT_OVERRIDE_RET(type)		*SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
   628 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   629 
   630 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   631 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   632 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   633 
   634 // If a hook on a function which returns a reference does not want to specify a return value,
   635 // it can use this macro.
   636 //   ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
   637 #define RETURN_META_NOREF(result, rettype)	do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
   638 
   639 // only call these from the hook handlers directly!
   640 // :TODO: enforce it ?
   641 
   642 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   643 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   644 
   645 // SourceHook::SetOverrideRet is defined later.
   646 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   647 	do { \
   648 		SET_META_RESULT(result); \
   649 		SH_GLOB_SHPTR->DoRecall(); \
   650 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   651 		RETURN_META(MRES_SUPERCEDE); \
   652 	} while (0)
   653 
   654 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   655 	do { \
   656 		SET_META_RESULT(result); \
   657 		SH_GLOB_SHPTR->DoRecall(); \
   658 		if ((result) >= MRES_OVERRIDE) \
   659 		{ \
   660 			/* meh, set the override result here because we don't get a chance to return */ \
   661 			/* before continuing the hook loop through the recall */ \
   662 			SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
   663 		} \
   664 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   665 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   666 	} while (0)
   667 
   668 // :TODO: thisptroffs in MNEWPARAMS ??
   669 
   670 #if SH_COMP == SH_COMP_MSVC
   671 
   672 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   673 	union \
   674 	{ \
   675 		SH_MFHCls(hookname)::ECMFP mfp; \
   676 		void *addr; \
   677 	} u; \
   678 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   679 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   680 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   681 
   682 #elif SH_COMP == SH_COMP_GCC
   683 
   684 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   685 	union \
   686 	{ \
   687 		SH_MFHCls(hookname)::ECMFP mfp; \
   688 		struct \
   689 		{ \
   690 			void *addr; \
   691 			intptr_t adjustor; \
   692 		} s; \
   693 	} u; \
   694 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   695 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   696 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   697 	u.s.adjustor = 0;
   698 
   699 #endif
   700 
   701 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   702 	do { \
   703 		SET_META_RESULT(result); \
   704 		SH_GLOB_SHPTR->DoRecall(); \
   705 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   706 		(thisptr->*(u.mfp)) newparams; \
   707 		RETURN_META(MRES_SUPERCEDE); \
   708 	} while (0)
   709 
   710 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   711 	do { \
   712 		SET_META_RESULT(result); \
   713 		SH_GLOB_SHPTR->DoRecall(); \
   714 		if ((result) >= MRES_OVERRIDE) \
   715 		{ \
   716 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   717 			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   718 		} \
   719 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   720 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   721 	} while (0)
   722 
   723 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   724 	do { \
   725 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   726 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   727 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   728 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   729 	} while (0)
   730 
   731 
   732 #define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
   733 
   734 // For source-level compatibility
   735 
   736 #define SH_GET_CALLCLASS(ptr) SourceHook::GetCallClass(ptr)
   737 #define SH_GET_MCALLCLASS(ptr, size) SourceHook::GetCallClass(reinterpret_cast<SourceHook::EmptyClass*>(ptr))
   738 #define SH_RELEASE_CALLCLASS(ptr) SourceHook::ReleaseCallClass(ptr)
   739 
   740 // New ADD / REMOVE macros.
   741 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
   742 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
   743 
   744 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   745 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   746 	post, handler)
   747 
   748 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   749 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   750 	post, handler) 
   751 
   752 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   753 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   754 
   755 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   756 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler) 
   757 
   758 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   759 	__SourceHook_FHVPAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   760 	post, handler, false)
   761 
   762 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, vtableptr, handler, post) \
   763 	__SourceHook_FHVPAdd##ifacetype##ifacefunc(reinterpret_cast<void*>(vtableptr), \
   764 	post, handler, true)
   765 
   766 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
   767 	__SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler, false)
   768 
   769 #define SH_ADD_MANUALDVPHOOK(hookname, vtableptr, handler, post) \
   770 	__SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(vtableptr), post, handler, true)
   771 
   772 #define SH_REMOVE_HOOK_ID(hookid) \
   773 	(SH_GLOB_SHPTR->RemoveHookByID(SH_GLOB_PLUGPTR, hookid))
   774 
   775 // Old macros
   776 // !! These are now deprecated. Instead, use one of these:
   777 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   778 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   779 
   780 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   781 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   782 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   783 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   784 
   785 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   786 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   787 
   788 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   789 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   790 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   791 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   792 
   793 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   794 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   795 
   796 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   797 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   798 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   799 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   800 
   801 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   802 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   803 
   804 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   805 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   806 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   807 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)  
   808 
   809 #define SH_NOATTRIB
   810 
   811 
   812 
   813 
   814 #if SH_COMP == SH_COMP_MSVC
   815 # define SH_SETUP_MFP(mfp) \
   816 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   817 #elif SH_COMP == SH_COMP_GCC
   818 # define SH_SETUP_MFP(mfp) \
   819 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   820 	reinterpret_cast<void**>(&mfp)[1] = 0;
   821 #else
   822 # error Not supported yet.
   823 #endif
   824 
   825 //////////////////////////////////////////////////////////////////////////
   826 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   827 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   828 
   829 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   830 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   831 	{ \
   832 		GetFuncInfo(funcptr, ms_MFI); \
   833 	} \
   834 	\
   835 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   836 	{ \
   837 		using namespace ::SourceHook; \
   838 		GetFuncInfo(funcptr, ms_MFI); \
   839 		/* Verify interface version */ \
   840 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   841 			return 1; \
   842 		if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   843 			return 1; \
   844 		\
   845 		if (action == HA_GetInfo) \
   846 		{ \
   847 			param->SetVersion(SH_HOOKMAN_VERSION); \
   848 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   849 				reinterpret_cast<const char*>(&ms_Proto)); \
   850 			\
   851 			MemFuncInfo mfi = {true, -1, 0, 0}; \
   852 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   853 			param->SetHookfuncVfnptr( \
   854 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   855 			return 0; \
   856 		} \
   857 		else if (action == HA_Register) \
   858 		{ \
   859 			ms_HI = param; \
   860 			return 0; \
   861 		} \
   862 		else if (action == HA_Unregister) \
   863 		{ \
   864 			ms_HI = NULL; \
   865 			return 0; \
   866 		} \
   867 		else \
   868 			return 1; \
   869 	}
   870 
   871 // It has to be possible to use the macros in namespaces
   872 // -> So we need to access and extend the global SourceHook namespace
   873 // We use a namespace alias for this
   874 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   875 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   876 	{ \
   877 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   878 		static ::SourceHook::MemFuncInfo ms_MFI; \
   879 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   880 		static ::SourceHook::ProtoInfo ms_Proto; \
   881 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   882 
   883 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   884 	}; \
   885 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   886 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   887 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   888 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   889 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   890 	{ \
   891 		using namespace ::SourceHook; \
   892 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   893 		GetFuncInfo(funcptr, mfi); \
   894 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   895 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   896 		\
   897 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, mfi.thisptroffs, \
   898 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   899 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   900 	} \
   901 	int __SourceHook_FHVPAdd##ifacetype##ifacefunc(void *iface, bool post, \
   902 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler, bool direct) \
   903 	{ \
   904 		using namespace ::SourceHook; \
   905 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   906 		GetFuncInfo(funcptr, mfi); \
   907 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   908 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   909 		\
   910 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
   911 			direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
   912 			iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   913 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   914 	} \
   915 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   916 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   917 	{ \
   918 		using namespace ::SourceHook; \
   919 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   920 		GetFuncInfo(funcptr, mfi); \
   921 		if (mfi.thisptroffs < 0) \
   922 			return false; /* No virtual inheritance supported */ \
   923 		\
   924 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   925 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   926 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   927 	} \
   928 
   929 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   930 	struct SH_MFHCls(hookname) \
   931 	{ \
   932 		static SH_MFHCls(hookname) ms_Inst; \
   933 		static ::SourceHook::MemFuncInfo ms_MFI; \
   934 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   935 		static ::SourceHook::ProtoInfo ms_Proto; \
   936 		\
   937 		SH_MFHCls(hookname)() \
   938 		{ \
   939 			ms_MFI.isVirtual = true; \
   940 			ms_MFI.thisptroffs = pthisptroffs; \
   941 			ms_MFI.vtblindex = pvtblidx; \
   942 			ms_MFI.vtbloffs = pvtbloffs; \
   943 		} \
   944 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   945 		{ \
   946 			using namespace ::SourceHook; \
   947 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   948 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   949 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   950 			/* Verify interface version */ \
   951 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   952 				return 1; \
   953 			if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   954 				return 1; \
   955 			\
   956 			if (action == HA_GetInfo) \
   957 			{ \
   958 				param->SetVersion(SH_HOOKMAN_VERSION); \
   959 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   960 					reinterpret_cast<const char*>(&ms_Proto)); \
   961 				\
   962 				MemFuncInfo mfi = {true, -1, 0, 0}; \
   963 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   964 				param->SetHookfuncVfnptr( \
   965 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   966 				return 0; \
   967 			} \
   968 			else if (action == HA_Register) \
   969 			{ \
   970 				ms_HI = param; \
   971 				return 0; \
   972 			} \
   973 			else if (action == HA_Unregister) \
   974 			{ \
   975 				ms_HI = NULL; \
   976 				return 0; \
   977 			} \
   978 			else \
   979 				return 1; \
   980 		}
   981 
   982 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   983 	}; \
   984 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   985 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   986 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   987 	int __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   988 		SH_MFHCls(hookname)::FD handler) \
   989 	{ \
   990 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, pthisptroffs, \
   991 			SH_MFHCls(hookname)::HookManPubFunc, \
   992 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   993 	} \
   994 	int __SourceHook_FHMVPAdd##hookname(void *iface, bool post, \
   995 		SH_MFHCls(hookname)::FD handler, bool direct) \
   996 	{ \
   997 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
   998 			direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
   999 			iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
  1000 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
  1001 	} \
  1002 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
  1003 		SH_MFHCls(hookname)::FD handler) \
  1004 	{ \
  1005 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
  1006 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
  1007 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
  1008 	} \
  1009 
  1010 
  1011 #define SH_SETUPCALLS(rettype, paramtypes, params) \
  1012 	/* 1) Find the vfnptr */ \
  1013 	using namespace ::SourceHook; \
  1014 	void *ourvfnptr = reinterpret_cast<void*>( \
  1015 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
  1016 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
  1017 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
  1018 	\
  1019 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
  1020 	/* ... and the iface */ \
  1021 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
  1022 	if (!ifinfo) \
  1023 	{ \
  1024 		/* The iface info was not found. Redirect the call to the original function. */ \
  1025 		rettype (EmptyClass::*mfp)paramtypes; \
  1026 		SH_SETUP_MFP(mfp); \
  1027 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1028 	} \
  1029 	/* 2) Declare some vars and set it up */ \
  1030 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
  1031 	IHookList *prelist = ifinfo->GetPreHooks(); \
  1032 	IHookList *postlist = ifinfo->GetPostHooks(); \
  1033 	META_RES status = MRES_IGNORED; \
  1034 	META_RES prev_res; \
  1035 	META_RES cur_res; \
  1036 	typedef ReferenceCarrier<rettype>::type my_rettype; \
  1037 	my_rettype orig_ret; \
  1038 	my_rettype override_ret; \
  1039 	my_rettype plugin_ret; \
  1040 	void* ifptr; \
  1041 	my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
  1042 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
  1043 
  1044 #define SH_CALL_HOOKS(post, params) \
  1045 	if (SH_GLOB_SHPTR->ShouldContinue()) \
  1046 	{ \
  1047 		prev_res = MRES_IGNORED; \
  1048 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
  1049 		{ \
  1050 			cur_res = MRES_IGNORED; \
  1051 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
  1052 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
  1053 			prev_res = cur_res; \
  1054 			if (cur_res > status) \
  1055 				status = cur_res; \
  1056 			if (cur_res >= MRES_OVERRIDE) \
  1057 				*pOverrideRet = plugin_ret; \
  1058 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
  1059 			{ \
  1060 				iter.SetToZero(); \
  1061 				break; \
  1062 			} \
  1063 		} \
  1064 	}
  1065 
  1066 #define SH_CALL_ORIG(rettype, paramtypes, params) \
  1067 	if (status != MRES_SUPERCEDE) \
  1068 	{ \
  1069 		rettype (EmptyClass::*mfp)paramtypes; \
  1070 		SH_SETUP_MFP(mfp); \
  1071 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1072 	} \
  1073 	else \
  1074 		orig_ret = override_ret; \
  1075 
  1076 #define SH_RETURN() \
  1077 	SH_GLOB_SHPTR->HookLoopEnd(); \
  1078 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
  1079 
  1080 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
  1081 	SH_SETUPCALLS(rettype, paramtypes, params) \
  1082 	SH_CALL_HOOKS(pre, params) \
  1083 	SH_CALL_ORIG(rettype, paramtypes, params) \
  1084 	SH_CALL_HOOKS(post, params) \
  1085 	SH_RETURN()
  1086 
  1087 //////////////////////////////////////////////////////////////////////////
  1088 #define SH_SETUPCALLS_void(paramtypes, params) \
  1089 	/* 1) Find the vfnptr */ \
  1090 	using namespace ::SourceHook; \
  1091 	void *ourvfnptr = reinterpret_cast<void*>( \
  1092 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
  1093 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
  1094 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
  1095 	\
  1096 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
  1097 	/* ... and the iface */ \
  1098 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
  1099 	if (!ifinfo) \
  1100 	{ \
  1101 		/* The iface info was not found. Redirect the call to the original function. */ \
  1102 		void (EmptyClass::*mfp)paramtypes; \
  1103 		SH_SETUP_MFP(mfp); \
  1104 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1105 		return; \
  1106 	} \
  1107 	/* 2) Declare some vars and set it up */ \
  1108 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
  1109 	IHookList *prelist = ifinfo->GetPreHooks(); \
  1110 	IHookList *postlist = ifinfo->GetPostHooks(); \
  1111 	META_RES status = MRES_IGNORED; \
  1112 	META_RES prev_res; \
  1113 	META_RES cur_res; \
  1114 	void* ifptr; \
  1115 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
  1116 
  1117 #define SH_CALL_HOOKS_void(post, params) \
  1118 	if (SH_GLOB_SHPTR->ShouldContinue()) \
  1119 	{ \
  1120 		prev_res = MRES_IGNORED; \
  1121 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
  1122 		{ \
  1123 			cur_res = MRES_IGNORED; \
  1124 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
  1125 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
  1126 			prev_res = cur_res; \
  1127 			if (cur_res > status) \
  1128 				status = cur_res; \
  1129 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
  1130 			{ \
  1131 				iter.SetToZero(); \
  1132 				break; \
  1133 			} \
  1134 		} \
  1135 	}
  1136 
  1137 #define SH_CALL_ORIG_void(paramtypes, params) \
  1138 	if (status != MRES_SUPERCEDE) \
  1139 	{ \
  1140 		void (EmptyClass::*mfp)paramtypes; \
  1141 		SH_SETUP_MFP(mfp); \
  1142 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1143 	}
  1144 
  1145 #define SH_RETURN_void() \
  1146 	SH_GLOB_SHPTR->HookLoopEnd();
  1147 
  1148 #define SH_HANDLEFUNC_void(paramtypes, params) \
  1149 	SH_SETUPCALLS_void(paramtypes, params) \
  1150 	SH_CALL_HOOKS_void(pre, params) \
  1151 	SH_CALL_ORIG_void(paramtypes, params) \
  1152 	SH_CALL_HOOKS_void(post, params) \
  1153 	SH_RETURN_void()
  1154 
  1155 
  1156 // Special vafmt handlers
  1157 // :TODO: what
  1158 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
  1159 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
  1160 	SH_CALL_HOOKS(pre, params_plug) \
  1161 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
  1162 	SH_CALL_HOOKS(post, params_plug) \
  1163 	SH_RETURN()
  1164 
  1165 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
  1166 	SH_SETUPCALLS_void(paramtypes, params_orig) \
  1167 	SH_CALL_HOOKS_void(pre, params_plug) \
  1168 	SH_CALL_ORIG_void(paramtypes, params_orig) \
  1169 	SH_CALL_HOOKS_void(post, params_plug) \
  1170 	SH_RETURN_void()
  1171 
  1172 //////////////////////////////////////////////////////////////////////////
  1173 
  1174 // :FIXME:
  1175 //  sizeof on references returns the size of the datatype, NOT the pointer size or something
  1176 //  -> one should probably flag references in __SourceHook_ParamSizes_* !
  1177 //		or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
  1178 
  1179 @[$1,0,$a:
  1180 // ********* Support for $1 arguments *********
  1181 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1182 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1183 		(&ifacetype::ifacefunc))) \
  1184 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1185 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1186 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1187 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1188 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1189 	\
  1190 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1191 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1192 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1193 
  1194 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1195 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1196 		(&ifacetype::ifacefunc))) \
  1197 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1198 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1199 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1200 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1201 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1202 	\
  1203 	\
  1204 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1205 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1206 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1207 
  1208 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1209 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1210 		(&ifacetype::ifacefunc))) \
  1211 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
  1212 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1213 		{ \
  1214 			char buf[::SourceHook::STRBUF_LEN]; \
  1215 			va_list ap; \
  1216 			va_start(ap, fmt); \
  1217 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1218 			va_end(ap); \
  1219 			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); \
  1220 		} \
  1221 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1222 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1223 	\
  1224 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1225 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1226 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1227 
  1228 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1229 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1230 		(&ifacetype::ifacefunc))) \
  1231 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1232 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1233 		{ \
  1234 			char buf[::SourceHook::STRBUF_LEN]; \
  1235 			va_list ap; \
  1236 			va_start(ap, fmt); \
  1237 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1238 			va_end(ap); \
  1239 			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)); \
  1240 		} \
  1241 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1242 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1243 	\
  1244 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1245 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1246 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1247 
  1248 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1249 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1250 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1251 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1252 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1253 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1254 		typedef rettype RetType; \
  1255 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1256 	\
  1257 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1258 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
  1259 		$1, __SourceHook_ParamSizesM_##hookname); \
  1260 
  1261 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1262 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1263 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1264 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1265 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1266 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1267 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1268 	\
  1269 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1270 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
  1271 		$1, __SourceHook_ParamSizesM_##hookname); \
  1272 
  1273 @]
  1274 
  1275 
  1276 //////////////////////////////////////////////////////////////////////////
  1277 // SH_CALL
  1278 
  1279 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1280 { \
  1281 	using namespace ::SourceHook; \
  1282 	\
  1283 	m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
  1284 	RetType tmpret = (m_ThisPtr->*m_MFP)call; \
  1285 	m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
  1286 	return tmpret; \
  1287 }
  1288 
  1289 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
  1290 { \
  1291 	using namespace ::SourceHook; \
  1292 	\
  1293 	m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
  1294 	(m_ThisPtr->*m_MFP)call; \
  1295 	m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
  1296 }
  1297 
  1298 namespace SourceHook
  1299 {
  1300 	// Call Class Wrapper!
  1301 	template <class T> struct CCW
  1302 	{
  1303 		typedef T type;
  1304 
  1305 		// Get Real Pointer!
  1306 		static inline T *GRP(T *p)
  1307 		{
  1308 			return p;
  1309 		}
  1310 	};
  1311 
  1312 	template <class T> struct CCW< CallClass<T> >
  1313 	{
  1314 		typedef T type;
  1315 
  1316 		// Get Real Pointer!
  1317 		static inline T *GRP(CallClass<T> *p)
  1318 		{
  1319 			return p->ptr;
  1320 		}
  1321 	};
  1322 
  1323 @[$1,0,$a:
  1324 	// Support for $1 arguments
  1325 	template<class ObjType, class MFP, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1326 	{
  1327 		ObjType *m_ThisPtr;
  1328 		void *m_VfnPtr;
  1329 		MFP m_MFP;
  1330 		ISourceHook *m_pSH;
  1331 		Plugin m_Plug;
  1332 	public:
  1333 		ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
  1334 			m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
  1335 	
  1336 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1337 			SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1338 	         
  1339 		@[$2,$1+1,$a:
  1340 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1341 			SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1342 		@]
  1343 	};
  1344 
  1345 	template<class ObjType, class MFP@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFP, void@[$2,1,$1:, Param$2@]>
  1346 	{
  1347 	   ObjType *m_ThisPtr;
  1348 	   void *m_VfnPtr;
  1349 	   MFP m_MFP;
  1350 	   ISourceHook *m_pSH;
  1351 	   Plugin m_Plug;
  1352 	public:
  1353 	   ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
  1354 		   m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
  1355 	
  1356 	   void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1357 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1358 	         
  1359 	   @[$2,$1+1,$a:
  1360 	   template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1361 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1362 	   @]
  1363 	};
  1364 @]
  1365 
  1366 }
  1367 
  1368 #define SH__CALL_GET_VFNPTR_NORMAL \
  1369 	using namespace ::SourceHook; \
  1370 	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1371 	GetFuncInfo(CCW<Y>::GRP(ptr), mfp, mfi); \
  1372 	void *vfnptr = reinterpret_cast<void*>( \
  1373 		*reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
  1374 
  1375 #define SH__CALL_GET_VFNPTR_MANUAL \
  1376 	using namespace ::SourceHook; \
  1377 	void *vfnptr = reinterpret_cast<void*>( \
  1378 		*reinterpret_cast<void***>( (reinterpret_cast<char*>(CCW<Y>::GRP(ptr)) + thisptroffs + vtbloffs) ) + vtblidx); \
  1379 	/* patch mfp */ \
  1380 	*reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
  1381 
  1382 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1383 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1384 // The only purpose of the mfp2 parameter is to extract the return type
  1385 
  1386 @[$1,0,$a:
  1387 // Support for $1 arguments
  1388 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1389 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1390 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1391 {
  1392 	SH__CALL_GET_VFNPTR_NORMAL
  1393 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1394 		mfp, vfnptr, shptr, plug);
  1395 }
  1396 
  1397 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1398 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1399 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1400 {
  1401 	SH__CALL_GET_VFNPTR_NORMAL
  1402 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1403 		mfp, vfnptr, shptr, plug);
  1404 }
  1405 
  1406 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1407 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
  1408 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1409 {
  1410 	SH__CALL_GET_VFNPTR_MANUAL
  1411 	return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
  1412 		reinterpret_cast<SourceHook::EmptyClass*>(CCW<Y>::GRP(ptr)), mfp, vfnptr, shptr, plug);
  1413 }
  1414 @]
  1415 
  1416 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1417 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1418 
  1419 @[$1,0,$a:
  1420 // Support for $1 arguments
  1421 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1422 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1423 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1424 {
  1425 	SH__CALL_GET_VFNPTR_NORMAL
  1426 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1427 		mfp, vfnptr, shptr, plug);
  1428 }
  1429 
  1430 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1431 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1432 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1433 {
  1434 	SH__CALL_GET_VFNPTR_NORMAL
  1435 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1436 		mfp, vfnptr, shptr, plug);
  1437 }
  1438 
  1439 @]
  1440 
  1441 #endif
  1442 
  1443 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
  1444 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
  1445 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1446 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1447 
  1448 #undef SH_MAKE_EXECUTABLECLASS_OB
  1449 
  1450 //////////////////////////////////////////////////////////////////////////
  1451 // SetOverrideRet and RecallGetIface for recalls
  1452 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1453 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1454 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1455 
  1456 namespace SourceHook
  1457 {
  1458 	template <class RetType>
  1459 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1460 	{
  1461 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1462 	}
  1463 
  1464 	// SetOverrideResult used to be implemented like this:
  1465 	//  SetOverrideResult(shptr, memfuncptr, return);
  1466 	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1467 	//  (it thought it was ambigous - the ref and non-ref type)
  1468 	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1469 	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1470 	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1471 
  1472 	template <class T> struct OverrideFunctor
  1473 	{
  1474 		void operator()(ISourceHook *shptr, T res)
  1475 		{
  1476 			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1477 		}
  1478 	};
  1479 	template <class T> struct OverrideFunctor<T&>
  1480 	{
  1481 		void operator()(ISourceHook *shptr, T &res)
  1482 		{
  1483 			// overrideretptr points to ReferenceCarrier<T&>
  1484 			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1485 		}
  1486 	};
  1487 @[$1,0,$a:
  1488 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1489 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1490 	{
  1491 		return OverrideFunctor<RetType>();
  1492 	}
  1493 
  1494 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1495 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1496 	{
  1497 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1498 	}
  1499 @]
  1500 }
  1501 
  1502 #endif
  1503 	// The pope is dead. -> :(