From 93212293215a8aaa9ddf2baa15835c17ec0e1b16 Mon Sep 17 00:00:00 2001 From: Corey D Date: Fri, 29 Jul 2022 11:10:51 +1000 Subject: [PATCH] Add GetClientOriginalLanguage (#1810) Creates a native which will return the language the client connected with. --- core/PlayerManager.cpp | 8 ++++++++ core/PlayerManager.h | 2 ++ core/logic/smn_lang.cpp | 13 +++++++++++++ plugins/include/lang.inc | 9 +++++++++ public/IPlayerHelpers.h | 10 +++++++++- 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/core/PlayerManager.cpp b/core/PlayerManager.cpp index 95b92ee9..3fd7b2a3 100644 --- a/core/PlayerManager.cpp +++ b/core/PlayerManager.cpp @@ -528,6 +528,7 @@ bool PlayerManager::OnClientConnect(edict_t *pEntity, const char *pszName, const pPlayer->m_LangId = translator->GetServerLanguage(); } #endif + pPlayer->m_OriginalLangId = pPlayer->m_LangId; } List::iterator iter; @@ -2035,6 +2036,7 @@ bool PlayerManager::HandleConVarQuery(QueryCvarCookie_t cookie, int client, EQue unsigned int langid; unsigned int new_langid = (translator->GetLanguageByName(cvarValue, &langid)) ? langid : translator->GetServerLanguage(); m_Players[i].m_LangId = new_langid; + m_Players[i].m_OriginalLangId = new_langid; OnClientLanguageChanged(i, new_langid); return true; @@ -2063,6 +2065,7 @@ void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity) m_pEdict = pEntity; m_iIndex = IndexOfEdict(pEntity); m_LangId = translator->GetServerLanguage(); + m_OriginalLangId = m_LangId; m_Serial.bits.index = m_iIndex; m_Serial.bits.serial = g_PlayerSerialCount++; @@ -2629,6 +2632,11 @@ unsigned int CPlayer::GetLanguageId() return m_LangId; } +unsigned int CPlayer::GetOriginalLanguageId() +{ + return m_OriginalLangId; +} + void CPlayer::SetLanguageId(unsigned int id) { if(m_LangId != id) diff --git a/core/PlayerManager.h b/core/PlayerManager.h index 68e96b04..16ac86d8 100644 --- a/core/PlayerManager.h +++ b/core/PlayerManager.h @@ -96,6 +96,7 @@ public: bool IsInKickQueue(); IPlayerInfo *GetPlayerInfo(); unsigned int GetLanguageId(); + unsigned int GetOriginalLanguageId(); void SetLanguageId(unsigned int id); int GetUserId(); bool RunAdminCacheChecks(); @@ -145,6 +146,7 @@ private: bool m_bAdminCheckSignalled = false; int m_iIndex; unsigned int m_LangId = SOURCEMOD_LANGUAGE_ENGLISH; + unsigned int m_OriginalLangId = SOURCEMOD_LANGUAGE_ENGLISH; int m_UserId = -1; bool m_bFakeClient = false; bool m_bIsSourceTV = false; diff --git a/core/logic/smn_lang.cpp b/core/logic/smn_lang.cpp index 87c61e73..249ce8f2 100644 --- a/core/logic/smn_lang.cpp +++ b/core/logic/smn_lang.cpp @@ -144,6 +144,18 @@ static cell_t sm_SetClientLanguage(IPluginContext *pContext, const cell_t *param return 1; } +static cell_t sm_GetClientOriginalLanguage(IPluginContext *pContext, const cell_t *params) +{ + IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]); + + if (!player || !player->IsConnected()) + { + return pContext->ThrowNativeError("Invalid client index %d", params[1]); + } + + return player->GetOriginalLanguageId(); +} + static cell_t sm_GetLanguageByCode(IPluginContext *pContext, const cell_t *params) { char *code; @@ -181,6 +193,7 @@ REGISTER_NATIVES(langNatives) {"GetLanguageCount", sm_GetLanguageCount}, {"GetLanguageInfo", sm_GetLanguageInfo}, {"SetClientLanguage", sm_SetClientLanguage}, + {"GetClientOriginalLanguage", sm_GetClientOriginalLanguage}, {"GetLanguageByCode", sm_GetLanguageByCode}, {"GetLanguageByName", sm_GetLanguageByName}, {NULL, NULL}, diff --git a/plugins/include/lang.inc b/plugins/include/lang.inc index 3c5dd1fb..06e171ed 100644 --- a/plugins/include/lang.inc +++ b/plugins/include/lang.inc @@ -99,6 +99,15 @@ native void GetLanguageInfo(int language, char[] code="", int codeLen=0, char[] */ native void SetClientLanguage(int client, int language); +/** + * Retrieves the language number a client had when they connected. + * + * @param client Client index. + * @return Language number client originally had. + * @error Invalid client index or client not connected. + */ +native int GetClientOriginalLanguage(int client); + /** * Retrieves the language number from a language code. * diff --git a/public/IPlayerHelpers.h b/public/IPlayerHelpers.h index c0cd0c5c..b3893e1e 100644 --- a/public/IPlayerHelpers.h +++ b/public/IPlayerHelpers.h @@ -41,7 +41,7 @@ #include #define SMINTERFACE_PLAYERMANAGER_NAME "IPlayerManager" -#define SMINTERFACE_PLAYERMANAGER_VERSION 21 +#define SMINTERFACE_PLAYERMANAGER_VERSION 22 struct edict_t; class IPlayerInfo; @@ -294,6 +294,14 @@ namespace SourceMod * @return Steam3 Id on success or NULL if not available. */ virtual const char *GetSteam3Id(bool validated = true) =0; + + /** + * @brief Returns the original language id the client had when + * they connected. + * + * @return Language id. + */ + virtual unsigned int GetOriginalLanguageId() =0; }; /**