sourcehook/sourcehook_impl_chookmaninfo.h
author Pavol Marko <pavolmarko@gmail.com>
Sat Nov 17 09:53:58 2007 +0000 (2007-11-17)
branchhookman_autogen
changeset 461 2fa049f36721
parent 436 41f21d77c3c1
permissions -rw-r--r--
New hookfunc local vars code; first step to vafmt support
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2007 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 #include "sourcehook_hookmangen.h"
    17 
    18 namespace SourceHook
    19 {
    20 	namespace Impl
    21 	{
    22 		class CVfnPtr;
    23 		
    24 		class CHookManager : public IHookManagerInfo
    25 		{
    26 			// *** Data ***
    27 			Plugin m_OwnerPlugin;
    28 			HookManagerPubFunc m_PubFunc;
    29 			int m_VtblOffs;
    30 			int m_VtblIdx;
    31 			CProto m_Proto;
    32 			int m_Version;
    33 
    34 			void *m_HookfuncVfnptr;
    35 
    36 			List<CVfnPtr*> m_VfnPtrs;
    37 		public:
    38 			// *** Descriptor ***
    39 			struct Descriptor
    40 			{
    41 				Plugin m_OwnerPlugin;
    42 				HookManagerPubFunc m_PubFunc;
    43 				Descriptor(Plugin ownerPlugin, HookManagerPubFunc pubFunc)
    44 					: m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc)
    45 				{
    46 				}
    47 			};
    48 
    49 			// *** Interface ***
    50 			inline CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc);
    51 
    52 			inline bool operator==(const Descriptor &other) const;
    53 			inline bool operator==(const CHookManager &other) const;
    54 
    55 			inline operator bool()  const;
    56 			inline Plugin GetOwnerPlugin()  const;
    57 			inline int GetVtblOffs()  const;
    58 			inline int GetVtblIdx()  const;
    59 			inline const CProto &GetProto()  const;
    60 			inline int GetVersion()  const;
    61 			inline void *GetHookFunc() const;
    62 			inline HookManagerPubFunc GetPubFunc() const;
    63 
    64 			inline void Register();
    65 			inline void Unregister();
    66 
    67 			inline void IncrRef(CVfnPtr *pVfnPtr);
    68 			inline void DecrRef(CVfnPtr *pVfnPtr);
    69 
    70 			List<CVfnPtr*> &GetVfnPtrList()
    71 			{
    72 				return m_VfnPtrs;
    73 			}
    74 
    75 			// *** IHookManagerInfo interface ***
    76 			void SetInfo(int hookman_version, int vtbloffs, int vtblidx,
    77 				ProtoInfo *proto, void *hookfunc_vfnptr);
    78 		};
    79 
    80 		class CHookManList : public List<CHookManager>
    81 		{
    82 		public:
    83 			inline CHookManager *GetHookMan(Plugin plug, HookManagerPubFunc pubFunc);
    84 			inline CHookManager *GetHookMan(CHookManager &hm);
    85 		};
    86 
    87 		// *** Implementation ***
    88 		inline CHookManager::CHookManager(Plugin ownerPlugin, HookManagerPubFunc pubFunc)
    89 			: m_OwnerPlugin(ownerPlugin), m_PubFunc(pubFunc), m_Version(-1)
    90 		{
    91 			// Query pubfunc
    92 			//  -> Should call SetInfo and set all the other variables!
    93 			if (m_PubFunc(false, this) != 0)
    94 			{
    95 				// Error!
    96 				m_Version = -1;
    97 			}
    98 		}
    99 
   100 		inline CHookManager::operator bool() const
   101 		{
   102 			return m_Version != -1;
   103 		}
   104 
   105 		inline bool CHookManager::operator==(const Descriptor &other) const
   106 		{
   107 			return m_OwnerPlugin == other.m_OwnerPlugin
   108 				&& m_PubFunc == other.m_PubFunc;
   109 		}
   110 
   111 		inline bool CHookManager::operator==(const CHookManager &other) const
   112 		{
   113 			return m_OwnerPlugin == other.m_OwnerPlugin
   114 				&& m_PubFunc == other.m_PubFunc;
   115 		}
   116 
   117 		inline Plugin CHookManager::GetOwnerPlugin() const
   118 		{
   119 			return m_OwnerPlugin;
   120 		}
   121 
   122 		inline int CHookManager::GetVtblOffs() const
   123 		{
   124 			return m_VtblOffs;
   125 		}
   126 
   127 		inline int CHookManager::GetVtblIdx() const
   128 		{
   129 			return m_VtblIdx;
   130 		}
   131 
   132 		inline const CProto &CHookManager::GetProto() const
   133 		{
   134 			return m_Proto;
   135 		}
   136 
   137 		inline int CHookManager::GetVersion() const
   138 		{
   139 			return m_Version;
   140 		}
   141 
   142 		inline void *CHookManager::GetHookFunc() const
   143 		{
   144 			return *reinterpret_cast<void**>(m_HookfuncVfnptr);
   145 		}
   146 
   147 		inline HookManagerPubFunc CHookManager::GetPubFunc() const
   148 		{
   149 			return m_PubFunc;
   150 		}
   151 
   152 		inline void CHookManager::Register()
   153 		{
   154 			m_PubFunc(true, this);
   155 		}
   156 
   157 		inline void CHookManager::Unregister()
   158 		{
   159 			m_PubFunc(true, NULL);
   160 		}
   161 
   162 		inline void CHookManager::IncrRef(CVfnPtr *pVfnPtr)
   163 		{
   164 			m_VfnPtrs.push_back(pVfnPtr);
   165 			if (m_VfnPtrs.size() == 1)
   166 				Register();
   167 		}
   168 
   169 		inline void CHookManager::DecrRef(CVfnPtr *pVfnPtr)
   170 		{
   171 			m_VfnPtrs.remove(pVfnPtr);
   172 			if (m_VfnPtrs.empty())
   173 				Unregister();
   174 		}
   175 
   176 		inline CHookManager *CHookManList::GetHookMan(Plugin plug, HookManagerPubFunc pubFunc)
   177 		{
   178 			CHookManager hm(plug, pubFunc);
   179 			return GetHookMan(hm);
   180 		}
   181 
   182 		inline CHookManager *CHookManList::GetHookMan(CHookManager &hm)
   183 		{
   184 			iterator iter = find(hm);
   185 			if (iter == end())
   186 			{
   187 				push_back(hm);
   188 				return &(back());
   189 			}
   190 			else
   191 			{
   192 				return &(*iter);
   193 			}
   194 		}
   195 	}
   196 }
   197 
   198 #endif
   199