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