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