diff --git a/OpenRA.Game/Network/UnitOrders.cs b/OpenRA.Game/Network/UnitOrders.cs index be3ff8fef2..0355076aed 100644 --- a/OpenRA.Game/Network/UnitOrders.cs +++ b/OpenRA.Game/Network/UnitOrders.cs @@ -152,6 +152,9 @@ namespace OpenRA.Network break; } + Game.Settings.Player.Name = Settings.SanitizedPlayerName(Game.Settings.Player.Name); + Game.Settings.Save(); + // Otherwise send the handshake with our current settings and let the server reject us var info = new Session.Client() { diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 1985dbf944..2d0f117e81 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -130,6 +130,9 @@ namespace OpenRA.Server Port = localEndpoint.Port; Settings = settings; + + Settings.Name = OpenRA.Settings.SanitizedServerName(Settings.Name); + ModData = modData; randomSeed = (int)DateTime.Now.ToBinary(); @@ -286,7 +289,7 @@ namespace OpenRA.Server var client = new Session.Client() { - Name = handshake.Client.Name, + Name = OpenRA.Settings.SanitizedPlayerName(handshake.Client.Name), IpAddress = ((IPEndPoint)newConn.Socket.RemoteEndPoint).Address.ToString(), Index = newConn.PlayerIndex, Slot = LobbyInfo.FirstEmptySlot(), diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index e5240e58d4..c6e890b847 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -11,7 +11,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using OpenRA.Graphics; +using OpenRA.Traits; namespace OpenRA { @@ -301,6 +303,46 @@ namespace OpenRA root.WriteToFile(settingsFile); } + static string SanitizedName(string dirty) + { + if (string.IsNullOrEmpty(dirty)) + return null; + + var clean = dirty; + + // reserved characters for MiniYAML and JSON + var disallowedChars = new char[] { '#', '@', ':', '\n', '\t', '[', ']', '{', '}', '"', '`' }; + foreach (var disallowedChar in disallowedChars) + clean = clean.Replace(disallowedChar.ToString(), string.Empty); + + // avoid UI glitches + if (clean.Length > 16) + clean = clean.Substring(0, 16); + + return clean; + } + + public static string SanitizedServerName(string dirty) + { + var clean = SanitizedName(dirty); + if (string.IsNullOrWhiteSpace(clean)) + return new ServerSettings().Name; + else + return clean; + } + + public static string SanitizedPlayerName(string dirty) + { + var forbiddenNames = new string[] { "Open", "Closed" }; + var botNames = OpenRA.Game.ModData.DefaultRules.Actors["player"].Traits.WithInterface().Select(t => t.Name); + + var clean = SanitizedName(dirty); + if (string.IsNullOrWhiteSpace(clean) || forbiddenNames.Contains(clean) || botNames.Contains(clean)) + clean = new PlayerSettings().Name; + + return clean; + } + static void LoadSectionYaml(MiniYaml yaml, object section) { var defaults = Activator.CreateInstance(section.GetType()); diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index db261a4aed..e7eff5123f 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -717,9 +717,13 @@ namespace OpenRA.Mods.Common.Server { "name", s => { - Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, s); - server.SendMessage("{0} is now known as {1}.".F(client.Name, s)); - client.Name = s; + var sanitizedName = OpenRA.Settings.SanitizedPlayerName(s); + if (sanitizedName == client.Name) + return true; + + Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, sanitizedName); + server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName)); + client.Name = sanitizedName; server.SyncLobbyClients(); return true; } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs b/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs index 13f6ebaf57..f40cc67c53 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Lobby/LobbyUtils.cs @@ -266,6 +266,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic if (name.Text == c.Name) return; + name.Text = Settings.SanitizedPlayerName(name.Text); + orderManager.IssueOrder(Order.Command("name " + name.Text)); Game.Settings.Player.Name = name.Text; Game.Settings.Save(); diff --git a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs index 7ba181328b..d95406dc7b 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs @@ -56,8 +56,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic panel.Get("MAP_NAME").GetText = () => preview.Title; } - panel.Get("SERVER_NAME").Text = settings.Server.Name ?? ""; + var serverName = panel.Get("SERVER_NAME"); + serverName.Text = Settings.SanitizedServerName(settings.Server.Name); + serverName.OnEnterKey = () => { serverName.YieldKeyboardFocus(); return true; }; + serverName.OnLoseFocus = () => + { + serverName.Text = Settings.SanitizedServerName(serverName.Text); + settings.Server.Name = serverName.Text; + }; + panel.Get("LISTEN_PORT").Text = settings.Server.ListenPort.ToString(); + advertiseOnline = Game.Settings.Server.AdvertiseOnline; var externalPort = panel.Get("EXTERNAL_PORT"); @@ -81,7 +90,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic void CreateAndJoin() { - var name = panel.Get("SERVER_NAME").Text; + var name = Settings.SanitizedServerName(panel.Get("SERVER_NAME").Text); int listenPort, externalPort; if (!Exts.TryParseIntegerInvariant(panel.Get("LISTEN_PORT").Text, out listenPort)) listenPort = 1234; diff --git a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs index 2e02fa6634..fa330b69fb 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs @@ -204,9 +204,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic var ps = Game.Settings.Player; var nameTextfield = panel.Get("PLAYERNAME"); - nameTextfield.Text = ps.Name; + nameTextfield.Text = Settings.SanitizedPlayerName(ps.Name); nameTextfield.OnEnterKey = () => { nameTextfield.YieldKeyboardFocus(); return true; }; - nameTextfield.OnLoseFocus = () => { ps.Name = nameTextfield.Text; }; + nameTextfield.OnLoseFocus = () => + { + nameTextfield.Text = Settings.SanitizedPlayerName(nameTextfield.Text); + ps.Name = nameTextfield.Text; + }; var colorPreview = panel.Get("COLOR_MANAGER"); colorPreview.Color = ps.Color;