It seems to work, but it's not reentrant yet.
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;
75 List<HookInfo>::iterator m_Iter;
77 CIter(CHookList *pList);
85 ISHDelegate *Handler();
90 CIter *m_pNext; // When stored in m_FreeIters and m_UsedIters
91 CIter *m_pPrev; // Only used when stored in m_UsedIters
98 CHookList(const CHookList &other);
101 void operator=(const CHookList &other);
104 void ReleaseIter(IIter *pIter);
107 // I know, data hiding... But I'm a lazy bastard!
109 class CIface : public IIface
113 CHookList m_PreHooks;
114 CHookList m_PostHooks;
120 IHookList *GetPreHooks();
121 IHookList *GetPostHooks();
123 bool operator==(void *ptr)
129 class CVfnPtr : public IVfnPtr
132 typedef List<CIface> IfaceList;
133 typedef IfaceList::iterator IfaceListIter;
145 void *GetOrigEntry();
147 virtual IIface *FindIface(void *ptr);
149 bool operator==(void *ptr)
155 class CHookManagerInfo : public IHookManagerInfo
158 typedef List<CVfnPtr> VfnPtrList;
159 typedef VfnPtrList::iterator VfnPtrListIter;
162 HookManagerPubFunc m_Func;
167 void *m_HookfuncVfnptr;
169 VfnPtrList m_VfnPtrs;
172 virtual ~CHookManagerInfo();
174 IVfnPtr *FindVfnPtr(void *vfnptr);
176 void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
177 void SetHookfuncVfnptr(void *hookfunc_vfnptr);
180 * @brief A list of CHookManagerInfo classes
182 typedef List<CHookManagerInfo> HookManInfoList;
184 class CCallClassImpl : public GenericCallClass
188 typedef SourceHook::CVector<void*> OrigFuncs;
189 typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
191 void *m_Ptr; //!< Pointer to the actual object
192 size_t m_ObjSize; //!< Size of the instance
193 OrigVTables m_VT; //!< Info about vtables & functions
197 CCallClassImpl(void *ptr, size_t size);
198 virtual ~CCallClassImpl();
200 bool operator==(void *other)
202 return m_Ptr == other;
206 void *GetOrigFunc(int vtbloffs, int vtblidx);
208 void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
209 void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
213 * @brief A list of CallClass structures
215 typedef List<CCallClassImpl> Impl_CallClassList;
217 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
218 HookManInfoList m_HookMans; //!< A list of hook managers
221 * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
223 HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
224 const char *proto, int vtblofs, int vtblidx);
226 void ApplyCallClassPatches(CCallClassImpl &cc);
227 void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
228 void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
230 void SetPluginPaused(Plugin plug, bool paused);
232 META_RES m_Status, m_PrevRes, m_CurRes;
233 HookLoopStatus m_HLS;
235 const void *m_OrigRet;
236 const void *m_OverrideRet;
240 virtual ~CSourceHookImpl();
243 * @brief Returns the interface version
245 int GetIfaceVersion();
248 * @brief Returns the implemnetation version
250 int GetImplVersion();
253 * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
255 void UnloadPlugin(Plugin plug);
258 * @brief Shut down the whole system, unregister all hook managers
260 void CompleteShutdown();
265 * @return True if the function succeeded, false otherwise
267 * @param plug The unique identifier of the plugin that calls this function
268 * @param iface The interface pointer
269 * @param ifacesize The size of the class iface points to
270 * @param myHookMan A hook manager function that should be capable of handling the function
271 * @param handler A pointer to a FastDelegate containing the hook handler
272 * @param post Set to true if you want a post handler
274 bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
277 * @brief Removes a hook.
279 * @return True if the function succeeded, false otherwise
281 * @param plug The unique identifier of the plugin that calls this function
282 * @param iface The interface pointer
283 * @param thisptr_offs This pointer adjuster
284 * @param myHookMan A hook manager function that should be capable of handling the function
285 * @param handler A pointer to a FastDelegate containing the hook handler
286 * @param post Set to true if you want a post handler
288 bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
291 * @brief Removes a hook.
293 * @ return True if the function succeeded, false otherwise
295 * @param info A RemoveHookInfo structure, describing the hook
297 bool RemoveHook(RemoveHookInfo info);
300 * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
302 * @param plug The unique identifier of the plugin in question
304 bool IsPluginInUse(Plugin plug);
307 * @brief Pauses all hooks of a plugin
309 * @param plug The unique identifier of the plugin
311 void PausePlugin(Plugin plug);
314 * @brief Unpauses all hooks of a plugin
316 * @param plug The unique identifier of the plugin
318 void UnpausePlugin(Plugin plug);
321 * @brief Return a pointer to a callclass. Generate a new one if required.
323 * @param iface The interface pointer
324 * @param size Size of the class instance
326 GenericCallClass *GetCallClass(void *iface, size_t size);
329 * @brief Release a callclass
331 * @param ptr Pointer to the callclass
333 virtual void ReleaseCallClass(GenericCallClass *ptr);
335 virtual void SetRes(META_RES res); //!< Sets the meta result
336 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
337 virtual META_RES GetStatus(); //!< Gets the highest meta result
338 virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
339 virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
340 virtual void *GetIfacePtr(); //!< Gets the interface pointer
342 //////////////////////////////////////////////////////////////////////////
344 virtual META_RES &GetCurResRef(); //!< Gets the reference to the current meta result
345 virtual META_RES &GetPrevResRef(); //!< Gets the reference to the previous meta result
346 virtual META_RES &GetStatusRef(); //!< Gets the reference to the status variable
347 virtual void* &GetIfacePtrRef(); //!< Gets the reference to the iface ptr
348 virtual void SetOrigRet(const void *ptr); //!< Sets the original return pointer
349 virtual void SetOverrideRet(const void *ptr); //!< Sets the override result pointer
350 HookLoopStatus &GetStatusVarRef(IIface *pIface);