diff -r a95db73792e5 -r 377bdd31c28d sourcehook/sourcehook_impl.h --- a/sourcehook/sourcehook_impl.h Fri Dec 23 11:58:11 2005 +0000 +++ b/sourcehook/sourcehook_impl.h Fri Dec 23 23:00:55 2005 +0000 @@ -17,6 +17,107 @@ #include "sh_tinyhash.h" #include "sh_stack.h" +/* + +IMPLEMENTATION INFO + +--------------------------------------- +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. + Our protos do not fully do this, but they provide the size of the return value, the amount of + parameters, and the size of each parameter, which is enough for most situations, + + There are two version of protos: + OLD: + C-Style strings. + + 0_void: + "attrib" + 1_void: + "attrib|param1_type" + 2_void: + "attrib|param1_type|param2_type + 0: + "attrib|ret_type" + 1: + "attrib|ret_type|param1_type" + 2: + "attrib|ret_type|param2_type" + + Old protos are deprecated. + + NEW: + New protos are in fact pointers to the ProtoInfo structure (see sourcehook.h) + + Old protos begin with a non-zero byte, new protos begin with a zero byte. + + Protos are usually stored in a CProto instance. + +--------------------------------------- +Hook managers and hook manager containers + Each hookman container is tied to _one_ proto/vtable index/vtable offset info. + Hookman containers then contain a list of hook managers provided by plugins, sorted by version. + (higher versions come first) + + Duplicate hook managers are ignored (ie. hook managers where proto, vtable index, vtable offset, + plugin, version are the same as in an already exisiting hook manager) + + A new hook manager is always added to the end of the version group in the corresponding + hook container. + + If the new hook manager was added to the beginning of the container (which only happens if + it is the first one or if it has a higher version than the previously first hook manager), + the now second hook manager is shut down and the new hook manager takes its job. + + A "hook manager container id" (HMCI) consits of three values: proto, vtbl index, vtbl offset. +--------------------------------------- +Hooks + When adding a hook, first the proposed hook manager is added to the corresponding + hook manager container as described above. + + Then the first hook manager in the the manhook container is chosen to handle the function. + + Removing a hook does not neccessarily unreigster the plugin's hook manager. In order to do this, + use RemoveHookManager or UnloadPlugin/ + + Hooks can be paused - they remain in memory but they are not called. In SH, the hook iterator + classes handle pausing transparently. + + The hook loop is supposed to call ShouldContinue before each iteration. This makes hook handlers + able to remove themselves. + +--------------------------------------- +Call classes + Call classes are identified by a this pointer and an instance size + + We use the instance size because a derived class instance and a base class instance could + have the same this pointers, and we want to avoid that the derived class + (which could be bigger) gets the same callclass as the base class (mainly if the + base class' callclass was requested first). + + Call classes are reference counted. + + The original function pointers are stored in a vector (in order to allow fast random access). + These vectors are stored as the value type of a hash. The key type is int and represents the + vtable offset. + + If the hash key doesn't exist or the vtblidx is out of range or the corresponding element in the + vector is NULL, there is no hook on that function. + +--------------------------------------- +Recalls + Recalls are used for the META_RETURN_(VALUE_)NEWPARAMS macros, ie. to change the parameters + in the hook loop on the fly. + + First, the macro calls DoRecall(), then it calls the function the hook is attached to -> it + calls the hookfunc. SourceHook makes sure that the newly invoked hook loop starts where the last + one left off and that status variables like status, previous result, override return are kept. + When this recurisvely called hookfunc returns, the macro returns what it returned + (using MRES_SUPERCEDE). CSourceHookImpl returns false from ShouldContinue so the original hook loop + is abandonned. +*/ + namespace SourceHook { /** @@ -25,6 +126,40 @@ class CSourceHookImpl : public ISourceHook { private: + class CProto + { + const char *m_Proto; + + static bool Equal(const char *p1, const char *p2); + public: + CProto(const char *szProto) : m_Proto(szProto) + { + } + + CProto(const CProto &other) : m_Proto(other.m_Proto) + { + } + + void operator = (const char *szProto) + { + m_Proto = szProto; + } + void operator = (const CProto &other) + { + m_Proto = 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); + } + }; + + /** * @brief A hook can be removed if you have this information */ @@ -45,6 +180,17 @@ bool post; }; + struct RemoveHookManInfo + { + RemoveHookManInfo(Plugin pplug, HookManagerPubFunc phookman) + : plug(pplug), hookman(phookman) + { + } + + Plugin plug; + HookManagerPubFunc hookman; + }; + struct HookInfo { ISHDelegate *handler; //!< Pointer to the handler @@ -175,21 +321,93 @@ VfnPtrList m_VfnPtrs; - int m_HookFuncVersion; - + 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; + } + + 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; + } }; - /** - * @brief A list of CHookManagerInfo classes - */ + 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 GenericCallClass { public: @@ -224,7 +442,13 @@ typedef List Impl_CallClassList; Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses - HookManInfoList m_HookMans; //!< A list of hook managers + + /** + * @brief A list of CHookManagerContainers + */ + typedef List HookManContList; + + HookManContList m_HookMans; //!< A list of hook managers struct HookLoopInfo { @@ -242,20 +466,12 @@ }; typedef CStack HookLoopInfoStack; - /** - * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index - */ - HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end, - const char *proto, int vtblofs, int vtblidx); - 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 SetPluginPaused(Plugin plug, bool paused); - bool ProtosEquiv(const char *p1, const char *p2); - HookLoopInfoStack m_HLIStack; public: CSourceHookImpl(); @@ -380,6 +596,8 @@ * @param pubFunc The hook manager's info function */ virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc); + virtual void RemoveHookManager(RemoveHookManInfo info); + virtual void DoRecall(); //!< Initiates a recall sequence virtual void *GetOverrideRetPtr(); //!< Returns the pointer set by SetOverrideRetPtr