1.1 --- a/sourcehook/sourcehook_impl.h Thu Oct 04 19:46:28 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,817 +0,0 @@
1.4 -/* ======== SourceHook ========
1.5 -* Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 -* No warranties of any kind
1.7 -*
1.8 -* License: zlib/libpng
1.9 -*
1.10 -* Author(s): Pavol "PM OnoTo" Marko
1.11 -* ============================
1.12 -*/
1.13 -
1.14 -#ifndef __SOURCEHOOK_IMPL_H__
1.15 -#define __SOURCEHOOK_IMPL_H__
1.16 -
1.17 -#include "sourcehook.h"
1.18 -#include "sh_list.h"
1.19 -#include "sh_vector.h"
1.20 -#include "sh_tinyhash.h"
1.21 -#include "sh_stack.h"
1.22 -#include "sh_listcat.h"
1.23 -
1.24 -/*
1.25 -
1.26 -IMPLEMENTATION INFO
1.27 -
1.28 ----------------------------------------
1.29 -Protos ("Prototypes")
1.30 - The purpose of protos is to provide the amount of type information about a function
1.31 - which is required to be able to execute a function call without corrupting the stack.
1.32 - Our protos do not fully do this, but they provide the size of the return value, the number of
1.33 - parameters, and the size of each parameter, which is enough for most situations.
1.34 -
1.35 - There are two version of protos:
1.36 - OLD:
1.37 - C-Style strings.
1.38 -
1.39 - 0_void:
1.40 - "attrib"
1.41 - 1_void:
1.42 - "attrib|param1_type"
1.43 - 2_void:
1.44 - "attrib|param1_type|param2_type
1.45 - 0:
1.46 - "attrib|ret_type"
1.47 - 1:
1.48 - "attrib|ret_type|param1_type"
1.49 - 2:
1.50 - "attrib|ret_type|param2_type"
1.51 -
1.52 - Old protos are deprecated.
1.53 -
1.54 - NEW:
1.55 - New protos are in fact pointers to the ProtoInfo structure (see sourcehook.h)
1.56 -
1.57 - Old protos begin with a non-zero byte, new protos begin with a zero byte.
1.58 -
1.59 - Protos are usually stored in a CProto instance.
1.60 -
1.61 ----------------------------------------
1.62 -Hook managers and hook manager containers
1.63 - Each hookman container is tied to _one_ proto/vtable index/vtable offset info.
1.64 - Hookman containers then contain a list of hook managers provided by plugins, sorted by version.
1.65 - (higher versions come first)
1.66 -
1.67 - Duplicate hook managers are ignored (ie. hook managers where proto, vtable index, vtable offset,
1.68 - plugin, version are the same as in an already exisiting hook manager)
1.69 -
1.70 - A new hook manager is always added to the end of the version group in the corresponding
1.71 - hook container.
1.72 -
1.73 - If the new hook manager was added to the beginning of the container (which only happens if
1.74 - it is the first one or if it has a higher version than the previously first hook manager),
1.75 - the now second hook manager is shut down and the new hook manager takes its job.
1.76 -
1.77 - A "hook manager container id" (HMCI) consits of three values: proto, vtbl index, vtbl offset.
1.78 ----------------------------------------
1.79 -Hooks
1.80 - When adding a hook, first the proposed hook manager is added to the corresponding
1.81 - hook manager container as described above.
1.82 -
1.83 - Then the first hook manager in the the manhook container is chosen to handle the function.
1.84 -
1.85 - Removing a hook does not neccessarily unreigster the plugin's hook manager. In order to do this,
1.86 - use RemoveHookManager or UnloadPlugin/
1.87 -
1.88 - Hooks can be paused - they remain in memory but they are not called. In SH, the hook iterator
1.89 - classes handle pausing transparently.
1.90 -
1.91 - The hook loop is supposed to call ShouldContinue before each iteration. This makes hook handlers
1.92 - able to remove themselves.
1.93 -
1.94 ----------------------------------------
1.95 -Call classes
1.96 -
1.97 - !! deprecated !! - see below (new SH_CALL)
1.98 -
1.99 - Call classes are identified by a this pointer and an instance size
1.100 -
1.101 - We use the instance size because a derived class instance and a base class instance could
1.102 - have the same this pointers, and we want to avoid that the derived class
1.103 - (which could be bigger) gets the same callclass as the base class (mainly if the
1.104 - base class' callclass was requested first).
1.105 -
1.106 - Call classes are reference counted.
1.107 -
1.108 - The original function pointers are stored in a vector (in order to allow fast random access).
1.109 - These vectors are stored as the value type of a hash. The key type is int and represents the
1.110 - vtable offset.
1.111 -
1.112 - If the hash key doesn't exist or the vtblidx is out of range or the corresponding element in the
1.113 - vector is NULL, there is no hook on that function.
1.114 -
1.115 ----------------------------------------
1.116 -Recalls
1.117 - Recalls are used for the META_RETURN_(VALUE_)NEWPARAMS macros, ie. to change the parameters
1.118 - in the hook loop on the fly.
1.119 -
1.120 - First, the macro calls DoRecall(), then it calls the function the hook is attached to -> it
1.121 - calls the hookfunc. SourceHook makes sure that the newly invoked hook loop starts where the last
1.122 - one left off and that status variables like status, previous result, override return are kept.
1.123 - When this recurisvely called hookfunc returns, the macro returns what it returned
1.124 - (using MRES_SUPERCEDE). CSourceHookImpl returns false from ShouldContinue so the original hook loop
1.125 - is abandonned.
1.126 -
1.127 -Post Recalls
1.128 - People wanted to be able to use META_RETURN_(VALUE_)NEWPARAMS from post hooks as well. Crazy people!
1.129 - Anyway, for this, we have to know where a hook handler is. Is it executing pre or post hooks at the moment?
1.130 - The only way we can know this is watching when it calls CHookList::GetIter(). So CHookList gets a new variable:
1.131 - m_RequestedFlag. It also gets two new functions: RQFlagReset() and RQFlagGet().
1.132 - HookLoopBegin() calls RQFlagReset on both hooklists of the iface; then DoRecall() checks whether the postlist's
1.133 - RQ flag is set. if yes, the hook loop is in post mode.
1.134 -
1.135 - So, what a about a recall in post mode? The first ShouldContinue returns false and sets Status to supercede.
1.136 - This way the pre hooks and the function call will be skipped. Then, then next ShouldContinue returns true, so we get
1.137 - into the post hooks. HA!
1.138 -
1.139 -Return Values in Post Recalls
1.140 - The easy case is when we already have an override return value. In this case, the status register gets transferred,
1.141 - and so does the override return pointer. So, basically, everything is ok.
1.142 -
1.143 - However, what happens if we don't? ie. the status register is on MRES_IGNORED? In this case we'd have to transfer the
1.144 - 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.
1.145 - So, we emulate it. GetOrigRet will return the orig ret pointer from the old hook loop. If still no one overrides it,
1.146 - we'd have to return it. BUT! HOW TO DO THIS? Check out SH_RETURN(). First calls HookLoopEnd(), then decides whether
1.147 - to return the override retval or the orig retval. But it doesn't ask for a new override return. So we give the function
1.148 - the last orig return value as its new override return value; but leave status where it is so everything works, and in
1.149 - HookLoopEnd we make sure that status is high enough so that the override return will be returned. crazy.
1.150 -
1.151 - All this stuff could be much less complicated if I didn't try to preserve binary compatibility :)
1.152 -
1.153 -VP Hooks
1.154 - VP hooks are hooks which are called on a vfnptr, regardless of the this pointer with which it was called. They are
1.155 - implemented as a special CIface instance with m_Ptr = NULL. All Hook Lists have a new "ListCatIterator" which
1.156 - virtually concatenates the NULL-interface-hook-list with their normal hook list.
1.157 -
1.158 -
1.159 - I'm afraid that with the addition of Recalls and VP Hooks, SourceHook is now a pretty complex and hacked-together
1.160 - binary compatible beast which is pretty hard to maintain unless you've written it :)
1.161 -
1.162 -New SH_CALL
1.163 - The addition of VP hooks messed up the Call Classes concept (see above) - call classes are bound to an
1.164 - instance pointer; they only work on one of the hooked instances. But VP hooks are called on all instances.
1.165 -
1.166 - That's why now, SH_CALL takes an instance pointer instead of a callclass pointer. It basically does this:
1.167 - 1) call SH_GLOB_PTR->SetIgnoreHooks(vfnptr)
1.168 - 2) call this->*mfp
1.169 - 3) call SH_GLOB_PTR->ResetIgnoreHooks(vfnptr)
1.170 -
1.171 - SourceHook stroes the "ignored vfnptr" and makes CVfnPtr::FindIface return NULL if the CVfnPtr instance
1.172 - corresponds to the ignored vfnptr. This way the hook manager thinks that the instance isn't hooked, and calls
1.173 - the original function. Everything works fine. This works even for VP hooks.
1.174 -*/
1.175 -
1.176 -namespace SourceHook
1.177 -{
1.178 - /**
1.179 - * @brief The SourceHook implementation class
1.180 - */
1.181 - class CSourceHookImpl : public ISourceHook
1.182 - {
1.183 - private:
1.184 - class CProto
1.185 - {
1.186 - char *m_Proto;
1.187 -
1.188 - static bool Equal(const char *p1, const char *p2);
1.189 - char *DupProto(const char *src);
1.190 - void FreeProto(char *prot);
1.191 - public:
1.192 - CProto() : m_Proto(NULL)
1.193 - {
1.194 - }
1.195 -
1.196 - CProto(const char *szProto) : m_Proto(DupProto(szProto))
1.197 - {
1.198 - }
1.199 -
1.200 - CProto(const CProto &other) : m_Proto(DupProto(other.m_Proto))
1.201 - {
1.202 - }
1.203 -
1.204 - ~CProto()
1.205 - {
1.206 - FreeProto(m_Proto);
1.207 - m_Proto = NULL;
1.208 - }
1.209 -
1.210 - void operator = (const char *szProto)
1.211 - {
1.212 - m_Proto = DupProto(szProto);
1.213 - }
1.214 -
1.215 - void operator = (const CProto &other)
1.216 - {
1.217 - m_Proto = DupProto(other.m_Proto);
1.218 - }
1.219 -
1.220 - bool operator == (const char *szProto) const
1.221 - {
1.222 - return Equal(szProto, m_Proto);
1.223 - }
1.224 - bool operator == (const CProto &other) const
1.225 - {
1.226 - return Equal(other.m_Proto, m_Proto);
1.227 - }
1.228 -
1.229 - const char *GetProto() const
1.230 - {
1.231 - return m_Proto;
1.232 - }
1.233 - };
1.234 -
1.235 -
1.236 - /**
1.237 - * @brief A hook can be removed if you have this information
1.238 - */
1.239 - struct RemoveHookInfo
1.240 - {
1.241 - RemoveHookInfo(Plugin pplug, void *piface, int tpo, HookManagerPubFunc phookman,
1.242 - ISHDelegate *phandler, bool ppost)
1.243 - : plug(pplug), iface(piface), thisptr_offs(tpo),
1.244 - hookman(phookman), handler(phandler), post(ppost)
1.245 - {
1.246 - }
1.247 -
1.248 - Plugin plug;
1.249 - void *iface;
1.250 - int thisptr_offs;
1.251 - HookManagerPubFunc hookman;
1.252 - ISHDelegate *handler;
1.253 - bool post;
1.254 - };
1.255 -
1.256 - struct RemoveHookManInfo
1.257 - {
1.258 - RemoveHookManInfo(Plugin pplug, HookManagerPubFunc phookman)
1.259 - : plug(pplug), hookman(phookman)
1.260 - {
1.261 - }
1.262 -
1.263 - Plugin plug;
1.264 - HookManagerPubFunc hookman;
1.265 - };
1.266 -
1.267 - // Associates hook ids with info about the hooks
1.268 - // Also used to keep track of used hook ids
1.269 - class CHookIDManager
1.270 - {
1.271 - public:
1.272 - struct Entry
1.273 - {
1.274 - bool isfree;
1.275 -
1.276 - // hookman info
1.277 - CProto proto;
1.278 - int vtbl_offs;
1.279 - int vtbl_idx;
1.280 -
1.281 - // vfnptr
1.282 - void *vfnptr;
1.283 -
1.284 - // iface
1.285 - void* adjustediface;
1.286 -
1.287 - // hook
1.288 - Plugin plug;
1.289 - int thisptr_offs;
1.290 - ISHDelegate *handler;
1.291 - bool post;
1.292 -
1.293 - Entry(const CProto &pprt, int pvo, int pvi, void *pvp, void *pai, Plugin pplug, int pto,
1.294 - ISHDelegate *ph, bool ppost)
1.295 - : isfree(false), proto(pprt), vtbl_offs(pvo), vtbl_idx(pvi), vfnptr(pvp),
1.296 - adjustediface(pai), plug(pplug), thisptr_offs(pto), handler(ph), post(ppost)
1.297 - {
1.298 - }
1.299 - Entry()
1.300 - {
1.301 - }
1.302 - };
1.303 - private:
1.304 - // Internally, hookid 1 is stored as m_Entries[0]
1.305 -
1.306 - CVector<Entry> m_Entries;
1.307 - public:
1.308 - CHookIDManager();
1.309 - int New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr, void *adjustediface,
1.310 - Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
1.311 - bool Remove(int hookid);
1.312 - const Entry * QueryHook(int hookid);
1.313 -
1.314 - // Finds all hooks with the given info, and fills the hookids into output.
1.315 - void FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs, int vtbl_idx,
1.316 - void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post);
1.317 -
1.318 - // Removes all hooks with a specified vfnptr
1.319 - bool RemoveAll(void *vfnptr);
1.320 - };
1.321 -
1.322 - struct HookInfo
1.323 - {
1.324 - ISHDelegate *handler; //!< Pointer to the handler
1.325 - bool paused; //!< If true, the hook should not be executed
1.326 - Plugin plug; //!< The owner plugin
1.327 - int thisptr_offs; //!< This pointer offset
1.328 - int hookid; //!< Unique ID given by CHookIDManager
1.329 -
1.330 - bool operator==(int otherid)
1.331 - {
1.332 - return hookid == otherid;
1.333 - }
1.334 - };
1.335 -
1.336 - class CHookList : public IHookList
1.337 - {
1.338 - public:
1.339 - List<HookInfo> *m_VPList; // left-hand list for ListCatIterator -> for VP hooks
1.340 - List<HookInfo> m_List;
1.341 -
1.342 - friend class CIter;
1.343 -
1.344 - class CIter : public IHookList::IIter
1.345 - {
1.346 - friend class CHookList;
1.347 -
1.348 - CHookList *m_pList;
1.349 -
1.350 - void SkipPaused();
1.351 - public:
1.352 -
1.353 - ListCatIterator<HookInfo> m_Iter;
1.354 -
1.355 - CIter(CHookList *pList);
1.356 -
1.357 - virtual ~CIter();
1.358 -
1.359 - void GoToBegin();
1.360 - void Set(CIter *pOther);
1.361 -
1.362 - bool End();
1.363 - void Next();
1.364 - ISHDelegate *Handler();
1.365 - int ThisPtrOffs();
1.366 -
1.367 - void Clear();
1.368 -
1.369 - CIter *m_pNext; // When stored in m_FreeIters and m_UsedIters
1.370 - CIter *m_pPrev; // Only used when stored in m_UsedIters
1.371 - };
1.372 -
1.373 - CIter *m_FreeIters;
1.374 - CIter *m_UsedIters; // The last returned and not-yet-released iter is always m_UsedIters
1.375 -
1.376 - // For recalls
1.377 - bool m_Recall;
1.378 - bool m_RQFlag;
1.379 -
1.380 - void SetRecallState(); // Sets the list into a state where the next returned
1.381 - // iterator (from GetIter) will be a copy of the last
1.382 - // returned iterator, incremented by one. This is used in Recalls.
1.383 - // The hook resets this state automatically on:
1.384 - // GetIter, ReleaseIter
1.385 -
1.386 - void RQFlagReset() { m_RQFlag = false; }
1.387 - bool RQFlagGet() { return m_RQFlag; }
1.388 - CHookList();
1.389 - CHookList(const CHookList &other);
1.390 - virtual ~CHookList();
1.391 -
1.392 - void operator=(const CHookList &other);
1.393 -
1.394 - IIter *GetIter();
1.395 - void ReleaseIter(IIter *pIter);
1.396 -
1.397 - void SetVPList(List<HookInfo> *newList);
1.398 - void ClearVPList();
1.399 - };
1.400 -
1.401 - // I know, data hiding... But I'm a lazy bastard!
1.402 -
1.403 - class CIface : public IIface
1.404 - {
1.405 - public:
1.406 - void *m_Ptr;
1.407 - CHookList m_PreHooks;
1.408 - CHookList m_PostHooks;
1.409 - public:
1.410 - CIface(void *ptr);
1.411 - virtual ~CIface();
1.412 -
1.413 - void *GetPtr();
1.414 - IHookList *GetPreHooks();
1.415 - IHookList *GetPostHooks();
1.416 -
1.417 - bool operator==(void *ptr)
1.418 - {
1.419 - return m_Ptr == ptr;
1.420 - }
1.421 - bool operator!=(void *ptr)
1.422 - {
1.423 - return m_Ptr != ptr;
1.424 - }
1.425 - };
1.426 -
1.427 - class CVfnPtr : public IVfnPtr
1.428 - {
1.429 - public:
1.430 - typedef List<CIface> IfaceList;
1.431 - typedef IfaceList::iterator IfaceListIter;
1.432 -
1.433 - void *m_Ptr;
1.434 - void *m_OrigEntry;
1.435 -
1.436 - IfaceList m_Ifaces;
1.437 -
1.438 - void **m_pOneIgnore;
1.439 - public:
1.440 - CVfnPtr(void *ptr, void **pOneIgnore);
1.441 - virtual ~CVfnPtr();
1.442 -
1.443 - void *GetVfnPtr();
1.444 - void *GetOrigEntry();
1.445 -
1.446 - virtual IIface *FindIface(void *ptr);
1.447 -
1.448 - bool operator==(void *ptr)
1.449 - {
1.450 - return m_Ptr == ptr;
1.451 - }
1.452 - };
1.453 -
1.454 - class CHookManagerInfo : public IHookManagerInfo
1.455 - {
1.456 - public:
1.457 - typedef List<CVfnPtr> VfnPtrList;
1.458 - typedef VfnPtrList::iterator VfnPtrListIter;
1.459 -
1.460 - Plugin m_Plug;
1.461 - HookManagerPubFunc m_Func;
1.462 -
1.463 - int m_VtblOffs;
1.464 - int m_VtblIdx;
1.465 - const char *m_Proto;
1.466 - void *m_HookfuncVfnptr;
1.467 -
1.468 - VfnPtrList m_VfnPtrs;
1.469 -
1.470 - int m_HookManVersion;
1.471 - public:
1.472 - CHookManagerInfo();
1.473 - virtual ~CHookManagerInfo();
1.474 -
1.475 - IVfnPtr *FindVfnPtr(void *vfnptr);
1.476 -
1.477 - void SetInfo(int vtbl_offs, int vtbl_idx, const char *proto);
1.478 - void SetHookfuncVfnptr(void *hookfunc_vfnptr);
1.479 -
1.480 - void SetVersion(int version);
1.481 -
1.482 - bool operator < (const CHookManagerInfo &other)
1.483 - {
1.484 - return m_HookManVersion < other.m_HookManVersion;
1.485 - }
1.486 -
1.487 - struct Descriptor
1.488 - {
1.489 - Descriptor(Plugin pplug, HookManagerPubFunc ppubFunc) : plug(pplug), pubFunc(ppubFunc)
1.490 - {
1.491 - }
1.492 -
1.493 - Plugin plug;
1.494 - HookManagerPubFunc pubFunc;
1.495 - };
1.496 -
1.497 - bool operator == (const Descriptor desc)
1.498 - {
1.499 - return m_Func == desc.pubFunc && m_Plug == desc.plug;
1.500 - }
1.501 - };
1.502 -
1.503 - typedef List<CHookManagerInfo> HookManInfoList;
1.504 -
1.505 - class CHookManagerContainer : public HookManInfoList
1.506 - {
1.507 - public:
1.508 - // HMCI (Hook Manager Container Identification)
1.509 - class HMCI
1.510 - {
1.511 - CProto m_Proto;
1.512 - int m_VtableOffset;
1.513 - int m_VtableIndex;
1.514 - public:
1.515 - HMCI(const char *proto, int vtbloffs, int vtblidx) :
1.516 - m_Proto(proto), m_VtableOffset(vtbloffs), m_VtableIndex(vtblidx)
1.517 - {
1.518 - }
1.519 - ~HMCI()
1.520 - {
1.521 - }
1.522 -
1.523 - bool operator==(const HMCI &other) const
1.524 - {
1.525 - return
1.526 - other.m_VtableIndex == m_VtableIndex &&
1.527 - other.m_Proto == m_Proto &&
1.528 - other.m_VtableOffset == m_VtableOffset;
1.529 - }
1.530 -
1.531 - const CProto &GetProto() const
1.532 - {
1.533 - return m_Proto;
1.534 - }
1.535 - int GetVtableOffset() const
1.536 - {
1.537 - return m_VtableOffset;
1.538 - }
1.539 - int GetVtableIndex() const
1.540 - {
1.541 - return m_VtableIndex;
1.542 - }
1.543 - };
1.544 - HMCI m_HCMI;
1.545 -
1.546 - public:
1.547 - CHookManagerContainer(const HMCI &hmci) : m_HCMI(hmci)
1.548 - {
1.549 - }
1.550 - bool operator == (const HMCI &other) const
1.551 - {
1.552 - return m_HCMI == other;
1.553 - }
1.554 - void AddHookManager(Plugin plug, const CHookManagerInfo &hookman);
1.555 - };
1.556 -
1.557 - class CCallClassImpl : public DeprecatedCallClass<void>
1.558 - {
1.559 - public:
1.560 -
1.561 - typedef SourceHook::CVector<void*> OrigFuncs;
1.562 - typedef SourceHook::THash<int, OrigFuncs> OrigVTables;
1.563 -
1.564 - void *m_Ptr; //!< Pointer to the actual object
1.565 - size_t m_ObjSize; //!< Size of the instance
1.566 - OrigVTables m_VT; //!< Info about vtables & functions
1.567 -
1.568 - int m_RefCounter;
1.569 -
1.570 - CCallClassImpl(void *ptr, size_t size);
1.571 - virtual ~CCallClassImpl();
1.572 -
1.573 - bool operator==(void *other)
1.574 - {
1.575 - return m_Ptr == other;
1.576 - }
1.577 -
1.578 - void *GetThisPtr();
1.579 - void *GetOrigFunc(int vtbloffs, int vtblidx);
1.580 -
1.581 - void ApplyCallClassPatch(int vtbl_offs, int vtbl_idx, void *orig_entry);
1.582 - void RemoveCallClassPatch(int vtbl_offs, int vtbl_idx);
1.583 - };
1.584 -
1.585 - /**
1.586 - * @brief A list of CallClass structures
1.587 - */
1.588 - typedef List<CCallClassImpl> Impl_CallClassList;
1.589 -
1.590 - Impl_CallClassList m_CallClasses; //!< A list of already generated callclasses
1.591 -
1.592 - /**
1.593 - * @brief A list of CHookManagerContainers
1.594 - */
1.595 - typedef List<CHookManagerContainer> HookManContList;
1.596 -
1.597 - HookManContList m_HookMans; //!< A list of hook managers
1.598 -
1.599 - struct HookLoopInfo
1.600 - {
1.601 - enum RecallType
1.602 - {
1.603 - Recall_No=0,
1.604 - Recall_Pre,
1.605 - Recall_Post1,
1.606 - Recall_Post2
1.607 - };
1.608 -
1.609 - META_RES *pStatus;
1.610 - META_RES *pPrevRes;
1.611 - META_RES *pCurRes;
1.612 -
1.613 - META_RES temporaryStatus; //!< Stored during Post1 recall phase
1.614 - bool shouldContinue;
1.615 - RecallType recall; //!< Specifies which kind of recall we're in.
1.616 -
1.617 - IIface *pCurIface;
1.618 - const void *pOrigRet;
1.619 - void *pOverrideRet;
1.620 - void **pIfacePtrPtr;
1.621 - };
1.622 - typedef CStack<HookLoopInfo> HookLoopInfoStack;
1.623 -
1.624 - void ApplyCallClassPatches(CCallClassImpl &cc);
1.625 - void ApplyCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx, void *orig_entry);
1.626 - void RemoveCallClassPatches(void *ifaceptr, int vtbl_offs, int vtbl_idx);
1.627 -
1.628 - void SetPluginPaused(Plugin plug, bool paused);
1.629 -
1.630 - HookLoopInfoStack m_HLIStack;
1.631 - CHookIDManager m_HookIDMan;
1.632 -
1.633 - void *m_OneIgnore; //:TODO:
1.634 - bool m_IgnoreActive;
1.635 - public:
1.636 - CSourceHookImpl();
1.637 - virtual ~CSourceHookImpl();
1.638 -
1.639 - /**
1.640 - * @brief Returns the interface version
1.641 - */
1.642 - int GetIfaceVersion();
1.643 -
1.644 - /**
1.645 - * @brief Returns the implemnetation version
1.646 - */
1.647 - int GetImplVersion();
1.648 -
1.649 - /**
1.650 - * @brief Make sure that a plugin is not used by any other plugins anymore, and unregister all its hook managers
1.651 - */
1.652 - void UnloadPlugin(Plugin plug);
1.653 -
1.654 - /**
1.655 - * @brief Shut down the whole system, unregister all hook managers
1.656 - */
1.657 - void CompleteShutdown();
1.658 -
1.659 - /**
1.660 - * @brief Add a hook.
1.661 - *
1.662 - * @return True if the function succeeded, false otherwise
1.663 - *
1.664 - * @param plug The unique identifier of the plugin that calls this function
1.665 - * @param iface The interface pointer
1.666 - * @param ifacesize The size of the class iface points to
1.667 - * @param myHookMan A hook manager function that should be capable of handling the function
1.668 - * @param handler A pointer to a FastDelegate containing the hook handler
1.669 - * @param post Set to true if you want a post handler
1.670 - */
1.671 - bool AddHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
1.672 -
1.673 - /**
1.674 - * @brief Removes a hook.
1.675 - *
1.676 - * @return True if the function succeeded, false otherwise
1.677 - *
1.678 - * @param plug The unique identifier of the plugin that calls this function
1.679 - * @param iface The interface pointer
1.680 - * @param thisptr_offs This pointer adjuster
1.681 - * @param myHookMan A hook manager function that should be capable of handling the function
1.682 - * @param handler A pointer to a FastDelegate containing the hook handler
1.683 - * @param post Set to true if you want a post handler
1.684 - */
1.685 - bool RemoveHook(Plugin plug, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan, ISHDelegate *handler, bool post);
1.686 -
1.687 - /**
1.688 - * @brief Removes a hook.
1.689 - *
1.690 - * @ return True if the function succeeded, false otherwise
1.691 - *
1.692 - * @param info A RemoveHookInfo structure, describing the hook
1.693 - */
1.694 - bool RemoveHook(RemoveHookInfo info);
1.695 -
1.696 - /**
1.697 - * @brief Checks whether a plugin has (a) hook manager(s) that is/are currently used by other plugins
1.698 - *
1.699 - * @param plug The unique identifier of the plugin in question
1.700 - */
1.701 - bool IsPluginInUse(Plugin plug);
1.702 -
1.703 - /**
1.704 - * @brief Pauses all hooks of a plugin
1.705 - *
1.706 - * @param plug The unique identifier of the plugin
1.707 - */
1.708 - void PausePlugin(Plugin plug);
1.709 -
1.710 - /**
1.711 - * @brief Unpauses all hooks of a plugin
1.712 - *
1.713 - * @param plug The unique identifier of the plugin
1.714 - */
1.715 - void UnpausePlugin(Plugin plug);
1.716 -
1.717 - /**
1.718 - * @brief Return a pointer to a callclass. Generate a new one if required.
1.719 - *
1.720 - * @param iface The interface pointer
1.721 - * @param size Size of the class instance
1.722 - */
1.723 - DeprecatedCallClass<void> *GetCallClass(void *iface, size_t size);
1.724 -
1.725 - /**
1.726 - * @brief Release a callclass
1.727 - *
1.728 - * @param ptr Pointer to the callclass
1.729 - */
1.730 - virtual void ReleaseCallClass(DeprecatedCallClass<void> *ptr);
1.731 -
1.732 - virtual void SetRes(META_RES res); //!< Sets the meta result
1.733 - virtual META_RES GetPrevRes(); //!< Gets the meta result of the previously called handler
1.734 - virtual META_RES GetStatus(); //!< Gets the highest meta result
1.735 - virtual const void *GetOrigRet(); //!< Gets the original result. If not in post function, undefined
1.736 - virtual const void *GetOverrideRet(); //!< Gets the override result. If none is specified, NULL
1.737 - virtual void *GetIfacePtr(); //!< Gets the interface pointer
1.738 -
1.739 - //////////////////////////////////////////////////////////////////////////
1.740 - // For hook managers
1.741 - void HookLoopBegin(IIface *pIface); //!< Should be called when a hook loop begins
1.742 - void HookLoopEnd(); //!< Should be called when a hook loop exits
1.743 - void SetCurResPtr(META_RES *mres); //!< Sets pointer to the current meta result
1.744 - void SetPrevResPtr(META_RES *mres); //!< Sets pointer to previous meta result
1.745 - void SetStatusPtr(META_RES *mres); //!< Sets pointer to the status variable
1.746 - void SetIfacePtrPtr(void **pp); //!< Sets pointer to the interface this pointer
1.747 - void SetOrigRetPtr(const void *ptr); //!< Sets the original return pointer
1.748 - void SetOverrideRetPtr(void *ptr); //!< Sets the override result pointer
1.749 - bool ShouldContinue(); //!< Returns false if the hook loop should exit
1.750 -
1.751 - /**
1.752 - * @brief Remove a hook manager. Auto-removes all hooks attached to it from plugin plug.
1.753 - *
1.754 - * @param plug The owner of the hook manager
1.755 - * @param pubFunc The hook manager's info function
1.756 - */
1.757 - virtual void RemoveHookManager(Plugin plug, HookManagerPubFunc pubFunc);
1.758 - virtual void RemoveHookManager(RemoveHookManInfo info);
1.759 -
1.760 - virtual void DoRecall(); //!< Initiates a recall sequence
1.761 - virtual void *GetOverrideRetPtr(); //!< Returns the pointer set by SetOverrideRetPtr
1.762 -
1.763 - virtual void *SetupHookLoop(META_RES *statusPtr, META_RES *prevResPtr, META_RES *curResPtr,
1.764 - void **ifacePtrPtr, const void *origRetPtr, void *overrideRetPtr);
1.765 -
1.766 - /**
1.767 - * @brief Add a (VP) hook.
1.768 - *
1.769 - * @return non-zero hook id on success, 0 otherwise
1.770 - *
1.771 - * @param plug The unique identifier of the plugin that calls this function
1.772 - * @param mode Can be either Hook_Normal or Hook_VP (vtable-wide hook)
1.773 - * @param iface The interface pointer
1.774 - * The representative interface pointer for VP hooks
1.775 - * The vtable pointer for direct VP hooks !!!
1.776 - * @param ifacesize The size of the class iface points to
1.777 - * @param myHookMan A hook manager function that should be capable of handling the function
1.778 - * @param handler A pointer to a FastDelegate containing the hook handler
1.779 - * @param post Set to true if you want a post handler
1.780 - */
1.781 - virtual int AddHookNew(Plugin plug, AddHookMode mode, void *iface, int thisptr_offs, HookManagerPubFunc myHookMan,
1.782 - ISHDelegate *handler, bool post);
1.783 -
1.784 - /**
1.785 - * @brief Remove a VP hook by ID.
1.786 - *
1.787 - * @return true on success, false otherwise
1.788 - *
1.789 - * @param plug The unique identifier of the plugin that calls this function
1.790 - * @param hookid The hook id (returned by AddHookNew)
1.791 - */
1.792 - virtual bool RemoveHookByID(Plugin plug, int hookid);
1.793 -
1.794 - /**
1.795 - * @brief Makes sure that hooks are going to be ignored on the next call of vfnptr
1.796 - *
1.797 - * @param plug The unique identifier of the plugin that calls this function
1.798 - * @param vfnptr The virtual function pointer of the function in question
1.799 - */
1.800 - virtual void SetIgnoreHooks(Plugin plug, void *vfnptr);
1.801 -
1.802 - /**
1.803 - * @brief Reverses SetIgnoreHooks' effect
1.804 - *
1.805 - * @param plug The unique identifier of the plugin that calls this function
1.806 - * @param vfnptr The virtual function pointer of the function in question
1.807 - */
1.808 - virtual void ResetIgnoreHooks(Plugin plug, void *vfnptr);
1.809 -
1.810 - /**
1.811 - * @brief Finds the original entry of a virtual function pointer
1.812 - *
1.813 - * @param vfnptr The virtual function pointer
1.814 - * @return The original entry if the virtual function pointer has been patched; NULL otherwise.
1.815 - */
1.816 - virtual void *GetOrigVfnPtrEntry(void *vfnptr);
1.817 - };
1.818 -}
1.819 -
1.820 -#endif