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