How to detect bad characters in player names?

Discussions about Coding and Scripting
Post Reply
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

How to detect bad characters in player names?

Post by Barbie »

Sometimes players with strange characters in name join the server. To avoid using characters that are not proper displayed in UT, I created a function to adjust such names:

Code: Select all

function bool AdjustPlayerName(string CurrentName, out string NewName) {
/*******************************************************************************
Returns all "bad" characters in *CurrentName* replaced with "_" in *NewName*.
If at least one bad character was found, TRUE is returned.
*******************************************************************************/
local int i, CharCode;

	logger(LOG_Debug, "AdjustPlayerName", "CurrentName='" $ CurrentName $ "', Len(CurrentName)=" $ len(CurrentName));
	NewName = "";
	for (i = 0; i < len(CurrentName); i++)
	{
		CharCode = asc(mid(CurrentName, i, 1));
		logger(LOG_Debug, "AdjustPlayerName", mid(CurrentName, i, 1) $ "=" $ CharCode);
		if (class'SharedCode'.static.CharIsWhite(CharCode) || CharCode > 255)
			NewName = NewName $ "_";
		else
			NewName = NewName $ Chr(CharCode);
	}
	if (CurrentName == NewName)
	{
		logger(LOG_All, "AdjustPlayerName", "'" $ CurrentName $ "' has no bad chars, keeping this name");
		return false;
	}
	else
	{
		logger(LOG_Debug, "AdjustPlayerName", "changed" @ CurrentName @ "to '" $ NewName $ "'");
		return true;
	}
}
static function bool CharIsWhite

Code: Select all

static function bool CharIsWhite(int charcode) {
/******************************************************************************
Returns TRUE if the char with *charcode* is a white char (= no ink is used if
printed).
******************************************************************************/

	switch (charcode) {
		case 9: // tab
			return true;
		case 10: // LF
			return true;
		case 11: // vTab
			return true;
		case 12: // FF
			return true;
		case 13: // CR
			return true;
		case 32: // Space
			return true;
		case 0xAD: // soft hyphen
			return true;
	}
	if (charcode <= 255)
		return false;

	// what about unicode?
	return false;
}
But it does not catch the <Device Control String> (DCS)?I tried it and the result is:
The log file wrote:Level server received: JOIN
Join request: MH-GreatescapeV0?Name=??Class=BotPack.TFemale1?Team=255?skin=FCommandoSkins_Negaverse_Titus.ttus?Face=FCommandoSkins_Negaverse_Titus.titus?Voice=BotPack.VoiceFemaleTwo?Override
Team 255
Login: ?
2023-06-16 01:18:19 MH-GreatescapeV0.MonsterHuntSB0.AdjustPlayerName LOG_Debug: CurrentName='?', Len(CurrentName)=1
2023-06-16 01:18:19 MH-GreatescapeV0.MonsterHuntSB0.AdjustPlayerName LOG_Debug: ?=144
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
sektor2111
Godlike
Posts: 6412
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: How to detect bad characters in player names?

Post by sektor2111 »

Barbie wrote: Fri Jun 16, 2023 12:29 am To avoid using characters that are not proper displayed in UT
Interesting...
Subject it's more extended, they are not only not displayed in UT (Nexgen does lousy records loading database with crap), but when you check stats at Game-Tracker there will be also some funky ID which has nothing to do with the clown from game. I would try to figure implementing such corrections in PreLogin using XC_Engine but... last time it did not work, or... I failed to figure something at this point...

Edit: "AlternatePath"
Perhaps I'll edit server's run-line with something more cute related to NexgenPlayerLookUp.ini file. I'll save it after each auto-stop and adding a fresh EMPTY file before start - as an "auto-update" stuff (automatic update is already set). Old file will be LZMA compressed and things are getting clean excepting server from processing a long array with trash (that version has a 25,000 elements array - a SLOW-DOWN).
User avatar
The_Cowboy
Skilled
Posts: 165
Joined: Mon Jan 24, 2011 3:22 am
Personal rank: Codezilla

Re: How to detect bad characters in player names?

Post by The_Cowboy »

Barbie wrote: Fri Jun 16, 2023 12:29 am But it does not catch the <Device Control String> (DCS)?
Hmm, there may be native way to detect them (https://www.geeksforgeeks.org/iscntrl-c ... haracters/). Let me know if that is the thing (or give me the relevant c++ code). I can try to hook a native function for you (windows and linux only).
Feralidragon wrote:Trial and error is sometimes better than any tutorial, because we learn how it works for ourselfs, which kills any doubts about anything :tu:
Patreon: https://www.patreon.com/FreeandOpen
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: How to detect bad characters in player names?

Post by Barbie »

Thanks for answers.
I think the problem lies in handling of different characters in different pages and unicode, but with the same char code: Len(<Device Control String>) returns "1" correctly, and Asc(<Device Control String>) returns 0x90 (144 decimal) what is <DCS> in UTF-16 and UTF-32, unused in code page Windows-1252, Latin capital letter E with acute ("É") in code page 437, etc.

I think I'll stick to UTF-16 and extend the function CharIsWhite with the unused characters of code page Windows-1252.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Post Reply