sourcehook/generate/sourcehook.hxx
author Pavol Marko <pavolmarko@gmail.com>
Sat May 13 17:57:35 2006 +0000 (2006-05-13)
changeset 196 25d98645f668
parent 165 76da01197c4c
child 199 7a5b326bb7f8
permissions -rwxr-xr-x
rewritten sh's template files to use new shworker syntax, fixed sh_call. tbh, i'm good
     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 
   302 	/**
   303 	*	@brief The main SourceHook interface
   304 	*/
   305 	class ISourceHook
   306 	{
   307 	public:
   308 		/**
   309 		*	@brief Return interface version
   310 		*/
   311 		virtual int GetIfaceVersion() = 0;
   312 
   313 		/**
   314 		*	@brief Return implementation version
   315 		*/
   316 		virtual int GetImplVersion() = 0;
   317 
   318 		/**
   319 		*	@brief Add a hook.
   320 		*
   321 		*	@return True if the function succeeded, false otherwise
   322 		*
   323 		*	@param plug The unique identifier of the plugin that calls this function
   324 		*	@param iface The interface pointer
   325 		*	@param ifacesize The size of the class iface points to
   326 		*	@param myHookMan A hook manager function that should be capable of handling the function
   327 		*	@param handler A pointer to a FastDelegate containing the hook handler
   328 		*	@param post Set to true if you want a post handler
   329 		*/
   330 		virtual bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   331 			ISHDelegate *handler, bool post) = 0;
   332 
   333 		/**
   334 		*	@brief Removes a hook.
   335 		*
   336 		*	@return True if the function succeeded, false otherwise
   337 		*
   338 		*	@param plug The unique identifier of the plugin that calls this function
   339 		*	@param iface The interface pointer
   340 		*	@param myHookMan A hook manager function that should be capable of handling the function
   341 		*	@param handler A pointer to a FastDelegate containing the hook handler
   342 		*	@param post Set to true if you want a post handler
   343 		*/
   344 		virtual bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   345 			ISHDelegate *handler, bool post) = 0;
   346 
   347 		/**
   348 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   349 		*
   350 		*	@param plug The unique identifier of the plugin in question
   351 		*/
   352 		virtual bool IsPluginInUse(Plugin plug) = 0;
   353 
   354 		/**
   355 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   356 		*
   357 		*	@param iface The interface pointer
   358 		*	@param size Size of the class instance
   359 		*/
   360 		virtual GenericCallClass *GetCallClass(void *iface, size_t size) = 0;
   361 
   362 		/**
   363 		*	@brief Release a callclass
   364 		*
   365 		*	@param ptr Pointer to the callclass
   366 		*/
   367 		virtual void ReleaseCallClass(GenericCallClass *ptr) = 0;
   368 
   369 		virtual void SetRes(META_RES res) = 0;				//!< Sets the meta result
   370 		virtual META_RES GetPrevRes() = 0;					//!< Gets the meta result of the
   371 															//!<  previously calledhandler
   372 		virtual META_RES GetStatus() = 0;					//!< Gets the highest meta result
   373 		virtual const void *GetOrigRet() = 0;				//!< Gets the original result.
   374 															//!<  If not in post function, undefined
   375 		virtual const void *GetOverrideRet() = 0;			//!< Gets the override result.
   376 															//!<  If none is specified, NULL
   377 		virtual void *GetIfacePtr() = 0;					//!< Gets the interface pointer
   378 		//////////////////////////////////////////////////////////////////////////
   379 		// For hook managers
   380 		virtual void HookLoopBegin(IIface *pIface) = 0;			//!< Should be called when a hook loop begins
   381 		virtual void HookLoopEnd() = 0;							//!< Should be called when a hook loop exits
   382 		virtual void SetCurResPtr(META_RES *mres) = 0;			//!< Sets pointer to the current meta result
   383 		virtual void SetPrevResPtr(META_RES *mres) = 0;			//!< Sets pointer to previous meta result
   384 		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
   385 		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
   386 		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
   387 		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
   388 		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
   389 
   390 		/**
   391 		*	@brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
   392 		*
   393 		*	@param plug The owner of the hook manager
   394 		*   @param pubFunc The hook manager's info function
   395 		*/
   396 		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
   397 
   398 		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
   399 		/*
   400 			HOW RECALLS WORK:
   401 			
   402 			The problem:
   403 				Users want the ability to change parameters of the called function
   404 				from inside their handler.
   405 			The solution:
   406 				1) Mark as "recall"
   407 				2) Recall the function
   408 				3) => SH's hook func gets called:
   409 				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
   410 				  5) does all iteration and returns normally
   411 		        6) The user's handler returns immediately
   412 				7) The hook func returns immediately as well
   413 
   414 				Also note that the recalled hookfunc starts with the status the recalling hookfunc
   415 				ended with. The last handler (doing the recall) is also able to specify its own
   416 				META_RES.
   417 		*/
   418 
   419 		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
   420 
   421 		/**
   422 		*	@brief Set up the hook loop. Equivalent to calling:
   423 		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
   424 		*
   425 		*	@param statusPtr		pointer to status variable
   426 		*	@param prevResPtr		pointer to previous result variable
   427 		*	@param curResPtr		pointer to current result variable
   428 		*	@param ifacePtrPtr		pointer to interface this pointer variable
   429 		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
   430 		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
   431 		*	
   432 		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
   433 		*		when the hook func is being called as part of a recall
   434 		*/
   435 		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   436 			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
   437 
   438 		//!< 
   439 	};
   440 }
   441 
   442 /************************************************************************/
   443 /* High level interface                                                 */
   444 /************************************************************************/
   445 #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
   446 #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
   447 #define META_RESULT_ORIG_RET(type)			*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
   448 #define META_RESULT_OVERRIDE_RET(type)		*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
   449 #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
   450 
   451 #define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
   452 #define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
   453 #define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
   454 
   455 
   456 // NEVER-EVER call these from post hooks!
   457 // also, only call it 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 /**
   487 *	@brief Get/generate callclass for an interface pointer
   488 *
   489 *	@param ifaceptr The interface pointer
   490 */
   491 template<class ifacetype>
   492 inline SourceHook::CallClass<ifacetype> *SH_GET_CALLCLASS_R(SourceHook::ISourceHook *shptr, ifacetype *ptr)
   493 {
   494 	return reinterpret_cast<SourceHook::CallClass<ifacetype>*>(
   495 		shptr->GetCallClass(reinterpret_cast<void*>(ptr), sizeof(ifacetype)));
   496 }
   497 
   498 template<class ifacetype>
   499 inline void SH_RELEASE_CALLCLASS_R(SourceHook::ISourceHook *shptr, SourceHook::CallClass<ifacetype> *ptr)
   500 {
   501 	shptr->ReleaseCallClass(reinterpret_cast<SourceHook::GenericCallClass*>(ptr));
   502 }
   503 
   504 #define SH_MANUALHOOK_RECONFIGURE(hookname, pvtblindex, pvtbloffs, pthisptroffs) \
   505 	do { \
   506 		SH_GLOB_SHPTR->RemoveHookManager(SH_GLOB_PLUGPTR, SH_MFHCls(hookname)::HookManPubFunc); \
   507 		SH_MFHCls(hookname)::ms_MFI.thisptroffs = pthisptroffs; \
   508 		SH_MFHCls(hookname)::ms_MFI.vtblindex = pvtblindex; \
   509 		SH_MFHCls(hookname)::ms_MFI.vtbloffs = pvtbloffs; \
   510 	} while (0)
   511 
   512 #define SH_GET_CALLCLASS(ptr) SH_GET_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   513 #define SH_RELEASE_CALLCLASS(ptr) SH_RELEASE_CALLCLASS_R(SH_GLOB_SHPTR, ptr)
   514 
   515 #define SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   516 	__SourceHook_FHAdd##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   517 	post, handler)
   518 #define SH_ADD_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   519 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   520 #define SH_ADD_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   521 	SH_ADD_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   522 
   523 #define SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, handler, post) \
   524 	__SourceHook_FHRemove##ifacetype##ifacefunc((void*)SourceHook::implicit_cast<ifacetype*>(ifaceptr), \
   525 	post, handler)
   526 #define SH_REMOVE_HOOK_STATICFUNC(ifacetype, ifacefunc, ifaceptr, handler, post) \
   527 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   528 #define SH_REMOVE_HOOK_MEMFUNC(ifacetype, ifacefunc, ifaceptr, handler_inst, handler_func, post) \
   529 	SH_REMOVE_HOOK(ifacetype, ifacefunc, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   530 
   531 
   532 #define SH_ADD_MANUALHOOK(hookname, ifaceptr, handler, post) \
   533 	__SourceHook_FHMAdd##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   534 #define SH_ADD_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   535 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   536 #define SH_ADD_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   537 	SH_ADD_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   538 
   539 #define SH_REMOVE_MANUALHOOK(hookname, ifaceptr, handler, post) \
   540 	__SourceHook_FHMRemove##hookname(reinterpret_cast<void*>(ifaceptr), post, handler)
   541 #define SH_REMOVE_MANUALHOOK_STATICFUNC(hookname, ifaceptr, handler, post) \
   542 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler), post)
   543 #define SH_REMOVE_MANUALHOOK_MEMFUNC(hookname, ifaceptr, handler_inst, handler_func, post) \
   544 	SH_REMOVE_MANUALHOOK(hookname, ifaceptr, fastdelegate::MakeDelegate(handler_inst, handler_func), post)
   545 
   546 
   547 #define SH_NOATTRIB
   548 
   549 
   550 
   551 
   552 #if SH_COMP == SH_COMP_MSVC
   553 # define SH_SETUP_MFP(mfp) \
   554 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
   555 #elif SH_COMP == SH_COMP_GCC
   556 # define SH_SETUP_MFP(mfp) \
   557 	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
   558 	reinterpret_cast<void**>(&mfp)[1] = 0;
   559 #else
   560 # error Not supported yet.
   561 #endif
   562 
   563 //////////////////////////////////////////////////////////////////////////
   564 #define SH_FHCls(ift, iff, ov) __SourceHook_FHCls_##ift##iff##ov
   565 #define SH_MFHCls(hookname) __SourceHook_MFHCls_##hookname
   566 
   567 #define SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr) \
   568 	SH_FHCls(ifacetype,ifacefunc,overload)() \
   569 	{ \
   570 		GetFuncInfo(funcptr, ms_MFI); \
   571 	} \
   572 	\
   573 	static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   574 	{ \
   575 		using namespace ::SourceHook; \
   576 		GetFuncInfo(funcptr, ms_MFI); \
   577 		/* Verify interface version */ \
   578 		if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   579 			return 1; \
   580 		\
   581 		if (action == HA_GetInfo) \
   582 		{ \
   583 			param->SetVersion(SH_HOOKMAN_VERSION); \
   584 			param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   585 				reinterpret_cast<const char*>(&ms_Proto)); \
   586 			\
   587 			MemFuncInfo mfi; \
   588 			GetFuncInfo(&SH_FHCls(ifacetype,ifacefunc,overload)::Func, mfi); \
   589 			param->SetHookfuncVfnptr( \
   590 				reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   591 			return 0; \
   592 		} \
   593 		else if (action == HA_Register) \
   594 		{ \
   595 			ms_HI = param; \
   596 			return 0; \
   597 		} \
   598 		else if (action == HA_Unregister) \
   599 		{ \
   600 			ms_HI = NULL; \
   601 			return 0; \
   602 		} \
   603 		else \
   604 			return 1; \
   605 	}
   606 
   607 // It has to be possible to use the macros in namespaces
   608 // -> So we need to access and extend the global SourceHook namespace
   609 // We use a namespace alias for this
   610 #define SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, funcptr) \
   611 	struct SH_FHCls(ifacetype,ifacefunc,overload) \
   612 	{ \
   613 		static SH_FHCls(ifacetype,ifacefunc,overload) ms_Inst; \
   614 		static ::SourceHook::MemFuncInfo ms_MFI; \
   615 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   616 		static ::SourceHook::ProtoInfo ms_Proto; \
   617 		SHINT_MAKE_HOOKMANPUBFUNC(ifacetype, ifacefunc, overload, funcptr)
   618 
   619 #define SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, funcptr) \
   620 	}; \
   621 	SH_FHCls(ifacetype,ifacefunc,overload) SH_FHCls(ifacetype,ifacefunc,overload)::ms_Inst; \
   622 	::SourceHook::MemFuncInfo SH_FHCls(ifacetype,ifacefunc,overload)::ms_MFI; \
   623 	::SourceHook::IHookManagerInfo *SH_FHCls(ifacetype,ifacefunc,overload)::ms_HI; \
   624 	bool __SourceHook_FHAdd##ifacetype##ifacefunc(void *iface, bool post, \
   625 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   626 	{ \
   627 		using namespace ::SourceHook; \
   628 		MemFuncInfo mfi; \
   629 		GetFuncInfo(funcptr, mfi); \
   630 		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   631 			return false; /* No non-virtual functions / virtual inheritance supported */ \
   632 		\
   633 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   634 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   635 			new CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD>(handler), post); \
   636 	} \
   637 	bool __SourceHook_FHRemove##ifacetype##ifacefunc(void *iface, bool post, \
   638 		SH_FHCls(ifacetype,ifacefunc,overload)::FD handler) \
   639 	{ \
   640 		using namespace ::SourceHook; \
   641 		MemFuncInfo mfi; \
   642 		GetFuncInfo(funcptr, mfi); \
   643 		if (mfi.thisptroffs < 0) \
   644 			return false; /* No virtual inheritance supported */ \
   645 		\
   646 		CSHDelegate<SH_FHCls(ifacetype,ifacefunc,overload)::FD> tmp(handler); \
   647 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   648 			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, &tmp, post); \
   649 	} \
   650 
   651 #define SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   652 	struct SH_MFHCls(hookname) \
   653 	{ \
   654 		static SH_MFHCls(hookname) ms_Inst; \
   655 		static ::SourceHook::MemFuncInfo ms_MFI; \
   656 		static ::SourceHook::IHookManagerInfo *ms_HI; \
   657 		static ::SourceHook::ProtoInfo ms_Proto; \
   658 		\
   659 		SH_MFHCls(hookname)() \
   660 		{ \
   661 			ms_MFI.isVirtual = true; \
   662 			ms_MFI.thisptroffs = pthisptroffs; \
   663 			ms_MFI.vtblindex = pvtblidx; \
   664 			ms_MFI.vtbloffs = pvtbloffs; \
   665 		} \
   666 		static int HookManPubFunc(::SourceHook::HookManagerAction action, ::SourceHook::IHookManagerInfo *param) \
   667 		{ \
   668 			using namespace ::SourceHook; \
   669 			/* we don't set ms_MFI here because manual hookmans can be reconfigured */ \
   670 			/* :FIXME: possible problem: someone adding a hook from a constructor of a global entity */ \
   671 			/* which is construced before SH_MFHCls(hookname)() gets called? */ \
   672 			/* Verify interface version */ \
   673 			if (SH_GLOB_SHPTR->GetIfaceVersion() != SH_IFACE_VERSION) \
   674 				return 1; \
   675 			\
   676 			if (action == HA_GetInfo) \
   677 			{ \
   678 				param->SetVersion(SH_HOOKMAN_VERSION); \
   679 				param->SetInfo(ms_MFI.vtbloffs, ms_MFI.vtblindex, \
   680 					reinterpret_cast<const char*>(&ms_Proto)); \
   681 				\
   682 				MemFuncInfo mfi; \
   683 				GetFuncInfo(&SH_MFHCls(hookname)::Func, mfi); \
   684 				param->SetHookfuncVfnptr( \
   685 					reinterpret_cast<void**>(reinterpret_cast<char*>(&ms_Inst) + mfi.vtbloffs)[mfi.vtblindex]); \
   686 				return 0; \
   687 			} \
   688 			else if (action == HA_Register) \
   689 			{ \
   690 				ms_HI = param; \
   691 				return 0; \
   692 			} \
   693 			else if (action == HA_Unregister) \
   694 			{ \
   695 				ms_HI = NULL; \
   696 				return 0; \
   697 			} \
   698 			else \
   699 				return 1; \
   700 		}
   701 
   702 #define SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, pvtbloffs, pvtblidx, pthisptroffs) \
   703 	}; \
   704 	SH_MFHCls(hookname) SH_MFHCls(hookname)::ms_Inst; \
   705 	::SourceHook::MemFuncInfo SH_MFHCls(hookname)::ms_MFI; \
   706 	::SourceHook::IHookManagerInfo *SH_MFHCls(hookname)::ms_HI; \
   707 	bool __SourceHook_FHMAdd##hookname(void *iface, bool post, \
   708 		SH_MFHCls(hookname)::FD handler) \
   709 	{ \
   710 		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   711 			SH_MFHCls(hookname)::HookManPubFunc, \
   712 			new ::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD>(handler), post); \
   713 	} \
   714 	bool __SourceHook_FHMRemove##hookname(void *iface, bool post, \
   715 		SH_MFHCls(hookname)::FD handler) \
   716 	{ \
   717 		::SourceHook::CSHDelegate<SH_MFHCls(hookname)::FD> tmp(handler); \
   718 		return SH_GLOB_SHPTR->RemoveHook(SH_GLOB_PLUGPTR, iface, pthisptroffs, \
   719 			SH_MFHCls(hookname)::HookManPubFunc, &tmp, post); \
   720 	} \
   721 
   722 
   723 #define SH_SETUPCALLS(rettype, paramtypes, params) \
   724 	/* 1) Find the vfnptr */ \
   725 	using namespace ::SourceHook; \
   726 	void *ourvfnptr = reinterpret_cast<void*>( \
   727 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   728 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   729 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   730 	\
   731 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   732 	/* ... and the iface */ \
   733 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   734 	if (!ifinfo) \
   735 	{ \
   736 		/* The iface info was not found. Redirect the call to the original function. */ \
   737 		rettype (EmptyClass::*mfp)paramtypes; \
   738 		SH_SETUP_MFP(mfp); \
   739 		return (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   740 	} \
   741 	/* 2) Declare some vars and set it up */ \
   742 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   743 	IHookList *prelist = ifinfo->GetPreHooks(); \
   744 	IHookList *postlist = ifinfo->GetPostHooks(); \
   745 	META_RES status = MRES_IGNORED; \
   746 	META_RES prev_res; \
   747 	META_RES cur_res; \
   748 	rettype orig_ret; \
   749 	rettype override_ret; \
   750 	rettype plugin_ret; \
   751 	void* ifptr; \
   752 	rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
   753 		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
   754 
   755 #define SH_CALL_HOOKS(post, params) \
   756 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   757 	{ \
   758 		prev_res = MRES_IGNORED; \
   759 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   760 		{ \
   761 			cur_res = MRES_IGNORED; \
   762 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   763 			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   764 			prev_res = cur_res; \
   765 			if (cur_res > status) \
   766 				status = cur_res; \
   767 			if (cur_res >= MRES_OVERRIDE) \
   768 				*pOverrideRet = plugin_ret; \
   769 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   770 			{ \
   771 				iter.SetToZero(); \
   772 				break; \
   773 			} \
   774 		} \
   775 	}
   776 
   777 #define SH_CALL_ORIG(rettype, paramtypes, params) \
   778 	if (status != MRES_SUPERCEDE) \
   779 	{ \
   780 		rettype (EmptyClass::*mfp)paramtypes; \
   781 		SH_SETUP_MFP(mfp); \
   782 		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   783 	} \
   784 	else \
   785 		orig_ret = override_ret; \
   786 
   787 #define SH_RETURN() \
   788 	SH_GLOB_SHPTR->HookLoopEnd(); \
   789 	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
   790 
   791 #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   792 	SH_SETUPCALLS(rettype, paramtypes, params) \
   793 	SH_CALL_HOOKS(pre, params) \
   794 	SH_CALL_ORIG(rettype, paramtypes, params) \
   795 	SH_CALL_HOOKS(post, params) \
   796 	SH_RETURN()
   797 
   798 //////////////////////////////////////////////////////////////////////////
   799 #define SH_SETUPCALLS_void(paramtypes, params) \
   800 	/* 1) Find the vfnptr */ \
   801 	using namespace ::SourceHook; \
   802 	void *ourvfnptr = reinterpret_cast<void*>( \
   803 		*reinterpret_cast<void***>(reinterpret_cast<char*>(this) + ms_MFI.vtbloffs) + ms_MFI.vtblindex); \
   804 	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   805 	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   806 	\
   807 	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   808 	/* ... and the iface */ \
   809 	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   810 	if (!ifinfo) \
   811 	{ \
   812 		/* The iface info was not found. Redirect the call to the original function. */ \
   813 		void (EmptyClass::*mfp)paramtypes; \
   814 		SH_SETUP_MFP(mfp); \
   815 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   816 		return; \
   817 	} \
   818 	/* 2) Declare some vars and set it up */ \
   819 	SH_GLOB_SHPTR->HookLoopBegin(ifinfo); \
   820 	IHookList *prelist = ifinfo->GetPreHooks(); \
   821 	IHookList *postlist = ifinfo->GetPostHooks(); \
   822 	META_RES status = MRES_IGNORED; \
   823 	META_RES prev_res; \
   824 	META_RES cur_res; \
   825 	void* ifptr; \
   826 	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
   827 
   828 #define SH_CALL_HOOKS_void(post, params) \
   829 	if (SH_GLOB_SHPTR->ShouldContinue()) \
   830 	{ \
   831 		prev_res = MRES_IGNORED; \
   832 		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   833 		{ \
   834 			cur_res = MRES_IGNORED; \
   835 			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   836 			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   837 			prev_res = cur_res; \
   838 			if (cur_res > status) \
   839 				status = cur_res; \
   840 			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   841 			{ \
   842 				iter.SetToZero(); \
   843 				break; \
   844 			} \
   845 		} \
   846 	}
   847 
   848 #define SH_CALL_ORIG_void(paramtypes, params) \
   849 	if (status != MRES_SUPERCEDE) \
   850 	{ \
   851 		void (EmptyClass::*mfp)paramtypes; \
   852 		SH_SETUP_MFP(mfp); \
   853 		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   854 	}
   855 
   856 #define SH_RETURN_void() \
   857 	SH_GLOB_SHPTR->HookLoopEnd();
   858 
   859 #define SH_HANDLEFUNC_void(paramtypes, params) \
   860 	SH_SETUPCALLS_void(paramtypes, params) \
   861 	SH_CALL_HOOKS_void(pre, params) \
   862 	SH_CALL_ORIG_void(paramtypes, params) \
   863 	SH_CALL_HOOKS_void(post, params) \
   864 	SH_RETURN_void()
   865 
   866 
   867 // Special vafmt handlers
   868 // :TODO: what
   869 #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
   870 	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   871 	SH_CALL_HOOKS(pre, params_plug) \
   872 	SH_CALL_ORIG(rettype, paramtypes, params_orig) \
   873 	SH_CALL_HOOKS(post, params_plug) \
   874 	SH_RETURN()
   875 
   876 #define SH_HANDLEFUNC_void_vafmt(paramtypes, params_orig, params_plug) \
   877 	SH_SETUPCALLS_void(paramtypes, params_orig) \
   878 	SH_CALL_HOOKS_void(pre, params_plug) \
   879 	SH_CALL_ORIG_void(paramtypes, params_orig) \
   880 	SH_CALL_HOOKS_void(post, params_plug) \
   881 	SH_RETURN_void()
   882 
   883 //////////////////////////////////////////////////////////////////////////
   884 
   885 @[$1,0,$a:
   886 // ********* Support for $1 arguments *********
   887 #define SH_DECL_HOOK$1(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   888 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   889 		(&ifacetype::ifacefunc))) \
   890 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
   891 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
   892 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
   893 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   894 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   895 	\
   896 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   897 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
   898 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   899 
   900 #define SH_DECL_HOOK$1_void(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   901 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr> \
   902 		(&ifacetype::ifacefunc))) \
   903 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
   904 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   905 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
   906 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   907 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]) attr>(&ifacetype::ifacefunc))) \
   908 	\
   909 	\
   910 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   911 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
   912 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   913 
   914 #define SH_DECL_HOOK$1_vafmt(ifacetype, ifacefunc, attr, overload, rettype@[$2,1,$1:, param$2@]) \
   915 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   916 		(&ifacetype::ifacefunc))) \
   917 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, rettype> FD; \
   918 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
   919 		{ \
   920 			char buf[::SourceHook::STRBUF_LEN]; \
   921 			va_list ap; \
   922 			va_start(ap, fmt); \
   923 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   924 			va_end(ap); \
   925 			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); \
   926 		} \
   927 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   928 		(static_cast<rettype (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
   929 	\
   930 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
   931 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(sizeof(rettype), \
   932 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   933 
   934 #define SH_DECL_HOOK$1_void_vafmt(ifacetype, ifacefunc, attr, overload@[$2,1,$1:, param$2@]) \
   935 	SHINT_MAKE_GENERICSTUFF_BEGIN(ifacetype, ifacefunc, overload, (static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr> \
   936 		(&ifacetype::ifacefunc))) \
   937 		typedef fastdelegate::FastDelegate@($1+1)<@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *> FD; \
   938 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]@[$1!=0:, @]const char *fmt, ...) \
   939 		{ \
   940 			char buf[::SourceHook::STRBUF_LEN]; \
   941 			va_list ap; \
   942 			va_start(ap, fmt); \
   943 			vsnprintf(buf, sizeof(buf), fmt, ap); \
   944 			va_end(ap); \
   945 			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)); \
   946 		} \
   947 	SHINT_MAKE_GENERICSTUFF_END(ifacetype, ifacefunc, overload, \
   948 		(static_cast<void (ifacetype::*)(@[$2,1,$1|, :param$2@]@[$1!=0:, @]const char *, ...) attr>(&ifacetype::ifacefunc))) \
   949 	\
   950 	const int __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload[] = { -1@[$2,1,$1:, sizeof(param$2)@] }; \
   951 	::SourceHook::ProtoInfo SH_FHCls(ifacetype, ifacefunc, overload)::ms_Proto(0, \
   952 		$1, __SourceHook_ParamSizes_##ifacetype##ifacefunc##overload);
   953 
   954 #define SH_DECL_MANUALHOOK$1(hookname, vtblidx, vtbloffs, thisptroffs, rettype@[$2,1,$1:, param$2@]) \
   955 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   956 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]@[$1!=0:, @]rettype> FD; \
   957 		virtual rettype Func(@[$2,1,$1|, :param$2 p$2@]) \
   958 		{ SH_HANDLEFUNC((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@]), rettype); } \
   959 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   960 	\
   961 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   962 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(sizeof(rettype), \
   963 		$1, __SourceHook_ParamSizesM_##hookname);
   964 
   965 #define SH_DECL_MANUALHOOK$1_void(hookname, vtblidx, vtbloffs, thisptroffs@[$2,1,$1:, param$2@]) \
   966 	SHINT_MAKE_GENERICSTUFF_BEGIN_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   967 		typedef fastdelegate::FastDelegate$1<@[$2,1,$1|, :param$2@]> FD; \
   968 		virtual void Func(@[$2,1,$1|, :param$2 p$2@]) \
   969 		{ SH_HANDLEFUNC_void((@[$2,1,$1|, :param$2@]), (@[$2,1,$1|, :p$2@])); } \
   970 	SHINT_MAKE_GENERICSTUFF_END_MANUAL(hookname, vtbloffs, vtblidx, thisptroffs) \
   971 	\
   972 	const int __SourceHook_ParamSizesM_##hookname[] = { 0@[$2,1,$1:, sizeof(param$2)@] }; \
   973 	::SourceHook::ProtoInfo SH_MFHCls(hookname)::ms_Proto(0, \
   974 		$1, __SourceHook_ParamSizesM_##hookname);
   975 
   976 @]
   977 
   978 
   979 //////////////////////////////////////////////////////////////////////////
   980 // SH_CALL
   981 
   982 #if SH_COMP == SH_COMP_MSVC
   983 
   984 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
   985 { \
   986 	using namespace ::SourceHook; \
   987 	MemFuncInfo mfi; \
   988 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
   989 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
   990 	if (!origfunc) \
   991 		return (m_CC->GetThisPtr()->*m_MFP)call; \
   992 	\
   993 	/* It's hooked. Call the original function. */ \
   994 	union \
   995 	{ \
   996 		RetType(EmptyClass::*mfpnew)prms; \
   997 		void *addr; \
   998 	} u; \
   999 	u.addr = origfunc; \
  1000 	\
  1001 	void *adjustedthisptr = reinterpret_cast<void*>(reinterpret_cast<char*>(m_CC->GetThisPtr()) + mfi.thisptroffs); \
  1002 	return (reinterpret_cast<EmptyClass*>(adjustedthisptr)->*u.mfpnew)call; \
  1003 }
  1004 
  1005 #elif SH_COMP == SH_COMP_GCC
  1006 
  1007 # define SH_MAKE_EXECUTABLECLASS_OB(call, prms) \
  1008 { \
  1009 	using namespace ::SourceHook; \
  1010 	MemFuncInfo mfi; \
  1011 	GetFuncInfo(m_CC->GetThisPtr(), m_MFP, mfi); \
  1012 	void *origfunc = m_CC->GetOrigFunc(mfi.thisptroffs + mfi.vtbloffs, mfi.vtblindex); \
  1013 	if (!origfunc) \
  1014 		return (m_CC->GetThisPtr()->*m_MFP)call; \
  1015 	\
  1016 	/* It's hooked. Call the original function. */ \
  1017 	union \
  1018 	{ \
  1019 		RetType(EmptyClass::*mfpnew)prms; \
  1020 		struct \
  1021 		{ \
  1022 			void *addr; \
  1023 			intptr_t adjustor; \
  1024 		} s; \
  1025 	} u; \
  1026 	u.s.addr = origfunc; \
  1027 	u.s.adjustor = mfi.thisptroffs; \
  1028 	\
  1029 	return (reinterpret_cast<EmptyClass*>(m_CC->GetThisPtr())->*u.mfpnew)call; \
  1030 }
  1031 
  1032 #endif
  1033 
  1034 namespace SourceHook
  1035 {
  1036 @[$1,0,$a:
  1037 	// Support for $1 arguments
  1038 	template<class CCType, class MFPType, class RetType@[$2,1,$1:, class Param$2@]> class ExecutableClass$1
  1039 	{
  1040 	   CCType *m_CC;
  1041 	   MFPType m_MFP;
  1042 	public:
  1043 	   ExecutableClass$1(CCType *cc, MFPType mfp) : m_CC(cc), m_MFP(mfp) { }
  1044 	
  1045 	   RetType operator()(@[$2,1,$1|, :Param$2 p$2@]) const
  1046 	      SH_MAKE_EXECUTABLECLASS_OB((@[$2,1,$1|, :p$2@]), (@[$2,1,$1|, :Param$2@]))
  1047 	         
  1048 	   @[$2,$1+1,$a:
  1049 	   template <@[$3,$1+1,$2|, :class Param$3@]> RetType operator()(@[$3,1,$2|, :Param$3 p$3@]) const
  1050 	      SH_MAKE_EXECUTABLECLASS_OB((@[$3,1,$2|, :p$3@]), (@[$3,1,$2|, :Param$3@]))
  1051 	   @]
  1052 	};
  1053 @]
  1054 
  1055 }
  1056 
  1057 // SH_CALL needs to deduce the return type -> it uses templates and function overloading
  1058 // That's why SH_CALL takes two parameters: "mfp2" of type RetType(X::*mfp)(params), and "mfp" of type MFP
  1059 // The only purpose of the mfp2 parameter is to extract the return type
  1060 
  1061 @[$1,0,$a:
  1062 // Support for $1 arguments
  1063 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1064 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1065 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]))
  1066 {
  1067 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1068 }
  1069 
  1070 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1071 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1072 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@])const)
  1073 {
  1074 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1075 }
  1076 
  1077 @]
  1078 
  1079 #if SH_COMP != SH_COMP_MSVC || _MSC_VER > 1300
  1080 // GCC & MSVC 7.1 need this, MSVC 7.0 doesn't like it
  1081 
  1082 @[$1,0,$a:
  1083 // Support for $1 arguments
  1084 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1085 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1086 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...))
  1087 {
  1088 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1089 }
  1090 
  1091 template <class X, class Y, class MFP, class RetType@[$2,1,$1:, class Param$2@]>
  1092 SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>
  1093 SH_CALL2(SourceHook::CallClass<Y> *ptr, MFP mfp, RetType(X::*mfp2)(@[$2,1,$1|, :Param$2@]@[$1!=0:, @]...)const)
  1094 {
  1095 	return SourceHook::ExecutableClass$1<SourceHook::CallClass<Y>, MFP, RetType@[$2,1,$1:, Param$2@]>(ptr, mfp);
  1096 }
  1097 @]
  1098 
  1099 #endif
  1100 
  1101 #define SH_CALL(ptr, mfp) SH_CALL2((ptr), (mfp), (mfp))
  1102 
  1103 #undef SH_MAKE_EXECUTABLECLASS_OB
  1104 
  1105 //////////////////////////////////////////////////////////////////////////
  1106 // SetOverrideRet and RecallGetIface for recalls
  1107 // These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
  1108 // The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
  1109 // (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
  1110 
  1111 namespace SourceHook
  1112 {
  1113 @[$1,0,$a:
  1114 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1115 	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]), const RetType res)
  1116 	{
  1117 		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
  1118 	}
  1119 
  1120 	template <class Iface, class RetType@[$2,1,$1:, class Param$2@]>
  1121 	Iface *RecallGetIface(ISourceHook *shptr, RetType (Iface::*mfp)(@[$2,1,$1|, :Param$2@]))
  1122 	{
  1123 		return reinterpret_cast<Iface*>(shptr->GetIfacePtr());
  1124 	}
  1125 @]
  1126 }
  1127 
  1128 #endif
  1129 	// The pope is dead. -> :(