Files
OpenRA/OpenRA.Mods.Common/UpdateRules/Rules/CopyIsometricSelectableHeight.cs
RoosterDragon 949ba589c0 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.
2023-08-07 21:57:10 +03:00

80 lines
2.3 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.IO;
using OpenRA.Mods.Common.FileFormats;
namespace OpenRA.Mods.Common.UpdateRules.Rules
{
public class CopyIsometricSelectableHeight : UpdateRule
{
public override string Name => "Copy IsometricSelectable.Height from art*.ini definitions.";
public override string Description =>
"Reads building Height entries art.ini/artfs.ini/artmd.ini from the current working directory\n" +
"and adds IsometricSelectable definitions to matching actors.";
static readonly string[] SourceFiles = { "art.ini", "artfs.ini", "artmd.ini" };
readonly Dictionary<string, int> selectionHeight = new();
bool complete;
public override IEnumerable<string> BeforeUpdate(ModData modData)
{
if (complete)
yield break;
var grid = Game.ModData.Manifest.Get<MapGrid>();
foreach (var filename in SourceFiles)
{
if (!File.Exists(filename))
continue;
var file = new IniFile(File.Open(filename, FileMode.Open));
foreach (var section in file.Sections)
{
if (!section.Contains("Height"))
continue;
selectionHeight[section.Name] = (int)(float.Parse(section.GetValue("Height", "1")) * grid.TileSize.Height);
}
}
}
public override IEnumerable<string> AfterUpdate(ModData modData)
{
// Rule only applies to the default ruleset - skip maps
complete = true;
yield break;
}
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode)
{
if (complete || actorNode.LastChildMatching("IsometricSelectable") != null)
yield break;
if (!selectionHeight.TryGetValue(actorNode.Key.ToLowerInvariant(), out var height))
yield break;
// Don't redefine the default value
if (height == 24)
yield break;
var selection = new MiniYamlNodeBuilder("IsometricSelectable", "");
selection.AddNode("Height", FieldSaver.FormatValue(height));
actorNode.AddNode(selection);
}
}
}