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