From f6278a1f415e1846dcd42924234abef905764b5c Mon Sep 17 00:00:00 2001 From: reaperrr Date: Fri, 15 Jun 2018 00:29:59 +0200 Subject: [PATCH] Add some more useful extensions to UpdateUtils In many situations, these will save up to 3-4 lines, because quite often this will be enough to avoid curly brackets, "if"s or one-by-on renamings. --- OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs b/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs index df57c1df6e..23eda33dc5 100644 --- a/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs +++ b/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs @@ -273,6 +273,19 @@ namespace OpenRA.Mods.Common.UpdateRules node.Value.Nodes.Remove(toRemove); } + public static void MoveNode(this MiniYamlNode node, MiniYamlNode fromNode, MiniYamlNode toNode) + { + toNode.Value.Nodes.Add(node); + fromNode.Value.Nodes.Remove(node); + } + + public static void MoveAndRenameNode(this MiniYamlNode node, + MiniYamlNode fromNode, MiniYamlNode toNode, string newKey, bool preserveSuffix = true, bool includeRemovals = true) + { + node.RenameKey(newKey, preserveSuffix, includeRemovals); + node.MoveNode(fromNode, toNode); + } + /// Removes children with keys equal to [match] or [match]@[arbitrary suffix] public static int RemoveNodes(this MiniYamlNode node, string match, bool ignoreSuffix = true, bool includeRemovals = true) { @@ -307,5 +320,12 @@ namespace OpenRA.Mods.Common.UpdateRules { return node.ChildrenMatching(match, includeRemovals).LastOrDefault(); } + + public static void RenameChildrenMatching(this MiniYamlNode node, string match, string newKey, bool preserveSuffix = true, bool includeRemovals = true) + { + var matching = node.ChildrenMatching(match); + foreach (var m in matching) + m.RenameKey(newKey, preserveSuffix, includeRemovals); + } } }