From 4d87a5cc049ed49fc5f454f0e02f309ee5956c77 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 3 Apr 2011 22:17:06 +0000 Subject: [PATCH] Updated dump utility --- NBToolkit/NBToolkit/Dump.cs | 129 +++++++++++++++++- NBToolkit/NBToolkit/Makefile | 6 +- NBToolkit/NBToolkit/Map/NBT/JSONSerializer.cs | 13 +- NBToolkit/NBToolkit/NBToolkit.csproj.user | 4 +- NBToolkit/NBToolkit/Program.cs | 11 +- 5 files changed, 146 insertions(+), 17 deletions(-) diff --git a/NBToolkit/NBToolkit/Dump.cs b/NBToolkit/NBToolkit/Dump.cs index 8ff9deb..4fa69e2 100644 --- a/NBToolkit/NBToolkit/Dump.cs +++ b/NBToolkit/NBToolkit/Dump.cs @@ -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(); } diff --git a/NBToolkit/NBToolkit/Makefile b/NBToolkit/NBToolkit/Makefile index b423472..ae914d4 100644 --- a/NBToolkit/NBToolkit/Makefile +++ b/NBToolkit/NBToolkit/Makefile @@ -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 \ No newline at end of file +SRCS=*.cs Map/*.cs Map/NBT/*.cs Map/Util/*.cs Zlib/*.cs NDesk/*.cs + +all: $(SRCS) + gmcs -out:nbtoolkit.exe $(SRCS) \ No newline at end of file diff --git a/NBToolkit/NBToolkit/Map/NBT/JSONSerializer.cs b/NBToolkit/NBToolkit/Map/NBT/JSONSerializer.cs index d757666..27ba5dc 100644 --- a/NBToolkit/NBToolkit/Map/NBT/JSONSerializer.cs +++ b/NBToolkit/NBToolkit/Map/NBT/JSONSerializer.cs @@ -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; } } diff --git a/NBToolkit/NBToolkit/NBToolkit.csproj.user b/NBToolkit/NBToolkit/NBToolkit.csproj.user index d1ffb1c..01b9174 100644 --- a/NBToolkit/NBToolkit/NBToolkit.csproj.user +++ b/NBToolkit/NBToolkit/NBToolkit.csproj.user @@ -1,9 +1,9 @@  - replace -w "F:\Minecraft\tps - Copy" -b 61 -a 18 -vv + dump -w "F:\Minecraft\tps - Copy" --out="json.txt" - replace -w "F:\Minecraft\tps - Copy" -b 61 -a 18 -vv + dump -w "F:\Minecraft\tps - Copy" --out="json.txt" publish\ diff --git a/NBToolkit/NBToolkit/Program.cs b/NBToolkit/NBToolkit/Program.cs index 7a94880..02d7948 100644 --- a/NBToolkit/NBToolkit/Program.cs +++ b/NBToolkit/NBToolkit/Program.cs @@ -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;