Add tests for FieldLoader and FieldSaver

Test coverage for these classes will help prevent regressions. Major breaking changes are avoided since config files may already rely on various aspects of the behaviour, but some small breaking changes are made:

- Use `value.Split(Comma, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)` consistently for all comma-separated parsing.
- Rename LoadField to LoadFieldOrProperty for clarity - since this is the one method that cares about properties in a class otherwise focused only on fields.
- Use TryParse instead of Parse and ensure to call InvalidValueAction from all parsers so we throw the intended exception when badly formatted data is encountered.
- BooleanExpression/IntegerExpression exception message updated to align with the generic one.
- Remove FromYamlKey, and update the only user SkirmishLogic to perform this logic manually instead.
- Remove unused includePrivateByDefault parameter on GetTypeLoadInfo.
- Remove unused AllowEmptyEntriesAttribute.
This commit is contained in:
RoosterDragon
2025-11-15 12:03:39 +00:00
committed by Gustas Kažukauskas
parent 7d0340ad41
commit bc1a901f54
13 changed files with 1198 additions and 200 deletions

View File

@@ -24,7 +24,9 @@ namespace OpenRA.Mods.Common.Server
{
sealed class SkirmishSlot
{
[FieldLoader.Serialize(FromYamlKey = true)]
static string LoadSlot(MiniYaml yaml) => yaml.Value;
[FieldLoader.LoadUsing(nameof(LoadSlot))]
public readonly string Slot;
public readonly Color Color;
public readonly string Faction;
@@ -54,6 +56,12 @@ namespace OpenRA.Mods.Common.Server
c.Team = s.Team;
c.Handicap = s.Handicap;
}
public MiniYaml ToYaml()
{
var yaml = FieldSaver.Save(this);
return yaml.WithValue(Slot).WithNodes(yaml.Nodes.RemoveAll(n => n.Key == nameof(Slot)));
}
}
static bool TryInitializeFromFile(S server, string path, Connection conn)
@@ -156,9 +164,9 @@ namespace OpenRA.Mods.Common.Server
new("Map", server.LobbyInfo.GlobalSettings.Map),
new("Options", new MiniYaml("", server.LobbyInfo.GlobalSettings.LobbyOptions
.Select(kv => new MiniYamlNode(kv.Key, kv.Value.Value)))),
new("Player", FieldSaver.Save(new SkirmishSlot(playerClient))),
new("Player", new SkirmishSlot(playerClient).ToYaml()),
new("Bots", new MiniYaml("", server.LobbyInfo.Clients.Where(c => c.IsBot)
.Select(b => new MiniYamlNode(b.Bot, FieldSaver.Save(new SkirmishSlot(b))))))
.Select(b => new MiniYamlNode(b.Bot, new SkirmishSlot(b).ToYaml()))))
}.WriteToFile(path);
}