diff --git a/NBTUtil/ConsoleOptions.cs b/NBTUtil/ConsoleOptions.cs index 8efdb9d..1fdee5c 100644 --- a/NBTUtil/ConsoleOptions.cs +++ b/NBTUtil/ConsoleOptions.cs @@ -12,6 +12,7 @@ namespace NBTUtil PrintTree, SetValue, SetList, + Json, Help, } @@ -38,6 +39,10 @@ namespace NBTUtil { "print", "Print the value(s) of a tag", v => Command = ConsoleCommand.Print }, { "printtree", "Print the NBT tree rooted at a tag", v => Command = ConsoleCommand.PrintTree }, { "types", "Show data types when printing tags", v => ShowTypes = true }, + { "json=", "Export the NBT tree rooted at a tag as JSON", v => { + Command = ConsoleCommand.Json; + Values.Add(v); + }}, { "setvalue=", "Set a single tag value", v => { Command = ConsoleCommand.SetValue; _currentKey = "setvalue"; diff --git a/NBTUtil/ConsoleRunner.cs b/NBTUtil/ConsoleRunner.cs index eb89318..0f94ba1 100644 --- a/NBTUtil/ConsoleRunner.cs +++ b/NBTUtil/ConsoleRunner.cs @@ -16,6 +16,7 @@ namespace NBTUtil { ConsoleCommand.SetList, new SetListOperation() }, { ConsoleCommand.Print, new PrintOperation() }, { ConsoleCommand.PrintTree, new PrintTreeOperation() }, + { ConsoleCommand.Json, new JsonOperation() }, }; private ConsoleOptions _options; diff --git a/NBTUtil/NBTUtil.csproj b/NBTUtil/NBTUtil.csproj index 6c33f68..8571a2f 100644 --- a/NBTUtil/NBTUtil.csproj +++ b/NBTUtil/NBTUtil.csproj @@ -46,6 +46,7 @@ + diff --git a/NBTUtil/Ops/JsonOperation.cs b/NBTUtil/Ops/JsonOperation.cs new file mode 100644 index 0000000..2c9efe9 --- /dev/null +++ b/NBTUtil/Ops/JsonOperation.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using NBTExplorer.Model; +using Substrate.Nbt; + +namespace NBTUtil.Ops +{ + class JsonOperation : ConsoleOperation + { + public override bool CanProcess (DataNode dataNode) + { + return dataNode is NbtFileDataNode || dataNode is TagDataNode; + } + + public override bool Process (DataNode dataNode, ConsoleOptions options) + { + if (options.Values.Count == 0) + return false; + + string jsonPath = options.Values[0]; + using (FileStream stream = File.OpenWrite(jsonPath)) { + using (StreamWriter writer = new StreamWriter(stream)) { + if (dataNode is TagDataNode) { + TagDataNode tagNode = dataNode as TagDataNode; + WriteNbtTag(writer, tagNode.Tag); + } + else if (dataNode is NbtFileDataNode) { + dataNode.Expand(); + TagNodeCompound root = new TagNodeCompound(); + + foreach (DataNode child in dataNode.Nodes) { + TagDataNode childTagNode = child as TagDataNode; + if (childTagNode == null) + continue; + + if (childTagNode.Tag != null) + root.Add(childTagNode.NodeName, childTagNode.Tag); + } + + WriteNbtTag(writer, root); + } + } + } + + return true; + } + + private void WriteNbtTag (StreamWriter writer, TagNode tag) + { + if (tag == null) + return; + + writer.Write(JSONSerializer.Serialize(tag)); + } + } +} diff --git a/NBTUtil/Properties/AssemblyInfo.cs b/NBTUtil/Properties/AssemblyInfo.cs index 135fef8..8ced7bc 100644 --- a/NBTUtil/Properties/AssemblyInfo.cs +++ b/NBTUtil/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] +[assembly: AssemblyFileVersion("1.0.1.0")] diff --git a/References/Substrate.dll b/References/Substrate.dll index c079643..8094c73 100644 Binary files a/References/Substrate.dll and b/References/Substrate.dll differ