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:
@@ -49,7 +49,7 @@ namespace OpenRA.Mods.Common.Graphics
|
||||
var sequences = new Dictionary<string, ISpriteSequence>();
|
||||
var node = imageNode.Value.Nodes.SingleOrDefault(n => n.Key == "Defaults");
|
||||
var defaults = node?.Value ?? NoData;
|
||||
imageNode.Value.Nodes.Remove(node);
|
||||
imageNode = imageNode.WithValue(imageNode.Value.WithNodes(imageNode.Value.Nodes.Remove(node)));
|
||||
|
||||
foreach (var sequenceNode in imageNode.Value.Nodes)
|
||||
{
|
||||
@@ -262,7 +262,7 @@ namespace OpenRA.Mods.Common.Graphics
|
||||
|
||||
protected static T LoadField<T>(string key, T fallback, MiniYaml data, MiniYaml defaults = null)
|
||||
{
|
||||
var node = data.Nodes.Find(n => n.Key == key) ?? defaults?.Nodes.Find(n => n.Key == key);
|
||||
var node = data.Nodes.FirstOrDefault(n => n.Key == key) ?? defaults?.Nodes.FirstOrDefault(n => n.Key == key);
|
||||
if (node == null)
|
||||
return fallback;
|
||||
|
||||
@@ -276,7 +276,7 @@ namespace OpenRA.Mods.Common.Graphics
|
||||
|
||||
protected static T LoadField<T>(SpriteSequenceField<T> field, MiniYaml data, MiniYaml defaults, out MiniYamlNode.SourceLocation location)
|
||||
{
|
||||
var node = data.Nodes.Find(n => n.Key == field.Key) ?? defaults?.Nodes.Find(n => n.Key == field.Key);
|
||||
var node = data.Nodes.FirstOrDefault(n => n.Key == field.Key) ?? defaults?.Nodes.FirstOrDefault(n => n.Key == field.Key);
|
||||
if (node == null)
|
||||
{
|
||||
location = default;
|
||||
@@ -414,10 +414,10 @@ namespace OpenRA.Mods.Common.Graphics
|
||||
var offset = LoadField(Offset, data, defaults);
|
||||
var blendMode = LoadField(BlendMode, data, defaults);
|
||||
|
||||
var combineNode = data.Nodes.Find(n => n.Key == Combine.Key);
|
||||
var combineNode = data.Nodes.FirstOrDefault(n => n.Key == Combine.Key);
|
||||
if (combineNode != null)
|
||||
{
|
||||
for (var i = 0; i < combineNode.Value.Nodes.Count; i++)
|
||||
for (var i = 0; i < combineNode.Value.Nodes.Length; i++)
|
||||
{
|
||||
var subData = combineNode.Value.Nodes[i].Value;
|
||||
var subOffset = LoadField(Offset, subData, NoData);
|
||||
|
||||
Reference in New Issue
Block a user