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.
This commit is contained in:
Jon Kunkee 2020-06-27 16:52:19 -07:00
parent 15b1b2891f
commit f28f96c27f

View file

@ -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<DataNode>();
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++;