From 07fc524d27b3bc19a4475d80feddc20b43b2fc83 Mon Sep 17 00:00:00 2001 From: A1m` <33463136+A1mDev@users.noreply.github.com> Date: Tue, 14 Jul 2026 04:39:04 +0700 Subject: [PATCH] Fixed incorrect use of ReferenceToIndex, which caused some functions to be unstable during client connection. (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While this function works well with the client indexes we pass in, as its name suggests, we should essentially be passing player references, not client indexes. The problem is that SourceMod looks for this player in the entity list, but they might not be there when connecting, or they might be another client or a bot. This makes the code unstable and confusing, and it works intermittently. Steam doesn't care whether the player is in the entity list—it works with the Steam ID. Using ReferenceToIndex here is fundamentally wrong and causes random -1 errors, especially during OnClientConnect when the entity may not exist yet. This commit replaces it with a direct client index lookup, which is correct and reliable because params[1] is already a client index. --- Extension/gsnatives.cpp | 44 +++++++++++++++++++++++++++-------------- Extension/ssnatives.cpp | 39 +++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/Extension/gsnatives.cpp b/Extension/gsnatives.cpp index e3d7495..c3fc5ab 100644 --- a/Extension/gsnatives.cpp +++ b/Extension/gsnatives.cpp @@ -218,12 +218,17 @@ static cell_t sm_UserHasLicenseForApp(IPluginContext *pContext, const cell_t *pa { return k_EUserHasLicenseResultNoAuth; } - - int client = gamehelpers->ReferenceToIndex(params[1]); - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ - if (pPlayer == NULL || pPlayer->IsConnected() == false) + + int client = params[1]; + if (client < 1 || client > playerhelpers->GetMaxClients()) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); + return pContext->ThrowNativeError("Client index %d is invalid", client); + } + + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); + if (pPlayer == NULL || !pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client index %d is not connected", client); } CSteamID checkid = CreateCommonCSteamID(pPlayer, params, 3, 4); @@ -245,12 +250,16 @@ static cell_t sm_UserHasLicenseForAppId(IPluginContext *pContext, const cell_t * static cell_t sm_GetClientSteamID(IPluginContext *pContext, const cell_t *params) { - int client = gamehelpers->ReferenceToIndex(params[1]); - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); - - if (pPlayer == NULL || pPlayer->IsConnected() == false) + int client = params[1]; + if (client < 1 || client > playerhelpers->GetMaxClients()) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); + return pContext->ThrowNativeError("Client index %d is invalid", client); + } + + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); + if (pPlayer == NULL || !pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client index %d is not connected", client); } CSteamID steamId = CreateCommonCSteamID(pPlayer, params, 4, 5); @@ -270,14 +279,19 @@ static cell_t sm_GetUserGroupStatus(IPluginContext *pContext, const cell_t *para if (pServer == NULL) { - return false; + return 0; } - int client = gamehelpers->ReferenceToIndex(params[1]); - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ - if (pPlayer == NULL || pPlayer->IsConnected() == false) + int client = params[1]; + if (client < 1 || client > playerhelpers->GetMaxClients()) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); + return pContext->ThrowNativeError("Client index %d is invalid", client); + } + + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); + if (pPlayer == NULL || !pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client index %d is not connected", client); } CSteamID checkid = CreateCommonCSteamID(pPlayer, params, 3, 4); diff --git a/Extension/ssnatives.cpp b/Extension/ssnatives.cpp index 6d52d91..c49ddb8 100644 --- a/Extension/ssnatives.cpp +++ b/Extension/ssnatives.cpp @@ -60,11 +60,16 @@ static cell_t sm_RequestUserStats(IPluginContext *pContext, const cell_t *params return 0; } - int client = gamehelpers->ReferenceToIndex(params[1]); - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ - if (pPlayer == NULL || pPlayer->IsConnected() == false) + int client = params[1]; + if (client < 1 || client > playerhelpers->GetMaxClients()) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); + return pContext->ThrowNativeError("Client index %d is invalid", client); + } + + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); + if (pPlayer == NULL || !pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client index %d is not connected", client); } CSteamID checkid = CreateCommonCSteamID(pPlayer, params); @@ -80,11 +85,16 @@ static cell_t sm_GetStatCell(IPluginContext *pContext, const cell_t *params) return 0; } - int client = gamehelpers->ReferenceToIndex(params[1]); - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ - if (pPlayer == NULL || pPlayer->IsConnected() == false) + int client = params[1]; + if (client < 1 || client > playerhelpers->GetMaxClients()) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); + return pContext->ThrowNativeError("Client index %d is invalid", client); + } + + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); + if (pPlayer == NULL || !pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client index %d is not connected", client); } char *pName; @@ -123,11 +133,16 @@ static cell_t sm_GetStatFloat(IPluginContext *pContext, const cell_t *params) return 0; } - int client = gamehelpers->ReferenceToIndex(params[1]); - IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); /* Man, including GameHelpers and PlayerHelpers for this native :(. */ - if (pPlayer == NULL || pPlayer->IsConnected() == false) + int client = params[1]; + if (client < 1 || client > playerhelpers->GetMaxClients()) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); + return pContext->ThrowNativeError("Client index %d is invalid", client); + } + + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client); + if (pPlayer == NULL || !pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client index %d is not connected", client); } char *pName;