Skip to content

Commit

Permalink
add SanerGetClientName
Browse files Browse the repository at this point in the history
  • Loading branch information
rtldg committed Oct 26, 2021
1 parent 8a31bc8 commit 5312c31
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
41 changes: 41 additions & 0 deletions addons/sourcemod/scripting/include/shavit.inc
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,47 @@ stock float GetAngleDiff(float current, float previous)
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
// 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)
Expand Down
8 changes: 4 additions & 4 deletions addons/sourcemod/scripting/shavit-chat.sp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ void PreviewChat(int client, int rank)
Format(sTextFormatting, MAXLENGTH_BUFFER, "\x01%s", sTextFormatting);

char sOriginalName[MAXLENGTH_NAME];
GetClientName(client, sOriginalName, MAXLENGTH_NAME);
SanerGetClientName(client, sOriginalName);

// remove control characters
for(int i = 0; i < sizeof(gS_ControlCharacters); i++)
Expand Down Expand Up @@ -1402,7 +1402,7 @@ void FormatChat(int client, char[] buffer, int size)
FormatColors(buffer, size, true, true);
FormatRandom(buffer, size);

char temp[32];
char temp[33];

if(gEV_Type != Engine_TF2)
{
Expand Down Expand Up @@ -1443,7 +1443,7 @@ void FormatChat(int client, char[] buffer, int size)
ReplaceString(buffer, size, "{wrs}", temp);
}

GetClientName(client, temp, 32);
SanerGetClientName(client, temp);
ReplaceString(buffer, size, "{name}", temp);
}

Expand Down Expand Up @@ -1634,7 +1634,7 @@ public int Native_GetPlainChatrank(Handle handler, int numParams)
char sName[MAX_NAME_LENGTH];
if (includename /* || iChatRank == -1*/)
{
GetClientName(client, sName, MAX_NAME_LENGTH);
SanerGetClientName(client, sName);
}

ReplaceString(buf, sizeof(buf), "{name}", sName);
Expand Down
2 changes: 1 addition & 1 deletion addons/sourcemod/scripting/shavit-core.sp
Original file line number Diff line number Diff line change
Expand Up @@ -2262,7 +2262,7 @@ public void OnClientPutInServer(int client)
}

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

int iLength = ((strlen(sName) * 2) + 1);
Expand Down
5 changes: 2 additions & 3 deletions addons/sourcemod/scripting/shavit-hud.sp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
{
PrintToChat(client, "0x%X", (gI_HUDSettings[client] & (HUD_GLOCK|HUD_USP)));
int iSlot = CS_SLOT_SECONDARY;
int iWeapon = GetPlayerWeaponSlot(client, iSlot);
char sWeapon[32];
Expand Down Expand Up @@ -1907,7 +1906,7 @@ void UpdateSpectatorList(int client, Panel panel, bool &draw)
break;
}

GetClientName(iSpectatorClients[i], sName, sizeof(sName));
SanerGetClientName(iSpectatorClients[i], sName);
ReplaceString(sName, sizeof(sName), "#", "?");
TrimDisplayString(sName, sName, sizeof(sName), gCV_SpecNameSymbolLength.IntValue);

Expand Down Expand Up @@ -2107,7 +2106,7 @@ void UpdateKeyHint(int client)
break;
}

GetClientName(iSpectatorClients[i], sName, sizeof(sName));
SanerGetClientName(iSpectatorClients[i], sName);
ReplaceString(sName, sizeof(sName), "#", "?");
TrimDisplayString(sName, sName, sizeof(sName), gCV_SpecNameSymbolLength.IntValue);
Format(sMessage, 256, "%s\n%s", sMessage, sName);
Expand Down
4 changes: 2 additions & 2 deletions addons/sourcemod/scripting/shavit-mapchooser.sp
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ void Nominate(int client, const char mapname[PLATFORM_MAX_PATH])
g_aNominateList.PushString(mapname);
g_cNominatedMap[client] = mapname;
char name[MAX_NAME_LENGTH];
GetClientName(client, name, sizeof(name));
SanerGetClientName(client, name);

PrintToChatAll("%s%t", g_cPrefix, "Map Nominated", name, mapname);
}
Expand Down Expand Up @@ -1829,7 +1829,7 @@ int CheckRTV(int client = 0)

if(client != 0)
{
GetClientName(client, name, sizeof(name));
SanerGetClientName(client, name);
}
if(needed > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion addons/sourcemod/scripting/shavit-replay-recorder.sp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void DoReplaySaverCallbacks(int iSteamID, int client, int style, float time, int
}

char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, MAX_NAME_LENGTH);
SanerGetClientName(client, sName);
ReplaceString(sName, MAX_NAME_LENGTH, "#", "?");

int postframes = gI_PlayerFrames[client] - gI_PlayerFinishFrame[client];
Expand Down
2 changes: 1 addition & 1 deletion addons/sourcemod/scripting/shavit-stats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public Action Command_MapsDoneLeft(int client, int args)

if (iSteamID < 1)
{
GetClientName(target, gS_TargetName[client], sizeof(gS_TargetName[]));
SanerGetClientName(target, gS_TargetName[client]);
iSteamID = GetSteamAccountID(target);
}

Expand Down

0 comments on commit 5312c31

Please sign in to comment.