diff -r 7dcb118abe09 -r 257f0837a885 sourcehook/sourcehook_impl.h --- a/sourcehook/sourcehook_impl.h Mon May 07 18:33:34 2007 +0000 +++ b/sourcehook/sourcehook_impl.h Mon May 07 18:35:59 2007 +0000 @@ -16,6 +16,7 @@ #include "sh_vector.h" #include "sh_tinyhash.h" #include "sh_stack.h" +#include "sh_listcat.h" /* @@ -142,6 +143,15 @@ HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy. All this stuff could be much less complicated if I didn't try to preserve binary compatibility :) + +VP Hooks + VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are + implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which + virtually concatenates the NULL-interface-hook-list with their normal hook list. + + + I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together + binary compatible beast which is pretty hard to maintain unless you've written it :) */ namespace SourceHook @@ -160,6 +170,10 @@ char *DupProto(const char *src); void FreeProto(char *prot); public: + CProto() : m_Proto(NULL) + { + } + CProto(const char *szProto) : m_Proto(DupProto(szProto)) { } @@ -192,6 +206,11 @@ { return Equal(other.m_Proto, m_Proto); } + + const char *GetProto() const + { + return m_Proto; + } }; @@ -226,17 +245,79 @@ HookManagerPubFunc hookman; }; + // Associates hook ids with info about the hooks + // Also used to keep track of used hook ids + class CHookIDManager + { + public: + struct Entry + { + bool isfree; + + // hookman info + CProto proto; + int vtbl_offs; + int vtbl_idx; + + // vfnptr + void *vfnptr; + + // iface + void* adjustediface; + + // hook + Plugin plug; + int thisptr_offs; + ISHDelegate *handler; + bool post; + + Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto, + ISHDelegate *ph, bool ppost) + : isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp), + adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost) + { + } + Entry() + { + } + }; + private: + // Internally, hookid 1 is stored as m_Entries[0] + + CVector m_Entries; + public: + CHookIDManager(); + int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface, + Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post); + bool Remove(int hookid); + const Entry * QueryHook(int hookid); + + // Finds all hooks with the given info, and fills the hookids into output. + void FindAllHooks(CVector &output, const CProto &proto, int vtbl_offs, int vtbl_idx, + void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post); + + // Removes all hooks with a specified vfnptr + bool RemoveAll(void *vfnptr); + }; + struct HookInfo { ISHDelegate *handler; //!< Pointer to the handler bool paused; //!< If true, the hook should not be executed Plugin plug; //!< The owner plugin int thisptr_offs; //!< This pointer offset + int hookid; //!< Unique ID given by CHookIDManager + + bool operator==(int otherid) + { + return hookid == otherid; + } }; class CHookList : public IHookList { public: + List *m_VPList; // left-hand list for ListCatIterator -> for VP hooks List m_List; friend class CIter; @@ -250,7 +331,7 @@ void SkipPaused(); public: - List::iterator m_Iter; + ListCatIterator m_Iter; CIter(CHookList *pList); @@ -293,6 +374,9 @@ IIter *GetIter(); void ReleaseIter(IIter *pIter); + + void SetVPList(List *newList); + void ClearVPList(); }; // I know, data hiding... But I'm a lazy bastard! @@ -315,6 +399,10 @@ { return m_Ptr == ptr; } + bool operator!=(void *ptr) + { + return m_Ptr != ptr; + } }; class CVfnPtr : public IVfnPtr @@ -520,6 +608,7 @@ void SetPluginPaused(Plugin plug, bool paused); HookLoopInfoStack m_HLIStack; + CHookIDManager m_HookIDMan; public: CSourceHookImpl(); virtual ~CSourceHookImpl(); @@ -650,6 +739,32 @@ virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr, void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr); + + /** + * @brief Add a (VP) hook. + * + * @return non-zero hook id on success, 0 otherwise + * + * @param plug The unique identifier of the plugin that calls this function + * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook) + * @param iface The interface pointer + * @param ifacesize The size of the class iface points to + * @param myHookMan A hook manager function that should be capable of handling the function + * @param handler A pointer to a FastDelegate containing the hook handler + * @param post Set to true if you want a post handler + */ + virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, + ISHDelegate *handler, bool post); + + /** + * @brief Remove a VP hook by ID. + * + * @return true on success, false otherwise + * + * @param plug The unique identifier of the plugin that calls this function + * @param hookid The hook id (returned by AddHookNew) + */ + virtual bool RemoveHookByID(Plugin plug, int hookid); }; }