Skip to content

Commit

Permalink
Allow all names which were allowed before
Browse files Browse the repository at this point in the history
  • Loading branch information
derhass committed May 6, 2023
1 parent 1780cd8 commit 52dc321
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions GameMod/MPValidatePlayerNames.cs
Expand Up @@ -5,12 +5,13 @@
namespace GameMod {
/// <summary>
/// Prevents players to use invalid / empty names
/// - all names must have at least one printable ASCII character
/// - spaces are allowed, but not at the beginning and end, and not exclusively
/// - other sorts of whitespaces are converted to space
/// - Non-ASCII characters are still allowed
/// - Only Uppercase characters are allowed, convert to uppercase in any case
/// - forbidden characters are the following set: "*/:<>?\|
/// as well as all non-prinable values, we replace all of these by $
/// - forbidden characters (as per IsValidPilotNameChar) and characters not
/// in the font are replaced by a $ sign
/// - At least one non-forbidden character must be present
/// - maximum length is 17 characters, longer names are cut
/// </summary>
[HarmonyPatch(typeof(Overload.Server), "ResolvePotentialNameCollision")]
Expand All @@ -20,23 +21,22 @@ class MPValidatePlayerNames {
bool nameValid = false;
if (!String.IsNullOrEmpty(pld.m_name)) {
pld.m_name = pld.m_name.ToUpper(); // only uppercase
pld.m_name = pld.m_name.Trim(); // replace any leading or trailing whitespaces
if (pld.m_name.Length > 17) { // at most 17 characters
pld.m_name = pld.m_name.Substring(0,17);
}
for (int i=0; i<pld.m_name.Length; i++) {
Char ch = pld.m_name[i];
if (Char.IsControl(ch) || ch == '"' || ch == '*' || ch == '/' || ch == ':' || ch == '<' || ch == '>' || ch == '?' || ch == '\\' || ch =='|') {
// ininvisible control characters or forbidden characters are replaced by `$`
pld.m_name = pld.m_name.Replace(ch, '$');
nameValid = true;
} else if ((Char.IsWhiteSpace(ch) || Char.IsSeparator(ch)) && ch != ' ') {
if ((Char.IsWhiteSpace(ch) || Char.IsSeparator(ch)) && ch != ' ') {
// all whitespace and separator charachters which aren't space are converted to space
pld.m_name = pld.m_name.Replace(ch, ' ');
} else if (ch > 32 && ch < 127) {
} else if (FontInfo.IsInCharset((int)ch) && PilotManager.IsValidPilotNameChar(ch)) {
nameValid = true;
} else {
// replace invalid characters by '$'
pld.m_name = pld.m_name.Replace(ch, '$');
}
}
pld.m_name = pld.m_name.Trim(); // replace any leading or trailing whitespaces
}
if (!nameValid) {
pld.m_name = "<INVALID NAME>";
Expand Down

0 comments on commit 52dc321

Please sign in to comment.