sourcehook/sourcehook_impl.h
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 07 21:07:43 2005 +0000 (2005-07-07)
changeset 90 828fa4d35bdf
parent 86 0c0c0ab2e9ba
child 93 ba222649327d
permissions -rw-r--r--
Removed IsBadReadPtr and BadReadHandler from CSourceHookImpl to fix Linux compile bug
     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 
    16 #ifdef __linux__
    17 #include <signal.h>
    18 #include <setjmp.h>
    19 #endif
    20 
    21 // Set this to 1 to enable runtime code generation (faster)
    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 		/**
    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 		/**
    52 		*	@brief A list of HookManagerInfo structures
    53 		*/
    54 		typedef std::list<HookManagerInfo> HookManInfoList;
    55 
    56 		struct CallClassInfo
    57 		{
    58 			GenericCallClass cc;
    59 			int refcounter;
    60 			bool operator==(void *other)
    61 			{
    62 				return cc.ptr == other;
    63 			}
    64 		};
    65 		/**
    66 		*	@brief A list of CallClass structures
    67 		*/
    68 		typedef std::list<CallClassInfo> Impl_CallClassList;
    69 
    70 		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
    71 		HookManInfoList m_HookMans;					//!< A list of hook managers
    72 
    73 		/**
    74 		*	@brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
    75 		*/
    76 		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end, 
    77 			const char *proto, int vtblofs, int vtblidx);
    78 
    79 		void ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
    80 		void ApplyCallClassPatches(CallClassInfo &cc);
    81 		void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
    82 		void RemoveCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx);
    83 		void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
    84 
    85 		META_RES m_Status, m_PrevRes, m_CurRes;
    86 		const void *m_OrigRet;
    87 		const void *m_OverrideRet;
    88 		void *m_IfacePtr;
    89 	public:
    90 		CSourceHookImpl();
    91 		~CSourceHookImpl();
    92 
    93 		/**
    94 		*	@brief Returns the interface version
    95 		*/
    96 		int GetIfaceVersion();
    97 
    98 		/**
    99 		*	@brief Returns the implemnetation version
   100 		*/
   101 		int GetImplVersion();
   102 
   103 		/**
   104 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
   105 		*/
   106 		void UnloadPlugin(Plugin plug);
   107 
   108 		/**
   109 		*	@brief Shut down the whole system, unregister all hook managers
   110 		*/
   111 		void CompleteShutdown();
   112 
   113 		/**
   114 		*	@brief Add a hook.
   115 		*
   116 		*	@return True if the function succeeded, false otherwise
   117 		*
   118 		*	@param plug The unique identifier of the plugin that calls this function
   119 		*	@param iface The interface pointer
   120 		*	@param ifacesize The size of the class iface points to
   121 		*	@param myHookMan A hook manager function that should be capable of handling the function
   122 		*	@param handler A pointer to a FastDelegate containing the hook handler
   123 		*	@param post Set to true if you want a post handler
   124 		*/
   125 		bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   126 
   127 		/**
   128 		*	@brief Removes a hook.
   129 		*
   130 		*	@return True if the function succeeded, false otherwise
   131 		*
   132 		*	@param plug The unique identifier of the plugin that calls this function
   133 		*	@param iface The interface pointer
   134 		*	@param thisptr_offs This pointer adjuster
   135 		*	@param myHookMan A hook manager function that should be capable of handling the function
   136 		*	@param handler A pointer to a FastDelegate containing the hook handler
   137 		*	@param post Set to true if you want a post handler
   138 		*/
   139 		bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   140 
   141 		/**
   142 		*	@brief Removes a hook.
   143 		*
   144 		*	@ return True if the function succeeded, false otherwise
   145 		*
   146 		*	@param info A RemoveHookInfo structure, describing the hook
   147 		*/
   148 		bool RemoveHook(RemoveHookInfo info);
   149 
   150 		/**
   151 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   152 		*
   153 		*	@param plug The unique identifier of the plugin in question
   154 		*/
   155 		bool IsPluginInUse(Plugin plug);
   156 
   157 		/**
   158 		*	@brief Pauses all hooks of a plugin
   159 		*
   160 		*	@param plug The unique identifier of the plugin
   161 		*/
   162 		void PausePlugin(Plugin plug);
   163 
   164 		/**
   165 		*	@brief Unpauses all hooks of a plugin
   166 		*
   167 		*	@param plug The unique identifier of the plugin
   168 		*/
   169 		void UnpausePlugin(Plugin plug);
   170 
   171 		/**
   172 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   173 		*
   174 		*	@param iface The interface pointer
   175 		*	@param size Size of the class instance
   176 		*/
   177 		GenericCallClass *GetCallClass(void *iface, size_t size);
   178 
   179 		/**
   180 		*	@brief Release a callclass
   181 		*
   182 		*	@param ptr Pointer to the callclass
   183 		*/
   184 		virtual void ReleaseCallClass(GenericCallClass *ptr);
   185 
   186 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   187 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   188 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   189 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   190 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   191 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   192 
   193 		//////////////////////////////////////////////////////////////////////////
   194 		// For hook managers
   195 		virtual META_RES &GetCurResRef();				//!< Gets the reference to the current meta result
   196 		virtual META_RES &GetPrevResRef();				//!< Gets the reference to the previous meta result
   197 		virtual META_RES &GetStatusRef();				//!< Gets the reference to the status variable
   198 		virtual void* &GetIfacePtrRef();				//!< Gets the reference to the iface ptr
   199 		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   200 		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   201 	};
   202 
   203 #ifdef __linux__
   204 	/**
   205 	*	@brief Checks to see if a memory value can be read from a given pointer.
   206 	*
   207 	*	@return True if the value can be read and false if it cannot.
   208 	*
   209 	*	@param ptr The pointer to the memory value.
   210 	*	@param len The length of the memory value to be read from the pointer.
   211 	*/
   212 	bool IsBadReadPtr(const void *ptr, size_t len);
   213 	void BadReadHandler(int sig);
   214 
   215 	bool g_BadReadCalled;
   216 	jmp_buf g_BadReadJmpBuf;
   217 #endif
   218 }
   219 
   220 #endif
   221