1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/sourcehook/sourcehook_impl_chookmaninfo.h Fri Nov 14 04:04:02 2008 -0600
1.3 @@ -0,0 +1,198 @@
1.4 +/* ======== SourceHook ========
1.5 +* Copyright (C) 2004-2008 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_CHOOKMANINFO_H__
1.15 +#define __SOURCEHOOK_IMPL_CHOOKMANINFO_H__
1.16 +
1.17 +#include "sh_list.h"
1.18 +#include "sourcehook_impl_cproto.h"
1.19 +
1.20 +namespace SourceHook
1.21 +{
1.22 + namespace Impl
1.23 + {
1.24 + class CVfnPtr;
1.25 +
1.26 + class CHookManager : public IHookManagerInfo
1.27 + {
1.28 + // *** Data ***
1.29 + Plugin m_OwnerPlugin;
1.30 + HookManagerPubFunc m_PubFunc;
1.31 + int m_VtblOffs;
1.32 + int m_VtblIdx;
1.33 + CProto m_Proto;
1.34 + int m_Version;
1.35 +
1.36 + void *m_HookfuncVfnptr;
1.37 +
1.38 + List<CVfnPtr*> m_VfnPtrs;
1.39 + public:
1.40 + // *** Descriptor ***
1.41 + struct Descriptor
1.42 + {
1.43 + Plugin m_OwnerPlugin;
1.44 + HookManagerPubFunc m_PubFunc;
1.45 + Descriptor(Plugin ownerPlugin, HookManagerPubFunc pubFunc)
1.46 + : m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc)
1.47 + {
1.48 + }
1.49 + };
1.50 +
1.51 + // *** Interface ***
1.52 + inline CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc);
1.53 +
1.54 + inline bool operator==(const Descriptor &other) const;
1.55 + inline bool operator==(const CHookManager &other) const;
1.56 +
1.57 + inline operator bool() const;
1.58 + inline Plugin GetOwnerPlugin() const;
1.59 + inline int GetVtblOffs() const;
1.60 + inline int GetVtblIdx() const;
1.61 + inline const CProto &GetProto() const;
1.62 + inline int GetVersion() const;
1.63 + inline void *GetHookFunc() const;
1.64 + inline HookManagerPubFunc GetPubFunc() const;
1.65 +
1.66 + inline void Register();
1.67 + inline void Unregister();
1.68 +
1.69 + inline void IncrRef(CVfnPtr *pVfnPtr);
1.70 + inline void DecrRef(CVfnPtr *pVfnPtr);
1.71 +
1.72 + List<CVfnPtr*> &GetVfnPtrList()
1.73 + {
1.74 + return m_VfnPtrs;
1.75 + }
1.76 +
1.77 + // *** IHookManagerInfo interface ***
1.78 + void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
1.79 + ProtoInfo *proto, void *hookfunc_vfnptr);
1.80 + };
1.81 +
1.82 + class CHookManList : public List<CHookManager>
1.83 + {
1.84 + public:
1.85 + inline CHookManager *GetHookMan(Plugin plug, HookManagerPubFunc pubFunc);
1.86 + inline CHookManager *GetHookMan(CHookManager &hm);
1.87 + };
1.88 +
1.89 + // *** Implementation ***
1.90 + inline CHookManager::CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc)
1.91 + : m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc), m_Version(-1)
1.92 + {
1.93 + // Query pubfunc
1.94 + // -> Should call SetInfo and set all the other variables!
1.95 + if (m_PubFunc(false, this) != 0)
1.96 + {
1.97 + // Error!
1.98 + m_Version = -1;
1.99 + }
1.100 + }
1.101 +
1.102 + inline CHookManager::operator bool() const
1.103 + {
1.104 + return m_Version != -1;
1.105 + }
1.106 +
1.107 + inline bool CHookManager::operator==(const Descriptor &other) const
1.108 + {
1.109 + return m_OwnerPlugin == other.m_OwnerPlugin
1.110 + && m_PubFunc == other.m_PubFunc;
1.111 + }
1.112 +
1.113 + inline bool CHookManager::operator==(const CHookManager &other) const
1.114 + {
1.115 + return m_OwnerPlugin == other.m_OwnerPlugin
1.116 + && m_PubFunc == other.m_PubFunc;
1.117 + }
1.118 +
1.119 + inline Plugin CHookManager::GetOwnerPlugin() const
1.120 + {
1.121 + return m_OwnerPlugin;
1.122 + }
1.123 +
1.124 + inline int CHookManager::GetVtblOffs() const
1.125 + {
1.126 + return m_VtblOffs;
1.127 + }
1.128 +
1.129 + inline int CHookManager::GetVtblIdx() const
1.130 + {
1.131 + return m_VtblIdx;
1.132 + }
1.133 +
1.134 + inline const CProto &CHookManager::GetProto() const
1.135 + {
1.136 + return m_Proto;
1.137 + }
1.138 +
1.139 + inline int CHookManager::GetVersion() const
1.140 + {
1.141 + return m_Version;
1.142 + }
1.143 +
1.144 + inline void *CHookManager::GetHookFunc() const
1.145 + {
1.146 + return *reinterpret_cast<void**>(m_HookfuncVfnptr);
1.147 + }
1.148 +
1.149 + inline HookManagerPubFunc CHookManager::GetPubFunc() const
1.150 + {
1.151 + return m_PubFunc;
1.152 + }
1.153 +
1.154 + inline void CHookManager::Register()
1.155 + {
1.156 + m_PubFunc(true, this);
1.157 + }
1.158 +
1.159 + inline void CHookManager::Unregister()
1.160 + {
1.161 + m_PubFunc(true, NULL);
1.162 + }
1.163 +
1.164 + inline void CHookManager::IncrRef(CVfnPtr *pVfnPtr)
1.165 + {
1.166 + m_VfnPtrs.push_back(pVfnPtr);
1.167 + if (m_VfnPtrs.size() == 1)
1.168 + Register();
1.169 + }
1.170 +
1.171 + inline void CHookManager::DecrRef(CVfnPtr *pVfnPtr)
1.172 + {
1.173 + m_VfnPtrs.remove(pVfnPtr);
1.174 + if (m_VfnPtrs.empty())
1.175 + Unregister();
1.176 + }
1.177 +
1.178 + inline CHookManager *CHookManList::GetHookMan(Plugin plug, HookManagerPubFunc pubFunc)
1.179 + {
1.180 + CHookManager hm(plug, pubFunc);
1.181 + return GetHookMan(hm);
1.182 + }
1.183 +
1.184 + inline CHookManager *CHookManList::GetHookMan(CHookManager &hm)
1.185 + {
1.186 + iterator iter = find(hm);
1.187 + if (iter == end())
1.188 + {
1.189 + push_back(hm);
1.190 + return &(back());
1.191 + }
1.192 + else
1.193 + {
1.194 + return &(*iter);
1.195 + }
1.196 + }
1.197 + }
1.198 +}
1.199 +
1.200 +#endif
1.201 +