From f1dc24c089dcc34ffe9263dea1f21b33fb9678ee Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Thu, 4 Sep 2014 15:27:44 -0700 Subject: [PATCH] Move Steam2/3 id rendering logic to CPlayer --- core/PlayerManager.cpp | 77 ++++++++++++++++++++++++++++++++++++++ core/PlayerManager.h | 2 + core/logic/smn_players.cpp | 55 ++++++--------------------- public/IPlayerHelpers.h | 22 +++++++++++ 4 files changed, 113 insertions(+), 43 deletions(-) diff --git a/core/PlayerManager.cpp b/core/PlayerManager.cpp index c9600edf..eec92067 100644 --- a/core/PlayerManager.cpp +++ b/core/PlayerManager.cpp @@ -2070,6 +2070,83 @@ const CSteamID &CPlayer::GetSteamId(bool validated) return m_SteamId; } +bool CPlayer::GetSteam2Id(char *out, size_t maxlen, bool validate) +{ + if (IsFakeClient()) + { + snprintf(out, maxlen, "BOT"); + return true; + } + + auto steamId = GetSteamId(validate); + if (!steamId.IsValid()) + { + if (g_HL2.IsLANServer()) + { + snprintf(out, maxlen, "STEAM_ID_LAN"); + return true; + } + else if (!validate) + { + snprintf(out, maxlen, "STEAM_ID_PENDING"); + return true; + } + else + { + return false; + } + } + + EUniverse universe = steamId.GetEUniverse(); + if (atoi(g_pGameConf->GetKeyValue("UseInvalidUniverseInSteam2IDs")) == 1) + { + universe = k_EUniverseInvalid; + } + + snprintf(out, maxlen, "STEAM_%u:%u:%u", universe, steamId.GetAccountID() & 1, steamId.GetAccountID() >> 1); + return true; +} + +bool CPlayer::GetSteam3Id(char *out, size_t maxlen, bool validate) +{ + if (IsFakeClient()) + { + snprintf(out, maxlen, "BOT"); + return true; + } + + auto steamId = GetSteamId(validate); + if (!steamId.IsValid()) + { + if (g_HL2.IsLANServer()) + { + snprintf(out, maxlen, "STEAM_ID_LAN"); + return true; + } + else if (!validate) + { + snprintf(out, maxlen, "STEAM_ID_PENDING"); + return true; + } + else + { + return false; + } + } + + // TODO: make sure all hl2sdks' steamclientpublic.h have k_unSteamUserDesktopInstance. + if (steamId.GetUnAccountInstance() == 1 /* k_unSteamUserDesktopInstance */) + { + snprintf(out, maxlen, "[U:%u:%u]", steamId.GetEUniverse(), steamId.GetAccountID()); + } + else + { + snprintf(out, maxlen, "[U:%u:%u:%u]", steamId.GetEUniverse(), steamId.GetAccountID(), steamId.GetUnAccountInstance()); + } + + return true; +} + unsigned int CPlayer::GetSteamAccountID(bool validated) { if (!IsFakeClient() && (!validated || IsAuthStringValidated())) diff --git a/core/PlayerManager.h b/core/PlayerManager.h index cfd57cc0..715c0343 100644 --- a/core/PlayerManager.h +++ b/core/PlayerManager.h @@ -75,6 +75,8 @@ public: unsigned int GetSteamAccountID(bool validated = true); const CSteamID &GetSteamId(bool validated = true); uint64_t GetSteamId64(bool validated = true) { return GetSteamId(validated).ConvertToUint64(); } + bool GetSteam2Id(char *out, size_t maxlen, bool validated = true); + bool GetSteam3Id(char *out, size_t maxlen, bool validated = true); edict_t *GetEdict(); bool IsInGame(); bool WasCountedAsInGame(); diff --git a/core/logic/smn_players.cpp b/core/logic/smn_players.cpp index 34b24798..ee4872c9 100644 --- a/core/logic/smn_players.cpp +++ b/core/logic/smn_players.cpp @@ -367,53 +367,22 @@ static cell_t SteamIdToLocal(IPluginContext *pCtx, int index, AuthIdType authTyp } break; case AuthIdType::Steam2: + { + char szAuth[64]; + if (!pPlayer->GetSteam2Id(szAuth, sizeof(szAuth), validate)) + { + return 0; + } + + pCtx->StringToLocal(local_addr, bytes, szAuth); + } + break; case AuthIdType::Steam3: { - if (pPlayer->IsFakeClient()) - { - pCtx->StringToLocal(local_addr, bytes, "BOT"); - return 1; - } - - uint64_t steamId = pPlayer->GetSteamId64(validate); - if (steamId == 0) - { - if (gamehelpers->IsLANServer()) - { - pCtx->StringToLocal(local_addr, bytes, "STEAM_ID_LAN"); - return 1; - } - else if (!validate) - { - pCtx->StringToLocal(local_addr, bytes, "STEAM_ID_PENDING"); - return 1; - } - else - { - return 0; - } - } - char szAuth[64]; - unsigned int universe = steamId >> 56; - unsigned int accountId = steamId & 0xFFFFFFFF; - unsigned int instance = (steamId >> 32) & 0x000FFFFF; - if (authType == AuthIdType::Steam2) + if (!pPlayer->GetSteam2Id(szAuth, sizeof(szAuth), validate)) { - if (atoi(g_pGameConf->GetKeyValue("UseInvalidUniverseInSteam2IDs")) == 1) - { - universe = 0; - } - - snprintf(szAuth, sizeof(szAuth), "STEAM_%u:%u:%u", universe, accountId & 1, accountId >> 1); - } - else if (instance != 1) - { - snprintf(szAuth, sizeof(szAuth), "[U:%u:%u:%u]", universe, accountId, instance); - } - else - { - snprintf(szAuth, sizeof(szAuth), "[U:%u:%u]", universe, accountId); + return 0; } pCtx->StringToLocal(local_addr, bytes, szAuth); diff --git a/public/IPlayerHelpers.h b/public/IPlayerHelpers.h index 35a174a7..d115fb95 100644 --- a/public/IPlayerHelpers.h +++ b/public/IPlayerHelpers.h @@ -276,6 +276,28 @@ namespace SourceMod * @return Steam Id or 0 if not available. */ virtual uint64_t GetSteamId64(bool validated = true) =0; + + /** + * @brief Returns the client's Steam ID rendered in Steam2 format. + * + * @param out Buffer in which to output the id. + * @param maxlen Maximum length of the output buffer in bytes. + * @param validated Check backend validation status. + * + * @return True on success or false if not available. + */ + virtual bool GetSteam2Id(char *out, size_t maxlen, bool validated = true) =0; + + /** + * @brief Returns the client's Steam ID rendered in Steam3 format. + * + * @param out Buffer in which to output the id. + * @param maxlen Maximum length of the output buffer in bytes. + * @param validated Check backend validation status. + * + * @return True on success or false if not available. + */ + virtual bool GetSteam3Id(char *out, size_t maxlen, bool validated = true) =0; }; /**