1.1 --- a/sourcehook/sourcehook_impl.h Fri Dec 23 11:58:11 2005 +0000
1.2 +++ b/sourcehook/sourcehook_impl.h Fri Dec 23 23:00:55 2005 +0000
1.3 @@ -17,6 +17,107 @@
1.4 #include "sh_tinyhash.h"
1.5 #include "sh_stack.h"
1.6
1.7 +/*
1.8 +
1.9 +IMPLEMENTATION INFO
1.10 +
1.11 +---------------------------------------
1.12 +Protos ("Prototypes")
1.13 + The purpose of protos is to provide the amount of type information about a function
1.14 + which is required to be able to execute a function call without corrupting the stack.
1.15 + Our protos do not fully do this, but they provide the size of the return value, the amount of
1.16 + parameters, and the size of each parameter, which is enough for most situations,
1.17 +
1.18 + There are two version of protos:
1.19 + OLD:
1.20 + C-Style strings.
1.21 +
1.22 + 0_void:
1.23 + "attrib"
1.24 + 1_void:
1.25 + "attrib|param1_type"
1.26 + 2_void:
1.27 + "attrib|param1_type|param2_type
1.28 + 0:
1.29 + "attrib|ret_type"
1.30 + 1:
1.31 + "attrib|ret_type|param1_type"
1.32 + 2:
1.33 + "attrib|ret_type|param2_type"
1.34 +
1.35 + Old protos are deprecated.
1.36 +
1.37 + NEW:
1.38 + New protos are in fact pointers to the ProtoInfo structure (see sourcehook.h)
1.39 +
1.40 + Old protos begin with a non-zero byte, new protos begin with a zero byte.
1.41 +
1.42 + Protos are usually stored in a CProto instance.
1.43 +
1.44 +---------------------------------------
1.45 +Hook managers and hook manager containers
1.46 + Each hookman container is tied to _one_ proto/vtable index/vtable offset info.
1.47 + Hookman containers then contain a list of hook managers provided by plugins, sorted by version.
1.48 + (higher versions come first)
1.49 +
1.50 + Duplicate hook managers are ignored (ie. hook managers where proto, vtable index, vtable offset,
1.51 + plugin, version are the same as in an already exisiting hook manager)
1.52 +
1.53 + A new hook manager is always added to the end of the version group in the corresponding
1.54 + hook container.
1.55 +
1.56 + If the new hook manager was added to the beginning of the container (which only happens if
1.57 + it is the first one or if it has a higher version than the previously first hook manager),
1.58 + the now second hook manager is shut down and the new hook manager takes its job.
1.59 +
1.60 + A "hook manager container id" (HMCI) consits of three values: proto, vtbl index, vtbl offset.
1.61 +---------------------------------------
1.62 +Hooks
1.63 + When adding a hook, first the proposed hook manager is added to the corresponding
1.64 + hook manager container as described above.
1.65 +
1.66 + Then the first hook manager in the the manhook container is chosen to handle the function.
1.67 +
1.68 + Removing a hook does not neccessarily unreigster the plugin's hook manager. In order to do this,
1.69 + use RemoveHookManager or UnloadPlugin/
1.70 +
1.71 + Hooks can be paused - they remain in memory but they are not called. In SH, the hook iterator
1.72 + classes handle pausing transparently.
1.73 +
1.74 + The hook loop is supposed to call ShouldContinue before each iteration. This makes hook handlers
1.75 + able to remove themselves.
1.76 +
1.77 +---------------------------------------
1.78 +Call classes
1.79 + Call classes are identified by a this pointer and an instance size
1.80 +
1.81 + We use the instance size because a derived class instance and a base class instance could
1.82 + have the same this pointers, and we want to avoid that the derived class
1.83 + (which could be bigger) gets the same callclass as the base class (mainly if the
1.84 + base class' callclass was requested first).
1.85 +
1.86 + Call classes are reference counted.
1.87 +
1.88 + The original function pointers are stored in a vector (in order to allow fast random access).
1.89 + These vectors are stored as the value type of a hash. The key type is int and represents the
1.90 + vtable offset.
1.91 +
1.92 + If the hash key doesn't exist or the vtblidx is out of range or the corresponding element in the
1.93 + vector is NULL, there is no hook on that function.
1.94 +
1.95 +---------------------------------------
1.96 +Recalls
1.97 + Recalls are used for the META_RETURN_(VALUE_)NEWPARAMS macros, ie. to change the parameters
1.98 + in the hook loop on the fly.
1.99 +
1.100 + First, the macro calls DoRecall(), then it calls the function the hook is attached to -> it
1.101 + calls the hookfunc. SourceHook makes sure that the newly invoked hook loop starts where the last
1.102 + one left off and that status variables like status, previous result, override return are kept.
1.103 + When this recurisvely called hookfunc returns, the macro returns what it returned
1.104 + (using MRES_SUPERCEDE). CSourceHookImpl returns false from ShouldContinue so the original hook loop
1.105 + is abandonned.
1.106 +*/
1.107 +
1.108 namespace SourceHook
1.109 {
1.110 /**
1.111 @@ -25,6 +126,40 @@
1.112 class CSourceHookImpl : public ISourceHook
1.113 {
1.114 private:
1.115 + class CProto
1.116 + {
1.117 + const char *m_Proto;
1.118 +
1.119 + static bool Equal(const char *p1, const char *p2);
1.120 + public:
1.121 + CProto(const char *szProto) : m_Proto(szProto)
1.122 + {
1.123 + }
1.124 +
1.125 + CProto(const CProto &other) : m_Proto(other.m_Proto)
1.126 + {
1.127 + }
1.128 +
1.129 + void operator = (const char *szProto)
1.130 + {
1.131 + m_Proto = szProto;
1.132 + }
1.133 + void operator = (const CProto &other)
1.134 + {
1.135 + m_Proto = other.m_Proto;
1.136 + }
1.137 +
1.138 + bool operator == (const char *szProto) const
1.139 + {
1.140 + return Equal(szProto, m_Proto);
1.141 + }
1.142 + bool operator == (const CProto &other) const
1.143 + {
1.144 + return Equal(other.m_Proto, m_Proto);
1.145 + }
1.146 + };
1.147 +
1.148 +
1.149 /**
1.150 * @brief A hook can be removed if you have this information
1.151 */
1.152 @@ -45,6 +180,17 @@
1.153 bool post;
1.154 };
1.155
1.156 + struct RemoveHookManInfo
1.157 + {
1.158 + RemoveHookManInfo(Plugin pplug, HookManagerPubFunc phookman)
1.159 + : plug(pplug), hookman(phookman)
1.160 + {
1.161 + }
1.162 +
1.163 + Plugin plug;
1.164 + HookManagerPubFunc hookman;
1.165 + };
1.166 +
1.167 struct HookInfo
1.168 {
1.169 ISHDelegate *handler; //!< Pointer to the handler
1.170 @@ -175,21 +321,93 @@
1.171
1.172 VfnPtrList m_VfnPtrs;
1.173
1.174 - int m_HookFuncVersion;
1.175 -
1.176 + int m_HookManVersion;
1.177 public:
1.178 + CHookManagerInfo();
1.179 virtual ~CHookManagerInfo();
1.180
1.181 IVfnPtr *FindVfnPtr(void *vfnptr);
1.182
1.183 void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
1.184 void SetHookfuncVfnptr(void *hookfunc_vfnptr);
1.185 +
1.186 + void SetVersion(int version);
1.187 +
1.188 + bool operator < (const CHookManagerInfo &other)
1.189 + {
1.190 + return m_HookManVersion < other.m_HookManVersion;
1.191 + }
1.192 +
1.193 + struct Descriptor
1.194 + {
1.195 + Descriptor(Plugin pplug, HookManagerPubFunc ppubFunc) : plug(pplug), pubFunc(ppubFunc)
1.196 + {
1.197 + }
1.198 +
1.199 + Plugin plug;
1.200 + HookManagerPubFunc pubFunc;
1.201 + };
1.202 +
1.203 + bool operator == (const Descriptor desc)
1.204 + {
1.205 + return m_Func == desc.pubFunc && m_Plug == desc.plug;
1.206 + }
1.207 };
1.208 - /**
1.209 - * @brief A list of CHookManagerInfo classes
1.210 - */
1.211 +
1.212 typedef List<CHookManagerInfo> HookManInfoList;
1.213
1.214 + class CHookManagerContainer : public HookManInfoList
1.215 + {
1.216 + public:
1.217 + // HMCI (Hook Manager Container Identification)
1.218 + class HMCI
1.219 + {
1.220 + CProto m_Proto;
1.221 + int m_VtableOffset;
1.222 + int m_VtableIndex;
1.223 + public:
1.224 + HMCI(const char *proto, int vtbloffs, int vtblidx) :
1.225 + m_Proto(proto), m_VtableOffset(vtbloffs), m_VtableIndex(vtblidx)
1.226 + {
1.227 + }
1.228 + ~HMCI()
1.229 + {
1.230 + }
1.231 +
1.232 + bool operator==(const HMCI &other) const
1.233 + {
1.234 + return
1.235 + other.m_VtableIndex == m_VtableIndex &&
1.236 + other.m_Proto == m_Proto &&
1.237 + other.m_VtableOffset == m_VtableOffset;
1.238 + }
1.239 +
1.240 + const CProto &GetProto() const
1.241 + {
1.242 + return m_Proto;
1.243 + }
1.244 + int GetVtableOffset() const
1.245 + {
1.246 + return m_VtableOffset;
1.247 + }
1.248 + int GetVtableIndex() const
1.249 + {
1.250 + return m_VtableIndex;
1.251 + }
1.252 + };
1.253 + HMCI m_HCMI;
1.254 +
1.255 + public:
1.256 + CHookManagerContainer(const HMCI &hmci) : m_HCMI(hmci)
1.257 + {
1.258 + }
1.259 + bool operator == (const HMCI &other) const
1.260 + {
1.261 + return m_HCMI == other;
1.262 + }
1.263 + void AddHookManager(Plugin plug, const CHookManagerInfo &hookman);
1.264 + };
1.265 +
1.266 class CCallClassImpl : public GenericCallClass
1.267 {
1.268 public:
1.269 @@ -224,7 +442,13 @@
1.270 typedef List<CCallClassImpl> Impl_CallClassList;
1.271
1.272 Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
1.273 - HookManInfoList m_HookMans; //!< A list of hook managers
1.274 +
1.275 + /**
1.276 + * @brief A list of CHookManagerContainers
1.277 + */
1.278 + typedef List<CHookManagerContainer> HookManContList;
1.279 +
1.280 + HookManContList m_HookMans; //!< A list of hook managers
1.281
1.282 struct HookLoopInfo
1.283 {
1.284 @@ -242,20 +466,12 @@
1.285 };
1.286 typedef CStack<HookLoopInfo> HookLoopInfoStack;
1.287
1.288 - /**
1.289 - * @brief Finds a hook manager for a function based on a text-prototype, a vtable offset and a vtable index
1.290 - */
1.291 - HookManInfoList::iterator FindHookMan(HookManInfoList::iterator begin, HookManInfoList::iterator end,
1.292 - const char *proto, int vtblofs, int vtblidx);
1.293 -
1.294 void ApplyCallClassPatches(CCallClassImpl &cc);
1.295 void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
1.296 void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
1.297
1.298 void SetPluginPaused(Plugin plug, bool paused);
1.299
1.300 - bool ProtosEquiv(const char *p1, const char *p2);
1.301 -
1.302 HookLoopInfoStack m_HLIStack;
1.303 public:
1.304 CSourceHookImpl();
1.305 @@ -380,6 +596,8 @@
1.306 * @param pubFunc The hook manager's info function
1.307 */
1.308 virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc);
1.309 + virtual void RemoveHookManager(RemoveHookManInfo info);
1.310 +
1.311 virtual void DoRecall(); //!< Initiates a recall sequence
1.312 virtual void *GetOverrideRetPtr(); //!< Returns the pointer set by SetOverrideRetPtr
1.313