sourcehook/sourcehook_impl.h
author David Anderson <dvander@alliedmods.net>
Tue Apr 19 17:27:05 2005 +0000 (2005-04-19)
changeset 30 b36f706275e4
parent 10 406d8e5ddfc4
child 34 11e62a912700
permissions -rw-r--r--
Added some more comments
     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 p, void *i, HookManagerPubFunc h, ISHDelegate *hd, bool ps)
    32 				: plug(p), iface(i), hpf(h), handler(hd), post(ps)
    33 			{
    34 			}
    35 
    36 			Plugin plug;
    37 			void *iface;
    38 			HookManagerPubFunc hpf;
    39 			ISHDelegate *handler;
    40 			bool post;
    41 		};
    42 
    43 		/**
    44 		*	@brief A list of HookManagerInfo structures
    45 		*/
    46 		typedef std::list<HookManagerInfo> HookManInfoList;
    47 
    48 
    49 		/**
    50 		*	@brief A list of Impl_CallClass structures
    51 		*/
    52 		typedef std::list<CallClass> Impl_CallClassList;
    53 
    54 		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
    55 		HookManInfoList m_HookMans;					//!< A list of hook managers
    56 
    57 		int m_PageSize;								//!< Stores the system's page size
    58 
    59 		/**
    60 		*	@brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
    61 		*/
    62 		HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end, 
    63 			const char *proto, int vtblofs, int vtblidx, int thisptrofs);
    64 
    65 		void FreeCallClass(CallClass &cc);
    66 		bool ApplyCallClassPatch(CallClass &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
    67 
    68 		static const int MAX_VTABLE_LEN = 4096;		//!< Maximal vtable length in bytes
    69 
    70 		META_RES m_Status, m_PrevRes, m_CurRes;
    71 		const void *m_OrigRet;
    72 		const void *m_OverrideRet;
    73 		void *m_IfacePtr;
    74 	public:
    75 		CSourceHookImpl();
    76 		~CSourceHookImpl();
    77 
    78 		/**
    79 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
    80 		*/
    81 		void UnloadPlugin(Plugin plug);
    82 
    83 		/**
    84 		*	@brief Shut down the whole system, unregister all hook managers
    85 		*/
    86 		void CompleteShutdown();
    87 
    88 		/**
    89 		*	@brief Add a hook.
    90 		*
    91 		*	@return True if the function succeeded, false otherwise
    92 		*
    93 		*	@param plug The unique identifier of the plugin that calls this function
    94 		*	@param iface The interface pointer
    95 		*	@param ifacesize The size of the class iface points to
    96 		*	@param myHookMan A hook manager function that should be capable of handling the function
    97 		*	@param handler A pointer to a FastDelegate containing the hook handler
    98 		*	@param post Set to true if you want a post handler
    99 		*/
   100 		bool AddHook(Plugin plug, void *iface, int ifacesize, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   101 
   102 		/**
   103 		*	@brief Removes a hook.
   104 		*
   105 		*	@return True if the function succeeded, false otherwise
   106 		*
   107 		*	@param plug The unique identifier of the plugin that calls this function
   108 		*	@param iface The interface pointer
   109 		*	@param myHookMan A hook manager function that should be capable of handling the function
   110 		*	@param handler A pointer to a FastDelegate containing the hook handler
   111 		*	@param post Set to true if you want a post handler
   112 		*/
   113 		bool RemoveHook(Plugin plug, void *iface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
   114 
   115 		/**
   116 		*	@brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
   117 		*
   118 		*	@param plug The unique identifier of the plugin in question
   119 		*/
   120 		bool IsPluginInUse(Plugin plug);
   121 
   122 		/**
   123 		*	@brief Pauses all hooks of a plugin
   124 		*
   125 		*	@param plug The unique identifier of the plugin
   126 		*/
   127 		void PausePlugin(Plugin plug);
   128 
   129 		/**
   130 		*	@brief Unpauses all hooks of a plugin
   131 		*
   132 		*	@param plug The unique identifier of the plugin
   133 		*/
   134 		void UnpausePlugin(Plugin plug);
   135 
   136 		/**
   137 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   138 		*
   139 		*	@param iface The interface pointer
   140 		*/
   141 		void *GetCallClass(void *iface, size_t size);
   142 
   143 		/**
   144 		*	@brief Release a callclass
   145 		*
   146 		*	@param ptr Pointer to the callclass
   147 		*/
   148 		virtual void ReleaseCallClass(void *ptr);
   149 
   150 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   151 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   152 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   153 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   154 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   155 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   156 
   157 		//////////////////////////////////////////////////////////////////////////
   158 		// For hook managers
   159 		virtual META_RES &GetCurResRef();				//!< Gets the pointer to the current meta result
   160 		virtual META_RES &GetPrevResRef();				//!< Gets the pointer to the previous meta result
   161 		virtual META_RES &GetStatusRef();				//!< Gets the pointer to the status variable
   162 		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   163 		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   164 		virtual void SetIfacePtr(void *ptr);			//!< Sets the interface this pointer
   165 	};
   166 }
   167 
   168 #endif