dvander@541: /* ======== SourceHook ======== dvander@659: * Copyright (C) 2004-2007 Metamod:Source Development Team dvander@541: * No warranties of any kind dvander@541: * dvander@541: * License: zlib/libpng dvander@541: * dvander@541: * Author(s): Pavol "PM OnoTo" Marko dvander@541: * ============================ dvander@541: */ dvander@541: dvander@541: #ifndef __SOURCEHOOK_IMPL_H__ dvander@541: #define __SOURCEHOOK_IMPL_H__ dvander@541: dvander@541: #include "sourcehook.h" dvander@541: #include "sh_list.h" dvander@541: #include "sh_vector.h" dvander@541: #include "sh_tinyhash.h" dvander@541: #include "sh_stack.h" dvander@659: #include "sh_listcat.h" dvander@541: dvander@541: /* dvander@541: dvander@541: IMPLEMENTATION INFO dvander@541: dvander@541: --------------------------------------- dvander@541: Protos ("Prototypes") dvander@541: The purpose of protos is to provide the amount of type information about a function dvander@541: which is required to be able to execute a function call without corrupting the stack. dvander@541: Our protos do not fully do this, but they provide the size of the return value, the number of dvander@541: parameters, and the size of each parameter, which is enough for most situations. dvander@541: dvander@541: There are two version of protos: dvander@541: OLD: dvander@541: C-Style strings. dvander@541: dvander@541: 0_void: dvander@541: "attrib" dvander@541: 1_void: dvander@541: "attrib|param1_type" dvander@541: 2_void: dvander@541: "attrib|param1_type|param2_type dvander@541: 0: dvander@541: "attrib|ret_type" dvander@541: 1: dvander@541: "attrib|ret_type|param1_type" dvander@541: 2: dvander@541: "attrib|ret_type|param2_type" dvander@541: dvander@541: Old protos are deprecated. dvander@541: dvander@541: NEW: dvander@541: New protos are in fact pointers to the ProtoInfo structure (see sourcehook.h) dvander@541: dvander@541: Old protos begin with a non-zero byte, new protos begin with a zero byte. dvander@541: dvander@541: Protos are usually stored in a CProto instance. dvander@541: dvander@541: --------------------------------------- dvander@541: Hook managers and hook manager containers dvander@541: Each hookman container is tied to _one_ proto/vtable index/vtable offset info. dvander@541: Hookman containers then contain a list of hook managers provided by plugins, sorted by version. dvander@541: (higher versions come first) dvander@541: dvander@541: Duplicate hook managers are ignored (ie. hook managers where proto, vtable index, vtable offset, dvander@541: plugin, version are the same as in an already exisiting hook manager) dvander@541: dvander@541: A new hook manager is always added to the end of the version group in the corresponding dvander@541: hook container. dvander@541: dvander@541: If the new hook manager was added to the beginning of the container (which only happens if dvander@541: it is the first one or if it has a higher version than the previously first hook manager), dvander@541: the now second hook manager is shut down and the new hook manager takes its job. dvander@541: dvander@541: A "hook manager container id" (HMCI) consits of three values: proto, vtbl index, vtbl offset. dvander@541: --------------------------------------- dvander@541: Hooks dvander@541: When adding a hook, first the proposed hook manager is added to the corresponding dvander@541: hook manager container as described above. dvander@541: dvander@541: Then the first hook manager in the the manhook container is chosen to handle the function. dvander@541: dvander@541: Removing a hook does not neccessarily unreigster the plugin's hook manager. In order to do this, dvander@541: use RemoveHookManager or UnloadPlugin/ dvander@541: dvander@541: Hooks can be paused - they remain in memory but they are not called. In SH, the hook iterator dvander@541: classes handle pausing transparently. dvander@541: dvander@541: The hook loop is supposed to call ShouldContinue before each iteration. This makes hook handlers dvander@541: able to remove themselves. dvander@541: dvander@541: --------------------------------------- dvander@541: Call classes dvander@659: dvander@659: !! deprecated !! - see below (new SH_CALL) dvander@659: dvander@541: Call classes are identified by a this pointer and an instance size dvander@541: dvander@541: We use the instance size because a derived class instance and a base class instance could dvander@541: have the same this pointers, and we want to avoid that the derived class dvander@541: (which could be bigger) gets the same callclass as the base class (mainly if the dvander@541: base class' callclass was requested first). dvander@541: dvander@541: Call classes are reference counted. dvander@541: dvander@541: The original function pointers are stored in a vector (in order to allow fast random access). dvander@541: These vectors are stored as the value type of a hash. The key type is int and represents the dvander@541: vtable offset. dvander@541: dvander@541: If the hash key doesn't exist or the vtblidx is out of range or the corresponding element in the dvander@541: vector is NULL, there is no hook on that function. dvander@541: dvander@541: --------------------------------------- dvander@541: Recalls dvander@541: Recalls are used for the META_RETURN_(VALUE_)NEWPARAMS macros, ie. to change the parameters dvander@541: in the hook loop on the fly. dvander@541: dvander@541: First, the macro calls DoRecall(), then it calls the function the hook is attached to -> it dvander@541: calls the hookfunc. SourceHook makes sure that the newly invoked hook loop starts where the last dvander@541: one left off and that status variables like status, previous result, override return are kept. dvander@541: When this recurisvely called hookfunc returns, the macro returns what it returned dvander@541: (using MRES_SUPERCEDE). CSourceHookImpl returns false from ShouldContinue so the original hook loop dvander@541: is abandonned. dvander@541: dvander@541: Post Recalls dvander@541: People wanted to be able to use META_RETURN_(VALUE_)NEWPARAMS from post hooks as well. Crazy people! dvander@541: Anyway, for this, we have to know where a hook handler is. Is it executing pre or post hooks at the moment? dvander@541: The only way we can know this is watching when it calls CHookList::GetIter(). So CHookList gets a new variable: dvander@541: m_RequestedFlag. It also gets two new functions: RQFlagReset() and RQFlagGet(). dvander@541: HookLoopBegin() calls RQFlagReset on both hooklists of the iface; then DoRecall() checks whether the postlist's dvander@541: RQ flag is set. if yes, the hook loop is in post mode. dvander@541: dvander@541: So, what a about a recall in post mode? The first ShouldContinue returns false and sets Status to supercede. dvander@541: This way the pre hooks and the function call will be skipped. Then, then next ShouldContinue returns true, so we get dvander@541: into the post hooks. HA! dvander@541: dvander@541: Return Values in Post Recalls dvander@541: The easy case is when we already have an override return value. In this case, the status register gets transferred, dvander@541: and so does the override return pointer. So, basically, everything is ok. dvander@541: dvander@541: However, what happens if we don't? ie. the status register is on MRES_IGNORED? In this case we'd have to transfer the dvander@541: orig ret value. But we can't: There's no way to tell the hookfunc: "Use this as orig ret pointer". It uses its own. dvander@541: So, we emulate it. GetOrigRet will return the orig ret pointer from the old hook loop. If still no one overrides it, dvander@541: we'd have to return it. BUT! HOW TO DO THIS? Check out SH_RETURN(). First calls HookLoopEnd(), then decides whether dvander@541: to return the override retval or the orig retval. But it doesn't ask for a new override return. So we give the function dvander@541: the last orig return value as its new override return value; but leave status where it is so everything works, and in dvander@541: HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy. dvander@541: dvander@541: All this stuff could be much less complicated if I didn't try to preserve binary compatibility :) dvander@659: dvander@659: VP Hooks dvander@659: VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are dvander@659: implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which dvander@659: virtually concatenates the NULL-interface-hook-list with their normal hook list. dvander@659: dvander@659: dvander@659: I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together dvander@659: binary compatible beast which is pretty hard to maintain unless you've written it :) dvander@659: dvander@659: New SH_CALL dvander@659: The addition of VP hooks messed up the Call Classes concept (see above) - call classes are bound to an dvander@659: instance pointer; they only work on one of the hooked instances. But VP hooks are called on all instances. dvander@659: dvander@659: That's why now, SH_CALL takes an instance pointer instead of a callclass pointer. It basically does this: dvander@659: 1) call SH_GLOB_PTR->SetIgnoreHooks(vfnptr) dvander@659: 2) call this->*mfp dvander@659: 3) call SH_GLOB_PTR->ResetIgnoreHooks(vfnptr) dvander@659: dvander@659: SourceHook stroes the "ignored vfnptr" and makes CVfnPtr::FindIface return NULL if the CVfnPtr instance dvander@659: corresponds to the ignored vfnptr. This way the hook manager thinks that the instance isn't hooked, and calls dvander@659: the original function. Everything works fine. This works even for VP hooks. dvander@541: */ dvander@541: dvander@541: namespace SourceHook dvander@541: { dvander@541: /** dvander@541: * @brief The SourceHook implementation class dvander@541: */ dvander@541: class CSourceHookImpl : public ISourceHook dvander@541: { dvander@541: private: dvander@541: class CProto dvander@541: { dvander@541: char *m_Proto; dvander@541: dvander@541: static bool Equal(const char *p1, const char *p2); dvander@541: char *DupProto(const char *src); dvander@541: void FreeProto(char *prot); dvander@541: public: dvander@659: CProto() : m_Proto(NULL) dvander@659: { dvander@659: } dvander@659: dvander@541: CProto(const char *szProto) : m_Proto(DupProto(szProto)) dvander@541: { dvander@541: } dvander@541: dvander@541: CProto(const CProto &other) : m_Proto(DupProto(other.m_Proto)) dvander@541: { dvander@541: } dvander@541: dvander@541: ~CProto() dvander@541: { dvander@541: FreeProto(m_Proto); dvander@541: m_Proto = NULL; dvander@541: } dvander@541: dvander@541: void operator = (const char *szProto) dvander@541: { dvander@541: m_Proto = DupProto(szProto); dvander@541: } dvander@541: dvander@541: void operator = (const CProto &other) dvander@541: { dvander@541: m_Proto = DupProto(other.m_Proto); dvander@541: } dvander@541: dvander@541: bool operator == (const char *szProto) const dvander@541: { dvander@541: return Equal(szProto, m_Proto); dvander@541: } dvander@541: bool operator == (const CProto &other) const dvander@541: { dvander@541: return Equal(other.m_Proto, m_Proto); dvander@541: } dvander@659: dvander@659: const char *GetProto() const dvander@659: { dvander@659: return m_Proto; dvander@659: } dvander@541: }; dvander@541: dvander@541: dvander@541: /** dvander@541: * @brief A hook can be removed if you have this information dvander@541: */ dvander@541: struct RemoveHookInfo dvander@541: { dvander@541: RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman, dvander@541: ISHDelegate *phandler, bool ppost) dvander@541: : plug(pplug), iface(piface), thisptr_offs(tpo), dvander@541: hookman(phookman), handler(phandler), post(ppost) dvander@541: { dvander@541: } dvander@541: dvander@541: Plugin plug; dvander@541: void *iface; dvander@541: int thisptr_offs; dvander@541: HookManagerPubFunc hookman; dvander@541: ISHDelegate *handler; dvander@541: bool post; dvander@541: }; dvander@541: dvander@541: struct RemoveHookManInfo dvander@541: { dvander@541: RemoveHookManInfo(Plugin pplug, HookManagerPubFunc phookman) dvander@541: : plug(pplug), hookman(phookman) dvander@541: { dvander@541: } dvander@541: dvander@541: Plugin plug; dvander@541: HookManagerPubFunc hookman; dvander@541: }; dvander@541: dvander@659: // Associates hook ids with info about the hooks dvander@659: // Also used to keep track of used hook ids dvander@659: class CHookIDManager dvander@659: { dvander@659: public: dvander@659: struct Entry dvander@659: { dvander@659: bool isfree; dvander@659: dvander@659: // hookman info dvander@659: CProto proto; dvander@659: int vtbl_offs; dvander@659: int vtbl_idx; dvander@659: dvander@659: // vfnptr dvander@659: void *vfnptr; dvander@659: dvander@659: // iface dvander@659: void* adjustediface; dvander@659: dvander@659: // hook dvander@659: Plugin plug; dvander@659: int thisptr_offs; dvander@659: ISHDelegate *handler; dvander@659: bool post; dvander@659: dvander@659: Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto, dvander@659: ISHDelegate *ph, bool ppost) dvander@659: : isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp), dvander@659: adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost) dvander@659: { dvander@659: } dvander@659: Entry() dvander@659: { dvander@659: } dvander@659: }; dvander@659: private: dvander@659: // Internally, hookid 1 is stored as m_Entries[0] dvander@659: dvander@659: CVector m_Entries; dvander@659: public: dvander@659: CHookIDManager(); dvander@659: int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface, dvander@659: Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post); dvander@659: bool Remove(int hookid); dvander@659: const Entry * QueryHook(int hookid); dvander@659: dvander@659: // Finds all hooks with the given info, and fills the hookids into output. dvander@659: void FindAllHooks(CVector &output, const CProto &proto, int vtbl_offs, int vtbl_idx, dvander@659: void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post); dvander@659: dvander@659: // Removes all hooks with a specified vfnptr dvander@659: bool RemoveAll(void *vfnptr); dvander@659: }; dvander@659: dvander@541: struct HookInfo dvander@541: { dvander@541: ISHDelegate *handler; //!< Pointer to the handler dvander@541: bool paused; //!< If true, the hook should not be executed dvander@541: Plugin plug; //!< The owner plugin dvander@541: int thisptr_offs; //!< This pointer offset dvander@659: int hookid; //!< Unique ID given by CHookIDManager dvander@659: dvander@659: bool operator==(int otherid) dvander@659: { dvander@659: return hookid == otherid; dvander@659: } dvander@541: }; dvander@541: dvander@541: class CHookList : public IHookList dvander@541: { dvander@541: public: dvander@659: List *m_VPList; // left-hand list for ListCatIterator -> for VP hooks dvander@541: List m_List; dvander@541: dvander@541: friend class CIter; dvander@541: dvander@541: class CIter : public IHookList::IIter dvander@541: { dvander@541: friend class CHookList; dvander@541: dvander@541: CHookList *m_pList; dvander@541: dvander@541: void SkipPaused(); dvander@541: public: dvander@541: dvander@659: ListCatIterator m_Iter; dvander@541: dvander@541: CIter(CHookList *pList); dvander@541: dvander@541: virtual ~CIter(); dvander@541: dvander@541: void GoToBegin(); dvander@541: void Set(CIter *pOther); dvander@541: dvander@541: bool End(); dvander@541: void Next(); dvander@541: ISHDelegate *Handler(); dvander@541: int ThisPtrOffs(); dvander@541: dvander@541: void Clear(); dvander@541: dvander@541: CIter *m_pNext; // When stored in m_FreeIters and m_UsedIters dvander@541: CIter *m_pPrev; // Only used when stored in m_UsedIters dvander@541: }; dvander@541: dvander@541: CIter *m_FreeIters; dvander@541: CIter *m_UsedIters; // The last returned and not-yet-released iter is always m_UsedIters dvander@541: dvander@541: // For recalls dvander@541: bool m_Recall; dvander@541: bool m_RQFlag; dvander@541: dvander@541: void SetRecallState(); // Sets the list into a state where the next returned dvander@541: // iterator (from GetIter) will be a copy of the last dvander@541: // returned iterator, incremented by one. This is used in Recalls. dvander@541: // The hook resets this state automatically on: dvander@541: // GetIter, ReleaseIter dvander@541: dvander@659: void RQFlagReset() { m_RQFlag = false; } dvander@541: bool RQFlagGet() { return m_RQFlag; } dvander@541: CHookList(); dvander@541: CHookList(const CHookList &other); dvander@541: virtual ~CHookList(); dvander@541: dvander@541: void operator=(const CHookList &other); dvander@541: dvander@541: IIter *GetIter(); dvander@541: void ReleaseIter(IIter *pIter); dvander@659: dvander@659: void SetVPList(List *newList); dvander@659: void ClearVPList(); dvander@541: }; dvander@541: dvander@541: // I know, data hiding... But I'm a lazy bastard! dvander@541: dvander@541: class CIface : public IIface dvander@541: { dvander@541: public: dvander@541: void *m_Ptr; dvander@541: CHookList m_PreHooks; dvander@541: CHookList m_PostHooks; dvander@541: public: dvander@541: CIface(void *ptr); dvander@541: virtual ~CIface(); dvander@541: dvander@541: void *GetPtr(); dvander@541: IHookList *GetPreHooks(); dvander@541: IHookList *GetPostHooks(); dvander@541: dvander@541: bool operator==(void *ptr) dvander@541: { dvander@541: return m_Ptr == ptr; dvander@541: } dvander@659: bool operator!=(void *ptr) dvander@659: { dvander@659: return m_Ptr != ptr; dvander@659: } dvander@541: }; dvander@541: dvander@541: class CVfnPtr : public IVfnPtr dvander@541: { dvander@541: public: dvander@541: typedef List IfaceList; dvander@541: typedef IfaceList::iterator IfaceListIter; dvander@541: dvander@541: void *m_Ptr; dvander@541: void *m_OrigEntry; dvander@541: dvander@541: IfaceList m_Ifaces; dvander@541: dvander@659: void **m_pOneIgnore; dvander@541: public: dvander@659: CVfnPtr(void *ptr, void **pOneIgnore); dvander@541: virtual ~CVfnPtr(); dvander@541: dvander@541: void *GetVfnPtr(); dvander@541: void *GetOrigEntry(); dvander@541: dvander@541: virtual IIface *FindIface(void *ptr); dvander@541: dvander@541: bool operator==(void *ptr) dvander@541: { dvander@541: return m_Ptr == ptr; dvander@541: } dvander@541: }; dvander@541: dvander@541: class CHookManagerInfo : public IHookManagerInfo dvander@541: { dvander@541: public: dvander@541: typedef List VfnPtrList; dvander@541: typedef VfnPtrList::iterator VfnPtrListIter; dvander@541: dvander@541: Plugin m_Plug; dvander@541: HookManagerPubFunc m_Func; dvander@541: dvander@541: int m_VtblOffs; dvander@541: int m_VtblIdx; dvander@541: const char *m_Proto; dvander@541: void *m_HookfuncVfnptr; dvander@541: dvander@541: VfnPtrList m_VfnPtrs; dvander@541: dvander@541: int m_HookManVersion; dvander@541: public: dvander@541: CHookManagerInfo(); dvander@541: virtual ~CHookManagerInfo(); dvander@541: dvander@541: IVfnPtr *FindVfnPtr(void *vfnptr); dvander@541: dvander@541: void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto); dvander@541: void SetHookfuncVfnptr(void *hookfunc_vfnptr); dvander@541: dvander@541: void SetVersion(int version); dvander@541: dvander@541: bool operator < (const CHookManagerInfo &other) dvander@541: { dvander@541: return m_HookManVersion < other.m_HookManVersion; dvander@541: } dvander@541: dvander@541: struct Descriptor dvander@541: { dvander@541: Descriptor(Plugin pplug, HookManagerPubFunc ppubFunc) : plug(pplug), pubFunc(ppubFunc) dvander@541: { dvander@541: } dvander@541: dvander@541: Plugin plug; dvander@541: HookManagerPubFunc pubFunc; dvander@541: }; dvander@541: dvander@541: bool operator == (const Descriptor desc) dvander@541: { dvander@541: return m_Func == desc.pubFunc && m_Plug == desc.plug; dvander@541: } dvander@541: }; dvander@541: dvander@541: typedef List HookManInfoList; dvander@541: dvander@541: class CHookManagerContainer : public HookManInfoList dvander@541: { dvander@541: public: dvander@541: // HMCI (Hook Manager Container Identification) dvander@541: class HMCI dvander@541: { dvander@541: CProto m_Proto; dvander@541: int m_VtableOffset; dvander@541: int m_VtableIndex; dvander@541: public: dvander@541: HMCI(const char *proto, int vtbloffs, int vtblidx) : dvander@541: m_Proto(proto), m_VtableOffset(vtbloffs), m_VtableIndex(vtblidx) dvander@541: { dvander@541: } dvander@541: ~HMCI() dvander@541: { dvander@541: } dvander@541: dvander@541: bool operator==(const HMCI &other) const dvander@541: { dvander@541: return dvander@541: other.m_VtableIndex == m_VtableIndex && dvander@541: other.m_Proto == m_Proto && dvander@541: other.m_VtableOffset == m_VtableOffset; dvander@541: } dvander@541: dvander@541: const CProto &GetProto() const dvander@541: { dvander@541: return m_Proto; dvander@541: } dvander@541: int GetVtableOffset() const dvander@541: { dvander@541: return m_VtableOffset; dvander@541: } dvander@541: int GetVtableIndex() const dvander@541: { dvander@541: return m_VtableIndex; dvander@541: } dvander@541: }; dvander@541: HMCI m_HCMI; dvander@541: dvander@541: public: dvander@541: CHookManagerContainer(const HMCI &hmci) : m_HCMI(hmci) dvander@541: { dvander@541: } dvander@541: bool operator == (const HMCI &other) const dvander@541: { dvander@541: return m_HCMI == other; dvander@541: } dvander@541: void AddHookManager(Plugin plug, const CHookManagerInfo &hookman); dvander@541: }; dvander@541: dvander@659: class CCallClassImpl : public DeprecatedCallClass dvander@541: { dvander@541: public: dvander@541: dvander@541: typedef SourceHook::CVector OrigFuncs; dvander@541: typedef SourceHook::THash OrigVTables; dvander@541: dvander@541: void *m_Ptr; //!< Pointer to the actual object dvander@541: size_t m_ObjSize; //!< Size of the instance dvander@541: OrigVTables m_VT; //!< Info about vtables & functions dvander@541: dvander@541: int m_RefCounter; dvander@541: dvander@541: CCallClassImpl(void *ptr, size_t size); dvander@541: virtual ~CCallClassImpl(); dvander@541: dvander@541: bool operator==(void *other) dvander@541: { dvander@541: return m_Ptr == other; dvander@541: } dvander@541: dvander@541: void *GetThisPtr(); dvander@541: void *GetOrigFunc(int vtbloffs, int vtblidx); dvander@541: dvander@541: void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry); dvander@541: void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx); dvander@541: }; dvander@541: dvander@541: /** dvander@541: * @brief A list of CallClass structures dvander@541: */ dvander@541: typedef List Impl_CallClassList; dvander@541: dvander@541: Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses dvander@541: dvander@541: /** dvander@541: * @brief A list of CHookManagerContainers dvander@541: */ dvander@541: typedef List HookManContList; dvander@541: dvander@541: HookManContList m_HookMans; //!< A list of hook managers dvander@541: dvander@541: struct HookLoopInfo dvander@541: { dvander@541: enum RecallType dvander@541: { dvander@541: Recall_No=0, dvander@541: Recall_Pre, dvander@541: Recall_Post1, dvander@541: Recall_Post2 dvander@541: }; dvander@541: dvander@541: META_RES *pStatus; dvander@541: META_RES *pPrevRes; dvander@541: META_RES *pCurRes; dvander@541: dvander@541: META_RES temporaryStatus; //!< Stored during Post1 recall phase dvander@541: bool shouldContinue; dvander@541: RecallType recall; //!< Specifies which kind of recall we're in. dvander@541: dvander@541: IIface *pCurIface; dvander@541: const void *pOrigRet; dvander@541: void *pOverrideRet; dvander@541: void **pIfacePtrPtr; dvander@541: }; dvander@541: typedef CStack HookLoopInfoStack; dvander@541: dvander@541: void ApplyCallClassPatches(CCallClassImpl &cc); dvander@541: void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry); dvander@541: void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx); dvander@541: dvander@541: void SetPluginPaused(Plugin plug, bool paused); dvander@541: dvander@541: HookLoopInfoStack m_HLIStack; dvander@659: CHookIDManager m_HookIDMan; dvander@659: dvander@659: void *m_OneIgnore; //:TODO: dvander@659: bool m_IgnoreActive; dvander@541: public: dvander@541: CSourceHookImpl(); dvander@541: virtual ~CSourceHookImpl(); dvander@541: dvander@541: /** dvander@541: * @brief Returns the interface version dvander@541: */ dvander@541: int GetIfaceVersion(); dvander@541: dvander@541: /** dvander@541: * @brief Returns the implemnetation version dvander@541: */ dvander@541: int GetImplVersion(); dvander@541: dvander@541: /** dvander@541: * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers dvander@541: */ dvander@541: void UnloadPlugin(Plugin plug); dvander@541: dvander@541: /** dvander@541: * @brief Shut down the whole system, unregister all hook managers dvander@541: */ dvander@541: void CompleteShutdown(); dvander@541: dvander@541: /** dvander@541: * @brief Add a hook. dvander@541: * dvander@541: * @return True if the function succeeded, false otherwise dvander@541: * dvander@541: * @param plug The unique identifier of the plugin that calls this function dvander@541: * @param iface The interface pointer dvander@541: * @param ifacesize The size of the class iface points to dvander@541: * @param myHookMan A hook manager function that should be capable of handling the function dvander@541: * @param handler A pointer to a FastDelegate containing the hook handler dvander@541: * @param post Set to true if you want a post handler dvander@541: */ dvander@541: bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post); dvander@541: dvander@541: /** dvander@541: * @brief Removes a hook. dvander@541: * dvander@541: * @return True if the function succeeded, false otherwise dvander@541: * dvander@541: * @param plug The unique identifier of the plugin that calls this function dvander@541: * @param iface The interface pointer dvander@541: * @param thisptr_offs This pointer adjuster dvander@541: * @param myHookMan A hook manager function that should be capable of handling the function dvander@541: * @param handler A pointer to a FastDelegate containing the hook handler dvander@541: * @param post Set to true if you want a post handler dvander@541: */ dvander@541: bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post); dvander@541: dvander@541: /** dvander@541: * @brief Removes a hook. dvander@541: * dvander@541: * @ return True if the function succeeded, false otherwise dvander@541: * dvander@541: * @param info A RemoveHookInfo structure, describing the hook dvander@541: */ dvander@541: bool RemoveHook(RemoveHookInfo info); dvander@541: dvander@541: /** dvander@541: * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins dvander@541: * dvander@541: * @param plug The unique identifier of the plugin in question dvander@541: */ dvander@541: bool IsPluginInUse(Plugin plug); dvander@541: dvander@541: /** dvander@541: * @brief Pauses all hooks of a plugin dvander@541: * dvander@541: * @param plug The unique identifier of the plugin dvander@541: */ dvander@541: void PausePlugin(Plugin plug); dvander@541: dvander@541: /** dvander@541: * @brief Unpauses all hooks of a plugin dvander@541: * dvander@541: * @param plug The unique identifier of the plugin dvander@541: */ dvander@541: void UnpausePlugin(Plugin plug); dvander@541: dvander@541: /** dvander@541: * @brief Return a pointer to a callclass. Generate a new one if required. dvander@541: * dvander@541: * @param iface The interface pointer dvander@541: * @param size Size of the class instance dvander@541: */ dvander@659: DeprecatedCallClass *GetCallClass(void *iface, size_t size); dvander@541: dvander@541: /** dvander@541: * @brief Release a callclass dvander@541: * dvander@541: * @param ptr Pointer to the callclass dvander@541: */ dvander@659: virtual void ReleaseCallClass(DeprecatedCallClass *ptr); dvander@541: dvander@541: virtual void SetRes(META_RES res); //!< Sets the meta result dvander@541: virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler dvander@541: virtual META_RES GetStatus(); //!< Gets the highest meta result dvander@541: virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined dvander@541: virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL dvander@541: virtual void *GetIfacePtr(); //!< Gets the interface pointer dvander@541: dvander@541: ////////////////////////////////////////////////////////////////////////// dvander@541: // For hook managers dvander@541: void HookLoopBegin(IIface *pIface); //!< Should be called when a hook loop begins dvander@541: void HookLoopEnd(); //!< Should be called when a hook loop exits dvander@541: void SetCurResPtr(META_RES *mres); //!< Sets pointer to the current meta result dvander@541: void SetPrevResPtr(META_RES *mres); //!< Sets pointer to previous meta result dvander@541: void SetStatusPtr(META_RES *mres); //!< Sets pointer to the status variable dvander@541: void SetIfacePtrPtr(void **pp); //!< Sets pointer to the interface this pointer dvander@541: void SetOrigRetPtr(const void *ptr); //!< Sets the original return pointer dvander@541: void SetOverrideRetPtr(void *ptr); //!< Sets the override result pointer dvander@541: bool ShouldContinue(); //!< Returns false if the hook loop should exit dvander@541: dvander@541: /** dvander@541: * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug. dvander@541: * dvander@541: * @param plug The owner of the hook manager dvander@541: * @param pubFunc The hook manager's info function dvander@541: */ dvander@541: virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc); dvander@541: virtual void RemoveHookManager(RemoveHookManInfo info); dvander@541: dvander@541: virtual void DoRecall(); //!< Initiates a recall sequence dvander@541: virtual void *GetOverrideRetPtr(); //!< Returns the pointer set by SetOverrideRetPtr dvander@541: dvander@541: virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr, dvander@541: void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr); dvander@659: dvander@659: /** dvander@659: * @brief Add a (VP) hook. dvander@659: * dvander@659: * @return non-zero hook id on success, 0 otherwise dvander@659: * dvander@659: * @param plug The unique identifier of the plugin that calls this function dvander@659: * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook) dvander@659: * @param iface The interface pointer dvander@659: * The representative interface pointer for VP hooks dvander@659: * The vtable pointer for direct VP hooks !!! dvander@659: * @param ifacesize The size of the class iface points to dvander@659: * @param myHookMan A hook manager function that should be capable of handling the function dvander@659: * @param handler A pointer to a FastDelegate containing the hook handler dvander@659: * @param post Set to true if you want a post handler dvander@659: */ dvander@659: virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, dvander@659: ISHDelegate *handler, bool post); dvander@659: dvander@659: /** dvander@659: * @brief Remove a VP hook by ID. dvander@659: * dvander@659: * @return true on success, false otherwise dvander@659: * dvander@659: * @param plug The unique identifier of the plugin that calls this function dvander@659: * @param hookid The hook id (returned by AddHookNew) dvander@659: */ dvander@659: virtual bool RemoveHookByID(Plugin plug, int hookid); dvander@659: dvander@659: /** dvander@659: * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr dvander@659: * dvander@659: * @param plug The unique identifier of the plugin that calls this function dvander@659: * @param vfnptr The virtual function pointer of the function in question dvander@659: */ dvander@659: virtual void SetIgnoreHooks(Plugin plug, void *vfnptr); dvander@659: dvander@659: /** dvander@659: * @brief Reverses SetIgnoreHooks' effect dvander@659: * dvander@659: * @param plug The unique identifier of the plugin that calls this function dvander@659: * @param vfnptr The virtual function pointer of the function in question dvander@659: */ dvander@659: virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr); dvander@659: dvander@659: /** dvander@659: * @brief Finds the original entry of a virtual function pointer dvander@659: * dvander@659: * @param vfnptr The virtual function pointer dvander@659: * @return The original entry if the virtual function pointer has been patched; NULL otherwise. dvander@659: */ dvander@659: virtual void *GetOrigVfnPtrEntry(void *vfnptr); dvander@541: }; dvander@541: } dvander@541: dvander@541: #endif