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

@@ -171,6 +171,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
}
}
var nodes = new List<MiniYamlNode>();
foreach (var cell in map.AllCells)
{
var overlayType = overlayPack[overlayIndex[cell]];
@@ -180,7 +181,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
if (TryHandleOverlayToActorInner(cell, overlayPack, overlayIndex, overlayType, out var ar))
{
if (ar != null)
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
nodes.Add(new MiniYamlNode("Actor" + (map.ActorDefinitions.Count + nodes.Count), ar.Save()));
continue;
}
@@ -196,10 +197,13 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
Console.WriteLine($"Cell {cell}: unknown overlay {overlayType}");
}
map.ActorDefinitions = map.ActorDefinitions.Concat(nodes).ToArray();
}
protected virtual void ReadWaypoints(Map map, IniFile file, int2 fullSize)
{
var nodes = new List<MiniYamlNode>();
var waypointsSection = file.GetSection("Waypoints", true);
foreach (var kv in waypointsSection)
{
@@ -214,12 +218,15 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
new OwnerInit("Neutral")
};
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
nodes.Add(new MiniYamlNode("Actor" + (map.ActorDefinitions.Count + nodes.Count), ar.Save()));
}
map.ActorDefinitions = map.ActorDefinitions.Concat(nodes).ToArray();
}
protected virtual void ReadTerrainActors(Map map, IniFile file, int2 fullSize)
{
var nodes = new List<MiniYamlNode>();
var terrainSection = file.GetSection("Terrain", true);
foreach (var kv in terrainSection)
{
@@ -238,12 +245,15 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
if (!map.Rules.Actors.ContainsKey(name))
Console.WriteLine($"Ignoring unknown actor type: `{name}`");
else
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
nodes.Add(new MiniYamlNode("Actor" + (map.ActorDefinitions.Count + nodes.Count), ar.Save()));
}
map.ActorDefinitions = map.ActorDefinitions.Concat(nodes).ToArray();
}
protected virtual void ReadActors(Map map, IniFile file, string type, int2 fullSize)
{
var nodes = new List<MiniYamlNode>();
var structuresSection = file.GetSection(type, true);
foreach (var kv in structuresSection)
{
@@ -296,8 +306,10 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
if (!map.Rules.Actors.ContainsKey(name))
Console.WriteLine($"Ignoring unknown actor type: `{name}`");
else
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
nodes.Add(new MiniYamlNode("Actor" + (map.ActorDefinitions.Count + nodes.Count), ar.Save()));
}
map.ActorDefinitions = map.ActorDefinitions.Concat(nodes).ToArray();
}
protected virtual void ReadLighting(Map map, IniFile file)
@@ -340,10 +352,13 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
if (lightingNodes.Count > 0)
{
map.RuleDefinitions.Nodes.Add(new MiniYamlNode("^BaseWorld", new MiniYaml("", new List<MiniYamlNode>()
map.RuleDefinitions = map.RuleDefinitions.WithNodesAppended(new[]
{
new MiniYamlNode("TerrainLighting", new MiniYaml("", lightingNodes))
})));
new MiniYamlNode("^BaseWorld", new MiniYaml("", new[]
{
new MiniYamlNode("TerrainLighting", new MiniYaml("", lightingNodes))
}))
});
}
}
@@ -357,6 +372,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
{ "LightBlueTint", "BlueTint" },
};
var nodes = new List<MiniYamlNode>();
foreach (var lamp in LampActors)
{
var lightingSection = file.GetSection(lamp, true);
@@ -380,12 +396,14 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
if (lightingNodes.Count > 0)
{
map.RuleDefinitions.Nodes.Add(new MiniYamlNode(lamp, new MiniYaml("", new List<MiniYamlNode>()
nodes.Add(new MiniYamlNode(lamp, new MiniYaml("", new[]
{
new MiniYamlNode("TerrainLightSource", new MiniYaml("", lightingNodes))
})));
}
}
map.RuleDefinitions = map.RuleDefinitions.WithNodesAppended(nodes);
}
protected virtual void SetInteractableBounds(Map map, int[] iniBounds)