diff --git a/NBToolkit/NBToolkit/BlockManager.cs b/NBToolkit/NBToolkit/BlockManager.cs index ba81791..5ffea76 100644 --- a/NBToolkit/NBToolkit/BlockManager.cs +++ b/NBToolkit/NBToolkit/BlockManager.cs @@ -36,6 +36,18 @@ namespace NBToolkit { _chunkMan = bm._chunkMan; } + + public virtual BlockRef GetBlockRef (int x, int y, int z) + { + if (x < MIN_X || x >= MAX_X) + return null; + if (y < MIN_Y || y >= MAX_Y) + return null; + if (z < MIN_Z || z >= MAX_Z) + return null; + + return new BlockRef(this, x, y, z); + } public virtual int GetBlockID (int x, int y, int z) { diff --git a/NBToolkit/NBToolkit/BlockRef.cs b/NBToolkit/NBToolkit/BlockRef.cs new file mode 100644 index 0000000..2f3996f --- /dev/null +++ b/NBToolkit/NBToolkit/BlockRef.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NBToolkit +{ + public class BlockRef + { + protected Chunk _chunk; + + protected int _lx; + protected int _ly; + protected int _lz; + + public int X + { + get + { + return _lx + (_chunk.X * BlockManager.CHUNK_XLEN); + } + } + + public int Y + { + get + { + return _ly; + } + } + + public int Z + { + get + { + return _lz + (_chunk.Z * BlockManager.CHUNK_ZLEN); + } + } + + public int LocalX + { + get + { + return _lx; + } + } + + public int LocalY + { + get + { + return _ly; + } + } + + public int LocalZ + { + get + { + return _lz; + } + } + + public int ID + { + get + { + return _chunk.GetBlockID(_lx, _ly, _lz); + } + + set + { + _chunk.SetBlockID(_lx, _ly, _lz, value); + } + } + + public int Data + { + get + { + return _chunk.GetBlockData(_lx, _ly, _lz); + } + + set + { + _chunk.SetBlockData(_lx, _ly, _lz, value); + } + } + + public int BlockLight + { + get + { + return _chunk.GetBlockLight(_lx, _ly, _lz); + } + + set + { + _chunk.SetBlockLight(_lx, _ly, _lz, value); + } + } + + public int SkyLight + { + get + { + return _chunk.GetSkyLight(_lx, _ly, _lz); + } + + set + { + _chunk.SetSkyLight(_lx, _ly, _lz, value); + } + } + + public BlockRef (BlockManager bm, int x, int y, int z) + { + _chunk = bm.GetChunk(x, y, z); + _lx = x - _chunk.X * BlockManager.CHUNK_XLEN; + _ly = y; + _lz = z - _chunk.Z * BlockManager.CHUNK_ZLEN; + } + + public BlockRef (Chunk c, int lx, int ly, int lz) + { + _chunk = c; + _lx = lx; + _ly = ly; + _lz = lz; + } + } +} diff --git a/NBToolkit/NBToolkit/Chunk.cs b/NBToolkit/NBToolkit/Chunk.cs index 84fed61..3ad2736 100644 --- a/NBToolkit/NBToolkit/Chunk.cs +++ b/NBToolkit/NBToolkit/Chunk.cs @@ -13,23 +13,13 @@ namespace NBToolkit protected NBT_Tree _nbt = null; protected NBT_ByteArray _blocks = null; protected NibbleArray _data = null; + protected NibbleArray _blockLight = null; + protected NibbleArray _skyLight = null; protected bool _dirty = false; protected ChunkManager _chunkMan; - public Chunk (ChunkManager cm, int cx, int cz) - { - _chunkMan = cm; - _cx = cx; - _cz = cz; - - Region r = cm.GetRegion(cx, cz); - if (r == null || !r.ChunkExists(LocalX, LocalZ)) { - throw new MissingChunkException(); - } - } - public int X { get @@ -62,6 +52,18 @@ namespace NBToolkit } } + public Chunk (ChunkManager cm, int cx, int cz) + { + _chunkMan = cm; + _cx = cx; + _cz = cz; + + Region r = cm.GetRegion(cx, cz); + if (r == null || !r.ChunkExists(LocalX, LocalZ)) { + throw new MissingChunkException(); + } + } + public bool Save () { if (_dirty) { @@ -108,27 +110,32 @@ namespace NBToolkit return false; } + public BlockRef GetBlockRef (int lx, int ly, int lz) + { + return new BlockRef(this, lx, ly, lz); + } + public int GetBlockID (int x, int y, int z) { if (_blocks == null) { - _blocks = GetTree().getRoot().findTagByName("Level").findTagByName("Blocks").value.toByteArray(); + _blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").value.toByteArray(); } - return _blocks.data[x << 11 | z << 7 | y]; + return _blocks.Data[x << 11 | z << 7 | y]; } public bool SetBlockID (int x, int y, int z, int id) { if (_blocks == null) { - _blocks = GetTree().getRoot().findTagByName("Level").findTagByName("Blocks").value.toByteArray(); + _blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").value.toByteArray(); } int index = x << 11 | z << 7 | y; - if (_blocks.data[index] == id) { + if (_blocks.Data[index] == id) { return false; } - _blocks.data[index] = (byte)id; + _blocks.Data[index] = (byte)id; MarkDirty(); return true; @@ -137,12 +144,12 @@ namespace NBToolkit public int CountBlockID (int id) { if (_blocks == null) { - _blocks = GetTree().getRoot().findTagByName("Level").findTagByName("Blocks").value.toByteArray(); + _blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").value.toByteArray(); } int c = 0; - for (int i = 0; i < _blocks.length; i++) { - if (_blocks.data[i] == id) { + for (int i = 0; i < _blocks.Length; i++) { + if (_blocks.Data[i] == id) { c++; } } @@ -153,7 +160,7 @@ namespace NBToolkit public int GetBlockData (int x, int y, int z) { if (_data == null) { - _data = new NibbleArray(GetTree().getRoot().findTagByName("Level").findTagByName("Data").value.toByteArray().data); + _data = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("Data").value.toByteArray().Data); } return _data[x << 11 | z << 7 | y]; @@ -162,7 +169,7 @@ namespace NBToolkit public bool SetBlockData (int x, int y, int z, int data) { if (_data == null) { - _data = new NibbleArray(GetTree().getRoot().findTagByName("Level").findTagByName("Data").value.toByteArray().data); + _data = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("Data").value.toByteArray().Data); } int index = x << 11 | z << 7 | y; @@ -176,9 +183,61 @@ namespace NBToolkit return true; } + public int GetBlockLight (int x, int y, int z) + { + if (_blockLight == null) { + _blockLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("BlockLight").value.toByteArray().Data); + } + + return _blockLight[x << 11 | z << 7 | y]; + } + + 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); + } + + int index = x << 11 | z << 7 | y; + if (_blockLight[index] == light) { + return false; + } + + _blockLight[index] = light; + MarkDirty(); + + return true; + } + + public int GetSkyLight (int x, int y, int z) + { + if (_skyLight == null) { + _skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").value.toByteArray().Data); + } + + return _skyLight[x << 11 | z << 7 | y]; + } + + 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); + } + + int index = x << 11 | z << 7 | y; + if (_skyLight[index] == light) { + return false; + } + + _skyLight[index] = light; + MarkDirty(); + + return true; + } + public bool IsPopulated () { - return GetTree().getRoot().findTagByName("Level").findTagByName("TerrainPopulated").value.toByte().data == 1; + return GetTree().Root.FindTagByName("Level").FindTagByName("TerrainPopulated").value.toByte().Data == 1; } protected bool MarkDirty () @@ -191,5 +250,25 @@ namespace NBToolkit _chunkMan.MarkChunkDirty(this); return true; } + + public Chunk GetNorthNeighbor () + { + return _chunkMan.GetChunk(_cx - 1, _cz); + } + + public Chunk GetSouthNeighbor () + { + return _chunkMan.GetChunk(_cx + 1, _cz); + } + + public Chunk GetEastNeighbor () + { + return _chunkMan.GetChunk(_cx, _cz - 1); + } + + public Chunk GetWestNeighbor () + { + return _chunkMan.GetChunk(_cx, _cz + 1); + } } } diff --git a/NBToolkit/NBToolkit/ChunkEnumerator.cs b/NBToolkit/NBToolkit/ChunkEnumerator.cs index 51966eb..5451e04 100644 --- a/NBToolkit/NBToolkit/ChunkEnumerator.cs +++ b/NBToolkit/NBToolkit/ChunkEnumerator.cs @@ -48,6 +48,8 @@ namespace NBToolkit protected Region _region; protected ChunkManager _cm; + protected Chunk _chunk; + protected RegionEnumerator _enum = null; protected int _x = 0; protected int _z = -1; @@ -88,6 +90,7 @@ namespace NBToolkit _region = _enum.Current; } if (MoveNextInRegion()) { + _chunk = _cm.GetChunkInRegion(_region, _x, _z); return true; } } @@ -146,8 +149,7 @@ namespace NBToolkit if (_x >= ChunkManager.REGION_XLEN) { throw new InvalidOperationException(); } - - return _cm.GetChunkInRegion(_region, _x, _z); + return _chunk; } } } diff --git a/NBToolkit/NBToolkit/ChunkManager.cs b/NBToolkit/NBToolkit/ChunkManager.cs index cf59256..7c6610f 100644 --- a/NBToolkit/NBToolkit/ChunkManager.cs +++ b/NBToolkit/NBToolkit/ChunkManager.cs @@ -31,15 +31,22 @@ namespace NBToolkit { ChunkKey k = new ChunkKey(cx, cz); + Chunk c = null; + WeakReference chunkref = null; if (_cache.TryGetValue(k, out chunkref)) { - return chunkref.Target as Chunk; + c = chunkref.Target as Chunk; + } + else { + _cache.Add(k, new WeakReference(null)); } - _cache.Add(k, new WeakReference(null)); + if (c != null) { + return c; + } try { - Chunk c = new Chunk(this, cx, cz); + c = new Chunk(this, cx, cz); _cache[k].Target = c; return c; } diff --git a/NBToolkit/NBToolkit/GenOres.cs b/NBToolkit/NBToolkit/GenOres.cs index ce8dcf4..31ee4b7 100644 --- a/NBToolkit/NBToolkit/GenOres.cs +++ b/NBToolkit/NBToolkit/GenOres.cs @@ -99,8 +99,11 @@ namespace NBToolkit for (int iz = zStart; iz <= zEnd; iz++) { double zThresh = (iz + 0.5D - zPos) / (fuzzXZ / 2.0D); if (xThresh * xThresh + yThresh * yThresh + zThresh * zThresh < 1.0D) { - blockMan.SetBlockID(ix, iy, iz, _blockId); - blockMan.SetBlockData(ix, iy, iz, _blockData); + BlockRef block = blockMan.GetBlockRef(ix, iy, iz); + if (block != null) { + block.ID = _blockId; + block.Data = _blockData; + } } } } diff --git a/NBToolkit/NBToolkit/Makefile b/NBToolkit/NBToolkit/Makefile index 27ac2a4..064454e 100644 --- a/NBToolkit/NBToolkit/Makefile +++ b/NBToolkit/NBToolkit/Makefile @@ -1,2 +1,2 @@ all: - gmcs -out:nbtoolkit.exe Program.cs NBT.cs Harness.cs TKOptions.cs NDesk/Options.cs TKFilter.cs Oregen.cs Replace.cs RegionFile.cs RegionFileCache.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 + 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 \ No newline at end of file diff --git a/NBToolkit/NBToolkit/NBT.cs b/NBToolkit/NBToolkit/NBT.cs index 7a20093..7bc619c 100644 --- a/NBToolkit/NBToolkit/NBT.cs +++ b/NBToolkit/NBToolkit/NBT.cs @@ -22,99 +22,301 @@ namespace NBT TAG_COMPOUND = 10 } - public class NBT_Value + public abstract class NBT_Value { - virtual public NBT_Byte toByte () { return null; } - virtual public NBT_Short toShort () { return null; } - virtual public NBT_Int toInt () { return null; } - virtual public NBT_Long toLong () { return null; } - virtual public NBT_Float toFloat () { return null; } - virtual public NBT_Double toDouble () { return null; } - virtual public NBT_ByteArray toByteArray () { return null; } - virtual public NBT_String toString () { return null; } - virtual public NBT_List toList () { return null; } - virtual public NBT_Compound toCompound () { return null; } + 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 { - public byte data = 0; + 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 { - public short data = 0; + 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 { - public int data = 0; + 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 { - public long data = 0; + 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 { - public float data = 0; + 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 { - public double data = 0; + 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 { - public int length = 0; - public byte[] data = null; + 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 { - public String data = null; + 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 { - public int length = 0; - public NBT_Type type = NBT_Type.TAG_END; + protected NBT_Type _type = NBT_Type.TAG_END; - public List items = null; + protected List _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 Items + { + get { return _items; } + } + + public NBT_Type ValueType + { + get { return _type; } + } + + public NBT_List (NBT_Type type) { + _type = type; + _items = new List(); + } + + public NBT_List (NBT_Type type, List 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 { - public int length = 0; - - public List tags = null; + protected List _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 Tags + { + get { return _tags; } + } + + public NBT_Compound () { + _tags = new List(); + } + + public NBT_Compound (List 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 @@ -124,19 +326,13 @@ namespace NBT public NBT_Value value; - public NBT_Tag findTagByName (String name) + public NBT_Tag FindTagByName (string name) { if (type != NBT_Type.TAG_COMPOUND) { - return null; + throw new InvalidTagException(); } - foreach (NBT_Tag tag in value.toCompound().tags) { - if (tag.name.data.Equals(name)) { - return tag; - } - } - - return null; + return value.toCompound().FindTagByName(name); } } @@ -144,9 +340,7 @@ namespace NBT { private Stream stream = null; - //String path = null; - - NBT_Tag root = null; + protected NBT_Tag _root = null; public NBT_Tree (Stream s) { @@ -157,7 +351,7 @@ namespace NBT { if (s != null) { stream = s; - root = ReadTag(); + _root = ReadTag(); stream = null; } } @@ -167,76 +361,19 @@ namespace NBT if (s != null) { stream = s; - if (root != null) { - WriteTag(root); + if (_root != null) { + WriteTag(_root); } stream = null; } } - /*public void activate () + public NBT_Tag Root { - if (root == null) { - read(); - } + get { return _root; } } - public void deactivate () - { - if (root != null) { - root = null; - } - } - - public void save () - { - if (root != null) { - write(); - } - }*/ - - public NBT_Tag getRoot () - { - return root; - } - - /*public void addListItem (NBT_Tag tag, NBT_Value val) - { - if (tag.type != NBT_Type.TAG_LIST) { - throw new Exception(); - } - - if (tag.value.toList().type != val.getType()) { - throw new Exception(); - } - - val.toList().length++; - val.toList().items.Add(val); - } - - public void addTag (NBT_Tag tag, NBT_Tag sub) - { - if (tag.type != NBT_Type.TAG_COMPOUND) { - throw new Exception(); - } - - tag.value.toCompound().length++; - tag.value.toCompound().tags.Add(sub); - }*/ - - /*private void read () - { - FileStream fStream = new FileStream(path, System.IO.FileMode.Open); - gzStream = new GZipStream(fStream, CompressionMode.Decompress); - - root = readTag(); - - gzStream.Close(); - - gzStream = null; - }*/ - private NBT_Value ReadValue (NBT_Type type) { switch (type) { @@ -279,25 +416,18 @@ namespace NBT private NBT_Value ReadByte () { - //Console.Write("NBT_File.readByte()"); - int gzByte = stream.ReadByte(); if (gzByte == -1) { throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM); } - NBT_Byte val = new NBT_Byte(); - val.data = (byte)gzByte; - - //Console.WriteLine(" [" + val.data + "]"); + NBT_Byte val = new NBT_Byte((byte)gzByte); return val; } private NBT_Value ReadShort () { - //Console.Write("NBT_File.readShort"); - byte[] gzBytes = new byte[2]; stream.Read(gzBytes, 0, 2); @@ -305,18 +435,13 @@ namespace NBT Array.Reverse(gzBytes); } - NBT_Short val = new NBT_Short(); - val.data = BitConverter.ToInt16(gzBytes, 0); - - //Console.WriteLine(" [" + val.data + "]"); + NBT_Short val = new NBT_Short(BitConverter.ToInt16(gzBytes, 0)); return val; } private NBT_Value ReadInt () { - //Console.Write("NBT_File.readInt"); - byte[] gzBytes = new byte[4]; stream.Read(gzBytes, 0, 4); @@ -324,18 +449,13 @@ namespace NBT Array.Reverse(gzBytes); } - NBT_Int val = new NBT_Int(); - val.data = BitConverter.ToInt32(gzBytes, 0); - - //Console.WriteLine(" [" + val.data + "]"); + NBT_Int val = new NBT_Int(BitConverter.ToInt32(gzBytes, 0)); return val; } private NBT_Value ReadLong () { - //Console.Write("NBT_File.readLong"); - byte[] gzBytes = new byte[8]; stream.Read(gzBytes, 0, 8); @@ -343,18 +463,13 @@ namespace NBT Array.Reverse(gzBytes); } - NBT_Long val = new NBT_Long(); - val.data = BitConverter.ToInt64(gzBytes, 0); - - //Console.WriteLine(" [" + val.data + "]"); + NBT_Long val = new NBT_Long(BitConverter.ToInt64(gzBytes, 0)); return val; } private NBT_Value ReadFloat () { - //Console.Write("NBT_File.readFloat()"); - byte[] gzBytes = new byte[4]; stream.Read(gzBytes, 0, 4); @@ -362,18 +477,13 @@ namespace NBT Array.Reverse(gzBytes); } - NBT_Float val = new NBT_Float(); - val.data = BitConverter.ToSingle(gzBytes, 0); - - //Console.WriteLine(" [" + val.data + "]"); + NBT_Float val = new NBT_Float(BitConverter.ToSingle(gzBytes, 0)); return val; } private NBT_Value ReadDouble () { - //Console.Write("NBT_File.readDouble()"); - byte[] gzBytes = new byte[8]; stream.Read(gzBytes, 0, 8); @@ -381,18 +491,13 @@ namespace NBT Array.Reverse(gzBytes); } - NBT_Double val = new NBT_Double(); - val.data = BitConverter.ToDouble(gzBytes, 0); - - //Console.WriteLine(" [" + val.data + "]"); + NBT_Double val = new NBT_Double(BitConverter.ToDouble(gzBytes, 0)); return val; } private NBT_Value ReadByteArray () { - //Console.Write("NBT_File.readByteArray()"); - byte[] lenBytes = new byte[4]; stream.Read(lenBytes, 0, 4); @@ -400,25 +505,21 @@ namespace NBT Array.Reverse(lenBytes); } - NBT_ByteArray val = new NBT_ByteArray(); - val.length = BitConverter.ToInt32(lenBytes, 0); - - if (val.length < 0) { + int length = BitConverter.ToInt32(lenBytes, 0); + if (length < 0) { throw new NBTException(NBTException.MSG_READ_NEG); } - //Console.WriteLine(" [" + val.length + "]"); + byte[] data = new byte[length]; + stream.Read(data, 0, length); - val.data = new byte[val.length]; - stream.Read(val.data, 0, val.length); + NBT_ByteArray val = new NBT_ByteArray(data); return val; } private NBT_Value ReadString () { - //Console.Write("NBT_File.readString()"); - byte[] lenBytes = new byte[2]; stream.Read(lenBytes, 0, 2); @@ -436,26 +537,20 @@ namespace NBT System.Text.Encoding str = Encoding.GetEncoding(28591); - NBT_String val = new NBT_String(); - val.data = str.GetString(strBytes); - - //Console.WriteLine(" [" + val.data.ToString() + "]"); + NBT_String val = new NBT_String(str.GetString(strBytes)); return val; } private NBT_Value ReadList () { - //Console.Write("NBT_File.readList()"); - int gzByte = stream.ReadByte(); if (gzByte == -1) { throw new NBTException(NBTException.MSG_GZIP_ENDOFSTREAM); } - NBT_List val = new NBT_List(); - val.type = (NBT_Type)gzByte; - if (val.type > (NBT_Type)Enum.GetValues(typeof(NBT_Type)).GetUpperBound(0)) { + 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); } @@ -466,17 +561,13 @@ namespace NBT Array.Reverse(lenBytes); } - val.length = BitConverter.ToInt32(lenBytes, 0); - if (val.length < 0) { + int length = BitConverter.ToInt32(lenBytes, 0); + if (length < 0) { throw new NBTException(NBTException.MSG_READ_NEG); } - //Console.WriteLine(" [" + val.type + ", " + val.length + "]"); - - val.items = new List(); - - for (int i = 0; i < val.length; i++) { - val.items.Add(ReadValue(val.type)); + for (int i = 0; i < length; i++) { + val.AddItem(ReadValue(val.ValueType)); } return val; @@ -484,10 +575,7 @@ namespace NBT private NBT_Value ReadCompound () { - //Console.WriteLine("NBT_File.readCompound()"); - NBT_Compound val = new NBT_Compound(); - val.tags = new List(); while (true) { NBT_Tag tag = ReadTag(); @@ -495,8 +583,7 @@ namespace NBT break; } - val.length++; - val.tags.Add(tag); + val.AddTag(tag); } return val; @@ -504,16 +591,12 @@ namespace NBT private NBT_Tag ReadTag () { - //Console.Write("NBT_File.readTag()"); - NBT_Tag tag = new NBT_Tag(); tag.type = (NBT_Type)stream.ReadByte(); tag.name = null; tag.value = null; - //Console.WriteLine(" [" + tag.type + "]"); - if (tag.type != NBT_Type.TAG_END) { tag.name = ReadString().toString(); } @@ -523,20 +606,6 @@ namespace NBT return tag; } - /*private void write () - { - if (root != null) { - FileStream fStream = new FileStream(path, System.IO.FileMode.Truncate); - stream = new GZipStream(fStream, CompressionMode.Compress); - - writeTag(root); - - stream.Close(); - } - - stream = null; - }*/ - private void WriteValue (NBT_Value val) { switch (val.getType()) { @@ -587,15 +656,14 @@ namespace NBT private void WriteByte (NBT_Byte val) { - stream.WriteByte(val.data); + stream.WriteByte(val.Data); } private void WriteShort (NBT_Short val) { - byte[] gzBytes = BitConverter.GetBytes(val.data); + byte[] gzBytes = BitConverter.GetBytes(val.Data); if (BitConverter.IsLittleEndian) { - //gzBytes.Reverse(); Array.Reverse(gzBytes); } @@ -604,10 +672,9 @@ namespace NBT private void WriteInt (NBT_Int val) { - byte[] gzBytes = BitConverter.GetBytes(val.data); + byte[] gzBytes = BitConverter.GetBytes(val.Data); if (BitConverter.IsLittleEndian) { - //gzBytes.Reverse(); Array.Reverse(gzBytes); } @@ -616,10 +683,9 @@ namespace NBT private void WriteLong (NBT_Long val) { - byte[] gzBytes = BitConverter.GetBytes(val.data); + byte[] gzBytes = BitConverter.GetBytes(val.Data); if (BitConverter.IsLittleEndian) { - //gzBytes.Reverse(); Array.Reverse(gzBytes); } @@ -628,10 +694,9 @@ namespace NBT private void WriteFloat (NBT_Float val) { - byte[] gzBytes = BitConverter.GetBytes(val.data); + byte[] gzBytes = BitConverter.GetBytes(val.Data); if (BitConverter.IsLittleEndian) { - //gzBytes.Reverse(); Array.Reverse(gzBytes); } @@ -640,10 +705,9 @@ namespace NBT private void WriteDouble (NBT_Double val) { - byte[] gzBytes = BitConverter.GetBytes(val.data); + byte[] gzBytes = BitConverter.GetBytes(val.Data); if (BitConverter.IsLittleEndian) { - //gzBytes.Reverse(); Array.Reverse(gzBytes); } @@ -652,54 +716,51 @@ namespace NBT private void WriteByteArray (NBT_ByteArray val) { - byte[] lenBytes = BitConverter.GetBytes(val.length); + byte[] lenBytes = BitConverter.GetBytes(val.Length); if (BitConverter.IsLittleEndian) { - //lenBytes.Reverse(); Array.Reverse(lenBytes); } stream.Write(lenBytes, 0, 4); - stream.Write(val.data, 0, val.length); + stream.Write(val.Data, 0, val.Length); } private void WriteString (NBT_String val) { - byte[] lenBytes = BitConverter.GetBytes((short)val.data.Length); + byte[] lenBytes = BitConverter.GetBytes((short)val.Length); if (BitConverter.IsLittleEndian) { - //lenBytes.Reverse(); Array.Reverse(lenBytes); } stream.Write(lenBytes, 0, 2); System.Text.Encoding str = Encoding.GetEncoding(28591); - byte[] gzBytes = str.GetBytes(val.data); + byte[] gzBytes = str.GetBytes(val.Data); stream.Write(gzBytes, 0, gzBytes.Length); } private void WriteList (NBT_List val) { - byte[] lenBytes = BitConverter.GetBytes(val.length); + byte[] lenBytes = BitConverter.GetBytes(val.Count); if (BitConverter.IsLittleEndian) { - //lenBytes.Reverse(); Array.Reverse(lenBytes); } - stream.WriteByte((byte)val.type); + stream.WriteByte((byte)val.ValueType); stream.Write(lenBytes, 0, 4); - foreach (NBT_Value v in val.items) { + foreach (NBT_Value v in val.Items) { WriteValue(v); } } private void WriteCompound (NBT_Compound val) { - foreach (NBT_Tag t in val.tags) { + foreach (NBT_Tag t in val.Tags) { WriteTag(t); } @@ -720,7 +781,7 @@ namespace NBT } } - class NBTException : Exception + public class NBTException : Exception { public const String MSG_GZIP_ENDOFSTREAM = "Gzip Error: Unexpected end of stream"; @@ -733,4 +794,8 @@ namespace NBT public NBTException (String msg, Exception innerException) : base(msg, innerException) { } } + + public class InvalidTagException : Exception { } + + public class InvalidValueException : Exception { } } diff --git a/NBToolkit/NBToolkit/NBToolkit.csproj b/NBToolkit/NBToolkit/NBToolkit.csproj index b50acf4..e800cdc 100644 --- a/NBToolkit/NBToolkit/NBToolkit.csproj +++ b/NBToolkit/NBToolkit/NBToolkit.csproj @@ -54,6 +54,7 @@ + diff --git a/NBToolkit/NBToolkit/NBToolkit.csproj.user b/NBToolkit/NBToolkit/NBToolkit.csproj.user index 6827b14..7cafe7d 100644 --- a/NBToolkit/NBToolkit/NBToolkit.csproj.user +++ b/NBToolkit/NBToolkit/NBToolkit.csproj.user @@ -1,9 +1,9 @@  - oregen -w "E:\temp\worlds\testsave2" -b 112 -r 5 -s 5 -mindepth 1 -maxdepth 127 -oi 1 -d 2 -v + oregen -w "F:\Minecraft\tps - Copy" -b 15 --chunkinclude=35 -vv --MaxDepth=75 --MinDepth=67 - oregen -w "E:\temp\worlds\testsave2" -b 112 -r 5 -s 5 -mindepth 1 -maxdepth 127 -oi 1 -d 2 -v + oregen -w "F:\Minecraft\tps - Copy" -b 15 --chunkinclude=35 -vv --MaxDepth=75 --MinDepth=67 publish\ diff --git a/NBToolkit/NBToolkit/Oregen.cs b/NBToolkit/NBToolkit/Oregen.cs index 9ca35f2..d6e1ad9 100644 --- a/NBToolkit/NBToolkit/Oregen.cs +++ b/NBToolkit/NBToolkit/Oregen.cs @@ -196,7 +196,7 @@ namespace NBToolkit affectedChunks++; ApplyChunk(world, chunk); - //chunk.Save(); + world.GetChunkManager().SaveDirtyChunks(); } @@ -256,6 +256,45 @@ namespace NBToolkit opt = o; } + public override BlockRef GetBlockRef (int x, int y, int z) + { + BlockRef block; + try { + block = base.GetBlockRef(x, y, z); + } + catch { + return null; + } + + int blockID = block.ID; + + if ( + ((opt.OPT_OA) && (blockID != opt.OPT_ID)) || + ((opt.OPT_OO) && ( + blockID == BLOCK_COAL || blockID == BLOCK_IRON || + blockID == BLOCK_GOLD || blockID == BLOCK_REDSTONE || + blockID == BLOCK_DIAMOND || blockID == BLOCK_LAPIS || + blockID == BLOCK_DIRT || blockID == BLOCK_GRAVEL) && (blockID != opt.OPT_ID)) || + (opt.OPT_OB_INCLUDE.Count > 0) || + (blockID == BLOCK_STONE) + ) { + // If overriding list of ores, check membership + if (opt.OPT_OB_INCLUDE.Count > 0 && !opt.OPT_OB_INCLUDE.Contains(blockID)) { + return null; + } + + // Check for any excluded block + if (opt.OPT_OB_EXCLUDE.Contains(blockID)) { + return null; + } + + // We're allowed to update the block + return block; + } + + return null; + } + public override bool SetBlockID (int x, int y, int z, int id) { int blockID = 0; diff --git a/NBToolkit/NBToolkit/Purge.cs b/NBToolkit/NBToolkit/Purge.cs index 8729c8d..5c86311 100644 --- a/NBToolkit/NBToolkit/Purge.cs +++ b/NBToolkit/NBToolkit/Purge.cs @@ -62,8 +62,6 @@ namespace NBToolkit { private PurgeOptions opt; - private static Random rand = new Random(); - public Purge (PurgeOptions o) { opt = o; diff --git a/NBToolkit/NBToolkit/Replace.cs b/NBToolkit/NBToolkit/Replace.cs index faff422..2af3b67 100644 --- a/NBToolkit/NBToolkit/Replace.cs +++ b/NBToolkit/NBToolkit/Replace.cs @@ -50,28 +50,16 @@ namespace NBToolkit v => { OPT_PROB = Convert.ToDouble(v); OPT_PROB = Math.Max((double)OPT_PROB, 0.0); OPT_PROB = Math.Min((double)OPT_PROB, 1.0); } }, - /*{ "bxa=", "Update blocks with X-coord equal to or above {VAL}", - v => BL_X_GE = Convert.ToInt32(v) }, - { "bxb=", "Update blocks with X-coord equal to or below {VAL}", - v => BL_X_LE = Convert.ToInt32(v) },*/ { "bxr|BlockXRange=", "Update blocks with X-coord between {0:V1} and {1:V2}, inclusive. V1 or V2 may be left blank.", (v1, v2) => { try { BL_X_GE = Convert.ToInt32(v1); } catch (FormatException) { } try { BL_X_LE = Convert.ToInt32(v2); } catch (FormatException) { } } }, - /*{ "bya=", "Update blocks with Y-coord equal to or above {VAL}", - v => BL_Y_GE = Convert.ToInt32(v) }, - { "byb=", "Update blocks with Y-coord equal to or below {VAL}", - v => BL_Y_LE = Convert.ToInt32(v) },*/ { "byr|BlockYRange=", "Update blocks with Y-coord between {0:V1} and {1:V2}, inclusive. V1 or V2 may be left blank", (v1, v2) => { try { BL_Y_GE = Convert.ToInt32(v1); } catch (FormatException) { } try { BL_Y_LE = Convert.ToInt32(v2); } catch (FormatException) { } } }, - /*{ "bza=", "Update blocks with Z-coord equal to or above {VAL}", - v => BL_Z_GE = Convert.ToInt32(v) }, - { "bzb=", "Update blocks with Z-coord equal to or below {VAL}", - v => BL_Z_LE = Convert.ToInt32(v) },*/ { "bzr|BlockZRange=", "Update blocks with Z-coord between {0:V1} and {1:V2}, inclusive. V1 or V2 may be left blank", (v1, v2) => { try { BL_Z_GE = Convert.ToInt32(v1); } catch (FormatException) { }