From 3c9891e729b9e5362ab4e8a6620e22f3a73ac425 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Mon, 21 May 2018 02:42:44 +0200 Subject: [PATCH] Refactor some update util extensions Using arguments instead of separate overloads, plus better support for automatically handling trait/property removals ('-' prefix). --- .../Rules/ChangeIntensityToDuration.cs | 2 +- .../UpdateRules/Rules/DefineLocomotors.cs | 2 +- .../Rules/RemoveWithReloadingSpriteTurret.cs | 2 +- .../Rules/RenameEmitInfantryOnSell.cs | 5 +-- .../UpdateRules/Rules/RenameWormSpawner.cs | 6 ++-- .../UpdateRules/Rules/SplitAimAnimations.cs | 26 +++++++-------- OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs | 32 +++++++++++-------- 7 files changed, 39 insertions(+), 36 deletions(-) diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/ChangeIntensityToDuration.cs b/OpenRA.Mods.Common/UpdateRules/Rules/ChangeIntensityToDuration.cs index 842a1bf8c0..d002cb8f6d 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/ChangeIntensityToDuration.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/ChangeIntensityToDuration.cs @@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules if (intensity == null) continue; - intensity.RenameKeyPreservingSuffix("Duration"); + intensity.RenameKey("Duration"); } yield break; diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/DefineLocomotors.cs b/OpenRA.Mods.Common/UpdateRules/Rules/DefineLocomotors.cs index dbae0af6e9..200dc8ca61 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/DefineLocomotors.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/DefineLocomotors.cs @@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules mobileNode.RemoveNodes("Subterranean"); var conditionNode = mobileNode.LastChildMatching("SubterraneanCondition"); if (conditionNode != null) - conditionNode.RenameKeyPreservingSuffix("Condition"); + conditionNode.RenameKey("Condition"); var transitionImageNode = mobileNode.LastChildMatching("SubterraneanTransitionImage"); var transitionSequenceNode = mobileNode.LastChildMatching("SubterraneanTransitionSequence"); diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/RemoveWithReloadingSpriteTurret.cs b/OpenRA.Mods.Common/UpdateRules/Rules/RemoveWithReloadingSpriteTurret.cs index 2f6537ee6e..02f68b117a 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/RemoveWithReloadingSpriteTurret.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/RemoveWithReloadingSpriteTurret.cs @@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules { foreach (var turret in actorNode.ChildrenMatching("WithReloadingSpriteTurret")) { - turret.RenameKeyPreservingSuffix("WithSpriteTurret"); + turret.RenameKey("WithSpriteTurret"); locations.Add("{0} ({1})".F(actorNode.Key, turret.Location.Filename)); } diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/RenameEmitInfantryOnSell.cs b/OpenRA.Mods.Common/UpdateRules/Rules/RenameEmitInfantryOnSell.cs index ce0f3eeee5..7f38906b64 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/RenameEmitInfantryOnSell.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/RenameEmitInfantryOnSell.cs @@ -28,12 +28,9 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules public override IEnumerable UpdateActorNode(ModData modData, MiniYamlNode actorNode) { - foreach (var uneios in actorNode.ChildrenMatching("-EmitInfantryOnSell")) - uneios.RenameKeyPreservingSuffix("-SpawnActorsOnSell"); - foreach (var eios in actorNode.ChildrenMatching("EmitInfantryOnSell")) { - eios.RenameKeyPreservingSuffix("SpawnActorsOnSell"); + eios.RenameKey("SpawnActorsOnSell"); var actortypes = eios.LastChildMatching("ActorTypes"); if (actortypes == null) eios.AddNode("ActorTypes", "e1"); diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/RenameWormSpawner.cs b/OpenRA.Mods.Common/UpdateRules/Rules/RenameWormSpawner.cs index bba8217ba1..f13a0d8385 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/RenameWormSpawner.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/RenameWormSpawner.cs @@ -28,14 +28,14 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules public override IEnumerable UpdateActorNode(ModData modData, MiniYamlNode actorNode) { foreach (var spawner in actorNode.ChildrenMatching("WormSpawner")) - spawner.RenameKeyPreservingSuffix("ActorSpawner"); + spawner.RenameKey("ActorSpawner"); foreach (var manager in actorNode.ChildrenMatching("WormManager")) { - manager.RenameKeyPreservingSuffix("ActorSpawnManager"); + manager.RenameKey("ActorSpawnManager"); var signature = manager.LastChildMatching("WormSignature"); if (signature != null) - signature.RenameKeyPreservingSuffix("Actors"); + signature.RenameKey("Actors"); } yield break; diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/SplitAimAnimations.cs b/OpenRA.Mods.Common/UpdateRules/Rules/SplitAimAnimations.cs index 42b78467d2..fdc43da965 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/SplitAimAnimations.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/SplitAimAnimations.cs @@ -74,23 +74,23 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules // If both aren't null, split/copy everything relevant to the new WithTurretAimAnimation. // If both are null (extremely unlikely), do nothing. if (attackSequence != null && aimSequence == null) - attackSequence.RenameKeyPreservingSuffix("Sequence"); + attackSequence.RenameKey("Sequence"); else if (attackSequence == null && aimSequence == null && reloadPrefix != null) { turretAttack.RemoveNode(reloadPrefix); - turretAttack.RenameKeyPreservingSuffix("WithTurretAimAnimation"); + turretAttack.RenameKey("WithTurretAimAnimation"); } else if (attackSequence == null && aimSequence != null) { - turretAttack.RenameKeyPreservingSuffix("WithTurretAimAnimation"); - aimSequence.RenameKeyPreservingSuffix("Sequence"); + turretAttack.RenameKey("WithTurretAimAnimation"); + aimSequence.RenameKey("Sequence"); if (reloadPrefix != null) turretAttack.RemoveNode(reloadPrefix); } else if (attackSequence != null && aimSequence != null) { var turretAim = new MiniYamlNode("WithTurretAimAnimation", ""); - aimSequence.RenameKeyPreservingSuffix("Sequence"); + aimSequence.RenameKey("Sequence"); turretAim.AddNode(aimSequence); turretAttack.RemoveNode(aimSequence); @@ -104,7 +104,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules if (armament != null) turretAim.AddNode(armament); - attackSequence.RenameKeyPreservingSuffix("Sequence"); + attackSequence.RenameKey("Sequence"); actorNode.AddNode(turretAim); } } @@ -118,7 +118,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules aimAnimLocations.Add(Tuple.Create(actorNode.Key, actorNode.Location.Filename)); var aimAnim = new MiniYamlNode("WithTurretAimAnimation", ""); - aimSequence.RenameKeyPreservingSuffix("Sequence"); + aimSequence.RenameKey("Sequence"); aimAnim.AddNode(aimSequence); spriteTurret.RemoveNode(aimSequence); actorNode.AddNode(aimAnim); @@ -144,23 +144,23 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules // If both sequences aren't null, split/copy everything relevant to the new WithAimAnimation. // If both sequences and the prefix are null (extremely unlikely), do nothing. if (attackSequence != null && aimSequence == null && reloadPrefix == null) - attackSequence.RenameKeyPreservingSuffix("Sequence"); + attackSequence.RenameKey("Sequence"); else if (attackSequence == null && aimSequence == null && reloadPrefix != null) { attackAnim.RemoveNode(reloadPrefix); - attackAnim.RenameKeyPreservingSuffix("WithAimAnimation"); + attackAnim.RenameKey("WithAimAnimation"); } else if (attackSequence == null && aimSequence != null) { - attackAnim.RenameKeyPreservingSuffix("WithAimAnimation"); - aimSequence.RenameKeyPreservingSuffix("Sequence"); + attackAnim.RenameKey("WithAimAnimation"); + aimSequence.RenameKey("Sequence"); if (reloadPrefix != null) attackAnim.RemoveNode(reloadPrefix); } else if (attackSequence != null && aimSequence != null) { var aimAnim = new MiniYamlNode("WithAimAnimation", ""); - aimSequence.RenameKeyPreservingSuffix("Sequence"); + aimSequence.RenameKey("Sequence"); aimAnim.AddNode(aimSequence); attackAnim.RemoveNode(aimSequence); @@ -174,7 +174,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules if (armament != null) aimAnim.AddNode(armament); - attackSequence.RenameKeyPreservingSuffix("Sequence"); + attackSequence.RenameKey("Sequence"); actorNode.AddNode(aimAnim); } } diff --git a/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs b/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs index 28f6d0cc7f..df57c1df6e 100644 --- a/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs +++ b/OpenRA.Mods.Common/UpdateRules/UpdateUtils.cs @@ -238,13 +238,14 @@ namespace OpenRA.Mods.Common.UpdateRules } /// Renames a yaml key preserving any @suffix - public static void RenameKeyPreservingSuffix(this MiniYamlNode node, string newKey) + public static void RenameKey(this MiniYamlNode node, string newKey, bool preserveSuffix = true, bool includeRemovals = true) { + var prefix = includeRemovals && node.Key[0].ToString() == "-" ? "-" : ""; var split = node.Key.IndexOf("@", StringComparison.Ordinal); - if (split == -1) - node.Key = newKey; + if (preserveSuffix && split > -1) + node.Key = prefix + newKey + node.Key.Substring(split); else - node.Key = newKey + node.Key.Substring(split); + node.Key = prefix + newKey; } public static T NodeValue(this MiniYamlNode node) @@ -273,33 +274,38 @@ namespace OpenRA.Mods.Common.UpdateRules } /// Removes children with keys equal to [match] or [match]@[arbitrary suffix] - public static int RemoveNodes(this MiniYamlNode node, string match) + public static int RemoveNodes(this MiniYamlNode node, string match, bool ignoreSuffix = true, bool includeRemovals = true) { - return node.Value.Nodes.RemoveAll(n => n.KeyMatches(match)); + return node.Value.Nodes.RemoveAll(n => n.KeyMatches(match, ignoreSuffix, includeRemovals)); } /// Returns true if the node is of the form or @arbitrary - public static bool KeyMatches(this MiniYamlNode node, string match) + public static bool KeyMatches(this MiniYamlNode node, string match, bool ignoreSuffix = true, bool includeRemovals = true) { if (node.Key == null) return false; - if (node.Key == match) + var prefix = includeRemovals && node.Key[0].ToString() == "-" ? "-" : ""; + if (node.Key == prefix + match) return true; + // If the previous check didn't return true and we wanted the suffix to match, return false unconditionally here + if (!ignoreSuffix) + return false; + var atPosition = node.Key.IndexOf('@'); - return atPosition > 0 && node.Key.Substring(0, atPosition) == match; + return atPosition > 0 && node.Key.Substring(0, atPosition) == prefix + match; } /// Returns children with keys equal to [match] or [match]@[arbitrary suffix] - public static IEnumerable ChildrenMatching(this MiniYamlNode node, string match) + public static IEnumerable ChildrenMatching(this MiniYamlNode node, string match, bool ignoreSuffix = true, bool includeRemovals = true) { - return node.Value.Nodes.Where(n => n.KeyMatches(match)); + return node.Value.Nodes.Where(n => n.KeyMatches(match, ignoreSuffix, includeRemovals)); } - public static MiniYamlNode LastChildMatching(this MiniYamlNode node, string match) + public static MiniYamlNode LastChildMatching(this MiniYamlNode node, string match, bool includeRemovals = true) { - return node.ChildrenMatching(match).LastOrDefault(); + return node.ChildrenMatching(match, includeRemovals).LastOrDefault(); } } }