From 4ea0e70589ac1efc5a26649bb4dbd4ff7d1121fe Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 22 Feb 2025 09:51:21 +0000 Subject: [PATCH] Sanitize saved skirmish factions. --- .../ServerTraits/LobbyCommands.cs | 21 +++++++++---------- .../ServerTraits/SkirmishLogic.cs | 8 +++++++ mods/common/fluent/common.ftl | 1 - 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index 21ee5dd1f9..b07d439946 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -87,9 +87,6 @@ namespace OpenRA.Mods.Common.Server [FluentReference("faction")] const string InvalidFactionSelected = "notification-invalid-faction-selected"; - [FluentReference("factions")] - const string SupportedFactions = "notification-supported-factions"; - [FluentReference] const string RequiresHost = "notification-requires-host"; @@ -609,9 +606,8 @@ namespace OpenRA.Mods.Common.Server foreach (var c in server.LobbyInfo.Clients) { + c.Faction = SanitizePlayerFaction(server, c.Faction, selectableFactions); c.State = Session.ClientState.Invalid; - if (!selectableFactions.Contains(c.Faction)) - c.Faction = "Random"; } // Reassign players into new slots based on their old slots: @@ -1055,15 +1051,13 @@ namespace OpenRA.Mods.Common.Server if (server.LobbyInfo.Slots[targetClient.Slot].LockFaction) return true; - var factions = server.Map.WorldActorInfo.TraitInfos() - .Where(f => f.Selectable).Select(f => f.InternalName) - .ToList(); - var faction = parts[1]; - if (!factions.Contains(faction)) + var isValidFaction = server.Map.WorldActorInfo.TraitInfos() + .Any(f => f.Selectable && f.InternalName == client.Faction); + + if (!isValidFaction) { server.SendFluentMessageTo(conn, InvalidFactionSelected, new object[] { "faction", faction }); - server.SendFluentMessageTo(conn, SupportedFactions, new object[] { "factions", factions.JoinWith(", ") }); return true; } @@ -1438,6 +1432,11 @@ namespace OpenRA.Mods.Common.Server } } + public static string SanitizePlayerFaction(S server, string askedFaction, IEnumerable validFactions) + { + return !validFactions.Contains(askedFaction) ? "Random" : askedFaction; + } + static string MissionBriefingOrDefault(S server) { var missionData = server.Map.WorldActorInfo.TraitInfoOrDefault(); diff --git a/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs b/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs index 537ef429bd..bb4d42a699 100644 --- a/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs +++ b/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs @@ -92,12 +92,18 @@ namespace OpenRA.Mods.Common.Server } } + var selectableFactions = server.Map.WorldActorInfo.TraitInfos() + .Where(f => f.Selectable) + .Select(f => f.InternalName) + .ToList(); + var playerNode = nodes.NodeWithKeyOrDefault("Player"); if (playerNode != null) { var client = server.GetClient(conn); SkirmishSlot.DeserializeToClient(playerNode.Value, client); client.Color = LobbyCommands.SanitizePlayerColor(server, client.Color, client.Index); + client.Faction = LobbyCommands.SanitizePlayerFaction(server, client.Faction, selectableFactions); } var botsNode = nodes.NodeWithKeyOrDefault("Bots"); @@ -128,6 +134,8 @@ namespace OpenRA.Mods.Common.Server if (client.Slot != null && !server.LobbyInfo.Slots[client.Slot].LockColor) client.Color = LobbyCommands.SanitizePlayerColor(server, client.Color, client.Index); + client.Faction = LobbyCommands.SanitizePlayerFaction(server, client.Faction, selectableFactions); + server.LobbyInfo.Clients.Add(client); S.SyncClientToPlayerReference(client, server.Map.Players.Players[client.Slot]); } diff --git a/mods/common/fluent/common.ftl b/mods/common/fluent/common.ftl index 7ddb13959f..5ffd699e6a 100644 --- a/mods/common/fluent/common.ftl +++ b/mods/common/fluent/common.ftl @@ -17,7 +17,6 @@ notification-insufficient-enabled-spawn-points = Unable to start the game until notification-malformed-command = Malformed { $command } command. notification-state-unchanged-ready = Cannot change state when marked as ready. notification-invalid-faction-selected = Invalid faction selected: { $faction }. -notification-supported-factions = Supported values: { $factions }. notification-state-unchanged-game-started = State cannot be changed once the game has started ({ $command }). notification-requires-host = Only the host can do that. notification-invalid-bot-slot = Cannot add bots to a slot with another client.