sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Thu Jun 08 18:34:18 2006 +0000 (2006-06-08)
changeset 211 af7f4f2148f5
parent 199 7a5b326bb7f8
child 233 6d19421c552c
permissions -rwxr-xr-x
Added manual recalls: RETURN_META_(VALUE_)MNEWPARAMS
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2005 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 #define SH_IFACE_VERSION 4
    25 #define SH_IMPL_VERSION 3
    26 
    27 // Hookman version:
    28 //  1 - Support for recalls, performance optimisations
    29 #define SH_HOOKMAN_VERSION 1
    30 
    31 // The value of SH_GLOB_SHPTR has to be a pointer to SourceHook::ISourceHook
    32 // It's used in various macros
    33 #ifndef SH_GLOB_SHPTR
    34 #define SH_GLOB_SHPTR g_SHPtr
    35 #endif
    36 
    37 // Used to identify the current plugin
    38 #ifndef SH_GLOB_PLUGPTR
    39 #define SH_GLOB_PLUGPTR g_PLID
    40 #endif
    41 
    42 #ifdef SH_DEBUG
    43 # include <stdio.h>
    44 # include <stdlib.h>
    45 
    46 # define SH_ASSERT(x, info) \
    47 	do { \
    48 		if (!(x)) \
    49 		{ \
    50 			printf("SOURCEHOOK DEBUG ASSERTION FAILED: %s:%u(%s): %s: ", __FILE__, __LINE__, __FUNCTION__, #x); \
    51 			printf info; \
    52 			putchar('\n'); \
    53 			abort(); \
    54 		} \
    55 	} while(0)
    56 
    57 #else
    58 # define SH_ASSERT(x, info)
    59 #endif
    60 
    61 // System
    62 #define SH_SYS_WIN32 1
    63 #define SH_SYS_LINUX 2
    64 
    65 #ifdef _WIN32
    66 # define SH_SYS SH_SYS_WIN32
    67 #elif defined __linux__
    68 # define SH_SYS SH_SYS_LINUX
    69 #else
    70 # error Unsupported system
    71 #endif
    72 
    73 // Compiler
    74 #define SH_COMP_GCC 1
    75 #define SH_COMP_MSVC 2
    76 
    77 #ifdef _MSC_VER
    78 # define SH_COMP SH_COMP_MSVC
    79 #elif defined __GNUC__
    80 # define SH_COMP SH_COMP_GCC
    81 #else
    82 # error Unsupported compiler
    83 #endif
    84 
    85 #if SH_COMP==SH_COMP_MSVC
    86 # define vsnprintf _vsnprintf
    87 #endif
    88 
    89 #if SH_SYS != SH_SYS_WIN32
    90 # include <unistd.h>
    91 #endif
    92 
    93 #define SH_PTRSIZE sizeof(void*)
    94 
    95 #include "FastDelegate.h"
    96 #include "sh_memfuncinfo.h"
    97 
    98 // Good old metamod!
    99 
   100 // Flags returned by a plugin's api function.
   101 // NOTE: order is crucial, as greater/less comparisons are made.
   102 enum META_RES
   103 {
   104 	MRES_IGNORED=0,		// plugin didn't take any action
   105 	MRES_HANDLED,		// plugin did something, but real function should still be called
   106 	MRES_OVERRIDE,		// call real function, but use my return value
   107 	MRES_SUPERCEDE		// skip real function; use my return value
   108 };
   109 
   110 
   111 namespace SourceHook
   112 {
   113 	/**
   114 	*	@brief	Specifies the size (in bytes) for the internal buffer of vafmt(printf-like) function handlers
   115 	*/
   116 	const int STRBUF_LEN=4096;
   117 
   118 	/**
   119 	*	@brief	An empty class. No inheritance used. Used for original-function-call hacks
   120 	*/
   121 	class EmptyClass
   122 	{
   123 	};
   124 
   125 	/**
   126 	*	@brief Implicit cast.
   127 	*/
   128 	template <class In, class Out>
   129 		inline Out implicit_cast(In input)
   130 		{
   131 			return input;
   132 		}
   133 
   134 	/**
   135 	*	@brief A plugin typedef
   136 	*
   137 	*	SourceHook doesn't really care what this is. As long as the ==, != and = operators work on it and every
   138 	*	plugin has a unique identifier, everything is ok.
   139 	*	It should also be possible to set it to 0.
   140 	*/
   141 	typedef int Plugin;
   142 
   143 	struct ProtoInfo
   144 	{
   145 		ProtoInfo(int rtsz, int nop, const int *p) : beginningNull(0), retTypeSize(rtsz), numOfParams(nop), params(p)
   146 		{
   147 		}
   148 		int beginningNull;		//!< To distinguish from old protos (which never begin with 0)
   149 		int retTypeSize;		//!< 0 if void
   150 		int numOfParams;		//!< number of parameters
   151 		const int *params;		//!< params[0]=0 (or -1 for vararg), params[1]=size of first param, ...
   152 	};
   153 
   154 	/**
   155 	*	@brief Specifies the actions for hook managers
   156 	*/
   157 	enum HookManagerAction
   158 	{
   159 		HA_GetInfo = 0,			//!< Store info about the hook manager
   160 		HA_Register,			//!< Save the IHookManagerInfo pointer for future reference
   161 		HA_Unregister			//!< Clear the saved pointer
   162 	};
   163 
   164 	struct IHookManagerInfo;
   165 
   166 	/**
   167 	*	@brief Pointer to hook manager interface function
   168 	*
   169 	*	A "hook manager" is a the only thing that knows the actual protoype of the function at compile time.
   170 	*
   171 	*   @param ha What the hook manager should do
   172 	*	@param hi A pointer to IHookManagerInfo
   173 	*/
   174 	typedef int (*HookManagerPubFunc)(HookManagerAction ha, IHookManagerInfo *hi);
   175 
   176 	class ISHDelegate
   177 	{
   178 	public:
   179 		virtual void DeleteThis() = 0;				// Ugly, I know
   180 		virtual bool IsEqual(ISHDelegate *other) = 0;
   181 	};
   182 
   183 	template <class T> class CSHDelegate : public ISHDelegate
   184 	{
   185 		T m_Deleg;
   186 	public:
   187 		CSHDelegate(T deleg) : m_Deleg(deleg)
   188 		{
   189 		}
   190 
   191 		CSHDelegate(const CSHDelegate &other) : m_Deleg(other.m_Deleg)
   192 		{
   193 		}
   194 
   195 		void DeleteThis()
   196 		{
   197 			delete this;
   198 		}
   199 
   200 		bool IsEqual(ISHDelegate *other)
   201 		{
   202 			return static_cast<CSHDelegate<T>* >(other)->GetDeleg() == GetDeleg();
   203 		}
   204 
   205 		T &GetDeleg()
   206 		{
   207 			return m_Deleg;
   208 		}
   209 	};
   210 
   211 	struct IHookList
   212 	{
   213 		struct IIter
   214 		{
   215 			virtual bool End() = 0;
   216 			virtual void Next() = 0;
   217 			virtual ISHDelegate *Handler() = 0;
   218 			virtual int ThisPtrOffs() = 0;
   219 		};
   220 		virtual IIter *GetIter() = 0;
   221 		virtual void ReleaseIter(IIter *pIter) = 0;
   222 	};
   223 
   224 	struct IIface
   225 	{
   226 		virtual void *GetPtr() = 0;
   227 		virtual IHookList *GetPreHooks() = 0;
   228 		virtual IHookList *GetPostHooks() = 0;
   229 	};
   230 
   231 	struct IVfnPtr
   232 	{
   233 		virtual void *GetVfnPtr() = 0;
   234 		virtual void *GetOrigEntry() = 0;
   235 
   236 		virtual IIface *FindIface(void *ptr) = 0;
   237 	};
   238 
   239 	struct IHookManagerInfo
   240 	{
   241 		virtual IVfnPtr *FindVfnPtr(void *vfnptr) = 0;
   242 
   243 		virtual void SetInfo(int vtbloffs, int vtblidx, const char *proto) = 0;
   244 		virtual void SetHookfuncVfnptr(void *hookfunc_vfnptr) = 0;
   245 
   246 		// Added 23.12.2005 (yup! I'm coding RIGHT BEFORE CHRISTMAS!)
   247 		// If the hookman doesn't set this, it defaults 0
   248 		// SourceHook prefers hookmans with higher version numbers
   249 		virtual void SetVersion(int version) = 0;
   250 	};
   251 
   252 	class AutoHookIter
   253 	{
   254 		IHookList *m_pList;
   255 		IHookList::IIter *m_pIter;
   256 	public:
   257 		AutoHookIter(IHookList *pList)
   258 			: m_pList(pList), m_pIter(pList->GetIter())
   259 		{
   260 		}
   261 
   262 		~AutoHookIter()
   263 		{
   264 			if (m_pList)
   265 				m_pList->ReleaseIter(m_pIter);
   266 		}
   267 
   268 		bool End()
   269 		{
   270 			return m_pIter->End();
   271 		}
   272 
   273 		void Next()
   274 		{
   275 			m_pIter->Next();
   276 		}
   277 
   278 		ISHDelegate *Handler()
   279 		{
   280 			return m_pIter->Handler();
   281 		}
   282 
   283 		int ThisPtrOffs()
   284 		{
   285 			return m_pIter->ThisPtrOffs();
   286 		}
   287 
   288 		void SetToZero()
   289 		{
   290 			m_pList = 0;
   291 		}
   292 	};
   293 
   294 	template<class B> struct CallClass
   295 	{
   296 		virtual B *GetThisPtr() = 0;
   297 		virtual void *GetOrigFunc(int vtbloffs, int vtblidx) = 0;
   298 	};
   299 
   300 	typedef CallClass<void> GenericCallClass;
   301 	typedef CallClass<EmptyClass> ManualCallClass;
   302 
   303 	/**
   304 	*	@brief The main SourceHook interface
   305 	*/
   306 	class ISourceHook
   307 	{
   308 	public:
   309 		/**
   310 		*	@brief Return interface version
   311 		*/
   312 		virtual int GetIfaceVersion() = 0;
   313 
   314 		/**
   315 		*	@brief Return implementation version
   316 		*/
   317 		virtual int GetImplVersion() = 0;
   318 
   319 		/**
   320 		*	@brief Add a hook.
   321 		*
   322 		*	@return True if the function succeeded, false otherwise
   323 		*
   324 		*	@param plug The unique identifier of the plugin that calls this function
   325 		*	@param iface The interface pointer
   326 		*	@param ifacesize The size of the class iface points to
   327 		*	@param myHookMan A hook manager function that should be capable of handling the function
   328 		*	@param handler A pointer to a FastDelegate containing the hook handler
   329 		*	@param post Set to true if you want a post handler
   330 		*/
   331 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   332 			ISHDelegate *handler, bool post) = 0;
   333 
   334 		/**
   335 		*	@brief Removes a hook.
   336 		*
   337 		*	@return True if the function succeeded, false otherwise
   338 		*
   339 		*	@param plug The unique identifier of the plugin that calls this function
   340 		*	@param iface The interface pointer
   341 		*	@param myHookMan A hook manager function that should be capable of handling the function
   342 		*	@param handler A pointer to a FastDelegate containing the hook handler
   343 		*	@param post Set to true if you want a post handler
   344 		*/
   345 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   346 			ISHDelegate *handler, bool post) = 0;
   347 
   348 		/**
   349 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   350 		*
   351 		*	@param plug The unique identifier of the plugin in question
   352 		*/
   353 		virtual bool IsPluginInUse(Plugin plug) = 0;
   354 
   355 		/**
   356 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   357 		*
   358 		*	@param iface The interface pointer
   359 		*	@param size Size of the class instance
   360 		*/
   361 		virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
   362 
   363 		/**
   364 		*	@brief Release a callclass
   365 		*
   366 		*	@param ptr Pointer to the callclass
   367 		*/
   368 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   369 
   370 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   371 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   372 															//!<  previously calledhandler
   373 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   374 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   375 															//!<  If not in post function, undefined
   376 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   377 															//!<  If none is specified, NULL
   378 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   379 		//////////////////////////////////////////////////////////////////////////
   380 		// For hook managers
   381 		virtual void HookLoopBegin(IIface *pIface) = 0;			//!< Should be called when a hook loop begins
   382 		virtual void HookLoopEnd() = 0;							//!< Should be called when a hook loop exits
   383 		virtual void SetCurResPtr(META_RES *mres) = 0;			//!< Sets pointer to the current meta result
   384 		virtual void SetPrevResPtr(META_RES *mres) = 0;			//!< Sets pointer to previous meta result
   385 		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
   386 		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
   387 		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
   388 		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
   389 		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
   390 
   391 		/**
   392 		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
   393 		*
   394 		*	@param plug The owner of the hook manager
   395 		*   @param pubFunc The hook manager's info function
   396 		*/
   397 		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
   398 
   399 		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   400 		/*
   401 			HOW RECALLS WORK:
   402 			
   403 			The problem:
   404 				Users want the ability to change parameters of the called function
   405 				from inside their handler.
   406 			The solution:
   407 				1) Mark as "recall"
   408 				2) Recall the function
   409 				3) => SH's hook func gets called:
   410 				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   411 				  5) does all iteration and returns normally
   412 		        6) The user's handler returns immediately
   413 				7) The hook func returns immediately as well
   414 
   415 				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   416 				ended with. The last handler (doing the recall) is also able to specify its own
   417 				META_RES.
   418 		*/
   419 
   420 		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
   421 
   422 		/**
   423 		*	@brief Set up the hook loop. Equivalent to calling:
   424 		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
   425 		*
   426 		*	@param statusPtr		pointer to status variable
   427 		*	@param prevResPtr		pointer to previous result variable
   428 		*	@param curResPtr		pointer to current result variable
   429 		*	@param ifacePtrPtr		pointer to interface this pointer variable
   430 		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   431 		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   432 		*	
   433 		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   434 		*		when the hook func is being called as part of a recall
   435 		*/
   436 		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   437 			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
   438 
   439 		//!< 
   440 	};
   441 }
   442 
   443 /************************************************************************/
   444 /* High level interface                                                 */
   445 /************************************************************************/
   446 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   447 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   448 #define META_RESULT_ORIG_RET(type)			*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
   449 #define META_RESULT_OVERRIDE_RET(type)		*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
   450 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   451 
   452 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   453 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   454 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   455 
   456 
   457 // only call these from the hook handlers directly!
   458 // :TODO: enforce it ?
   459 
   460 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   461 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   462 
   463 // SourceHook::SetOverrideRet is defined later.
   464 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   465 	do { \
   466 		SET_META_RESULT(result); \
   467 		SH_GLOB_SHPTR->DoRecall(); \
   468 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   469 		RETURN_META(MRES_SUPERCEDE); \
   470 	} while (0)
   471 
   472 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   473 	do { \
   474 		SET_META_RESULT(result); \
   475 		SH_GLOB_SHPTR->DoRecall(); \
   476 		if ((result) >= MRES_OVERRIDE) \
   477 		{ \
   478 			/* meh, set the override result here because we don't get a chance to return */ \
   479 			/* before continuing the hook loop through the recall */ \
   480 			SourceHook::SetOverrideResult(SH_GLOB_SHPTR, memfuncptr, value); \
   481 		} \
   482 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   483 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   484 	} while (0)
   485 
   486 // :TODO: thisptroffs in MNEWPARAMS ??
   487 
   488 #if SH_COMP == SH_COMP_MSVC
   489 
   490 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   491 	union \
   492 	{ \
   493 		SH_MFHCls(hookname)::ECMFP mfp; \
   494 		void *addr; \
   495 	} u; \
   496 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   497 	u.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   498 		SH_MFHCls(hookname)::ms_MFI.vtblindex];
   499 
   500 #elif SH_COMP == SH_COMP_GCC
   501 
   502 #define SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname) \
   503 	union \
   504 	{ \
   505 		SH_MFHCls(hookname)::ECMFP mfp; \
   506 		struct \
   507 		{ \
   508 			void *addr; \
   509 			intptr_t adjustor; \
   510 		} s; \
   511 	} u; \
   512 	SourceHook::EmptyClass *thisptr = reinterpret_cast<SourceHook::EmptyClass*>(SH_GLOB_SHPTR->GetIfacePtr()); \
   513 	u.s.addr = (*reinterpret_cast<void***>(reinterpret_cast<char*>(thisptr) + SH_MFHCls(hookname)::ms_MFI.vtbloffs))[ \
   514 		SH_MFHCls(hookname)::ms_MFI.vtblindex]; \
   515 	u.s.adjustor = 0;
   516 
   517 #endif
   518 
   519 #define RETURN_META_MNEWPARAMS(result, hookname, newparams) \
   520 	do { \
   521 		SET_META_RESULT(result); \
   522 		SH_GLOB_SHPTR->DoRecall(); \
   523 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   524 		(thisptr->*(u.mfp)) newparams; \
   525 		RETURN_META(MRES_SUPERCEDE); \
   526 	} while (0)
   527 
   528 #define RETURN_META_VALUE_MNEWPARAMS(result, value, hookname, newparams) \
   529 	do { \
   530 		SET_META_RESULT(result); \
   531 		SH_GLOB_SHPTR->DoRecall(); \
   532 		if ((result) >= MRES_OVERRIDE) \
   533 		{ \
   534 			/* see RETURN_META_VALUE_NEWPARAMS */ \
   535 			SourceHook::SetOverrideResult<SH_MFHCls(hookname)::RetType>(SH_GLOB_SHPTR, value); \
   536 		} \
   537 		SOUREHOOK__MNEWPARAMS_PREPAREMFP(hookname); \
   538 		RETURN_META_VALUE(MRES_SUPERCEDE, (thisptr->*(u.mfp)) newparams); \
   539 	} while (0)
   540 
   541 /**
   542 *	@brief Get/generate callclass for an interface pointer
   543 *
   544 *	@param ifaceptr The interface pointer
   545 */
   546 template<class ifacetype>
   547 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   548 {
   549 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   550 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
   551 }
   552 
   553 template<class ifacetype>
   554 inline SourceHook::CallClass<ifacetype> *SH_GET_MCALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr, int ifacesize)
   555 {
   556 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   557 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), ifacesize));
   558 }
   559 
   560 template<class ifacetype>
   561 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   562 {
   563 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   564 }
   565 
   566 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   567 	do { \
   568 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   569 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   570 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   571 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   572 	} while (0)
   573 
   574 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   575 #define SH_GET_MCALLCLASS(ptr, size) SH_GET_MCALLCLASS_R(SH_GLOB_SHPTR, reinterpret_cast<SourceHook::EmptyClass*>(ptr), size)
   576 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   577 
   578 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   579 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   580 	post, handler)
   581 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   582 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   583 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   584 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   585 
   586 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   587 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   588 	post, handler)
   589 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   590 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   591 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   592 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   593 
   594 
   595 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   596 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   597 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   598 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   599 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   600 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   601 
   602 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   603 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   604 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   605 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   606 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   607 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   608 
   609 
   610 #define SH_NOATTRIB
   611 
   612 
   613 
   614 
   615 #if SH_COMP == SH_COMP_MSVC
   616 # define SH_SETUP_MFP(mfp) \
   617 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   618 #elif SH_COMP == SH_COMP_GCC
   619 # define SH_SETUP_MFP(mfp) \
   620 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   621 	reinterpret_cast<void**>(&mfp)[1] = 0;
   622 #else
   623 # error Not supported yet.
   624 #endif
   625 
   626 //////////////////////////////////////////////////////////////////////////
   627 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   628 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   629 
   630 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   631 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   632 	{ \
   633 		GetFuncInfo(funcptr, ms_MFI); \
   634 	} \
   635 	\
   636 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   637 	{ \
   638 		using namespace ::SourceHook; \
   639 		GetFuncInfo(funcptr, ms_MFI); \
   640 		/* Verify interface version */ \
   641 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   642 			return 1; \
   643 		\
   644 		if (action == HA_GetInfo) \
   645 		{ \
   646 			param->SetVersion(SH_HOOKMAN_VERSION); \
   647 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   648 				reinterpret_cast<const char*>(&ms_Proto)); \
   649 			\
   650 			MemFuncInfo mfi; \
   651 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   652 			param->SetHookfuncVfnptr( \
   653 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   654 			return 0; \
   655 		} \
   656 		else if (action == HA_Register) \
   657 		{ \
   658 			ms_HI = param; \
   659 			return 0; \
   660 		} \
   661 		else if (action == HA_Unregister) \
   662 		{ \
   663 			ms_HI = NULL; \
   664 			return 0; \
   665 		} \
   666 		else \
   667 			return 1; \
   668 	}
   669 
   670 // It has to be possible to use the macros in namespaces
   671 // -> So we need to access and extend the global SourceHook namespace
   672 // We use a namespace alias for this
   673 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   674 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   675 	{ \
   676 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   677 		static ::SourceHook::MemFuncInfo ms_MFI; \
   678 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   679 		static ::SourceHook::ProtoInfo ms_Proto; \
   680 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   681 
   682 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   683 	}; \
   684 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   685 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   686 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   687 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   688 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   689 	{ \
   690 		using namespace ::SourceHook; \
   691 		MemFuncInfo mfi; \
   692 		GetFuncInfo(funcptr, mfi); \
   693 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   694 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   695 		\
   696 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   697 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   698 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   699 	} \
   700 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   701 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   702 	{ \
   703 		using namespace ::SourceHook; \
   704 		MemFuncInfo mfi; \
   705 		GetFuncInfo(funcptr, mfi); \
   706 		if (mfi.thisptroffs < 0) \
   707 			return false; /* No virtual inheritance supported */ \
   708 		\
   709 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   710 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   711 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   712 	} \
   713 
   714 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   715 	struct SH_MFHCls(hookname) \
   716 	{ \
   717 		static SH_MFHCls(hookname) ms_Inst; \
   718 		static ::SourceHook::MemFuncInfo ms_MFI; \
   719 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   720 		static ::SourceHook::ProtoInfo ms_Proto; \
   721 		\
   722 		SH_MFHCls(hookname)() \
   723 		{ \
   724 			ms_MFI.isVirtual = true; \
   725 			ms_MFI.thisptroffs = pthisptroffs; \
   726 			ms_MFI.vtblindex = pvtblidx; \
   727 			ms_MFI.vtbloffs = pvtbloffs; \
   728 		} \
   729 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   730 		{ \
   731 			using namespace ::SourceHook; \
   732 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   733 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   734 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   735 			/* Verify interface version */ \
   736 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   737 				return 1; \
   738 			\
   739 			if (action == HA_GetInfo) \
   740 			{ \
   741 				param->SetVersion(SH_HOOKMAN_VERSION); \
   742 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   743 					reinterpret_cast<const char*>(&ms_Proto)); \
   744 				\
   745 				MemFuncInfo mfi; \
   746 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   747 				param->SetHookfuncVfnptr( \
   748 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   749 				return 0; \
   750 			} \
   751 			else if (action == HA_Register) \
   752 			{ \
   753 				ms_HI = param; \
   754 				return 0; \
   755 			} \
   756 			else if (action == HA_Unregister) \
   757 			{ \
   758 				ms_HI = NULL; \
   759 				return 0; \
   760 			} \
   761 			else \
   762 				return 1; \
   763 		}
   764 
   765 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   766 	}; \
   767 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   768 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   769 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   770 	bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   771 		SH_MFHCls(hookname)::FD handler) \
   772 	{ \
   773 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   774 			SH_MFHCls(hookname)::HookManPubFunc, \
   775 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   776 	} \
   777 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   778 		SH_MFHCls(hookname)::FD handler) \
   779 	{ \
   780 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   781 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   782 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   783 	} \
   784 
   785 
   786 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   787 	/* 1) Find the vfnptr */ \
   788 	using namespace ::SourceHook; \
   789 	void *ourvfnptr = reinterpret_cast<void*>( \
   790 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   791 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   792 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   793 	\
   794 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   795 	/* ... and the iface */ \
   796 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   797 	if (!ifinfo) \
   798 	{ \
   799 		/* The iface info was not found. Redirect the call to the original function. */ \
   800 		rettype (EmptyClass::*mfp)paramtypes; \
   801 		SH_SETUP_MFP(mfp); \
   802 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   803 	} \
   804 	/* 2) Declare some vars and set it up */ \
   805 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   806 	IHookList *prelist = ifinfo->GetPreHooks(); \
   807 	IHookList *postlist = ifinfo->GetPostHooks(); \
   808 	META_RES status = MRES_IGNORED; \
   809 	META_RES prev_res; \
   810 	META_RES cur_res; \
   811 	rettype orig_ret; \
   812 	rettype override_ret; \
   813 	rettype plugin_ret; \
   814 	void* ifptr; \
   815 	rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
   816 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
   817 
   818 #define SH_CALL_HOOKS(post, params) \
   819 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   820 	{ \
   821 		prev_res = MRES_IGNORED; \
   822 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   823 		{ \
   824 			cur_res = MRES_IGNORED; \
   825 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   826 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   827 			prev_res = cur_res; \
   828 			if (cur_res > status) \
   829 				status = cur_res; \
   830 			if (cur_res >= MRES_OVERRIDE) \
   831 				*pOverrideRet = plugin_ret; \
   832 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   833 			{ \
   834 				iter.SetToZero(); \
   835 				break; \
   836 			} \
   837 		} \
   838 	}
   839 
   840 #define SH_CALL_ORIG(rettype, paramtypes, params) \
   841 	if (status != MRES_SUPERCEDE) \
   842 	{ \
   843 		rettype (EmptyClass::*mfp)paramtypes; \
   844 		SH_SETUP_MFP(mfp); \
   845 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   846 	} \
   847 	else \
   848 		orig_ret = override_ret; \
   849 
   850 #define SH_RETURN() \
   851 	SH_GLOB_SHPTR->HookLoopEnd(); \
   852 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
   853 
   854 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   855 	SH_SETUPCALLS(rettype, paramtypes, params) \
   856 	SH_CALL_HOOKS(pre, params) \
   857 	SH_CALL_ORIG(rettype, paramtypes, params) \
   858 	SH_CALL_HOOKS(post, params) \
   859 	SH_RETURN()
   860 
   861 //////////////////////////////////////////////////////////////////////////
   862 #define SH_SETUPCALLS_void(paramtypes, params) \
   863 	/* 1) Find the vfnptr */ \
   864 	using namespace ::SourceHook; \
   865 	void *ourvfnptr = reinterpret_cast<void*>( \
   866 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   867 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   868 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   869 	\
   870 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   871 	/* ... and the iface */ \
   872 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   873 	if (!ifinfo) \
   874 	{ \
   875 		/* The iface info was not found. Redirect the call to the original function. */ \
   876 		void (EmptyClass::*mfp)paramtypes; \
   877 		SH_SETUP_MFP(mfp); \
   878 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   879 		return; \
   880 	} \
   881 	/* 2) Declare some vars and set it up */ \
   882 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   883 	IHookList *prelist = ifinfo->GetPreHooks(); \
   884 	IHookList *postlist = ifinfo->GetPostHooks(); \
   885 	META_RES status = MRES_IGNORED; \
   886 	META_RES prev_res; \
   887 	META_RES cur_res; \
   888 	void* ifptr; \
   889 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
   890 
   891 #define SH_CALL_HOOKS_void(post, params) \
   892 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   893 	{ \
   894 		prev_res = MRES_IGNORED; \
   895 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   896 		{ \
   897 			cur_res = MRES_IGNORED; \
   898 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   899 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   900 			prev_res = cur_res; \
   901 			if (cur_res > status) \
   902 				status = cur_res; \
   903 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   904 			{ \
   905 				iter.SetToZero(); \
   906 				break; \
   907 			} \
   908 		} \
   909 	}
   910 
   911 #define SH_CALL_ORIG_void(paramtypes, params) \
   912 	if (status != MRES_SUPERCEDE) \
   913 	{ \
   914 		void (EmptyClass::*mfp)paramtypes; \
   915 		SH_SETUP_MFP(mfp); \
   916 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   917 	}
   918 
   919 #define SH_RETURN_void() \
   920 	SH_GLOB_SHPTR->HookLoopEnd();
   921 
   922 #define SH_HANDLEFUNC_void(paramtypes, params) \
   923 	SH_SETUPCALLS_void(paramtypes, params) \
   924 	SH_CALL_HOOKS_void(pre, params) \
   925 	SH_CALL_ORIG_void(paramtypes, params) \
   926 	SH_CALL_HOOKS_void(post, params) \
   927 	SH_RETURN_void()
   928 
   929 
   930 // Special vafmt handlers
   931 // :TODO: what
   932 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
   933 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   934 	SH_CALL_HOOKS(pre, params_plug) \
   935 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
   936 	SH_CALL_HOOKS(post, params_plug) \
   937 	SH_RETURN()
   938 
   939 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
   940 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   941 	SH_CALL_HOOKS_void(pre, params_plug) \
   942 	SH_CALL_ORIG_void(paramtypes, params_orig) \
   943 	SH_CALL_HOOKS_void(post, params_plug) \
   944 	SH_RETURN_void()
   945 
   946 //////////////////////////////////////////////////////////////////////////
   947 
   948 @[$1,0,$a:
   949 // ********* Support for $1 arguments *********
   950 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   951 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   952 		(&ifacetype::ifacefunc))) \
   953 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
   954 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
   955 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
   956 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   957 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   958 	\
   959 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   960 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
   961 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   962 
   963 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   964 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   965 		(&ifacetype::ifacefunc))) \
   966 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
   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 	\
   973 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   974 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
   975 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   976 
   977 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   978 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   979 		(&ifacetype::ifacefunc))) \
   980 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
   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 int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
   994 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
   995 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   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 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
  1002 		{ \
  1003 			char buf[::SourceHook::STRBUF_LEN]; \
  1004 			va_list ap; \
  1005 			va_start(ap, fmt); \
  1006 			vsnprintf(buf, sizeof(buf), fmt, ap); \
  1007 			va_end(ap); \
  1008 			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)); \
  1009 		} \
  1010 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
  1011 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
  1012 	\
  1013 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
  1014 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
  1015 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
  1016 
  1017 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
  1018 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1019 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
  1020 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
  1021 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
  1022 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1023 		typedef rettype RetType; \
  1024 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1025 	\
  1026 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1027 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
  1028 		$1, __SourceHook_ParamSizesM_##hookname); \
  1029 
  1030 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
  1031 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1032 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
  1033 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
  1034 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
  1035 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
  1036 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
  1037 	\
  1038 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
  1039 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
  1040 		$1, __SourceHook_ParamSizesM_##hookname); \
  1041 
  1042 @]
  1043 
  1044 
  1045 //////////////////////////////////////////////////////////////////////////
  1046 // SH_CALL
  1047 
  1048 #if SH_COMP == SH_COMP_MSVC
  1049 
  1050 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1051 { \
  1052 	using namespace ::SourceHook; \
  1053 	MemFuncInfo mfi; \
  1054 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1055 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1056 	if (!origfunc) \
  1057 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1058 	\
  1059 	/* It's hooked. Call the original function. */ \
  1060 	union \
  1061 	{ \
  1062 		RetType(EmptyClass::*mfpnew)prms; \
  1063 		void *addr; \
  1064 	} u; \
  1065 	u.addr = origfunc; \
  1066 	\
  1067 	void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
  1068 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1069 }
  1070 
  1071 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1072 { \
  1073 	using namespace ::SourceHook; \
  1074 	char *adjustedthisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()) + m_ThisPtrOffs; \
  1075 	union \
  1076 	{ \
  1077 		RetType(EmptyClass::*mfpnew)prms; \
  1078 		void *addr; \
  1079 	} u; \
  1080 	u.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1081 	if (!u.addr) \
  1082 		u.addr = (*reinterpret_cast<void***>(adjustedthisptr + m_VtblOffs))[m_VtblIdx]; \
  1083 	\
  1084 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1085 }
  1086 
  1087 #elif SH_COMP == SH_COMP_GCC
  1088 
  1089 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1090 { \
  1091 	using namespace ::SourceHook; \
  1092 	MemFuncInfo mfi; \
  1093 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1094 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1095 	if (!origfunc) \
  1096 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1097 	\
  1098 	/* It's hooked. Call the original function. */ \
  1099 	union \
  1100 	{ \
  1101 		RetType(EmptyClass::*mfpnew)prms; \
  1102 		struct \
  1103 		{ \
  1104 			void *addr; \
  1105 			intptr_t adjustor; \
  1106 		} s; \
  1107 	} u; \
  1108 	u.s.addr = origfunc; \
  1109 	u.s.adjustor = mfi.thisptroffs; \
  1110 	\
  1111 	return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
  1112 }
  1113 
  1114 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1115 { \
  1116 	using namespace ::SourceHook; \
  1117 	char *thisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()); \
  1118 	union \
  1119 	{ \
  1120 		RetType(EmptyClass::*mfpnew)prms; \
  1121 		struct { \
  1122 			void *addr; \
  1123 			intptr_t adjustor; \
  1124 		} s; \
  1125 	} u; \
  1126 	u.s.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1127 	if (!u.s.addr) \
  1128 		u.s.addr = (*reinterpret_cast<void***>(thisptr + m_ThisPtrOffs + m_VtblOffs))[m_VtblIdx]; \
  1129 	\
  1130 	u.s.adjustor = m_ThisPtrOffs; \
  1131 	return (reinterpret_cast<EmptyClass*>(thisptr)->*u.mfpnew)call; \
  1132 }
  1133 
  1134 #endif
  1135 
  1136 namespace SourceHook
  1137 {
  1138 @[$1,0,$a:
  1139 	// Support for $1 arguments
  1140 	template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1141 	{
  1142 	   CCType *m_CC;
  1143 	   MFPType m_MFP;
  1144 	public:
  1145 	   ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
  1146 	
  1147 	   RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1148 	      SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1149 	         
  1150 	   @[$2,$1+1,$a:
  1151 	   template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1152 	      SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1153 	   @]
  1154 	};
  1155 
  1156 	template <class RetType@[$2,1,$1:, class Param$2@]> class MExecutableClass$1
  1157 	{
  1158 		ManualCallClass *m_CC;
  1159 		int m_ThisPtrOffs;
  1160 		int m_VtblIdx;
  1161 		int m_VtblOffs;
  1162 	public:
  1163 		MExecutableClass$1(ManualCallClass *cc, int vtbloffs, int vtblidx, int thisptroffs) : m_CC(cc),
  1164 			m_ThisPtrOffs(thisptroffs), m_VtblIdx(vtblidx), m_VtblOffs(vtbloffs) { }
  1165 
  1166 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1167 			SH_MAKE_MEXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1168 
  1169 		@[$2,$1+1,$a:
  1170 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1171 			SH_MAKE_MEXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1172 		@]
  1173 	};
  1174 @]
  1175 
  1176 }
  1177 
  1178 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1179 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1180 // The only purpose of the mfp2 parameter is to extract the return type
  1181 
  1182 @[$1,0,$a:
  1183 // Support for $1 arguments
  1184 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1185 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1186 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
  1187 {
  1188 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1189 }
  1190 
  1191 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1192 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1193 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
  1194 {
  1195 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1196 }
  1197 
  1198 template <class X, class RetType@[$2,1,$1:, class Param$2@]>
  1199 SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>
  1200 SH_MCALL2(SourceHook::ManualCallClass *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
  1201 {
  1202 	return SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>(ptr, vtbloffs, vtblidx, thisptroffs);
  1203 }
  1204 @]
  1205 
  1206 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1207 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1208 
  1209 @[$1,0,$a:
  1210 // Support for $1 arguments
  1211 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1212 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1213 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
  1214 {
  1215 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1216 }
  1217 
  1218 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1219 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1220 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
  1221 {
  1222 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1223 }
  1224 
  1225 @]
  1226 
  1227 #endif
  1228 
  1229 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
  1230 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1231 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1232 
  1233 #undef SH_MAKE_EXECUTABLECLASS_OB
  1234 
  1235 //////////////////////////////////////////////////////////////////////////
  1236 // SetOverrideRet and RecallGetIface for recalls
  1237 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1238 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1239 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1240 
  1241 namespace SourceHook
  1242 {
  1243 	template <class RetType>
  1244 	void SetOverrideResult(ISourceHook *shptr, const RetType res)
  1245 	{
  1246 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1247 	}
  1248 
  1249 @[$1,0,$a:
  1250 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1251 	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]), const RetType res)
  1252 	{
  1253 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1254 	}
  1255 
  1256 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1257 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1258 	{
  1259 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1260 	}
  1261 @]
  1262 }
  1263 
  1264 #endif
  1265 	// The pope is dead. -> :(