From 66d57858ac5ddf767fd33db6c5c6e6c3bfcfd289 Mon Sep 17 00:00:00 2001 From: tmp64 Date: Wed, 11 Sep 2024 13:05:05 +0700 Subject: [PATCH] Add player connected checks to GetPlayerInfo calls --- src/game/client/GameStudioModelRenderer.cpp | 4 +- src/game/client/hud/ag/ag_global.cpp | 3 +- src/game/client/hud/chat.cpp | 72 ++++++++++----------- src/game/client/hud/death_notice.cpp | 8 +-- src/game/client/player_info.cpp | 1 + 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/game/client/GameStudioModelRenderer.cpp b/src/game/client/GameStudioModelRenderer.cpp index 6de19d77..a11fc8bf 100644 --- a/src/game/client/GameStudioModelRenderer.cpp +++ b/src/game/client/GameStudioModelRenderer.cpp @@ -525,7 +525,7 @@ void CGameStudioModelRenderer::ForceModelCommand(void) CPlayerInfo *pi = GetPlayerInfo(i + 1)->Update(); - if (!CHudSpectator::Get()->IsActivePlayer(gEngfuncs.GetEntityByIndex(i + 1))) + if (!pi->IsConnected() || !CHudSpectator::Get()->IsActivePlayer(gEngfuncs.GetEntityByIndex(i + 1))) continue; strncpy(plrName, pi->GetName(), MAX_PLAYER_NAME - 1); @@ -601,7 +601,7 @@ void CGameStudioModelRenderer::ForceColorsCommand(void) CPlayerInfo *pi = GetPlayerInfo(i + 1)->Update(); - if (!CHudSpectator::Get()->IsActivePlayer(gEngfuncs.GetEntityByIndex(i + 1))) + if (!pi->IsConnected() || !CHudSpectator::Get()->IsActivePlayer(gEngfuncs.GetEntityByIndex(i + 1))) continue; strncpy(plrName, pi->GetName(), MAX_PLAYER_NAME - 1); diff --git a/src/game/client/hud/ag/ag_global.cpp b/src/game/client/hud/ag/ag_global.cpp index 300d6738..ae47762c 100644 --- a/src/game/client/hud/ag/ag_global.cpp +++ b/src/game/client/hud/ag/ag_global.cpp @@ -82,9 +82,8 @@ int AgHudGlobal::MsgFunc_AuthID(const char *pszName, int iSize, void *pbuf) const int slot = READ_BYTE(); char *steamid = READ_STRING(); - if (slot > 0 && slot <= MAX_PLAYERS) + if (CPlayerInfo *pi = GetPlayerInfoSafe(slot)) { - CPlayerInfo *pi = GetPlayerInfo(slot); if (!strncmp(steamid, "STEAM_", 6) || !strncmp(steamid, "VALVE_", 6)) Q_strncpy(pi->m_szSteamID, steamid + 6, sizeof(pi->m_szSteamID)); // cutout "STEAM_" or "VALVE_" start of the string else diff --git a/src/game/client/hud/chat.cpp b/src/game/client/hud/chat.cpp index f2a58872..920b520d 100644 --- a/src/game/client/hud/chat.cpp +++ b/src/game/client/hud/chat.cpp @@ -838,8 +838,8 @@ Color CHudChat::GetDefaultTextColor(void) //----------------------------------------------------------------------------- Color CHudChat::GetClientColor(int clientIndex) { - if (clientIndex >= 1 && clientIndex <= MAX_PLAYERS) - return g_pViewport->GetTeamColor(GetPlayerInfo(clientIndex)->Update()->GetTeamNumber()); + if (CPlayerInfo *pi = GetPlayerInfoSafe(clientIndex)) + return g_pViewport->GetTeamColor(pi->Update()->GetTeamNumber()); return g_pViewport->GetTeamColor(0); } @@ -1206,46 +1206,44 @@ void CHudChat::ChatPrintf(int iPlayerIndex, const char *fmt, ...) AgHudLocation::Get()->ParseAndEditSayString(iPlayerIndex, msg, sizeof(msg)); // Replace server name with real name - CPlayerInfo *pi = nullptr; - if (iPlayerIndex != 0) - { - pi = GetPlayerInfo(iPlayerIndex)->Update(); + CPlayerInfo *pi = GetPlayerInfoSafe(iPlayerIndex); + if (pi) + pi->Update(); - if (pi->HasRealName()) - { - const char *realname = pi->GetDisplayName(); - int realnamelen = strlen(realname); + if (pi && pi->HasRealName()) + { + const char *realname = pi->GetDisplayName(); + int realnamelen = strlen(realname); - // Find player name - const char *nameinmsg = strstr(msg, pi->GetName()); - int namelen = 0; + // Find player name + const char *nameinmsg = strstr(msg, pi->GetName()); + int namelen = 0; - if (!nameinmsg) - { - // Try name without color codes (miniAG bug) - const char *strippedname = RemoveColorCodes(pi->GetName()); - nameinmsg = strstr(msg, strippedname); + if (!nameinmsg) + { + // Try name without color codes (miniAG bug) + const char *strippedname = RemoveColorCodes(pi->GetName()); + nameinmsg = strstr(msg, strippedname); - if (nameinmsg) - namelen = strlen(strippedname); - } - else - { - namelen = strlen(pi->GetName()); - } + if (nameinmsg) + namelen = strlen(strippedname); + } + else + { + namelen = strlen(pi->GetName()); + } - if (namelen > 0) - { - int namestart = nameinmsg - msg; - int nameend = namestart + namelen; - int realnameend = namestart + realnamelen; + if (namelen > 0) + { + int namestart = nameinmsg - msg; + int nameend = namestart + namelen; + int realnameend = namestart + realnamelen; - // Move part after the name to where it will be after replace - memmove(msg + realnameend, msg + nameend, std::min(sizeof(msg) - nameend + 1, sizeof(msg) - realnameend - 1)); + // Move part after the name to where it will be after replace + memmove(msg + realnameend, msg + nameend, std::min(sizeof(msg) - nameend + 1, sizeof(msg) - realnameend - 1)); - // Replace name with realname - memcpy(msg + namestart, realname, realnamelen); - } + // Replace name with realname + memcpy(msg + namestart, realname, realnamelen); } } @@ -1293,9 +1291,9 @@ void CHudChat::ChatPrintf(int iPlayerIndex, const char *fmt, ...) int iNameLength = 0; const char *playerName = "Console"; - if (iPlayerIndex != 0) + if (CPlayerInfo *pi = GetPlayerInfoSafe(iPlayerIndex)) { - playerName = GetPlayerInfo(iPlayerIndex)->Update()->GetDisplayName(); + playerName = pi->Update()->GetDisplayName(); } int msglen = strlen(pmsg); diff --git a/src/game/client/hud/death_notice.cpp b/src/game/client/hud/death_notice.cpp index fdd5d059..d5f8dfd7 100644 --- a/src/game/client/hud/death_notice.cpp +++ b/src/game/client/hud/death_notice.cpp @@ -197,7 +197,7 @@ int CHudDeathNotice::MsgFunc_DeathMsg(const char *pszName, int iSize, void *pbuf // Get the Killer's name CPlayerInfo *killerInfo = nullptr; const char *killer_name; - if (killer != 0 && (killerInfo = GetPlayerInfo(killer))->IsConnected()) + if (killer != 0 && (killerInfo = GetPlayerInfoSafe(killer))) { killer_name = killerInfo->GetDisplayName(); @@ -223,7 +223,7 @@ int CHudDeathNotice::MsgFunc_DeathMsg(const char *pszName, int iSize, void *pbuf // Get the Victim's name const char *victim_name = NULL; // If victim is -1, the killer killed a specific, non-player object (like a sentrygun) - if (((char)victim) != -1) + if (((char)victim) != -1 && GetPlayerInfoSafe(victim)) victim_name = GetPlayerInfo(victim)->GetDisplayName(); if (!victim_name) { @@ -232,9 +232,9 @@ int CHudDeathNotice::MsgFunc_DeathMsg(const char *pszName, int iSize, void *pbuf } else { - CPlayerInfo *victimInfo = GetPlayerInfo(victim); + CPlayerInfo *victimInfo = GetPlayerInfoSafe(victim); - if (victimInfo->GetTeamNumber() == 0) + if (!victimInfo || victimInfo->GetTeamNumber() == 0) { rgDeathNoticeList[i].bVictimHasColor = false; } diff --git a/src/game/client/player_info.cpp b/src/game/client/player_info.cpp index df2a9784..55676acb 100644 --- a/src/game/client/player_info.cpp +++ b/src/game/client/player_info.cpp @@ -334,6 +334,7 @@ bool CPlayerInfo::IsSpectator() const char *CPlayerInfo::GetDisplayName(bool bNoColorCodes) { + Assert(m_bIsConnected); const char *name = nullptr; if (m_szRealName[0])