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 #include "sh_vector.h"
17 #include "sh_tinyhash.h"
19 // Set this to 1 to enable runtime code generation (faster)
20 // (unused at the moment, but may come back, so I'm leaving it in here!)
21 #define SH_RUNTIME_CODEGEN 1
26 * @brief The SourceHook implementation class
28 class CSourceHookImpl : public ISourceHook
32 * @brief A hook can be removed if you have this information
36 RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
37 ISHDelegate *phandler, bool ppost)
38 : plug(pplug), iface(piface), thisptr_offs(tpo),
39 hookman(phookman), handler(phandler), post(ppost)
46 HookManagerPubFunc hookman;
53 ISHDelegate *handler; //!< Pointer to the handler
54 bool paused; //!< If true, the hook should not be executed
55 Plugin plug; //!< The owner plugin
56 int thisptr_offs; //!< This pointer offset
59 class CHookList : public IHookList
62 List<HookInfo> m_List;
66 class CIter : public IHookList::IIter
68 friend class CHookList;
71 List<HookInfo>::iterator m_Iter;
75 CIter(CHookList *pList);
83 ISHDelegate *Handler();
86 CIter *m_pNext; // When stored in m_FreeIters
92 CHookList(const CHookList &other);
95 void operator=(const CHookList &other);
98 void ReleaseIter(IIter *pIter);
101 // I know, data hiding... But I'm a lazy bastard!
103 class CIface : public IIface
107 CHookList m_PreHooks;
108 CHookList m_PostHooks;
114 IHookList *GetPreHooks();
115 IHookList *GetPostHooks();
117 bool operator==(void *ptr)
123 class CVfnPtr : public IVfnPtr
126 typedef List<CIface> IfaceList;
127 typedef IfaceList::iterator IfaceListIter;
139 void *GetOrigEntry();
141 virtual IIface *FindIface(void *ptr);
143 bool operator==(void *ptr)
149 class CHookManagerInfo : public IHookManagerInfo
152 typedef List<CVfnPtr> VfnPtrList;
153 typedef VfnPtrList::iterator VfnPtrListIter;
156 HookManagerPubFunc m_Func;
161 void *m_HookfuncVfnptr;
163 VfnPtrList m_VfnPtrs;
166 virtual ~CHookManagerInfo();
168 IVfnPtr *FindVfnPtr(void *vfnptr);
170 void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
171 void SetHookfuncVfnptr(void *hookfunc_vfnptr);
174 * @brief A list of CHookManagerInfo classes
176 typedef List<CHookManagerInfo> HookManInfoList;
178 class CCallClassImpl : public GenericCallClass
182 typedef SourceHook::CVector<void*> OrigFuncs;
183 typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
185 void *m_Ptr; //!< Pointer to the actual object
186 size_t m_ObjSize; //!< Size of the instance
187 OrigVTables m_VT; //!< Info about vtables & functions
191 CCallClassImpl(void *ptr, size_t size);
192 virtual ~CCallClassImpl();
194 bool operator==(void *other)
196 return m_Ptr == other;
200 void *GetOrigFunc(int vtbloffs, int vtblidx);
202 void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
203 void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
207 * @brief A list of CallClass structures
209 typedef List<CCallClassImpl> Impl_CallClassList;
211 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
212 HookManInfoList m_HookMans; //!< A list of hook managers
215 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
217 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
218 const char *proto, int vtblofs, int vtblidx);
220 void ApplyCallClassPatches(CCallClassImpl &cc);
221 void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
222 void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
224 void SetPluginPaused(Plugin plug, bool paused);
226 META_RES m_Status, m_PrevRes, m_CurRes;
227 const void *m_OrigRet;
228 const void *m_OverrideRet;
232 virtual ~CSourceHookImpl();
235 * @brief Returns the interface version
237 int GetIfaceVersion();
240 * @brief Returns the implemnetation version
242 int GetImplVersion();
245 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
247 void UnloadPlugin(Plugin plug);
250 * @brief Shut down the whole system, unregister all hook managers
252 void CompleteShutdown();
257 * @return True if the function succeeded, false otherwise
259 * @param plug The unique identifier of the plugin that calls this function
260 * @param iface The interface pointer
261 * @param ifacesize The size of the class iface points to
262 * @param myHookMan A hook manager function that should be capable of handling the function
263 * @param handler A pointer to a FastDelegate containing the hook handler
264 * @param post Set to true if you want a post handler
266 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
269 * @brief Removes a hook.
271 * @return True if the function succeeded, false otherwise
273 * @param plug The unique identifier of the plugin that calls this function
274 * @param iface The interface pointer
275 * @param thisptr_offs This pointer adjuster
276 * @param myHookMan A hook manager function that should be capable of handling the function
277 * @param handler A pointer to a FastDelegate containing the hook handler
278 * @param post Set to true if you want a post handler
280 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
283 * @brief Removes a hook.
285 * @ return True if the function succeeded, false otherwise
287 * @param info A RemoveHookInfo structure, describing the hook
289 bool RemoveHook(RemoveHookInfo info);
292 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
294 * @param plug The unique identifier of the plugin in question
296 bool IsPluginInUse(Plugin plug);
299 * @brief Pauses all hooks of a plugin
301 * @param plug The unique identifier of the plugin
303 void PausePlugin(Plugin plug);
306 * @brief Unpauses all hooks of a plugin
308 * @param plug The unique identifier of the plugin
310 void UnpausePlugin(Plugin plug);
313 * @brief Return a pointer to a callclass. Generate a new one if required.
315 * @param iface The interface pointer
316 * @param size Size of the class instance
318 GenericCallClass *GetCallClass(void *iface, size_t size);
321 * @brief Release a callclass
323 * @param ptr Pointer to the callclass
325 virtual void ReleaseCallClass(GenericCallClass *ptr);
327 virtual void SetRes(META_RES res); //!< Sets the meta result
328 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
329 virtual META_RES GetStatus(); //!< Gets the highest meta result
330 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
331 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
332 virtual void *GetIfacePtr(); //!< Gets the interface pointer
334 //////////////////////////////////////////////////////////////////////////
336 virtual META_RES &GetCurResRef(); //!< Gets the reference to the current meta result
337 virtual META_RES &GetPrevResRef(); //!< Gets the reference to the previous meta result
338 virtual META_RES &GetStatusRef(); //!< Gets the reference to the status variable
339 virtual void* &GetIfacePtrRef(); //!< Gets the reference to the iface ptr
340 virtual void SetOrigRet(const void *ptr); //!< Sets the original return pointer
341 virtual void SetOverrideRet(const void *ptr); //!< Sets the override result pointer