NBT cleanup / refactoring

This commit is contained in:
Justin Aquadro 2011-03-28 08:47:21 +00:00
parent fb25567713
commit e003835975
13 changed files with 960 additions and 820 deletions

View file

@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
using NBT;
namespace NBToolkit
{
using NBT;
public class Chunk
{
protected int _cx;
@ -118,7 +119,7 @@ namespace NBToolkit
public int GetBlockID (int x, int y, int z)
{
if (_blocks == null) {
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").value.toByteArray();
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").Value.ToNBTByteArray();
}
return _blocks.Data[x << 11 | z << 7 | y];
@ -127,7 +128,7 @@ namespace NBToolkit
public bool SetBlockID (int x, int y, int z, int id)
{
if (_blocks == null) {
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").value.toByteArray();
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").Value.ToNBTByteArray();
}
int index = x << 11 | z << 7 | y;
@ -144,7 +145,7 @@ namespace NBToolkit
public int CountBlockID (int id)
{
if (_blocks == null) {
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").value.toByteArray();
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").Value.ToNBTByteArray();
}
int c = 0;
@ -160,7 +161,7 @@ namespace NBToolkit
public int GetBlockData (int x, int y, int z)
{
if (_data == null) {
_data = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("Data").value.toByteArray().Data);
_data = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("Data").Value.ToNBTByteArray().Data);
}
return _data[x << 11 | z << 7 | y];
@ -169,7 +170,7 @@ namespace NBToolkit
public bool SetBlockData (int x, int y, int z, int data)
{
if (_data == null) {
_data = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("Data").value.toByteArray().Data);
_data = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("Data").Value.ToNBTByteArray().Data);
}
int index = x << 11 | z << 7 | y;
@ -186,7 +187,7 @@ namespace NBToolkit
public int GetBlockLight (int x, int y, int z)
{
if (_blockLight == null) {
_blockLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("BlockLight").value.toByteArray().Data);
_blockLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("BlockLight").Value.ToNBTByteArray().Data);
}
return _blockLight[x << 11 | z << 7 | y];
@ -195,7 +196,7 @@ namespace NBToolkit
public bool SetBlockLight (int x, int y, int z, int light)
{
if (_blockLight == null) {
_blockLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("BlockLight").value.toByteArray().Data);
_blockLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("BlockLight").Value.ToNBTByteArray().Data);
}
int index = x << 11 | z << 7 | y;
@ -212,7 +213,7 @@ namespace NBToolkit
public int GetSkyLight (int x, int y, int z)
{
if (_skyLight == null) {
_skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").value.toByteArray().Data);
_skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").Value.ToNBTByteArray().Data);
}
return _skyLight[x << 11 | z << 7 | y];
@ -221,7 +222,7 @@ namespace NBToolkit
public bool SetSkyLight (int x, int y, int z, int light)
{
if (_skyLight == null) {
_skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").value.toByteArray().Data);
_skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").Value.ToNBTByteArray().Data);
}
int index = x << 11 | z << 7 | y;
@ -237,7 +238,7 @@ namespace NBToolkit
public bool IsPopulated ()
{
return GetTree().Root.FindTagByName("Level").FindTagByName("TerrainPopulated").value.toByte().Data == 1;
return GetTree().Root.FindTagByName("Level").FindTagByName("TerrainPopulated").Value.ToNBTByte().Data == 1;
}
protected bool MarkDirty ()

View file

@ -1,2 +1,2 @@
all:
gmcs -out:nbtoolkit.exe BlockRef.cs BlockManager.cs Chunk.cs ChunkEnumerator.cs ChunkFilter.cs ChunkKey.cs ChunkManager.cs FilteredChunkEnumerator.cs GenOres.cs MathHelper.cs NBT.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
gmcs -out:nbtoolkit.exe BlockRef.cs BlockManager.cs Chunk.cs ChunkEnumerator.cs ChunkFilter.cs ChunkKey.cs ChunkManager.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

View file

@ -1,801 +0,0 @@
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace NBT
{
public enum NBT_Type
{
TAG_END = 0,
TAG_BYTE = 1, // 8 bits signed
TAG_SHORT = 2, // 16 bits signed
TAG_INT = 3, // 32 buts signed
TAG_LONG = 4, // 64 bits signed
TAG_FLOAT = 5,
TAG_DOUBLE = 6,
TAG_BYTE_ARRAY = 7,
TAG_STRING = 8,
TAG_LIST = 9,
TAG_COMPOUND = 10
}
public abstract class NBT_Value
{
virtual public NBT_Byte toByte () { throw new InvalidCastException(); }
virtual public NBT_Short toShort () { throw new InvalidCastException(); }
virtual public NBT_Int toInt () { throw new InvalidCastException(); }
virtual public NBT_Long toLong () { throw new InvalidCastException(); }
virtual public NBT_Float toFloat () { throw new InvalidCastException(); }
virtual public NBT_Double toDouble () { throw new InvalidCastException(); }
virtual public NBT_ByteArray toByteArray () { throw new InvalidCastException(); }
virtual public NBT_String toString () { throw new InvalidCastException(); }
virtual public NBT_List toList () { throw new InvalidCastException(); }
virtual public NBT_Compound toCompound () { throw new InvalidCastException(); }
virtual public NBT_Type getType () { return NBT_Type.TAG_END; }
}
public abstract class NBT_NumericValue : NBT_Value
{
}
public class NBT_Byte : NBT_Value
{
protected byte _data = 0;
override public NBT_Byte toByte () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_BYTE; }
public byte Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Byte () { }
public NBT_Byte (byte d)
{
_data = d;
}
}
public class NBT_Short : NBT_Value
{
protected short _data = 0;
override public NBT_Short toShort () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_SHORT; }
public short Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Short () { }
public NBT_Short (short d)
{
_data = d;
}
}
public class NBT_Int : NBT_Value
{
protected int _data = 0;
override public NBT_Int toInt () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_INT; }
public int Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Int () { }
public NBT_Int (int d)
{
_data = d;
}
}
public class NBT_Long : NBT_Value
{
protected long _data = 0;
override public NBT_Long toLong () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_LONG; }
public long Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Long () { }
public NBT_Long (long d)
{
_data = d;
}
}
public class NBT_Float : NBT_Value
{
protected float _data = 0;
override public NBT_Float toFloat () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_FLOAT; }
public float Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Float () { }
public NBT_Float (float d)
{
_data = d;
}
}
public class NBT_Double : NBT_Value
{
protected double _data = 0;
override public NBT_Double toDouble () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_DOUBLE; }
public double Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Double () { }
public NBT_Double (double d)
{
_data = d;
}
}
public class NBT_ByteArray : NBT_Value
{
protected byte[] _data = null;
override public NBT_ByteArray toByteArray () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_BYTE_ARRAY; }
public byte[] Data
{
get { return _data; }
set { _data = value; }
}
public int Length
{
get { return _data.Length; }
}
public NBT_ByteArray () { }
public NBT_ByteArray (byte[] d)
{
_data = d;
}
}
public class NBT_String : NBT_Value
{
protected string _data = null;
override public NBT_String toString () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_STRING; }
public string Data
{
get { return _data; }
set { _data = value; }
}
public int Length
{
get { return _data.Length; }
}
public NBT_String () { }
public NBT_String (string d)
{
_data = d;
}
}
public class NBT_List : NBT_Value
{
protected NBT_Type _type = NBT_Type.TAG_END;
protected List<NBT_Value> _items = null;
override public NBT_List toList () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_LIST; }
public int Count
{
get { return _items.Count; }
}
public List<NBT_Value> Items
{
get { return _items; }
}
public NBT_Type ValueType
{
get { return _type; }
}
public NBT_List (NBT_Type type) {
_type = type;
_items = new List<NBT_Value>();
}
public NBT_List (NBT_Type type, List<NBT_Value> items)
{
_type = type;
_items = items;
}
public void AddItem (NBT_Value val)
{
if (_type != val.getType()) {
throw new InvalidValueException();
}
_items.Add(val);
}
public bool RemoveItem (NBT_Value val)
{
return _items.Remove(val);
}
}
public class NBT_Compound : NBT_Value
{
protected List<NBT_Tag> _tags = null;
override public NBT_Compound toCompound () { return this; }
override public NBT_Type getType () { return NBT_Type.TAG_COMPOUND; }
public int Count
{
get { return _tags.Count; }
}
public List<NBT_Tag> Tags
{
get { return _tags; }
}
public NBT_Compound () {
_tags = new List<NBT_Tag>();
}
public NBT_Compound (List<NBT_Tag> tags)
{
_tags = tags;
}
public void AddTag (NBT_Tag sub)
{
_tags.Add(sub);
}
public bool RemoveTag (NBT_Tag sub)
{
return _tags.Remove(sub);
}
public NBT_Tag FindTagByName (string name)
{
foreach (NBT_Tag tag in _tags) {
if (tag.name.Data == name) {
return tag;
}
}
return null;
}
}
public class NBT_Tag
{
public NBT_Type type;
public NBT_String name;
public NBT_Value value;
public NBT_Tag FindTagByName (string name)
{
if (type != NBT_Type.TAG_COMPOUND) {
throw new InvalidTagException();
}
return value.toCompound().FindTagByName(name);
}
}
public class NBT_Tree
{
private Stream stream = null;
protected NBT_Tag _root = null;
public NBT_Tree (Stream s)
{
ReadFrom(s);
}
public void ReadFrom (Stream s)
{
if (s != null) {
stream = s;
_root = ReadTag();
stream = null;
}
}
public void WriteTo (Stream s)
{
if (s != null) {
stream = s;
if (_root != null) {
WriteTag(_root);
}
stream = null;
}
}
public NBT_Tag Root
{
get { return _root; }
}
private NBT_Value ReadValue (NBT_Type type)
{
switch (type) {
case NBT_Type.TAG_END:
return null;
case NBT_Type.TAG_BYTE:
return ReadByte();
case NBT_Type.TAG_SHORT:
return ReadShort();
case NBT_Type.TAG_INT:
return ReadInt();
case NBT_Type.TAG_LONG:
return ReadLong();
case NBT_Type.TAG_FLOAT:
return ReadFloat();
case NBT_Type.TAG_DOUBLE:
return ReadDouble();
case NBT_Type.TAG_BYTE_ARRAY:
return ReadByteArray();
case NBT_Type.TAG_STRING:
return ReadString();
case NBT_Type.TAG_LIST:
return ReadList();
case NBT_Type.TAG_COMPOUND:
return ReadCompound();
}
throw new Exception();
}
private NBT_Value ReadByte ()
{
int gzByte = stream.ReadByte();
if (gzByte == -1) {
throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
}
NBT_Byte val = new NBT_Byte((byte)gzByte);
return val;
}
private NBT_Value ReadShort ()
{
byte[] gzBytes = new byte[2];
stream.Read(gzBytes, 0, 2);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Short val = new NBT_Short(BitConverter.ToInt16(gzBytes, 0));
return val;
}
private NBT_Value ReadInt ()
{
byte[] gzBytes = new byte[4];
stream.Read(gzBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Int val = new NBT_Int(BitConverter.ToInt32(gzBytes, 0));
return val;
}
private NBT_Value ReadLong ()
{
byte[] gzBytes = new byte[8];
stream.Read(gzBytes, 0, 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Long val = new NBT_Long(BitConverter.ToInt64(gzBytes, 0));
return val;
}
private NBT_Value ReadFloat ()
{
byte[] gzBytes = new byte[4];
stream.Read(gzBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Float val = new NBT_Float(BitConverter.ToSingle(gzBytes, 0));
return val;
}
private NBT_Value ReadDouble ()
{
byte[] gzBytes = new byte[8];
stream.Read(gzBytes, 0, 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Double val = new NBT_Double(BitConverter.ToDouble(gzBytes, 0));
return val;
}
private NBT_Value ReadByteArray ()
{
byte[] lenBytes = new byte[4];
stream.Read(lenBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
byte[] data = new byte[length];
stream.Read(data, 0, length);
NBT_ByteArray val = new NBT_ByteArray(data);
return val;
}
private NBT_Value ReadString ()
{
byte[] lenBytes = new byte[2];
stream.Read(lenBytes, 0, 2);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
short len = BitConverter.ToInt16(lenBytes, 0);
if (len < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
byte[] strBytes = new byte[len];
stream.Read(strBytes, 0, len);
System.Text.Encoding str = Encoding.GetEncoding(28591);
NBT_String val = new NBT_String(str.GetString(strBytes));
return val;
}
private NBT_Value ReadList ()
{
int gzByte = stream.ReadByte();
if (gzByte == -1) {
throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
}
NBT_List val = new NBT_List((NBT_Type)gzByte);
if (val.ValueType > (NBT_Type)Enum.GetValues(typeof(NBT_Type)).GetUpperBound(0)) {
throw new NBTException(NBTException.MSG_READ_TYPE);
}
byte[] lenBytes = new byte[4];
stream.Read(lenBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
for (int i = 0; i < length; i++) {
val.AddItem(ReadValue(val.ValueType));
}
return val;
}
private NBT_Value ReadCompound ()
{
NBT_Compound val = new NBT_Compound();
while (true) {
NBT_Tag tag = ReadTag();
if (tag.type == NBT_Type.TAG_END) {
break;
}
val.AddTag(tag);
}
return val;
}
private NBT_Tag ReadTag ()
{
NBT_Tag tag = new NBT_Tag();
tag.type = (NBT_Type)stream.ReadByte();
tag.name = null;
tag.value = null;
if (tag.type != NBT_Type.TAG_END) {
tag.name = ReadString().toString();
}
tag.value = ReadValue(tag.type);
return tag;
}
private void WriteValue (NBT_Value val)
{
switch (val.getType()) {
case NBT_Type.TAG_END:
break;
case NBT_Type.TAG_BYTE:
WriteByte(val.toByte());
break;
case NBT_Type.TAG_SHORT:
WriteShort(val.toShort());
break;
case NBT_Type.TAG_INT:
WriteInt(val.toInt());
break;
case NBT_Type.TAG_LONG:
WriteLong(val.toLong());
break;
case NBT_Type.TAG_FLOAT:
WriteFloat(val.toFloat());
break;
case NBT_Type.TAG_DOUBLE:
WriteDouble(val.toDouble());
break;
case NBT_Type.TAG_BYTE_ARRAY:
WriteByteArray(val.toByteArray());
break;
case NBT_Type.TAG_STRING:
WriteString(val.toString());
break;
case NBT_Type.TAG_LIST:
WriteList(val.toList());
break;
case NBT_Type.TAG_COMPOUND:
WriteCompound(val.toCompound());
break;
}
}
private void WriteByte (NBT_Byte val)
{
stream.WriteByte(val.Data);
}
private void WriteShort (NBT_Short val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
stream.Write(gzBytes, 0, 2);
}
private void WriteInt (NBT_Int val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
stream.Write(gzBytes, 0, 4);
}
private void WriteLong (NBT_Long val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
stream.Write(gzBytes, 0, 8);
}
private void WriteFloat (NBT_Float val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
stream.Write(gzBytes, 0, 4);
}
private void WriteDouble (NBT_Double val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
stream.Write(gzBytes, 0, 8);
}
private void WriteByteArray (NBT_ByteArray val)
{
byte[] lenBytes = BitConverter.GetBytes(val.Length);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
stream.Write(lenBytes, 0, 4);
stream.Write(val.Data, 0, val.Length);
}
private void WriteString (NBT_String val)
{
byte[] lenBytes = BitConverter.GetBytes((short)val.Length);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
stream.Write(lenBytes, 0, 2);
System.Text.Encoding str = Encoding.GetEncoding(28591);
byte[] gzBytes = str.GetBytes(val.Data);
stream.Write(gzBytes, 0, gzBytes.Length);
}
private void WriteList (NBT_List val)
{
byte[] lenBytes = BitConverter.GetBytes(val.Count);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
stream.WriteByte((byte)val.ValueType);
stream.Write(lenBytes, 0, 4);
foreach (NBT_Value v in val.Items) {
WriteValue(v);
}
}
private void WriteCompound (NBT_Compound val)
{
foreach (NBT_Tag t in val.Tags) {
WriteTag(t);
}
NBT_Tag e = new NBT_Tag();
e.type = NBT_Type.TAG_END;
WriteTag(e);
}
private void WriteTag (NBT_Tag tag)
{
stream.WriteByte((byte)tag.type);
if (tag.type != NBT_Type.TAG_END) {
WriteString(tag.name);
WriteValue(tag.value);
}
}
}
public class NBTException : Exception
{
public const String MSG_GZIP_ENDOFSTREAM = "Gzip Error: Unexpected end of stream";
public const String MSG_READ_NEG = "Read Error: Negative length";
public const String MSG_READ_TYPE = "Read Error: Invalid value type";
public NBTException () { }
public NBTException (String msg) : base(msg) { }
public NBTException (String msg, Exception innerException) : base(msg, innerException) { }
}
public class InvalidTagException : Exception { }
public class InvalidValueException : Exception { }
}

View file

@ -0,0 +1,471 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace NBToolkit.NBT
{
public class NBT_Tree
{
private Stream _stream = null;
private NBT_Tag _root = null;
public NBT_Tag Root
{
get { return _root; }
}
public NBT_Tree ()
{
_root = new NBT_Tag("", new NBT_Compound());
}
public NBT_Tree (Stream s)
{
ReadFrom(s);
}
public void ReadFrom (Stream s)
{
if (s != null) {
_stream = s;
_root = ReadTag();
_stream = null;
}
}
public void WriteTo (Stream s)
{
if (s != null) {
_stream = s;
if (_root != null) {
WriteTag(_root);
}
_stream = null;
}
}
private NBT_Value ReadValue (NBT_Type type)
{
switch (type) {
case NBT_Type.TAG_END:
return null;
case NBT_Type.TAG_BYTE:
return ReadByte();
case NBT_Type.TAG_SHORT:
return ReadShort();
case NBT_Type.TAG_INT:
return ReadInt();
case NBT_Type.TAG_LONG:
return ReadLong();
case NBT_Type.TAG_FLOAT:
return ReadFloat();
case NBT_Type.TAG_DOUBLE:
return ReadDouble();
case NBT_Type.TAG_BYTE_ARRAY:
return ReadByteArray();
case NBT_Type.TAG_STRING:
return ReadString();
case NBT_Type.TAG_LIST:
return ReadList();
case NBT_Type.TAG_COMPOUND:
return ReadCompound();
}
throw new Exception();
}
private NBT_Value ReadByte ()
{
int gzByte = _stream.ReadByte();
if (gzByte == -1) {
throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
}
NBT_Byte val = new NBT_Byte((byte)gzByte);
return val;
}
private NBT_Value ReadShort ()
{
byte[] gzBytes = new byte[2];
_stream.Read(gzBytes, 0, 2);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Short val = new NBT_Short(BitConverter.ToInt16(gzBytes, 0));
return val;
}
private NBT_Value ReadInt ()
{
byte[] gzBytes = new byte[4];
_stream.Read(gzBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Int val = new NBT_Int(BitConverter.ToInt32(gzBytes, 0));
return val;
}
private NBT_Value ReadLong ()
{
byte[] gzBytes = new byte[8];
_stream.Read(gzBytes, 0, 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Long val = new NBT_Long(BitConverter.ToInt64(gzBytes, 0));
return val;
}
private NBT_Value ReadFloat ()
{
byte[] gzBytes = new byte[4];
_stream.Read(gzBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Float val = new NBT_Float(BitConverter.ToSingle(gzBytes, 0));
return val;
}
private NBT_Value ReadDouble ()
{
byte[] gzBytes = new byte[8];
_stream.Read(gzBytes, 0, 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
NBT_Double val = new NBT_Double(BitConverter.ToDouble(gzBytes, 0));
return val;
}
private NBT_Value ReadByteArray ()
{
byte[] lenBytes = new byte[4];
_stream.Read(lenBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
byte[] data = new byte[length];
_stream.Read(data, 0, length);
NBT_ByteArray val = new NBT_ByteArray(data);
return val;
}
private NBT_Value ReadString ()
{
byte[] lenBytes = new byte[2];
_stream.Read(lenBytes, 0, 2);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
short len = BitConverter.ToInt16(lenBytes, 0);
if (len < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
byte[] strBytes = new byte[len];
_stream.Read(strBytes, 0, len);
System.Text.Encoding str = Encoding.GetEncoding(28591);
NBT_String val = new NBT_String(str.GetString(strBytes));
return val;
}
private NBT_Value ReadList ()
{
int gzByte = _stream.ReadByte();
if (gzByte == -1) {
throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM);
}
NBT_List val = new NBT_List((NBT_Type)gzByte);
if (val.ValueType > (NBT_Type)Enum.GetValues(typeof(NBT_Type)).GetUpperBound(0)) {
throw new NBTException(NBTException.MSG_READ_TYPE);
}
byte[] lenBytes = new byte[4];
_stream.Read(lenBytes, 0, 4);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}
for (int i = 0; i < length; i++) {
val.AddItem(ReadValue(val.ValueType));
}
return val;
}
private NBT_Value ReadCompound ()
{
NBT_Compound val = new NBT_Compound();
while (true) {
NBT_Tag tag = ReadTag();
if (tag.Type == NBT_Type.TAG_END) {
break;
}
val.AddTag(tag);
}
return val;
}
private NBT_Tag ReadTag ()
{
NBT_Tag tag = new NBT_Tag();
NBT_Type type = (NBT_Type)_stream.ReadByte();
if (type != NBT_Type.TAG_END) {
tag.Name = ReadString().ToNBTString();
}
tag.Value = ReadValue(type);
return tag;
}
private void WriteValue (NBT_Value val)
{
switch (val.GetNBTType()) {
case NBT_Type.TAG_END:
break;
case NBT_Type.TAG_BYTE:
WriteByte(val.ToNBTByte());
break;
case NBT_Type.TAG_SHORT:
WriteShort(val.ToNBTShort());
break;
case NBT_Type.TAG_INT:
WriteInt(val.ToNBTInt());
break;
case NBT_Type.TAG_LONG:
WriteLong(val.ToNBTLong());
break;
case NBT_Type.TAG_FLOAT:
WriteFloat(val.ToNBTFloat());
break;
case NBT_Type.TAG_DOUBLE:
WriteDouble(val.ToNBTDouble());
break;
case NBT_Type.TAG_BYTE_ARRAY:
WriteByteArray(val.ToNBTByteArray());
break;
case NBT_Type.TAG_STRING:
WriteString(val.ToNBTString());
break;
case NBT_Type.TAG_LIST:
WriteList(val.ToNBTList());
break;
case NBT_Type.TAG_COMPOUND:
WriteCompound(val.ToNBTCompound());
break;
}
}
private void WriteByte (NBT_Byte val)
{
_stream.WriteByte(val.Data);
}
private void WriteShort (NBT_Short val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 2);
}
private void WriteInt (NBT_Int val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 4);
}
private void WriteLong (NBT_Long val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 8);
}
private void WriteFloat (NBT_Float val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 4);
}
private void WriteDouble (NBT_Double val)
{
byte[] gzBytes = BitConverter.GetBytes(val.Data);
if (BitConverter.IsLittleEndian) {
Array.Reverse(gzBytes);
}
_stream.Write(gzBytes, 0, 8);
}
private void WriteByteArray (NBT_ByteArray val)
{
byte[] lenBytes = BitConverter.GetBytes(val.Length);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
_stream.Write(lenBytes, 0, 4);
_stream.Write(val.Data, 0, val.Length);
}
private void WriteString (NBT_String val)
{
byte[] lenBytes = BitConverter.GetBytes((short)val.Length);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
_stream.Write(lenBytes, 0, 2);
System.Text.Encoding str = Encoding.GetEncoding(28591);
byte[] gzBytes = str.GetBytes(val.Data);
_stream.Write(gzBytes, 0, gzBytes.Length);
}
private void WriteList (NBT_List val)
{
byte[] lenBytes = BitConverter.GetBytes(val.Count);
if (BitConverter.IsLittleEndian) {
Array.Reverse(lenBytes);
}
_stream.WriteByte((byte)val.ValueType);
_stream.Write(lenBytes, 0, 4);
foreach (NBT_Value v in val.Items) {
WriteValue(v);
}
}
private void WriteCompound (NBT_Compound val)
{
foreach (NBT_Tag t in val.Tags) {
WriteTag(t);
}
NBT_Tag e = new NBT_Tag();
WriteTag(e);
}
private void WriteTag (NBT_Tag tag)
{
_stream.WriteByte((byte)tag.Type);
if (tag.Type != NBT_Type.TAG_END) {
WriteString(tag.Name);
WriteValue(tag.Value);
}
}
}
public class NBTException : Exception
{
public const String MSG_GZIP_ENDOFSTREAM = "Gzip Error: Unexpected end of stream";
public const String MSG_READ_NEG = "Read Error: Negative length";
public const String MSG_READ_TYPE = "Read Error: Invalid value type";
public NBTException () { }
public NBTException (String msg) : base(msg) { }
public NBTException (String msg, Exception innerException) : base(msg, innerException) { }
}
public class InvalidTagException : Exception { }
public class InvalidValueException : Exception { }
}

View file

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit.NBT
{
public class NBT_Tag
{
private NBT_String _name;
private NBT_Value _value;
public NBT_String Name
{
get { return _name; }
set { _name = value; }
}
public NBT_Type Type
{
get { return (_value is NBT_Value) ? _value.GetNBTType() : NBT_Type.TAG_END; }
}
public NBT_Value Value
{
get { return _value; }
set { _value = value; }
}
public NBT_Tag () { }
public NBT_Tag (NBT_String name)
{
_name = name;
}
public NBT_Tag (NBT_String name, NBT_Value value)
{
_name = name;
_value = value;
}
public NBT_Tag FindTagByName (string name)
{
NBT_Compound cval = _value as NBT_Compound;
if (cval == null) {
throw new InvalidTagException();
}
return cval.FindTagByName(name);
}
public NBT_Tag AddTag (string name, NBT_Value value)
{
NBT_Compound cval = _value as NBT_Compound;
if (cval == null) {
throw new InvalidTagException();
}
NBT_Tag tag = new NBT_Tag(name, value);
cval.AddTag(tag);
return tag;
}
public NBT_Tag AddTag (NBT_Tag tag)
{
NBT_Compound cval = _value as NBT_Compound;
if (cval == null) {
throw new InvalidTagException();
}
tag.AddTag(tag);
return tag;
}
public bool RemoveTag (string name)
{
NBT_Compound cval = _value as NBT_Compound;
if (cval == null) {
throw new InvalidTagException();
}
return cval.RemoveTag(name);
}
}
}

View file

@ -0,0 +1,373 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit.NBT {
/// <summary>
/// Describes the type of value held by an NBT_Tag
/// </summary>
public enum NBT_Type
{
TAG_END = 0,
TAG_BYTE = 1, // 8 bits signed
TAG_SHORT = 2, // 16 bits signed
TAG_INT = 3, // 32 bits signed
TAG_LONG = 4, // 64 bits signed
TAG_FLOAT = 5,
TAG_DOUBLE = 6,
TAG_BYTE_ARRAY = 7,
TAG_STRING = 8,
TAG_LIST = 9,
TAG_COMPOUND = 10
}
public abstract class NBT_Value
{
virtual public NBT_Byte ToNBTByte () { throw new InvalidCastException(); }
virtual public NBT_Short ToNBTShort () { throw new InvalidCastException(); }
virtual public NBT_Int ToNBTInt () { throw new InvalidCastException(); }
virtual public NBT_Long ToNBTLong () { throw new InvalidCastException(); }
virtual public NBT_Float ToNBTFloat () { throw new InvalidCastException(); }
virtual public NBT_Double ToNBTDouble () { throw new InvalidCastException(); }
virtual public NBT_ByteArray ToNBTByteArray () { throw new InvalidCastException(); }
virtual public NBT_String ToNBTString () { throw new InvalidCastException(); }
virtual public NBT_List ToNBTList () { throw new InvalidCastException(); }
virtual public NBT_Compound ToNBTCompound () { throw new InvalidCastException(); }
virtual public NBT_Type GetNBTType () { return NBT_Type.TAG_END; }
}
public class NBT_Byte : NBT_Value
{
private byte _data = 0;
override public NBT_Byte ToNBTByte () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_BYTE; }
public byte Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Byte () { }
public NBT_Byte (byte d)
{
_data = d;
}
public static implicit operator NBT_Byte (byte b)
{
return new NBT_Byte(b);
}
}
public class NBT_Short : NBT_Value
{
private short _data = 0;
override public NBT_Short ToNBTShort () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_SHORT; }
public short Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Short () { }
public NBT_Short (short d)
{
_data = d;
}
public static implicit operator NBT_Short (short s)
{
return new NBT_Short(s);
}
}
public class NBT_Int : NBT_Value
{
private int _data = 0;
override public NBT_Int ToNBTInt () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_INT; }
public int Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Int () { }
public NBT_Int (int d)
{
_data = d;
}
public static implicit operator NBT_Int (int i)
{
return new NBT_Int(i);
}
}
public class NBT_Long : NBT_Value
{
private long _data = 0;
override public NBT_Long ToNBTLong () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_LONG; }
public long Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Long () { }
public NBT_Long (long d)
{
_data = d;
}
public static implicit operator NBT_Long (long l)
{
return new NBT_Long(l);
}
}
public class NBT_Float : NBT_Value
{
private float _data = 0;
override public NBT_Float ToNBTFloat () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_FLOAT; }
public float Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Float () { }
public NBT_Float (float d)
{
_data = d;
}
public static implicit operator NBT_Float (float f)
{
return new NBT_Float(f);
}
}
public class NBT_Double : NBT_Value
{
private double _data = 0;
override public NBT_Double ToNBTDouble () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_DOUBLE; }
public double Data
{
get { return _data; }
set { _data = value; }
}
public NBT_Double () { }
public NBT_Double (double d)
{
_data = d;
}
public static implicit operator NBT_Double (double d)
{
return new NBT_Double(d);
}
}
public class NBT_ByteArray : NBT_Value
{
private byte[] _data = null;
override public NBT_ByteArray ToNBTByteArray () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_BYTE_ARRAY; }
public byte[] Data
{
get { return _data; }
set { _data = value; }
}
public int Length
{
get { return _data.Length; }
}
public NBT_ByteArray () { }
public NBT_ByteArray (byte[] d)
{
_data = d;
}
public static implicit operator NBT_ByteArray (byte[] b)
{
return new NBT_ByteArray(b);
}
}
public class NBT_String : NBT_Value
{
private string _data = null;
override public NBT_String ToNBTString () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_STRING; }
public string Data
{
get { return _data; }
set { _data = value; }
}
public int Length
{
get { return _data.Length; }
}
public NBT_String () { }
public NBT_String (string d)
{
_data = d;
}
public static implicit operator NBT_String (string s)
{
return new NBT_String(s);
}
}
public class NBT_List : NBT_Value
{
private NBT_Type _type = NBT_Type.TAG_END;
private List<NBT_Value> _items = null;
override public NBT_List ToNBTList () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_LIST; }
public int Count
{
get { return _items.Count; }
}
public List<NBT_Value> Items
{
get { return _items; }
}
public NBT_Type ValueType
{
get { return _type; }
}
public NBT_List (NBT_Type type)
{
_type = type;
_items = new List<NBT_Value>();
}
public NBT_List (NBT_Type type, List<NBT_Value> items)
{
_type = type;
_items = items;
}
public void AddItem (NBT_Value val)
{
if (_type != val.GetNBTType()) {
throw new InvalidValueException();
}
_items.Add(val);
}
public void RemoveItem (int index)
{
_items.RemoveAt(index);
}
public bool RemoveItem (NBT_Value val)
{
return _items.Remove(val);
}
}
public class NBT_Compound : NBT_Value
{
private List<NBT_Tag> _tags = null;
override public NBT_Compound ToNBTCompound () { return this; }
override public NBT_Type GetNBTType () { return NBT_Type.TAG_COMPOUND; }
public int Count
{
get { return _tags.Count; }
}
public List<NBT_Tag> Tags
{
get { return _tags; }
}
public NBT_Compound ()
{
_tags = new List<NBT_Tag>();
}
public NBT_Compound (List<NBT_Tag> tags)
{
_tags = tags;
}
public void AddTag (NBT_Tag sub)
{
_tags.Add(sub);
}
public void RemoveTag (int index)
{
_tags.RemoveAt(index);
}
public bool RemoveTag (NBT_Tag sub)
{
return _tags.Remove(sub);
}
public bool RemoveTag (string name)
{
return _tags.Remove(_tags.Find(v => v.Name.Data == name));
}
public NBT_Tag FindTagByName (string name)
{
foreach (NBT_Tag tag in _tags) {
if (tag.Name.Data == name) {
return tag;
}
}
return null;
}
}
}

View file

@ -38,6 +38,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -46,6 +48,8 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -63,7 +67,9 @@
<Compile Include="FilteredChunkEnumerator.cs" />
<Compile Include="GenOres.cs" />
<Compile Include="MathHelper.cs" />
<Compile Include="NBT.cs" />
<Compile Include="NBT\NBT.cs" />
<Compile Include="NBT\NBTTag.cs" />
<Compile Include="NBT\NBTValues.cs" />
<Compile Include="NDesk\Options.cs" />
<Compile Include="NibbleArray.cs" />
<Compile Include="Oregen.cs" />

View file

@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Text;
using NDesk.Options;
using NBT;
namespace NBToolkit
{
using NBT;
public class OregenOptions : TKOptions, IChunkFilterable
{
private OptionSet _filterOpt = null;

View file

@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
using NBT;
namespace NBToolkit
{
using NBT;
class Program
{
static void Main (string[] args)

View file

@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Text;
using NDesk.Options;
using NBT;
namespace NBToolkit
{
using NBT;
public class PurgeOptions : TKOptions, IChunkFilterable
{
private OptionSet _filterOpt = null;

View file

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using NBT;
namespace NBToolkit
{
using NBT;
public class Region : IDisposable
{
protected int _rx;

View file

@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Text;
using NDesk.Options;
using NBT;
namespace NBToolkit
{
using NBT;
public class ReplaceOptions : TKOptions, IChunkFilterable
{
private OptionSet _filterOpt = null;

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using NBT;
namespace NBToolkit
{