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 pplug, void *piface, int tpo, HookManagerPubFunc phookman,
32 ISHDelegate *phandler, bool ppost)
33 : plug(pplug), iface(piface), thisptr_offs(tpo),
34 hookman(phookman), handler(phandler), post(ppost)
41 HookManagerPubFunc hookman;
47 * @brief A list of HookManagerInfo structures
49 typedef std::list<HookManagerInfo> HookManInfoList;
55 bool operator==(void *other)
57 return cc.ptr == other;
61 * @brief A list of CallClass structures
63 typedef std::list<CallClassInfo> Impl_CallClassList;
65 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
66 HookManInfoList m_HookMans; //!< A list of hook managers
69 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
71 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
72 const char *proto, int vtblofs, int vtblidx);
74 void ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
75 void ApplyCallClassPatches(CallClassInfo &cc);
76 void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
77 void RemoveCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx);
78 void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
80 META_RES m_Status, m_PrevRes, m_CurRes;
81 const void *m_OrigRet;
82 const void *m_OverrideRet;
89 * @brief Returns the interface version
91 int GetIfaceVersion();
94 * @brief Returns the implemnetation version
99 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
101 void UnloadPlugin(Plugin plug);
104 * @brief Shut down the whole system, unregister all hook managers
106 void CompleteShutdown();
111 * @return True if the function succeeded, false otherwise
113 * @param plug The unique identifier of the plugin that calls this function
114 * @param iface The interface pointer
115 * @param ifacesize The size of the class iface points to
116 * @param myHookMan A hook manager function that should be capable of handling the function
117 * @param handler A pointer to a FastDelegate containing the hook handler
118 * @param post Set to true if you want a post handler
120 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
123 * @brief Removes a hook.
125 * @return True if the function succeeded, false otherwise
127 * @param plug The unique identifier of the plugin that calls this function
128 * @param iface The interface pointer
129 * @param thisptr_offs This pointer adjuster
130 * @param myHookMan A hook manager function that should be capable of handling the function
131 * @param handler A pointer to a FastDelegate containing the hook handler
132 * @param post Set to true if you want a post handler
134 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
137 * @brief Removes a hook.
139 * @ return True if the function succeeded, false otherwise
141 * @param info A RemoveHookInfo structure, describing the hook
143 bool RemoveHook(RemoveHookInfo info);
146 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
148 * @param plug The unique identifier of the plugin in question
150 bool IsPluginInUse(Plugin plug);
153 * @brief Pauses all hooks of a plugin
155 * @param plug The unique identifier of the plugin
157 void PausePlugin(Plugin plug);
160 * @brief Unpauses all hooks of a plugin
162 * @param plug The unique identifier of the plugin
164 void UnpausePlugin(Plugin plug);
167 * @brief Return a pointer to a callclass. Generate a new one if required.
169 * @param iface The interface pointer
170 * @param size Size of the class instance
172 GenericCallClass *GetCallClass(void *iface, size_t size);
175 * @brief Release a callclass
177 * @param ptr Pointer to the callclass
179 virtual void ReleaseCallClass(GenericCallClass *ptr);
181 virtual void SetRes(META_RES res); //!< Sets the meta result
182 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
183 virtual META_RES GetStatus(); //!< Gets the highest meta result
184 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
185 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
186 virtual void *GetIfacePtr(); //!< Gets the interface pointer
188 //////////////////////////////////////////////////////////////////////////
190 virtual META_RES &GetCurResRef(); //!< Gets the reference to the current meta result
191 virtual META_RES &GetPrevResRef(); //!< Gets the reference to the previous meta result
192 virtual META_RES &GetStatusRef(); //!< Gets the reference to the status variable
193 virtual void* &GetIfacePtrRef(); //!< Gets the reference to the iface ptr
194 virtual void SetOrigRet(const void *ptr); //!< Sets the original return pointer
195 virtual void SetOverrideRet(const void *ptr); //!< Sets the override result pointer