add SanerGetClientName

This commit is contained in:
rtldg 2021-10-26 21:35:01 +00:00
parent 8a31bc84aa
commit 5312c31253
7 changed files with 52 additions and 12 deletions

View File

@ -440,6 +440,47 @@ stock float GetAngleDiff(float current, float previous)
return diff - 360.0 * RoundToFloor((diff + 180.0) / 360.0); return diff - 360.0 * RoundToFloor((diff + 180.0) / 360.0);
} }
// Steam names are `char[32+1];`. Source engine names are `char[32];` (MAX_PLAYER_NAME_LENGTH).
// This means Source engine names can end up with an invalid unicode sequence at the end.
// This will remove the unicode codepoint if necessary.
stock void SanerGetClientName(int client, char[] name)
{
static EngineVersion ev = Engine_Unknown;
if (ev == Engine_Unknown)
{
ev = GetEngineVersion();
}
GetClientName(client, name, 32+1);
// CSGO doesn't have this problem because `MAX_PLAYER_NAME_LENGTH` is 128...
if (ev == Engine_CSGO)
{
return;
}
TrimTrailingInvalidUnicode(name);
}
stock void TrimTrailingInvalidUnicode(char[] outstr)
{
static int masks[3] = {0xC0, 0xE0, 0xF0};
int maxidx = strlen(outstr)-1;
for (int i = 0; (maxidx-i >= 0) && (i < 3); i++)
{
int x = (outstr[maxidx-i] & 0xF0);
if ((x & masks[i]) == masks[i])
{
outstr[maxidx-i] = 0;
return;
}
}
}
// https://forums.alliedmods.net/showthread.php?t=216841 // https://forums.alliedmods.net/showthread.php?t=216841
// Trims display string to specified max possible length, and appends "..." if initial string exceeds that length // Trims display string to specified max possible length, and appends "..." if initial string exceeds that length
stock void TrimDisplayString(const char[] str, char[] outstr, int outstrlen, int max_allowed_length) stock void TrimDisplayString(const char[] str, char[] outstr, int outstrlen, int max_allowed_length)

View File

@ -1032,7 +1032,7 @@ void PreviewChat(int client, int rank)
Format(sTextFormatting, MAXLENGTH_BUFFER, "\x01%s", sTextFormatting); Format(sTextFormatting, MAXLENGTH_BUFFER, "\x01%s", sTextFormatting);
char sOriginalName[MAXLENGTH_NAME]; char sOriginalName[MAXLENGTH_NAME];
GetClientName(client, sOriginalName, MAXLENGTH_NAME); SanerGetClientName(client, sOriginalName);
// remove control characters // remove control characters
for(int i = 0; i < sizeof(gS_ControlCharacters); i++) for(int i = 0; i < sizeof(gS_ControlCharacters); i++)
@ -1402,7 +1402,7 @@ void FormatChat(int client, char[] buffer, int size)
FormatColors(buffer, size, true, true); FormatColors(buffer, size, true, true);
FormatRandom(buffer, size); FormatRandom(buffer, size);
char temp[32]; char temp[33];
if(gEV_Type != Engine_TF2) if(gEV_Type != Engine_TF2)
{ {
@ -1443,7 +1443,7 @@ void FormatChat(int client, char[] buffer, int size)
ReplaceString(buffer, size, "{wrs}", temp); ReplaceString(buffer, size, "{wrs}", temp);
} }
GetClientName(client, temp, 32); SanerGetClientName(client, temp);
ReplaceString(buffer, size, "{name}", temp); ReplaceString(buffer, size, "{name}", temp);
} }
@ -1634,7 +1634,7 @@ public int Native_GetPlainChatrank(Handle handler, int numParams)
char sName[MAX_NAME_LENGTH]; char sName[MAX_NAME_LENGTH];
if (includename /* || iChatRank == -1*/) if (includename /* || iChatRank == -1*/)
{ {
GetClientName(client, sName, MAX_NAME_LENGTH); SanerGetClientName(client, sName);
} }
ReplaceString(buf, sizeof(buf), "{name}", sName); ReplaceString(buf, sizeof(buf), "{name}", sName);

View File

@ -2262,7 +2262,7 @@ public void OnClientPutInServer(int client)
} }
char sName[MAX_NAME_LENGTH]; char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, MAX_NAME_LENGTH); SanerGetClientName(client, sName);
ReplaceString(sName, MAX_NAME_LENGTH, "#", "?"); // to avoid this: https://user-images.githubusercontent.com/3672466/28637962-0d324952-724c-11e7-8b27-15ff021f0a59.png ReplaceString(sName, MAX_NAME_LENGTH, "#", "?"); // to avoid this: https://user-images.githubusercontent.com/3672466/28637962-0d324952-724c-11e7-8b27-15ff021f0a59.png
int iLength = ((strlen(sName) * 2) + 1); int iLength = ((strlen(sName) * 2) + 1);

View File

@ -938,7 +938,6 @@ public void Player_Spawn(Event event, const char[] name, bool dontBroadcast)
{ {
if (gEV_Type != Engine_TF2 && (gI_HUDSettings[client] & (HUD_GLOCK|HUD_USP))) if (gEV_Type != Engine_TF2 && (gI_HUDSettings[client] & (HUD_GLOCK|HUD_USP)))
{ {
PrintToChat(client, "0x%X", (gI_HUDSettings[client] & (HUD_GLOCK|HUD_USP)));
int iSlot = CS_SLOT_SECONDARY; int iSlot = CS_SLOT_SECONDARY;
int iWeapon = GetPlayerWeaponSlot(client, iSlot); int iWeapon = GetPlayerWeaponSlot(client, iSlot);
char sWeapon[32]; char sWeapon[32];
@ -1907,7 +1906,7 @@ void UpdateSpectatorList(int client, Panel panel, bool &draw)
break; break;
} }
GetClientName(iSpectatorClients[i], sName, sizeof(sName)); SanerGetClientName(iSpectatorClients[i], sName);
ReplaceString(sName, sizeof(sName), "#", "?"); ReplaceString(sName, sizeof(sName), "#", "?");
TrimDisplayString(sName, sName, sizeof(sName), gCV_SpecNameSymbolLength.IntValue); TrimDisplayString(sName, sName, sizeof(sName), gCV_SpecNameSymbolLength.IntValue);
@ -2107,7 +2106,7 @@ void UpdateKeyHint(int client)
break; break;
} }
GetClientName(iSpectatorClients[i], sName, sizeof(sName)); SanerGetClientName(iSpectatorClients[i], sName);
ReplaceString(sName, sizeof(sName), "#", "?"); ReplaceString(sName, sizeof(sName), "#", "?");
TrimDisplayString(sName, sName, sizeof(sName), gCV_SpecNameSymbolLength.IntValue); TrimDisplayString(sName, sName, sizeof(sName), gCV_SpecNameSymbolLength.IntValue);
Format(sMessage, 256, "%s\n%s", sMessage, sName); Format(sMessage, 256, "%s\n%s", sMessage, sName);

View File

@ -1772,7 +1772,7 @@ void Nominate(int client, const char mapname[PLATFORM_MAX_PATH])
g_aNominateList.PushString(mapname); g_aNominateList.PushString(mapname);
g_cNominatedMap[client] = mapname; g_cNominatedMap[client] = mapname;
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
GetClientName(client, name, sizeof(name)); SanerGetClientName(client, name);
PrintToChatAll("%s%t", g_cPrefix, "Map Nominated", name, mapname); PrintToChatAll("%s%t", g_cPrefix, "Map Nominated", name, mapname);
} }
@ -1829,7 +1829,7 @@ int CheckRTV(int client = 0)
if(client != 0) if(client != 0)
{ {
GetClientName(client, name, sizeof(name)); SanerGetClientName(client, name);
} }
if(needed > 0) if(needed > 0)
{ {

View File

@ -352,7 +352,7 @@ void DoReplaySaverCallbacks(int iSteamID, int client, int style, float time, int
} }
char sName[MAX_NAME_LENGTH]; char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, MAX_NAME_LENGTH); SanerGetClientName(client, sName);
ReplaceString(sName, MAX_NAME_LENGTH, "#", "?"); ReplaceString(sName, MAX_NAME_LENGTH, "#", "?");
int postframes = gI_PlayerFrames[client] - gI_PlayerFinishFrame[client]; int postframes = gI_PlayerFrames[client] - gI_PlayerFinishFrame[client];

View File

@ -560,7 +560,7 @@ public Action Command_MapsDoneLeft(int client, int args)
if (iSteamID < 1) if (iSteamID < 1)
{ {
GetClientName(target, gS_TargetName[client], sizeof(gS_TargetName[])); SanerGetClientName(target, gS_TargetName[client]);
iSteamID = GetSteamAccountID(target); iSteamID = GetSteamAccountID(target);
} }