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 HookLoopInfoStack m_HLIStack;
247 virtual ~CSourceHookImpl();
250 * @brief Returns the interface version
252 int GetIfaceVersion();
255 * @brief Returns the implemnetation version
257 int GetImplVersion();
260 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
262 void UnloadPlugin(Plugin plug);
265 * @brief Shut down the whole system, unregister all hook managers
267 void CompleteShutdown();
272 * @return True if the function succeeded, false otherwise
274 * @param plug The unique identifier of the plugin that calls this function
275 * @param iface The interface pointer
276 * @param ifacesize The size of the class iface points to
277 * @param myHookMan A hook manager function that should be capable of handling the function
278 * @param handler A pointer to a FastDelegate containing the hook handler
279 * @param post Set to true if you want a post handler
281 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
284 * @brief Removes a hook.
286 * @return True if the function succeeded, false otherwise
288 * @param plug The unique identifier of the plugin that calls this function
289 * @param iface The interface pointer
290 * @param thisptr_offs This pointer adjuster
291 * @param myHookMan A hook manager function that should be capable of handling the function
292 * @param handler A pointer to a FastDelegate containing the hook handler
293 * @param post Set to true if you want a post handler
295 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
298 * @brief Removes a hook.
300 * @ return True if the function succeeded, false otherwise
302 * @param info A RemoveHookInfo structure, describing the hook
304 bool RemoveHook(RemoveHookInfo info);
307 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
309 * @param plug The unique identifier of the plugin in question
311 bool IsPluginInUse(Plugin plug);
314 * @brief Pauses all hooks of a plugin
316 * @param plug The unique identifier of the plugin
318 void PausePlugin(Plugin plug);
321 * @brief Unpauses all hooks of a plugin
323 * @param plug The unique identifier of the plugin
325 void UnpausePlugin(Plugin plug);
328 * @brief Return a pointer to a callclass. Generate a new one if required.
330 * @param iface The interface pointer
331 * @param size Size of the class instance
333 GenericCallClass *GetCallClass(void *iface, size_t size);
336 * @brief Release a callclass
338 * @param ptr Pointer to the callclass
340 virtual void ReleaseCallClass(GenericCallClass *ptr);
342 virtual void SetRes(META_RES res); //!< Sets the meta result
343 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
344 virtual META_RES GetStatus(); //!< Gets the highest meta result
345 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
346 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
347 virtual void *GetIfacePtr(); //!< Gets the interface pointer
349 //////////////////////////////////////////////////////////////////////////
351 void HookLoopBegin(IIface *pIface); //!< Should be called when a hook loop begins
352 void HookLoopEnd(); //!< Should be called when a hook loop exits
353 void SetCurResPtr(META_RES *mres); //!< Sets pointer to the current meta result
354 void SetPrevResPtr(META_RES *mres); //!< Sets pointer to previous meta result
355 void SetStatusPtr(META_RES *mres); //!< Sets pointer to the status variable
356 void SetIfacePtrPtr(void **pp); //!< Sets pointer to the interface this pointer
357 void SetOrigRetPtr(const void *ptr); //!< Sets the original return pointer
358 void SetOverrideRetPtr(const void *ptr); //!< Sets the override result pointer
359 bool ShouldContinue(); //!< Returns false if the hook loop should exit