Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from derhass/for_roncli/server_validate_playe…
…r_names Validate player names on the server
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using HarmonyLib; | ||
using Overload; | ||
|
||
namespace GameMod { | ||
/// <summary> | ||
/// Prevents players to use invalid / empty names | ||
/// - 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 (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")] | ||
class MPValidatePlayerNames { | ||
public static void Prefix(PlayerLobbyData pld) { | ||
if (pld != null) { | ||
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.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 (FontInfo.IsInCharset((int)ch) && PilotManager.IsValidPilotNameChar(ch)) { | ||
nameValid = true; | ||
} else { | ||
// replace invalid characters by '$' | ||
pld.m_name = pld.m_name.Replace(ch, '$'); | ||
} | ||
} | ||
} | ||
if (!nameValid) { | ||
pld.m_name = "<INVALID NAME>"; | ||
} | ||
} | ||
} | ||
} | ||
} |