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