Refactor some update util extensions
Using arguments instead of separate overloads, plus better support for
automatically handling trait/property removals ('-' prefix).
This commit is contained in:
@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||
if (intensity == null)
|
||||
continue;
|
||||
|
||||
intensity.RenameKeyPreservingSuffix("Duration");
|
||||
intensity.RenameKey("Duration");
|
||||
}
|
||||
|
||||
yield break;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -28,12 +28,9 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||
|
||||
public override IEnumerable<string> 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");
|
||||
|
||||
@@ -28,14 +28,14 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||
public override IEnumerable<string> 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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,13 +238,14 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
}
|
||||
|
||||
/// <summary>Renames a yaml key preserving any @suffix</summary>
|
||||
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<T>(this MiniYamlNode node)
|
||||
@@ -273,33 +274,38 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
}
|
||||
|
||||
/// <summary>Removes children with keys equal to [match] or [match]@[arbitrary suffix]</summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>Returns true if the node is of the form <match> or <match>@arbitrary</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Returns children with keys equal to [match] or [match]@[arbitrary suffix]</summary>
|
||||
public static IEnumerable<MiniYamlNode> ChildrenMatching(this MiniYamlNode node, string match)
|
||||
public static IEnumerable<MiniYamlNode> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user