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"
20 // Set this to 1 to enable runtime code generation (faster)
21 // (unused at the moment, but may come back, so I'm leaving it in here!)
22 #define SH_RUNTIME_CODEGEN 1
27 * @brief The SourceHook implementation class
29 class CSourceHookImpl : public ISourceHook
33 * @brief A hook can be removed if you have this information
37 RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
38 ISHDelegate *phandler, bool ppost)
39 : plug(pplug), iface(piface), thisptr_offs(tpo),
40 hookman(phookman), handler(phandler), post(ppost)
47 HookManagerPubFunc hookman;
54 ISHDelegate *handler; //!< Pointer to the handler
55 bool paused; //!< If true, the hook should not be executed
56 Plugin plug; //!< The owner plugin
57 int thisptr_offs; //!< This pointer offset
60 class CHookList : public IHookList
63 List<HookInfo> m_List;
67 class CIter : public IHookList::IIter
69 friend class CHookList;
76 List<HookInfo>::iterator m_Iter;
78 CIter(CHookList *pList);
86 ISHDelegate *Handler();
91 CIter *m_pNext; // When stored in m_FreeIters and m_UsedIters
92 CIter *m_pPrev; // Only used when stored in m_UsedIters
99 CHookList(const CHookList &other);
100 virtual ~CHookList();
102 void operator=(const CHookList &other);
105 void ReleaseIter(IIter *pIter);
108 // I know, data hiding... But I'm a lazy bastard!
110 class CIface : public IIface
114 CHookList m_PreHooks;
115 CHookList m_PostHooks;
121 IHookList *GetPreHooks();
122 IHookList *GetPostHooks();
124 bool operator==(void *ptr)
130 class CVfnPtr : public IVfnPtr
133 typedef List<CIface> IfaceList;
134 typedef IfaceList::iterator IfaceListIter;
146 void *GetOrigEntry();
148 virtual IIface *FindIface(void *ptr);
150 bool operator==(void *ptr)
156 class CHookManagerInfo : public IHookManagerInfo
159 typedef List<CVfnPtr> VfnPtrList;
160 typedef VfnPtrList::iterator VfnPtrListIter;
163 HookManagerPubFunc m_Func;
168 void *m_HookfuncVfnptr;
170 VfnPtrList m_VfnPtrs;
173 virtual ~CHookManagerInfo();
175 IVfnPtr *FindVfnPtr(void *vfnptr);
177 void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
178 void SetHookfuncVfnptr(void *hookfunc_vfnptr);
181 * @brief A list of CHookManagerInfo classes
183 typedef List<CHookManagerInfo> HookManInfoList;
185 class CCallClassImpl : public GenericCallClass
189 typedef SourceHook::CVector<void*> OrigFuncs;
190 typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
192 void *m_Ptr; //!< Pointer to the actual object
193 size_t m_ObjSize; //!< Size of the instance
194 OrigVTables m_VT; //!< Info about vtables & functions
198 CCallClassImpl(void *ptr, size_t size);
199 virtual ~CCallClassImpl();
201 bool operator==(void *other)
203 return m_Ptr == other;
207 void *GetOrigFunc(int vtbloffs, int vtblidx);
209 void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
210 void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
214 * @brief A list of CallClass structures
216 typedef List<CCallClassImpl> Impl_CallClassList;
218 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
219 HookManInfoList m_HookMans; //!< A list of hook managers
230 const void *pOrigRet;
231 const void *pOverrideRet;
234 typedef CStack<HookLoopInfo> HookLoopInfoStack;
237 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
239 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
240 const char *proto, int vtblofs, int vtblidx);
242 void ApplyCallClassPatches(CCallClassImpl &cc);
243 void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
244 void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
246 void SetPluginPaused(Plugin plug, bool paused);
248 HookLoopInfoStack m_HLIStack;
251 virtual ~CSourceHookImpl();
254 * @brief Returns the interface version
256 int GetIfaceVersion();
259 * @brief Returns the implemnetation version
261 int GetImplVersion();
264 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
266 void UnloadPlugin(Plugin plug);
269 * @brief Shut down the whole system, unregister all hook managers
271 void CompleteShutdown();
276 * @return True if the function succeeded, false otherwise
278 * @param plug The unique identifier of the plugin that calls this function
279 * @param iface The interface pointer
280 * @param ifacesize The size of the class iface points to
281 * @param myHookMan A hook manager function that should be capable of handling the function
282 * @param handler A pointer to a FastDelegate containing the hook handler
283 * @param post Set to true if you want a post handler
285 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
288 * @brief Removes a hook.
290 * @return True if the function succeeded, false otherwise
292 * @param plug The unique identifier of the plugin that calls this function
293 * @param iface The interface pointer
294 * @param thisptr_offs This pointer adjuster
295 * @param myHookMan A hook manager function that should be capable of handling the function
296 * @param handler A pointer to a FastDelegate containing the hook handler
297 * @param post Set to true if you want a post handler
299 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
302 * @brief Removes a hook.
304 * @ return True if the function succeeded, false otherwise
306 * @param info A RemoveHookInfo structure, describing the hook
308 bool RemoveHook(RemoveHookInfo info);
311 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
313 * @param plug The unique identifier of the plugin in question
315 bool IsPluginInUse(Plugin plug);
318 * @brief Pauses all hooks of a plugin
320 * @param plug The unique identifier of the plugin
322 void PausePlugin(Plugin plug);
325 * @brief Unpauses all hooks of a plugin
327 * @param plug The unique identifier of the plugin
329 void UnpausePlugin(Plugin plug);
332 * @brief Return a pointer to a callclass. Generate a new one if required.
334 * @param iface The interface pointer
335 * @param size Size of the class instance
337 GenericCallClass *GetCallClass(void *iface, size_t size);
340 * @brief Release a callclass
342 * @param ptr Pointer to the callclass
344 virtual void ReleaseCallClass(GenericCallClass *ptr);
346 virtual void SetRes(META_RES res); //!< Sets the meta result
347 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
348 virtual META_RES GetStatus(); //!< Gets the highest meta result
349 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
350 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
351 virtual void *GetIfacePtr(); //!< Gets the interface pointer
353 //////////////////////////////////////////////////////////////////////////
355 void HookLoopBegin(IIface *pIface); //!< Should be called when a hook loop begins
356 void HookLoopEnd(); //!< Should be called when a hook loop exits
357 void SetCurResPtr(META_RES *mres); //!< Sets pointer to the current meta result
358 void SetPrevResPtr(META_RES *mres); //!< Sets pointer to previous meta result
359 void SetStatusPtr(META_RES *mres); //!< Sets pointer to the status variable
360 void SetIfacePtrPtr(void **pp); //!< Sets pointer to the interface this pointer
361 void SetOrigRetPtr(const void *ptr); //!< Sets the original return pointer
362 void SetOverrideRetPtr(const void *ptr); //!< Sets the override result pointer
363 bool ShouldContinue(); //!< Returns false if the hook loop should exit