sourcehook/sourcehook_impl.h
author Pavol Marko <pavolmarko@gmail.com>
Mon Oct 03 17:20:22 2005 +0000 (2005-10-03)
changeset 117 e322da4af2d2
parent 96 e023f17b7b73
child 121 158b860440c0
permissions -rw-r--r--
CHANGES!
sh_memory.h: Added ModuleInMemory; first checks /proc/self/maps OR /proc/curproc/map; if not available tries DS's sigsegv trapping method
sh_tinyhash: Added simple erase function(s)
test/Makefile: New makefile
Changed internal interface: sourcehook.hxx now doesn't need to know about implementation details like vectors etc.
     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 				List<HookInfo>::iterator m_Iter;
    72 
    73 				void SkipPaused();
    74 			public:
    75 				CIter(CHookList *pList);
    76 
    77 				virtual ~CIter();
    78 
    79 				void GoToBegin();
    80 
    81 				bool End();
    82 				void Next();
    83 				ISHDelegate *Handler();
    84 				int ThisPtrOffs();
    85 
    86 				CIter *m_pNext;		// When stored in m_FreeIters
    87 			};
    88 
    89 			CIter *m_FreeIters;
    90 
    91 			CHookList();
    92 			CHookList(const CHookList &other);
    93 			virtual ~CHookList();
    94 
    95 			void operator=(const CHookList &other);
    96 
    97 			IIter *GetIter();
    98 			void ReleaseIter(IIter *pIter);
    99 		};
   100 
   101 		// I know, data hiding... But I'm a lazy bastard!
   102 
   103 		class CIface : public IIface
   104 		{
   105 		public:
   106 			void *m_Ptr;
   107 			CHookList m_PreHooks;
   108 			CHookList m_PostHooks;
   109 		public:
   110 			CIface(void *ptr);
   111 			virtual ~CIface();
   112 
   113 			void *GetPtr();
   114 			IHookList *GetPreHooks();
   115 			IHookList *GetPostHooks();
   116 
   117 			bool operator==(void *ptr)
   118 			{
   119 				return m_Ptr == ptr;
   120 			}
   121 		};
   122 
   123 		class CVfnPtr : public IVfnPtr
   124 		{
   125 		public:
   126 			typedef List<CIface> IfaceList;
   127 			typedef IfaceList::iterator IfaceListIter;
   128 
   129 			void *m_Ptr;
   130 			void *m_OrigEntry;
   131 
   132 			IfaceList m_Ifaces;
   133 
   134 		public:
   135 			CVfnPtr(void *ptr);
   136 			virtual ~CVfnPtr();
   137 
   138 			void *GetVfnPtr();
   139 			void *GetOrigEntry();
   140 
   141 			virtual IIface *FindIface(void *ptr);
   142 
   143 			bool operator==(void *ptr)
   144 			{
   145 				return m_Ptr == ptr;
   146 			}
   147 		};
   148 
   149 		class CHookManagerInfo : public IHookManagerInfo
   150 		{
   151 		public:
   152 			typedef List<CVfnPtr> VfnPtrList;
   153 			typedef VfnPtrList::iterator VfnPtrListIter;
   154 
   155 			Plugin m_Plug;
   156 			HookManagerPubFunc m_Func;
   157 
   158 			int m_VtblOffs;
   159 			int m_VtblIdx;
   160 			const char *m_Proto;
   161 			void *m_HookfuncVfnptr;
   162 
   163 			VfnPtrList m_VfnPtrs;
   164 
   165 		public:
   166 			virtual ~CHookManagerInfo();
   167 
   168 			IVfnPtr *FindVfnPtr(void *vfnptr);
   169 
   170 			void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
   171 			void SetHookfuncVfnptr(void *hookfunc_vfnptr);
   172 		};
   173 		/**
   174 		*	@brief A list of CHookManagerInfo classes
   175 		*/
   176 		typedef List<CHookManagerInfo> HookManInfoList;
   177 
   178 		class CCallClassImpl : public GenericCallClass
   179 		{
   180 		public:
   181 
   182 			typedef SourceHook::CVector<void*> OrigFuncs;
   183 			typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
   184 
   185 			void *m_Ptr;			//!< Pointer to the actual object
   186 			size_t m_ObjSize;		//!< Size of the instance
   187 			OrigVTables m_VT;		//!< Info about vtables & functions
   188 
   189 			int m_RefCounter;
   190 
   191 			CCallClassImpl(void *ptr, size_t size);
   192 			virtual ~CCallClassImpl();
   193 
   194 			bool operator==(void *other)
   195 			{
   196 				return m_Ptr == other;
   197 			}
   198 
   199 			void *GetThisPtr();
   200 			void *GetOrigFunc(int vtbloffs, int vtblidx);
   201 
   202 			void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
   203 			void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
   204 		};
   205 
   206 		/**
   207 		*	@brief A list of CallClass structures
   208 		*/
   209 		typedef List<CCallClassImpl> Impl_CallClassList;
   210 
   211 		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
   212 		HookManInfoList m_HookMans;					//!< A list of hook managers
   213 
   214 		/**
   215 		*	@brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
   216 		*/
   217 		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
   218 			const char *proto, int vtblofs, int vtblidx);
   219 
   220 		void ApplyCallClassPatches(CCallClassImpl &cc);
   221 		void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
   222 		void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
   223 
   224 		void SetPluginPaused(Plugin plug, bool paused);
   225 
   226 		META_RES m_Status, m_PrevRes, m_CurRes;
   227 		const void *m_OrigRet;
   228 		const void *m_OverrideRet;
   229 		void *m_IfacePtr;
   230 	public:
   231 		CSourceHookImpl();
   232 		virtual ~CSourceHookImpl();
   233 
   234 		/**
   235 		*	@brief Returns the interface version
   236 		*/
   237 		int GetIfaceVersion();
   238 
   239 		/**
   240 		*	@brief Returns the implemnetation version
   241 		*/
   242 		int GetImplVersion();
   243 
   244 		/**
   245 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
   246 		*/
   247 		void UnloadPlugin(Plugin plug);
   248 
   249 		/**
   250 		*	@brief Shut down the whole system, unregister all hook managers
   251 		*/
   252 		void CompleteShutdown();
   253 
   254 		/**
   255 		*	@brief Add a hook.
   256 		*
   257 		*	@return True if the function succeeded, false otherwise
   258 		*
   259 		*	@param plug The unique identifier of the plugin that calls this function
   260 		*	@param iface The interface pointer
   261 		*	@param ifacesize The size of the class iface points to
   262 		*	@param myHookMan A hook manager function that should be capable of handling the function
   263 		*	@param handler A pointer to a FastDelegate containing the hook handler
   264 		*	@param post Set to true if you want a post handler
   265 		*/
   266 		bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   267 
   268 		/**
   269 		*	@brief Removes a hook.
   270 		*
   271 		*	@return True if the function succeeded, false otherwise
   272 		*
   273 		*	@param plug The unique identifier of the plugin that calls this function
   274 		*	@param iface The interface pointer
   275 		*	@param thisptr_offs This pointer adjuster
   276 		*	@param myHookMan A hook manager function that should be capable of handling the function
   277 		*	@param handler A pointer to a FastDelegate containing the hook handler
   278 		*	@param post Set to true if you want a post handler
   279 		*/
   280 		bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   281 
   282 		/**
   283 		*	@brief Removes a hook.
   284 		*
   285 		*	@ return True if the function succeeded, false otherwise
   286 		*
   287 		*	@param info A RemoveHookInfo structure, describing the hook
   288 		*/
   289 		bool RemoveHook(RemoveHookInfo info);
   290 
   291 		/**
   292 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   293 		*
   294 		*	@param plug The unique identifier of the plugin in question
   295 		*/
   296 		bool IsPluginInUse(Plugin plug);
   297 
   298 		/**
   299 		*	@brief Pauses all hooks of a plugin
   300 		*
   301 		*	@param plug The unique identifier of the plugin
   302 		*/
   303 		void PausePlugin(Plugin plug);
   304 
   305 		/**
   306 		*	@brief Unpauses all hooks of a plugin
   307 		*
   308 		*	@param plug The unique identifier of the plugin
   309 		*/
   310 		void UnpausePlugin(Plugin plug);
   311 
   312 		/**
   313 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   314 		*
   315 		*	@param iface The interface pointer
   316 		*	@param size Size of the class instance
   317 		*/
   318 		GenericCallClass *GetCallClass(void *iface, size_t size);
   319 
   320 		/**
   321 		*	@brief Release a callclass
   322 		*
   323 		*	@param ptr Pointer to the callclass
   324 		*/
   325 		virtual void ReleaseCallClass(GenericCallClass *ptr);
   326 
   327 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   328 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   329 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   330 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   331 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   332 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   333 
   334 		//////////////////////////////////////////////////////////////////////////
   335 		// For hook managers
   336 		virtual META_RES &GetCurResRef();				//!< Gets the reference to the current meta result
   337 		virtual META_RES &GetPrevResRef();				//!< Gets the reference to the previous meta result
   338 		virtual META_RES &GetStatusRef();				//!< Gets the reference to the status variable
   339 		virtual void* &GetIfacePtrRef();				//!< Gets the reference to the iface ptr
   340 		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   341 		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   342 	};
   343 }
   344 
   345 #endif
   346