sourcehook/sourcehook.h
changeset 162 a95db73792e5
parent 157 064f77cac7ab
child 163 377bdd31c28d
     1.1 --- a/sourcehook/sourcehook.h	Thu Dec 22 21:00:54 2005 +0000
     1.2 +++ b/sourcehook/sourcehook.h	Fri Dec 23 11:58:11 2005 +0000
     1.3 @@ -372,7 +372,7 @@
     1.4  		virtual void SetStatusPtr(META_RES *mres) = 0;			//!< Sets pointer to the status variable
     1.5  		virtual void SetIfacePtrPtr(void **pp) = 0;				//!< Sets pointer to the interface this pointer
     1.6  		virtual void SetOrigRetPtr(const void *ptr) = 0;		//!< Sets the original return pointer
     1.7 -		virtual void SetOverrideRetPtr(const void *ptr) = 0;	//!< Sets the override result pointer
     1.8 +		virtual void SetOverrideRetPtr(void *ptr) = 0;			//!< Sets the override result pointer
     1.9  		virtual bool ShouldContinue() = 0;						//!< Returns false if the hook loop should exit
    1.10  
    1.11  		/**
    1.12 @@ -382,22 +382,89 @@
    1.13  		*   @param pubFunc The hook manager's info function
    1.14  		*/
    1.15  		virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc) = 0;
    1.16 +
    1.17 +		virtual void DoRecall() = 0;							//!< Initiates a recall sequence
    1.18 +		/*
    1.19 +			HOW RECALLS WORK:
    1.20 +			
    1.21 +			The problem:
    1.22 +				Users want the ability to change parameters of the called function
    1.23 +				from inside their handler.
    1.24 +			The solution:
    1.25 +				1) Mark as "recall"
    1.26 +				2) Recall the function
    1.27 +				3) => SH's hook func gets called:
    1.28 +				  4) The first iterator points at the first hook the last hookfunc didn't execute yet
    1.29 +				  5) does all iteration and returns normally
    1.30 +		        6) The user's handler returns immediately
    1.31 +				7) The hook func returns immediately as well
    1.32 +
    1.33 +				Also note that the recalled hookfunc starts with the status the recalling hookfunc
    1.34 +				ended with. The last handler (doing the recall) is also able to specify its own
    1.35 +				META_RES.
    1.36 +		*/
    1.37 +
    1.38 +		virtual void *GetOverrideRetPtr() = 0;		//!< Returns the pointer set by SetOverrideRetPtr
    1.39 +
    1.40 +		/**
    1.41 +		*	@brief Set up the hook loop. Equivalent to calling:
    1.42 +		*	SetStatusPtr, SetPrevResPtr, SetCurResPtr, SetIfacePtrPtr, SetOrigRetPtr, Get/SetOverrideRetPtr
    1.43 +		*
    1.44 +		*	@param statusPtr		pointer to status variable
    1.45 +		*	@param prevResPtr		pointer to previous result variable
    1.46 +		*	@param curResPtr		pointer to current result variable
    1.47 +		*	@param ifacePtrPtr		pointer to interface this pointer variable
    1.48 +		*	@param origRetPr		pointer to original return value variable. NULL for void funcs
    1.49 +		*	@param overrideRetPtr	pointer to override return value variable. NULL for void funcs
    1.50 +		*	
    1.51 +		*	@return Override Return Pointer the hookfunc should use (may differ from overrideRetPtr
    1.52 +		*		when the hook func is being called as part of a recall
    1.53 +		*/
    1.54 +		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
    1.55 +			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr) = 0;
    1.56  	};
    1.57  }
    1.58  
    1.59  /************************************************************************/
    1.60  /* High level interface                                                 */
    1.61  /************************************************************************/
    1.62 -#define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
    1.63 -#define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
    1.64 -#define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
    1.65 -
    1.66  #define META_RESULT_STATUS					SH_GLOB_SHPTR->GetStatus()
    1.67  #define META_RESULT_PREVIOUS				SH_GLOB_SHPTR->GetPrevRes()
    1.68  #define META_RESULT_ORIG_RET(type)			*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOrigRet())
    1.69  #define META_RESULT_OVERRIDE_RET(type)		*reinterpret_cast<const type*>(SH_GLOB_SHPTR->GetOverrideRet())
    1.70  #define META_IFACEPTR(type)					reinterpret_cast<type*>(SH_GLOB_SHPTR->GetIfacePtr())
    1.71  
    1.72 +#define SET_META_RESULT(result)				SH_GLOB_SHPTR->SetRes(result)
    1.73 +#define RETURN_META(result)					do { SET_META_RESULT(result); return; } while(0)
    1.74 +#define RETURN_META_VALUE(result, value)	do { SET_META_RESULT(result); return (value); } while(0)
    1.75 +
    1.76 +
    1.77 +// NEVER-EVER call these from post hooks!
    1.78 +// also, only call it from the hook handlers directly!
    1.79 +// :TODO: enforce it
    1.80 +// :TODO: problems with SetOverrideResult and overloaded iface::func ?
    1.81 +
    1.82 +// SourceHook::SetOverrideRet is defined later.
    1.83 +#define RETURN_META_NEWPARAMS(result, iface, func, newparams) \
    1.84 +	do { \
    1.85 +		SET_META_RESULT(result); \
    1.86 +		SH_GLOB_SHPTR->DoRecall(); \
    1.87 +		META_IFACEPTR(iface)->func newparams; \
    1.88 +		RETURN_META(MRES_SUPERCEDE); \
    1.89 +	} while (0)
    1.90 +
    1.91 +#define RETURN_META_VALUE_NEWPARAMS(result, value, iface, func, newparams) \
    1.92 +	do { \
    1.93 +		SET_META_RESULT(result); \
    1.94 +		SH_GLOB_SHPTR->DoRecall(); \
    1.95 +		if ((result) >= MRES_OVERRIDE) \
    1.96 +		{ \
    1.97 +			/* meh, set the override result here because we don't get a chance to return */ \
    1.98 +			/* before continuing the hook loop through the recall */ \
    1.99 +			SourceHook::SetOverrideResult(SH_GLOB_SHPTR, &iface::func, value); \
   1.100 +		} \
   1.101 +		RETURN_META_VALUE(MRES_SUPERCEDE, META_IFACEPTR(iface)->func newparams); \
   1.102 +	} while (0)
   1.103  
   1.104  /**
   1.105  *	@brief Get/generate callclass for an interface pointer
   1.106 @@ -542,8 +609,8 @@
   1.107  		using namespace ::SourceHook; \
   1.108  		MemFuncInfo mfi; \
   1.109  		GetFuncInfo(funcptr, mfi); \
   1.110 -		if (mfi.thisptroffs < 0) \
   1.111 -			return false; /* No virtual inheritance supported */ \
   1.112 +		if (mfi.thisptroffs < 0 || !mfi.isVirtual) \
   1.113 +			return false; /* No non-virtual functions / virtual inheritance supported */ \
   1.114  		\
   1.115  		return SH_GLOB_SHPTR->AddHook(SH_GLOB_PLUGPTR, iface, mfi.thisptroffs, \
   1.116  			SH_FHCls(ifacetype,ifacefunc,overload)::HookManPubFunc, \
   1.117 @@ -659,16 +726,12 @@
   1.118  	META_RES status = MRES_IGNORED; \
   1.119  	META_RES prev_res; \
   1.120  	META_RES cur_res; \
   1.121 -	SH_GLOB_SHPTR->SetStatusPtr(&status); \
   1.122 -	SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
   1.123 -	SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
   1.124  	rettype orig_ret; \
   1.125  	rettype override_ret; \
   1.126  	rettype plugin_ret; \
   1.127  	void* ifptr; \
   1.128 -	SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
   1.129 -	SH_GLOB_SHPTR->SetOrigRetPtr(reinterpret_cast<void*>(&orig_ret)); \
   1.130 -	SH_GLOB_SHPTR->SetOverrideRetPtr(NULL);
   1.131 +	rettype *pOverrideRet = reinterpret_cast<rettype*>(SH_GLOB_SHPTR->SetupHookLoop( \
   1.132 +		&status, &prev_res, &cur_res, &ifptr, &orig_ret, &override_ret));
   1.133  
   1.134  #define SH_CALL_HOOKS(post, params) \
   1.135  	if (SH_GLOB_SHPTR->ShouldContinue()) \
   1.136 @@ -683,10 +746,7 @@
   1.137  			if (cur_res > status) \
   1.138  				status = cur_res; \
   1.139  			if (cur_res >= MRES_OVERRIDE) \
   1.140 -			{ \
   1.141 -				override_ret = plugin_ret; \
   1.142 -				SH_GLOB_SHPTR->SetOverrideRetPtr(&override_ret); \
   1.143 -			} \
   1.144 +				*pOverrideRet = plugin_ret; \
   1.145  			if (!SH_GLOB_SHPTR->ShouldContinue()) \
   1.146  			{ \
   1.147  				iter.SetToZero(); \
   1.148 @@ -703,11 +763,11 @@
   1.149  		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.150  	} \
   1.151  	else \
   1.152 -		orig_ret = override_ret;
   1.153 +		orig_ret = override_ret; \
   1.154  
   1.155  #define SH_RETURN() \
   1.156  	SH_GLOB_SHPTR->HookLoopEnd(); \
   1.157 -	return status >= MRES_OVERRIDE ? override_ret : orig_ret;
   1.158 +	return status >= MRES_OVERRIDE ? *pOverrideRet : orig_ret;
   1.159  
   1.160  #define SH_HANDLEFUNC(paramtypes, params, rettype) \
   1.161  	SH_SETUPCALLS(rettype, paramtypes, params) \
   1.162 @@ -743,13 +803,8 @@
   1.163  	META_RES status = MRES_IGNORED; \
   1.164  	META_RES prev_res; \
   1.165  	META_RES cur_res; \
   1.166 -	SH_GLOB_SHPTR->SetStatusPtr(&status); \
   1.167 -	SH_GLOB_SHPTR->SetPrevResPtr(&prev_res); \
   1.168 -	SH_GLOB_SHPTR->SetCurResPtr(&cur_res); \
   1.169  	void* ifptr; \
   1.170 -	SH_GLOB_SHPTR->SetIfacePtrPtr(&ifptr); \
   1.171 -	SH_GLOB_SHPTR->SetOverrideRetPtr(NULL); \
   1.172 -	SH_GLOB_SHPTR->SetOrigRetPtr(NULL);
   1.173 +	SH_GLOB_SHPTR->SetupHookLoop(&status, &prev_res, &cur_res, &ifptr, NULL, NULL); \
   1.174  
   1.175  #define SH_CALL_HOOKS_void(post, params) \
   1.176  	if (SH_GLOB_SHPTR->ShouldContinue()) \
   1.177 @@ -791,6 +846,7 @@
   1.178  
   1.179  
   1.180  // Special vafmt handlers
   1.181 +// :TODO: what
   1.182  #define SH_HANDLEFUNC_vafmt(paramtypes, params_orig, params_plug, rettype) \
   1.183  	SH_SETUPCALLS(rettype, paramtypes, params_orig) \
   1.184  	SH_CALL_HOOKS(pre, params_plug) \
   1.185 @@ -3497,6 +3553,121 @@
   1.186  
   1.187  #undef SH_MAKE_EXECUTABLECLASS_OB
   1.188  
   1.189 +//////////////////////////////////////////////////////////////////////////
   1.190 +// SetOverrideRet for recalls
   1.191 +// These take a ISourceHook pointer instead of using SH_GLOB_SHPTR directly
   1.192 +// The reason is that the user may want to redefine SH_GLOB_SHPTR - then the macros
   1.193 +// (META_RETURN_VALUE_NEWPARAMS) should obey the new pointer.
   1.194 +
   1.195 +namespace SourceHook
   1.196 +{
   1.197 +	template <class Iface, class RetType>
   1.198 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(), const RetType res)
   1.199 +	{
   1.200 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.201 +	}
   1.202 +	template <class Iface, class RetType, class Param1>
   1.203 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1), const RetType res)
   1.204 +	{
   1.205 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.206 +	}
   1.207 +	template <class Iface, class RetType, class Param1, class Param2>
   1.208 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2), const RetType res)
   1.209 +	{
   1.210 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.211 +	}
   1.212 +	template <class Iface, class RetType, class Param1, class Param2, class Param3>
   1.213 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3), const RetType res)
   1.214 +	{
   1.215 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.216 +	}
   1.217 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4>
   1.218 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4), const RetType res)
   1.219 +	{
   1.220 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.221 +	}
   1.222 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5>
   1.223 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5), const RetType res)
   1.224 +	{
   1.225 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.226 +	}
   1.227 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6>
   1.228 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6), const RetType res)
   1.229 +	{
   1.230 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.231 +	}
   1.232 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7>
   1.233 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7), const RetType res)
   1.234 +	{
   1.235 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.236 +	}
   1.237 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8>
   1.238 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8), const RetType res)
   1.239 +	{
   1.240 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.241 +	}
   1.242 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9>
   1.243 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9), const RetType res)
   1.244 +	{
   1.245 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.246 +	}
   1.247 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10>
   1.248 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10), const RetType res)
   1.249 +	{
   1.250 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.251 +	}
   1.252 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11>
   1.253 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11), const RetType res)
   1.254 +	{
   1.255 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.256 +	}
   1.257 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12>
   1.258 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12), const RetType res)
   1.259 +	{
   1.260 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.261 +	}
   1.262 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13>
   1.263 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13), const RetType res)
   1.264 +	{
   1.265 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.266 +	}
   1.267 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14>
   1.268 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14), const RetType res)
   1.269 +	{
   1.270 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.271 +	}
   1.272 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14, class Param15>
   1.273 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15), const RetType res)
   1.274 +	{
   1.275 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.276 +	}
   1.277 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14, class Param15, class Param16>
   1.278 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15, Param16), const RetType res)
   1.279 +	{
   1.280 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.281 +	}
   1.282 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14, class Param15, class Param16, class Param17>
   1.283 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15, Param16, Param17), const RetType res)
   1.284 +	{
   1.285 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.286 +	}
   1.287 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14, class Param15, class Param16, class Param17, class Param18>
   1.288 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15, Param16, Param17, Param18), const RetType res)
   1.289 +	{
   1.290 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.291 +	}
   1.292 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14, class Param15, class Param16, class Param17, class Param18, class Param19>
   1.293 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15, Param16, Param17, Param18, Param19), const RetType res)
   1.294 +	{
   1.295 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.296 +	}
   1.297 +	template <class Iface, class RetType, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class Param9, class Param10, class Param11, class Param12, class Param13, class Param14, class Param15, class Param16, class Param17, class Param18, class Param19, class Param20>
   1.298 +	void SetOverrideResult(ISourceHook *shptr, RetType (Iface::*mfp)(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15, Param16, Param17, Param18, Param19, Param20), const RetType res)
   1.299 +	{
   1.300 +		*reinterpret_cast<RetType*>(shptr->GetOverrideRetPtr()) = res;
   1.301 +	}
   1.302 +}
   1.303 +
   1.304  #endif
   1.305  	// The pope is dead. -> :(
   1.306