From c7fb295eab4f465536b9906dc7cecf1aa9e05274 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Thu, 16 Jul 2026 13:29:22 -0400 Subject: [PATCH] Slightly speed up OnClientAuthorized where possible (#2504) --- core/PlayerManager.cpp | 28 ++++++++++++++++++++++++++++ core/PlayerManager.h | 7 +++++++ core/frame_hooks.cpp | 10 +++++++++- core/frame_hooks.h | 1 + plugins/include/clients.inc | 9 ++++++--- 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/core/PlayerManager.cpp b/core/PlayerManager.cpp index 2bcdd786..f2153e14 100644 --- a/core/PlayerManager.cpp +++ b/core/PlayerManager.cpp @@ -46,6 +46,7 @@ #include #include #include "ConsoleDetours.h" +#include "frame_hooks.h" #include "logic_bridge.h" #include #include "smn_keyvalues.h" @@ -78,6 +79,16 @@ SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *) #endif SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *); +// mock lacks NetworkIDValidated; episode1/darkm (< SE_ORANGEBOX) predate it. +#if SOURCE_ENGINE >= SE_ORANGEBOX && SOURCE_ENGINE != SE_MOCK +#if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_BLADE || SOURCE_ENGINE == SE_MCV +SH_DECL_HOOK3_void(IServerGameClients, NetworkIDValidated, SH_NOATTRIB, 0, const char *, const char *, CSteamID); +#else +SH_DECL_HOOK2_void(IServerGameClients, NetworkIDValidated, SH_NOATTRIB, 0, const char *, const char *); +#endif +#define SM_HAS_NETWORKID_VALIDATED 1 +#endif + #if SOURCE_ENGINE >= SE_EYE SH_DECL_HOOK2_void(IServerGameClients, ClientCommandKeyValues, SH_NOATTRIB, 0, edict_t *, KeyValues *); #endif @@ -175,6 +186,9 @@ void PlayerManager::OnSourceModAllInitialized() SH_ADD_HOOK(IServerGameClients, ClientCommandKeyValues, serverClients, SH_MEMBER(this, &PlayerManager::OnClientCommandKeyValues_Post), true); #endif SH_ADD_HOOK(IServerGameClients, ClientSettingsChanged, serverClients, SH_MEMBER(this, &PlayerManager::OnClientSettingsChanged), true); +#if defined SM_HAS_NETWORKID_VALIDATED + SH_ADD_HOOK(IServerGameClients, NetworkIDValidated, serverClients, SH_MEMBER(this, &PlayerManager::OnNetworkIDValidated), true); +#endif SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_MEMBER(this, &PlayerManager::OnServerActivate), true); #if SOURCE_ENGINE >= SE_LEFT4DEAD SH_ADD_HOOK(IServerGameDLL, ServerHibernationUpdate, gamedll, SH_MEMBER(this, &PlayerManager::OnServerHibernationUpdate), true); @@ -232,6 +246,9 @@ void PlayerManager::OnSourceModShutdown() SH_REMOVE_HOOK(IServerGameClients, ClientCommandKeyValues, serverClients, SH_MEMBER(this, &PlayerManager::OnClientCommandKeyValues_Post), true); #endif SH_REMOVE_HOOK(IServerGameClients, ClientSettingsChanged, serverClients, SH_MEMBER(this, &PlayerManager::OnClientSettingsChanged), true); +#if defined SM_HAS_NETWORKID_VALIDATED + SH_REMOVE_HOOK(IServerGameClients, NetworkIDValidated, serverClients, SH_MEMBER(this, &PlayerManager::OnNetworkIDValidated), true); +#endif SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_MEMBER(this, &PlayerManager::OnServerActivate), true); #if SOURCE_ENGINE >= SE_LEFT4DEAD SH_REMOVE_HOOK(IServerGameDLL, ServerHibernationUpdate, gamedll, SH_MEMBER(this, &PlayerManager::OnServerHibernationUpdate), true); @@ -492,6 +509,17 @@ void PlayerManager::RunAuthChecks() } } +#if defined SM_HAS_NETWORKID_VALIDATED +#if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_BLADE || SOURCE_ENGINE == SE_MCV +void PlayerManager::OnNetworkIDValidated(const char *pszUserName, const char *pszNetworkID, CSteamID steamID) +#else +void PlayerManager::OnNetworkIDValidated(const char *pszUserName, const char *pszNetworkID) +#endif +{ + g_PendingAuthCheck = true; +} +#endif + bool PlayerManager::OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen) { int client = IndexOfEdict(pEntity); diff --git a/core/PlayerManager.h b/core/PlayerManager.h index 799ce562..6107c427 100644 --- a/core/PlayerManager.h +++ b/core/PlayerManager.h @@ -194,6 +194,13 @@ public: #endif void OnClientSettingsChanged(edict_t *pEntity); //void OnClientSettingsChanged_Pre(edict_t *pEntity); +#if SOURCE_ENGINE >= SE_ORANGEBOX && SOURCE_ENGINE != SE_MOCK +#if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_BLADE || SOURCE_ENGINE == SE_MCV + void OnNetworkIDValidated(const char *pszUserName, const char *pszNetworkID, CSteamID steamID); +#else + void OnNetworkIDValidated(const char *pszUserName, const char *pszNetworkID); +#endif +#endif void OnClientLanguageChanged(int client, unsigned int language); void OnServerHibernationUpdate(bool bHibernating); void OnClientPrintf(edict_t *pEdict, const char *szMsg); diff --git a/core/frame_hooks.cpp b/core/frame_hooks.cpp index c270b28d..5ae06363 100644 --- a/core/frame_hooks.cpp +++ b/core/frame_hooks.cpp @@ -45,6 +45,7 @@ static Queue *frame_actions; static float g_LastMenuTime = 0.0f; static float g_LastAuthCheck = 0.0f; bool g_PendingInternalPush = false; +bool g_PendingAuthCheck = false; class FrameActionInit : public SMGlobalClass { @@ -114,9 +115,16 @@ void RunFrameHooks(bool simulating) g_LastMenuTime = curtime; } - if (*g_NumPlayersToAuth && curtime - g_LastAuthCheck >= 0.7f) + /* g_PendingAuthCheck is set by the NetworkIDValidated hook so that a client + * whose Steam ticket was just validated is authorized on the next frame rather + * than waiting up to 0.7s for the periodic check. It's deferred to the next frame + * (instead of authorizing inline in the hook) because the engine doesn't flag the + * client as fully authenticated until after the NetworkIDValidated callback returns. + */ + if (*g_NumPlayersToAuth && (g_PendingAuthCheck || curtime - g_LastAuthCheck >= 0.7f)) { g_Players.RunAuthChecks(); g_LastAuthCheck = curtime; + g_PendingAuthCheck = false; } } diff --git a/core/frame_hooks.h b/core/frame_hooks.h index 65331598..a8136dff 100644 --- a/core/frame_hooks.h +++ b/core/frame_hooks.h @@ -46,6 +46,7 @@ struct FrameAction }; extern bool g_PendingInternalPush; +extern bool g_PendingAuthCheck; void AddFrameAction(const FrameAction & action); void RunFrameHooks(bool simulating); diff --git a/plugins/include/clients.inc b/plugins/include/clients.inc index 51ed0bb1..9e8711a3 100644 --- a/plugins/include/clients.inc +++ b/plugins/include/clients.inc @@ -170,9 +170,12 @@ forward void OnClientCommandKeyValues_Post(int client, KeyValues kv); forward void OnClientSettingsChanged(int client); /** - * Called when a client receives an auth ID. The state of a client's - * authorization as an admin is not guaranteed here. Use - * OnClientPostAdminCheck() if you need a client's admin status. + * Called after a client's auth ID has been validated. The state + * of a client's authorization as an admin is not guaranteed here. + * Use OnClientPostAdminCheck() if you need a client's admin status. + * + * There may be a delay in receiving this callback if the Steam + * netowrk is experience downtime or other issues. * * This is called by bots, but the ID will be "BOT". *