1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-19 20:16:10 +08:00

Update CBaseHandle from CS:GO SDK (#120)

This commit is contained in:
sappho
2022-12-02 03:47:14 -05:00
committed by GitHub
parent 4209d92435
commit 361122610f
2 changed files with 20 additions and 6 deletions

View File

@ -1,4 +1,4 @@
//========= Copyright <EFBFBD> 1996-2005, Valve Corporation, All rights reserved. ============// //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
// //
// Purpose: // Purpose:
// //
@ -92,10 +92,10 @@ inline CBaseHandle::CBaseHandle( int iEntry, int iSerialNumber )
inline void CBaseHandle::Init( int iEntry, int iSerialNumber ) inline void CBaseHandle::Init( int iEntry, int iSerialNumber )
{ {
Assert( iEntry >= 0 && iEntry < NUM_ENT_ENTRIES ); Assert( iEntry >= 0 && (iEntry & ENT_ENTRY_MASK) == iEntry);
Assert( iSerialNumber >= 0 && iSerialNumber < (1 << NUM_SERIAL_NUM_BITS) ); Assert( iSerialNumber >= 0 && iSerialNumber < (1 << NUM_SERIAL_NUM_BITS) );
m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS); m_Index = iEntry | (iSerialNumber << NUM_SERIAL_NUM_SHIFT_BITS);
} }
inline void CBaseHandle::Term() inline void CBaseHandle::Term()
@ -110,12 +110,26 @@ inline bool CBaseHandle::IsValid() const
inline int CBaseHandle::GetEntryIndex() const 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; return m_Index & ENT_ENTRY_MASK;
} }
inline int CBaseHandle::GetSerialNumber() const inline int CBaseHandle::GetSerialNumber() const
{ {
return m_Index >> NUM_ENT_ENTRY_BITS; return m_Index >> NUM_SERIAL_NUM_SHIFT_BITS;
} }
inline int CBaseHandle::ToInt() const inline int CBaseHandle::ToInt() const

View File

@ -1,4 +1,4 @@
//========= Copyright <EFBFBD> 1996-2005, Valve Corporation, All rights reserved. ============// //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
// //
// Purpose: // Purpose:
// //
@ -69,7 +69,7 @@
#define INVALID_EHANDLE_INDEX 0xFFFFFFFF #define INVALID_EHANDLE_INDEX 0xFFFFFFFF
#define NUM_SERIAL_NUM_BITS (32 - NUM_ENT_ENTRY_BITS) #define NUM_SERIAL_NUM_BITS (32 - NUM_ENT_ENTRY_BITS)
#define NUM_SERIAL_NUM_SHIFT_BITS 16
// Networked ehandles use less bits to encode the serial number. // Networked ehandles use less bits to encode the serial number.
#define NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS 10 #define NUM_NETWORKED_EHANDLE_SERIAL_NUMBER_BITS 10