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)
|
if (intensity == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
intensity.RenameKeyPreservingSuffix("Duration");
|
intensity.RenameKey("Duration");
|
||||||
}
|
}
|
||||||
|
|
||||||
yield break;
|
yield break;
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
|||||||
mobileNode.RemoveNodes("Subterranean");
|
mobileNode.RemoveNodes("Subterranean");
|
||||||
var conditionNode = mobileNode.LastChildMatching("SubterraneanCondition");
|
var conditionNode = mobileNode.LastChildMatching("SubterraneanCondition");
|
||||||
if (conditionNode != null)
|
if (conditionNode != null)
|
||||||
conditionNode.RenameKeyPreservingSuffix("Condition");
|
conditionNode.RenameKey("Condition");
|
||||||
|
|
||||||
var transitionImageNode = mobileNode.LastChildMatching("SubterraneanTransitionImage");
|
var transitionImageNode = mobileNode.LastChildMatching("SubterraneanTransitionImage");
|
||||||
var transitionSequenceNode = mobileNode.LastChildMatching("SubterraneanTransitionSequence");
|
var transitionSequenceNode = mobileNode.LastChildMatching("SubterraneanTransitionSequence");
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
|||||||
{
|
{
|
||||||
foreach (var turret in actorNode.ChildrenMatching("WithReloadingSpriteTurret"))
|
foreach (var turret in actorNode.ChildrenMatching("WithReloadingSpriteTurret"))
|
||||||
{
|
{
|
||||||
turret.RenameKeyPreservingSuffix("WithSpriteTurret");
|
turret.RenameKey("WithSpriteTurret");
|
||||||
locations.Add("{0} ({1})".F(actorNode.Key, turret.Location.Filename));
|
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)
|
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"))
|
foreach (var eios in actorNode.ChildrenMatching("EmitInfantryOnSell"))
|
||||||
{
|
{
|
||||||
eios.RenameKeyPreservingSuffix("SpawnActorsOnSell");
|
eios.RenameKey("SpawnActorsOnSell");
|
||||||
var actortypes = eios.LastChildMatching("ActorTypes");
|
var actortypes = eios.LastChildMatching("ActorTypes");
|
||||||
if (actortypes == null)
|
if (actortypes == null)
|
||||||
eios.AddNode("ActorTypes", "e1");
|
eios.AddNode("ActorTypes", "e1");
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
|||||||
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
|
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
|
||||||
{
|
{
|
||||||
foreach (var spawner in actorNode.ChildrenMatching("WormSpawner"))
|
foreach (var spawner in actorNode.ChildrenMatching("WormSpawner"))
|
||||||
spawner.RenameKeyPreservingSuffix("ActorSpawner");
|
spawner.RenameKey("ActorSpawner");
|
||||||
|
|
||||||
foreach (var manager in actorNode.ChildrenMatching("WormManager"))
|
foreach (var manager in actorNode.ChildrenMatching("WormManager"))
|
||||||
{
|
{
|
||||||
manager.RenameKeyPreservingSuffix("ActorSpawnManager");
|
manager.RenameKey("ActorSpawnManager");
|
||||||
var signature = manager.LastChildMatching("WormSignature");
|
var signature = manager.LastChildMatching("WormSignature");
|
||||||
if (signature != null)
|
if (signature != null)
|
||||||
signature.RenameKeyPreservingSuffix("Actors");
|
signature.RenameKey("Actors");
|
||||||
}
|
}
|
||||||
|
|
||||||
yield break;
|
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 aren't null, split/copy everything relevant to the new WithTurretAimAnimation.
|
||||||
// If both are null (extremely unlikely), do nothing.
|
// If both are null (extremely unlikely), do nothing.
|
||||||
if (attackSequence != null && aimSequence == null)
|
if (attackSequence != null && aimSequence == null)
|
||||||
attackSequence.RenameKeyPreservingSuffix("Sequence");
|
attackSequence.RenameKey("Sequence");
|
||||||
else if (attackSequence == null && aimSequence == null && reloadPrefix != null)
|
else if (attackSequence == null && aimSequence == null && reloadPrefix != null)
|
||||||
{
|
{
|
||||||
turretAttack.RemoveNode(reloadPrefix);
|
turretAttack.RemoveNode(reloadPrefix);
|
||||||
turretAttack.RenameKeyPreservingSuffix("WithTurretAimAnimation");
|
turretAttack.RenameKey("WithTurretAimAnimation");
|
||||||
}
|
}
|
||||||
else if (attackSequence == null && aimSequence != null)
|
else if (attackSequence == null && aimSequence != null)
|
||||||
{
|
{
|
||||||
turretAttack.RenameKeyPreservingSuffix("WithTurretAimAnimation");
|
turretAttack.RenameKey("WithTurretAimAnimation");
|
||||||
aimSequence.RenameKeyPreservingSuffix("Sequence");
|
aimSequence.RenameKey("Sequence");
|
||||||
if (reloadPrefix != null)
|
if (reloadPrefix != null)
|
||||||
turretAttack.RemoveNode(reloadPrefix);
|
turretAttack.RemoveNode(reloadPrefix);
|
||||||
}
|
}
|
||||||
else if (attackSequence != null && aimSequence != null)
|
else if (attackSequence != null && aimSequence != null)
|
||||||
{
|
{
|
||||||
var turretAim = new MiniYamlNode("WithTurretAimAnimation", "");
|
var turretAim = new MiniYamlNode("WithTurretAimAnimation", "");
|
||||||
aimSequence.RenameKeyPreservingSuffix("Sequence");
|
aimSequence.RenameKey("Sequence");
|
||||||
turretAim.AddNode(aimSequence);
|
turretAim.AddNode(aimSequence);
|
||||||
turretAttack.RemoveNode(aimSequence);
|
turretAttack.RemoveNode(aimSequence);
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
|||||||
if (armament != null)
|
if (armament != null)
|
||||||
turretAim.AddNode(armament);
|
turretAim.AddNode(armament);
|
||||||
|
|
||||||
attackSequence.RenameKeyPreservingSuffix("Sequence");
|
attackSequence.RenameKey("Sequence");
|
||||||
actorNode.AddNode(turretAim);
|
actorNode.AddNode(turretAim);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
|||||||
aimAnimLocations.Add(Tuple.Create(actorNode.Key, actorNode.Location.Filename));
|
aimAnimLocations.Add(Tuple.Create(actorNode.Key, actorNode.Location.Filename));
|
||||||
|
|
||||||
var aimAnim = new MiniYamlNode("WithTurretAimAnimation", "");
|
var aimAnim = new MiniYamlNode("WithTurretAimAnimation", "");
|
||||||
aimSequence.RenameKeyPreservingSuffix("Sequence");
|
aimSequence.RenameKey("Sequence");
|
||||||
aimAnim.AddNode(aimSequence);
|
aimAnim.AddNode(aimSequence);
|
||||||
spriteTurret.RemoveNode(aimSequence);
|
spriteTurret.RemoveNode(aimSequence);
|
||||||
actorNode.AddNode(aimAnim);
|
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 aren't null, split/copy everything relevant to the new WithAimAnimation.
|
||||||
// If both sequences and the prefix are null (extremely unlikely), do nothing.
|
// If both sequences and the prefix are null (extremely unlikely), do nothing.
|
||||||
if (attackSequence != null && aimSequence == null && reloadPrefix == null)
|
if (attackSequence != null && aimSequence == null && reloadPrefix == null)
|
||||||
attackSequence.RenameKeyPreservingSuffix("Sequence");
|
attackSequence.RenameKey("Sequence");
|
||||||
else if (attackSequence == null && aimSequence == null && reloadPrefix != null)
|
else if (attackSequence == null && aimSequence == null && reloadPrefix != null)
|
||||||
{
|
{
|
||||||
attackAnim.RemoveNode(reloadPrefix);
|
attackAnim.RemoveNode(reloadPrefix);
|
||||||
attackAnim.RenameKeyPreservingSuffix("WithAimAnimation");
|
attackAnim.RenameKey("WithAimAnimation");
|
||||||
}
|
}
|
||||||
else if (attackSequence == null && aimSequence != null)
|
else if (attackSequence == null && aimSequence != null)
|
||||||
{
|
{
|
||||||
attackAnim.RenameKeyPreservingSuffix("WithAimAnimation");
|
attackAnim.RenameKey("WithAimAnimation");
|
||||||
aimSequence.RenameKeyPreservingSuffix("Sequence");
|
aimSequence.RenameKey("Sequence");
|
||||||
if (reloadPrefix != null)
|
if (reloadPrefix != null)
|
||||||
attackAnim.RemoveNode(reloadPrefix);
|
attackAnim.RemoveNode(reloadPrefix);
|
||||||
}
|
}
|
||||||
else if (attackSequence != null && aimSequence != null)
|
else if (attackSequence != null && aimSequence != null)
|
||||||
{
|
{
|
||||||
var aimAnim = new MiniYamlNode("WithAimAnimation", "");
|
var aimAnim = new MiniYamlNode("WithAimAnimation", "");
|
||||||
aimSequence.RenameKeyPreservingSuffix("Sequence");
|
aimSequence.RenameKey("Sequence");
|
||||||
aimAnim.AddNode(aimSequence);
|
aimAnim.AddNode(aimSequence);
|
||||||
attackAnim.RemoveNode(aimSequence);
|
attackAnim.RemoveNode(aimSequence);
|
||||||
|
|
||||||
@@ -174,7 +174,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
|
|||||||
if (armament != null)
|
if (armament != null)
|
||||||
aimAnim.AddNode(armament);
|
aimAnim.AddNode(armament);
|
||||||
|
|
||||||
attackSequence.RenameKeyPreservingSuffix("Sequence");
|
attackSequence.RenameKey("Sequence");
|
||||||
actorNode.AddNode(aimAnim);
|
actorNode.AddNode(aimAnim);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -238,13 +238,14 @@ namespace OpenRA.Mods.Common.UpdateRules
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Renames a yaml key preserving any @suffix</summary>
|
/// <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);
|
var split = node.Key.IndexOf("@", StringComparison.Ordinal);
|
||||||
if (split == -1)
|
if (preserveSuffix && split > -1)
|
||||||
node.Key = newKey;
|
node.Key = prefix + newKey + node.Key.Substring(split);
|
||||||
else
|
else
|
||||||
node.Key = newKey + node.Key.Substring(split);
|
node.Key = prefix + newKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T NodeValue<T>(this MiniYamlNode node)
|
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>
|
/// <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>
|
/// <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)
|
if (node.Key == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (node.Key == match)
|
var prefix = includeRemovals && node.Key[0].ToString() == "-" ? "-" : "";
|
||||||
|
if (node.Key == prefix + match)
|
||||||
return true;
|
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('@');
|
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>
|
/// <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