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