1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/sourcehook/sourcehook_impl_chookidman.cpp Fri Dec 17 22:22:44 2010 -0500
1.3 @@ -0,0 +1,111 @@
1.4 +/* ======== SourceHook ========
1.5 +* Copyright (C) 2004-2010 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 +* Contributors: Scott "Damaged Soul" Ehlert
1.12 +* ============================
1.13 +*/
1.14 +
1.15 +#include "sourcehook_impl.h"
1.16 +
1.17 +namespace SourceHook
1.18 +{
1.19 + namespace Impl
1.20 + {
1.21 + CHookIDManager::CHookIDManager()
1.22 + {
1.23 + }
1.24 +
1.25 + int CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
1.26 + void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
1.27 + {
1.28 + Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
1.29 +
1.30 + size_t cursize = m_Entries.size();
1.31 + for (size_t i = 0; i < cursize; ++i)
1.32 + {
1.33 + if (m_Entries[i].isfree)
1.34 + {
1.35 + m_Entries[i] = tmp;
1.36 + return static_cast<int>(i) + 1;
1.37 + }
1.38 + }
1.39 +
1.40 + m_Entries.push_back(tmp);
1.41 + return static_cast<int>(m_Entries.size()); // return size() because hookid = id+1 anyway
1.42 + }
1.43 +
1.44 + bool CHookIDManager::Remove(int hookid)
1.45 + {
1.46 + int realid = hookid - 1;
1.47 + if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
1.48 + return false;
1.49 +
1.50 + m_Entries[realid].isfree = true;
1.51 +
1.52 + // :TODO: remove free ids from back sometimes ??
1.53 +
1.54 + return true;
1.55 + }
1.56 +
1.57 + const CHookIDManager::Entry * CHookIDManager::QueryHook(int hookid)
1.58 + {
1.59 + int realid = hookid - 1;
1.60 + if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
1.61 + return NULL;
1.62 +
1.63 + return &m_Entries[realid];
1.64 + }
1.65 +
1.66 + void CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
1.67 + int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
1.68 + {
1.69 + // oh my god, a lot of parameters...
1.70 + size_t cursize = m_Entries.size();
1.71 + for (size_t i = 0; i < cursize; ++i)
1.72 + {
1.73 + if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
1.74 + m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
1.75 + m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
1.76 + {
1.77 + output.push_back(static_cast<int>(i) + 1);
1.78 + }
1.79 + }
1.80 + }
1.81 +
1.82 + void CHookIDManager::FindAllHooks(CVector<int> &output)
1.83 + {
1.84 + size_t cursize = m_Entries.size();
1.85 + for (size_t i = 0; i < cursize; ++i)
1.86 + {
1.87 + if (!m_Entries[i].isfree)
1.88 + output.push_back(static_cast<int>(i) + 1);
1.89 + }
1.90 + }
1.91 +
1.92 + void CHookIDManager::FindAllHooks(CVector<int> &output, Plugin plug)
1.93 + {
1.94 + size_t cursize = m_Entries.size();
1.95 + for (size_t i = 0; i < cursize; ++i)
1.96 + {
1.97 + if (!m_Entries[i].isfree && m_Entries[i].plug == plug)
1.98 + output.push_back(static_cast<int>(i) + 1);
1.99 + }
1.100 + }
1.101 +
1.102 +
1.103 + void CHookIDManager::RemoveAll(void *vfnptr)
1.104 + {
1.105 + size_t cursize = m_Entries.size();
1.106 + for (size_t i = 0; i < cursize; ++i)
1.107 + {
1.108 + if (!m_Entries[i].isfree && m_Entries[i].vfnptr == vfnptr)
1.109 + m_Entries[i].isfree = true;
1.110 + }
1.111 + }
1.112 +
1.113 + }
1.114 +}