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"
23 * @brief The SourceHook implementation class
25 class CSourceHookImpl : public ISourceHook
29 * @brief A hook can be removed if you have this information
33 RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
34 ISHDelegate *phandler, bool ppost)
35 : plug(pplug), iface(piface), thisptr_offs(tpo),
36 hookman(phookman), handler(phandler), post(ppost)
43 HookManagerPubFunc hookman;
50 ISHDelegate *handler; //!< Pointer to the handler
51 bool paused; //!< If true, the hook should not be executed
52 Plugin plug; //!< The owner plugin
53 int thisptr_offs; //!< This pointer offset
56 class CHookList : public IHookList
59 List<HookInfo> m_List;
63 class CIter : public IHookList::IIter
65 friend class CHookList;
72 List<HookInfo>::iterator m_Iter;
74 CIter(CHookList *pList);
82 ISHDelegate *Handler();
87 CIter *m_pNext; // When stored in m_FreeIters and m_UsedIters
88 CIter *m_pPrev; // Only used when stored in m_UsedIters
95 CHookList(const CHookList &other);
98 void operator=(const CHookList &other);
101 void ReleaseIter(IIter *pIter);
104 // I know, data hiding... But I'm a lazy bastard!
106 class CIface : public IIface
110 CHookList m_PreHooks;
111 CHookList m_PostHooks;
117 IHookList *GetPreHooks();
118 IHookList *GetPostHooks();
120 bool operator==(void *ptr)
126 class CVfnPtr : public IVfnPtr
129 typedef List<CIface> IfaceList;
130 typedef IfaceList::iterator IfaceListIter;
142 void *GetOrigEntry();
144 virtual IIface *FindIface(void *ptr);
146 bool operator==(void *ptr)
152 class CHookManagerInfo : public IHookManagerInfo
155 typedef List<CVfnPtr> VfnPtrList;
156 typedef VfnPtrList::iterator VfnPtrListIter;
159 HookManagerPubFunc m_Func;
164 void *m_HookfuncVfnptr;
166 VfnPtrList m_VfnPtrs;
169 virtual ~CHookManagerInfo();
171 IVfnPtr *FindVfnPtr(void *vfnptr);
173 void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
174 void SetHookfuncVfnptr(void *hookfunc_vfnptr);
177 * @brief A list of CHookManagerInfo classes
179 typedef List<CHookManagerInfo> HookManInfoList;
181 class CCallClassImpl : public GenericCallClass
185 typedef SourceHook::CVector<void*> OrigFuncs;
186 typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
188 void *m_Ptr; //!< Pointer to the actual object
189 size_t m_ObjSize; //!< Size of the instance
190 OrigVTables m_VT; //!< Info about vtables & functions
194 CCallClassImpl(void *ptr, size_t size);
195 virtual ~CCallClassImpl();
197 bool operator==(void *other)
199 return m_Ptr == other;
203 void *GetOrigFunc(int vtbloffs, int vtblidx);
205 void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
206 void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
210 * @brief A list of CallClass structures
212 typedef List<CCallClassImpl> Impl_CallClassList;
214 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
215 HookManInfoList m_HookMans; //!< A list of hook managers
226 const void *pOrigRet;
227 const void *pOverrideRet;
230 typedef CStack<HookLoopInfo> HookLoopInfoStack;
233 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
235 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
236 const char *proto, int vtblofs, int vtblidx);
238 void ApplyCallClassPatches(CCallClassImpl &cc);
239 void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
240 void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
242 void SetPluginPaused(Plugin plug, bool paused);
244 bool ProtosEquiv(const char *p1, const char *p2);
246 HookLoopInfoStack m_HLIStack;
249 virtual ~CSourceHookImpl();
252 * @brief Returns the interface version
254 int GetIfaceVersion();
257 * @brief Returns the implemnetation version
259 int GetImplVersion();
262 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
264 void UnloadPlugin(Plugin plug);
267 * @brief Shut down the whole system, unregister all hook managers
269 void CompleteShutdown();
274 * @return True if the function succeeded, false otherwise
276 * @param plug The unique identifier of the plugin that calls this function
277 * @param iface The interface pointer
278 * @param ifacesize The size of the class iface points to
279 * @param myHookMan A hook manager function that should be capable of handling the function
280 * @param handler A pointer to a FastDelegate containing the hook handler
281 * @param post Set to true if you want a post handler
283 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
286 * @brief Removes a hook.
288 * @return True if the function succeeded, false otherwise
290 * @param plug The unique identifier of the plugin that calls this function
291 * @param iface The interface pointer
292 * @param thisptr_offs This pointer adjuster
293 * @param myHookMan A hook manager function that should be capable of handling the function
294 * @param handler A pointer to a FastDelegate containing the hook handler
295 * @param post Set to true if you want a post handler
297 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
300 * @brief Removes a hook.
302 * @ return True if the function succeeded, false otherwise
304 * @param info A RemoveHookInfo structure, describing the hook
306 bool RemoveHook(RemoveHookInfo info);
309 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
311 * @param plug The unique identifier of the plugin in question
313 bool IsPluginInUse(Plugin plug);
316 * @brief Pauses all hooks of a plugin
318 * @param plug The unique identifier of the plugin
320 void PausePlugin(Plugin plug);
323 * @brief Unpauses all hooks of a plugin
325 * @param plug The unique identifier of the plugin
327 void UnpausePlugin(Plugin plug);
330 * @brief Return a pointer to a callclass. Generate a new one if required.
332 * @param iface The interface pointer
333 * @param size Size of the class instance
335 GenericCallClass *GetCallClass(void *iface, size_t size);
338 * @brief Release a callclass
340 * @param ptr Pointer to the callclass
342 virtual void ReleaseCallClass(GenericCallClass *ptr);
344 virtual void SetRes(META_RES res); //!< Sets the meta result
345 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
346 virtual META_RES GetStatus(); //!< Gets the highest meta result
347 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
348 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
349 virtual void *GetIfacePtr(); //!< Gets the interface pointer
351 //////////////////////////////////////////////////////////////////////////
353 void HookLoopBegin(IIface *pIface); //!< Should be called when a hook loop begins
354 void HookLoopEnd(); //!< Should be called when a hook loop exits
355 void SetCurResPtr(META_RES *mres); //!< Sets pointer to the current meta result
356 void SetPrevResPtr(META_RES *mres); //!< Sets pointer to previous meta result
357 void SetStatusPtr(META_RES *mres); //!< Sets pointer to the status variable
358 void SetIfacePtrPtr(void **pp); //!< Sets pointer to the interface this pointer
359 void SetOrigRetPtr(const void *ptr); //!< Sets the original return pointer
360 void SetOverrideRetPtr(const void *ptr); //!< Sets the override result pointer
361 bool ShouldContinue(); //!< Returns false if the hook loop should exit
364 * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
366 * @param plug The owner of the hook manager
367 * @param pubFunc The hook manager's info function
369 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc);