MiniYaml becomes an immutable data structure.

This changeset is motivated by a simple concept - get rid of the MiniYaml.Clone and MiniYamlNode.Clone methods to avoid deep copying yaml trees during merging. MiniYaml becoming immutable allows the merge function to reuse existing yaml trees rather than cloning them, saving on memory and improving merge performance. On initial loading the YAML for all maps is processed, so this provides a small reduction in initial loading time.

The rest of the changeset is dealing with the change in the exposed API surface. Some With* helper methods are introduced to allow creating new YAML from existing YAML. Areas of code that generated small amounts of YAML are able to transition directly to the immutable model without too much ceremony. Some use cases are far less ergonomic even with these helper methods and so a MiniYamlBuilder is introduced to retain mutable creation functionality. This allows those areas to continue to use the old mutable structures. The main users are the update rules and linting capabilities.
This commit is contained in:
RoosterDragon
2023-05-06 10:39:15 +01:00
committed by Gustas
parent b6a5d19871
commit 949ba589c0
104 changed files with 722 additions and 393 deletions

View File

@@ -350,6 +350,8 @@ namespace OpenRA.Mods.D2k.UtilityCommands
void FillMap()
{
var actorNodes = new List<MiniYamlNode>();
var playerNodes = new List<MiniYamlNode>();
while (stream.Position < stream.Length)
{
var tileInfo = stream.ReadUInt16();
@@ -379,9 +381,10 @@ namespace OpenRA.Mods.D2k.UtilityCommands
new OwnerInit(kvp.Owner)
};
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, a.Save()));
actorNodes.Add(new MiniYamlNode("Actor" + (map.ActorDefinitions.Count + actorNodes.Count), a.Save()));
if (map.PlayerDefinitions.All(x => x.Value.Nodes.Single(y => y.Key == "Name").Value.Value != kvp.Owner))
if (map.PlayerDefinitions.Concat(playerNodes).All(
x => x.Value.Nodes.Single(y => y.Key == "Name").Value.Value != kvp.Owner))
{
var playerInfo = PlayerReferenceDataByPlayerName[kvp.Owner];
var playerReference = new PlayerReference
@@ -394,7 +397,7 @@ namespace OpenRA.Mods.D2k.UtilityCommands
};
var node = new MiniYamlNode($"{nameof(PlayerReference)}@{kvp.Owner}", FieldSaver.SaveDifferences(playerReference, new PlayerReference()));
map.PlayerDefinitions.Add(node);
playerNodes.Add(node);
}
if (kvp.Actor == "mpspawn")
@@ -402,6 +405,9 @@ namespace OpenRA.Mods.D2k.UtilityCommands
}
}
}
map.ActorDefinitions = map.ActorDefinitions.Concat(actorNodes).ToArray();
map.PlayerDefinitions = map.PlayerDefinitions.Concat(playerNodes).ToArray();
}
CPos GetCurrentTilePositionOnMap()