core/sourcehook/sourcehook_impl_chookmaninfo.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 15 23:46:27 2008 -0600 (2008-11-15)
changeset 552 4b73ffd6e176
parent 539 db914129ae01
child 658 8353605c1d97
permissions -rw-r--r--
Initial commit of vsp bridge for new core. Not tested, barely compiles.
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2008 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 #ifndef __SOURCEHOOK_IMPL_CHOOKMANINFO_H__
    12 #define __SOURCEHOOK_IMPL_CHOOKMANINFO_H__
    13 
    14 #include "sh_list.h"
    15 #include "sourcehook_impl_cproto.h"
    16 
    17 namespace SourceHook
    18 {
    19 	namespace Impl
    20 	{
    21 		class CVfnPtr;
    22 		
    23 		class CHookManager : public IHookManagerInfo
    24 		{
    25 			// *** Data ***
    26 			Plugin m_OwnerPlugin;
    27 			HookManagerPubFunc m_PubFunc;
    28 			int m_VtblOffs;
    29 			int m_VtblIdx;
    30 			CProto m_Proto;
    31 			int m_Version;
    32 
    33 			void *m_HookfuncVfnptr;
    34 
    35 			List<CVfnPtr*> m_VfnPtrs;
    36 		public:
    37 			// *** Descriptor ***
    38 			struct Descriptor
    39 			{
    40 				Plugin m_OwnerPlugin;
    41 				HookManagerPubFunc m_PubFunc;
    42 				Descriptor(Plugin ownerPlugin, HookManagerPubFunc pubFunc)
    43 					: m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc)
    44 				{
    45 				}
    46 			};
    47 
    48 			// *** Interface ***
    49 			inline CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc);
    50 
    51 			inline bool operator==(const Descriptor &other) const;
    52 			inline bool operator==(const CHookManager &other) const;
    53 
    54 			inline operator bool()  const;
    55 			inline Plugin GetOwnerPlugin()  const;
    56 			inline int GetVtblOffs()  const;
    57 			inline int GetVtblIdx()  const;
    58 			inline const CProto &GetProto()  const;
    59 			inline int GetVersion()  const;
    60 			inline void *GetHookFunc() const;
    61 			inline HookManagerPubFunc GetPubFunc() const;
    62 
    63 			inline void Register();
    64 			inline void Unregister();
    65 
    66 			inline void IncrRef(CVfnPtr *pVfnPtr);
    67 			inline void DecrRef(CVfnPtr *pVfnPtr);
    68 
    69 			List<CVfnPtr*> &GetVfnPtrList()
    70 			{
    71 				return m_VfnPtrs;
    72 			}
    73 
    74 			// *** IHookManagerInfo interface ***
    75 			void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
    76 				ProtoInfo *proto, void *hookfunc_vfnptr);
    77 		};
    78 
    79 		class CHookManList : public List<CHookManager>
    80 		{
    81 		public:
    82 			inline CHookManager *GetHookMan(Plugin plug, HookManagerPubFunc pubFunc);
    83 			inline CHookManager *GetHookMan(CHookManager &hm);
    84 		};
    85 
    86 		// *** Implementation ***
    87 		inline CHookManager::CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc)
    88 			: m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc), m_Version(-1)
    89 		{
    90 			// Query pubfunc
    91 			//  -> Should call SetInfo and set all the other variables!
    92 			if (m_PubFunc(false, this) != 0)
    93 			{
    94 				// Error!
    95 				m_Version = -1;
    96 			}
    97 		}
    98 
    99 		inline CHookManager::operator bool() const
   100 		{
   101 			return m_Version != -1;
   102 		}
   103 
   104 		inline bool CHookManager::operator==(const Descriptor &other) const
   105 		{
   106 			return m_OwnerPlugin == other.m_OwnerPlugin
   107 				&& m_PubFunc == other.m_PubFunc;
   108 		}
   109 
   110 		inline bool CHookManager::operator==(const CHookManager &other) const
   111 		{
   112 			return m_OwnerPlugin == other.m_OwnerPlugin
   113 				&& m_PubFunc == other.m_PubFunc;
   114 		}
   115 
   116 		inline Plugin CHookManager::GetOwnerPlugin() const
   117 		{
   118 			return m_OwnerPlugin;
   119 		}
   120 
   121 		inline int CHookManager::GetVtblOffs() const
   122 		{
   123 			return m_VtblOffs;
   124 		}
   125 
   126 		inline int CHookManager::GetVtblIdx() const
   127 		{
   128 			return m_VtblIdx;
   129 		}
   130 
   131 		inline const CProto &CHookManager::GetProto() const
   132 		{
   133 			return m_Proto;
   134 		}
   135 
   136 		inline int CHookManager::GetVersion() const
   137 		{
   138 			return m_Version;
   139 		}
   140 
   141 		inline void *CHookManager::GetHookFunc() const
   142 		{
   143 			return *reinterpret_cast<void**>(m_HookfuncVfnptr);
   144 		}
   145 
   146 		inline HookManagerPubFunc CHookManager::GetPubFunc() const
   147 		{
   148 			return m_PubFunc;
   149 		}
   150 
   151 		inline void CHookManager::Register()
   152 		{
   153 			m_PubFunc(true, this);
   154 		}
   155 
   156 		inline void CHookManager::Unregister()
   157 		{
   158 			m_PubFunc(true, NULL);
   159 		}
   160 
   161 		inline void CHookManager::IncrRef(CVfnPtr *pVfnPtr)
   162 		{
   163 			m_VfnPtrs.push_back(pVfnPtr);
   164 			if (m_VfnPtrs.size() == 1)
   165 				Register();
   166 		}
   167 
   168 		inline void CHookManager::DecrRef(CVfnPtr *pVfnPtr)
   169 		{
   170 			m_VfnPtrs.remove(pVfnPtr);
   171 			if (m_VfnPtrs.empty())
   172 				Unregister();
   173 		}
   174 
   175 		inline CHookManager *CHookManList::GetHookMan(Plugin plug, HookManagerPubFunc pubFunc)
   176 		{
   177 			CHookManager hm(plug, pubFunc);
   178 			return GetHookMan(hm);
   179 		}
   180 
   181 		inline CHookManager *CHookManList::GetHookMan(CHookManager &hm)
   182 		{
   183 			iterator iter = find(hm);
   184 			if (iter == end())
   185 			{
   186 				push_back(hm);
   187 				return &(back());
   188 			}
   189 			else
   190 			{
   191 				return &(*iter);
   192 			}
   193 		}
   194 	}
   195 }
   196 
   197 #endif
   198