From f28f96c27f2d7091b1e498b6ebec6e14118a1ebb Mon Sep 17 00:00:00 2001 From: Jon Kunkee Date: Sat, 27 Jun 2020 16:52:19 -0700 Subject: [PATCH] Split find and act operations to allow deletion This change splits CanProcess into a separate loop from Process so that Process can perform actions like deletion that cause NbtPathEnumerator to throw an exception. It also caches targetNode.Root before Process because Process can cause targetNode to be invalid. --- NBTUtil/ConsoleRunner.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/NBTUtil/ConsoleRunner.cs b/NBTUtil/ConsoleRunner.cs index 0f94ba1..c0f07fc 100644 --- a/NBTUtil/ConsoleRunner.cs +++ b/NBTUtil/ConsoleRunner.cs @@ -45,17 +45,30 @@ namespace NBTUtil int successCount = 0; int failCount = 0; - foreach (var targetNode in new NbtPathEnumerator(_options.Path)) { - if (!op.CanProcess(targetNode)) { - Console.WriteLine(targetNode.NodePath + ": ERROR (invalid command)"); + var nodesToProcess = new List(); + + foreach (var node in new NbtPathEnumerator(_options.Path)) + { + if (op.CanProcess(node)) + { + nodesToProcess.Add(node); + } + else + { + Console.WriteLine(node.NodePath + ": ERROR (invalid command)"); failCount++; } + } + + foreach (var targetNode in nodesToProcess) { + var root = targetNode.Root; + if (!op.Process(targetNode, _options)) { Console.WriteLine(targetNode.NodePath + ": ERROR (apply)"); failCount++; } - targetNode.Root.Save(); + root.Save(); Console.WriteLine(targetNode.NodePath + ": OK"); successCount++;