sourcehook/sourcehook_impl.h
branchsourcemm-1.6.0
changeset 361 f34718303c8f
parent 347 e82968c21606
     1.1 --- a/sourcehook/sourcehook_impl.h	Fri Sep 28 21:32:02 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_impl.h	Fri Sep 28 21:40:58 2007 +0000
     1.3 @@ -16,6 +16,7 @@
     1.4  #include "sh_vector.h"
     1.5  #include "sh_tinyhash.h"
     1.6  #include "sh_stack.h"
     1.7 +#include "sh_listcat.h"
     1.8  
     1.9  /*
    1.10  
    1.11 @@ -89,6 +90,9 @@
    1.12  
    1.13  ---------------------------------------
    1.14  Call classes
    1.15 +
    1.16 +	!! deprecated !!   - see below (new SH_CALL)
    1.17 +
    1.18  	Call classes are identified by a this pointer and an instance size
    1.19  
    1.20  	We use the instance size because a derived class instance and a base class instance could
    1.21 @@ -142,6 +146,28 @@
    1.22  	HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy.
    1.23  
    1.24  	All this stuff could be much less complicated if I didn't try to preserve binary compatibility :)
    1.25 +
    1.26 +VP Hooks
    1.27 +	VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are
    1.28 +	implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which
    1.29 +	virtually concatenates the NULL-interface-hook-list with their normal hook list.
    1.30 +
    1.31 +
    1.32 +	I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together
    1.33 +	binary compatible beast which is pretty hard to maintain unless you've written it :)
    1.34 +
    1.35 +New SH_CALL
    1.36 +	The addition of VP hooks messed up the Call Classes concept (see above) - call classes are bound to an
    1.37 +	instance pointer; they only work on one of the hooked instances. But VP hooks are called on all instances.
    1.38 +	
    1.39 +	That's why now, SH_CALL takes an instance pointer instead of a callclass pointer. It basically does this:
    1.40 +	1) call SH_GLOB_PTR->SetIgnoreHooks(vfnptr)
    1.41 +	2) call this->*mfp
    1.42 +	3) call SH_GLOB_PTR->ResetIgnoreHooks(vfnptr)
    1.43 +
    1.44 +	SourceHook stroes the "ignored vfnptr" and makes CVfnPtr::FindIface return NULL if the CVfnPtr instance
    1.45 +	corresponds to the ignored vfnptr. This way the hook manager thinks that the instance isn't hooked, and calls
    1.46 +	the original function. Everything works fine. This works even for VP hooks.
    1.47  */
    1.48  
    1.49  namespace SourceHook
    1.50 @@ -160,6 +186,10 @@
    1.51  			char *DupProto(const char *src);
    1.52  			void FreeProto(char *prot);
    1.53  		public:
    1.54 +			CProto() : m_Proto(NULL)
    1.55 +			{
    1.56 +			}
    1.57 +
    1.58  			CProto(const char *szProto) : m_Proto(DupProto(szProto))
    1.59  			{
    1.60  			}
    1.61 @@ -192,6 +222,11 @@
    1.62  			{
    1.63  				return Equal(other.m_Proto, m_Proto);
    1.64  			}
    1.65 +
    1.66 +			const char *GetProto() const
    1.67 +			{
    1.68 +				return m_Proto;
    1.69 +			}
    1.70  		};
    1.71  
    1.72  
    1.73 @@ -226,17 +261,79 @@
    1.74  			HookManagerPubFunc hookman;
    1.75  		};
    1.76  
    1.77 +		// Associates hook ids with info about the hooks
    1.78 +		// Also used to keep track of used hook ids
    1.79 +		class CHookIDManager
    1.80 +		{
    1.81 +		public:
    1.82 +			struct Entry
    1.83 +			{
    1.84 +				bool isfree;
    1.85 +
    1.86 +				// hookman info
    1.87 +				CProto proto;
    1.88 +				int vtbl_offs;
    1.89 +				int vtbl_idx;
    1.90 +
    1.91 +				// vfnptr
    1.92 +				void *vfnptr;
    1.93 +
    1.94 +				// iface
    1.95 +				void* adjustediface;
    1.96 +
    1.97 +				// hook
    1.98 +				Plugin plug;
    1.99 +				int thisptr_offs;
   1.100 +				ISHDelegate *handler;
   1.101 +				bool post;
   1.102 +
   1.103 +				Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto,
   1.104 +					ISHDelegate *ph, bool ppost)
   1.105 +					: isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp), 
   1.106 +					adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost)
   1.107 +				{
   1.108 +				}
   1.109 +				Entry()
   1.110 +				{
   1.111 +				}
   1.112 +			};
   1.113 +		private:
   1.114 +			// Internally, hookid 1 is stored as m_Entries[0]
   1.115 +
   1.116 +			CVector<Entry> m_Entries;
   1.117 +		public:
   1.118 +			CHookIDManager();
   1.119 +			int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface,
   1.120 +				Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
   1.121 +			bool Remove(int hookid);
   1.122 +			const Entry * QueryHook(int hookid);
   1.123 +
   1.124 +			// Finds all hooks with the given info, and fills the hookids into output.
   1.125 +			void FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs, int vtbl_idx,
   1.126 +				void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
   1.127 +
   1.128 +			// Removes all hooks with a specified vfnptr
   1.129 +			bool RemoveAll(void *vfnptr);
   1.130 +		};
   1.131 +
   1.132  		struct HookInfo
   1.133  		{
   1.134  			ISHDelegate *handler;			//!< Pointer to the handler
   1.135  			bool paused;					//!< If true, the hook should not be executed
   1.136  			Plugin plug;					//!< The owner plugin
   1.137  			int thisptr_offs;				//!< This pointer offset
   1.138 +			int hookid;						//!< Unique ID given by CHookIDManager
   1.139 +
   1.140 +			bool operator==(int otherid)
   1.141 +			{
   1.142 +				return hookid == otherid;
   1.143 +			}
   1.144  		};
   1.145  
   1.146  		class CHookList : public IHookList
   1.147  		{
   1.148  		public:
   1.149 +			List<HookInfo> *m_VPList;			// left-hand list for ListCatIterator -> for VP hooks
   1.150  			List<HookInfo> m_List;
   1.151  
   1.152  			friend class CIter;
   1.153 @@ -250,7 +347,7 @@
   1.154  				void SkipPaused();
   1.155  			public:
   1.156  
   1.157 -				List<HookInfo>::iterator m_Iter;
   1.158 +				ListCatIterator<HookInfo> m_Iter;
   1.159  
   1.160  				CIter(CHookList *pList);
   1.161  
   1.162 @@ -293,6 +390,9 @@
   1.163  
   1.164  			IIter *GetIter();
   1.165  			void ReleaseIter(IIter *pIter);
   1.166 +
   1.167 +			void SetVPList(List<HookInfo> *newList);
   1.168 +			void ClearVPList();
   1.169  		};
   1.170  
   1.171  		// I know, data hiding... But I'm a lazy bastard!
   1.172 @@ -315,6 +415,10 @@
   1.173  			{
   1.174  				return m_Ptr == ptr;
   1.175  			}
   1.176 +			bool operator!=(void *ptr)
   1.177 +			{
   1.178 +				return m_Ptr != ptr;
   1.179 +			}
   1.180  		};
   1.181  
   1.182  		class CVfnPtr : public IVfnPtr
   1.183 @@ -328,8 +432,9 @@
   1.184  
   1.185  			IfaceList m_Ifaces;
   1.186  
   1.187 +			void **m_pOneIgnore;
   1.188  		public:
   1.189 -			CVfnPtr(void *ptr);
   1.190 +			CVfnPtr(void *ptr, void **pOneIgnore);
   1.191  			virtual ~CVfnPtr();
   1.192  
   1.193  			void *GetVfnPtr();
   1.194 @@ -446,7 +551,7 @@
   1.195  			void AddHookManager(Plugin plug, const CHookManagerInfo &hookman);
   1.196  		};
   1.197  
   1.198 -		class CCallClassImpl : public GenericCallClass
   1.199 +		class CCallClassImpl : public DeprecatedCallClass<void>
   1.200  		{
   1.201  		public:
   1.202  
   1.203 @@ -520,6 +625,10 @@
   1.204  		void SetPluginPaused(Plugin plug, bool paused);
   1.205  
   1.206  		HookLoopInfoStack m_HLIStack;
   1.207 +		CHookIDManager m_HookIDMan;
   1.208 +
   1.209 +		void *m_OneIgnore; //:TODO:
   1.210 +		bool m_IgnoreActive;
   1.211  	public:
   1.212  		CSourceHookImpl();
   1.213  		virtual ~CSourceHookImpl();
   1.214 @@ -608,14 +717,14 @@
   1.215  		*	@param iface The interface pointer
   1.216  		*	@param size Size of the class instance
   1.217  		*/
   1.218 -		GenericCallClass *GetCallClass(void *iface, size_t size);
   1.219 +		DeprecatedCallClass<void> *GetCallClass(void *iface, size_t size);
   1.220  
   1.221  		/**
   1.222  		*	@brief Release a callclass
   1.223  		*
   1.224  		*	@param ptr Pointer to the callclass
   1.225  		*/
   1.226 -		virtual void ReleaseCallClass(GenericCallClass *ptr);
   1.227 +		virtual void ReleaseCallClass(DeprecatedCallClass<void> *ptr);
   1.228  
   1.229  		virtual void SetRes(META_RES res);				//!< Sets the meta result
   1.230  		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   1.231 @@ -650,6 +759,58 @@
   1.232  
   1.233  		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   1.234  			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr);
   1.235 +
   1.236 +		/**
   1.237 +		*	@brief Add a (VP) hook.
   1.238 +		*
   1.239 +		*	@return non-zero hook id on success, 0 otherwise
   1.240 +		*
   1.241 +		*	@param plug The unique identifier of the plugin that calls this function
   1.242 +		*	@param mode	Can be either Hook_Normal or Hook_VP (vtable-wide hook)
   1.243 +		*	@param iface The interface pointer
   1.244 +		*				 The representative interface pointer for VP hooks
   1.245 +		*				 The vtable pointer for direct VP hooks !!!
   1.246 +		*	@param ifacesize The size of the class iface points to
   1.247 +		*	@param myHookMan A hook manager function that should be capable of handling the function
   1.248 +		*	@param handler A pointer to a FastDelegate containing the hook handler
   1.249 +		*	@param post Set to true if you want a post handler
   1.250 +		*/
   1.251 +		virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   1.252 +			ISHDelegate *handler, bool post);
   1.253 +
   1.254 +		/**
   1.255 +		*	@brief Remove a VP hook by ID.
   1.256 +		*
   1.257 +		*	@return true on success, false otherwise
   1.258 +		*
   1.259 +		*	@param plug The unique identifier of the plugin that calls this function
   1.260 +		*	@param hookid The hook id (returned by AddHookNew)
   1.261 +		*/
   1.262 +		virtual bool RemoveHookByID(Plugin plug, int hookid);
   1.263 +
   1.264 +		/**
   1.265 +		*	@brief Makes sure that hooks are going to be ignored on the next call of vfnptr
   1.266 +		*
   1.267 +		*	@param plug The unique identifier of the plugin that calls this function
   1.268 +		*	@param vfnptr The virtual function pointer of the function in question
   1.269 +		*/
   1.270 +		virtual void SetIgnoreHooks(Plugin plug, void *vfnptr);
   1.271 +
   1.272 +		/**
   1.273 +		*	@brief Reverses SetIgnoreHooks' effect
   1.274 +		*
   1.275 +		*	@param plug The unique identifier of the plugin that calls this function
   1.276 +		*	@param vfnptr The virtual function pointer of the function in question
   1.277 +		*/
   1.278 +		virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr);
   1.279 +
   1.280 +		/**
   1.281 +		*	@brief Finds the original entry of a virtual function pointer
   1.282 +		*
   1.283 +		*	@param vfnptr The virtual function pointer
   1.284 +		*	@return The original entry if the virtual function pointer has been patched; NULL otherwise.
   1.285 +		*/
   1.286 +		virtual void *GetOrigVfnPtrEntry(void *vfnptr);
   1.287  	};
   1.288  }
   1.289