core/sourcehook/generate/sourcehook.hxx
author David Anderson <dvander@alliedmods.net>
Tue Nov 18 04:44:36 2008 -0600 (2008-11-18)
changeset 571 a9eeed1bcb50
parent 539 db914129ae01
child 680 39d0803bdb6e
permissions -rwxr-xr-x
More version.rc fixes.
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2008 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 /**
    12 *	@file sourcehook.h
    13 *	@brief Contains the public SourceHook API
    14 */
    15 
    16 #ifndef __SOURCEHOOK_H__
    17 #define __SOURCEHOOK_H__
    18 
    19 // Interface revisions:
    20 //  1 - Initial revision
    21 //  2 - Changed to virtual functions for iterators and all queries
    22 //  3 - Added "hook loop status variable"
    23 //  4 - Reentrant
    24 //  5 - New "V2" interface
    25 #define SH_IFACE_VERSION 5
    26 
    27 // Impl versions:
    28 // ???
    29 // 4 - addition of hook ids and vp hooks (with them, AddHookNew and RemoveHookNew)
    30 //     This is not a SH_IFACE_VERSION change so that old plugins continue working!
    31 // 5 - implementation of the new "V2" interface
    32 #define SH_IMPL_VERSION 5
    33 
    34 // Hookman version:
    35 // 1 - standard
    36 #define SH_HOOKMAN_VERSION 1
    37 
    38 // Hookmanautogen versions
    39 //  1 - initial
    40 #define SH_HOOKMANAUTOGEN_IFACE_VERSION 1
    41 #define SH_HOOKMANAUTOGEN_IMPL_VERSION 1
    42 
    43 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
    44 // It's used in various macros
    45 #ifndef SH_GLOB_SHPTR
    46 #define SH_GLOB_SHPTR g_SHPtr
    47 #endif
    48 
    49 // Used to identify the current plugin
    50 #ifndef SH_GLOB_PLUGPTR
    51 #define SH_GLOB_PLUGPTR g_PLID
    52 #endif
    53 
    54 #ifdef SH_DEBUG
    55 # include <stdio.h>
    56 # include <stdlib.h>
    57 
    58 # define SH_ASSERT(x, info) \
    59 	do { \
    60 		if (!(x)) \
    61 		{ \
    62 			printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
    63 			printf info; \
    64 			putchar('\n'); \
    65 			abort(); \
    66 		} \
    67 	} while(0)
    68 
    69 #else
    70 # define SH_ASSERT(x, info)
    71 #endif
    72 
    73 // System
    74 #define SH_SYS_WIN32 1
    75 #define SH_SYS_LINUX 2
    76 
    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 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   614 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   615 
   616 #elif SH_COMP == SH_COMP_GCC
   617 
   618 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   619 	union \
   620 	{ \
   621 		SH_MFHCls(hookname)::ECMFP mfp; \
   622 		struct \
   623 		{ \
   624 			void *addr; \
   625 			intptr_t adjustor; \
   626 		} s; \
   627 	} u; \
   628 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   629 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   630 	u.s.adjustor = 0;
   631 
   632 #endif
   633 
   634 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   635 	do { \
   636 		SET_META_RESULT(result); \
   637 		SH_GLOB_SHPTR->DoRecall(); \
   638 		SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   639 		(thisptr->*(__SoureceHook_FHM_GetRecallMFP##hookname(thisptr))) newparams; \
   640 		RETURN_META(MRES_SUPERCEDE); \
   641 	} while (0)
   642 
   643 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   644 	do { \
   645 		SET_META_RESULT(result); \
   646 		SH_GLOB_SHPTR->DoRecall(); \
   647 		if ((result) >= MRES_OVERRIDE) \
   648 		{ \
   649 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   650 			__SoureceHook_FHM_SetOverrideResult##hookname(SH_GLOB_SHPTR, value); \
   651 		} \
   652 		SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   653 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(__SoureceHook_FHM_GetRecallMFP##hookname(thisptr))) newparams); \
   654 	} while (0)
   655 
   656 
   657 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   658 	__SourceHook_FHM_Reconfigure##hookname(pvtblindex, pvtbloffs, pthisptroffs)
   659 
   660 #define SH_GET_ORIG_VFNPTR_ENTRY(inst, mfp) (SourceHook::GetOrigVfnPtrEntry(inst, mfp, SH_GLOB_SHPTR))
   661 
   662 // New ADD / REMOVE macros.
   663 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
   664 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
   665 
   666 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   667 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   668 	SourceHook::ISourceHook::Hook_Normal, post, handler)
   669 
   670 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   671 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   672 	post, handler) 
   673 
   674 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   675 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), \
   676 	SourceHook::ISourceHook::Hook_Normal, post, handler)
   677 
   678 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   679 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler) 
   680 
   681 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   682 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   683 	SourceHook::ISourceHook::Hook_VP, post, handler)
   684 
   685 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   686 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   687 	SourceHook::ISourceHook::Hook_DVP, post, handler)
   688 
   689 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
   690 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_VP, post, handler) 
   691 
   692 #define SH_ADD_MANUALDVPHOOK(hookname, ifaceptr, handler, post) \
   693 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_DVP, post, handler) 
   694 
   695 #define SH_REMOVE_HOOK_ID(hookid) \
   696 	(SH_GLOB_SHPTR->RemoveHookByID(hookid))
   697 
   698 // Old macros
   699 // !! These are now deprecated. Instead, use one of these:
   700 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   701 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   702 
   703 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   704 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   705 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   706 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   707 
   708 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   709 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   710 
   711 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   712 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   713 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   714 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   715 
   716 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   717 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   718 
   719 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   720 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   721 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   722 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   723 
   724 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   725 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   726 
   727 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   728 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   729 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   730 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)  
   731 
   732 #define SH_NOATTRIB
   733 
   734 
   735 
   736 
   737 #if SH_COMP == SH_COMP_MSVC
   738 # define SH_SETUP_MFP(mfp) \
   739 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   740 #elif SH_COMP == SH_COMP_GCC
   741 # define SH_SETUP_MFP(mfp) \
   742 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   743 	reinterpret_cast<void**>(&mfp)[1] = 0;
   744 #else
   745 # error Not supported yet.
   746 #endif
   747 
   748 //////////////////////////////////////////////////////////////////////////
   749 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   750 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   751 
   752 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   753 	static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
   754 	{ \
   755 		using namespace ::SourceHook; \
   756 		GetFuncInfo(funcptr, ms_MFI); \
   757 		/* Verify interface version */ \
   758 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   759 			return 1; \
   760 		if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   761 			return 1; \
   762 		if (store) \
   763 			ms_HI = hi; \
   764 		if (hi) \
   765 		{ \
   766 			MemFuncInfo mfi = {true, -1, 0, 0}; \
   767 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   768 			hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
   769 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
   770 		} \
   771 		return 0; \
   772 	}
   773 
   774 // It has to be possible to use the macros in namespaces
   775 // -> So we need to access and extend the global SourceHook namespace
   776 // We use a namespace alias for this
   777 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   778 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   779 	{ \
   780 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   781 		static ::SourceHook::MemFuncInfo ms_MFI; \
   782 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   783 		static ::SourceHook::ProtoInfo ms_Proto; \
   784 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   785 
   786 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   787 	}; \
   788 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   789 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   790 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   791 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
   792 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   793 	{ \
   794 		using namespace ::SourceHook; \
   795 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   796 		GetFuncInfo(funcptr, mfi); \
   797 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   798 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   799 		\
   800 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
   801 			iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   802 			new SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl(handler), post); \
   803 	} \
   804 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   805 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   806 	{ \
   807 		using namespace ::SourceHook; \
   808 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   809 		GetFuncInfo(funcptr, mfi); \
   810 		SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl tmp(handler); \
   811 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   812 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   813 	} \
   814 
   815 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   816 	struct SH_MFHCls(hookname) \
   817 	{ \
   818 		static SH_MFHCls(hookname) ms_Inst; \
   819 		static ::SourceHook::MemFuncInfo ms_MFI; \
   820 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   821 		static ::SourceHook::ProtoInfo ms_Proto; \
   822 		\
   823 		SH_MFHCls(hookname)() \
   824 		{ \
   825 			ms_MFI.isVirtual = true; \
   826 			ms_MFI.thisptroffs = pthisptroffs; \
   827 			ms_MFI.vtblindex = pvtblidx; \
   828 			ms_MFI.vtbloffs = pvtbloffs; \
   829 		} \
   830 		static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
   831 		{ \
   832 			using namespace ::SourceHook; \
   833 			/* Verify interface version */ \
   834 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   835 				return 1; \
   836 			if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   837 				return 1; \
   838 			if (store) \
   839 				ms_HI = hi; \
   840 			if (hi) \
   841 			{ \
   842 				MemFuncInfo mfi = {true, -1, 0, 0}; \
   843 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   844 				hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
   845 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
   846 			} \
   847 			return 0; \
   848 		}
   849 
   850 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   851 	}; \
   852 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   853 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   854 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   855 	int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
   856 		SH_MFHCls(hookname)::FD handler) \
   857 	{ \
   858 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
   859 			iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
   860 			new SH_MFHCls(hookname)::CMyDelegateImpl(handler), post); \
   861 	} \
   862 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   863 		SH_MFHCls(hookname)::FD handler) \
   864 	{ \
   865 		SH_MFHCls(hookname)::CMyDelegateImpl tmp(handler); \
   866 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   867 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   868 	} \
   869 	SH_MFHCls(hookname)::ECMFP __SoureceHook_FHM_GetRecallMFP##hookname(::SourceHook::EmptyClass *thisptr) \
   870 	{ \
   871 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   872 		return u.mfp; \
   873 	} \
   874 	SH_MFHCls(hookname)::CallEC __SoureceHook_FHM_SHCall##hookname(void *ptr) \
   875 	{ \
   876 		SH_MFHCls(hookname)::ECMFP mfp; \
   877 		void *vfnptr = reinterpret_cast<void*>( \
   878 			*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); \
   879 		/* patch mfp */ \
   880 		*reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr); \
   881 		if (sizeof(mfp) == 2*sizeof(void*)) /* gcc */ \
   882 			*(reinterpret_cast<void**>(&mfp) + 1) = 0; \
   883 		return SH_MFHCls(hookname)::CallEC(reinterpret_cast< ::SourceHook::EmptyClass* >(ptr), mfp, vfnptr, SH_GLOB_SHPTR); \
   884 	} \
   885 	void __SourceHook_FHM_Reconfigure##hookname(int p_vtblindex, int p_vtbloffs, int p_thisptroffs) \
   886 	{ \
   887 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   888 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = p_thisptroffs; \
   889 		SH_MFHCls(hookname)::ms_MFI.vtblindex = p_vtblindex; \
   890 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = p_vtbloffs; \
   891 	}
   892 
   893 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   894 	/* 1) Set up */ \
   895 	using namespace ::SourceHook; \
   896 	void *ourvfnptr = reinterpret_cast<void*>( \
   897 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   898 	void *vfnptr_origentry; \
   899 	\
   900 	META_RES status = MRES_IGNORED; \
   901 	META_RES prev_res; \
   902 	META_RES cur_res; \
   903 	\
   904 	typedef ReferenceCarrier< rettype >::type my_rettype; \
   905 	my_rettype orig_ret; \
   906 	my_rettype override_ret; \
   907 	my_rettype plugin_ret; \
   908 	IMyDelegate *iter; \
   909 	IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
   910 		&vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
   911 
   912 #define SH_CALL_HOOKS(post, params) \
   913 	prev_res = MRES_IGNORED; \
   914 	while ( (iter = static_cast<IMyDelegate*>(pContext->GetNext())) ) \
   915 	{ \
   916 		cur_res = MRES_IGNORED; \
   917 		plugin_ret = iter->Call params; \
   918 		prev_res = cur_res; \
   919 		if (cur_res > status) \
   920 			status = cur_res; \
   921 		if (cur_res >= MRES_OVERRIDE) \
   922 			*reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret; \
   923 	}
   924 
   925 #define SH_CALL_ORIG(rettype, paramtypes, params) \
   926 	if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
   927 	{ \
   928 		rettype (EmptyClass::*mfp)paramtypes; \
   929 		SH_SETUP_MFP(mfp); \
   930 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   931 	} \
   932 	else \
   933 		orig_ret = override_ret; /* :TODO: ??? : use pContext->GetOverrideRetPtr() or not? */
   934 
   935 #define SH_RETURN() \
   936 	const my_rettype *retptr = reinterpret_cast<const my_rettype*>( \
   937 		(status >= MRES_OVERRIDE) ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()); \
   938 	SH_GLOB_SHPTR->EndContext(pContext); \
   939 	return *retptr;
   940 
   941 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   942 	SH_SETUPCALLS(rettype, paramtypes, params) \
   943 	SH_CALL_HOOKS(pre, params) \
   944 	SH_CALL_ORIG(rettype, paramtypes, params) \
   945 	SH_CALL_HOOKS(post, params) \
   946 	SH_RETURN()
   947 
   948 //////////////////////////////////////////////////////////////////////////
   949 #define SH_SETUPCALLS_void(paramtypes, params) \
   950 	/* 1) Set up */ \
   951 	using namespace ::SourceHook; \
   952 	void *ourvfnptr = reinterpret_cast<void*>( \
   953 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   954 	void *vfnptr_origentry; \
   955 	\
   956 	META_RES status = MRES_IGNORED; \
   957 	META_RES prev_res; \
   958 	META_RES cur_res; \
   959 	\
   960 	IMyDelegate *iter; \
   961 	IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
   962 		&vfnptr_origentry, &status, &prev_res, &cur_res, NULL, NULL);
   963 
   964 #define SH_CALL_HOOKS_void(post, params) \
   965 	prev_res = MRES_IGNORED; \
   966 	while ( (iter = static_cast<IMyDelegate*>(pContext->GetNext())) ) \
   967 	{ \
   968 		cur_res = MRES_IGNORED; \
   969 		iter->Call params; \
   970 		prev_res = cur_res; \
   971 		if (cur_res > status) \
   972 			status = cur_res; \
   973 	}
   974 
   975 #define SH_CALL_ORIG_void(paramtypes, params) \
   976 	if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
   977 	{ \
   978 		void (EmptyClass::*mfp)paramtypes; \
   979 		SH_SETUP_MFP(mfp); \
   980 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   981 	}
   982 
   983 #define SH_RETURN_void() \
   984 	SH_GLOB_SHPTR->EndContext(pContext);
   985 
   986 #define SH_HANDLEFUNC_void(paramtypes, params) \
   987 	SH_SETUPCALLS_void(paramtypes, params) \
   988 	SH_CALL_HOOKS_void(pre, params) \
   989 	SH_CALL_ORIG_void(paramtypes, params) \
   990 	SH_CALL_HOOKS_void(post, params) \
   991 	SH_RETURN_void()
   992 
   993 
   994 // Special vafmt handlers
   995 // :TODO: what
   996 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
   997 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   998 	SH_CALL_HOOKS(pre, params_plug) \
   999 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
  1000 	SH_CALL_HOOKS(post, params_plug) \
  1001 	SH_RETURN()
  1002 
  1003 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
  1004 	SH_SETUPCALLS_void(paramtypes, params_orig) \
  1005 	SH_CALL_HOOKS_void(pre, params_plug) \
  1006 	SH_CALL_ORIG_void(paramtypes, params_orig) \
  1007 	SH_CALL_HOOKS_void(post, params_plug) \
  1008 	SH_RETURN_void()
  1009 
  1010 //////////////////////////////////////////////////////////////////////////
  1011 
  1012 #define MAKE_DELEG(ret_type, params_decl, params_pass) \
  1013 	struct IMyDelegate : ::SourceHook::ISHDelegate { virtual ret_type Call params_decl = 0; }; \
  1014 	struct CMyDelegateImpl : IMyDelegate \
  1015 	{ \
  1016 		FD m_Deleg; \
  1017 		CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
  1018 		ret_type Call params_decl { return m_Deleg params_pass; } \
  1019 		void DeleteThis() { delete this; } \
  1020 		bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
  1021 	};
  1022 
  1023 #define MAKE_DELEG_void(params_decl, params_pass) \
  1024 	struct IMyDelegate : ::SourceHook::ISHDelegate { virtual void  Call params_decl = 0; }; \
  1025 	struct CMyDelegateImpl : IMyDelegate \
  1026 	{ \
  1027 		FD m_Deleg; \
  1028 		CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
  1029 		void Call params_decl { m_Deleg params_pass; } \
  1030 		void DeleteThis() { delete this; } \
  1031 		bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
  1032 	};
  1033 
  1034 // GetPassInfo -> easier access. __SH_GPI generates a SourceHook::PassInfo instance.
  1035 #define __SH_GPI(tt) { sizeof(tt), ::SourceHook::GetPassInfo< tt >::type, ::SourceHook::GetPassInfo< tt >::flags }
  1036 // Extra pass info V2
  1037 #define __SH_EPI { 0, 0, 0, 0 }
  1038 
  1039 #define __SH_VAFMT_SEQ(fmt, buf) \
  1040 	char buf[::SourceHook::STRBUF_LEN]; \
  1041 	va_list ap; \
  1042 	va_start(ap, fmt); \
  1043 	vsnprintf(buf, sizeof(buf), fmt, ap); \
  1044 	buf[sizeof(buf) - 1] = 0; \
  1045 	va_end(ap);
  1046 
  1047 @[$1,0,$a:
  1048 // ********* Support for $1 arguments *********
  1049 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1050 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1051 		(&ifacetype::ifacefunc))) \
  1052 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1053 		MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
  1054 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1055 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1056 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1057 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1058 	\
  1059 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1060 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1061 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, __SH_GPI(rettype), \
  1062 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
  1063 
  1064 #define SH_DECL_EXTERN$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1065 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1066 		fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> handler); \
  1067 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
  1068 		fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> handler);
  1069 
  1070 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1071 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
  1072 		(&ifacetype::ifacefunc))) \
  1073 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1074 		MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
  1075 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1076 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1077 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1078 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
  1079 	\
  1080 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:,__SH_GPI(param$2)@] }; \
  1081 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1082 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
  1083 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
  1084 
  1085 #define SH_DECL_EXTERN$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1086 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1087 		fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler); \
  1088 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
  1089 		fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler);
  1090 
  1091 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1092 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1093 		(&ifacetype::ifacefunc))) \
  1094 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
  1095 		MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
  1096 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@] @[$1!=0:, @]const char *fmt, ...) \
  1097 		{ \
  1098 			__SH_VAFMT_SEQ(fmt, buf); \
  1099 			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); \
  1100 		} \
  1101 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1102 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1103 	\
  1104 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1105 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1106 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, __SH_GPI(rettype), \
  1107 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
  1108 
  1109 #define SH_DECL_EXTERN$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
  1110 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1111 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> handler); \
  1112 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
  1113 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> handler);
  1114 
  1115 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1116 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
  1117 		(&ifacetype::ifacefunc))) \
  1118 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1119 		MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
  1120 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1121 		{ \
  1122 			__SH_VAFMT_SEQ(fmt, buf); \
  1123 			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)); \
  1124 		} \
  1125 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1126 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1127 	\
  1128 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1129 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1130 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
  1131 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0, __SH_EPI, __SourceHook_ParamInfos2_##ifacetype##ifacefunc##overload };
  1132 
  1133 #define SH_DECL_EXTERN$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
  1134 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1135 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> handler); \
  1136 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
  1137 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> handler);
  1138 
  1139 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1140 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1141 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1142 		MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
  1143 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1144 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1145 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1146 		typedef SourceHook::ExecutableClass$1< ::SourceHook::EmptyClass, ECMFP, rettype@[$2,1,$1:, param$2@] > CallEC; \
  1147 		typedef rettype RetType; \
  1148 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1149 	\
  1150 	const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1151 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1152 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, __SH_GPI(rettype), \
  1153 		__SourceHook_ParamInfosM_##hookname, 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname }; \
  1154 	void __SoureceHook_FHM_SetOverrideResult##hookname(::SourceHook::ISourceHook *shptr, rettype value) \
  1155 	{ \
  1156 		::SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(shptr, value); \
  1157 	}
  1158 
  1159 #define SH_DECL_MANUALEXTERN$1(hookname, rettype@[$2,1,$1:, param$2@]) \
  1160 	int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1161 		fastdelegate::FastDelegate$1<@[$2,1,$1:param$2, @]rettype> handler); \
  1162 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
  1163 		fastdelegate::FastDelegate$1<@[$2,1,$1:param$2, @]rettype> handler); \
  1164 	rettype(::SourceHook::EmptyClass::* __SoureceHook_FHM_GetRecallMFP##hookname(::SourceHook::EmptyClass *thisptr) )(@[$2,1,$1|, :param$2@]); \
  1165 	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); \
  1166 	void __SoureceHook_FHM_SetOverrideResult##hookname(::SourceHook::ISourceHook *shptr, rettype res); \
  1167 	void __SourceHook_FHM_Reconfigure##hookname(int pvtblindex, int pvtbloffs, int pthisptroffs);
  1168 
  1169 #define SH_DECL_MANUALHOOK$1_vafmt(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1170 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1171 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1:param$2, @]const char *, rettype> FD; \
  1172 		MAKE_DELEG(rettype, (@[$2,1,$1:param$2 p$2, @]const char *px), (@[$2,1,$1:p$2, @]px)); \
  1173 		virtual rettype Func(@[$2,1,$1:param$2 p$2, @]const char *fmt, ...) \
  1174 		{ \
  1175 			__SH_VAFMT_SEQ(fmt, buf); \
  1176 			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); \
  1177 		} \
  1178 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1:param$2, @]const char *, ...); \
  1179 		typedef SourceHook::ExecutableClass@($1+1)< ::SourceHook::EmptyClass, ECMFP, rettype@[$2,1,$1:, param$2@], const char * > CallEC; \
  1180 		typedef rettype RetType; \
  1181 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1182 	\
  1183 	const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1184 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1185 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, __SH_GPI(rettype), \
  1186 		__SourceHook_ParamInfosM_##hookname, 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname }; \
  1187 	void __SoureceHook_FHM_SetOverrideResult##hookname(::SourceHook::ISourceHook *shptr, rettype value) \
  1188 	{ \
  1189 		::SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(shptr, value); \
  1190 	}
  1191 
  1192 #define SH_DECL_MANUALEXTERN$1_vafmt(hookname, rettype@[$2,1,$1:, param$2@]) \
  1193 	int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1194 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1:param$2, @]const char *, rettype> handler); \
  1195 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
  1196 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1:param$2, @]const char *, rettype> handler); \
  1197 	rettype(::SourceHook::EmptyClass::* __SoureceHook_FHM_GetRecallMFP##hookname(::SourceHook::EmptyClass *thisptr) )(@[$2,1,$1:param$2, @]const char *, ...); \
  1198 	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); \
  1199 	void __SoureceHook_FHM_SetOverrideResult##hookname(::SourceHook::ISourceHook *shptr, rettype res); \
  1200 	void __SourceHook_FHM_Reconfigure##hookname(int pvtblindex, int pvtbloffs, int pthisptroffs);
  1201 
  1202 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1203 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1204 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1205 		MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
  1206 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1207 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1208 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1209 		typedef SourceHook::ExecutableClass$1<SourceHook::EmptyClass, ECMFP, void@[$2,1,$1:, param$2@]> CallEC; \
  1210 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1211 	\
  1212 	const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1213 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1214 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
  1215 		__SourceHook_ParamInfosM_##hookname	, 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname };
  1216 
  1217 #define SH_DECL_MANUALEXTERN$1_void(hookname@[$2,1,$1:, param$2@]) \
  1218 	int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1219 		fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler); \
  1220 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
  1221 		fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> handler); \
  1222 	void(::SourceHook::EmptyClass::* __SoureceHook_FHM_GetRecallMFP##hookname(::SourceHook::EmptyClass *thisptr) )(@[$2,1,$1|, :param$2@]); \
  1223 	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); \
  1224 	void __SourceHook_FHM_Reconfigure##hookname(int pvtblindex, int pvtbloffs, int pthisptroffs);
  1225 
  1226 #define SH_DECL_MANUALHOOK$1_void_vafmt(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1227 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1228 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1:param$2, @]const char *> FD; \
  1229 		MAKE_DELEG_void((@[$2,1,$1:param$2 p$2, @]const char *px), (@[$2,1,$1:p$2, @]px)); \
  1230 		virtual void Func(@[$2,1,$1:param$2 p$2, @]const char *fmt, ...) \
  1231 		{ \
  1232 			__SH_VAFMT_SEQ(fmt, buf); \
  1233 			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)); \
  1234 		} \
  1235 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1:param$2, @]const char *, ...); \
  1236 		typedef SourceHook::ExecutableClass@($1+1)< ::SourceHook::EmptyClass, ECMFP, void@[$2,1,$1:, param$2@], const char * > CallEC; \
  1237 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1238 	\
  1239 	const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {1, 0, 0}@[$2,1,$1:, __SH_GPI(param$2)@] }; \
  1240 	const ::SourceHook::PassInfo::V2Info __SourceHook_ParamInfos2M_##hookname[] = { __SH_EPI@[$2,1,$1:, __SH_EPI@] }; \
  1241 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
  1242 		__SourceHook_ParamInfosM_##hookname	, 0, __SH_EPI, __SourceHook_ParamInfos2M_##hookname };
  1243 
  1244 #define SH_DECL_MANUALEXTERN$1_void_vafmt(hookname@[$2,1,$1:, param$2@]) \
  1245 	int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
  1246 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1:param$2, @]const char *> handler); \
  1247 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
  1248 		fastdelegate::FastDelegate@($1+1)<@[$2,1,$1:param$2, @]const char *> handler); \
  1249 	void(::SourceHook::EmptyClass::* __SoureceHook_FHM_GetRecallMFP##hookname(::SourceHook::EmptyClass *thisptr) )(@[$2,1,$1:param$2, @]const char *, ...); \
  1250 	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); \
  1251 	void __SourceHook_FHM_Reconfigure##hookname(int pvtblindex, int pvtbloffs, int pthisptroffs);
  1252 @]
  1253 
  1254 
  1255 //////////////////////////////////////////////////////////////////////////
  1256 // SH_CALL
  1257 
  1258 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1259 { \
  1260 	using namespace ::SourceHook; \
  1261 	\
  1262 	m_pSH->SetIgnoreHooks(m_VfnPtr); \
  1263 	RetType tmpret = (m_ThisPtr->*m_MFP)call; \
  1264 	m_pSH->ResetIgnoreHooks(m_VfnPtr); \
  1265 	return tmpret; \
  1266 }
  1267 
  1268 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
  1269 { \
  1270 	using namespace ::SourceHook; \
  1271 	\
  1272 	m_pSH->SetIgnoreHooks(m_VfnPtr); \
  1273 	(m_ThisPtr->*m_MFP)call; \
  1274 	m_pSH->ResetIgnoreHooks(m_VfnPtr); \
  1275 }
  1276 
  1277 namespace SourceHook
  1278 {
  1279 @[$1,0,$a:
  1280 	// Support for $1 arguments
  1281 	template<class ObjType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1282 	{
  1283 		ObjType *m_ThisPtr;
  1284 		void *m_VfnPtr;
  1285 		MFPType m_MFP;
  1286 		ISourceHook *m_pSH;
  1287 	public:
  1288 		ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
  1289 			m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
  1290 	
  1291 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1292 			SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1293 	         
  1294 		@[$2,$1+1,$a:
  1295 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1296 			SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1297 		@]
  1298 	};
  1299 
  1300 	template<class ObjType, class MFPType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFPType, void@[$2,1,$1:, Param$2@]>
  1301 	{
  1302 		ObjType *m_ThisPtr;
  1303 		void *m_VfnPtr;
  1304 		MFPType m_MFP;
  1305 		ISourceHook *m_pSH;
  1306 	public:
  1307 		ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
  1308 			m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
  1309 	
  1310 	   void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1311 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1312 	         
  1313 	   @[$2,$1+1,$a:
  1314 	   template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1315 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1316 	   @]
  1317 	};
  1318 @]
  1319 
  1320 }
  1321 
  1322 #define SH__CALL_GET_VFNPTR_NORMAL \
  1323 	using namespace ::SourceHook; \
  1324 	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1325 	GetFuncInfo(ptr, mfp, mfi); \
  1326 	void *vfnptr = reinterpret_cast<void*>( \
  1327 		*reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
  1328 
  1329 #define SH__CALL_GET_VFNPTR_MANUAL \
  1330 	using namespace ::SourceHook; \
  1331 	void *vfnptr = reinterpret_cast<void*>( \
  1332 		*reinterpret_cast<void***>( (reinterpret_cast<char*>(ptr) + thisptroffs + vtbloffs) ) + vtblidx); \
  1333 	/* patch mfp */ \
  1334 	*reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
  1335 
  1336 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1337 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1338 // The only purpose of the mfp2 parameter is to extract the return type
  1339 
  1340 @[$1,0,$a:
  1341 // Support for $1 arguments
  1342 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1343 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1344 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr)
  1345 {
  1346 	SH__CALL_GET_VFNPTR_NORMAL
  1347 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1348 }
  1349 
  1350 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1351 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1352 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr)
  1353 {
  1354 	SH__CALL_GET_VFNPTR_NORMAL
  1355 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1356 }
  1357 
  1358 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1359 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
  1360 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr)
  1361 {
  1362 	SH__CALL_GET_VFNPTR_MANUAL
  1363 	return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
  1364 		reinterpret_cast<SourceHook::EmptyClass*>(ptr), mfp, vfnptr, shptr);
  1365 }
  1366 @]
  1367 
  1368 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1369 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1370 
  1371 @[$1,0,$a:
  1372 // Support for $1 arguments
  1373 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1374 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1375 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr)
  1376 {
  1377 	SH__CALL_GET_VFNPTR_NORMAL
  1378 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1379 }
  1380 
  1381 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1382 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1383 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr)
  1384 {
  1385 	SH__CALL_GET_VFNPTR_NORMAL
  1386 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1387 }
  1388 
  1389 @]
  1390 
  1391 #endif
  1392 
  1393 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR)
  1394 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR)
  1395 #define SH_MCALL(ptr, mhookname) __SoureceHook_FHM_SHCall##mhookname(ptr)
  1396 
  1397 #undef SH_MAKE_EXECUTABLECLASS_OB
  1398 #undef SH_MAKE_EXECUTABLECLASS_OB_void
  1399 
  1400 //////////////////////////////////////////////////////////////////////////
  1401 // SetOverrideRet and RecallGetIface for recalls
  1402 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1403 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1404 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1405 
  1406 namespace SourceHook
  1407 {
  1408 	template <class RetType>
  1409 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1410 	{
  1411 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1412 	}
  1413 
  1414 	// SetOverrideResult used to be implemented like this:
  1415 	//  SetOverrideResult(shptr, memfuncptr, return);
  1416 	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1417 	//  (it thought it was ambigous - the ref and non-ref type)
  1418 	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1419 	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1420 	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1421 
  1422 	template <class T> struct OverrideFunctor
  1423 	{
  1424 		void operator()(ISourceHook *shptr, T res)
  1425 		{
  1426 			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1427 		}
  1428 	};
  1429 	template <class T> struct OverrideFunctor<T&>
  1430 	{
  1431 		void operator()(ISourceHook *shptr, T &res)
  1432 		{
  1433 			// overrideretptr points to ReferenceCarrier<T&>
  1434 			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1435 		}
  1436 	};
  1437 @[$1,0,$a:
  1438 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1439 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1440 	{
  1441 		return OverrideFunctor<RetType>();
  1442 	}
  1443 
  1444 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1445 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1:Param$2, @]...))
  1446 	{
  1447 		return OverrideFunctor<RetType>();
  1448 	}
  1449 
  1450 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1451 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1452 	{
  1453 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1454 	}
  1455 
  1456 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1457 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1:Param$2, @]...))
  1458 	{
  1459 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1460 	}
  1461 @]
  1462 }
  1463 
  1464 #endif
  1465 	// The pope is dead. -> :(