1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
11 #ifndef __SOURCEHOOK_IMPL_H__
12 #define __SOURCEHOOK_IMPL_H__
14 #include "sourcehook.h"
16 // Set this to 1 to enable runtime code generation (faster)
17 #define SH_RUNTIME_CODEGEN 1
22 * @brief The SourceHook implementation class
24 class CSourceHookImpl : public ISourceHook
27 * @brief A hook can be removed if you have this information
31 RemoveHookInfo(Plugin p, void *i, HookManagerPubFunc h, ISHDelegate *hd, bool ps)
32 : plug(p), iface(i), hpf(h), handler(hd), post(ps)
38 HookManagerPubFunc hpf;
44 * @brief A list of HookManagerInfo structures
46 typedef std::list<HookManagerInfo> HookManInfoList;
50 * @brief A list of Impl_CallClass structures
52 typedef std::list<CallClass> Impl_CallClassList;
54 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
55 HookManInfoList m_HookMans; //!< A list of hook managers
57 int m_PageSize; //!< Stores the system's page size
60 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
62 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
63 const char *proto, int vtblofs, int vtblidx, int thisptrofs);
65 void FreeCallClass(CallClass &cc);
66 bool ApplyCallClassPatch(CallClass &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
68 static const int MAX_VTABLE_LEN = 4096; //!< Maximal vtable length in bytes
70 META_RES m_Status, m_PrevRes, m_CurRes;
71 const void *m_OrigRet;
72 const void *m_OverrideRet;
79 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
81 void UnloadPlugin(Plugin plug);
84 * @brief Shut down the whole system, unregister all hook managers
86 void CompleteShutdown();
91 * @return True if the function succeeded, false otherwise
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
100 bool AddHook(Plugin plug, void *iface, int ifacesize, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
103 * @brief Removes a hook.
105 * @return True if the function succeeded, false otherwise
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
113 bool RemoveHook(Plugin plug, void *iface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
116 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
118 * @param plug The unique identifier of the plugin in question
120 bool IsPluginInUse(Plugin plug);
123 * @brief Pauses all hooks of a plugin
125 * @param plug The unique identifier of the plugin
127 void PausePlugin(Plugin plug);
130 * @brief Unpauses all hooks of a plugin
132 * @param plug The unique identifier of the plugin
134 void UnpausePlugin(Plugin plug);
137 * @brief Return a pointer to a callclass. Generate a new one if required.
139 * @param iface The interface pointer
141 void *GetCallClass(void *iface, size_t size);
144 * @brief Release a callclass
146 * @param ptr Pointer to the callclass
148 virtual void ReleaseCallClass(void *ptr);
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
157 //////////////////////////////////////////////////////////////////////////
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