sourcehook/sourcehook_impl.h
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 07 18:41:08 2005 +0000 (2005-07-07)
changeset 88 2586c2eb900e
parent 86 0c0c0ab2e9ba
child 90 828fa4d35bdf
permissions -rw-r--r--
One last oops. I really need to install Linux again SOON :(
     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 #ifdef __linux__
    86 		/**
    87 		*	@brief Checks to see if a memory value can be read from a given pointer.
    88 		*
    89 		*	@return True if the value can be read and false if it cannot.
    90 		*
    91 		*	@param ptr The pointer to the memory value.
    92 		*	@param len The length of the memory value to be read from the pointer.
    93 		*/
    94 		static bool IsBadReadPtr(const void *ptr, size_t len);
    95 		static void BadReadHandler(int sig);
    96 
    97 		bool m_BadReadCalled;
    98 		jmp_buf m_BadReadJmpBuf;
    99 #endif
   100 
   101 		META_RES m_Status, m_PrevRes, m_CurRes;
   102 		const void *m_OrigRet;
   103 		const void *m_OverrideRet;
   104 		void *m_IfacePtr;
   105 	public:
   106 		CSourceHookImpl();
   107 		~CSourceHookImpl();
   108 
   109 		/**
   110 		*	@brief Returns the interface version
   111 		*/
   112 		int GetIfaceVersion();
   113 
   114 		/**
   115 		*	@brief Returns the implemnetation version
   116 		*/
   117 		int GetImplVersion();
   118 
   119 		/**
   120 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
   121 		*/
   122 		void UnloadPlugin(Plugin plug);
   123 
   124 		/**
   125 		*	@brief Shut down the whole system, unregister all hook managers
   126 		*/
   127 		void CompleteShutdown();
   128 
   129 		/**
   130 		*	@brief Add a hook.
   131 		*
   132 		*	@return True if the function succeeded, false otherwise
   133 		*
   134 		*	@param plug The unique identifier of the plugin that calls this function
   135 		*	@param iface The interface pointer
   136 		*	@param ifacesize The size of the class iface points to
   137 		*	@param myHookMan A hook manager function that should be capable of handling the function
   138 		*	@param handler A pointer to a FastDelegate containing the hook handler
   139 		*	@param post Set to true if you want a post handler
   140 		*/
   141 		bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   142 
   143 		/**
   144 		*	@brief Removes a hook.
   145 		*
   146 		*	@return True if the function succeeded, false otherwise
   147 		*
   148 		*	@param plug The unique identifier of the plugin that calls this function
   149 		*	@param iface The interface pointer
   150 		*	@param thisptr_offs This pointer adjuster
   151 		*	@param myHookMan A hook manager function that should be capable of handling the function
   152 		*	@param handler A pointer to a FastDelegate containing the hook handler
   153 		*	@param post Set to true if you want a post handler
   154 		*/
   155 		bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   156 
   157 		/**
   158 		*	@brief Removes a hook.
   159 		*
   160 		*	@ return True if the function succeeded, false otherwise
   161 		*
   162 		*	@param info A RemoveHookInfo structure, describing the hook
   163 		*/
   164 		bool RemoveHook(RemoveHookInfo info);
   165 
   166 		/**
   167 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   168 		*
   169 		*	@param plug The unique identifier of the plugin in question
   170 		*/
   171 		bool IsPluginInUse(Plugin plug);
   172 
   173 		/**
   174 		*	@brief Pauses all hooks of a plugin
   175 		*
   176 		*	@param plug The unique identifier of the plugin
   177 		*/
   178 		void PausePlugin(Plugin plug);
   179 
   180 		/**
   181 		*	@brief Unpauses all hooks of a plugin
   182 		*
   183 		*	@param plug The unique identifier of the plugin
   184 		*/
   185 		void UnpausePlugin(Plugin plug);
   186 
   187 		/**
   188 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   189 		*
   190 		*	@param iface The interface pointer
   191 		*	@param size Size of the class instance
   192 		*/
   193 		GenericCallClass *GetCallClass(void *iface, size_t size);
   194 
   195 		/**
   196 		*	@brief Release a callclass
   197 		*
   198 		*	@param ptr Pointer to the callclass
   199 		*/
   200 		virtual void ReleaseCallClass(GenericCallClass *ptr);
   201 
   202 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   203 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   204 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   205 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   206 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   207 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   208 
   209 		//////////////////////////////////////////////////////////////////////////
   210 		// For hook managers
   211 		virtual META_RES &GetCurResRef();				//!< Gets the reference to the current meta result
   212 		virtual META_RES &GetPrevResRef();				//!< Gets the reference to the previous meta result
   213 		virtual META_RES &GetStatusRef();				//!< Gets the reference to the status variable
   214 		virtual void* &GetIfacePtrRef();				//!< Gets the reference to the iface ptr
   215 		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   216 		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   217 	};
   218 }
   219 
   220 #endif
   221