core/sourcehook/sourcehook_impl_chookmaninfo.h
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 658 8353605c1d97
child 733 2365240861a2
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

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