1 /* ======== SourceHook ========
3 * No warranties of any kind
5 * This file declares necessary stuff for the sourcehook implementation.
6 * ============================
9 #ifndef __SOURCEHOOK_IMPL_H__
10 #define __SOURCEHOOK_IMPL_H__
12 #include "sourcehook.h"
14 // Set this to 1 to enable runtime code generation (faster)
15 #define SH_RUNTIME_CODEGEN 1
20 * @brief The SourceHook implementation class
22 class CSourceHookImpl : public ISourceHook
25 * @brief A list of HookerInfo structures
27 typedef std::list<HookerInfo> HookerInfoList;
31 * @brief A list of Impl_CallClass structures
33 typedef std::list<CallClass> Impl_CallClassList;
35 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
36 HookerInfoList m_Hookers; //!< A list of hookers
38 int m_PageSize; //!< Stores the system's page size
41 * @brief Finds a hooker for a function based on a text-prototype, a vtable offset and a vtable index
43 HookerInfoList::iterator FindHooker(HookerInfoList::iterator begin, HookerInfoList::iterator end,
44 const char *proto, int vtblofs, int vtblidx, int thisptrofs);
46 void FreeCallClass(CallClass &cc);
47 bool ApplyCallClassPatch(CallClass &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
49 static const int MAX_VTABLE_LEN = 4096; //!< Maximal vtable length in bytes
51 META_RES m_Status, m_PrevRes, m_CurRes;
52 const void *m_OrigRet;
53 const void *m_OverrideRet;
60 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hookers
62 void UnloadPlugin(Plugin plug);
65 * @brief Shut down the whole system, unregister all hookers
67 void CompleteShutdown();
72 * @return True if the function succeeded, false otherwise
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
80 bool AddHook(Plugin plug, void *iface, int ifacesize, Hooker myHooker, ISHDelegate *handler, bool post);
83 * @brief Removes a hook.
85 * @return True if the function succeeded, false otherwise
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
95 bool RemoveHook(Plugin plug, void *iface, Hooker myHooker, ISHDelegate *handler, bool post);
98 * @brief Checks whether a plugin has (a) hooker(s) that is/are currently used by other plugins
100 * @param plug The unique identifier of the plugin in question
102 bool IsPluginInUse(Plugin plug);
105 * @brief Return a pointer to a callclass. Generate a new one if required.
107 * @param iface The interface pointer
109 void *GetCallClass(void *iface, size_t size);
112 * @brief Release a callclass
114 * @param ptr Pointer to the callclass
116 virtual void ReleaseCallClass(void *ptr);
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
125 //////////////////////////////////////////////////////////////////////////
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