diff -r e8a3f2617a3e -r f9e21a2ac558 sourcehook/sourcehook_impl.h --- a/sourcehook/sourcehook_impl.h Sun Oct 07 03:12:19 2007 +0000 +++ b/sourcehook/sourcehook_impl.h Sun Oct 07 22:10:07 2007 +0000 @@ -23,6 +23,8 @@ IMPLEMENTATION INFO --------------------------------------- + :TODO: update ??? + Protos ("Prototypes") The purpose of protos is to provide the amount of type information about a function which is required to be able to execute a function call without corrupting the stack. @@ -170,648 +172,168 @@ the original function. Everything works fine. This works even for VP hooks. */ +#include "sourcehook_impl_cproto.h" +#include "sourcehook_impl_chook.h" +#include "sourcehook_impl_ciface.h" +#include "sourcehook_impl_cvfnptr.h" +#include "sourcehook_impl_chookmaninfo.h" +#include "sourcehook_impl_chookmancont.h" +#include "sourcehook_impl_chookidman.h" + namespace SourceHook { /** - * @brief The SourceHook implementation class + * @brief The SourceHook implementation */ - class CSourceHookImpl : public ISourceHook - { - private: - class CProto - { - char *m_Proto; - - static bool Equal(const char *p1, const char *p2); - char *DupProto(const char *src); - void FreeProto(char *prot); - public: - CProto() : m_Proto(NULL) - { - } - - CProto(const char *szProto) : m_Proto(DupProto(szProto)) - { - } - - CProto(const CProto &other) : m_Proto(DupProto(other.m_Proto)) - { - } - - ~CProto() - { - FreeProto(m_Proto); - m_Proto = NULL; - } - - void operator = (const char *szProto) - { - m_Proto = DupProto(szProto); - } - - void operator = (const CProto &other) - { - m_Proto = DupProto(other.m_Proto); - } - bool operator == (const char *szProto) const - { - return Equal(szProto, m_Proto); - } - bool operator == (const CProto &other) const - { - return Equal(other.m_Proto, m_Proto); - } - - const char *GetProto() const - { - return m_Proto; - } - }; - + namespace Impl + { - /** - * @brief A hook can be removed if you have this information - */ - struct RemoveHookInfo - { - RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman, - ISHDelegate *phandler, bool ppost) - : plug(pplug), iface(piface), thisptr_offs(tpo), - hookman(phookman), handler(phandler), post(ppost) - { - } - - Plugin plug; - void *iface; - int thisptr_offs; - HookManagerPubFunc hookman; - ISHDelegate *handler; - bool post; - }; - - struct RemoveHookManInfo - { - RemoveHookManInfo(Plugin pplug, HookManagerPubFunc phookman) - : plug(pplug), hookman(phookman) - { - } - - Plugin plug; - HookManagerPubFunc hookman; - }; - - // Associates hook ids with info about the hooks - // Also used to keep track of used hook ids - class CHookIDManager + class CHookManContainerList : public List { 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); + CHookManagerContainer &GetContainer(int vtbloffs, int vtblidx, const CProto &proto); + void RemoveHookMans(Plugin plug); + void RemoveHookMans(Plugin plug, HookManagerPubFunc pubFunc); }; - struct HookInfo + struct CHookContext : IHookContext { - 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; - - class CIter : public IHookList::IIter + enum State { - friend class CHookList; - - CHookList *m_pList; - - void SkipPaused(); - public: - - ListCatIterator m_Iter; - - CIter(CHookList *pList); - - virtual ~CIter(); + State_Born, + State_Pre, + State_PreVP, + State_Post, + State_PostVP, + State_OrigCall, + State_Dead, - void GoToBegin(); - void Set(CIter *pOther); - - bool End(); - void Next(); - ISHDelegate *Handler(); - int ThisPtrOffs(); - - void Clear(); - - CIter *m_pNext; // When stored in m_FreeIters and m_UsedIters - CIter *m_pPrev; // Only used when stored in m_UsedIters + // Special + State_Ignore, + State_Recall_Pre, + State_Recall_PreVP, + State_Recall_Post, + State_Recall_PostVP }; - CIter *m_FreeIters; - CIter *m_UsedIters; // The last returned and not-yet-released iter is always m_UsedIters - - // For recalls - bool m_Recall; - bool m_RQFlag; - - void SetRecallState(); // Sets the list into a state where the next returned - // iterator (from GetIter) will be a copy of the last - // returned iterator, incremented by one. This is used in Recalls. - // The hook resets this state automatically on: - // GetIter, ReleaseIter - - void RQFlagReset() { m_RQFlag = false; } - bool RQFlagGet() { return m_RQFlag; } - CHookList(); - CHookList(const CHookList &other); - virtual ~CHookList(); - - void operator=(const CHookList &other); - - IIter *GetIter(); - void ReleaseIter(IIter *pIter); - - void SetVPList(List *newList); - void ClearVPList(); - }; - - // I know, data hiding... But I'm a lazy bastard! - - class CIface : public IIface - { - public: - void *m_Ptr; - CHookList m_PreHooks; - CHookList m_PostHooks; - public: - CIface(void *ptr); - virtual ~CIface(); - - void *GetPtr(); - IHookList *GetPreHooks(); - IHookList *GetPostHooks(); - - bool operator==(void *ptr) - { - return m_Ptr == ptr; - } - bool operator!=(void *ptr) - { - return m_Ptr != ptr; - } - }; - - class CVfnPtr : public IVfnPtr - { - public: - typedef List IfaceList; - typedef IfaceList::iterator IfaceListIter; - - void *m_Ptr; - void *m_OrigEntry; - - IfaceList m_Ifaces; - - void **m_pOneIgnore; - public: - CVfnPtr(void *ptr, void **pOneIgnore); - virtual ~CVfnPtr(); - - void *GetVfnPtr(); - void *GetOrigEntry(); - - virtual IIface *FindIface(void *ptr); - - bool operator==(void *ptr) - { - return m_Ptr == ptr; - } - }; - - class CHookManagerInfo : public IHookManagerInfo - { - public: - typedef List VfnPtrList; - typedef VfnPtrList::iterator VfnPtrListIter; - - Plugin m_Plug; - HookManagerPubFunc m_Func; - - int m_VtblOffs; - int m_VtblIdx; - const char *m_Proto; - void *m_HookfuncVfnptr; - - VfnPtrList m_VfnPtrs; - - int m_HookManVersion; - public: - CHookManagerInfo(); - virtual ~CHookManagerInfo(); - - IVfnPtr *FindVfnPtr(void *vfnptr); - - void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto); - void SetHookfuncVfnptr(void *hookfunc_vfnptr); - - void SetVersion(int version); - - bool operator < (const CHookManagerInfo &other) - { - return m_HookManVersion < other.m_HookManVersion; - } + int m_State; + List::iterator m_Iter; - struct Descriptor - { - Descriptor(Plugin pplug, HookManagerPubFunc ppubFunc) : plug(pplug), pubFunc(ppubFunc) - { - } - - Plugin plug; - HookManagerPubFunc pubFunc; - }; - - bool operator == (const Descriptor desc) - { - return m_Func == desc.pubFunc && m_Plug == desc.plug; - } - }; - - typedef List HookManInfoList; - - class CHookManagerContainer : public HookManInfoList - { - public: - // HMCI (Hook Manager Container Identification) - class HMCI - { - CProto m_Proto; - int m_VtableOffset; - int m_VtableIndex; - public: - HMCI(const char *proto, int vtbloffs, int vtblidx) : - m_Proto(proto), m_VtableOffset(vtbloffs), m_VtableIndex(vtblidx) - { - } - ~HMCI() - { - } - - bool operator==(const HMCI &other) const - { - return - other.m_VtableIndex == m_VtableIndex && - other.m_Proto == m_Proto && - other.m_VtableOffset == m_VtableOffset; - } - - const CProto &GetProto() const - { - return m_Proto; - } - int GetVtableOffset() const - { - return m_VtableOffset; - } - int GetVtableIndex() const - { - return m_VtableIndex; - } - }; - HMCI m_HCMI; - - public: - CHookManagerContainer(const HMCI &hmci) : m_HCMI(hmci) - { - } - bool operator == (const HMCI &other) const - { - return m_HCMI == other; - } - void AddHookManager(Plugin plug, const CHookManagerInfo &hookman); - }; - - class CCallClassImpl : public DeprecatedCallClass - { - public: - - typedef SourceHook::CVector OrigFuncs; - typedef SourceHook::THash OrigVTables; - - void *m_Ptr; //!< Pointer to the actual object - size_t m_ObjSize; //!< Size of the instance - OrigVTables m_VT; //!< Info about vtables & functions - - int m_RefCounter; - - CCallClassImpl(void *ptr, size_t size); - virtual ~CCallClassImpl(); - - bool operator==(void *other) - { - return m_Ptr == other; - } - - void *GetThisPtr(); - void *GetOrigFunc(int vtbloffs, int vtblidx); - - void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry); - void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx); - }; - - /** - * @brief A list of CallClass structures - */ - typedef List Impl_CallClassList; - - Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses - - /** - * @brief A list of CHookManagerContainers - */ - typedef List HookManContList; - - HookManContList m_HookMans; //!< A list of hook managers - - struct HookLoopInfo - { - enum RecallType - { - Recall_No=0, - Recall_Pre, - Recall_Post1, - Recall_Post2 - }; - + CVfnPtr *pVfnPtr; + CIface *pIface; + META_RES *pStatus; META_RES *pPrevRes; META_RES *pCurRes; - META_RES temporaryStatus; //!< Stored during Post1 recall phase - bool shouldContinue; - RecallType recall; //!< Specifies which kind of recall we're in. - - IIface *pCurIface; + void *pThisPtr; const void *pOrigRet; void *pOverrideRet; - void **pIfacePtrPtr; - }; - typedef CStack HookLoopInfoStack; - - void ApplyCallClassPatches(CCallClassImpl &cc); - void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry); - void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx); + void *pIfacePtr; - void SetPluginPaused(Plugin plug, bool paused); - - HookLoopInfoStack m_HLIStack; - CHookIDManager m_HookIDMan; - - void *m_OneIgnore; //:TODO: - bool m_IgnoreActive; - public: - CSourceHookImpl(); - virtual ~CSourceHookImpl(); + bool m_CallOrig; - /** - * @brief Returns the interface version - */ - int GetIfaceVersion(); - - /** - * @brief Returns the implemnetation version - */ - int GetImplVersion(); + void SkipPaused(List::iterator &iter, List &list) + { + while (iter != list.end() && iter->IsPaused()) + ++iter; + } + public: + void HookRemoved(List::iterator oldhookiter, List::iterator nexthookiter); + void IfaceRemoved(CIface *iface); + void VfnPtrRemoved(CVfnPtr *vfnptr); - /** - * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers - */ - void UnloadPlugin(Plugin plug); + ISHDelegate *GetNext(); + void *GetOverrideRetPtr(); + const void *GetOrigRetPtr(); + bool ShouldCallOrig(); + }; - /** - * @brief Shut down the whole system, unregister all hook managers - */ - void CompleteShutdown(); + typedef CStack HookContextStack; - /** - * @brief Add a hook. - * - * @return True if the function succeeded, false otherwise - * - * @param plug The unique identifier of the plugin that calls this function - * @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 - */ - bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post); + class CSourceHookImpl : public ISourceHook + { + private: + CHookManContainerList m_HookManContainers; + CHookIDManager m_HookIDMan; + HookContextStack m_ContextStack; - /** - * @brief Removes a hook. - * - * @return True if the function succeeded, false otherwise - * - * @param plug The unique identifier of the plugin that calls this function - * @param iface The interface pointer - * @param thisptr_offs This pointer adjuster - * @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 - */ - bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post); + bool SetHookPaused(int hookid, bool paused); + public: + CSourceHookImpl(); + virtual ~CSourceHookImpl(); - /** - * @brief Removes a hook. - * - * @ return True if the function succeeded, false otherwise - * - * @param info A RemoveHookInfo structure, describing the hook - */ - bool RemoveHook(RemoveHookInfo info); + /** + * @brief Returns the interface version + */ + int GetIfaceVersion(); - /** - * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins - * - * @param plug The unique identifier of the plugin in question - */ - bool IsPluginInUse(Plugin plug); + /** + * @brief Returns the implemnetation version + */ + int GetImplVersion(); - /** - * @brief Pauses all hooks of a plugin - * - * @param plug The unique identifier of the plugin - */ - void PausePlugin(Plugin plug); + int AddHook(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, + ISHDelegate *handler, bool post); - /** - * @brief Unpauses all hooks of a plugin - * - * @param plug The unique identifier of the plugin - */ - void UnpausePlugin(Plugin plug); + bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, + ISHDelegate *handler, bool post); + + bool RemoveHookByID(int hookid); - /** - * @brief Return a pointer to a callclass. Generate a new one if required. - * - * @param iface The interface pointer - * @param size Size of the class instance - */ - DeprecatedCallClass *GetCallClass(void *iface, size_t size); - - /** - * @brief Release a callclass - * - * @param ptr Pointer to the callclass - */ - virtual void ReleaseCallClass(DeprecatedCallClass *ptr); - - virtual void SetRes(META_RES res); //!< Sets the meta result - virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler - virtual META_RES GetStatus(); //!< Gets the highest meta result - virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined - virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL - virtual void *GetIfacePtr(); //!< Gets the interface pointer + bool PauseHookByID(int hookid); + bool UnpauseHookByID(int hookid); - ////////////////////////////////////////////////////////////////////////// - // For hook managers - void HookLoopBegin(IIface *pIface); //!< Should be called when a hook loop begins - void HookLoopEnd(); //!< Should be called when a hook loop exits - void SetCurResPtr(META_RES *mres); //!< Sets pointer to the current meta result - void SetPrevResPtr(META_RES *mres); //!< Sets pointer to previous meta result - void SetStatusPtr(META_RES *mres); //!< Sets pointer to the status variable - void SetIfacePtrPtr(void **pp); //!< Sets pointer to the interface this pointer - void SetOrigRetPtr(const void *ptr); //!< Sets the original return pointer - void SetOverrideRetPtr(void *ptr); //!< Sets the override result pointer - bool ShouldContinue(); //!< Returns false if the hook loop should exit + void SetRes(META_RES res); //!< Sets the meta result + META_RES GetPrevRes(); //!< Gets the meta result of the + //!< previously calledhandler + META_RES GetStatus(); //!< Gets the highest meta result + const void *GetOrigRet(); //!< Gets the original result. + //!< If not in post function, undefined + const void *GetOverrideRet(); //!< Gets the override result. + //!< If none is specified, NULL + void *GetIfacePtr(); //!< Gets the interface pointer - /** - * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug. - * - * @param plug The owner of the hook manager - * @param pubFunc The hook manager's info function - */ - virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc); - virtual void RemoveHookManager(RemoveHookManInfo info); + void *GetOverrideRetPtr(); //!< Used for setting the override return value - virtual void DoRecall(); //!< Initiates a recall sequence - virtual void *GetOverrideRetPtr(); //!< Returns the pointer set by SetOverrideRetPtr + /** + * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers + */ + void UnloadPlugin(Plugin plug); - virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr, - void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr); + void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc); + + void SetIgnoreHooks(void *vfnptr); + void ResetIgnoreHooks(void *vfnptr); - /** - * @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 - * The representative interface pointer for VP hooks - * The vtable pointer for direct VP hooks !!! - * @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); + void DoRecall(); + + IHookContext *SetupHookLoop(IHookManagerInfo *hi, void *vfnptr, void *thisptr, void **origentry, META_RES *statusPtr, + META_RES *prevResPtr, META_RES *curResPtr, const void *origRetPtr, void *overrideRetPtr); + + void EndContext(IHookContext *pCtx); + + /** + * @brief Shut down the whole system, unregister all hook managers + */ + void CompleteShutdown(); - /** - * @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); + /** + * @brief Pauses all hooks of a plugin + * + * @param plug The unique identifier of the plugin + */ + void PausePlugin(Plugin plug); - /** - * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr - * - * @param plug The unique identifier of the plugin that calls this function - * @param vfnptr The virtual function pointer of the function in question - */ - virtual void SetIgnoreHooks(Plugin plug, void *vfnptr); - - /** - * @brief Reverses SetIgnoreHooks' effect - * - * @param plug The unique identifier of the plugin that calls this function - * @param vfnptr The virtual function pointer of the function in question - */ - virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr); - - /** - * @brief Finds the original entry of a virtual function pointer - * - * @param vfnptr The virtual function pointer - * @return The original entry if the virtual function pointer has been patched; NULL otherwise. - */ - virtual void *GetOrigVfnPtrEntry(void *vfnptr); - }; + /** + * @brief Unpauses all hooks of a plugin + * + * @param plug The unique identifier of the plugin + */ + void UnpausePlugin(Plugin plug); + }; + } } #endif