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