sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 07 22:45:22 2007 +0000 (2007-10-07)
changeset 384 264cab8ff2cc
parent 382 8b06ecc6e76d
child 386 08583103ba6f
permissions -rwxr-xr-x
correct version now... i guess i'm just tired :)
     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 
   396 		// For hook managers	
   397 
   398 		// Returns the current iface ptr
   399 		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   400 
   401 		/*
   402 			HOW RECALLS WORK:
   403 			
   404 			The problem:
   405 				Users want the ability to change parameters of the called function
   406 				from inside their handler.
   407 			The solution:
   408 				1) Mark as "recall"
   409 				2) Recall the function
   410 				3) => SH's hook func gets called:
   411 				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   412 				  5) does all iteration and returns normally
   413 		        6) The user's handler returns immediately
   414 				7) The hook func returns immediately as well
   415 
   416 				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   417 				ended with. The last handler (doing the recall) is also able to specify its own
   418 				META_RES.
   419 		*/
   420 
   421 		/**
   422 		*	@brief Set up the hook loop.
   423 		*
   424 		*	@param statusPtr		pointer to status variable
   425 		*	@param prevResPtr		pointer to previous result variable
   426 		*	@param curResPtr		pointer to current result variable
   427 		*	@param ifacePtrPtr		pointer to interface this pointer variable
   428 		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   429 		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   430 		*	
   431 		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   432 		*		when the hook func is being called as part of a recall
   433 		*/
   434 		virtual IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry,
   435 			META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   436 			const void *origRetPtr, void *overrideRetPtr) = 0;
   437 
   438 		virtual void EndContext(IHookContext *pCtx) = 0;
   439 	};
   440 
   441 	// For META_RESULT_ORIG_RET and META_RESULT_OVERRIDE_RET:
   442 	//  These have to be able to return references. If T is a reference, the pointers returned
   443 	//  from the SH_GLOB_SHPTR are pointers to instances of ReferenceCarrier<T>::type.
   444 	template <class T> struct MacroRefHelpers
   445 	{
   446 		inline static const T* GetOrigRet(ISourceHook *shptr)
   447 		{
   448 			return reinterpret_cast<const T*>(shptr->GetOrigRet());
   449 		}
   450 		inline static const T* GetOverrideRet(ISourceHook *shptr)
   451 		{
   452 			return reinterpret_cast<const T*>(shptr->GetOverrideRet());
   453 		}
   454 	};
   455 
   456 	template <class T> struct MacroRefHelpers<T&>
   457 	{
   458 		inline static T* GetOrigRet(ISourceHook *shptr)
   459 		{
   460 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOrigRet());
   461 			return &ref;
   462 		}
   463 		inline static T* GetOverrideRet(ISourceHook *shptr)
   464 		{
   465 			T &ref = *reinterpret_cast<const typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRet());
   466 			return &ref;
   467 		}
   468 	};
   469 }
   470 
   471 /************************************************************************/
   472 /* High level interface                                                 */
   473 /************************************************************************/
   474 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   475 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   476 #define META_RESULT_ORIG_RET(type)			*SourceHook::MacroRefHelpers<type>::GetOrigRet(SH_GLOB_SHPTR)
   477 #define META_RESULT_OVERRIDE_RET(type)		*SourceHook::MacroRefHelpers<type>::GetOverrideRet(SH_GLOB_SHPTR)
   478 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   479 
   480 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   481 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   482 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   483 
   484 
   485 template<class T>
   486 SourceHook::CallClass<T> *SH_GET_CALLCLASS(T *p)
   487 {
   488 	return static_cast< SourceHook::CallClass<T> *>(p);
   489 }
   490 #define SH_RELEASE_CALLCLASS(p)
   491 
   492 #define SH_GET_MCALLCLASS(p, size) reinterpret_cast<void*>(p)
   493 
   494 // only call these from the hook handlers directly!
   495 
   496 // If a hook on a function which returns a reference does not want to specify a return value,
   497 // it can use this macro.
   498 //   ONLY USE THIS WITH MRES_IGNORED AND MRES_HANDLED !!!
   499 #define RETURN_META_NOREF(result, rettype)	do { SET_META_RESULT(result); return reinterpret_cast<rettype>(*SH_GLOB_SHPTR); } while(0)
   500 
   501 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   502 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   503 
   504 // SourceHook::SetOverrideRet is defined later.
   505 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   506 	do { \
   507 		SET_META_RESULT(result); \
   508 		SH_GLOB_SHPTR->DoRecall(); \
   509 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   510 		RETURN_META(MRES_SUPERCEDE); \
   511 	} while (0)
   512 
   513 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   514 	do { \
   515 		SET_META_RESULT(result); \
   516 		SH_GLOB_SHPTR->DoRecall(); \
   517 		if ((result) >= MRES_OVERRIDE) \
   518 		{ \
   519 			/* meh, set the override result here because we don't get a chance to return */ \
   520 			/* before continuing the hook loop through the recall */ \
   521 			SourceHook::SetOverrideResult(memfuncptr)(SH_GLOB_SHPTR, value); \
   522 		} \
   523 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   524 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   525 	} while (0)
   526 
   527 // :TODO: thisptroffs in MNEWPARAMS ??
   528 
   529 #if SH_COMP == SH_COMP_MSVC
   530 
   531 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   532 	union \
   533 	{ \
   534 		SH_MFHCls(hookname)::ECMFP mfp; \
   535 		void *addr; \
   536 	} u; \
   537 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   538 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   539 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   540 
   541 #elif SH_COMP == SH_COMP_GCC
   542 
   543 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   544 	union \
   545 	{ \
   546 		SH_MFHCls(hookname)::ECMFP mfp; \
   547 		struct \
   548 		{ \
   549 			void *addr; \
   550 			intptr_t adjustor; \
   551 		} s; \
   552 	} u; \
   553 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   554 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   555 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   556 	u.s.adjustor = 0;
   557 
   558 #endif
   559 
   560 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   561 	do { \
   562 		SET_META_RESULT(result); \
   563 		SH_GLOB_SHPTR->DoRecall(); \
   564 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   565 		(thisptr->*(u.mfp)) newparams; \
   566 		RETURN_META(MRES_SUPERCEDE); \
   567 	} while (0)
   568 
   569 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   570 	do { \
   571 		SET_META_RESULT(result); \
   572 		SH_GLOB_SHPTR->DoRecall(); \
   573 		if ((result) >= MRES_OVERRIDE) \
   574 		{ \
   575 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   576 			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   577 		} \
   578 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   579 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   580 	} while (0)
   581 
   582 
   583 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   584 	do { \
   585 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   586 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   587 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   588 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   589 	} while (0)
   590 
   591 // New ADD / REMOVE macros.
   592 #define SH_STATIC(func) fastdelegate::MakeDelegate(func)
   593 #define SH_MEMBER(inst, func) fastdelegate::MakeDelegate(inst, func)
   594 
   595 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   596 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   597 	SourceHook::ISourceHook::Hook_Normal, post, handler)
   598 
   599 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   600 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   601 	post, handler) 
   602 
   603 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   604 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), \
   605 	SourceHook::ISourceHook::Hook_Normal, post, handler)
   606 
   607 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   608 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler) 
   609 
   610 #define SH_ADD_VPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   611 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   612 	SourceHook::ISourceHook::Hook_VP, post, handler)
   613 
   614 #define SH_ADD_DVPHOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   615 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   616 	SourceHook::ISourceHook::Hook_DVP, post, handler)
   617 
   618 #define SH_ADD_MANUALVPHOOK(hookname, ifaceptr, handler, post) \
   619 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_VP, post, handler) 
   620 
   621 #define SH_ADD_MANUALDVPHOOK(hookname, ifaceptr, handler, post) \
   622 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), SourceHook::ISourceHook::Hook_DVP, post, handler) 
   623 
   624 #define SH_REMOVE_HOOK_ID(hookid) \
   625 	(SH_GLOB_SHPTR->RemoveHookByID(hookid))
   626 
   627 // Old macros
   628 // !! These are now deprecated. Instead, use one of these:
   629 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   630 //  SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   631 
   632 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   633 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   634 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   635 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   636 
   637 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   638 //  SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(inst, func), post)
   639 
   640 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   641 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_STATIC(handler), post)
   642 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   643 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   644 
   645 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   646 //  SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   647 
   648 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   649 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   650 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   651 	SH_ADD_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)
   652 
   653 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   654 //  SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(inst, func), post)
   655 
   656 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   657 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_STATIC(handler), post)
   658 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   659 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, SH_MEMBER(handler_inst, handler_func), post)  
   660 
   661 #define SH_NOATTRIB
   662 
   663 
   664 
   665 
   666 #if SH_COMP == SH_COMP_MSVC
   667 # define SH_SETUP_MFP(mfp) \
   668 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   669 #elif SH_COMP == SH_COMP_GCC
   670 # define SH_SETUP_MFP(mfp) \
   671 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   672 	reinterpret_cast<void**>(&mfp)[1] = 0;
   673 #else
   674 # error Not supported yet.
   675 #endif
   676 
   677 //////////////////////////////////////////////////////////////////////////
   678 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   679 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   680 
   681 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   682 	static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
   683 	{ \
   684 		using namespace ::SourceHook; \
   685 		GetFuncInfo(funcptr, ms_MFI); \
   686 		/* Verify interface version */ \
   687 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   688 			return 1; \
   689 		if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   690 			return 1; \
   691 		if (store) \
   692 			ms_HI = hi; \
   693 		if (hi) \
   694 		{ \
   695 			MemFuncInfo mfi = {true, -1, 0, 0}; \
   696 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   697 			hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
   698 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
   699 		} \
   700 		return 0; \
   701 	}
   702 
   703 // It has to be possible to use the macros in namespaces
   704 // -> So we need to access and extend the global SourceHook namespace
   705 // We use a namespace alias for this
   706 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   707 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   708 	{ \
   709 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   710 		static ::SourceHook::MemFuncInfo ms_MFI; \
   711 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   712 		static ::SourceHook::ProtoInfo ms_Proto; \
   713 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   714 
   715 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   716 	}; \
   717 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   718 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   719 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   720 	int __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
   721 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   722 	{ \
   723 		using namespace ::SourceHook; \
   724 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   725 		GetFuncInfo(funcptr, mfi); \
   726 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   727 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   728 		\
   729 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
   730 			iface, mfi.thisptroffs, SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   731 			new SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl(handler), post); \
   732 	} \
   733 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   734 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   735 	{ \
   736 		using namespace ::SourceHook; \
   737 		MemFuncInfo mfi = {true, -1, 0, 0}; \
   738 		GetFuncInfo(funcptr, mfi); \
   739 		SH_FHCls(ifacetype,ifacefunc,overload)::CMyDelegateImpl tmp(handler); \
   740 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   741 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   742 	} \
   743 
   744 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   745 	struct SH_MFHCls(hookname) \
   746 	{ \
   747 		static SH_MFHCls(hookname) ms_Inst; \
   748 		static ::SourceHook::MemFuncInfo ms_MFI; \
   749 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   750 		static ::SourceHook::ProtoInfo ms_Proto; \
   751 		\
   752 		SH_MFHCls(hookname)() \
   753 		{ \
   754 			ms_MFI.isVirtual = true; \
   755 			ms_MFI.thisptroffs = pthisptroffs; \
   756 			ms_MFI.vtblindex = pvtblidx; \
   757 			ms_MFI.vtbloffs = pvtbloffs; \
   758 		} \
   759 		static int HookManPubFunc(bool store, ::SourceHook::IHookManagerInfo *hi) \
   760 		{ \
   761 			using namespace ::SourceHook; \
   762 			/* Verify interface version */ \
   763 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   764 				return 1; \
   765 			if (SH_GLOB_SHPTR->GetImplVersion() < SH_IMPL_VERSION) \
   766 				return 1; \
   767 			if (store) \
   768 				ms_HI = hi; \
   769 			if (hi) \
   770 			{ \
   771 				MemFuncInfo mfi = {true, -1, 0, 0}; \
   772 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   773 				hi->SetInfo(SH_HOOKMAN_VERSION, ms_MFI.vtbloffs, ms_MFI.vtblindex, &ms_Proto, \
   774 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex] ); \
   775 			} \
   776 			return 0; \
   777 		}
   778 
   779 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   780 	}; \
   781 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   782 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   783 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   784 	int __SourceHook_FHMAdd##hookname(void *iface, ::SourceHook::ISourceHook::AddHookMode mode, bool post, \
   785 		SH_MFHCls(hookname)::FD handler) \
   786 	{ \
   787 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, mode, \
   788 			iface, pthisptroffs, SH_MFHCls(hookname)::HookManPubFunc, \
   789 			new SH_MFHCls(hookname)::CMyDelegateImpl(handler), post); \
   790 	} \
   791 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   792 		SH_MFHCls(hookname)::FD handler) \
   793 	{ \
   794 		SH_MFHCls(hookname)::CMyDelegateImpl tmp(handler); \
   795 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   796 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   797 	} \
   798 
   799 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   800 	/* 1) Set up */ \
   801 	using namespace ::SourceHook; \
   802 	void *ourvfnptr = reinterpret_cast<void*>( \
   803 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   804 	void *vfnptr_origentry; \
   805 	\
   806 	META_RES status = MRES_IGNORED; \
   807 	META_RES prev_res; \
   808 	META_RES cur_res; \
   809 	\
   810 	typedef ReferenceCarrier<rettype>::type my_rettype; \
   811 	my_rettype orig_ret; \
   812 	my_rettype override_ret; \
   813 	my_rettype plugin_ret; \
   814 	IMyDelegate *iter; \
   815 	IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
   816 		&vfnptr_origentry, &status, &prev_res, &cur_res, &orig_ret, &override_ret);
   817 
   818 #define SH_CALL_HOOKS(post, params) \
   819 	prev_res = MRES_IGNORED; \
   820 	while ( iter = static_cast<IMyDelegate*>(pContext->GetNext()) ) \
   821 	{ \
   822 		cur_res = MRES_IGNORED; \
   823 		plugin_ret = iter->Call params; \
   824 		prev_res = cur_res; \
   825 		if (cur_res > status) \
   826 			status = cur_res; \
   827 		if (cur_res >= MRES_OVERRIDE) \
   828 			*reinterpret_cast<my_rettype*>(pContext->GetOverrideRetPtr()) = plugin_ret; \
   829 	}
   830 
   831 #define SH_CALL_ORIG(rettype, paramtypes, params) \
   832 	if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
   833 	{ \
   834 		rettype (EmptyClass::*mfp)paramtypes; \
   835 		SH_SETUP_MFP(mfp); \
   836 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   837 	} \
   838 	else \
   839 		orig_ret = override_ret; /* :TODO: ??? : use pContext->GetOverrideRetPtr() or not? */
   840 
   841 #define SH_RETURN() \
   842 	const my_rettype *retptr = reinterpret_cast<const my_rettype*>( \
   843 		(status >= MRES_OVERRIDE) ? pContext->GetOverrideRetPtr() : pContext->GetOrigRetPtr()); \
   844 	SH_GLOB_SHPTR->EndContext(pContext); \
   845 	return *retptr;
   846 
   847 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   848 	SH_SETUPCALLS(rettype, paramtypes, params) \
   849 	SH_CALL_HOOKS(pre, params) \
   850 	SH_CALL_ORIG(rettype, paramtypes, params) \
   851 	SH_CALL_HOOKS(post, params) \
   852 	SH_RETURN()
   853 
   854 //////////////////////////////////////////////////////////////////////////
   855 #define SH_SETUPCALLS_void(paramtypes, params) \
   856 	/* 1) Set up */ \
   857 	using namespace ::SourceHook; \
   858 	void *ourvfnptr = reinterpret_cast<void*>( \
   859 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   860 	void *vfnptr_origentry; \
   861 	\
   862 	META_RES status = MRES_IGNORED; \
   863 	META_RES prev_res; \
   864 	META_RES cur_res; \
   865 	\
   866 	IMyDelegate *iter; \
   867 	IHookContext *pContext = SH_GLOB_SHPTR->SetupHookLoop(ms_HI, ourvfnptr, reinterpret_cast<void*>(this), \
   868 		&vfnptr_origentry, &status, &prev_res, &cur_res, NULL, NULL);
   869 
   870 #define SH_CALL_HOOKS_void(post, params) \
   871 	prev_res = MRES_IGNORED; \
   872 	while ( iter = static_cast<IMyDelegate*>(pContext->GetNext()) ) \
   873 	{ \
   874 		cur_res = MRES_IGNORED; \
   875 		iter->Call params; \
   876 		prev_res = cur_res; \
   877 		if (cur_res > status) \
   878 			status = cur_res; \
   879 	}
   880 
   881 #define SH_CALL_ORIG_void(paramtypes, params) \
   882 	if (status != MRES_SUPERCEDE && pContext->ShouldCallOrig()) \
   883 	{ \
   884 		void (EmptyClass::*mfp)paramtypes; \
   885 		SH_SETUP_MFP(mfp); \
   886 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   887 	}
   888 
   889 #define SH_RETURN_void() \
   890 	SH_GLOB_SHPTR->EndContext(pContext);
   891 
   892 #define SH_HANDLEFUNC_void(paramtypes, params) \
   893 	SH_SETUPCALLS_void(paramtypes, params) \
   894 	SH_CALL_HOOKS_void(pre, params) \
   895 	SH_CALL_ORIG_void(paramtypes, params) \
   896 	SH_CALL_HOOKS_void(post, params) \
   897 	SH_RETURN_void()
   898 
   899 
   900 // Special vafmt handlers
   901 // :TODO: what
   902 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
   903 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   904 	SH_CALL_HOOKS(pre, params_plug) \
   905 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
   906 	SH_CALL_HOOKS(post, params_plug) \
   907 	SH_RETURN()
   908 
   909 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
   910 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   911 	SH_CALL_HOOKS_void(pre, params_plug) \
   912 	SH_CALL_ORIG_void(paramtypes, params_orig) \
   913 	SH_CALL_HOOKS_void(post, params_plug) \
   914 	SH_RETURN_void()
   915 
   916 //////////////////////////////////////////////////////////////////////////
   917 
   918 #define MAKE_DELEG(ret_type, params_decl, params_pass) \
   919 	struct IMyDelegate : ::SourceHook::ISHDelegate { virtual ret_type Call params_decl = 0; }; \
   920 	struct CMyDelegateImpl : IMyDelegate \
   921 	{ \
   922 		FD m_Deleg; \
   923 		CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
   924 		ret_type Call params_decl { return m_Deleg params_pass; } \
   925 		void DeleteThis() { delete this; } \
   926 		bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
   927 	};
   928 
   929 #define MAKE_DELEG_void(params_decl, params_pass) \
   930 	struct IMyDelegate : ::SourceHook::ISHDelegate { virtual void  Call params_decl = 0; }; \
   931 	struct CMyDelegateImpl : IMyDelegate \
   932 	{ \
   933 		FD m_Deleg; \
   934 		CMyDelegateImpl(FD deleg) : m_Deleg(deleg) {} \
   935 		void Call params_decl { m_Deleg params_pass; } \
   936 		void DeleteThis() { delete this; } \
   937 		bool IsEqual(ISHDelegate *pOtherDeleg) { return m_Deleg == static_cast<CMyDelegateImpl*>(pOtherDeleg)->m_Deleg; } \
   938 	};
   939 
   940 // :FIXME:
   941 //  sizeof on references returns the size of the datatype, NOT the pointer size or something
   942 //  -> one should probably flag references in __SourceHook_ParamSizes_* !
   943 //		or simply assume that their size is sizeof(void*)=SH_PTRSIZE... could be doable through a simple template
   944 
   945 @[$1,0,$a:
   946 // ********* Support for $1 arguments *********
   947 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   948 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   949 		(&ifacetype::ifacefunc))) \
   950 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
   951 		MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
   952 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
   953 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
   954 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   955 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   956 	\
   957 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
   958 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
   959 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
   960 
   961 
   962 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   963 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   964 		(&ifacetype::ifacefunc))) \
   965 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
   966 		MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
   967 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   968 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
   969 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   970 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   971 	\
   972 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
   973 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
   974 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
   975 
   976 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   977 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   978 		(&ifacetype::ifacefunc))) \
   979 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
   980 		MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
   981 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@] @[$1!=0:, @]const char *fmt, ...) \
   982 		{ \
   983 			char buf[::SourceHook::STRBUF_LEN]; \
   984 			va_list ap; \
   985 			va_start(ap, fmt); \
   986 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   987 			va_end(ap); \
   988 			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); \
   989 		} \
   990 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   991 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
   992 	\
   993 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
   994 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
   995 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
   996 
   997 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   998 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   999 		(&ifacetype::ifacefunc))) \
  1000 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
  1001 		MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *px), (@[$2,1,$1|, :p$2@]@[$1!=0:, @]px)); \
  1002 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1003 		{ \
  1004 			char buf[::SourceHook::STRBUF_LEN]; \
  1005 			va_list ap; \
  1006 			va_start(ap, fmt); \
  1007 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1008 			va_end(ap); \
  1009 			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)); \
  1010 		} \
  1011 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1012 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1013 	\
  1014 	const ::SourceHook::PassInfo __SourceHook_ParamInfos_##ifacetype##ifacefunc##overload[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
  1015 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto = { $1, { 0, 0, 0 }, \
  1016 		__SourceHook_ParamInfos_##ifacetype##ifacefunc##overload, 0 };
  1017 
  1018 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1019 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1020 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1021 		MAKE_DELEG(rettype, (@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
  1022 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1023 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1024 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1025 		typedef rettype RetType; \
  1026 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1027 	\
  1028 	const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
  1029 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { sizeof(rettype), 0, 0 }, \
  1030 		__SourceHook_ParamInfosM_##hookname, 0 };
  1031 
  1032 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1033 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1034 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1035 		MAKE_DELEG_void((@[$2,1,$1|, :param$2 p$2@]), (@[$2,1,$1|, :p$2@])); \
  1036 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1037 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1038 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1039 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1040 	\
  1041 	const ::SourceHook::PassInfo __SourceHook_ParamInfosM_##hookname[] = { {0, 0, 0}@[$2,1,$1:, {sizeof(param$2), 0, 0}@] }; \
  1042 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto = { $1, { 0, 0, 0 }, \
  1043 		__SourceHook_ParamInfosM_##hookname	, 0 };
  1044 
  1045 @]
  1046 
  1047 
  1048 //////////////////////////////////////////////////////////////////////////
  1049 // SH_CALL
  1050 
  1051 #define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1052 { \
  1053 	using namespace ::SourceHook; \
  1054 	\
  1055 	m_pSH->SetIgnoreHooks(m_VfnPtr); \
  1056 	RetType tmpret = (m_ThisPtr->*m_MFP)call; \
  1057 	m_pSH->ResetIgnoreHooks(m_VfnPtr); \
  1058 	return tmpret; \
  1059 }
  1060 
  1061 #define SH_MAKE_EXECUTABLECLASS_OB_void(call, prms) \
  1062 { \
  1063 	using namespace ::SourceHook; \
  1064 	\
  1065 	m_pSH->SetIgnoreHooks(m_VfnPtr); \
  1066 	(m_ThisPtr->*m_MFP)call; \
  1067 	m_pSH->ResetIgnoreHooks(m_VfnPtr); \
  1068 }
  1069 
  1070 namespace SourceHook
  1071 {
  1072 @[$1,0,$a:
  1073 	// Support for $1 arguments
  1074 	template<class ObjType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1075 	{
  1076 		ObjType *m_ThisPtr;
  1077 		void *m_VfnPtr;
  1078 		MFPType m_MFP;
  1079 		ISourceHook *m_pSH;
  1080 	public:
  1081 		ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
  1082 			m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
  1083 	
  1084 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1085 			SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1086 	         
  1087 		@[$2,$1+1,$a:
  1088 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1089 			SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1090 		@]
  1091 	};
  1092 
  1093 	template<class ObjType, class MFPType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1<ObjType, MFPType, void@[$2,1,$1:, Param$2@]>
  1094 	{
  1095 		ObjType *m_ThisPtr;
  1096 		void *m_VfnPtr;
  1097 		MFPType m_MFP;
  1098 		ISourceHook *m_pSH;
  1099 	public:
  1100 		ExecutableClass$1(ObjType *tp, MFPType mfp, void *vp, ISourceHook *pSH) : m_ThisPtr(tp),
  1101 			m_VfnPtr(vp), m_MFP(mfp), m_pSH(pSH) { }
  1102 	
  1103 	   void operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1104 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1105 	         
  1106 	   @[$2,$1+1,$a:
  1107 	   template <@[$3,$1+1,$2|, :class Param$3@]> void operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1108 	      SH_MAKE_EXECUTABLECLASS_OB_void((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1109 	   @]
  1110 	};
  1111 @]
  1112 
  1113 }
  1114 
  1115 #define SH__CALL_GET_VFNPTR_NORMAL \
  1116 	using namespace ::SourceHook; \
  1117 	MemFuncInfo mfi = {true, -1, 0, 0}; \
  1118 	GetFuncInfo(ptr, mfp, mfi); \
  1119 	void *vfnptr = reinterpret_cast<void*>( \
  1120 		*reinterpret_cast<void***>(reinterpret_cast<char*>(ptr) + mfi.thisptroffs + mfi.vtbloffs) + mfi.vtblindex);
  1121 
  1122 #define SH__CALL_GET_VFNPTR_MANUAL \
  1123 	using namespace ::SourceHook; \
  1124 	void *vfnptr = reinterpret_cast<void*>( \
  1125 		*reinterpret_cast<void***>( (reinterpret_cast<char*>(ptr) + thisptroffs + vtbloffs) ) + vtblidx); \
  1126 	/* patch mfp */ \
  1127 	*reinterpret_cast<void**>(&mfp) = *reinterpret_cast<void**>(vfnptr);
  1128 
  1129 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1130 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1131 // The only purpose of the mfp2 parameter is to extract the return type
  1132 
  1133 @[$1,0,$a:
  1134 // Support for $1 arguments
  1135 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1136 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1137 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), SourceHook::ISourceHook *shptr)
  1138 {
  1139 	SH__CALL_GET_VFNPTR_NORMAL
  1140 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1141 }
  1142 
  1143 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1144 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1145 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const, SourceHook::ISourceHook *shptr)
  1146 {
  1147 	SH__CALL_GET_VFNPTR_NORMAL
  1148 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1149 }
  1150 
  1151 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1152 SourceHook::ExecutableClass$1<SourceHook::EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>
  1153 SH_MCALL3(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs, SourceHook::ISourceHook *shptr)
  1154 {
  1155 	SH__CALL_GET_VFNPTR_MANUAL
  1156 	return SourceHook::ExecutableClass$1<EmptyClass, MFP, RetType@[$2,1,$1:, Param$2@]>(
  1157 		reinterpret_cast<SourceHook::EmptyClass*>(ptr), mfp, vfnptr, shptr);
  1158 }
  1159 @]
  1160 
  1161 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1162 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1163 
  1164 @[$1,0,$a:
  1165 // Support for $1 arguments
  1166 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1167 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1168 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...), SourceHook::ISourceHook *shptr)
  1169 {
  1170 	SH__CALL_GET_VFNPTR_NORMAL
  1171 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1172 }
  1173 
  1174 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1175 SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>
  1176 SH_CALL2(Y *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const, SourceHook::ISourceHook *shptr)
  1177 {
  1178 	SH__CALL_GET_VFNPTR_NORMAL
  1179 	return SourceHook::ExecutableClass$1<Y, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp, vfnptr, shptr);
  1180 }
  1181 
  1182 @]
  1183 
  1184 #endif
  1185 
  1186 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp), SH_GLOB_SHPTR)
  1187 #define SH_MCALL2(ptr, mfp, vtblidx, vtbloffs, thisptroffs) SH_MCALL3((ptr), (mfp), (mfp), (vtblidx), (vtbloffs), (thisptroffs), SH_GLOB_SHPTR)
  1188 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1189 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1190 
  1191 #undef SH_MAKE_EXECUTABLECLASS_OB
  1192 #undef SH_MAKE_EXECUTABLECLASS_OB_void
  1193 
  1194 //////////////////////////////////////////////////////////////////////////
  1195 // SetOverrideRet and RecallGetIface for recalls
  1196 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1197 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1198 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1199 
  1200 namespace SourceHook
  1201 {
  1202 	template <class RetType>
  1203 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1204 	{
  1205 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1206 	}
  1207 
  1208 	// SetOverrideResult used to be implemented like this:
  1209 	//  SetOverrideResult(shptr, memfuncptr, return);
  1210 	//  normally the compiler can deduce the return type from memfuncptr, but (at least msvc8) failed when it was a reference
  1211 	//  (it thought it was ambigous - the ref and non-ref type)
  1212 	//  so now SetOverrideResult(memfuncptr) deduces the ret type, and returns a functor which does the work
  1213 	//  new syntax: SetOverrideResult(memfuncptr)(shptr, return)
  1214 	// This also allows us to create a special version for references which respects ReferenceCarrier.
  1215 
  1216 	template <class T> struct OverrideFunctor
  1217 	{
  1218 		void operator()(ISourceHook *shptr, T res)
  1219 		{
  1220 			*reinterpret_cast<T*>(shptr->GetOverrideRetPtr()) = res;
  1221 		}
  1222 	};
  1223 	template <class T> struct OverrideFunctor<T&>
  1224 	{
  1225 		void operator()(ISourceHook *shptr, T &res)
  1226 		{
  1227 			// overrideretptr points to ReferenceCarrier<T&>
  1228 			*reinterpret_cast<typename ReferenceCarrier<T&>::type *>(shptr->GetOverrideRetPtr()) = res;
  1229 		}
  1230 	};
  1231 @[$1,0,$a:
  1232 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1233 	OverrideFunctor<RetType> SetOverrideResult(RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1234 	{
  1235 		return OverrideFunctor<RetType>();
  1236 	}
  1237 
  1238 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1239 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1240 	{
  1241 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1242 	}
  1243 @]
  1244 }
  1245 
  1246 #endif
  1247 	// The pope is dead. -> :(