sourcehook/sourcehook_impl.h
author David Anderson <dvander@alliedmods.net>
Tue Dec 06 03:13:50 2005 +0000 (2005-12-06)
changeset 150 22fbddfa0209
parent 137 edafdaacae96
child 157 064f77cac7ab
permissions -rw-r--r--
Fixed operator overload bug in List
Fixed memory leak in TinyHash
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 #ifndef __SOURCEHOOK_IMPL_H__
    12 #define __SOURCEHOOK_IMPL_H__
    13 
    14 #include "sourcehook.h"
    15 #include "sh_list.h"
    16 #include "sh_vector.h"
    17 #include "sh_tinyhash.h"
    18 #include "sh_stack.h"
    19 
    20 namespace SourceHook
    21 {
    22 	/**
    23 	*	@brief The SourceHook implementation class
    24 	*/
    25 	class CSourceHookImpl : public ISourceHook
    26 	{
    27 	private:
    28 		/**
    29 		*	@brief A hook can be removed if you have this information
    30 		*/
    31 		struct RemoveHookInfo
    32 		{
    33 			RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
    34 				ISHDelegate *phandler, bool ppost)
    35 				: plug(pplug), iface(piface), thisptr_offs(tpo),
    36 				hookman(phookman), handler(phandler), post(ppost)
    37 			{
    38 			}
    39 
    40 			Plugin plug;
    41 			void *iface;
    42 			int thisptr_offs;
    43 			HookManagerPubFunc hookman;
    44 			ISHDelegate *handler;
    45 			bool post;
    46 		};
    47 
    48 		struct HookInfo
    49 		{
    50 			ISHDelegate *handler;			//!< Pointer to the handler
    51 			bool paused;					//!< If true, the hook should not be executed
    52 			Plugin plug;					//!< The owner plugin
    53 			int thisptr_offs;				//!< This pointer offset
    54 		};
    55 
    56 		class CHookList : public IHookList
    57 		{
    58 		public:
    59 			List<HookInfo> m_List;
    60 
    61 			friend class CIter;
    62 
    63 			class CIter : public IHookList::IIter
    64 			{
    65 				friend class CHookList;
    66 
    67 				CHookList *m_pList;
    68 
    69 				void SkipPaused();
    70 			public:
    71 
    72 				List<HookInfo>::iterator m_Iter;
    73 
    74 				CIter(CHookList *pList);
    75 
    76 				virtual ~CIter();
    77 
    78 				void GoToBegin();
    79 
    80 				bool End();
    81 				void Next();
    82 				ISHDelegate *Handler();
    83 				int ThisPtrOffs();
    84 
    85 				void Clear();
    86 
    87 				CIter *m_pNext;		// When stored in m_FreeIters and m_UsedIters
    88 				CIter *m_pPrev;		// Only used when stored in m_UsedIters
    89 			};
    90 
    91 			CIter *m_FreeIters;
    92 			CIter *m_UsedIters;
    93 
    94 			CHookList();
    95 			CHookList(const CHookList &other);
    96 			virtual ~CHookList();
    97 
    98 			void operator=(const CHookList &other);
    99 
   100 			IIter *GetIter();
   101 			void ReleaseIter(IIter *pIter);
   102 		};
   103 
   104 		// I know, data hiding... But I'm a lazy bastard!
   105 
   106 		class CIface : public IIface
   107 		{
   108 		public:
   109 			void *m_Ptr;
   110 			CHookList m_PreHooks;
   111 			CHookList m_PostHooks;
   112 		public:
   113 			CIface(void *ptr);
   114 			virtual ~CIface();
   115 
   116 			void *GetPtr();
   117 			IHookList *GetPreHooks();
   118 			IHookList *GetPostHooks();
   119 
   120 			bool operator==(void *ptr)
   121 			{
   122 				return m_Ptr == ptr;
   123 			}
   124 		};
   125 
   126 		class CVfnPtr : public IVfnPtr
   127 		{
   128 		public:
   129 			typedef List<CIface> IfaceList;
   130 			typedef IfaceList::iterator IfaceListIter;
   131 
   132 			void *m_Ptr;
   133 			void *m_OrigEntry;
   134 
   135 			IfaceList m_Ifaces;
   136 
   137 		public:
   138 			CVfnPtr(void *ptr);
   139 			virtual ~CVfnPtr();
   140 
   141 			void *GetVfnPtr();
   142 			void *GetOrigEntry();
   143 
   144 			virtual IIface *FindIface(void *ptr);
   145 
   146 			bool operator==(void *ptr)
   147 			{
   148 				return m_Ptr == ptr;
   149 			}
   150 		};
   151 
   152 		class CHookManagerInfo : public IHookManagerInfo
   153 		{
   154 		public:
   155 			typedef List<CVfnPtr> VfnPtrList;
   156 			typedef VfnPtrList::iterator VfnPtrListIter;
   157 
   158 			Plugin m_Plug;
   159 			HookManagerPubFunc m_Func;
   160 
   161 			int m_VtblOffs;
   162 			int m_VtblIdx;
   163 			const char *m_Proto;
   164 			void *m_HookfuncVfnptr;
   165 
   166 			VfnPtrList m_VfnPtrs;
   167 
   168 		public:
   169 			virtual ~CHookManagerInfo();
   170 
   171 			IVfnPtr *FindVfnPtr(void *vfnptr);
   172 
   173 			void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
   174 			void SetHookfuncVfnptr(void *hookfunc_vfnptr);
   175 		};
   176 		/**
   177 		*	@brief A list of CHookManagerInfo classes
   178 		*/
   179 		typedef List<CHookManagerInfo> HookManInfoList;
   180 
   181 		class CCallClassImpl : public GenericCallClass
   182 		{
   183 		public:
   184 
   185 			typedef SourceHook::CVector<void*> OrigFuncs;
   186 			typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
   187 
   188 			void *m_Ptr;			//!< Pointer to the actual object
   189 			size_t m_ObjSize;		//!< Size of the instance
   190 			OrigVTables m_VT;		//!< Info about vtables & functions
   191 
   192 			int m_RefCounter;
   193 
   194 			CCallClassImpl(void *ptr, size_t size);
   195 			virtual ~CCallClassImpl();
   196 
   197 			bool operator==(void *other)
   198 			{
   199 				return m_Ptr == other;
   200 			}
   201 
   202 			void *GetThisPtr();
   203 			void *GetOrigFunc(int vtbloffs, int vtblidx);
   204 
   205 			void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
   206 			void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
   207 		};
   208 
   209 		/**
   210 		*	@brief A list of CallClass structures
   211 		*/
   212 		typedef List<CCallClassImpl> Impl_CallClassList;
   213 
   214 		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
   215 		HookManInfoList m_HookMans;					//!< A list of hook managers
   216 
   217 		struct HookLoopInfo
   218 		{
   219 			META_RES *pStatus;
   220 			META_RES *pPrevRes;
   221 			META_RES *pCurRes;
   222 
   223 			bool shouldContinue;
   224 
   225 			IIface *pCurIface;
   226 			const void *pOrigRet;
   227 			const void *pOverrideRet;
   228 			void **pIfacePtrPtr;
   229 		};
   230 		typedef CStack<HookLoopInfo> HookLoopInfoStack;
   231 
   232 		/**
   233 		*	@brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
   234 		*/
   235 		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
   236 			const char *proto, int vtblofs, int vtblidx);
   237 
   238 		void ApplyCallClassPatches(CCallClassImpl &cc);
   239 		void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
   240 		void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
   241 
   242 		void SetPluginPaused(Plugin plug, bool paused);
   243 
   244 		HookLoopInfoStack m_HLIStack;
   245 	public:
   246 		CSourceHookImpl();
   247 		virtual ~CSourceHookImpl();
   248 
   249 		/**
   250 		*	@brief Returns the interface version
   251 		*/
   252 		int GetIfaceVersion();
   253 
   254 		/**
   255 		*	@brief Returns the implemnetation version
   256 		*/
   257 		int GetImplVersion();
   258 
   259 		/**
   260 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
   261 		*/
   262 		void UnloadPlugin(Plugin plug);
   263 
   264 		/**
   265 		*	@brief Shut down the whole system, unregister all hook managers
   266 		*/
   267 		void CompleteShutdown();
   268 
   269 		/**
   270 		*	@brief Add a hook.
   271 		*
   272 		*	@return True if the function succeeded, false otherwise
   273 		*
   274 		*	@param plug The unique identifier of the plugin that calls this function
   275 		*	@param iface The interface pointer
   276 		*	@param ifacesize The size of the class iface points to
   277 		*	@param myHookMan A hook manager function that should be capable of handling the function
   278 		*	@param handler A pointer to a FastDelegate containing the hook handler
   279 		*	@param post Set to true if you want a post handler
   280 		*/
   281 		bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   282 
   283 		/**
   284 		*	@brief Removes a hook.
   285 		*
   286 		*	@return True if the function succeeded, false otherwise
   287 		*
   288 		*	@param plug The unique identifier of the plugin that calls this function
   289 		*	@param iface The interface pointer
   290 		*	@param thisptr_offs This pointer adjuster
   291 		*	@param myHookMan A hook manager function that should be capable of handling the function
   292 		*	@param handler A pointer to a FastDelegate containing the hook handler
   293 		*	@param post Set to true if you want a post handler
   294 		*/
   295 		bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   296 
   297 		/**
   298 		*	@brief Removes a hook.
   299 		*
   300 		*	@ return True if the function succeeded, false otherwise
   301 		*
   302 		*	@param info A RemoveHookInfo structure, describing the hook
   303 		*/
   304 		bool RemoveHook(RemoveHookInfo info);
   305 
   306 		/**
   307 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   308 		*
   309 		*	@param plug The unique identifier of the plugin in question
   310 		*/
   311 		bool IsPluginInUse(Plugin plug);
   312 
   313 		/**
   314 		*	@brief Pauses all hooks of a plugin
   315 		*
   316 		*	@param plug The unique identifier of the plugin
   317 		*/
   318 		void PausePlugin(Plugin plug);
   319 
   320 		/**
   321 		*	@brief Unpauses all hooks of a plugin
   322 		*
   323 		*	@param plug The unique identifier of the plugin
   324 		*/
   325 		void UnpausePlugin(Plugin plug);
   326 
   327 		/**
   328 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   329 		*
   330 		*	@param iface The interface pointer
   331 		*	@param size Size of the class instance
   332 		*/
   333 		GenericCallClass *GetCallClass(void *iface, size_t size);
   334 
   335 		/**
   336 		*	@brief Release a callclass
   337 		*
   338 		*	@param ptr Pointer to the callclass
   339 		*/
   340 		virtual void ReleaseCallClass(GenericCallClass *ptr);
   341 
   342 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   343 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   344 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   345 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   346 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   347 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   348 
   349 		//////////////////////////////////////////////////////////////////////////
   350 		// For hook managers
   351 		void HookLoopBegin(IIface *pIface);			//!< Should be called when a hook loop begins
   352 		void HookLoopEnd();							//!< Should be called when a hook loop exits
   353 		void SetCurResPtr(META_RES *mres);			//!< Sets pointer to the current meta result
   354 		void SetPrevResPtr(META_RES *mres);			//!< Sets pointer to previous meta result
   355 		void SetStatusPtr(META_RES *mres);			//!< Sets pointer to the status variable
   356 		void SetIfacePtrPtr(void **pp);				//!< Sets pointer to the interface this pointer
   357 		void SetOrigRetPtr(const void *ptr);		//!< Sets the original return pointer
   358 		void SetOverrideRetPtr(const void *ptr);	//!< Sets the override result pointer
   359 		bool ShouldContinue();						//!< Returns false if the hook loop should exit
   360 	};
   361 }
   362 
   363 #endif
   364