sourcehook/sourcehook_impl.h
author Pavol Marko <pavolmarko@gmail.com>
Thu Apr 14 16:17:40 2005 +0000 (2005-04-14)
changeset 3 5a9efd949f83
parent 1 586787053c73
child 6 4803fd7d28bb
permissions -rw-r--r--
Fixed bugs with multiple and virtual inheritance
     1 /* ======== SourceHook ========
     2 * By PM
     3 * No warranties of any kind
     4 *
     5 * This file declares necessary stuff for the sourcehook implementation.
     6 * ============================
     7 */
     8 
     9 #ifndef __SOURCEHOOK_IMPL_H__
    10 #define __SOURCEHOOK_IMPL_H__
    11 
    12 #include "sourcehook.h"
    13 
    14 // Set this to 1 to enable runtime code generation (faster)
    15 #define SH_RUNTIME_CODEGEN 1
    16 
    17 namespace SourceHook
    18 {
    19 	/**
    20 	*	@brief The SourceHook implementation class
    21 	*/
    22 	class CSourceHookImpl : public ISourceHook
    23 	{
    24 		/**
    25 		*	@brief A list of HookerInfo structures
    26 		*/
    27 		typedef std::list<HookerInfo> HookerInfoList;
    28 
    29 
    30 		/**
    31 		*	@brief A list of Impl_CallClass structures
    32 		*/
    33 		typedef std::list<CallClass> Impl_CallClassList;
    34 
    35 		Impl_CallClassList m_CallClasses;			//!< A list of already generated callclasses
    36 		HookerInfoList m_Hookers;					//!< A list of hookers
    37 
    38 		int m_PageSize;								//!< Stores the system's page size
    39 
    40 		/**
    41 		*	@brief Finds a hooker for a function based on a text-prototype, a vtable offset and a vtable index
    42 		*/
    43 		HookerInfoList::iterator FindHooker(HookerInfoList::iterator begin, HookerInfoList::iterator end, 
    44 			const char *proto, int vtblofs, int vtblidx, int thisptrofs);
    45 
    46 		void FreeCallClass(CallClass &cc);
    47 		bool ApplyCallClassPatch(CallClass &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
    48 
    49 		static const int MAX_VTABLE_LEN = 4096;		//!< Maximal vtable length in bytes
    50 
    51 		META_RES m_Status, m_PrevRes, m_CurRes;
    52 		const void *m_OrigRet;
    53 		const void *m_OverrideRet;
    54 		void *m_IfacePtr;
    55 	public:
    56 		CSourceHookImpl();
    57 		~CSourceHookImpl();
    58 
    59 		/**
    60 		*	@brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hookers
    61 		*/
    62 		void UnloadPlugin(Plugin plug);
    63 
    64 		/**
    65 		*	@brief Shut down the whole system, unregister all hookers
    66 		*/
    67 		void CompleteShutdown();
    68 
    69 		/**
    70 		*	@brief Add a hook.
    71 		*
    72 		*	@return True if the function succeeded, false otherwise
    73 		*
    74 		*	@param plug The unique identifier of the plugin that calls this function
    75 		*	@param iface The interface pointer
    76 		*	@param myHooker A hooker (hook manager) function that should be capable of handling the corresponding function
    77 		*	@param handler A pointer to a FastDelegate containing the hook handler
    78 		*	@param post Set to true if you want a post handler
    79 		*/
    80 		bool AddHook(Plugin plug, void *iface, int ifacesize, Hooker myHooker, ISHDelegate *handler, bool post);
    81 
    82 		/**
    83 		*	@brief Removes a hook.
    84 		*
    85 		*	@return True if the function succeeded, false otherwise
    86 		*
    87 		*	@param plug The unique identifier of the plugin that calls this function
    88 		*	@param iface The interface pointer
    89 		*	@param vtableoffset Offset to the vtable, in bytes
    90 		*	@param vtableidx Pointer to the vtable index of the function
    91 		*	@param proto A text version of the function prototype; generated by the macros
    92 		*	@param handler A pointer to a FastDelegate containing the hook handler
    93 		*	@param post Set to true if you want a post handler
    94 		*/
    95 		bool RemoveHook(Plugin plug, void *iface, Hooker myHooker, ISHDelegate *handler, bool post);
    96 
    97 		/**
    98 		*	@brief Checks whether a plugin has (a) hooker(s) that is/are currently used by other plugins
    99 		*
   100 		*	@param plug The unique identifier of the plugin in question
   101 		*/
   102 		bool IsPluginInUse(Plugin plug);
   103 
   104 		/**
   105 		*	@brief Return a pointer to a callclass. Generate a new one if required.
   106 		*
   107 		*	@param iface The interface pointer
   108 		*/
   109 		void *GetCallClass(void *iface, size_t size);
   110 
   111 		/**
   112 		*	@brief Release a callclass
   113 		*
   114 		*	@param ptr Pointer to the callclass
   115 		*/
   116 		virtual void ReleaseCallClass(void *ptr);
   117 
   118 		virtual void SetRes(META_RES res);				//!< Sets the meta result
   119 		virtual META_RES GetPrevRes();					//!< Gets the meta result of the previously called handler
   120 		virtual META_RES GetStatus();					//!< Gets the highest meta result
   121 		virtual const void *GetOrigRet();				//!< Gets the original result. If not in post function, undefined
   122 		virtual const void *GetOverrideRet();			//!< Gets the override result. If none is specified, NULL
   123 		virtual void *GetIfacePtr();					//!< Gets the interface pointer
   124 
   125 		//////////////////////////////////////////////////////////////////////////
   126 		// For hookers
   127 		virtual META_RES &GetCurResRef();				//!< Gets the pointer to the current meta result
   128 		virtual META_RES &GetPrevResRef();				//!< Gets the pointer to the previous meta result
   129 		virtual META_RES &GetStatusRef();				//!< Gets the pointer to the status variable
   130 		virtual void SetOrigRet(const void *ptr);		//!< Sets the original return pointer
   131 		virtual void SetOverrideRet(const void *ptr);	//!< Sets the override result pointer
   132 		virtual void SetIfacePtr(void *ptr);			//!< Sets the interface this pointer
   133 	};
   134 }
   135 
   136 #endif