sourcehook/sourcehook_impl.h
author Pavol Marko <pavolmarko@gmail.com>
Thu May 05 13:13:16 2005 +0000 (2005-05-05)
changeset 59 8edcba00a103
parent 58 71016f952503
child 86 0c0c0ab2e9ba
permissions -rw-r--r--
Linux/GCC/32 compatibility
     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 // Set this to 1 to enable runtime code generation (faster)
    17 #define SH_RUNTIME_CODEGEN 1
    18 
    19 namespace SourceHook
    20 {
    21 	/**
    22 	*	@brief The SourceHook implementation class
    23 	*/
    24 	class CSourceHookImpl : public ISourceHook
    25 	{
    26 		/**
    27 		*	@brief A hook can be removed if you have this information
    28 		*/
    29 		struct RemoveHookInfo
    30 		{
    31 			RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
    32 				ISHDelegate *phandler, bool ppost)
    33 				: plug(pplug), iface(piface), thisptr_offs(tpo),
    34 				hookman(phookman), handler(phandler), post(ppost)
    35 			{
    36 			}
    37 
    38 			Plugin plug;
    39 			void *iface;
    40 			int thisptr_offs;
    41 			HookManagerPubFunc hookman;
    42 			ISHDelegate *handler;
    43 			bool post;
    44 		};
    45 
    46 		/**
    47 		*	@brief A list of HookManagerInfo structures
    48 		*/
    49 		typedef std::list<HookManagerInfo> HookManInfoList;
    50 
    51 		struct CallClassInfo
    52 		{
    53 			GenericCallClass cc;
    54 			int refcounter;
    55 			bool operator==(void *other)
    56 			{
    57 				return cc.ptr == other;
    58 			}
    59 		};
    60 		/**
    61 		*	@brief A list of CallClass structures
    62 		*/
    63 		typedef std::list<CallClassInfo> Impl_CallClassList;
    64 
    65 		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
    66 		HookManInfoList m_HookMans;					//!< A list of hook managers
    67 
    68 		/**
    69 		*	@brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
    70 		*/
    71 		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end, 
    72 			const char *proto, int vtblofs, int vtblidx);
    73 
    74 		void ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
    75 		void ApplyCallClassPatches(CallClassInfo &cc);
    76 		void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
    77 		void RemoveCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx);
    78 		void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
    79 
    80 		META_RES m_Status, m_PrevRes, m_CurRes;
    81 		const void *m_OrigRet;
    82 		const void *m_OverrideRet;
    83 		void *m_IfacePtr;
    84 	public:
    85 		CSourceHookImpl();
    86 		~CSourceHookImpl();
    87 
    88 		/**
    89 		*	@brief Returns the interface version
    90 		*/
    91 		int GetIfaceVersion();
    92 
    93 		/**
    94 		*	@brief Returns the implemnetation version
    95 		*/
    96 		int GetImplVersion();
    97 
    98 		/**
    99 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
   100 		*/
   101 		void UnloadPlugin(Plugin plug);
   102 
   103 		/**
   104 		*	@brief Shut down the whole system, unregister all hook managers
   105 		*/
   106 		void CompleteShutdown();
   107 
   108 		/**
   109 		*	@brief Add a hook.
   110 		*
   111 		*	@return True if the function succeeded, false otherwise
   112 		*
   113 		*	@param plug The unique identifier of the plugin that calls this function
   114 		*	@param iface The interface pointer
   115 		*	@param ifacesize The size of the class iface points to
   116 		*	@param myHookMan A hook manager function that should be capable of handling the function
   117 		*	@param handler A pointer to a FastDelegate containing the hook handler
   118 		*	@param post Set to true if you want a post handler
   119 		*/
   120 		bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   121 
   122 		/**
   123 		*	@brief Removes a hook.
   124 		*
   125 		*	@return True if the function succeeded, false otherwise
   126 		*
   127 		*	@param plug The unique identifier of the plugin that calls this function
   128 		*	@param iface The interface pointer
   129 		*	@param thisptr_offs This pointer adjuster
   130 		*	@param myHookMan A hook manager function that should be capable of handling the function
   131 		*	@param handler A pointer to a FastDelegate containing the hook handler
   132 		*	@param post Set to true if you want a post handler
   133 		*/
   134 		bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   135 
   136 		/**
   137 		*	@brief Removes a hook.
   138 		*
   139 		*	@ return True if the function succeeded, false otherwise
   140 		*
   141 		*	@param info A RemoveHookInfo structure, describing the hook
   142 		*/
   143 		bool RemoveHook(RemoveHookInfo info);
   144 
   145 		/**
   146 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   147 		*
   148 		*	@param plug The unique identifier of the plugin in question
   149 		*/
   150 		bool IsPluginInUse(Plugin plug);
   151 
   152 		/**
   153 		*	@brief Pauses all hooks of a plugin
   154 		*
   155 		*	@param plug The unique identifier of the plugin
   156 		*/
   157 		void PausePlugin(Plugin plug);
   158 
   159 		/**
   160 		*	@brief Unpauses all hooks of a plugin
   161 		*
   162 		*	@param plug The unique identifier of the plugin
   163 		*/
   164 		void UnpausePlugin(Plugin plug);
   165 
   166 		/**
   167 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   168 		*
   169 		*	@param iface The interface pointer
   170 		*	@param size Size of the class instance
   171 		*/
   172 		GenericCallClass *GetCallClass(void *iface, size_t size);
   173 
   174 		/**
   175 		*	@brief Release a callclass
   176 		*
   177 		*	@param ptr Pointer to the callclass
   178 		*/
   179 		virtual void ReleaseCallClass(GenericCallClass *ptr);
   180 
   181 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   182 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   183 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   184 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   185 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   186 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   187 
   188 		//////////////////////////////////////////////////////////////////////////
   189 		// For hook managers
   190 		virtual META_RES &GetCurResRef();				//!< Gets the reference to the current meta result
   191 		virtual META_RES &GetPrevResRef();				//!< Gets the reference to the previous meta result
   192 		virtual META_RES &GetStatusRef();				//!< Gets the reference to the status variable
   193 		virtual void* &GetIfacePtrRef();				//!< Gets the reference to the iface ptr
   194 		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   195 		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   196 	};
   197 }
   198 
   199 #endif
   200