From 6cd5bf87d8cf5b3b292e99a7ae10dd9f8a2f4328 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Thu, 13 Feb 2025 18:04:38 -0500 Subject: [PATCH] Fix regression in DescribePlayer (%N / %L) for players not yet having entities (#2260) * Define a constant for ENTREF_MASK * Fix regression in DescribePlayer for players that don't yet have entities --- core/HalfLife2.cpp | 14 +++++++------- core/HalfLife2.h | 2 ++ core/logic_bridge.cpp | 9 ++++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/core/HalfLife2.cpp b/core/HalfLife2.cpp index e6ea60db0..b23540275 100644 --- a/core/HalfLife2.cpp +++ b/core/HalfLife2.cpp @@ -978,7 +978,7 @@ cell_t CHalfLife2::EntityToReference(CBaseEntity *pEntity) { IServerUnknown *pUnknown = (IServerUnknown *)pEntity; CBaseHandle hndl = pUnknown->GetRefEHandle(); - return (hndl.ToInt() | (1<<31)); + return (hndl.ToInt() | ENTREF_MASK); } CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef) @@ -990,10 +990,10 @@ CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef) CEntInfo *pInfo = NULL; - if (entRef & (1<<31)) + if (entRef & ENTREF_MASK) { /* Proper ent reference */ - int hndlValue = entRef & ~(1<<31); + int hndlValue = entRef & ~ENTREF_MASK; CBaseHandle hndl(hndlValue); pInfo = LookupEntity(hndl.GetEntryIndex()); @@ -1094,10 +1094,10 @@ int CHalfLife2::ReferenceToIndex(cell_t entRef) return INVALID_EHANDLE_INDEX; } - if (entRef & (1<<31)) + if (entRef & ENTREF_MASK) { /* Proper ent reference */ - int hndlValue = entRef & ~(1<<31); + int hndlValue = entRef & ~ENTREF_MASK; CBaseHandle hndl(hndlValue); CEntInfo *pInfo = LookupEntity(hndl.GetEntryIndex()); @@ -1148,7 +1148,7 @@ cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity) if (hndl.GetEntryIndex() >= MAX_EDICTS) { - return (hndl.ToInt() | (1<<31)); + return (hndl.ToInt() | ENTREF_MASK); } else { @@ -1163,7 +1163,7 @@ cell_t CHalfLife2::ReferenceToBCompatRef(cell_t entRef) return INVALID_EHANDLE_INDEX; } - int hndlValue = entRef & ~(1<<31); + int hndlValue = entRef & ~ENTREF_MASK; CBaseHandle hndl(hndlValue); if (hndl.GetEntryIndex() < MAX_EDICTS) diff --git a/core/HalfLife2.h b/core/HalfLife2.h index 903394583..33d404316 100644 --- a/core/HalfLife2.h +++ b/core/HalfLife2.h @@ -57,6 +57,8 @@ class ICommandArgs; using namespace SourceHook; using namespace SourceMod; +static const int ENTREF_MASK = (1 << 31); + #define HUD_PRINTTALK 3 #define HUD_PRINTCENTER 4 diff --git a/core/logic_bridge.cpp b/core/logic_bridge.cpp index 57e4f341e..4e0e8d88a 100644 --- a/core/logic_bridge.cpp +++ b/core/logic_bridge.cpp @@ -576,7 +576,14 @@ int CoreProviderImpl::MaxClients() bool CoreProviderImpl::DescribePlayer(int entRef, const char **namep, const char **authp, int *useridp) { - int index = g_HL2.ReferenceToIndex(entRef); + int index = entRef; + if (entRef & ENTREF_MASK) + { + // Unless this is an explicit entity reference, we don't know nor care if the player has an entity yet, + // as long as they are currently connected. (But if it *is* an explicit ref, validate it) + index = g_HL2.ReferenceToIndex(entRef); + } + CPlayer *player = g_Players.GetPlayerByIndex(index); if (!player || !player->IsConnected()) return false;