Sanitize saved skirmish factions.

This commit is contained in:
Paul Chote
2025-02-22 09:51:21 +00:00
committed by Gustas
parent 3f4e4db836
commit 4ea0e70589
3 changed files with 18 additions and 12 deletions

View File

@@ -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<FactionInfo>()
.Where(f => f.Selectable).Select(f => f.InternalName)
.ToList();
var faction = parts[1];
if (!factions.Contains(faction))
var isValidFaction = server.Map.WorldActorInfo.TraitInfos<FactionInfo>()
.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<string> validFactions)
{
return !validFactions.Contains(askedFaction) ? "Random" : askedFaction;
}
static string MissionBriefingOrDefault(S server)
{
var missionData = server.Map.WorldActorInfo.TraitInfoOrDefault<MissionDataInfo>();

View File

@@ -92,12 +92,18 @@ namespace OpenRA.Mods.Common.Server
}
}
var selectableFactions = server.Map.WorldActorInfo.TraitInfos<FactionInfo>()
.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]);
}

View File

@@ -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.