1.1 --- a/core-legacy/sourcehook/sourcehook_impl.h Fri Feb 27 00:19:50 2009 -0600
1.2 +++ b/core-legacy/sourcehook/sourcehook_impl.h Wed Oct 28 23:14:32 2009 -0700
1.3 @@ -1,5 +1,5 @@
1.4 /* ======== SourceHook ========
1.5 -* Copyright (C) 2004-2008 Metamod:Source Development Team
1.6 +* Copyright (C) 2004-2007 Metamod:Source Development Team
1.7 * No warranties of any kind
1.8 *
1.9 * License: zlib/libpng
1.10 @@ -16,6 +16,7 @@
1.11 #include "sh_vector.h"
1.12 #include "sh_tinyhash.h"
1.13 #include "sh_stack.h"
1.14 +#include "sh_listcat.h"
1.15
1.16 /*
1.17
1.18 @@ -89,6 +90,9 @@
1.19
1.20 ---------------------------------------
1.21 Call classes
1.22 +
1.23 + !! deprecated !! - see below (new SH_CALL)
1.24 +
1.25 Call classes are identified by a this pointer and an instance size
1.26
1.27 We use the instance size because a derived class instance and a base class instance could
1.28 @@ -142,6 +146,28 @@
1.29 HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy.
1.30
1.31 All this stuff could be much less complicated if I didn't try to preserve binary compatibility :)
1.32 +
1.33 +VP Hooks
1.34 + VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are
1.35 + implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which
1.36 + virtually concatenates the NULL-interface-hook-list with their normal hook list.
1.37 +
1.38 +
1.39 + I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together
1.40 + binary compatible beast which is pretty hard to maintain unless you've written it :)
1.41 +
1.42 +New SH_CALL
1.43 + The addition of VP hooks messed up the Call Classes concept (see above) - call classes are bound to an
1.44 + instance pointer; they only work on one of the hooked instances. But VP hooks are called on all instances.
1.45 +
1.46 + That's why now, SH_CALL takes an instance pointer instead of a callclass pointer. It basically does this:
1.47 + 1) call SH_GLOB_PTR->SetIgnoreHooks(vfnptr)
1.48 + 2) call this->*mfp
1.49 + 3) call SH_GLOB_PTR->ResetIgnoreHooks(vfnptr)
1.50 +
1.51 + SourceHook stroes the "ignored vfnptr" and makes CVfnPtr::FindIface return NULL if the CVfnPtr instance
1.52 + corresponds to the ignored vfnptr. This way the hook manager thinks that the instance isn't hooked, and calls
1.53 + the original function. Everything works fine. This works even for VP hooks.
1.54 */
1.55
1.56 namespace SourceHook
1.57 @@ -160,6 +186,10 @@
1.58 char *DupProto(const char *src);
1.59 void FreeProto(char *prot);
1.60 public:
1.61 + CProto() : m_Proto(NULL)
1.62 + {
1.63 + }
1.64 +
1.65 CProto(const char *szProto) : m_Proto(DupProto(szProto))
1.66 {
1.67 }
1.68 @@ -192,6 +222,11 @@
1.69 {
1.70 return Equal(other.m_Proto, m_Proto);
1.71 }
1.72 +
1.73 + const char *GetProto() const
1.74 + {
1.75 + return m_Proto;
1.76 + }
1.77 };
1.78
1.79
1.80 @@ -226,17 +261,79 @@
1.81 HookManagerPubFunc hookman;
1.82 };
1.83
1.84 + // Associates hook ids with info about the hooks
1.85 + // Also used to keep track of used hook ids
1.86 + class CHookIDManager
1.87 + {
1.88 + public:
1.89 + struct Entry
1.90 + {
1.91 + bool isfree;
1.92 +
1.93 + // hookman info
1.94 + CProto proto;
1.95 + int vtbl_offs;
1.96 + int vtbl_idx;
1.97 +
1.98 + // vfnptr
1.99 + void *vfnptr;
1.100 +
1.101 + // iface
1.102 + void* adjustediface;
1.103 +
1.104 + // hook
1.105 + Plugin plug;
1.106 + int thisptr_offs;
1.107 + ISHDelegate *handler;
1.108 + bool post;
1.109 +
1.110 + Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto,
1.111 + ISHDelegate *ph, bool ppost)
1.112 + : isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp),
1.113 + adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost)
1.114 + {
1.115 + }
1.116 + Entry()
1.117 + {
1.118 + }
1.119 + };
1.120 + private:
1.121 + // Internally, hookid 1 is stored as m_Entries[0]
1.122 +
1.123 + CVector<Entry> m_Entries;
1.124 + public:
1.125 + CHookIDManager();
1.126 + int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface,
1.127 + Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
1.128 + bool Remove(int hookid);
1.129 + const Entry * QueryHook(int hookid);
1.130 +
1.131 + // Finds all hooks with the given info, and fills the hookids into output.
1.132 + void FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs, int vtbl_idx,
1.133 + void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
1.134 +
1.135 + // Removes all hooks with a specified vfnptr
1.136 + bool RemoveAll(void *vfnptr);
1.137 + };
1.138 +
1.139 struct HookInfo
1.140 {
1.141 ISHDelegate *handler; //!< Pointer to the handler
1.142 bool paused; //!< If true, the hook should not be executed
1.143 Plugin plug; //!< The owner plugin
1.144 int thisptr_offs; //!< This pointer offset
1.145 + int hookid; //!< Unique ID given by CHookIDManager
1.146 +
1.147 + bool operator==(int otherid)
1.148 + {
1.149 + return hookid == otherid;
1.150 + }
1.151 };
1.152
1.153 class CHookList : public IHookList
1.154 {
1.155 public:
1.156 + List<HookInfo> *m_VPList; // left-hand list for ListCatIterator -> for VP hooks
1.157 List<HookInfo> m_List;
1.158
1.159 friend class CIter;
1.160 @@ -250,14 +347,13 @@
1.161 void SkipPaused();
1.162 public:
1.163
1.164 - List<HookInfo>::iterator m_Iter;
1.165 + ListCatIterator<HookInfo> m_Iter;
1.166
1.167 CIter(CHookList *pList);
1.168
1.169 virtual ~CIter();
1.170
1.171 void GoToBegin();
1.172 - void GoToEnd();
1.173 void Set(CIter *pOther);
1.174
1.175 bool End();
1.176 @@ -277,7 +373,6 @@
1.177 // For recalls
1.178 bool m_Recall;
1.179 bool m_RQFlag;
1.180 - bool m_RelFlag;
1.181
1.182 void SetRecallState(); // Sets the list into a state where the next returned
1.183 // iterator (from GetIter) will be a copy of the last
1.184 @@ -285,9 +380,8 @@
1.185 // The hook resets this state automatically on:
1.186 // GetIter, ReleaseIter
1.187
1.188 - void RQFlagReset() { m_RQFlag = false; m_RelFlag = false; }
1.189 + void RQFlagReset() { m_RQFlag = false; }
1.190 bool RQFlagGet() { return m_RQFlag; }
1.191 - bool RelFlagGet() { return m_RelFlag; }
1.192 CHookList();
1.193 CHookList(const CHookList &other);
1.194 virtual ~CHookList();
1.195 @@ -296,6 +390,9 @@
1.196
1.197 IIter *GetIter();
1.198 void ReleaseIter(IIter *pIter);
1.199 +
1.200 + void SetVPList(List<HookInfo> *newList);
1.201 + void ClearVPList();
1.202 };
1.203
1.204 // I know, data hiding... But I'm a lazy bastard!
1.205 @@ -318,6 +415,10 @@
1.206 {
1.207 return m_Ptr == ptr;
1.208 }
1.209 + bool operator!=(void *ptr)
1.210 + {
1.211 + return m_Ptr != ptr;
1.212 + }
1.213 };
1.214
1.215 class CVfnPtr : public IVfnPtr
1.216 @@ -331,8 +432,9 @@
1.217
1.218 IfaceList m_Ifaces;
1.219
1.220 + void **m_pOneIgnore;
1.221 public:
1.222 - CVfnPtr(void *ptr);
1.223 + CVfnPtr(void *ptr, void **pOneIgnore);
1.224 virtual ~CVfnPtr();
1.225
1.226 void *GetVfnPtr();
1.227 @@ -449,7 +551,7 @@
1.228 void AddHookManager(Plugin plug, const CHookManagerInfo &hookman);
1.229 };
1.230
1.231 - class CCallClassImpl : public GenericCallClass
1.232 + class CCallClassImpl : public DeprecatedCallClass<void>
1.233 {
1.234 public:
1.235
1.236 @@ -523,6 +625,10 @@
1.237 void SetPluginPaused(Plugin plug, bool paused);
1.238
1.239 HookLoopInfoStack m_HLIStack;
1.240 + CHookIDManager m_HookIDMan;
1.241 +
1.242 + void *m_OneIgnore; //:TODO:
1.243 + bool m_IgnoreActive;
1.244 public:
1.245 CSourceHookImpl();
1.246 virtual ~CSourceHookImpl();
1.247 @@ -611,14 +717,14 @@
1.248 * @param iface The interface pointer
1.249 * @param size Size of the class instance
1.250 */
1.251 - GenericCallClass *GetCallClass(void *iface, size_t size);
1.252 + DeprecatedCallClass<void> *GetCallClass(void *iface, size_t size);
1.253
1.254 /**
1.255 * @brief Release a callclass
1.256 *
1.257 * @param ptr Pointer to the callclass
1.258 */
1.259 - virtual void ReleaseCallClass(GenericCallClass *ptr);
1.260 + virtual void ReleaseCallClass(DeprecatedCallClass<void> *ptr);
1.261
1.262 virtual void SetRes(META_RES res); //!< Sets the meta result
1.263 virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
1.264 @@ -653,6 +759,58 @@
1.265
1.266 virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
1.267 void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr);
1.268 +
1.269 + /**
1.270 + * @brief Add a (VP) hook.
1.271 + *
1.272 + * @return non-zero hook id on success, 0 otherwise
1.273 + *
1.274 + * @param plug The unique identifier of the plugin that calls this function
1.275 + * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
1.276 + * @param iface The interface pointer
1.277 + * The representative interface pointer for VP hooks
1.278 + * The vtable pointer for direct VP hooks !!!
1.279 + * @param ifacesize The size of the class iface points to
1.280 + * @param myHookMan A hook manager function that should be capable of handling the function
1.281 + * @param handler A pointer to a FastDelegate containing the hook handler
1.282 + * @param post Set to true if you want a post handler
1.283 + */
1.284 + virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
1.285 + ISHDelegate *handler, bool post);
1.286 +
1.287 + /**
1.288 + * @brief Remove a VP hook by ID.
1.289 + *
1.290 + * @return true on success, false otherwise
1.291 + *
1.292 + * @param plug The unique identifier of the plugin that calls this function
1.293 + * @param hookid The hook id (returned by AddHookNew)
1.294 + */
1.295 + virtual bool RemoveHookByID(Plugin plug, int hookid);
1.296 +
1.297 + /**
1.298 + * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
1.299 + *
1.300 + * @param plug The unique identifier of the plugin that calls this function
1.301 + * @param vfnptr The virtual function pointer of the function in question
1.302 + */
1.303 + virtual void SetIgnoreHooks(Plugin plug, void *vfnptr);
1.304 +
1.305 + /**
1.306 + * @brief Reverses SetIgnoreHooks' effect
1.307 + *
1.308 + * @param plug The unique identifier of the plugin that calls this function
1.309 + * @param vfnptr The virtual function pointer of the function in question
1.310 + */
1.311 + virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr);
1.312 +
1.313 + /**
1.314 + * @brief Finds the original entry of a virtual function pointer
1.315 + *
1.316 + * @param vfnptr The virtual function pointer
1.317 + * @return The original entry if the virtual function pointer has been patched; NULL otherwise.
1.318 + */
1.319 + virtual void *GetOrigVfnPtrEntry(void *vfnptr);
1.320 };
1.321 }
1.322