sourcehook/sourcehook_impl.h
changeset 117 e322da4af2d2
parent 96 e023f17b7b73
child 121 158b860440c0
     1.1 --- a/sourcehook/sourcehook_impl.h	Thu Sep 29 23:05:01 2005 +0000
     1.2 +++ b/sourcehook/sourcehook_impl.h	Mon Oct 03 17:20:22 2005 +0000
     1.3 @@ -12,13 +12,12 @@
     1.4  #define __SOURCEHOOK_IMPL_H__
     1.5  
     1.6  #include "sourcehook.h"
     1.7 -
     1.8 -#ifdef __linux__
     1.9 -#include <signal.h>
    1.10 -#include <setjmp.h>
    1.11 -#endif
    1.12 +#include "sh_list.h"
    1.13 +#include "sh_vector.h"
    1.14 +#include "sh_tinyhash.h"
    1.15  
    1.16  // Set this to 1 to enable runtime code generation (faster)
    1.17 +//  (unused at the moment, but may come back, so I'm leaving it in here!)
    1.18  #define SH_RUNTIME_CODEGEN 1
    1.19  
    1.20  namespace SourceHook
    1.21 @@ -28,6 +27,7 @@
    1.22  	*/
    1.23  	class CSourceHookImpl : public ISourceHook
    1.24  	{
    1.25 +	private:
    1.26  		/**
    1.27  		*	@brief A hook can be removed if you have this information
    1.28  		*/
    1.29 @@ -48,24 +48,165 @@
    1.30  			bool post;
    1.31  		};
    1.32  
    1.33 -		/**
    1.34 -		*	@brief A list of HookManagerInfo structures
    1.35 -		*/
    1.36 -		typedef List<HookManagerInfo> HookManInfoList;
    1.37 +		struct HookInfo
    1.38 +		{
    1.39 +			ISHDelegate *handler;			//!< Pointer to the handler
    1.40 +			bool paused;					//!< If true, the hook should not be executed
    1.41 +			Plugin plug;					//!< The owner plugin
    1.42 +			int thisptr_offs;				//!< This pointer offset
    1.43 +		};
    1.44 +
    1.45 +		class CHookList : public IHookList
    1.46 +		{
    1.47 +		public:
    1.48 +			List<HookInfo> m_List;
    1.49 +
    1.50 +			friend class CIter;
    1.51 +
    1.52 +			class CIter : public IHookList::IIter
    1.53 +			{
    1.54 +				friend class CHookList;
    1.55 +
    1.56 +				CHookList *m_pList;
    1.57 +				List<HookInfo>::iterator m_Iter;
    1.58 +
    1.59 +				void SkipPaused();
    1.60 +			public:
    1.61 +				CIter(CHookList *pList);
    1.62 +
    1.63 +				virtual ~CIter();
    1.64 +
    1.65 +				void GoToBegin();
    1.66 +
    1.67 +				bool End();
    1.68 +				void Next();
    1.69 +				ISHDelegate *Handler();
    1.70 +				int ThisPtrOffs();
    1.71 +
    1.72 +				CIter *m_pNext;		// When stored in m_FreeIters
    1.73 +			};
    1.74 +
    1.75 +			CIter *m_FreeIters;
    1.76 +
    1.77 +			CHookList();
    1.78 +			CHookList(const CHookList &other);
    1.79 +			virtual ~CHookList();
    1.80 +
    1.81 +			void operator=(const CHookList &other);
    1.82 +
    1.83 +			IIter *GetIter();
    1.84 +			void ReleaseIter(IIter *pIter);
    1.85 +		};
    1.86 +
    1.87 +		// I know, data hiding... But I'm a lazy bastard!
    1.88 +
    1.89 +		class CIface : public IIface
    1.90 +		{
    1.91 +		public:
    1.92 +			void *m_Ptr;
    1.93 +			CHookList m_PreHooks;
    1.94 +			CHookList m_PostHooks;
    1.95 +		public:
    1.96 +			CIface(void *ptr);
    1.97 +			virtual ~CIface();
    1.98 +
    1.99 +			void *GetPtr();
   1.100 +			IHookList *GetPreHooks();
   1.101 +			IHookList *GetPostHooks();
   1.102 +
   1.103 +			bool operator==(void *ptr)
   1.104 +			{
   1.105 +				return m_Ptr == ptr;
   1.106 +			}
   1.107 +		};
   1.108  
   1.109 -		struct CallClassInfo
   1.110 +		class CVfnPtr : public IVfnPtr
   1.111 +		{
   1.112 +		public:
   1.113 +			typedef List<CIface> IfaceList;
   1.114 +			typedef IfaceList::iterator IfaceListIter;
   1.115 +
   1.116 +			void *m_Ptr;
   1.117 +			void *m_OrigEntry;
   1.118 +
   1.119 +			IfaceList m_Ifaces;
   1.120 +
   1.121 +		public:
   1.122 +			CVfnPtr(void *ptr);
   1.123 +			virtual ~CVfnPtr();
   1.124 +
   1.125 +			void *GetVfnPtr();
   1.126 +			void *GetOrigEntry();
   1.127 +
   1.128 +			virtual IIface *FindIface(void *ptr);
   1.129 +
   1.130 +			bool operator==(void *ptr)
   1.131 +			{
   1.132 +				return m_Ptr == ptr;
   1.133 +			}
   1.134 +		};
   1.135 +
   1.136 +		class CHookManagerInfo : public IHookManagerInfo
   1.137  		{
   1.138 -			GenericCallClass cc;
   1.139 -			int refcounter;
   1.140 +		public:
   1.141 +			typedef List<CVfnPtr> VfnPtrList;
   1.142 +			typedef VfnPtrList::iterator VfnPtrListIter;
   1.143 +
   1.144 +			Plugin m_Plug;
   1.145 +			HookManagerPubFunc m_Func;
   1.146 +
   1.147 +			int m_VtblOffs;
   1.148 +			int m_VtblIdx;
   1.149 +			const char *m_Proto;
   1.150 +			void *m_HookfuncVfnptr;
   1.151 +
   1.152 +			VfnPtrList m_VfnPtrs;
   1.153 +
   1.154 +		public:
   1.155 +			virtual ~CHookManagerInfo();
   1.156 +
   1.157 +			IVfnPtr *FindVfnPtr(void *vfnptr);
   1.158 +
   1.159 +			void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
   1.160 +			void SetHookfuncVfnptr(void *hookfunc_vfnptr);
   1.161 +		};
   1.162 +		/**
   1.163 +		*	@brief A list of CHookManagerInfo classes
   1.164 +		*/
   1.165 +		typedef List<CHookManagerInfo> HookManInfoList;
   1.166 +
   1.167 +		class CCallClassImpl : public GenericCallClass
   1.168 +		{
   1.169 +		public:
   1.170 +
   1.171 +			typedef SourceHook::CVector<void*> OrigFuncs;
   1.172 +			typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
   1.173 +
   1.174 +			void *m_Ptr;			//!< Pointer to the actual object
   1.175 +			size_t m_ObjSize;		//!< Size of the instance
   1.176 +			OrigVTables m_VT;		//!< Info about vtables & functions
   1.177 +
   1.178 +			int m_RefCounter;
   1.179 +
   1.180 +			CCallClassImpl(void *ptr, size_t size);
   1.181 +			virtual ~CCallClassImpl();
   1.182 +
   1.183  			bool operator==(void *other)
   1.184  			{
   1.185 -				return cc.ptr == other;
   1.186 +				return m_Ptr == other;
   1.187  			}
   1.188 +
   1.189 +			void *GetThisPtr();
   1.190 +			void *GetOrigFunc(int vtbloffs, int vtblidx);
   1.191 +
   1.192 +			void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
   1.193 +			void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
   1.194  		};
   1.195 +
   1.196  		/**
   1.197  		*	@brief A list of CallClass structures
   1.198  		*/
   1.199 -		typedef List<CallClassInfo> Impl_CallClassList;
   1.200 +		typedef List<CCallClassImpl> Impl_CallClassList;
   1.201  
   1.202  		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
   1.203  		HookManInfoList m_HookMans;					//!< A list of hook managers
   1.204 @@ -73,22 +214,22 @@
   1.205  		/**
   1.206  		*	@brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
   1.207  		*/
   1.208 -		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end, 
   1.209 +		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
   1.210  			const char *proto, int vtblofs, int vtblidx);
   1.211  
   1.212 -		void ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
   1.213 -		void ApplyCallClassPatches(CallClassInfo &cc);
   1.214 +		void ApplyCallClassPatches(CCallClassImpl &cc);
   1.215  		void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
   1.216 -		void RemoveCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx);
   1.217  		void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
   1.218  
   1.219 +		void SetPluginPaused(Plugin plug, bool paused);
   1.220 +
   1.221  		META_RES m_Status, m_PrevRes, m_CurRes;
   1.222  		const void *m_OrigRet;
   1.223  		const void *m_OverrideRet;
   1.224  		void *m_IfacePtr;
   1.225  	public:
   1.226  		CSourceHookImpl();
   1.227 -		~CSourceHookImpl();
   1.228 +		virtual ~CSourceHookImpl();
   1.229  
   1.230  		/**
   1.231  		*	@brief Returns the interface version
   1.232 @@ -199,19 +340,6 @@
   1.233  		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   1.234  		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   1.235  	};
   1.236 -
   1.237 -#ifdef __linux__
   1.238 -	/**
   1.239 -	*	@brief Checks to see if a memory value can be read from a given pointer.
   1.240 -	*
   1.241 -	*	@return True if the value can be read and false if it cannot.
   1.242 -	*
   1.243 -	*	@param ptr The pointer to the memory value.
   1.244 -	*	@param len The length of the memory value to be read from the pointer.
   1.245 -	*/
   1.246 -	bool IsBadReadPtr(const void *ptr, size_t len);
   1.247 -	void BadReadHandler(int sig);
   1.248 -#endif
   1.249  }
   1.250  
   1.251  #endif