sourcehook/sourcehook_impl.h
changeset 324 257f0837a885
parent 294 0d87bc8606f8
child 328 3a04ef79edf1
     1.1 --- a/sourcehook/sourcehook_impl.h	Mon May 07 18:33:34 2007 +0000
     1.2 +++ b/sourcehook/sourcehook_impl.h	Mon May 07 18:35:59 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 @@ -142,6 +143,15 @@
    1.12  	HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy.
    1.13  
    1.14  	All this stuff could be much less complicated if I didn't try to preserve binary compatibility :)
    1.15 +
    1.16 +VP Hooks
    1.17 +	VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are
    1.18 +	implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which
    1.19 +	virtually concatenates the NULL-interface-hook-list with their normal hook list.
    1.20 +
    1.21 +
    1.22 +	I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together
    1.23 +	binary compatible beast which is pretty hard to maintain unless you've written it :)
    1.24  */
    1.25  
    1.26  namespace SourceHook
    1.27 @@ -160,6 +170,10 @@
    1.28  			char *DupProto(const char *src);
    1.29  			void FreeProto(char *prot);
    1.30  		public:
    1.31 +			CProto() : m_Proto(NULL)
    1.32 +			{
    1.33 +			}
    1.34 +
    1.35  			CProto(const char *szProto) : m_Proto(DupProto(szProto))
    1.36  			{
    1.37  			}
    1.38 @@ -192,6 +206,11 @@
    1.39  			{
    1.40  				return Equal(other.m_Proto, m_Proto);
    1.41  			}
    1.42 +
    1.43 +			const char *GetProto() const
    1.44 +			{
    1.45 +				return m_Proto;
    1.46 +			}
    1.47  		};
    1.48  
    1.49  
    1.50 @@ -226,17 +245,79 @@
    1.51  			HookManagerPubFunc hookman;
    1.52  		};
    1.53  
    1.54 +		// Associates hook ids with info about the hooks
    1.55 +		// Also used to keep track of used hook ids
    1.56 +		class CHookIDManager
    1.57 +		{
    1.58 +		public:
    1.59 +			struct Entry
    1.60 +			{
    1.61 +				bool isfree;
    1.62 +
    1.63 +				// hookman info
    1.64 +				CProto proto;
    1.65 +				int vtbl_offs;
    1.66 +				int vtbl_idx;
    1.67 +
    1.68 +				// vfnptr
    1.69 +				void *vfnptr;
    1.70 +
    1.71 +				// iface
    1.72 +				void* adjustediface;
    1.73 +
    1.74 +				// hook
    1.75 +				Plugin plug;
    1.76 +				int thisptr_offs;
    1.77 +				ISHDelegate *handler;
    1.78 +				bool post;
    1.79 +
    1.80 +				Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto,
    1.81 +					ISHDelegate *ph, bool ppost)
    1.82 +					: isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp), 
    1.83 +					adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost)
    1.84 +				{
    1.85 +				}
    1.86 +				Entry()
    1.87 +				{
    1.88 +				}
    1.89 +			};
    1.90 +		private:
    1.91 +			// Internally, hookid 1 is stored as m_Entries[0]
    1.92 +
    1.93 +			CVector<Entry> m_Entries;
    1.94 +		public:
    1.95 +			CHookIDManager();
    1.96 +			int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface,
    1.97 +				Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
    1.98 +			bool Remove(int hookid);
    1.99 +			const Entry * QueryHook(int hookid);
   1.100 +
   1.101 +			// Finds all hooks with the given info, and fills the hookids into output.
   1.102 +			void FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs, int vtbl_idx,
   1.103 +				void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
   1.104 +
   1.105 +			// Removes all hooks with a specified vfnptr
   1.106 +			bool RemoveAll(void *vfnptr);
   1.107 +		};
   1.108 +
   1.109  		struct HookInfo
   1.110  		{
   1.111  			ISHDelegate *handler;			//!< Pointer to the handler
   1.112  			bool paused;					//!< If true, the hook should not be executed
   1.113  			Plugin plug;					//!< The owner plugin
   1.114  			int thisptr_offs;				//!< This pointer offset
   1.115 +			int hookid;						//!< Unique ID given by CHookIDManager
   1.116 +
   1.117 +			bool operator==(int otherid)
   1.118 +			{
   1.119 +				return hookid == otherid;
   1.120 +			}
   1.121  		};
   1.122  
   1.123  		class CHookList : public IHookList
   1.124  		{
   1.125  		public:
   1.126 +			List<HookInfo> *m_VPList;			// left-hand list for ListCatIterator -> for VP hooks
   1.127  			List<HookInfo> m_List;
   1.128  
   1.129  			friend class CIter;
   1.130 @@ -250,7 +331,7 @@
   1.131  				void SkipPaused();
   1.132  			public:
   1.133  
   1.134 -				List<HookInfo>::iterator m_Iter;
   1.135 +				ListCatIterator<HookInfo> m_Iter;
   1.136  
   1.137  				CIter(CHookList *pList);
   1.138  
   1.139 @@ -293,6 +374,9 @@
   1.140  
   1.141  			IIter *GetIter();
   1.142  			void ReleaseIter(IIter *pIter);
   1.143 +
   1.144 +			void SetVPList(List<HookInfo> *newList);
   1.145 +			void ClearVPList();
   1.146  		};
   1.147  
   1.148  		// I know, data hiding... But I'm a lazy bastard!
   1.149 @@ -315,6 +399,10 @@
   1.150  			{
   1.151  				return m_Ptr == ptr;
   1.152  			}
   1.153 +			bool operator!=(void *ptr)
   1.154 +			{
   1.155 +				return m_Ptr != ptr;
   1.156 +			}
   1.157  		};
   1.158  
   1.159  		class CVfnPtr : public IVfnPtr
   1.160 @@ -520,6 +608,7 @@
   1.161  		void SetPluginPaused(Plugin plug, bool paused);
   1.162  
   1.163  		HookLoopInfoStack m_HLIStack;
   1.164 +		CHookIDManager m_HookIDMan;
   1.165  	public:
   1.166  		CSourceHookImpl();
   1.167  		virtual ~CSourceHookImpl();
   1.168 @@ -650,6 +739,32 @@
   1.169  
   1.170  		virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
   1.171  			void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr);
   1.172 +
   1.173 +		/**
   1.174 +		*	@brief Add a (VP) hook.
   1.175 +		*
   1.176 +		*	@return non-zero hook id on success, 0 otherwise
   1.177 +		*
   1.178 +		*	@param plug The unique identifier of the plugin that calls this function
   1.179 +		*	@param mode	Can be either Hook_Normal or Hook_VP (vtable-wide hook)
   1.180 +		*	@param iface The interface pointer
   1.181 +		*	@param ifacesize The size of the class iface points to
   1.182 +		*	@param myHookMan A hook manager function that should be capable of handling the function
   1.183 +		*	@param handler A pointer to a FastDelegate containing the hook handler
   1.184 +		*	@param post Set to true if you want a post handler
   1.185 +		*/
   1.186 +		virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
   1.187 +			ISHDelegate *handler, bool post);
   1.188 +
   1.189 +		/**
   1.190 +		*	@brief Remove a VP hook by ID.
   1.191 +		*
   1.192 +		*	@return true on success, false otherwise
   1.193 +		*
   1.194 +		*	@param plug The unique identifier of the plugin that calls this function
   1.195 +		*	@param hookid The hook id (returned by AddHookNew)
   1.196 +		*/
   1.197 +		virtual bool RemoveHookByID(Plugin plug, int hookid);
   1.198  	};
   1.199  }
   1.200