From 9f929b690bc687a291e3ce0953d35e16a99ecf6e Mon Sep 17 00:00:00 2001 From: ike709 Date: Thu, 21 Oct 2021 05:52:26 -0500 Subject: [PATCH] Restrict character names to alphanumerics via cvar (#4947) * Restrict character names to ASCII via cvar * Alphanumerics is better * name randomization Co-authored-by: ike709 --- Content.Shared/CCVar/CCVars.cs | 10 +++++++ .../Preferences/HumanoidCharacterProfile.cs | 29 +++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index 063b55fd02..0dbad615ae 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -461,5 +461,15 @@ namespace Content.Shared.CCVar /// public static readonly CVarDef AfkTime = CVarDef.Create("afk.time", 60f, CVar.SERVERONLY); + + /* + * IC + */ + + /// + /// Restricts IC character names to alphanumeric chars. + /// + public static readonly CVarDef RestrictedNames = + CVarDef.Create("ic.restricted_names", true, CVar.SERVER | CVar.REPLICATED); } } diff --git a/Content.Shared/Preferences/HumanoidCharacterProfile.cs b/Content.Shared/Preferences/HumanoidCharacterProfile.cs index 5b44b715b5..9ee15f22e2 100644 --- a/Content.Shared/Preferences/HumanoidCharacterProfile.cs +++ b/Content.Shared/Preferences/HumanoidCharacterProfile.cs @@ -1,11 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; +using Content.Shared.CCVar; using Content.Shared.CharacterAppearance; using Content.Shared.Dataset; using Content.Shared.GameTicking; using Content.Shared.Random.Helpers; using Content.Shared.Roles; +using Robust.Shared.Configuration; using Robust.Shared.Enums; using Robust.Shared.IoC; using Robust.Shared.Localization; @@ -261,7 +264,7 @@ namespace Content.Shared.Preferences string name; if (string.IsNullOrEmpty(Name)) { - name = "Urist McHands"; + name = RandomName(); } else if (Name.Length > MaxNameLength) { @@ -272,12 +275,18 @@ namespace Content.Shared.Preferences name = Name; } - // TODO: Avoid Z̨͇̙͉͎̭͔̼̿͋A͚̖̞̗̞͈̓̾̀ͩͩ̔L̟ͮ̈͝G̙O͍͎̗̺̺ͫ̀̽͊̓͝ͅ tier shenanigans. - // And other stuff like RTL overrides and such. - // Probably also emojis... - name = name.Trim(); + if (IoCManager.Resolve().GetCVar(CCVars.RestrictedNames)) + { + name = Regex.Replace(name, @"[^A-Z,a-z,0-9, -]", string.Empty); + } + + if (string.IsNullOrEmpty(name)) + { + name = RandomName(); + } + var appearance = HumanoidCharacterAppearance.EnsureValid(Appearance); var prefsUnavailableMode = PreferenceUnavailable switch @@ -337,6 +346,16 @@ namespace Content.Shared.Preferences _antagPreferences.Clear(); _antagPreferences.AddRange(antags); + + string RandomName() + { + if (Sex == null) return "Urist McHands"; // This shouldn't happen + var random = IoCManager.Resolve(); + var protoMan = IoCManager.Resolve(); + var firstName = random.Pick(Sex.FirstNames(protoMan).Values); + var lastName = random.Pick(protoMan.Index("names_last")); + return $"{firstName} {lastName}"; + } } public override bool Equals(object? obj)