From 00e8edf453306398a777e9f790c098aefbce7e7e Mon Sep 17 00:00:00 2001 From: Jon Kunkee Date: Sun, 28 Jun 2020 16:40:21 -0700 Subject: [PATCH] Fix failure accounting, add comments --- NBTUtil/ConsoleOptions.cs | 2 +- NBTUtil/ConsoleRunner.cs | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/NBTUtil/ConsoleOptions.cs b/NBTUtil/ConsoleOptions.cs index 863650d..b43ac3c 100644 --- a/NBTUtil/ConsoleOptions.cs +++ b/NBTUtil/ConsoleOptions.cs @@ -56,6 +56,7 @@ namespace NBTUtil if (!string.IsNullOrEmpty(v)) Values.Add(v); }}, + { "delete", "Delete the NBT tag if found", v => Command = ConsoleCommand.DeleteValue }, { "help", "Print this help message", v => Command = ConsoleCommand.Help }, { "<>", v => { switch (_currentKey) { @@ -65,7 +66,6 @@ namespace NBTUtil break; } }}, - { "delete", "Delete the NBT tag if found", v => Command = ConsoleCommand.DeleteValue }, }; } diff --git a/NBTUtil/ConsoleRunner.cs b/NBTUtil/ConsoleRunner.cs index df1a465..4a387ef 100644 --- a/NBTUtil/ConsoleRunner.cs +++ b/NBTUtil/ConsoleRunner.cs @@ -29,6 +29,8 @@ namespace NBTUtil public bool Run (string[] args) { + // Parse and validate command line arguments. + _options.Parse(args); if (_options.Command == ConsoleCommand.Help) @@ -48,6 +50,9 @@ namespace NBTUtil var nodesToProcess = new List(); + // 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)) { 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) { + // Since Process may render targetNode inoperable, save targetNode.Root beforehand. 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)"); failCount++; } - - root.Save(); - - Console.WriteLine(targetNode.NodePath + ": OK"); - successCount++; } Console.WriteLine("Operation complete. Nodes succeeded: {0} Nodes failed: {1}", successCount, failCount);