sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sat May 12 08:08:01 2007 +0000 (2007-05-12)
changeset 328 3a04ef79edf1
parent 324 257f0837a885
child 329 63bb4ae624f1
permissions -rwxr-xr-x
Direct VP hooks, new SH_CALL
     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 CallClass
   302 	{
   303 		virtual B *GetThisPtr() = 0;
   304 		virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
   305 	};
   306 
   307 	typedef CallClass<void> GenericCallClass;
   308 	typedef CallClass<EmptyClass> ManualCallClass;
   309 
   310 	// 09.08.2008 (6 AM, I just woke up, the others are still sleeping so i finally can use this notebook !!)
   311 	// - Today is an important day.
   312 	// I'm adding support for functions which return references.
   313 
   314 	// How it works:
   315 	//  SH_SETUPCALLS doesn't use plain rettype to store the temporary return values (plugin ret, orig ret,
   316 	//  override ret) anymore; instead, it uses SourceHook::ReferenceCarrier<rettype>::type
   317 	//  this is typedefed to the original rettype normally, but if the rettype is a reference, it's a special class
   318 	//  which stores the reference as a pointer, and implements constructors, operator= and a conversion operator.
   319 	//  special cases were needed for getoverrideret / getorigret; these are implemented through the
   320 	//  SourceHook::MacroRefHelpers structs.
   321 	//  Furthermore, SetOverrideRet had to be changed a bit; see SourceHook::OverrideFunctor somewhere down in this file.
   322 	template <class T> struct ReferenceCarrier
   323 	{
   324 		typedef T type;
   325 	};
   326 
   327 	template <class T> struct ReferenceCarrier<T&>
   328 	{
   329 		class type
   330 		{
   331 			T *m_StoredRef;
   332 		public:
   333 			type() : m_StoredRef(NULL)
   334 			{
   335 			}
   336 			type(T& ref) : m_StoredRef(&ref)
   337 			{
   338 			}
   339 
   340 			T& operator= (T& ref)
   341 			{
   342 				m_StoredRef = &ref;
   343 				return ref;
   344 			}
   345 
   346 			operator T& () const
   347 			{
   348 				return *m_StoredRef;
   349 			}
   350 		};
   351 	};
   352 
   353 	/**
   354 	*	@brief The main SourceHook interface
   355 	*/
   356 	class ISourceHook
   357 	{
   358 	public:
   359 		/**
   360 		*	@brief Return interface version
   361 		*/
   362 		virtual int GetIfaceVersion() = 0;
   363 
   364 		/**
   365 		*	@brief Return implementation version
   366 		*/
   367 		virtual int GetImplVersion() = 0;
   368 
   369 		/**
   370 		*	@brief Add a hook.
   371 		*
   372 		*	@return True if the function succeeded, false otherwise
   373 		*
   374 		*	@param plug The unique identifier of the plugin that calls this function
   375 		*	@param iface The interface pointer
   376 		*	@param ifacesize The size of the class iface points to
   377 		*	@param myHookMan A hook manager function that should be capable of handling the function
   378 		*	@param handler A pointer to a FastDelegate containing the hook handler
   379 		*	@param post Set to true if you want a post handler
   380 		*/
   381 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   382 			ISHDelegate *handler, bool post) = 0;
   383 
   384 		/**
   385 		*	@brief Removes a hook.
   386 		*
   387 		*	@return True if the function succeeded, false otherwise
   388 		*
   389 		*	@param plug The unique identifier of the plugin that calls this function
   390 		*	@param iface The interface pointer
   391 		*	@param myHookMan A hook manager function that should be capable of handling the function
   392 		*	@param handler A pointer to a FastDelegate containing the hook handler
   393 		*	@param post Set to true if you want a post handler
   394 		*/
   395 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   396 			ISHDelegate *handler, bool post) = 0;
   397 
   398 		/**
   399 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   400 		*
   401 		*	@param plug The unique identifier of the plugin in question
   402 		*/
   403 		virtual bool IsPluginInUse(Plugin plug) = 0;
   404 
   405 		/**
   406 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   407 		*
   408 		*	@param iface The interface pointer
   409 		*	@param size Size of the class instance
   410 		*/
   411 		virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
   412 
   413 		/**
   414 		*	@brief Release a callclass
   415 		*
   416 		*	@param ptr Pointer to the callclass
   417 		*/
   418 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   419 
   420 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   421 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   422 															//!<  previously calledhandler
   423 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   424 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   425 															//!<  If not in post function, undefined
   426 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   427 															//!<  If none is specified, NULL
   428 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   429 		//////////////////////////////////////////////////////////////////////////
   430 		// For hook managers
   431 		virtual void HookLoopBegin(IIface *pIface) = 0;			//!< Should be called when a hook loop begins
   432 		virtual void HookLoopEnd() = 0;							//!< Should be called when a hook loop exits
   433 		virtual void SetCurResPtr(META_RES *mres) = 0;			//!< Sets pointer to the current meta result
   434 		virtual void SetPrevResPtr(META_RES *mres) = 0;			//!< Sets pointer to previous meta result
   435 		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
   436 		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
   437 		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
   438 		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
   439 		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
   440 
   441 		/**
   442 		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
   443 		*
   444 		*	@param plug The owner of the hook manager
   445 		*   @param pubFunc The hook manager's info function
   446 		*/
   447 		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
   448 
   449 		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   450 		/*
   451 			HOW RECALLS WORK:
   452 			
   453 			The problem:
   454 				Users want the ability to change parameters of the called function
   455 				from inside their handler.
   456 			The solution:
   457 				1) Mark as "recall"
   458 				2) Recall the function
   459 				3) => SH's hook func gets called:
   460 				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   461 				  5) does all iteration and returns normally
   462 		        6) The user's handler returns immediately
   463 				7) The hook func returns immediately as well
   464 
   465 				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   466 				ended with. The last handler (doing the recall) is also able to specify its own
   467 				META_RES.
   468 		*/
   469 
   470 		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
   471 
   472 		/**
   473 		*	@brief Set up the hook loop. Equivalent to calling:
   474 		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
   475 		*
   476 		*	@param statusPtr		pointer to status variable
   477 		*	@param prevResPtr		pointer to previous result variable
   478 		*	@param curResPtr		pointer to current result variable
   479 		*	@param ifacePtrPtr		pointer to interface this pointer variable
   480 		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   481 		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   482 		*	
   483 		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   484 		*		when the hook func is being called as part of a recall
   485 		*/
   486 		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   487 			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
   488 
   489 		/**
   490 		*	@brief Modes for the new AddHook
   491 		*/
   492 		enum AddHookMode
   493 		{
   494 			Hook_Normal,
   495 			Hook_VP,
   496 			Hook_DVP
   497 		}; 
   498 
   499 		/**
   500 		*	@brief Add a (VP) hook.
   501 		*
   502 		*	@return non-zero hook id on success, 0 otherwise
   503 		*
   504 		*	@param plug The unique identifier of the plugin that calls this function
   505 		*	@param mode	Can be either Hook_Normal or Hook_VP (vtable-wide hook)
   506 		*	@param iface The interface pointer
   507 		*	@param ifacesize The size of the class iface points to
   508 		*	@param myHookMan A hook manager function that should be capable of handling the function
   509 		*	@param handler A pointer to a FastDelegate containing the hook handler
   510 		*	@param post Set to true if you want a post handler
   511 		*/
   512 		virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   513 			ISHDelegate *handler, bool post) = 0;
   514 
   515 		/**
   516 		*	@brief Remove a VP hook by ID.
   517 		*
   518 		*	@return true on success, false otherwise
   519 		*
   520 		*	@param plug The unique identifier of the plugin that calls this function
   521 		*	@param hookid The hook id (returned by AddHookNew)
   522 		*/
   523 		virtual bool RemoveHookByID(Plugin plug, int hookid) = 0;
   524 
   525 		/**
   526 		*	@brief Makes sure that hooks are going to be ignored on the next call of vfnptr
   527 		*
   528 		*	@param plug The unique identifier of the plugin that calls this function
   529 		*	@param vfnptr The virtual function pointer of the function in question
   530 		*/
   531 		virtual void SetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
   532 
   533 		/**
   534 		*	@brief Reverses SetIgnoreHooks' effect
   535 		*
   536 		*	@param plug The unique identifier of the plugin that calls this function
   537 		*	@param vfnptr The virtual function pointer of the function in question
   538 		*/
   539 		virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr) = 0;
   540 	};
   541 
   542 	// For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
   543 	//  These have to be able to return references. If T is a reference, the pointers returned
   544 	//  from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
   545 	template <class T> struct MacroRefHelpers
   546 	{
   547 		inline static const T* GetOrigRet(ISourceHook *shptr)
   548 		{
   549 			return reinterpret_cast<const T*>(shptr->GetOrigRet());
   550 		}
   551 		inline static const T* GetOverrideRet(ISourceHook *shptr)
   552 		{
   553 			return reinterpret_cast<const T*>(shptr->GetOverrideRet());
   554 		}
   555 	};
   556 
   557 	template <class T> struct MacroRefHelpers<T&>
   558 	{
   559 		inline static T* GetOrigRet(ISourceHook *shptr)
   560 		{
   561 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
   562 			return &ref;
   563 		}
   564 		inline static T* GetOverrideRet(ISourceHook *shptr)
   565 		{
   566 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
   567 			return &ref;
   568 		}
   569 	};
   570 
   571 	// For source-level compatibility
   572 	template <class T> struct LegacyCallClass
   573 	{
   574 		T *ptr;
   575 
   576 		LegacyCallClass(T *p) : ptr(p)
   577 		{
   578 		}
   579 
   580 		operator T*()
   581 		{
   582 			return ptr;
   583 		}
   584 	};
   585 
   586 	template <class T>
   587 	LegacyCallClass<T> *GetLegacyCallClass(T *p)
   588 	{
   589 		return new LegacyCallClass<T>(p);
   590 	}
   591 
   592 	template <class T>
   593 	void ReleaseLegacyCallClass(LegacyCallClass<T> *p)
   594 	{
   595 		delete p;
   596 	}
   597 }
   598 
   599 /************************************************************************/
   600 /* High level interface                                                 */
   601 /************************************************************************/
   602 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   603 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   604 #define META_RESULT_ORIG_RET(type)			*SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
   605 #define META_RESULT_OVERRIDE_RET(type)		*SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
   606 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   607 
   608 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   609 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   610 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   611 
   612 // If a hook on a function which returns a reference does not want to specify a return value,
   613 // it can use this macro.
   614 //   ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
   615 #define RETURN_META_NOREF(result, rettype)	do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
   616 
   617 // only call these from the hook handlers directly!
   618 // :TODO: enforce it ?
   619 
   620 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   621 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   622 
   623 // SourceHook::SetOverrideRet is defined later.
   624 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   625 	do { \
   626 		SET_META_RESULT(result); \
   627 		SH_GLOB_SHPTR->DoRecall(); \
   628 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   629 		RETURN_META(MRES_SUPERCEDE); \
   630 	} while (0)
   631 
   632 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   633 	do { \
   634 		SET_META_RESULT(result); \
   635 		SH_GLOB_SHPTR->DoRecall(); \
   636 		if ((result) >= MRES_OVERRIDE) \
   637 		{ \
   638 			/* meh, set the override result here because we don't get a chance to return */ \
   639 			/* before continuing the hook loop through the recall */ \
   640 			SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
   641 		} \
   642 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   643 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   644 	} while (0)
   645 
   646 // :TODO: thisptroffs in MNEWPARAMS ??
   647 
   648 #if SH_COMP == SH_COMP_MSVC
   649 
   650 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   651 	union \
   652 	{ \
   653 		SH_MFHCls(hookname)::ECMFP mfp; \
   654 		void *addr; \
   655 	} u; \
   656 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   657 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   658 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   659 
   660 #elif SH_COMP == SH_COMP_GCC
   661 
   662 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   663 	union \
   664 	{ \
   665 		SH_MFHCls(hookname)::ECMFP mfp; \
   666 		struct \
   667 		{ \
   668 			void *addr; \
   669 			intptr_t adjustor; \
   670 		} s; \
   671 	} u; \
   672 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   673 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   674 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   675 	u.s.adjustor = 0;
   676 
   677 #endif
   678 
   679 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   680 	do { \
   681 		SET_META_RESULT(result); \
   682 		SH_GLOB_SHPTR->DoRecall(); \
   683 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   684 		(thisptr->*(u.mfp)) newparams; \
   685 		RETURN_META(MRES_SUPERCEDE); \
   686 	} while (0)
   687 
   688 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   689 	do { \
   690 		SET_META_RESULT(result); \
   691 		SH_GLOB_SHPTR->DoRecall(); \
   692 		if ((result) >= MRES_OVERRIDE) \
   693 		{ \
   694 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   695 			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   696 		} \
   697 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   698 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   699 	} while (0)
   700 
   701 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   702 	do { \
   703 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   704 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   705 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   706 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   707 	} while (0)
   708 
   709 // For source-level compatibility
   710 #define CallClass LegacyCallClass
   711 #define ManualCallClass LegacyCallClass<SourceHook::EmptyClass>
   712 
   713 #define SH_GET_CALLCLASS(ptr) SourceHook::GetLegacyCallClass(ptr)
   714 #define SH_GET_MCALLCLASS(ptr, size) SourceHook::GetLegacyCallClass(reinterpret_cast<SourceHook::EmptyClass*>(ptr))
   715 #define SH_RELEASE_CALLCLASS(ptr) SourceHook::ReleaseLegacyCallClass(ptr)
   716 
   717 // New ADD / REMOVE macros.
   718 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
   719 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
   720 
   721 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   722 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   723 	post, handler)
   724 
   725 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   726 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   727 	post, handler) 
   728 
   729 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   730 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   731 
   732 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   733 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler) 
   734 
   735 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   736 	__SourceHook_FHVPAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   737 	post, handler, false)
   738 
   739 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, vtableptr, handler, post) \
   740 	__SourceHook_FHVPAdd##ifacetype##ifacefunc(reinterpret_cast<void*>(vtableptr), \
   741 	post, handler, true)
   742 
   743 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
   744 	__SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler, false)
   745 
   746 #define SH_ADD_MANUALDVPHOOK(hookname, vtableptr, handler, post) \
   747 	__SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(vtableptr), post, handler, true)
   748 
   749 #define SH_REMOVE_HOOK_ID(hookid) \
   750 	(SH_GLOB_SHPTR->RemoveHookByID(SH_GLOB_PLUGPTR, hookid))
   751 
   752 // Old macros
   753 // !! These are now deprecated. Instead, use one of these:
   754 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   755 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   756 
   757 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   758 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   759 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   760 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   761 
   762 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   763 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   764 
   765 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   766 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   767 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   768 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   769 
   770 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   771 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   772 
   773 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   774 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   775 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   776 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   777 
   778 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   779 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   780 
   781 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   782 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   783 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   784 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)  
   785 
   786 #define SH_NOATTRIB
   787 
   788 
   789 
   790 
   791 #if SH_COMP == SH_COMP_MSVC
   792 # define SH_SETUP_MFP(mfp) \
   793 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   794 #elif SH_COMP == SH_COMP_GCC
   795 # define SH_SETUP_MFP(mfp) \
   796 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   797 	reinterpret_cast<void**>(&mfp)[1] = 0;
   798 #else
   799 # error Not supported yet.
   800 #endif
   801 
   802 //////////////////////////////////////////////////////////////////////////
   803 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   804 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   805 
   806 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   807 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   808 	{ \
   809 		GetFuncInfo(funcptr, ms_MFI); \
   810 	} \
   811 	\
   812 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   813 	{ \
   814 		using namespace ::SourceHook; \
   815 		GetFuncInfo(funcptr, ms_MFI); \
   816 		/* Verify interface version */ \
   817 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   818 			return 1; \
   819 		if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   820 			return 1; \
   821 		\
   822 		if (action == HA_GetInfo) \
   823 		{ \
   824 			param->SetVersion(SH_HOOKMAN_VERSION); \
   825 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   826 				reinterpret_cast<const char*>(&ms_Proto)); \
   827 			\
   828 			MemFuncInfo mfi = {true, -1, 0, 0}; \
   829 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   830 			param->SetHookfuncVfnptr( \
   831 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   832 			return 0; \
   833 		} \
   834 		else if (action == HA_Register) \
   835 		{ \
   836 			ms_HI = param; \
   837 			return 0; \
   838 		} \
   839 		else if (action == HA_Unregister) \
   840 		{ \
   841 			ms_HI = NULL; \
   842 			return 0; \
   843 		} \
   844 		else \
   845 			return 1; \
   846 	}
   847 
   848 // It has to be possible to use the macros in namespaces
   849 // -> So we need to access and extend the global SourceHook namespace
   850 // We use a namespace alias for this
   851 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   852 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   853 	{ \
   854 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   855 		static ::SourceHook::MemFuncInfo ms_MFI; \
   856 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   857 		static ::SourceHook::ProtoInfo ms_Proto; \
   858 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   859 
   860 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   861 	}; \
   862 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   863 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   864 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   865 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   866 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   867 	{ \
   868 		using namespace ::SourceHook; \
   869 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   870 		GetFuncInfo(funcptr, mfi); \
   871 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   872 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   873 		\
   874 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, mfi.thisptroffs, \
   875 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   876 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   877 	} \
   878 	int __SourceHook_FHVPAdd##ifacetype##ifacefunc(void *iface, bool post, \
   879 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler, bool direct) \
   880 	{ \
   881 		using namespace ::SourceHook; \
   882 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   883 		GetFuncInfo(funcptr, mfi); \
   884 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   885 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   886 		\
   887 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
   888 			direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
   889 			iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   890 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   891 	} \
   892 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   893 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   894 	{ \
   895 		using namespace ::SourceHook; \
   896 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   897 		GetFuncInfo(funcptr, mfi); \
   898 		if (mfi.thisptroffs < 0) \
   899 			return false; /* No virtual inheritance supported */ \
   900 		\
   901 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   902 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   903 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   904 	} \
   905 
   906 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   907 	struct SH_MFHCls(hookname) \
   908 	{ \
   909 		static SH_MFHCls(hookname) ms_Inst; \
   910 		static ::SourceHook::MemFuncInfo ms_MFI; \
   911 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   912 		static ::SourceHook::ProtoInfo ms_Proto; \
   913 		\
   914 		SH_MFHCls(hookname)() \
   915 		{ \
   916 			ms_MFI.isVirtual = true; \
   917 			ms_MFI.thisptroffs = pthisptroffs; \
   918 			ms_MFI.vtblindex = pvtblidx; \
   919 			ms_MFI.vtbloffs = pvtbloffs; \
   920 		} \
   921 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   922 		{ \
   923 			using namespace ::SourceHook; \
   924 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   925 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   926 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   927 			/* Verify interface version */ \
   928 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   929 				return 1; \
   930 			if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   931 				return 1; \
   932 			\
   933 			if (action == HA_GetInfo) \
   934 			{ \
   935 				param->SetVersion(SH_HOOKMAN_VERSION); \
   936 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   937 					reinterpret_cast<const char*>(&ms_Proto)); \
   938 				\
   939 				MemFuncInfo mfi = {true, -1, 0, 0}; \
   940 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   941 				param->SetHookfuncVfnptr( \
   942 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   943 				return 0; \
   944 			} \
   945 			else if (action == HA_Register) \
   946 			{ \
   947 				ms_HI = param; \
   948 				return 0; \
   949 			} \
   950 			else if (action == HA_Unregister) \
   951 			{ \
   952 				ms_HI = NULL; \
   953 				return 0; \
   954 			} \
   955 			else \
   956 				return 1; \
   957 		}
   958 
   959 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   960 	}; \
   961 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   962 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   963 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   964 	int __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   965 		SH_MFHCls(hookname)::FD handler) \
   966 	{ \
   967 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, pthisptroffs, \
   968 			SH_MFHCls(hookname)::HookManPubFunc, \
   969 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   970 	} \
   971 	int __SourceHook_FHMVPAdd##hookname(void *iface, bool post, \
   972 		SH_MFHCls(hookname)::FD handler, bool direct) \
   973 	{ \
   974 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
   975 			direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
   976 			iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
   977 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   978 	} \
   979 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   980 		SH_MFHCls(hookname)::FD handler) \
   981 	{ \
   982 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   983 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   984 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   985 	} \
   986 
   987 
   988 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   989 	/* 1) Find the vfnptr */ \
   990 	using namespace ::SourceHook; \
   991 	void *ourvfnptr = reinterpret_cast<void*>( \
   992 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   993 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   994 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   995 	\
   996 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   997 	/* ... and the iface */ \
   998 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   999 	if (!ifinfo) \
  1000 	{ \
  1001 		/* The iface info was not found. Redirect the call to the original function. */ \
  1002 		rettype (EmptyClass::*mfp)paramtypes; \
  1003 		SH_SETUP_MFP(mfp); \
  1004 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1005 	} \
  1006 	/* 2) Declare some vars and set it up */ \
  1007 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
  1008 	IHookList *prelist = ifinfo->GetPreHooks(); \
  1009 	IHookList *postlist = ifinfo->GetPostHooks(); \
  1010 	META_RES status = MRES_IGNORED; \
  1011 	META_RES prev_res; \
  1012 	META_RES cur_res; \
  1013 	typedef ReferenceCarrier<rettype>::type my_rettype; \
  1014 	my_rettype orig_ret; \
  1015 	my_rettype override_ret; \
  1016 	my_rettype plugin_ret; \
  1017 	void* ifptr; \
  1018 	my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
  1019 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
  1020 
  1021 #define SH_CALL_HOOKS(post, params) \
  1022 	if (SH_GLOB_SHPTR->ShouldContinue()) \
  1023 	{ \
  1024 		prev_res = MRES_IGNORED; \
  1025 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
  1026 		{ \
  1027 			cur_res = MRES_IGNORED; \
  1028 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
  1029 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
  1030 			prev_res = cur_res; \
  1031 			if (cur_res > status) \
  1032 				status = cur_res; \
  1033 			if (cur_res >= MRES_OVERRIDE) \
  1034 				*pOverrideRet = plugin_ret; \
  1035 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
  1036 			{ \
  1037 				iter.SetToZero(); \
  1038 				break; \
  1039 			} \
  1040 		} \
  1041 	}
  1042 
  1043 #define SH_CALL_ORIG(rettype, paramtypes, params) \
  1044 	if (status != MRES_SUPERCEDE) \
  1045 	{ \
  1046 		rettype (EmptyClass::*mfp)paramtypes; \
  1047 		SH_SETUP_MFP(mfp); \
  1048 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1049 	} \
  1050 	else \
  1051 		orig_ret = override_ret; \
  1052 
  1053 #define SH_RETURN() \
  1054 	SH_GLOB_SHPTR->HookLoopEnd(); \
  1055 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
  1056 
  1057 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
  1058 	SH_SETUPCALLS(rettype, paramtypes, params) \
  1059 	SH_CALL_HOOKS(pre, params) \
  1060 	SH_CALL_ORIG(rettype, paramtypes, params) \
  1061 	SH_CALL_HOOKS(post, params) \
  1062 	SH_RETURN()
  1063 
  1064 //////////////////////////////////////////////////////////////////////////
  1065 #define SH_SETUPCALLS_void(paramtypes, params) \
  1066 	/* 1) Find the vfnptr */ \
  1067 	using namespace ::SourceHook; \
  1068 	void *ourvfnptr = reinterpret_cast<void*>( \
  1069 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
  1070 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
  1071 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
  1072 	\
  1073 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
  1074 	/* ... and the iface */ \
  1075 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
  1076 	if (!ifinfo) \
  1077 	{ \
  1078 		/* The iface info was not found. Redirect the call to the original function. */ \
  1079 		void (EmptyClass::*mfp)paramtypes; \
  1080 		SH_SETUP_MFP(mfp); \
  1081 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1082 		return; \
  1083 	} \
  1084 	/* 2) Declare some vars and set it up */ \
  1085 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
  1086 	IHookList *prelist = ifinfo->GetPreHooks(); \
  1087 	IHookList *postlist = ifinfo->GetPostHooks(); \
  1088 	META_RES status = MRES_IGNORED; \
  1089 	META_RES prev_res; \
  1090 	META_RES cur_res; \
  1091 	void* ifptr; \
  1092 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
  1093 
  1094 #define SH_CALL_HOOKS_void(post, params) \
  1095 	if (SH_GLOB_SHPTR->ShouldContinue()) \
  1096 	{ \
  1097 		prev_res = MRES_IGNORED; \
  1098 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
  1099 		{ \
  1100 			cur_res = MRES_IGNORED; \
  1101 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
  1102 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
  1103 			prev_res = cur_res; \
  1104 			if (cur_res > status) \
  1105 				status = cur_res; \
  1106 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
  1107 			{ \
  1108 				iter.SetToZero(); \
  1109 				break; \
  1110 			} \
  1111 		} \
  1112 	}
  1113 
  1114 #define SH_CALL_ORIG_void(paramtypes, params) \
  1115 	if (status != MRES_SUPERCEDE) \
  1116 	{ \
  1117 		void (EmptyClass::*mfp)paramtypes; \
  1118 		SH_SETUP_MFP(mfp); \
  1119 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1120 	}
  1121 
  1122 #define SH_RETURN_void() \
  1123 	SH_GLOB_SHPTR->HookLoopEnd();
  1124 
  1125 #define SH_HANDLEFUNC_void(paramtypes, params) \
  1126 	SH_SETUPCALLS_void(paramtypes, params) \
  1127 	SH_CALL_HOOKS_void(pre, params) \
  1128 	SH_CALL_ORIG_void(paramtypes, params) \
  1129 	SH_CALL_HOOKS_void(post, params) \
  1130 	SH_RETURN_void()
  1131 
  1132 
  1133 // Special vafmt handlers
  1134 // :TODO: what
  1135 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
  1136 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
  1137 	SH_CALL_HOOKS(pre, params_plug) \
  1138 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
  1139 	SH_CALL_HOOKS(post, params_plug) \
  1140 	SH_RETURN()
  1141 
  1142 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
  1143 	SH_SETUPCALLS_void(paramtypes, params_orig) \
  1144 	SH_CALL_HOOKS_void(pre, params_plug) \
  1145 	SH_CALL_ORIG_void(paramtypes, params_orig) \
  1146 	SH_CALL_HOOKS_void(post, params_plug) \
  1147 	SH_RETURN_void()
  1148 
  1149 //////////////////////////////////////////////////////////////////////////
  1150 
  1151 // :FIXME:
  1152 //  sizeof on references returns the size of the datatype, NOT the pointer size or something
  1153 //  -> one should probably flag references in __SourceHook_ParamSizes_* !
  1154 //		or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
  1155 
  1156 @[$1,0,$a:
  1157 // ********* Support for $1 arguments *********
  1158 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1159 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1160 		(&ifacetype::ifacefunc))) \
  1161 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1162 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1163 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1164 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1165 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1166 	\
  1167 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1168 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1169 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1170 
  1171 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1172 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1173 		(&ifacetype::ifacefunc))) \
  1174 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1175 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1176 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1177 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1178 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1179 	\
  1180 	\
  1181 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1182 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1183 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1184 
  1185 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1186 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1187 		(&ifacetype::ifacefunc))) \
  1188 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
  1189 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1190 		{ \
  1191 			char buf[::SourceHook::STRBUF_LEN]; \
  1192 			va_list ap; \
  1193 			va_start(ap, fmt); \
  1194 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1195 			va_end(ap); \
  1196 			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); \
  1197 		} \
  1198 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1199 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1200 	\
  1201 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1202 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1203 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1204 
  1205 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1206 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1207 		(&ifacetype::ifacefunc))) \
  1208 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1209 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1210 		{ \
  1211 			char buf[::SourceHook::STRBUF_LEN]; \
  1212 			va_list ap; \
  1213 			va_start(ap, fmt); \
  1214 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1215 			va_end(ap); \
  1216 			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)); \
  1217 		} \
  1218 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1219 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1220 	\
  1221 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1222 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1223 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1224 
  1225 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1226 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1227 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1228 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1229 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1230 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1231 		typedef rettype RetType; \
  1232 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1233 	\
  1234 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1235 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
  1236 		$1, __SourceHook_ParamSizesM_##hookname); \
  1237 
  1238 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1239 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1240 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1241 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1242 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1243 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1244 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1245 	\
  1246 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1247 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
  1248 		$1, __SourceHook_ParamSizesM_##hookname); \
  1249 
  1250 @]
  1251 
  1252 
  1253 //////////////////////////////////////////////////////////////////////////
  1254 // SH_CALL
  1255 
  1256 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1257 { \
  1258 	using namespace ::SourceHook; \
  1259 	\
  1260 	SH_GLOB_SHPTR->SetIgnoreHooks(SH_GLOB_PLUGPTR, m_VfnPtr); \
  1261 	RetType tmpret = (m_ThisPtr->*m_MFP)call; \
  1262 	SH_GLOB_SHPTR->ResetIgnoreHooks(SH_GLOB_PLUGPTR, m_VfnPtr); \
  1263 	return tmpret; \
  1264 }
  1265 
  1266 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
  1267 { \
  1268 	using namespace ::SourceHook; \
  1269 	\
  1270 	SH_GLOB_SHPTR->SetIgnoreHooks(SH_GLOB_PLUGPTR, m_VfnPtr); \
  1271 	(m_ThisPtr->*m_MFP)call; \
  1272 	SH_GLOB_SHPTR->ResetIgnoreHooks(SH_GLOB_PLUGPTR, m_VfnPtr); \
  1273 }
  1274 
  1275 namespace SourceHook
  1276 {
  1277 	// Call Class Wrapper!
  1278 	template <class T> struct CCW
  1279 	{
  1280 		typedef T type;
  1281 
  1282 		// Get Real Pointer!
  1283 		static inline T *GRP(T *p)
  1284 		{
  1285 			return p;
  1286 		}
  1287 	};
  1288 
  1289 	template <class T> struct CCW< LegacyCallClass<T> >
  1290 	{
  1291 		typedef T type;
  1292 
  1293 		// Get Real Pointer!
  1294 		static inline T *GRP(LegacyCallClass<T> *p)
  1295 		{
  1296 			return p->ptr;
  1297 		}
  1298 	};
  1299 
  1300 @[$1,0,$a:
  1301 	// Support for $1 arguments
  1302 	template<class ObjType, class MFP, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1303 	{
  1304 		ObjType *m_ThisPtr;
  1305 		void *m_VfnPtr;
  1306 		MFP m_MFP;
  1307 	public:
  1308 		ExecutableClass$1(ObjType *tp, MFP mfp, void *vp) : m_ThisPtr(tp), m_MFP(mfp), m_VfnPtr(vp) { }
  1309 	
  1310 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1311 			SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1312 	         
  1313 		@[$2,$1+1,$a:
  1314 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1315 			SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1316 		@]
  1317 	};
  1318 
  1319 	template<class ObjType, class MFP@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFP, void@[$2,1,$1:, Param$2@]>
  1320 	{
  1321 	   ObjType *m_ThisPtr;
  1322 	   void *m_VfnPtr;
  1323 	   MFP m_MFP;
  1324 	public:
  1325 	   ExecutableClass$1(ObjType *tp, MFP mfp, void *vp) : m_ThisPtr(tp), m_MFP(mfp), m_VfnPtr(vp) { }
  1326 	
  1327 	   void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1328 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1329 	         
  1330 	   @[$2,$1+1,$a:
  1331 	   template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1332 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1333 	   @]
  1334 	};
  1335 @]
  1336 
  1337 }
  1338 
  1339 #define SH__CALL_GET_VFNPTR_NORMAL \
  1340 	using namespace ::SourceHook; \
  1341 	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1342 	GetFuncInfo(CCW<Y>::GRP(ptr), mfp, mfi); \
  1343 	void *vfnptr = reinterpret_cast<void*>( \
  1344 		*reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
  1345 
  1346 #define SH__CALL_GET_VFNPTR_MANUAL \
  1347 	using namespace ::SourceHook; \
  1348 	void *vfnptr = reinterpret_cast<void*>( \
  1349 		*reinterpret_cast<void***>( (reinterpret_cast<char*>(CCW<Y>::GRP(ptr)) + thisptroffs + vtbloffs) ) + vtblidx); \
  1350 	/* patch mfp */ \
  1351 	*reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
  1352 
  1353 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1354 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1355 // The only purpose of the mfp2 parameter is to extract the return type
  1356 
  1357 @[$1,0,$a:
  1358 // Support for $1 arguments
  1359 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1360 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1361 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
  1362 {
  1363 	SH__CALL_GET_VFNPTR_NORMAL
  1364 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr), mfp, vfnptr);
  1365 }
  1366 
  1367 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1368 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1369 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
  1370 {
  1371 	SH__CALL_GET_VFNPTR_NORMAL
  1372 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr), mfp, vfnptr);
  1373 }
  1374 
  1375 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1376 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
  1377 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
  1378 {
  1379 	SH__CALL_GET_VFNPTR_MANUAL
  1380 	return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
  1381 		reinterpret_cast<SourceHook::EmptyClass*>(CCW<Y>::GRP(ptr)), mfp, vfnptr);
  1382 }
  1383 @]
  1384 
  1385 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1386 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1387 
  1388 @[$1,0,$a:
  1389 // Support for $1 arguments
  1390 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1391 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1392 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
  1393 {
  1394 	SH__CALL_GET_VFNPTR_NORMAL
  1395 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr), mfp, vfnptr);
  1396 }
  1397 
  1398 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1399 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1400 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
  1401 {
  1402 	SH__CALL_GET_VFNPTR_NORMAL
  1403 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr), mfp, vfnptr);
  1404 }
  1405 
  1406 @]
  1407 
  1408 #endif
  1409 
  1410 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
  1411 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs))
  1412 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1413 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1414 
  1415 #undef SH_MAKE_EXECUTABLECLASS_OB
  1416 
  1417 //////////////////////////////////////////////////////////////////////////
  1418 // SetOverrideRet and RecallGetIface for recalls
  1419 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1420 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1421 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1422 
  1423 namespace SourceHook
  1424 {
  1425 	template <class RetType>
  1426 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1427 	{
  1428 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1429 	}
  1430 
  1431 	// SetOverrideResult used to be implemented like this:
  1432 	//  SetOverrideResult(shptr, memfuncptr, return);
  1433 	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1434 	//  (it thought it was ambigous - the ref and non-ref type)
  1435 	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1436 	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1437 	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1438 
  1439 	template <class T> struct OverrideFunctor
  1440 	{
  1441 		void operator()(ISourceHook *shptr, T res)
  1442 		{
  1443 			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1444 		}
  1445 	};
  1446 	template <class T> struct OverrideFunctor<T&>
  1447 	{
  1448 		void operator()(ISourceHook *shptr, T &res)
  1449 		{
  1450 			// overrideretptr points to ReferenceCarrier<T&>
  1451 			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1452 		}
  1453 	};
  1454 @[$1,0,$a:
  1455 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1456 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1457 	{
  1458 		return OverrideFunctor<RetType>();
  1459 	}
  1460 
  1461 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1462 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1463 	{
  1464 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1465 	}
  1466 @]
  1467 }
  1468 
  1469 #endif
  1470 	// The pope is dead. -> :(