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