sourcehook/sourcehook.h
changeset 123 53e819ac032a
parent 119 8b8ccac34850
child 128 63f95eed16b6
     1.1 --- a/sourcehook/sourcehook.h	Thu Oct 06 00:59:01 2005 +0000
     1.2 +++ b/sourcehook/sourcehook.h	Thu Oct 06 20:46:49 2005 +0000
     1.3 @@ -16,7 +16,13 @@
     1.4  #ifndef __SOURCEHOOK_H__
     1.5  #define __SOURCEHOOK_H__
     1.6  
     1.7 -#define SH_IFACE_VERSION 2
     1.8 +// Interface revisions:
     1.9 +//  1 - Initial revision
    1.10 +//  2 - Changed to virtual functions for iterators and all queries
    1.11 +//  3 - Added "hook loop status variable"
    1.12 +//  Future: Thread safe interface?
    1.13 +
    1.14 +#define SH_IFACE_VERSION 3
    1.15  #define SH_IMPL_VERSION 2
    1.16  
    1.17  #ifndef SH_GLOB_SHPTR
    1.18 @@ -229,7 +235,8 @@
    1.19  
    1.20  		~AutoHookIter()
    1.21  		{
    1.22 -			m_pList->ReleaseIter(m_pIter);
    1.23 +			if (m_pList)
    1.24 +				m_pList->ReleaseIter(m_pIter);
    1.25  		}
    1.26  
    1.27  		bool End()
    1.28 @@ -252,6 +259,10 @@
    1.29  			return m_pIter->ThisPtrOffs();
    1.30  		}
    1.31  
    1.32 +		void SetToZero()
    1.33 +		{
    1.34 +			m_pList = 0;
    1.35 +		}
    1.36  	};
    1.37  
    1.38  	template<class B> struct CallClass
    1.39 @@ -263,6 +274,15 @@
    1.40  	typedef CallClass<void> GenericCallClass;
    1.41  
    1.42  	/**
    1.43 +	*	@brief SH informs the loop in the hook manager about its status through this
    1.44 +	*/
    1.45 +	enum HookLoopStatus
    1.46 +	{
    1.47 +		HLS_Continue=0,
    1.48 +		HLS_Stop
    1.49 +	};
    1.50 +
    1.51 +	/**
    1.52  	*	@brief The main SourceHook interface
    1.53  	*/
    1.54  	class ISourceHook
    1.55 @@ -346,6 +366,8 @@
    1.56  		virtual void* &GetIfacePtrRef() = 0;				//!< Gets the reference to the interface this pointer
    1.57  		virtual void SetOrigRet(const void *ptr) = 0;		//!< Sets the original return pointer
    1.58  		virtual void SetOverrideRet(const void *ptr) = 0;	//!< Sets the override result pointer
    1.59 +		virtual HookLoopStatus &GetStatusVarRef(IIface *pIface) = 0;			//!< gets the reference to the hook status loop variable
    1.60 +		virtual void HookLoopDone() = 0;
    1.61  	};
    1.62  }
    1.63  
    1.64 @@ -407,10 +429,10 @@
    1.65  
    1.66  #if SH_COMP == SH_COMP_MSVC
    1.67  # define SH_SETUP_MFP(mfp) \
    1.68 -	reinterpret_cast<void**>(&mfp)[0] = vfnptr->GetOrigEntry();
    1.69 +	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry;
    1.70  #elif SH_COMP == SH_COMP_GCC
    1.71  # define SH_SETUP_MFP(mfp) \
    1.72 -	reinterpret_cast<void**>(&mfp)[0] = vfnptr->GetOrigEntry(); \
    1.73 +	reinterpret_cast<void**>(&mfp)[0] = vfnptr_origentry; \
    1.74  	reinterpret_cast<void**>(&mfp)[1] = 0;
    1.75  #else
    1.76  # error Not supported yet.
    1.77 @@ -510,6 +532,7 @@
    1.78  	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
    1.79  	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
    1.80  	\
    1.81 +	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
    1.82  	/* ... and the iface */ \
    1.83  	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
    1.84  	if (!ifinfo) \
    1.85 @@ -522,6 +545,8 @@
    1.86  	/* 2) Declare some vars and set it up */ \
    1.87  	IHookList *prelist = ifinfo->GetPreHooks(); \
    1.88  	IHookList *postlist = ifinfo->GetPostHooks(); \
    1.89 +	HookLoopStatus &hls = SH_GLOB_SHPTR->GetStatusVarRef(ifinfo); \
    1.90 +	hls = HLS_Continue; \
    1.91  	rettype orig_ret; \
    1.92  	rettype override_ret; \
    1.93  	rettype plugin_ret; \
    1.94 @@ -534,19 +559,28 @@
    1.95  	SH_GLOB_SHPTR->SetOverrideRet(NULL);
    1.96  
    1.97  #define SH_CALL_HOOKS(post, params) \
    1.98 -	prev_res = MRES_IGNORED; \
    1.99 -	for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   1.100 +	if (hls == HLS_Continue) \
   1.101  	{ \
   1.102 -		cur_res = MRES_IGNORED; \
   1.103 -		ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   1.104 -		plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   1.105 -		prev_res = cur_res; \
   1.106 -		if (cur_res > status) \
   1.107 -			status = cur_res; \
   1.108 -		if (cur_res >= MRES_OVERRIDE) \
   1.109 +		prev_res = MRES_IGNORED; \
   1.110 +		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   1.111  		{ \
   1.112 -			override_ret = plugin_ret; \
   1.113 -			SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   1.114 +			cur_res = MRES_IGNORED; \
   1.115 +			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   1.116 +			hls = HLS_Continue; \
   1.117 +			plugin_ret = reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   1.118 +			prev_res = cur_res; \
   1.119 +			if (cur_res > status) \
   1.120 +				status = cur_res; \
   1.121 +			if (cur_res >= MRES_OVERRIDE) \
   1.122 +			{ \
   1.123 +				override_ret = plugin_ret; \
   1.124 +				SH_GLOB_SHPTR->SetOverrideRet(&override_ret); \
   1.125 +			} \
   1.126 +			if (hls == HLS_Stop) \
   1.127 +			{ \
   1.128 +				iter.SetToZero(); \
   1.129 +				break; \
   1.130 +			} \
   1.131  		} \
   1.132  	}
   1.133  
   1.134 @@ -555,7 +589,7 @@
   1.135  	{ \
   1.136  		rettype (EmptyClass::*mfp)paramtypes; \
   1.137  		SH_SETUP_MFP(mfp); \
   1.138 -		orig_ret = (reinterpret_cast<EmptyClass*>(ifinfo->GetPtr())->*mfp)params; \
   1.139 +		orig_ret = (reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.140  	} \
   1.141  	else \
   1.142  		orig_ret = override_ret;
   1.143 @@ -579,6 +613,7 @@
   1.144  	IVfnPtr *vfnptr = ms_HI->FindVfnPtr(ourvfnptr); \
   1.145  	SH_ASSERT(vfnptr, ("Called with vfnptr 0x%p which couldn't be found in the list", ourvfnptr)); \
   1.146  	\
   1.147 +	void *vfnptr_origentry = vfnptr->GetOrigEntry(); \
   1.148  	/* ... and the iface */ \
   1.149  	IIface *ifinfo = vfnptr->FindIface(reinterpret_cast<void*>(this)); \
   1.150  	if (!ifinfo) \
   1.151 @@ -593,6 +628,8 @@
   1.152  	IHookList *prelist = ifinfo->GetPreHooks(); \
   1.153  	IHookList *postlist = ifinfo->GetPostHooks(); \
   1.154  	META_RES &cur_res = SH_GLOB_SHPTR->GetCurResRef(); \
   1.155 +	HookLoopStatus &hls = SH_GLOB_SHPTR->GetStatusVarRef(ifinfo); \
   1.156 +	hls = HLS_Continue; \
   1.157  	META_RES &prev_res = SH_GLOB_SHPTR->GetPrevResRef(); \
   1.158  	META_RES &status = SH_GLOB_SHPTR->GetStatusRef(); \
   1.159  	void* &ifptr = SH_GLOB_SHPTR->GetIfacePtrRef(); \
   1.160 @@ -601,15 +638,24 @@
   1.161  	SH_GLOB_SHPTR->SetOrigRet(NULL);
   1.162  
   1.163  #define SH_CALL_HOOKS_void(post, params) \
   1.164 -	prev_res = MRES_IGNORED; \
   1.165 -	for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   1.166 +	if (hls == HLS_Continue) \
   1.167  	{ \
   1.168 -		cur_res = MRES_IGNORED; \
   1.169 -		ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   1.170 -		reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   1.171 -		prev_res = cur_res; \
   1.172 -		if (cur_res > status) \
   1.173 -			status = cur_res; \
   1.174 +		prev_res = MRES_IGNORED; \
   1.175 +		for (AutoHookIter iter(post##list); !iter.End(); iter.Next()) \
   1.176 +		{ \
   1.177 +			cur_res = MRES_IGNORED; \
   1.178 +			ifptr = reinterpret_cast<void*>(reinterpret_cast<char*>(this) - iter.ThisPtrOffs()); \
   1.179 +			hls = HLS_Continue; \
   1.180 +			reinterpret_cast<CSHDelegate<FD>*>(iter.Handler())->GetDeleg() params; \
   1.181 +			prev_res = cur_res; \
   1.182 +			if (cur_res > status) \
   1.183 +				status = cur_res; \
   1.184 +			if (hls == HLS_Stop) \
   1.185 +			{ \
   1.186 +				iter.SetToZero(); \
   1.187 +				break; \
   1.188 +			} \
   1.189 +		} \
   1.190  	}
   1.191  
   1.192  #define SH_CALL_ORIG_void(ifacetype, ifacefunc, paramtypes, params) \
   1.193 @@ -617,7 +663,7 @@
   1.194  	{ \
   1.195  		void (EmptyClass::*mfp)paramtypes; \
   1.196  		SH_SETUP_MFP(mfp); \
   1.197 -		(reinterpret_cast<EmptyClass*>(ifinfo->GetPtr())->*mfp)params; \
   1.198 +		(reinterpret_cast<EmptyClass*>(this)->*mfp)params; \
   1.199  	}
   1.200  
   1.201  #define SH_RETURN_void()