Updated dump utility

This commit is contained in:
Justin Aquadro 2011-04-03 22:17:06 +00:00
parent d89a892623
commit 4d87a5cc04
5 changed files with 146 additions and 17 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using NDesk.Options;
namespace NBToolkit
{
@ -8,11 +9,88 @@ namespace NBToolkit
using Map.NBT;
using System.IO;
public class DumpOptions : TKOptions, IChunkFilterable
{
private OptionSet _filterOpt = null;
private ChunkFilter _chunkFilter = null;
public string _outFile = "";
public bool _dumpBlocks = false;
public bool _dumpEntities = false;
public bool _dumpTileEntities = false;
public DumpOptions ()
: base()
{
_filterOpt = new OptionSet()
{
{ "out|OutFile=", "Path of file to write JSON data into",
var => _outFile = var },
{ "b|BlockData", "Dump block data (type, data, light arrays)",
var => _dumpBlocks = true },
{ "e|Entities", "Dump complete entity data",
var => _dumpEntities = true },
{ "t|TileEntities", "Dump complete tile entity data",
var => _dumpTileEntities = true },
};
_chunkFilter = new ChunkFilter();
}
public DumpOptions (string[] args)
: this()
{
Parse(args);
}
public override void Parse (string[] args)
{
base.Parse(args);
_filterOpt.Parse(args);
_chunkFilter.Parse(args);
}
public override void PrintUsage ()
{
Console.WriteLine("Usage: nbtoolkit dump [options]");
Console.WriteLine();
Console.WriteLine("Options for command 'dump':");
_filterOpt.WriteOptionDescriptions(Console.Out);
Console.WriteLine();
_chunkFilter.PrintUsage();
Console.WriteLine();
base.PrintUsage();
}
public override void SetDefaults ()
{
base.SetDefaults();
if (_outFile.Length == 0) {
Console.WriteLine("Error: You must specify an output file");
Console.WriteLine();
this.PrintUsage();
throw new TKOptionException();
}
}
public IChunkFilter GetChunkFilter ()
{
return _chunkFilter;
}
}
class Dump : TKFilter
{
private ReplaceOptions opt;
private DumpOptions opt;
public Dump (ReplaceOptions o)
public Dump (DumpOptions o)
{
opt = o;
}
@ -21,12 +99,49 @@ namespace NBToolkit
{
World world = new World(opt.OPT_WORLD);
StreamWriter fstr = new StreamWriter("json.txt", false);
foreach (ChunkRef chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
string s = JSONSerializer.Serialize(chunk.GetChunkRef().Tree.Root["Level"].ToNBTCompound()["TileEntities"]);
fstr.Write(s);
StreamWriter fstr;
try {
fstr = new StreamWriter(opt._outFile, false);
}
catch (IOException e) {
Console.WriteLine(e.Message);
return;
}
fstr.WriteLine("[");
bool first = true;
foreach (ChunkRef chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
if (!first) {
fstr.Write(",");
}
Chunk c = chunk.GetChunkRef();
if (!opt._dumpBlocks) {
c.Tree.Root["Level"].ToNBTCompound().Remove("Blocks");
c.Tree.Root["Level"].ToNBTCompound().Remove("Data");
c.Tree.Root["Level"].ToNBTCompound().Remove("BlockLight");
c.Tree.Root["Level"].ToNBTCompound().Remove("SkyLight");
c.Tree.Root["Level"].ToNBTCompound().Remove("HeightMap");
}
if (!opt._dumpEntities) {
c.Tree.Root["Level"].ToNBTCompound().Remove("Entities");
}
if (!opt._dumpTileEntities) {
c.Tree.Root["Level"].ToNBTCompound().Remove("TileEntities");
}
string s = JSONSerializer.Serialize(c.Tree.Root["Level"], 1);
fstr.Write(s);
first = false;
}
fstr.WriteLine();
fstr.WriteLine("]");
fstr.Close();
}

View file

@ -1,2 +1,4 @@
all:
gmcs -out:nbtoolkit.exe BlockInfo.cs BlockKey.cs BlockRef.cs BlockManager.cs Chunk.cs ChunkEnumerator.cs ChunkFilter.cs ChunkKey.cs ChunkManager.cs ChunkVerifier.cs FilteredChunkEnumerator.cs GenOres.cs MathHelper.cs NBT/NBT.cs NBT/NBTTag.cs NBT/NBTValues.cs NibbleArray.cs Oregen.cs Program.cs Purge.cs Region.cs RegionEnumerator.cs RegionFile.cs RegionKey.cs RegionManager.cs Replace.cs TKFilter.cs TKOptions.cs World.cs NDesk/Options.cs Zlib/Zlib.cs Zlib/ZlibStream.cs Zlib/ZlibConstants.cs Zlib/ZlibCodec.cs Zlib/ZlibBaseStream.cs Zlib/Tree.cs Zlib/InfTree.cs Zlib/Inflate.cs Zlib/GZipStream.cs Zlib/DeflateStream.cs Zlib/Deflate.cs Zlib/Crc32.cs
SRCS=*.cs Map/*.cs Map/NBT/*.cs Map/Util/*.cs Zlib/*.cs NDesk/*.cs
all: $(SRCS)
gmcs -out:nbtoolkit.exe $(SRCS)

View file

@ -7,21 +7,24 @@ namespace NBToolkit.Map.NBT
class JSONSerializer
{
public static string Serialize (NBT_Value tag)
{
return Serialize(tag, 0);
}
public static string Serialize (NBT_Value tag, int level)
{
StringBuilder str = new StringBuilder();
if (tag.GetNBTType() == NBT_Type.TAG_COMPOUND) {
SerializeCompound(tag as NBT_Compound, str, 0);
SerializeCompound(tag as NBT_Compound, str, level);
}
else if (tag.GetNBTType() == NBT_Type.TAG_LIST) {
SerializeList(tag as NBT_List, str, 0);
SerializeList(tag as NBT_List, str, level);
}
else {
SerializeScaler(tag, str);
}
str.AppendLine();
return str.ToString();
}
@ -137,7 +140,7 @@ namespace NBToolkit.Map.NBT
break;
case NBT_Type.TAG_BYTE_ARRAY:
str.Append("null");
str.Append(Convert.ToBase64String(tag.ToNBTByteArray().Data));
break;
}
}

View file

@ -1,9 +1,9 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<StartArguments>replace -w "F:\Minecraft\tps - Copy" -b 61 -a 18 -vv</StartArguments>
<StartArguments>dump -w "F:\Minecraft\tps - Copy" --out="json.txt"</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<StartArguments>replace -w "F:\Minecraft\tps - Copy" -b 61 -a 18 -vv</StartArguments>
<StartArguments>dump -w "F:\Minecraft\tps - Copy" --out="json.txt"</StartArguments>
</PropertyGroup>
<PropertyGroup>
<PublishUrlHistory>publish\</PublishUrlHistory>

View file

@ -37,7 +37,7 @@ namespace NBToolkit
filter.Run();
}
else if (args[0] == "dump") {
ReplaceOptions options = new ReplaceOptions(args);
DumpOptions options = new DumpOptions(args);
Dump filter = new Dump(options);
options.SetDefaults();
filter.Run();
@ -73,6 +73,13 @@ namespace NBToolkit
Console.WriteLine();
options.PrintUsage();
}
else if (args[1] == "dump") {
options = new DumpOptions(args);
WriteBlock("Dumps out chunk data in a readable JSON file. Block data, which are byte arrays, are printed as Bas64-encoded strings.");
Console.WriteLine();
options.PrintUsage();
}
else {
WriteBlock("Prints help and usage information for another command. Available commands are 'oregen' and 'replace'.");
Console.WriteLine();
@ -94,6 +101,8 @@ namespace NBToolkit
Console.WriteLine(" help Get help and usage info for another command");
Console.WriteLine(" oregen Generate structured deposits of a single block type");
Console.WriteLine(" replace Replace one block type with another");
Console.WriteLine(" purge Delete chunks");
Console.WriteLine(" dump Dump parsed chunk data to a readable JSON file");
Console.WriteLine();
options.PrintUsage();
return;