sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sun May 14 19:07:01 2006 +0000 (2006-05-14)
changeset 200 e1426a1a3fcd
parent 199 7a5b326bb7f8
child 211 af7f4f2148f5
permissions -rwxr-xr-x
Fixed linux/gcc/amd64
     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 // NEVER-EVER call these from post hooks!
   458 // also, only call it from the hook handlers directly!
   459 // :TODO: enforce it
   460 
   461 // Why take a memfuncptr instead of iface and func when we have to deduce the iface anyway now?
   462 // Well, without it, there'd be no way to specify which overloaded version we want in _VALUE
   463 
   464 // SourceHook::SetOverrideRet is defined later.
   465 #define RETURN_META_NEWPARAMS(result, memfuncptr, newparams) \
   466 	do { \
   467 		SET_META_RESULT(result); \
   468 		SH_GLOB_SHPTR->DoRecall(); \
   469 		(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams; \
   470 		RETURN_META(MRES_SUPERCEDE); \
   471 	} while (0)
   472 
   473 #define RETURN_META_VALUE_NEWPARAMS(result, value, memfuncptr, newparams) \
   474 	do { \
   475 		SET_META_RESULT(result); \
   476 		SH_GLOB_SHPTR->DoRecall(); \
   477 		if ((result) >= MRES_OVERRIDE) \
   478 		{ \
   479 			/* meh, set the override result here because we don't get a chance to return */ \
   480 			/* before continuing the hook loop through the recall */ \
   481 			SourceHook::SetOverrideResult(SH_GLOB_SHPTR, memfuncptr, value); \
   482 		} \
   483 		RETURN_META_VALUE(MRES_SUPERCEDE, \
   484 			(SourceHook::RecallGetIface(SH_GLOB_SHPTR, memfuncptr)->*(memfuncptr)) newparams); \
   485 	} while (0)
   486 
   487 /**
   488 *	@brief Get/generate callclass for an interface pointer
   489 *
   490 *	@param ifaceptr The interface pointer
   491 */
   492 template<class ifacetype>
   493 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   494 {
   495 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   496 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
   497 }
   498 
   499 template<class ifacetype>
   500 inline SourceHook::CallClass<ifacetype> *SH_GET_MCALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr, int ifacesize)
   501 {
   502 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   503 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), ifacesize));
   504 }
   505 
   506 template<class ifacetype>
   507 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   508 {
   509 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   510 }
   511 
   512 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   513 	do { \
   514 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   515 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   516 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   517 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   518 	} while (0)
   519 
   520 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   521 #define SH_GET_MCALLCLASS(ptr, size) SH_GET_MCALLCLASS_R(SH_GLOB_SHPTR, reinterpret_cast<SourceHook::EmptyClass*>(ptr), size)
   522 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   523 
   524 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   525 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   526 	post, handler)
   527 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   528 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   529 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   530 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   531 
   532 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   533 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   534 	post, handler)
   535 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   536 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   537 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   538 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   539 
   540 
   541 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   542 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   543 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   544 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   545 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   546 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   547 
   548 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   549 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   550 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   551 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   552 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   553 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   554 
   555 
   556 #define SH_NOATTRIB
   557 
   558 
   559 
   560 
   561 #if SH_COMP == SH_COMP_MSVC
   562 # define SH_SETUP_MFP(mfp) \
   563 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   564 #elif SH_COMP == SH_COMP_GCC
   565 # define SH_SETUP_MFP(mfp) \
   566 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   567 	reinterpret_cast<void**>(&mfp)[1] = 0;
   568 #else
   569 # error Not supported yet.
   570 #endif
   571 
   572 //////////////////////////////////////////////////////////////////////////
   573 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   574 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   575 
   576 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   577 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   578 	{ \
   579 		GetFuncInfo(funcptr, ms_MFI); \
   580 	} \
   581 	\
   582 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   583 	{ \
   584 		using namespace ::SourceHook; \
   585 		GetFuncInfo(funcptr, ms_MFI); \
   586 		/* Verify interface version */ \
   587 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   588 			return 1; \
   589 		\
   590 		if (action == HA_GetInfo) \
   591 		{ \
   592 			param->SetVersion(SH_HOOKMAN_VERSION); \
   593 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   594 				reinterpret_cast<const char*>(&ms_Proto)); \
   595 			\
   596 			MemFuncInfo mfi; \
   597 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   598 			param->SetHookfuncVfnptr( \
   599 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   600 			return 0; \
   601 		} \
   602 		else if (action == HA_Register) \
   603 		{ \
   604 			ms_HI = param; \
   605 			return 0; \
   606 		} \
   607 		else if (action == HA_Unregister) \
   608 		{ \
   609 			ms_HI = NULL; \
   610 			return 0; \
   611 		} \
   612 		else \
   613 			return 1; \
   614 	}
   615 
   616 // It has to be possible to use the macros in namespaces
   617 // -> So we need to access and extend the global SourceHook namespace
   618 // We use a namespace alias for this
   619 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   620 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   621 	{ \
   622 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   623 		static ::SourceHook::MemFuncInfo ms_MFI; \
   624 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   625 		static ::SourceHook::ProtoInfo ms_Proto; \
   626 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   627 
   628 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   629 	}; \
   630 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   631 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   632 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   633 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   634 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   635 	{ \
   636 		using namespace ::SourceHook; \
   637 		MemFuncInfo mfi; \
   638 		GetFuncInfo(funcptr, mfi); \
   639 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   640 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   641 		\
   642 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   643 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   644 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   645 	} \
   646 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   647 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   648 	{ \
   649 		using namespace ::SourceHook; \
   650 		MemFuncInfo mfi; \
   651 		GetFuncInfo(funcptr, mfi); \
   652 		if (mfi.thisptroffs < 0) \
   653 			return false; /* No virtual inheritance supported */ \
   654 		\
   655 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   656 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   657 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   658 	} \
   659 
   660 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   661 	struct SH_MFHCls(hookname) \
   662 	{ \
   663 		static SH_MFHCls(hookname) ms_Inst; \
   664 		static ::SourceHook::MemFuncInfo ms_MFI; \
   665 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   666 		static ::SourceHook::ProtoInfo ms_Proto; \
   667 		\
   668 		SH_MFHCls(hookname)() \
   669 		{ \
   670 			ms_MFI.isVirtual = true; \
   671 			ms_MFI.thisptroffs = pthisptroffs; \
   672 			ms_MFI.vtblindex = pvtblidx; \
   673 			ms_MFI.vtbloffs = pvtbloffs; \
   674 		} \
   675 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   676 		{ \
   677 			using namespace ::SourceHook; \
   678 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   679 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   680 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   681 			/* Verify interface version */ \
   682 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   683 				return 1; \
   684 			\
   685 			if (action == HA_GetInfo) \
   686 			{ \
   687 				param->SetVersion(SH_HOOKMAN_VERSION); \
   688 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   689 					reinterpret_cast<const char*>(&ms_Proto)); \
   690 				\
   691 				MemFuncInfo mfi; \
   692 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   693 				param->SetHookfuncVfnptr( \
   694 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   695 				return 0; \
   696 			} \
   697 			else if (action == HA_Register) \
   698 			{ \
   699 				ms_HI = param; \
   700 				return 0; \
   701 			} \
   702 			else if (action == HA_Unregister) \
   703 			{ \
   704 				ms_HI = NULL; \
   705 				return 0; \
   706 			} \
   707 			else \
   708 				return 1; \
   709 		}
   710 
   711 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   712 	}; \
   713 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   714 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   715 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   716 	bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   717 		SH_MFHCls(hookname)::FD handler) \
   718 	{ \
   719 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   720 			SH_MFHCls(hookname)::HookManPubFunc, \
   721 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   722 	} \
   723 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   724 		SH_MFHCls(hookname)::FD handler) \
   725 	{ \
   726 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   727 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   728 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   729 	} \
   730 
   731 
   732 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   733 	/* 1) Find the vfnptr */ \
   734 	using namespace ::SourceHook; \
   735 	void *ourvfnptr = reinterpret_cast<void*>( \
   736 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   737 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   738 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   739 	\
   740 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   741 	/* ... and the iface */ \
   742 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   743 	if (!ifinfo) \
   744 	{ \
   745 		/* The iface info was not found. Redirect the call to the original function. */ \
   746 		rettype (EmptyClass::*mfp)paramtypes; \
   747 		SH_SETUP_MFP(mfp); \
   748 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   749 	} \
   750 	/* 2) Declare some vars and set it up */ \
   751 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   752 	IHookList *prelist = ifinfo->GetPreHooks(); \
   753 	IHookList *postlist = ifinfo->GetPostHooks(); \
   754 	META_RES status = MRES_IGNORED; \
   755 	META_RES prev_res; \
   756 	META_RES cur_res; \
   757 	rettype orig_ret; \
   758 	rettype override_ret; \
   759 	rettype plugin_ret; \
   760 	void* ifptr; \
   761 	rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
   762 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
   763 
   764 #define SH_CALL_HOOKS(post, params) \
   765 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   766 	{ \
   767 		prev_res = MRES_IGNORED; \
   768 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   769 		{ \
   770 			cur_res = MRES_IGNORED; \
   771 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   772 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   773 			prev_res = cur_res; \
   774 			if (cur_res > status) \
   775 				status = cur_res; \
   776 			if (cur_res >= MRES_OVERRIDE) \
   777 				*pOverrideRet = plugin_ret; \
   778 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   779 			{ \
   780 				iter.SetToZero(); \
   781 				break; \
   782 			} \
   783 		} \
   784 	}
   785 
   786 #define SH_CALL_ORIG(rettype, paramtypes, params) \
   787 	if (status != MRES_SUPERCEDE) \
   788 	{ \
   789 		rettype (EmptyClass::*mfp)paramtypes; \
   790 		SH_SETUP_MFP(mfp); \
   791 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   792 	} \
   793 	else \
   794 		orig_ret = override_ret; \
   795 
   796 #define SH_RETURN() \
   797 	SH_GLOB_SHPTR->HookLoopEnd(); \
   798 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
   799 
   800 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   801 	SH_SETUPCALLS(rettype, paramtypes, params) \
   802 	SH_CALL_HOOKS(pre, params) \
   803 	SH_CALL_ORIG(rettype, paramtypes, params) \
   804 	SH_CALL_HOOKS(post, params) \
   805 	SH_RETURN()
   806 
   807 //////////////////////////////////////////////////////////////////////////
   808 #define SH_SETUPCALLS_void(paramtypes, params) \
   809 	/* 1) Find the vfnptr */ \
   810 	using namespace ::SourceHook; \
   811 	void *ourvfnptr = reinterpret_cast<void*>( \
   812 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   813 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   814 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   815 	\
   816 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   817 	/* ... and the iface */ \
   818 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   819 	if (!ifinfo) \
   820 	{ \
   821 		/* The iface info was not found. Redirect the call to the original function. */ \
   822 		void (EmptyClass::*mfp)paramtypes; \
   823 		SH_SETUP_MFP(mfp); \
   824 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   825 		return; \
   826 	} \
   827 	/* 2) Declare some vars and set it up */ \
   828 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   829 	IHookList *prelist = ifinfo->GetPreHooks(); \
   830 	IHookList *postlist = ifinfo->GetPostHooks(); \
   831 	META_RES status = MRES_IGNORED; \
   832 	META_RES prev_res; \
   833 	META_RES cur_res; \
   834 	void* ifptr; \
   835 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
   836 
   837 #define SH_CALL_HOOKS_void(post, params) \
   838 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   839 	{ \
   840 		prev_res = MRES_IGNORED; \
   841 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   842 		{ \
   843 			cur_res = MRES_IGNORED; \
   844 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   845 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   846 			prev_res = cur_res; \
   847 			if (cur_res > status) \
   848 				status = cur_res; \
   849 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   850 			{ \
   851 				iter.SetToZero(); \
   852 				break; \
   853 			} \
   854 		} \
   855 	}
   856 
   857 #define SH_CALL_ORIG_void(paramtypes, params) \
   858 	if (status != MRES_SUPERCEDE) \
   859 	{ \
   860 		void (EmptyClass::*mfp)paramtypes; \
   861 		SH_SETUP_MFP(mfp); \
   862 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   863 	}
   864 
   865 #define SH_RETURN_void() \
   866 	SH_GLOB_SHPTR->HookLoopEnd();
   867 
   868 #define SH_HANDLEFUNC_void(paramtypes, params) \
   869 	SH_SETUPCALLS_void(paramtypes, params) \
   870 	SH_CALL_HOOKS_void(pre, params) \
   871 	SH_CALL_ORIG_void(paramtypes, params) \
   872 	SH_CALL_HOOKS_void(post, params) \
   873 	SH_RETURN_void()
   874 
   875 
   876 // Special vafmt handlers
   877 // :TODO: what
   878 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
   879 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   880 	SH_CALL_HOOKS(pre, params_plug) \
   881 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
   882 	SH_CALL_HOOKS(post, params_plug) \
   883 	SH_RETURN()
   884 
   885 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
   886 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   887 	SH_CALL_HOOKS_void(pre, params_plug) \
   888 	SH_CALL_ORIG_void(paramtypes, params_orig) \
   889 	SH_CALL_HOOKS_void(post, params_plug) \
   890 	SH_RETURN_void()
   891 
   892 //////////////////////////////////////////////////////////////////////////
   893 
   894 @[$1,0,$a:
   895 // ********* Support for $1 arguments *********
   896 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   897 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   898 		(&ifacetype::ifacefunc))) \
   899 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
   900 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
   901 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
   902 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   903 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   904 	\
   905 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   906 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
   907 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   908 
   909 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   910 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   911 		(&ifacetype::ifacefunc))) \
   912 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
   913 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   914 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
   915 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   916 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   917 	\
   918 	\
   919 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   920 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
   921 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   922 
   923 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   924 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   925 		(&ifacetype::ifacefunc))) \
   926 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
   927 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
   928 		{ \
   929 			char buf[::SourceHook::STRBUF_LEN]; \
   930 			va_list ap; \
   931 			va_start(ap, fmt); \
   932 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   933 			va_end(ap); \
   934 			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); \
   935 		} \
   936 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   937 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
   938 	\
   939 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
   940 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
   941 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   942 
   943 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   944 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   945 		(&ifacetype::ifacefunc))) \
   946 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
   947 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
   948 		{ \
   949 			char buf[::SourceHook::STRBUF_LEN]; \
   950 			va_list ap; \
   951 			va_start(ap, fmt); \
   952 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   953 			va_end(ap); \
   954 			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)); \
   955 		} \
   956 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   957 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
   958 	\
   959 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
   960 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
   961 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   962 
   963 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
   964 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   965 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
   966 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
   967 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
   968 		typedef rettype(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
   969 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   970 	\
   971 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   972 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
   973 		$1, __SourceHook_ParamSizesM_##hookname); \
   974 
   975 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
   976 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   977 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
   978 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   979 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
   980 		typedef void(::SourceHook::EmptyClass::*ECMFP)(@[$2,1,$1|, :param$2@]); \
   981 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   982 	\
   983 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   984 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
   985 		$1, __SourceHook_ParamSizesM_##hookname); \
   986 
   987 @]
   988 
   989 
   990 //////////////////////////////////////////////////////////////////////////
   991 // SH_CALL
   992 
   993 #if SH_COMP == SH_COMP_MSVC
   994 
   995 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   996 { \
   997 	using namespace ::SourceHook; \
   998 	MemFuncInfo mfi; \
   999 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1000 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1001 	if (!origfunc) \
  1002 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1003 	\
  1004 	/* It's hooked. Call the original function. */ \
  1005 	union \
  1006 	{ \
  1007 		RetType(EmptyClass::*mfpnew)prms; \
  1008 		void *addr; \
  1009 	} u; \
  1010 	u.addr = origfunc; \
  1011 	\
  1012 	void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
  1013 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1014 }
  1015 
  1016 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1017 { \
  1018 	using namespace ::SourceHook; \
  1019 	char *adjustedthisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()) + m_ThisPtrOffs; \
  1020 	union \
  1021 	{ \
  1022 		RetType(EmptyClass::*mfpnew)prms; \
  1023 		void *addr; \
  1024 	} u; \
  1025 	u.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1026 	if (!u.addr) \
  1027 		u.addr = (*reinterpret_cast<void***>(adjustedthisptr + m_VtblOffs))[m_VtblIdx]; \
  1028 	\
  1029 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1030 }
  1031 
  1032 #elif SH_COMP == SH_COMP_GCC
  1033 
  1034 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1035 { \
  1036 	using namespace ::SourceHook; \
  1037 	MemFuncInfo mfi; \
  1038 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1039 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1040 	if (!origfunc) \
  1041 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1042 	\
  1043 	/* It's hooked. Call the original function. */ \
  1044 	union \
  1045 	{ \
  1046 		RetType(EmptyClass::*mfpnew)prms; \
  1047 		struct \
  1048 		{ \
  1049 			void *addr; \
  1050 			intptr_t adjustor; \
  1051 		} s; \
  1052 	} u; \
  1053 	u.s.addr = origfunc; \
  1054 	u.s.adjustor = mfi.thisptroffs; \
  1055 	\
  1056 	return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
  1057 }
  1058 
  1059 # define SH_MAKE_MEXECUTABLECLASS_OB(call, prms) \
  1060 { \
  1061 	using namespace ::SourceHook; \
  1062 	char *thisptr = reinterpret_cast<char*>(m_CC->GetThisPtr()); \
  1063 	union \
  1064 	{ \
  1065 		RetType(EmptyClass::*mfpnew)prms; \
  1066 		struct { \
  1067 			void *addr; \
  1068 			intptr_t adjustor; \
  1069 		} s; \
  1070 	} u; \
  1071 	u.s.addr = m_CC->GetOrigFunc(m_ThisPtrOffs + m_VtblOffs, m_VtblIdx); \
  1072 	if (!u.s.addr) \
  1073 		u.s.addr = (*reinterpret_cast<void***>(thisptr + m_ThisPtrOffs + m_VtblOffs))[m_VtblIdx]; \
  1074 	\
  1075 	u.s.adjustor = m_ThisPtrOffs; \
  1076 	return (reinterpret_cast<EmptyClass*>(thisptr)->*u.mfpnew)call; \
  1077 }
  1078 
  1079 #endif
  1080 
  1081 namespace SourceHook
  1082 {
  1083 @[$1,0,$a:
  1084 	// Support for $1 arguments
  1085 	template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1086 	{
  1087 	   CCType *m_CC;
  1088 	   MFPType m_MFP;
  1089 	public:
  1090 	   ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
  1091 	
  1092 	   RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1093 	      SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1094 	         
  1095 	   @[$2,$1+1,$a:
  1096 	   template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1097 	      SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1098 	   @]
  1099 	};
  1100 
  1101 	template <class RetType@[$2,1,$1:, class Param$2@]> class MExecutableClass$1
  1102 	{
  1103 		ManualCallClass *m_CC;
  1104 		int m_ThisPtrOffs;
  1105 		int m_VtblIdx;
  1106 		int m_VtblOffs;
  1107 	public:
  1108 		MExecutableClass$1(ManualCallClass *cc, int vtbloffs, int vtblidx, int thisptroffs) : m_CC(cc),
  1109 			m_ThisPtrOffs(thisptroffs), m_VtblIdx(vtblidx), m_VtblOffs(vtbloffs) { }
  1110 
  1111 		RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1112 			SH_MAKE_MEXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1113 
  1114 		@[$2,$1+1,$a:
  1115 		template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1116 			SH_MAKE_MEXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1117 		@]
  1118 	};
  1119 @]
  1120 
  1121 }
  1122 
  1123 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1124 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1125 // The only purpose of the mfp2 parameter is to extract the return type
  1126 
  1127 @[$1,0,$a:
  1128 // Support for $1 arguments
  1129 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1130 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1131 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
  1132 {
  1133 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1134 }
  1135 
  1136 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1137 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1138 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
  1139 {
  1140 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1141 }
  1142 
  1143 template <class X, class RetType@[$2,1,$1:, class Param$2@]>
  1144 SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>
  1145 SH_MCALL2(SourceHook::ManualCallClass *ptr, RetType(X::*mfp)(@[$2,1,$1|, :Param$2@]), int vtblidx, int vtbloffs, int thisptroffs)
  1146 {
  1147 	return SourceHook::MExecutableClass$1<RetType@[$2,1,$1:, Param$2@]>(ptr, vtbloffs, vtblidx, thisptroffs);
  1148 }
  1149 @]
  1150 
  1151 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1152 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1153 
  1154 @[$1,0,$a:
  1155 // Support for $1 arguments
  1156 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1157 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1158 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
  1159 {
  1160 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1161 }
  1162 
  1163 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1164 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1165 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
  1166 {
  1167 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1168 }
  1169 
  1170 @]
  1171 
  1172 #endif
  1173 
  1174 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
  1175 #define SH_MCALL(ptr, mhookname) SH_MCALL2((ptr), SH_MFHCls(mhookname)::ECMFP(), SH_MFHCls(mhookname)::ms_MFI.vtblindex, \
  1176 	SH_MFHCls(mhookname)::ms_MFI.vtbloffs, SH_MFHCls(mhookname)::ms_MFI.thisptroffs)
  1177 
  1178 #undef SH_MAKE_EXECUTABLECLASS_OB
  1179 
  1180 //////////////////////////////////////////////////////////////////////////
  1181 // SetOverrideRet and RecallGetIface for recalls
  1182 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1183 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1184 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1185 
  1186 namespace SourceHook
  1187 {
  1188 @[$1,0,$a:
  1189 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1190 	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]), const RetType res)
  1191 	{
  1192 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1193 	}
  1194 
  1195 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1196 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1197 	{
  1198 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1199 	}
  1200 @]
  1201 }
  1202 
  1203 #endif
  1204 	// The pope is dead. -> :(