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, HookManagerPubFunc phookman,
32 ISHDelegate *phandler, bool ppost)
33 : plug(pplug), iface(piface), hookman(phookman), handler(phandler), post(ppost)
39 HookManagerPubFunc hookman;
45 * @brief A list of HookManagerInfo structures
47 typedef std::list<HookManagerInfo> HookManInfoList;
53 bool operator==(void *other)
55 return cc.ptr == other;
59 * @brief A list of CallClass structures
61 typedef std::list<CallClassInfo> Impl_CallClassList;
63 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
64 HookManInfoList m_HookMans; //!< A list of hook managers
67 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
69 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
70 const char *proto, int vtblofs, int vtblidx);
72 void ApplyCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx, void *orig_entry);
73 void RemoveCallClassPatch(CallClassInfo &cc, int vtbl_offs, int vtbl_idx);
75 META_RES m_Status, m_PrevRes, m_CurRes;
76 const void *m_OrigRet;
77 const void *m_OverrideRet;
84 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
86 void UnloadPlugin(Plugin plug);
89 * @brief Shut down the whole system, unregister all hook managers
91 void CompleteShutdown();
96 * @return True if the function succeeded, false otherwise
98 * @param plug The unique identifier of the plugin that calls this function
99 * @param iface The interface pointer
100 * @param ifacesize The size of the class iface points to
101 * @param myHookMan A hook manager function that should be capable of handling the function
102 * @param handler A pointer to a FastDelegate containing the hook handler
103 * @param post Set to true if you want a post handler
105 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
108 * @brief Removes a hook.
110 * @return True if the function succeeded, false otherwise
112 * @param plug The unique identifier of the plugin that calls this function
113 * @param iface The interface pointer
114 * @param thisptr_offs This pointer adjuster
115 * @param myHookMan A hook manager function that should be capable of handling the function
116 * @param handler A pointer to a FastDelegate containing the hook handler
117 * @param post Set to true if you want a post handler
119 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
122 * @brief Removes a hook.
124 * @return True if the function succeeded, false otherwise
126 * @param plug The unique identifier of the plugin that calls this function
127 * @param iface Already adjusted interface pointer
128 * @param myHookMan A hook manager function that should be capable of handling the function
129 * @param handler A pointer to a FastDelegate containing the hook handler
130 * @param post Set to true if you want a post handler
132 bool RemoveHook(Plugin plug, void *iface, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
135 * @brief Removes a hook.
137 * @ return True if the function succeeded, false otherwise
139 * @param info A RemoveHookInfo structure, describing the hook
141 bool RemoveHook(RemoveHookInfo info);
144 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
146 * @param plug The unique identifier of the plugin in question
148 bool IsPluginInUse(Plugin plug);
151 * @brief Pauses all hooks of a plugin
153 * @param plug The unique identifier of the plugin
155 void PausePlugin(Plugin plug);
158 * @brief Unpauses all hooks of a plugin
160 * @param plug The unique identifier of the plugin
162 void UnpausePlugin(Plugin plug);
165 * @brief Return a pointer to a callclass. Generate a new one if required.
167 * @param iface The interface pointer
169 GenericCallClass *GetCallClass(void *iface);
172 * @brief Release a callclass
174 * @param ptr Pointer to the callclass
176 virtual void ReleaseCallClass(GenericCallClass *ptr);
178 virtual void SetRes(META_RES res); //!< Sets the meta result
179 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
180 virtual META_RES GetStatus(); //!< Gets the highest meta result
181 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
182 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
183 virtual void *GetIfacePtr(); //!< Gets the interface pointer
185 //////////////////////////////////////////////////////////////////////////
187 virtual META_RES &GetCurResRef(); //!< Gets the pointer to the current meta result
188 virtual META_RES &GetPrevResRef(); //!< Gets the pointer to the previous meta result
189 virtual META_RES &GetStatusRef(); //!< Gets the pointer to the status variable
190 virtual void SetOrigRet(const void *ptr); //!< Sets the original return pointer
191 virtual void SetOverrideRet(const void *ptr); //!< Sets the override result pointer
192 virtual void SetIfacePtr(void *ptr); //!< Sets the interface this pointer