Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NBTUtil/ConsoleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum ConsoleCommand
Print,
PrintTree,
SetValue,
DeleteValue,
SetList,
Json,
Help,
Expand Down Expand Up @@ -55,6 +56,7 @@ public ConsoleOptions ()
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) {
Expand Down
48 changes: 39 additions & 9 deletions NBTUtil/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ConsoleRunner
{
private static readonly Dictionary<ConsoleCommand, ConsoleOperation> _commandTable = new Dictionary<ConsoleCommand, ConsoleOperation>() {
{ ConsoleCommand.SetValue, new EditOperation() },
{ ConsoleCommand.DeleteValue, new DeleteOperation() },
{ ConsoleCommand.SetList, new SetListOperation() },
{ ConsoleCommand.Print, new PrintOperation() },
{ ConsoleCommand.PrintTree, new PrintTreeOperation() },
Expand All @@ -28,6 +29,8 @@ public ConsoleRunner ()

public bool Run (string[] args)
{
// Parse and validate command line arguments.

_options.Parse(args);

if (_options.Command == ConsoleCommand.Help)
Expand All @@ -45,20 +48,47 @@ public bool Run (string[] args)
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)");
failCount++;
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))
{
if (op.CanProcess(node))
{
nodesToProcess.Add(node);
}
if (!op.Process(targetNode, _options)) {
Console.WriteLine(targetNode.NodePath + ": ERROR (apply)");
else
{
Console.WriteLine(node.NodePath + ": ERROR (invalid command)");
failCount++;
}
}

targetNode.Root.Save();
// 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.

Console.WriteLine(targetNode.NodePath + ": OK");
successCount++;
foreach (var targetNode in nodesToProcess) {
// Since Process may render targetNode inoperable, save targetNode.Root beforehand.
var root = targetNode.Root;

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++;
}
}

Console.WriteLine("Operation complete. Nodes succeeded: {0} Nodes failed: {1}", successCount, failCount);
Expand Down
1 change: 1 addition & 0 deletions NBTUtil/NBTUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Compile Include="ConsoleRunner.cs" />
<Compile Include="NDesk\Options.cs" />
<Compile Include="Ops\ConsoleOperation.cs" />
<Compile Include="Ops\DeleteOperation.cs" />
<Compile Include="Ops\EditOperation.cs" />
<Compile Include="Ops\JsonOperation.cs" />
<Compile Include="Ops\PrintOperation.cs" />
Expand Down
25 changes: 25 additions & 0 deletions NBTUtil/Ops/DeleteOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using NBTExplorer.Model;

namespace NBTUtil.Ops
{
class DeleteOperation : ConsoleOperation
{
public override bool OptionsValid (ConsoleOptions options)
{
return true;
}

public override bool CanProcess (DataNode dataNode)
{
return (dataNode != null) && dataNode.CanDeleteNode && (dataNode.Root != dataNode);
}

public override bool Process (DataNode dataNode, ConsoleOptions options)
{
return dataNode.DeleteNode();
}
}
}