ds@658: /* ======== SourceHook ======== ds@658: * Copyright (C) 2004-2008 Metamod:Source Development Team ds@658: * No warranties of any kind ds@658: * ds@658: * License: zlib/libpng ds@658: * ds@658: * Author(s): Pavol "PM OnoTo" Marko ds@658: * ============================ ds@658: */ ds@658: ds@658: #ifndef __SOURCEHOOK_IMPL_CHOOKMANINFO_H__ ds@658: #define __SOURCEHOOK_IMPL_CHOOKMANINFO_H__ ds@658: ds@658: #include "sh_list.h" ds@658: #include "sourcehook_impl_cproto.h" ds@658: ds@658: namespace SourceHook ds@658: { ds@658: namespace Impl ds@658: { ds@658: class CVfnPtr; ds@658: ds@658: class CHookManager : public IHookManagerInfo ds@658: { ds@658: // *** Data *** ds@658: Plugin m_OwnerPlugin; ds@658: HookManagerPubFunc m_PubFunc; ds@658: int m_VtblOffs; ds@658: int m_VtblIdx; ds@658: CProto m_Proto; ds@658: int m_Version; ds@658: ds@658: void *m_HookfuncVfnptr; ds@658: ds@658: List m_VfnPtrs; ds@658: public: ds@658: // *** Descriptor *** ds@658: struct Descriptor ds@658: { ds@658: Plugin m_OwnerPlugin; ds@658: HookManagerPubFunc m_PubFunc; ds@658: Descriptor(Plugin ownerPlugin, HookManagerPubFunc pubFunc) ds@658: : m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc) ds@658: { ds@658: } ds@658: }; ds@658: ds@658: // *** Interface *** ds@658: inline CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc); ds@658: ds@658: inline bool operator==(const Descriptor &other) const; ds@658: inline bool operator==(const CHookManager &other) const; ds@658: ds@658: inline operator bool() const; ds@658: inline Plugin GetOwnerPlugin() const; ds@658: inline int GetVtblOffs() const; ds@658: inline int GetVtblIdx() const; ds@658: inline const CProto &GetProto() const; ds@658: inline int GetVersion() const; ds@658: inline void *GetHookFunc() const; ds@658: inline HookManagerPubFunc GetPubFunc() const; ds@658: ds@658: inline void Register(); ds@658: inline void Unregister(); ds@658: ds@658: inline void IncrRef(CVfnPtr *pVfnPtr); ds@658: inline void DecrRef(CVfnPtr *pVfnPtr); ds@658: ds@658: List &GetVfnPtrList() ds@658: { ds@658: return m_VfnPtrs; ds@658: } ds@658: ds@658: // *** IHookManagerInfo interface *** pavolmarko@380: void SetInfo(int hookman_version, int vtbloffs, int vtblidx, ds@658: ProtoInfo *proto, void *hookfunc_vfnptr); ds@658: }; ds@658: ds@658: class CHookManList : public List ds@658: { ds@658: public: ds@658: inline CHookManager *GetHookMan(Plugin plug, HookManagerPubFunc pubFunc); ds@658: inline CHookManager *GetHookMan(CHookManager &hm); ds@658: }; ds@658: ds@658: // *** Implementation *** ds@658: inline CHookManager::CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc) ds@658: : m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc), m_Version(-1) ds@658: { ds@658: // Query pubfunc ds@658: // -> Should call SetInfo and set all the other variables! ds@658: if (m_PubFunc(false, this) != 0) ds@658: { ds@658: // Error! ds@658: m_Version = -1; ds@658: } ds@658: } ds@658: ds@658: inline CHookManager::operator bool() const ds@658: { ds@658: return m_Version != -1; ds@658: } ds@658: ds@658: inline bool CHookManager::operator==(const Descriptor &other) const ds@658: { ds@658: return m_OwnerPlugin == other.m_OwnerPlugin ds@658: && m_PubFunc == other.m_PubFunc; ds@658: } ds@658: ds@658: inline bool CHookManager::operator==(const CHookManager &other) const ds@658: { ds@658: return m_OwnerPlugin == other.m_OwnerPlugin ds@658: && m_PubFunc == other.m_PubFunc; ds@658: } ds@658: ds@658: inline Plugin CHookManager::GetOwnerPlugin() const ds@658: { ds@658: return m_OwnerPlugin; ds@658: } ds@658: ds@658: inline int CHookManager::GetVtblOffs() const ds@658: { ds@658: return m_VtblOffs; ds@658: } ds@658: ds@658: inline int CHookManager::GetVtblIdx() const ds@658: { ds@658: return m_VtblIdx; ds@658: } ds@658: ds@658: inline const CProto &CHookManager::GetProto() const ds@658: { ds@658: return m_Proto; ds@658: } ds@658: ds@658: inline int CHookManager::GetVersion() const ds@658: { ds@658: return m_Version; ds@658: } ds@658: ds@658: inline void *CHookManager::GetHookFunc() const ds@658: { ds@658: return *reinterpret_cast(m_HookfuncVfnptr); ds@658: } ds@658: ds@658: inline HookManagerPubFunc CHookManager::GetPubFunc() const ds@658: { ds@658: return m_PubFunc; ds@658: } ds@658: ds@658: inline void CHookManager::Register() ds@658: { ds@658: m_PubFunc(true, this); ds@658: } ds@658: ds@658: inline void CHookManager::Unregister() ds@658: { ds@658: m_PubFunc(true, NULL); ds@658: } ds@658: ds@658: inline void CHookManager::IncrRef(CVfnPtr *pVfnPtr) ds@658: { ds@658: m_VfnPtrs.push_back(pVfnPtr); ds@658: if (m_VfnPtrs.size() == 1) ds@658: Register(); ds@658: } ds@658: ds@658: inline void CHookManager::DecrRef(CVfnPtr *pVfnPtr) ds@658: { ds@658: m_VfnPtrs.remove(pVfnPtr); ds@658: if (m_VfnPtrs.empty()) ds@658: Unregister(); ds@658: } ds@658: ds@658: inline CHookManager *CHookManList::GetHookMan(Plugin plug, HookManagerPubFunc pubFunc) ds@658: { ds@658: CHookManager hm(plug, pubFunc); ds@658: return GetHookMan(hm); ds@658: } ds@658: ds@658: inline CHookManager *CHookManList::GetHookMan(CHookManager &hm) ds@658: { ds@658: iterator iter = find(hm); ds@658: if (iter == end()) ds@658: { ds@658: push_back(hm); ds@658: return &(back()); ds@658: } ds@658: else ds@658: { ds@658: return &(*iter); ds@658: } ds@658: } ds@658: } ds@658: } ds@658: pavolmarko@381: #endif pavolmarko@381: