Fix failure accounting, add comments

This commit is contained in:
Jon Kunkee 2020-06-28 16:40:21 -07:00
parent 4dfee8cfd6
commit 00e8edf453
2 changed files with 23 additions and 7 deletions

View file

@ -56,6 +56,7 @@ namespace NBTUtil
if (!string.IsNullOrEmpty(v)) if (!string.IsNullOrEmpty(v))
Values.Add(v); Values.Add(v);
}}, }},
{ "delete", "Delete the NBT tag if found", v => Command = ConsoleCommand.DeleteValue },
{ "help", "Print this help message", v => Command = ConsoleCommand.Help }, { "help", "Print this help message", v => Command = ConsoleCommand.Help },
{ "<>", v => { { "<>", v => {
switch (_currentKey) { switch (_currentKey) {
@ -65,7 +66,6 @@ namespace NBTUtil
break; break;
} }
}}, }},
{ "delete", "Delete the NBT tag if found", v => Command = ConsoleCommand.DeleteValue },
}; };
} }

View file

@ -29,6 +29,8 @@ namespace NBTUtil
public bool Run (string[] args) public bool Run (string[] args)
{ {
// Parse and validate command line arguments.
_options.Parse(args); _options.Parse(args);
if (_options.Command == ConsoleCommand.Help) if (_options.Command == ConsoleCommand.Help)
@ -48,6 +50,9 @@ namespace NBTUtil
var nodesToProcess = new List<DataNode>(); var nodesToProcess = new List<DataNode>();
// Iterate over all nodes matching the provided Path and create a list of the ones that can be processed
// using the provided ConsoleCommand.
foreach (var node in new NbtPathEnumerator(_options.Path)) foreach (var node in new NbtPathEnumerator(_options.Path))
{ {
if (op.CanProcess(node)) if (op.CanProcess(node))
@ -61,18 +66,29 @@ namespace NBTUtil
} }
} }
// Iterate over all the processable nodes and process them.
// Doing this separately from the CanProcess loop allows Process to make significant changes to the NBT
// tree like node deletion.
foreach (var targetNode in nodesToProcess) { foreach (var targetNode in nodesToProcess) {
// Since Process may render targetNode inoperable, save targetNode.Root beforehand.
var root = targetNode.Root; var root = targetNode.Root;
if (!op.Process(targetNode, _options)) { if (op.Process(targetNode, _options))
{
// Now that processing has succeeded, save the changes.
root.Save();
Console.WriteLine(targetNode.NodePath + ": OK");
successCount++;
}
else
{
// Since processing failed, discard any changes that may have been made. This prevents other
// iterations of this loop from saving them.
targetNode.RefreshNode();
Console.WriteLine(targetNode.NodePath + ": ERROR (apply)"); Console.WriteLine(targetNode.NodePath + ": ERROR (apply)");
failCount++; failCount++;
} }
root.Save();
Console.WriteLine(targetNode.NodePath + ": OK");
successCount++;
} }
Console.WriteLine("Operation complete. Nodes succeeded: {0} Nodes failed: {1}", successCount, failCount); Console.WriteLine("Operation complete. Nodes succeeded: {0} Nodes failed: {1}", successCount, failCount);