sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sat May 12 19:05:16 2007 +0000 (2007-05-12)
changeset 332 030b917a8dcc
parent 330 d801744b795c
child 333 3813f1ac0cfe
permissions -rwxr-xr-x
Better treatement of deprecated callclasses
     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 	// For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
   540 	//  These have to be able to return references. If T is a reference, the pointers returned
   541 	//  from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
   542 	template <class T> struct MacroRefHelpers
   543 	{
   544 		inline static const T* GetOrigRet(ISourceHook *shptr)
   545 		{
   546 			return reinterpret_cast<const T*>(shptr->GetOrigRet());
   547 		}
   548 		inline static const T* GetOverrideRet(ISourceHook *shptr)
   549 		{
   550 			return reinterpret_cast<const T*>(shptr->GetOverrideRet());
   551 		}
   552 	};
   553 
   554 	template <class T> struct MacroRefHelpers<T&>
   555 	{
   556 		inline static T* GetOrigRet(ISourceHook *shptr)
   557 		{
   558 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
   559 			return &ref;
   560 		}
   561 		inline static T* GetOverrideRet(ISourceHook *shptr)
   562 		{
   563 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
   564 			return &ref;
   565 		}
   566 	};
   567 
   568 	// For source-level compatibility
   569 	template <class T> struct CallClass
   570 	{
   571 		T *ptr;
   572 
   573 		CallClass(T *p) : ptr(p)
   574 		{
   575 		}
   576 
   577 		operator T*()
   578 		{
   579 			return ptr;
   580 		}
   581 	};
   582 
   583 	typedef CallClass<void> GenericCallClass;
   584 	typedef CallClass<EmptyClass> ManualCallClass;
   585 
   586 	template <class T>
   587 	CallClass<T> *GetCallClass(T *p)
   588 	{
   589 		return new CallClass<T>(p);
   590 	}
   591 
   592 	template <class T>
   593 	void ReleaseCallClass(CallClass<T> *p)
   594 	{
   595 		delete p;
   596 	}
   597 
   598 }
   599 
   600 /************************************************************************/
   601 /* High level interface                                                 */
   602 /************************************************************************/
   603 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   604 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   605 #define META_RESULT_ORIG_RET(type)			*SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
   606 #define META_RESULT_OVERRIDE_RET(type)		*SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
   607 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   608 
   609 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   610 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   611 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   612 
   613 // If a hook on a function which returns a reference does not want to specify a return value,
   614 // it can use this macro.
   615 //   ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
   616 #define RETURN_META_NOREF(result, rettype)	do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
   617 
   618 // only call these from the hook handlers directly!
   619 // :TODO: enforce it ?
   620 
   621 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   622 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   623 
   624 // SourceHook::SetOverrideRet is defined later.
   625 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   626 	do { \
   627 		SET_META_RESULT(result); \
   628 		SH_GLOB_SHPTR->DoRecall(); \
   629 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   630 		RETURN_META(MRES_SUPERCEDE); \
   631 	} while (0)
   632 
   633 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   634 	do { \
   635 		SET_META_RESULT(result); \
   636 		SH_GLOB_SHPTR->DoRecall(); \
   637 		if ((result) >= MRES_OVERRIDE) \
   638 		{ \
   639 			/* meh, set the override result here because we don't get a chance to return */ \
   640 			/* before continuing the hook loop through the recall */ \
   641 			SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
   642 		} \
   643 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   644 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   645 	} while (0)
   646 
   647 // :TODO: thisptroffs in MNEWPARAMS ??
   648 
   649 #if SH_COMP == SH_COMP_MSVC
   650 
   651 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   652 	union \
   653 	{ \
   654 		SH_MFHCls(hookname)::ECMFP mfp; \
   655 		void *addr; \
   656 	} u; \
   657 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   658 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   659 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   660 
   661 #elif SH_COMP == SH_COMP_GCC
   662 
   663 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   664 	union \
   665 	{ \
   666 		SH_MFHCls(hookname)::ECMFP mfp; \
   667 		struct \
   668 		{ \
   669 			void *addr; \
   670 			intptr_t adjustor; \
   671 		} s; \
   672 	} u; \
   673 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   674 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   675 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   676 	u.s.adjustor = 0;
   677 
   678 #endif
   679 
   680 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   681 	do { \
   682 		SET_META_RESULT(result); \
   683 		SH_GLOB_SHPTR->DoRecall(); \
   684 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   685 		(thisptr->*(u.mfp)) newparams; \
   686 		RETURN_META(MRES_SUPERCEDE); \
   687 	} while (0)
   688 
   689 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   690 	do { \
   691 		SET_META_RESULT(result); \
   692 		SH_GLOB_SHPTR->DoRecall(); \
   693 		if ((result) >= MRES_OVERRIDE) \
   694 		{ \
   695 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   696 			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   697 		} \
   698 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   699 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   700 	} while (0)
   701 
   702 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   703 	do { \
   704 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   705 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   706 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   707 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   708 	} while (0)
   709 
   710 // For source-level compatibility
   711 
   712 #define SH_GET_CALLCLASS(ptr) SourceHook::GetCallClass(ptr)
   713 #define SH_GET_MCALLCLASS(ptr, size) SourceHook::GetCallClass(reinterpret_cast<SourceHook::EmptyClass*>(ptr))
   714 #define SH_RELEASE_CALLCLASS(ptr) SourceHook::ReleaseCallClass(ptr)
   715 
   716 // New ADD / REMOVE macros.
   717 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
   718 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
   719 
   720 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   721 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   722 	post, handler)
   723 
   724 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   725 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   726 	post, handler) 
   727 
   728 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   729 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   730 
   731 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   732 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler) 
   733 
   734 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   735 	__SourceHook_FHVPAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   736 	post, handler, false)
   737 
   738 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, vtableptr, handler, post) \
   739 	__SourceHook_FHVPAdd##ifacetype##ifacefunc(reinterpret_cast<void*>(vtableptr), \
   740 	post, handler, true)
   741 
   742 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
   743 	__SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler, false)
   744 
   745 #define SH_ADD_MANUALDVPHOOK(hookname, vtableptr, handler, post) \
   746 	__SourceHook_FHMVPAdd##hookname(reinterpret_cast<void*>(vtableptr), post, handler, true)
   747 
   748 #define SH_REMOVE_HOOK_ID(hookid) \
   749 	(SH_GLOB_SHPTR->RemoveHookByID(SH_GLOB_PLUGPTR, hookid))
   750 
   751 // Old macros
   752 // !! These are now deprecated. Instead, use one of these:
   753 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   754 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   755 
   756 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   757 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   758 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   759 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   760 
   761 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   762 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   763 
   764 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   765 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   766 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   767 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   768 
   769 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   770 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   771 
   772 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   773 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   774 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   775 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   776 
   777 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   778 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   779 
   780 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   781 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   782 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   783 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)  
   784 
   785 #define SH_NOATTRIB
   786 
   787 
   788 
   789 
   790 #if SH_COMP == SH_COMP_MSVC
   791 # define SH_SETUP_MFP(mfp) \
   792 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   793 #elif SH_COMP == SH_COMP_GCC
   794 # define SH_SETUP_MFP(mfp) \
   795 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   796 	reinterpret_cast<void**>(&mfp)[1] = 0;
   797 #else
   798 # error Not supported yet.
   799 #endif
   800 
   801 //////////////////////////////////////////////////////////////////////////
   802 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   803 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   804 
   805 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   806 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   807 	{ \
   808 		GetFuncInfo(funcptr, ms_MFI); \
   809 	} \
   810 	\
   811 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   812 	{ \
   813 		using namespace ::SourceHook; \
   814 		GetFuncInfo(funcptr, ms_MFI); \
   815 		/* Verify interface version */ \
   816 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   817 			return 1; \
   818 		if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   819 			return 1; \
   820 		\
   821 		if (action == HA_GetInfo) \
   822 		{ \
   823 			param->SetVersion(SH_HOOKMAN_VERSION); \
   824 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   825 				reinterpret_cast<const char*>(&ms_Proto)); \
   826 			\
   827 			MemFuncInfo mfi = {true, -1, 0, 0}; \
   828 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   829 			param->SetHookfuncVfnptr( \
   830 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   831 			return 0; \
   832 		} \
   833 		else if (action == HA_Register) \
   834 		{ \
   835 			ms_HI = param; \
   836 			return 0; \
   837 		} \
   838 		else if (action == HA_Unregister) \
   839 		{ \
   840 			ms_HI = NULL; \
   841 			return 0; \
   842 		} \
   843 		else \
   844 			return 1; \
   845 	}
   846 
   847 // It has to be possible to use the macros in namespaces
   848 // -> So we need to access and extend the global SourceHook namespace
   849 // We use a namespace alias for this
   850 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   851 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   852 	{ \
   853 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   854 		static ::SourceHook::MemFuncInfo ms_MFI; \
   855 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   856 		static ::SourceHook::ProtoInfo ms_Proto; \
   857 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   858 
   859 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   860 	}; \
   861 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   862 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   863 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   864 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   865 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   866 	{ \
   867 		using namespace ::SourceHook; \
   868 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   869 		GetFuncInfo(funcptr, mfi); \
   870 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   871 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   872 		\
   873 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, mfi.thisptroffs, \
   874 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   875 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   876 	} \
   877 	int __SourceHook_FHVPAdd##ifacetype##ifacefunc(void *iface, bool post, \
   878 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler, bool direct) \
   879 	{ \
   880 		using namespace ::SourceHook; \
   881 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   882 		GetFuncInfo(funcptr, mfi); \
   883 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   884 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   885 		\
   886 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
   887 			direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
   888 			iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   889 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   890 	} \
   891 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   892 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   893 	{ \
   894 		using namespace ::SourceHook; \
   895 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   896 		GetFuncInfo(funcptr, mfi); \
   897 		if (mfi.thisptroffs < 0) \
   898 			return false; /* No virtual inheritance supported */ \
   899 		\
   900 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   901 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   902 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   903 	} \
   904 
   905 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   906 	struct SH_MFHCls(hookname) \
   907 	{ \
   908 		static SH_MFHCls(hookname) ms_Inst; \
   909 		static ::SourceHook::MemFuncInfo ms_MFI; \
   910 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   911 		static ::SourceHook::ProtoInfo ms_Proto; \
   912 		\
   913 		SH_MFHCls(hookname)() \
   914 		{ \
   915 			ms_MFI.isVirtual = true; \
   916 			ms_MFI.thisptroffs = pthisptroffs; \
   917 			ms_MFI.vtblindex = pvtblidx; \
   918 			ms_MFI.vtbloffs = pvtbloffs; \
   919 		} \
   920 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   921 		{ \
   922 			using namespace ::SourceHook; \
   923 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   924 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   925 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   926 			/* Verify interface version */ \
   927 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   928 				return 1; \
   929 			if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   930 				return 1; \
   931 			\
   932 			if (action == HA_GetInfo) \
   933 			{ \
   934 				param->SetVersion(SH_HOOKMAN_VERSION); \
   935 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   936 					reinterpret_cast<const char*>(&ms_Proto)); \
   937 				\
   938 				MemFuncInfo mfi = {true, -1, 0, 0}; \
   939 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   940 				param->SetHookfuncVfnptr( \
   941 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   942 				return 0; \
   943 			} \
   944 			else if (action == HA_Register) \
   945 			{ \
   946 				ms_HI = param; \
   947 				return 0; \
   948 			} \
   949 			else if (action == HA_Unregister) \
   950 			{ \
   951 				ms_HI = NULL; \
   952 				return 0; \
   953 			} \
   954 			else \
   955 				return 1; \
   956 		}
   957 
   958 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   959 	}; \
   960 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   961 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   962 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   963 	int __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   964 		SH_MFHCls(hookname)::FD handler) \
   965 	{ \
   966 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, ::SourceHook::ISourceHook::Hook_Normal, iface, pthisptroffs, \
   967 			SH_MFHCls(hookname)::HookManPubFunc, \
   968 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   969 	} \
   970 	int __SourceHook_FHMVPAdd##hookname(void *iface, bool post, \
   971 		SH_MFHCls(hookname)::FD handler, bool direct) \
   972 	{ \
   973 		return SH_GLOB_SHPTR->AddHookNew(SH_GLOB_PLUGPTR, \
   974 			direct ? ::SourceHook::ISourceHook::Hook_DVP : ::SourceHook::ISourceHook::Hook_VP, \
   975 			iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
   976 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   977 	} \
   978 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   979 		SH_MFHCls(hookname)::FD handler) \
   980 	{ \
   981 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   982 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   983 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   984 	} \
   985 
   986 
   987 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   988 	/* 1) Find the vfnptr */ \
   989 	using namespace ::SourceHook; \
   990 	void *ourvfnptr = reinterpret_cast<void*>( \
   991 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   992 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   993 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   994 	\
   995 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   996 	/* ... and the iface */ \
   997 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   998 	if (!ifinfo) \
   999 	{ \
  1000 		/* The iface info was not found. Redirect the call to the original function. */ \
  1001 		rettype (EmptyClass::*mfp)paramtypes; \
  1002 		SH_SETUP_MFP(mfp); \
  1003 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1004 	} \
  1005 	/* 2) Declare some vars and set it up */ \
  1006 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
  1007 	IHookList *prelist = ifinfo->GetPreHooks(); \
  1008 	IHookList *postlist = ifinfo->GetPostHooks(); \
  1009 	META_RES status = MRES_IGNORED; \
  1010 	META_RES prev_res; \
  1011 	META_RES cur_res; \
  1012 	typedef ReferenceCarrier<rettype>::type my_rettype; \
  1013 	my_rettype orig_ret; \
  1014 	my_rettype override_ret; \
  1015 	my_rettype plugin_ret; \
  1016 	void* ifptr; \
  1017 	my_rettype *pOverrideRet = reinterpret_cast<my_rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
  1018 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
  1019 
  1020 #define SH_CALL_HOOKS(post, params) \
  1021 	if (SH_GLOB_SHPTR->ShouldContinue()) \
  1022 	{ \
  1023 		prev_res = MRES_IGNORED; \
  1024 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
  1025 		{ \
  1026 			cur_res = MRES_IGNORED; \
  1027 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
  1028 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
  1029 			prev_res = cur_res; \
  1030 			if (cur_res > status) \
  1031 				status = cur_res; \
  1032 			if (cur_res >= MRES_OVERRIDE) \
  1033 				*pOverrideRet = plugin_ret; \
  1034 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
  1035 			{ \
  1036 				iter.SetToZero(); \
  1037 				break; \
  1038 			} \
  1039 		} \
  1040 	}
  1041 
  1042 #define SH_CALL_ORIG(rettype, paramtypes, params) \
  1043 	if (status != MRES_SUPERCEDE) \
  1044 	{ \
  1045 		rettype (EmptyClass::*mfp)paramtypes; \
  1046 		SH_SETUP_MFP(mfp); \
  1047 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1048 	} \
  1049 	else \
  1050 		orig_ret = override_ret; \
  1051 
  1052 #define SH_RETURN() \
  1053 	SH_GLOB_SHPTR->HookLoopEnd(); \
  1054 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
  1055 
  1056 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
  1057 	SH_SETUPCALLS(rettype, paramtypes, params) \
  1058 	SH_CALL_HOOKS(pre, params) \
  1059 	SH_CALL_ORIG(rettype, paramtypes, params) \
  1060 	SH_CALL_HOOKS(post, params) \
  1061 	SH_RETURN()
  1062 
  1063 //////////////////////////////////////////////////////////////////////////
  1064 #define SH_SETUPCALLS_void(paramtypes, params) \
  1065 	/* 1) Find the vfnptr */ \
  1066 	using namespace ::SourceHook; \
  1067 	void *ourvfnptr = reinterpret_cast<void*>( \
  1068 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
  1069 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
  1070 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
  1071 	\
  1072 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
  1073 	/* ... and the iface */ \
  1074 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
  1075 	if (!ifinfo) \
  1076 	{ \
  1077 		/* The iface info was not found. Redirect the call to the original function. */ \
  1078 		void (EmptyClass::*mfp)paramtypes; \
  1079 		SH_SETUP_MFP(mfp); \
  1080 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1081 		return; \
  1082 	} \
  1083 	/* 2) Declare some vars and set it up */ \
  1084 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
  1085 	IHookList *prelist = ifinfo->GetPreHooks(); \
  1086 	IHookList *postlist = ifinfo->GetPostHooks(); \
  1087 	META_RES status = MRES_IGNORED; \
  1088 	META_RES prev_res; \
  1089 	META_RES cur_res; \
  1090 	void* ifptr; \
  1091 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
  1092 
  1093 #define SH_CALL_HOOKS_void(post, params) \
  1094 	if (SH_GLOB_SHPTR->ShouldContinue()) \
  1095 	{ \
  1096 		prev_res = MRES_IGNORED; \
  1097 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
  1098 		{ \
  1099 			cur_res = MRES_IGNORED; \
  1100 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
  1101 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
  1102 			prev_res = cur_res; \
  1103 			if (cur_res > status) \
  1104 				status = cur_res; \
  1105 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
  1106 			{ \
  1107 				iter.SetToZero(); \
  1108 				break; \
  1109 			} \
  1110 		} \
  1111 	}
  1112 
  1113 #define SH_CALL_ORIG_void(paramtypes, params) \
  1114 	if (status != MRES_SUPERCEDE) \
  1115 	{ \
  1116 		void (EmptyClass::*mfp)paramtypes; \
  1117 		SH_SETUP_MFP(mfp); \
  1118 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
  1119 	}
  1120 
  1121 #define SH_RETURN_void() \
  1122 	SH_GLOB_SHPTR->HookLoopEnd();
  1123 
  1124 #define SH_HANDLEFUNC_void(paramtypes, params) \
  1125 	SH_SETUPCALLS_void(paramtypes, params) \
  1126 	SH_CALL_HOOKS_void(pre, params) \
  1127 	SH_CALL_ORIG_void(paramtypes, params) \
  1128 	SH_CALL_HOOKS_void(post, params) \
  1129 	SH_RETURN_void()
  1130 
  1131 
  1132 // Special vafmt handlers
  1133 // :TODO: what
  1134 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
  1135 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
  1136 	SH_CALL_HOOKS(pre, params_plug) \
  1137 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
  1138 	SH_CALL_HOOKS(post, params_plug) \
  1139 	SH_RETURN()
  1140 
  1141 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
  1142 	SH_SETUPCALLS_void(paramtypes, params_orig) \
  1143 	SH_CALL_HOOKS_void(pre, params_plug) \
  1144 	SH_CALL_ORIG_void(paramtypes, params_orig) \
  1145 	SH_CALL_HOOKS_void(post, params_plug) \
  1146 	SH_RETURN_void()
  1147 
  1148 //////////////////////////////////////////////////////////////////////////
  1149 
  1150 // :FIXME:
  1151 //  sizeof on references returns the size of the datatype, NOT the pointer size or something
  1152 //  -> one should probably flag references in __SourceHook_ParamSizes_* !
  1153 //		or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
  1154 
  1155 @[$1,0,$a:
  1156 // ********* Support for $1 arguments *********
  1157 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1158 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1159 		(&ifacetype::ifacefunc))) \
  1160 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1161 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1162 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1163 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1164 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1165 	\
  1166 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1167 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1168 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1169 
  1170 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1171 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1172 		(&ifacetype::ifacefunc))) \
  1173 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1174 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1175 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1176 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1177 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1178 	\
  1179 	\
  1180 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1181 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1182 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1183 
  1184 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1185 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1186 		(&ifacetype::ifacefunc))) \
  1187 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
  1188 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1189 		{ \
  1190 			char buf[::SourceHook::STRBUF_LEN]; \
  1191 			va_list ap; \
  1192 			va_start(ap, fmt); \
  1193 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1194 			va_end(ap); \
  1195 			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); \
  1196 		} \
  1197 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1198 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1199 	\
  1200 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1201 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
  1202 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1203 
  1204 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1205 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1206 		(&ifacetype::ifacefunc))) \
  1207 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1208 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1209 		{ \
  1210 			char buf[::SourceHook::STRBUF_LEN]; \
  1211 			va_list ap; \
  1212 			va_start(ap, fmt); \
  1213 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1214 			va_end(ap); \
  1215 			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)); \
  1216 		} \
  1217 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1218 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1219 	\
  1220 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1221 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1222 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1223 
  1224 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1225 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1226 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1227 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1228 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1229 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1230 		typedef rettype RetType; \
  1231 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1232 	\
  1233 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1234 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
  1235 		$1, __SourceHook_ParamSizesM_##hookname); \
  1236 
  1237 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1238 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1239 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1240 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1241 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1242 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1243 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1244 	\
  1245 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1246 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
  1247 		$1, __SourceHook_ParamSizesM_##hookname); \
  1248 
  1249 @]
  1250 
  1251 
  1252 //////////////////////////////////////////////////////////////////////////
  1253 // SH_CALL
  1254 
  1255 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1256 { \
  1257 	using namespace ::SourceHook; \
  1258 	\
  1259 	m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
  1260 	RetType tmpret = (m_ThisPtr->*m_MFP)call; \
  1261 	m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
  1262 	return tmpret; \
  1263 }
  1264 
  1265 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
  1266 { \
  1267 	using namespace ::SourceHook; \
  1268 	\
  1269 	m_pSH->SetIgnoreHooks(m_Plug, m_VfnPtr); \
  1270 	(m_ThisPtr->*m_MFP)call; \
  1271 	m_pSH->ResetIgnoreHooks(m_Plug, m_VfnPtr); \
  1272 }
  1273 
  1274 namespace SourceHook
  1275 {
  1276 	// Call Class Wrapper!
  1277 	template <class T> struct CCW
  1278 	{
  1279 		typedef T type;
  1280 
  1281 		// Get Real Pointer!
  1282 		static inline T *GRP(T *p)
  1283 		{
  1284 			return p;
  1285 		}
  1286 	};
  1287 
  1288 	template <class T> struct CCW< CallClass<T> >
  1289 	{
  1290 		typedef T type;
  1291 
  1292 		// Get Real Pointer!
  1293 		static inline T *GRP(CallClass<T> *p)
  1294 		{
  1295 			return p->ptr;
  1296 		}
  1297 	};
  1298 
  1299 @[$1,0,$a:
  1300 	// Support for $1 arguments
  1301 	template<class ObjType, class MFP, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1302 	{
  1303 		ObjType *m_ThisPtr;
  1304 		void *m_VfnPtr;
  1305 		MFP m_MFP;
  1306 		ISourceHook *m_pSH;
  1307 		Plugin m_Plug;
  1308 	public:
  1309 		ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
  1310 			m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
  1311 	
  1312 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1313 			SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1314 	         
  1315 		@[$2,$1+1,$a:
  1316 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1317 			SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1318 		@]
  1319 	};
  1320 
  1321 	template<class ObjType, class MFP@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFP, void@[$2,1,$1:, Param$2@]>
  1322 	{
  1323 	   ObjType *m_ThisPtr;
  1324 	   void *m_VfnPtr;
  1325 	   MFP m_MFP;
  1326 	   ISourceHook *m_pSH;
  1327 	   Plugin m_Plug;
  1328 	public:
  1329 	   ExecutableClass$1(ObjType *tp, MFP mfp, void *vp, ISourceHook *pSH, Plugin plug) : m_ThisPtr(tp),
  1330 		   m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH), m_Plug(plug) { }
  1331 	
  1332 	   void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1333 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1334 	         
  1335 	   @[$2,$1+1,$a:
  1336 	   template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1337 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1338 	   @]
  1339 	};
  1340 @]
  1341 
  1342 }
  1343 
  1344 #define SH__CALL_GET_VFNPTR_NORMAL \
  1345 	using namespace ::SourceHook; \
  1346 	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1347 	GetFuncInfo(CCW<Y>::GRP(ptr), mfp, mfi); \
  1348 	void *vfnptr = reinterpret_cast<void*>( \
  1349 		*reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
  1350 
  1351 #define SH__CALL_GET_VFNPTR_MANUAL \
  1352 	using namespace ::SourceHook; \
  1353 	void *vfnptr = reinterpret_cast<void*>( \
  1354 		*reinterpret_cast<void***>( (reinterpret_cast<char*>(CCW<Y>::GRP(ptr)) + thisptroffs + vtbloffs) ) + vtblidx); \
  1355 	/* patch mfp */ \
  1356 	*reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
  1357 
  1358 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1359 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1360 // The only purpose of the mfp2 parameter is to extract the return type
  1361 
  1362 @[$1,0,$a:
  1363 // Support for $1 arguments
  1364 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1365 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1366 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1367 {
  1368 	SH__CALL_GET_VFNPTR_NORMAL
  1369 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1370 		mfp, vfnptr, shptr, plug);
  1371 }
  1372 
  1373 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1374 SourceHook::ExecutableClass$1<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1375 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1376 {
  1377 	SH__CALL_GET_VFNPTR_NORMAL
  1378 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1379 		mfp, vfnptr, shptr, plug);
  1380 }
  1381 
  1382 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1383 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
  1384 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)
  1385 {
  1386 	SH__CALL_GET_VFNPTR_MANUAL
  1387 	return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
  1388 		reinterpret_cast<SourceHook::EmptyClass*>(CCW<Y>::GRP(ptr)), mfp, vfnptr, shptr, plug);
  1389 }
  1390 @]
  1391 
  1392 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1393 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1394 
  1395 @[$1,0,$a:
  1396 // Support for $1 arguments
  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@]@[$1!=0:, @]...), 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<typename SourceHook::CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>
  1408 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr, SourceHook::Plugin plug)
  1409 {
  1410 	SH__CALL_GET_VFNPTR_NORMAL
  1411 	return SourceHook::ExecutableClass$1<typename CCW<Y>::type, MFP, RetType@[$2,1,$1:, Param$2@]>(CCW<Y>::GRP(ptr),
  1412 		mfp, vfnptr, shptr, plug);
  1413 }
  1414 
  1415 @]
  1416 
  1417 #endif
  1418 
  1419 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
  1420 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR, SH_GLOB_PLUGPTR)
  1421 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1422 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1423 
  1424 #undef SH_MAKE_EXECUTABLECLASS_OB
  1425 
  1426 //////////////////////////////////////////////////////////////////////////
  1427 // SetOverrideRet and RecallGetIface for recalls
  1428 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1429 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1430 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1431 
  1432 namespace SourceHook
  1433 {
  1434 	template <class RetType>
  1435 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1436 	{
  1437 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1438 	}
  1439 
  1440 	// SetOverrideResult used to be implemented like this:
  1441 	//  SetOverrideResult(shptr, memfuncptr, return);
  1442 	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1443 	//  (it thought it was ambigous - the ref and non-ref type)
  1444 	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1445 	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1446 	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1447 
  1448 	template <class T> struct OverrideFunctor
  1449 	{
  1450 		void operator()(ISourceHook *shptr, T res)
  1451 		{
  1452 			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1453 		}
  1454 	};
  1455 	template <class T> struct OverrideFunctor<T&>
  1456 	{
  1457 		void operator()(ISourceHook *shptr, T &res)
  1458 		{
  1459 			// overrideretptr points to ReferenceCarrier<T&>
  1460 			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1461 		}
  1462 	};
  1463 @[$1,0,$a:
  1464 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1465 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1466 	{
  1467 		return OverrideFunctor<RetType>();
  1468 	}
  1469 
  1470 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1471 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1472 	{
  1473 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1474 	}
  1475 @]
  1476 }
  1477 
  1478 #endif
  1479 	// The pope is dead. -> :(