core/sourcehook/sourcehook_impl_chookidman.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Apr 22 21:54:21 2013 -0500 (2013-04-22)
changeset 834 3cabb288e70a
parent 733 2365240861a2
permissions -rw-r--r--
Triggering a build.
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2010 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * Contributors: Scott "Damaged Soul" Ehlert
     9 * ============================
    10 */
    11 
    12 #include "sourcehook_impl.h"
    13 
    14 namespace SourceHook
    15 {
    16 	namespace Impl
    17 	{
    18 		CHookIDManager::CHookIDManager()
    19 		{
    20 		}
    21 
    22 		int CHookIDManager::New(const CProto &proto, int vtbl_offs, int vtbl_idx, void *vfnptr,
    23 			void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
    24 		{
    25 			Entry tmp(proto, vtbl_offs, vtbl_idx, vfnptr, adjustediface, plug, thisptr_offs, handler, post);
    26 
    27 			size_t cursize = m_Entries.size();
    28 			for (size_t i = 0; i < cursize; ++i)
    29 			{
    30 				if (m_Entries[i].isfree)
    31 				{
    32 					m_Entries[i] = tmp;
    33 					return static_cast<int>(i) + 1;
    34 				}
    35 			}
    36 
    37 			m_Entries.push_back(tmp);
    38 			return static_cast<int>(m_Entries.size());		// return size() because hookid = id+1 anyway
    39 		}
    40 
    41 		bool CHookIDManager::Remove(int hookid)
    42 		{
    43 			int realid = hookid - 1;
    44 			if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
    45 				return false;
    46 
    47 			m_Entries[realid].isfree = true;
    48 
    49 			// :TODO: remove free ids from back sometimes ??
    50 
    51 			return true;
    52 		}
    53 
    54 		const CHookIDManager::Entry * CHookIDManager::QueryHook(int hookid)
    55 		{
    56 			int realid = hookid - 1;
    57 			if (realid < 0 || realid >= static_cast<int>(m_Entries.size()) || m_Entries[realid].isfree)
    58 				return NULL;
    59 
    60 			return &m_Entries[realid];
    61 		}
    62 
    63 		void CHookIDManager::FindAllHooks(CVector<int> &output, const CProto &proto, int vtbl_offs,
    64 			int vtbl_idx, void *adjustediface, Plugin plug, int thisptr_offs, ISHDelegate *handler, bool post)
    65 		{
    66 			// oh my god, a lot of parameters...
    67 			size_t cursize = m_Entries.size();
    68 			for (size_t i = 0; i < cursize; ++i)
    69 			{
    70 				if (!m_Entries[i].isfree && m_Entries[i].proto == proto && m_Entries[i].vtbl_offs == vtbl_offs &&
    71 					m_Entries[i].vtbl_idx == vtbl_idx && m_Entries[i].adjustediface == adjustediface && m_Entries[i].plug == plug &&
    72 					m_Entries[i].thisptr_offs == thisptr_offs && m_Entries[i].handler->IsEqual(handler) && m_Entries[i].post == post)
    73 				{
    74 					output.push_back(static_cast<int>(i) + 1);
    75 				}
    76 			}
    77 		}
    78 
    79 		void CHookIDManager::FindAllHooks(CVector<int> &output)
    80 		{
    81 			size_t cursize = m_Entries.size();
    82 			for (size_t i = 0; i < cursize; ++i)
    83 			{
    84 				if (!m_Entries[i].isfree)
    85 					output.push_back(static_cast<int>(i) + 1);
    86 			}
    87 		}
    88 
    89 		void CHookIDManager::FindAllHooks(CVector<int> &output, Plugin plug)
    90 		{
    91 			size_t cursize = m_Entries.size();
    92 			for (size_t i = 0; i < cursize; ++i)
    93 			{
    94 				if (!m_Entries[i].isfree && m_Entries[i].plug == plug)
    95 					output.push_back(static_cast<int>(i) + 1);
    96 			}
    97 		}
    98 
    99 
   100 		void CHookIDManager::RemoveAll(void *vfnptr)
   101 		{
   102 			size_t cursize = m_Entries.size();
   103 			for (size_t i = 0; i < cursize; ++i)
   104 			{
   105 				if (!m_Entries[i].isfree && m_Entries[i].vfnptr == vfnptr)
   106 					m_Entries[i].isfree = true;
   107 			}
   108 		}
   109 
   110 	}
   111 }