Add JSON exporter to NBTUtil

This commit is contained in:
Justin Aquadro 2015-06-16 01:16:31 -04:00
parent 2882fcde17
commit 9d640079b8
6 changed files with 67 additions and 2 deletions

View file

@ -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";

View file

@ -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;

View file

@ -46,6 +46,7 @@
<Compile Include="NDesk\Options.cs" />
<Compile Include="Ops\ConsoleOperation.cs" />
<Compile Include="Ops\EditOperation.cs" />
<Compile Include="Ops\JsonOperation.cs" />
<Compile Include="Ops\PrintOperation.cs" />
<Compile Include="Ops\PrintTreeOperation.cs" />
<Compile Include="Ops\SetListOperation.cs" />

View file

@ -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));
}
}
}

View file

@ -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")]

Binary file not shown.