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