This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIClientEntityList.h
executable file
·276 lines (226 loc) · 6.66 KB
/
IClientEntityList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#pragma once
#include "C_BaseEntity.h"
#define NUM_ENT_ENTRY_BITS (11 + 2)
#define NUM_ENT_ENTRIES (1 << NUM_ENT_ENTRY_BITS)
#define INVALID_EHANDLE_INDEX 0xFFFFFFFF
#define NUM_SERIAL_NUM_BITS 16 // (32 - NUM_ENT_ENTRY_BITS)
#define NUM_SERIAL_NUM_SHIFT_BITS (32 - NUM_SERIAL_NUM_BITS)
#define ENT_ENTRY_MASK (( 1 << NUM_SERIAL_NUM_BITS) - 1)
class CBaseHandle;
class CBaseHandle
{
friend class CBaseEntityList;
public:
CBaseHandle();
CBaseHandle( const CBaseHandle &other );
CBaseHandle( unsigned long value );
CBaseHandle( int iEntry, int iSerialNumber );
void Init( int iEntry, int iSerialNumber );
void Term();
// Even if this returns true, Get() still can return return a non-null value.
// This just tells if the handle has been initted with any values.
bool IsValid() const;
int GetEntryIndex() const;
int GetSerialNumber() const;
int ToInt() const;
bool operator !=( const CBaseHandle &other ) const;
bool operator ==( const CBaseHandle &other ) const;
bool operator ==( const IHandleEntity* pEnt ) const;
bool operator !=( const IHandleEntity* pEnt ) const;
bool operator <( const CBaseHandle &other ) const;
bool operator <( const IHandleEntity* pEnt ) const;
// Assign a value to the handle.
const CBaseHandle& operator=( const IHandleEntity *pEntity );
const CBaseHandle& Set( const IHandleEntity *pEntity );
IHandleEntity* Get() const;
protected:
unsigned long m_Index;
};
template< class T >
class CHandle : public CBaseHandle
{
public:
CHandle();
CHandle( int iEntry, int iSerialNumber );
CHandle( const CBaseHandle &handle );
CHandle( T *pVal );
static CHandle<T> FromIndex( int index );
T* Get() const;
void Set( const T* pVal );
operator T*( );
operator T*( ) const;
bool operator !() const;
bool operator==( T *val ) const;
bool operator!=( T *val ) const;
const CBaseHandle& operator=( const T *val );
T* operator->() const;
};
template<class T>
CHandle<T>::CHandle()
{
}
template<class T>
CHandle<T>::CHandle( int iEntry, int iSerialNumber )
{
Init( iEntry, iSerialNumber );
}
template<class T>
CHandle<T>::CHandle( const CBaseHandle &handle )
: CBaseHandle( handle )
{
}
template<class T>
CHandle<T>::CHandle( T *pObj )
{
Term();
Set( pObj );
}
template<class T>
inline CHandle<T> CHandle<T>::FromIndex( int index )
{
CHandle<T> ret;
ret.m_Index = index;
return ret;
}
template<class T>
inline T* CHandle<T>::Get() const
{
return ( T* )CBaseHandle::Get();
}
template<class T>
inline bool CHandle<T>::operator !() const
{
return !Get();
}
template<class T>
inline bool CHandle<T>::operator==( T *val ) const
{
return Get() == val;
}
template<class T>
inline bool CHandle<T>::operator!=( T *val ) const
{
return Get() != val;
}
template<class T>
void CHandle<T>::Set( const T* pVal )
{
CBaseHandle::Set( reinterpret_cast<const IHandleEntity*>( pVal ) );
}
template<class T>
inline const CBaseHandle& CHandle<T>::operator=( const T *val )
{
Set( val );
return *this;
}
template<class T>
T* CHandle<T>::operator -> () const
{
return Get();
}
inline CBaseHandle::CBaseHandle()
{
m_Index = INVALID_EHANDLE_INDEX;
}
inline CBaseHandle::CBaseHandle( const CBaseHandle &other )
{
m_Index = other.m_Index;
}
inline CBaseHandle::CBaseHandle( unsigned long value )
{
m_Index = value;
}
inline CBaseHandle::CBaseHandle( int iEntry, int iSerialNumber )
{
Init( iEntry, iSerialNumber );
}
inline void CBaseHandle::Init( int iEntry, int iSerialNumber )
{
m_Index = ( unsigned long )( iEntry | ( iSerialNumber << NUM_SERIAL_NUM_SHIFT_BITS ) );
}
inline void CBaseHandle::Term()
{
m_Index = INVALID_EHANDLE_INDEX;
}
inline bool CBaseHandle::IsValid() const
{
return m_Index != INVALID_EHANDLE_INDEX;
}
inline int CBaseHandle::GetEntryIndex() const
{
// There is a hack here: due to a bug in the original implementation of the
// entity handle system, an attempt to look up an invalid entity index in
// certain cirumstances might fall through to the the mask operation below.
// This would mask an invalid index to be in fact a lookup of entity number
// NUM_ENT_ENTRIES, so invalid ent indexes end up actually looking up the
// last slot in the entities array. Since this slot is always empty, the
// lookup returns NULL and the expected behavior occurs through this unexpected
// route.
// A lot of code actually depends on this behavior, and the bug was only exposed
// after a change to NUM_SERIAL_NUM_BITS increased the number of allowable
// static props in the world. So the if-stanza below detects this case and
// retains the prior (bug-submarining) behavior.
if( !IsValid() )
return NUM_ENT_ENTRIES - 1;
return m_Index & ENT_ENTRY_MASK;
}
inline int CBaseHandle::GetSerialNumber() const
{
return m_Index >> NUM_SERIAL_NUM_SHIFT_BITS;
}
inline int CBaseHandle::ToInt() const
{
return ( int )m_Index;
}
inline bool CBaseHandle::operator !=( const CBaseHandle &other ) const
{
return m_Index != other.m_Index;
}
inline bool CBaseHandle::operator ==( const CBaseHandle &other ) const
{
return m_Index == other.m_Index;
}
inline bool CBaseHandle::operator ==( const IHandleEntity* pEnt ) const
{
return Get() == pEnt;
}
inline bool CBaseHandle::operator !=( const IHandleEntity* pEnt ) const
{
return Get() != pEnt;
}
inline bool CBaseHandle::operator <( const CBaseHandle &other ) const
{
return m_Index < other.m_Index;
}
inline bool CBaseHandle::operator <( const IHandleEntity *pEntity ) const
{
unsigned long otherIndex = ( pEntity ) ? pEntity->GetRefEHandle().m_Index : INVALID_EHANDLE_INDEX;
return m_Index < otherIndex;
}
inline const CBaseHandle& CBaseHandle::operator=( const IHandleEntity *pEntity )
{
return Set( pEntity );
}
inline const CBaseHandle& CBaseHandle::Set( const IHandleEntity *pEntity )
{
if( pEntity )
*this = pEntity->GetRefEHandle();
else
m_Index = INVALID_EHANDLE_INDEX;
return *this;
}
class IClientEntityList {
public:
C_BaseEntity* GetClientEntity(int index) {
typedef C_BaseEntity* (* oGetClientEntity)(void*, int);
return getvfunc<oGetClientEntity>(this, 3)(this, index);
}
C_BaseEntity* GetClientEntityFromHandle(CBaseHandle hHandle) {
typedef C_BaseEntity* (* oGetClientEntityFromHandle)(void*, CBaseHandle);
return getvfunc<oGetClientEntityFromHandle>(this, 4)(this, hHandle);
}
int GetHighestEntityIndex() {
typedef int (* oGetHighestEntityIndex)(void*);
return getvfunc<oGetHighestEntityIndex>(this, 6)(this);
}
};