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