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