Major refacotoring and updates to API.

This commit is contained in:
Justin Aquadro 2011-04-02 00:42:15 +00:00
parent e003835975
commit 3c2b29e9f0
31 changed files with 1126 additions and 1308 deletions

View file

@ -1,132 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public class BlockManager
{
public const int MIN_X = -32000000;
public const int MAX_X = 32000000;
public const int MIN_Y = 0;
public const int MAX_Y = 128;
public const int MIN_Z = -32000000;
public const int MAX_Z = 32000000;
public const int CHUNK_XLEN = 16;
public const int CHUNK_YLEN = 128;
public const int CHUNK_ZLEN = 16;
public const int CHUNK_XLOG = 4;
public const int CHUNK_YLOG = 7;
public const int CHUNK_ZLOG = 4;
public const int CHUNK_XMASK = 0xF;
public const int CHUNK_YMASK = 0x7F;
public const int CHUNK_ZMASK = 0xF;
protected ChunkManager _chunkMan;
public BlockManager (ChunkManager cm)
{
_chunkMan = cm;
}
public BlockManager (BlockManager bm)
{
_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)
{
if (x < MIN_X || x >= MAX_X)
return 0;
if (y < MIN_Y || y >= MAX_Y)
return 0;
if (z < MIN_Z || z >= MAX_Z)
return 0;
Chunk c = GetChunk(x, y, z);
if (c == null) {
return 0;
}
return c.GetBlockID(x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
}
public virtual int GetBlockData (int x, int y, int z) {
if (x < MIN_X || x >= MAX_X)
return 0;
if (y < MIN_Y || y >= MAX_Y)
return 0;
if (z < MIN_Z || z >= MAX_Z)
return 0;
Chunk c = GetChunk(x, y, z);
if (c == null) {
return 0;
}
return c.GetBlockData(x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
}
public int GetBlockLight (int x, int y, int z) { return 0; }
public int GetBlockSkylight (int x, int y, int z) { return 0; }
public void SetBlock (int x, int y, int z, int id, int data) { }
public virtual bool SetBlockID (int x, int y, int z, int id)
{
if (x < MIN_X || x >= MAX_X)
return false;
if (y < MIN_Y || y >= MAX_Y)
return false;
if (z < MIN_Z || z >= MAX_Z)
return false;
Chunk c = GetChunk(x, y, z);
if (c == null) {
return false;
}
return c.SetBlockID(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, id);
}
public virtual bool SetBlockData (int x, int y, int z, int data)
{
if (x < MIN_X || x >= MAX_X)
return false;
if (y < MIN_Y || y >= MAX_Y)
return false;
if (z < MIN_Z || z >= MAX_Z)
return false;
Chunk c = GetChunk(x, y, z);
if (c == null) {
return false;
}
return c.SetBlockData(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, data);
}
public Chunk GetChunk (int x, int y, int z)
{
x >>= CHUNK_XLOG;
z >>= CHUNK_ZLOG;
return _chunkMan.GetChunk(x, z);
}
}
}

View file

@ -1,131 +0,0 @@
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;
}
}
}

View file

@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public class ChunkKey : IEquatable<ChunkKey>
{
protected int cx;
protected int cz;
public ChunkKey (int _cx, int _cz)
{
cx = _cx;
cz = _cz;
}
public bool Equals (ChunkKey ck)
{
return this.cx == ck.cx && this.cz == ck.cz;
}
public override int GetHashCode ()
{
return cx ^ cz;
}
}
}

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/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
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

View file

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
using NBT;
public interface IBlock
{
BlockInfo Info { get; }
int ID { get; set; }
int Data { get; set; }
int BlockLight { get; set; }
int SkyLight { get; set; }
}
public class Block : IBlock
{
protected int _id;
protected int _data;
protected int _skylight;
protected int _blocklight;
protected NBT_Compound _tileEntities;
public BlockInfo Info
{
get { return BlockInfo.BlockTable[_id]; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
public int Data
{
get { return _data; }
set { _data = value; }
}
public int SkyLight
{
get { return _skylight; }
set { _skylight = value; }
}
public int BlockLight
{
get { return _blocklight; }
set { _blocklight = value; }
}
public Block (int id)
{
_id = id;
}
public Block (int id, int data)
{
_id = id;
_data = data;
}
public Block (Block block)
{
_id = block._id;
_data = block._data;
_skylight = block._skylight;
_blocklight = block._blocklight;
}
public Block (IBlock block)
{
_id = block.ID;
_data = block.Data;
_skylight = block.SkyLight;
_blocklight = block.BlockLight;
}
public Block (ChunkRef chunk, int lx, int ly, int lz)
{
_id = chunk.GetBlockID(lx, ly, lz);
_data = chunk.GetBlockData(lx, ly, lz);
_skylight = chunk.GetBlockSkyLight(lx, ly, lz);
_blocklight = chunk.GetBlockLight(lx, ly, lz);
}
public Block (BlockManager bm, int x, int y, int z)
: this(bm.GetBlockRef(x, y, z))
{
}
}
}

View file

@ -0,0 +1,445 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
using NBT;
public interface IBlockTileEntity
{
string TileEntityName { get; }
NBTCompoundNode TileEntitySchema { get; }
}
public class BlockInfo
{
public const int MAX_BLOCKS = 256;
public const int MAX_OPACITY = 15;
public const int MIN_OPACITY = 0;
public const int MAX_LUMINANCE = 15;
public const int MIN_LUMINANCE = 0;
private static BlockInfo[] _blockTable;
private static int[] _opacityTable;
private static int[] _luminanceTable;
public class ItemCache<T>
{
private T[] _cache;
public T this[int index]
{
get { return _cache[index]; }
}
public ItemCache (T[] cache)
{
_cache = cache;
}
}
private int _id = 0;
private string _name = "";
private int _opacity = MAX_OPACITY;
private int _luminance = MIN_LUMINANCE;
public static ItemCache<BlockInfo> BlockTable;
public static ItemCache<int> OpacityTable;
public static ItemCache<int> LuminanceTable;
public int ID
{
get { return _id; }
}
public string Name
{
get { return _name; }
}
public int Opacity
{
get { return _opacity; }
}
public int Luminance
{
get { return _luminance; }
}
public BlockInfo (int id)
{
_id = id;
_blockTable[_id] = this;
}
public BlockInfo (int id, string name)
{
_id = id;
_name = name;
_blockTable[_id] = this;
}
public BlockInfo SetOpacity (int opacity)
{
_opacity = MIN_OPACITY + opacity;
_opacityTable[_id] = _opacity;
return this;
}
public BlockInfo SetLuminance (int luminance)
{
_luminance = luminance;
_luminanceTable[_id] = _luminance;
return this;
}
protected static NBTCompoundNode tileEntitySchema = new NBTCompoundNode("")
{
new NBTScalerNode("id", NBT_Type.TAG_STRING),
new NBTScalerNode("x", NBT_Type.TAG_INT),
new NBTScalerNode("y", NBT_Type.TAG_INT),
new NBTScalerNode("z", NBT_Type.TAG_INT),
};
public static BlockInfo Air;
public static BlockInfo Stone;
public static BlockInfo Grass;
public static BlockInfo Dirt;
public static BlockInfo Cobblestone;
public static BlockInfo WoodPlank;
public static BlockInfo Sapling;
public static BlockInfo Bedrock;
public static BlockInfo Water;
public static BlockInfo StationaryWater;
public static BlockInfo Lava;
public static BlockInfo StationaryLava;
public static BlockInfo Sand;
public static BlockInfo Gravel;
public static BlockInfo GoldOre;
public static BlockInfo IronOre;
public static BlockInfo CoalOre;
public static BlockInfo Wood;
public static BlockInfo Leaves;
public static BlockInfo Sponge;
public static BlockInfo Glass;
public static BlockInfo LapisOre;
public static BlockInfo LapisBlock;
public static BlockInfoTrap Dispenser;
public static BlockInfo Sandstone;
public static BlockInfoMusic NoteBlock;
public static BlockInfo Bed;
public static BlockInfo Wool;
public static BlockInfo YellowFlower;
public static BlockInfo RedRose;
public static BlockInfo BrownMushroom;
public static BlockInfo RedMushroom;
public static BlockInfo GoldBlock;
public static BlockInfo IronBlock;
public static BlockInfo DoubleSlab;
public static BlockInfo Slab;
public static BlockInfo BrickBlock;
public static BlockInfo TNT;
public static BlockInfo Bookshelf;
public static BlockInfo MossStone;
public static BlockInfo Obsidian;
public static BlockInfo Torch;
public static BlockInfo Fire;
public static BlockInfoMonster MonsterSpawner;
public static BlockInfo WoodStairs;
public static BlockInfoChest Chest;
public static BlockInfo RedstoneWire;
public static BlockInfo DiamondOre;
public static BlockInfo DiamondBlock;
public static BlockInfo CraftTable;
public static BlockInfo Crops;
public static BlockInfo Farmland;
public static BlockInfoFurnace Furnace;
public static BlockInfoFurnace BurningFurnace;
public static BlockInfoSign SignPost;
public static BlockInfo WoodDoor;
public static BlockInfo Ladder;
public static BlockInfo Rails;
public static BlockInfo CobbleStairs;
public static BlockInfoSign WallSign;
public static BlockInfo Lever;
public static BlockInfo StonePlate;
public static BlockInfo IronDoor;
public static BlockInfo WoodPlate;
public static BlockInfo RedstoneOre;
public static BlockInfo GlowRedstoneOre;
public static BlockInfo RedstoneTorch;
public static BlockInfo RedstoneTorchOn;
public static BlockInfo StoneButton;
public static BlockInfo Snow;
public static BlockInfo Ice;
public static BlockInfo SnowBlock;
public static BlockInfo Cactus;
public static BlockInfo ClayBlock;
public static BlockInfo SugarCane;
public static BlockInfo Jukebox;
public static BlockInfo Fence;
public static BlockInfo Pumpkin;
public static BlockInfo Netherrack;
public static BlockInfo SoulSand;
public static BlockInfo Glowstone;
public static BlockInfo Portal;
public static BlockInfo JackOLantern;
public static BlockInfo CakeBlock;
public static BlockInfo RedstoneRepeater;
public static BlockInfo RedstoneRepeaterOn;
static BlockInfo ()
{
_blockTable = new BlockInfo[MAX_BLOCKS];
_opacityTable = new int[MAX_BLOCKS];
_luminanceTable = new int[MAX_BLOCKS];
BlockTable = new ItemCache<BlockInfo>(_blockTable);
OpacityTable = new ItemCache<int>(_opacityTable);
LuminanceTable = new ItemCache<int>(_luminanceTable);
Air = new BlockInfo(0, "Air").SetOpacity(0);
Stone = new BlockInfo(1, "Stone");
Grass = new BlockInfo(2, "Grass");
Dirt = new BlockInfo(3, "Dirt");
Cobblestone = new BlockInfo(4, "Cobblestone");
WoodPlank = new BlockInfo(5, "Wooden Plank");
Sapling = new BlockInfo(6, "Sapling").SetOpacity(0);
Bedrock = new BlockInfo(7, "Bedrock");
Water = new BlockInfo(8, "Water").SetOpacity(3);
StationaryWater = new BlockInfo(9, "Stationary Water").SetOpacity(3);
Lava = new BlockInfo(10, "Lava").SetLuminance(MAX_LUMINANCE);
StationaryLava = new BlockInfo(11, "Stationary Lava").SetLuminance(MAX_LUMINANCE);
Sand = new BlockInfo(12, "Sand");
Gravel = new BlockInfo(13, "Gravel");
GoldOre = new BlockInfo(14, "Gold Ore");
IronOre = new BlockInfo(15, "Iron Ore");
CoalOre = new BlockInfo(16, "Coal Ore");
Wood = new BlockInfo(17, "Wood");
Leaves = new BlockInfo(18, "Leaves").SetOpacity(1);
Sponge = new BlockInfo(19, "Sponge");
Glass = new BlockInfo(20, "Glass").SetOpacity(0);
LapisOre = new BlockInfo(21, "Lapis Lazuli Ore");
LapisBlock = new BlockInfo(22, "Lapis Lazuli Block");
Dispenser = new BlockInfoTrap(23, "Dispenser");
Sandstone = new BlockInfo(24, "Sandstone");
NoteBlock = new BlockInfoMusic(25, "Note Block");
Bed = new BlockInfo(26, "Bed").SetOpacity(0);
Wool = new BlockInfo(35, "Wool");
YellowFlower = new BlockInfo(37, "Yellow Flower").SetOpacity(0);
RedRose = new BlockInfo(38, "Red Rose").SetOpacity(0);
BrownMushroom = new BlockInfo(39, "Brown Mushroom").SetOpacity(0);
RedMushroom = new BlockInfo(40, "Red Mushroom").SetOpacity(0);
GoldBlock = new BlockInfo(41, "Gold Block");
IronBlock = new BlockInfo(42, "Iron Block");
DoubleSlab = new BlockInfo(43, "Double Slab");
Slab = new BlockInfo(44, "Slab");
BrickBlock = new BlockInfo(45, "Brick Block");
TNT = new BlockInfo(46, "TNT");
Bookshelf = new BlockInfo(47, "Bookshelf");
MossStone = new BlockInfo(48, "Moss Stone");
Obsidian = new BlockInfo(49, "Obsidian");
Torch = new BlockInfo(50, "Torch").SetOpacity(0).SetLuminance(MAX_LUMINANCE - 1);
Fire = new BlockInfo(51, "Fire").SetOpacity(0).SetLuminance(MAX_LUMINANCE);
MonsterSpawner = (BlockInfoMonster)new BlockInfoMonster(52, "Monster Spawner").SetOpacity(0);
WoodStairs = new BlockInfo(53, "Wooden Stairs").SetOpacity(0);
Chest = new BlockInfoChest(54, "Chest");
RedstoneWire = new BlockInfo(55, "Redstone Wire").SetOpacity(0);
DiamondOre = new BlockInfo(56, "Diamond Ore");
DiamondBlock = new BlockInfo(57, "Diamond Block");
CraftTable = new BlockInfo(58, "Crafting Table");
Crops = new BlockInfo(59, "Crops").SetOpacity(0);
Farmland = new BlockInfo(60, "Farmland").SetOpacity(0);
Furnace = new BlockInfoFurnace(61, "Furnace");
BurningFurnace = (BlockInfoFurnace)new BlockInfoFurnace(62, "Burning Furnace").SetLuminance(MAX_LUMINANCE - 1);
SignPost = (BlockInfoSign)new BlockInfoSign(63, "Sign Post").SetOpacity(0);
WoodDoor = new BlockInfo(64, "Wooden Door").SetOpacity(0);
Ladder = new BlockInfo(65, "Ladder").SetOpacity(0);
Rails = new BlockInfo(66, "Rails").SetOpacity(0);
CobbleStairs = new BlockInfo(67, "Cobblestone Stairs").SetOpacity(0);
WallSign = (BlockInfoSign)new BlockInfoSign(68, "Wall Sign").SetOpacity(0);
Lever = new BlockInfo(69, "Lever").SetOpacity(0);
StonePlate = new BlockInfo(70, "Stone Pressure Plate").SetOpacity(0);
IronDoor = new BlockInfo(71, "Iron Door").SetOpacity(0);
WoodPlank = new BlockInfo(72, "Wooden Pressure Plate").SetOpacity(0);
RedstoneOre = new BlockInfo(73, "Redstone Ore");
GlowRedstoneOre = new BlockInfo(74, "Glowing Redstone Ore").SetLuminance(9);
RedstoneTorch = new BlockInfo(75, "Redstone Torch (Off)").SetOpacity(0);
RedstoneTorchOn = new BlockInfo(76, "Redstone Torch (On)").SetOpacity(0).SetLuminance(7);
StoneButton = new BlockInfo(77, "Stone Button").SetOpacity(0);
Snow = new BlockInfo(78, "Snow").SetOpacity(0);
Ice = new BlockInfo(79, "Ice").SetOpacity(3);
SnowBlock = new BlockInfo(80, "Snow Block");
Cactus = new BlockInfo(81, "Cactus").SetOpacity(0);
ClayBlock = new BlockInfo(82, "Clay Block");
SugarCane = new BlockInfo(83, "Sugar Cane").SetOpacity(0);
Jukebox = new BlockInfo(84, "Jukebox");
Fence = new BlockInfo(85, "Fence").SetOpacity(0);
Pumpkin = new BlockInfo(86, "Pumpkin");
Netherrack = new BlockInfo(87, "Netherrack");
SoulSand = new BlockInfo(88, "Soul Sand");
Glowstone = new BlockInfo(89, "Glowstone Block").SetLuminance(MAX_LUMINANCE);
Portal = new BlockInfo(90, "Portal").SetOpacity(0).SetLuminance(11);
JackOLantern = new BlockInfo(91, "Jack-O-Lantern").SetLuminance(MAX_LUMINANCE);
CakeBlock = new BlockInfo(92, "Cake Block").SetOpacity(0);
RedstoneRepeater = new BlockInfo(93, "Redstone Repeater (Off)").SetOpacity(0);
RedstoneRepeaterOn = new BlockInfo(94, "Redstone Repeater (On)").SetOpacity(0).SetLuminance(7);
for (int i = 0; i < MAX_BLOCKS; i++) {
if (_blockTable[i] == null) {
_blockTable[i] = new BlockInfo(i, "Uknown Block");
}
}
}
}
public class BlockInfoTrap : BlockInfo, IBlockTileEntity
{
public string TileEntityName
{
get { return "Trap"; }
}
public NBTCompoundNode TileEntitySchema
{
get { return tileEntitySchema; }
}
public BlockInfoTrap (int id, string name)
: base(id, name)
{
}
protected static new NBTCompoundNode tileEntitySchema = BlockInfo.tileEntitySchema.MergeInto(new NBTCompoundNode("")
{
new NBTListNode("Items", NBT_Type.TAG_COMPOUND),
});
}
public class BlockInfoMusic : BlockInfo, IBlockTileEntity
{
public string TileEntityName
{
get { return "Music"; }
}
public NBTCompoundNode TileEntitySchema
{
get { return tileEntitySchema; }
}
public BlockInfoMusic (int id, string name)
: base(id, name)
{
}
protected static new NBTCompoundNode tileEntitySchema = BlockInfo.tileEntitySchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("note", NBT_Type.TAG_COMPOUND),
});
}
public class BlockInfoMonster : BlockInfo, IBlockTileEntity
{
public string TileEntityName
{
get { return "Monster Spawner"; }
}
public NBTCompoundNode TileEntitySchema
{
get { return tileEntitySchema; }
}
public BlockInfoMonster (int id, string name)
: base(id, name)
{
}
protected static new NBTCompoundNode tileEntitySchema = BlockInfo.tileEntitySchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("EntityId", NBT_Type.TAG_STRING),
new NBTScalerNode("Delay", NBT_Type.TAG_SHORT),
});
}
public class BlockInfoChest : BlockInfo, IBlockTileEntity
{
public string TileEntityName
{
get { return "Trap"; }
}
public NBTCompoundNode TileEntitySchema
{
get { return tileEntitySchema; }
}
public BlockInfoChest (int id, string name)
: base(id, name)
{
}
protected static new NBTCompoundNode tileEntitySchema = BlockInfo.tileEntitySchema.MergeInto(new NBTCompoundNode("")
{
new NBTListNode("Items", NBT_Type.TAG_COMPOUND),
});
}
public class BlockInfoFurnace : BlockInfo, IBlockTileEntity
{
public string TileEntityName
{
get { return "Furnace"; }
}
public NBTCompoundNode TileEntitySchema
{
get { return tileEntitySchema; }
}
public BlockInfoFurnace (int id, string name)
: base(id, name)
{
}
protected static new NBTCompoundNode tileEntitySchema = BlockInfo.tileEntitySchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("BurnTime", NBT_Type.TAG_SHORT),
new NBTScalerNode("CookTime", NBT_Type.TAG_SHORT),
new NBTListNode("Items", NBT_Type.TAG_COMPOUND),
});
}
public class BlockInfoSign : BlockInfo, IBlockTileEntity
{
public string TileEntityName
{
get { return "Sign"; }
}
public NBTCompoundNode TileEntitySchema
{
get { return tileEntitySchema; }
}
public BlockInfoSign (int id, string name)
: base(id, name)
{
}
protected static new NBTCompoundNode tileEntitySchema = BlockInfo.tileEntitySchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("Text1", NBT_Type.TAG_STRING),
new NBTScalerNode("Text2", NBT_Type.TAG_STRING),
new NBTScalerNode("Text3", NBT_Type.TAG_STRING),
new NBTScalerNode("Text4", NBT_Type.TAG_STRING),
});
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public struct BlockKey : IEquatable<BlockKey>
{
readonly int x;
readonly int y;
readonly int z;
public BlockKey (int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
public bool Equals (BlockKey bk)
{
return this.x == bk.x && this.y == bk.y && this.z == bk.z;
}
public override bool Equals (Object o)
{
try {
return this == (BlockKey)o;
}
catch {
return false;
}
}
public override int GetHashCode ()
{
int hash = 23;
hash = hash * 37 + x;
hash = hash * 37 + y;
hash = hash * 37 + z;
return hash;
}
public static bool operator == (BlockKey k1, BlockKey k2)
{
return k1.x == k2.x && k1.y == k2.y && k1.z == k2.z;
}
public static bool operator != (BlockKey k1, BlockKey k2)
{
return k1.x != k2.x || k1.y != k2.y || k1.z != k2.z;
}
}
}

View file

@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public class BlockManager
{
public const int MIN_X = -32000000;
public const int MAX_X = 32000000;
public const int MIN_Y = 0;
public const int MAX_Y = 128;
public const int MIN_Z = -32000000;
public const int MAX_Z = 32000000;
public const int CHUNK_XLEN = 16;
public const int CHUNK_YLEN = 128;
public const int CHUNK_ZLEN = 16;
public const int CHUNK_XLOG = 4;
public const int CHUNK_YLOG = 7;
public const int CHUNK_ZLOG = 4;
public const int CHUNK_XMASK = 0xF;
public const int CHUNK_YMASK = 0x7F;
public const int CHUNK_ZMASK = 0xF;
protected ChunkManager _chunkMan;
protected ChunkRef _cache;
public BlockManager (ChunkManager cm)
{
_chunkMan = cm;
}
public BlockManager (BlockManager bm)
{
_chunkMan = bm._chunkMan;
}
public virtual BlockRef GetBlockRef (int x, int y, int z)
{
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
return null;
}
return new BlockRef(_cache, x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
}
public virtual bool GetBlockID (int x, int y, int z, out int id)
{
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
id = 0;
return false;
}
id = _cache.GetBlockID(x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
return true;
}
public virtual bool GetBlockData (int x, int y, int z, out int data) {
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
data = 0;
return false;
}
data = _cache.GetBlockData(x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
return true;
}
public virtual bool GetBlockLight (int x, int y, int z, out int light) {
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
light = 0;
return false;
}
light = _cache.GetBlockLight(x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
return true;
}
public virtual bool GetBlockSkyLight (int x, int y, int z, out int light) {
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
light = 0;
return false;
}
light = _cache.GetBlockSkyLight(x & CHUNK_XMASK, y, z & CHUNK_ZMASK);
return true;
}
public void SetBlock (int x, int y, int z, int id, int data) { }
public virtual bool SetBlockID (int x, int y, int z, int id)
{
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
return false;
}
return _cache.SetBlockID(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, id);
}
public virtual bool SetBlockData (int x, int y, int z, int data)
{
_cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) {
return false;
}
return _cache.SetBlockData(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, data);
}
public ChunkRef GetChunk (int x, int y, int z)
{
x >>= CHUNK_XLOG;
z >>= CHUNK_ZLOG;
return _chunkMan.GetChunk(x, z);
}
/// <summary>
/// Called by other block-specific 'get' and 'set' functions to filter
/// out operations on some blocks. Override this method in derrived
/// classes to filter the entire BlockManager.
/// </summary>
protected virtual bool Check (int x, int y, int z) {
return (x >= MIN_X) && (x < MAX_X) &&
(y >= MIN_Y) && (y < MAX_Y) &&
(z >= MIN_Z) && (z < MAX_Z);
}
}
}

View file

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public class BlockRef : IBlock
{
protected ChunkRef _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 BlockInfo Info
{
get { return BlockInfo.BlockTable[_chunk.GetBlockID(_lx, _ly, _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.GetBlockSkyLight(_lx, _ly, _lz); }
set { _chunk.SetBlockSkyLight(_lx, _ly, _lz, value); }
}
public BlockRef (ChunkRef c, int lx, int ly, int lz)
{
_chunk = c;
_lx = lx;
_ly = ly;
_lz = lz;
}
public void CopyFrom (IBlock block)
{
ID = block.ID;
Data = block.Data;
BlockLight = block.BlockLight;
SkyLight = block.SkyLight;
}
}
}

View file

@ -7,7 +7,7 @@ using System.IO;
namespace NBToolkit
{
public class ChunkList : IEnumerable<Chunk>
public class ChunkList : IEnumerable<ChunkRef>
{
//private List<Region> _regions;
@ -32,9 +32,9 @@ namespace NBToolkit
return (IEnumerator)GetEnumerator();
}
IEnumerator<Chunk> IEnumerable<Chunk>.GetEnumerator ()
IEnumerator<ChunkRef> IEnumerable<ChunkRef>.GetEnumerator ()
{
return (IEnumerator<Chunk>)GetEnumerator();
return (IEnumerator<ChunkRef>)GetEnumerator();
}
public virtual ChunkEnumerator GetEnumerator ()
@ -43,12 +43,12 @@ namespace NBToolkit
}
}
public class ChunkEnumerator : IEnumerator<Chunk>
public class ChunkEnumerator : IEnumerator<ChunkRef>
{
protected Region _region;
protected ChunkManager _cm;
protected Chunk _chunk;
protected ChunkRef _chunk;
protected RegionEnumerator _enum = null;
protected int _x = 0;
@ -134,7 +134,7 @@ namespace NBToolkit
}
}
Chunk IEnumerator<Chunk>.Current
ChunkRef IEnumerator<ChunkRef>.Current
{
get
{
@ -142,7 +142,7 @@ namespace NBToolkit
}
}
public Chunk Current
public ChunkRef Current
{
get
{

View file

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public struct ChunkKey : IEquatable<ChunkKey>
{
readonly int cx;
readonly int cz;
public ChunkKey (int _cx, int _cz)
{
cx = _cx;
cz = _cz;
}
public bool Equals (ChunkKey ck)
{
return this.cx == ck.cx && this.cz == ck.cz;
}
public override bool Equals (Object o)
{
try {
return this == (ChunkKey)o;
}
catch {
return false;
}
}
public override int GetHashCode ()
{
int hash = 23;
hash = hash * 37 + cx;
hash = hash * 37 + cz;
return hash;
}
public static bool operator == (ChunkKey k1, ChunkKey k2)
{
return k1.cx == k2.cx && k1.cz == k2.cz;
}
public static bool operator != (ChunkKey k1, ChunkKey k2)
{
return k1.cx != k2.cx || k1.cz != k2.cz;
}
}
}

View file

@ -18,24 +18,24 @@ namespace NBToolkit
protected RegionManager _regionMan;
protected Dictionary<ChunkKey, WeakReference> _cache;
protected Dictionary<ChunkKey, Chunk> _dirty;
protected Dictionary<ChunkKey, ChunkRef> _dirty;
public ChunkManager (RegionManager rm)
{
_regionMan = rm;
_cache = new Dictionary<ChunkKey, WeakReference>();
_dirty = new Dictionary<ChunkKey, Chunk>();
_dirty = new Dictionary<ChunkKey, ChunkRef>();
}
public Chunk GetChunk (int cx, int cz)
public ChunkRef GetChunk (int cx, int cz)
{
ChunkKey k = new ChunkKey(cx, cz);
Chunk c = null;
ChunkRef c = null;
WeakReference chunkref = null;
if (_cache.TryGetValue(k, out chunkref)) {
c = chunkref.Target as Chunk;
c = chunkref.Target as ChunkRef;
}
else {
_cache.Add(k, new WeakReference(null));
@ -46,7 +46,7 @@ namespace NBToolkit
}
try {
c = new Chunk(this, cx, cz);
c = new ChunkRef(this, cx, cz);
_cache[k].Target = c;
return c;
}
@ -55,7 +55,7 @@ namespace NBToolkit
}
}
public Chunk GetChunkInRegion (Region r, int lcx, int lcz)
public ChunkRef GetChunkInRegion (Region r, int lcx, int lcz)
{
int cx = r.X * REGION_XLEN + lcx;
int cz = r.Z * REGION_ZLEN + lcz;
@ -79,7 +79,7 @@ namespace NBToolkit
return false;
}
public bool MarkChunkDirty (Chunk chunk)
public bool MarkChunkDirty (ChunkRef chunk)
{
ChunkKey k = new ChunkKey(chunk.X, chunk.Z);
if (!_dirty.ContainsKey(k)) {
@ -92,7 +92,7 @@ namespace NBToolkit
public int SaveDirtyChunks ()
{
int saved = 0;
foreach (Chunk c in _dirty.Values) {
foreach (ChunkRef c in _dirty.Values) {
if (c.Save()) {
saved++;
}

View file

@ -6,7 +6,38 @@ namespace NBToolkit
{
using NBT;
public class Chunk
public interface IChunk
{
bool IsPopulated { get; set; }
BlockInfo GetBlockInfo (int lx, int ly, int lz);
int GetBlockID (int lx, int ly, int lz);
int GetBlockData (int lx, int ly, int lz);
int GetBlockLight (int lx, int ly, int lz);
int GetBlockSkyLight (int lx, int ly, int lz);
void GetBlockID (int lx, int ly, int lz, int id);
void GetBlockData (int lx, int ly, int lz, int data);
void GetBlockLight (int lx, int ly, int lz, int light);
void GetBlockSkyLight (int lx, int ly, int lz, int light);
//int CountBlocks (Predicate<BlockRef> match);
int CountBlockID (int id);
int CountBlockData (int id, int data);
int GetHeight (int lx, int lz);
NBT_Compound GetTileEntity (int lx, int ly, int lz);
void AddTileEntity (int lx, int ly, int lz, string id, NBT_Compound data);
void RemoveTileEntity (int lx, int ly, int lz);
//IEnumerable<BlockRef> FilterBlocks (Predicate<BlockRef> match);
}
public class ChunkRef
{
protected int _cx;
protected int _cz;
@ -53,7 +84,7 @@ namespace NBToolkit
}
}
public Chunk (ChunkManager cm, int cx, int cz)
public ChunkRef (ChunkManager cm, int cx, int cz)
{
_chunkMan = cm;
_cx = cx;
@ -90,6 +121,8 @@ namespace NBToolkit
}
_nbt = r.GetChunkTree(LocalX, LocalZ);
ChunkVerifier cv = new ChunkVerifier(_nbt);
cv.Verify();
return _nbt;
}
@ -111,6 +144,11 @@ namespace NBToolkit
return false;
}
public Block GetBlock (int lx, int ly, int lz)
{
return new Block(this, lx, ly, lz);
}
public BlockRef GetBlockRef (int lx, int ly, int lz)
{
return new BlockRef(this, lx, ly, lz);
@ -119,7 +157,7 @@ namespace NBToolkit
public int GetBlockID (int x, int y, int z)
{
if (_blocks == null) {
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").Value.ToNBTByteArray();
_blocks = GetTree().Root["Level"].ToNBTCompound()["Blocks"].ToNBTByteArray();
}
return _blocks.Data[x << 11 | z << 7 | y];
@ -128,7 +166,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.ToNBTByteArray();
_blocks = GetTree().Root["Level"].ToNBTCompound()["Blocks"].ToNBTByteArray();
}
int index = x << 11 | z << 7 | y;
@ -145,7 +183,7 @@ namespace NBToolkit
public int CountBlockID (int id)
{
if (_blocks == null) {
_blocks = GetTree().Root.FindTagByName("Level").FindTagByName("Blocks").Value.ToNBTByteArray();
_blocks = GetTree().Root["Level"].ToNBTCompound()["Blocks"].ToNBTByteArray();
}
int c = 0;
@ -161,7 +199,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.ToNBTByteArray().Data);
_data = new NibbleArray(GetTree().Root["Level"].ToNBTCompound()["Data"].ToNBTByteArray().Data);
}
return _data[x << 11 | z << 7 | y];
@ -170,7 +208,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.ToNBTByteArray().Data);
_data = new NibbleArray(GetTree().Root["Level"].ToNBTCompound()["Data"].ToNBTByteArray().Data);
}
int index = x << 11 | z << 7 | y;
@ -187,7 +225,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.ToNBTByteArray().Data);
_blockLight = new NibbleArray(GetTree().Root["Level"].ToNBTCompound()["BlockLight"].ToNBTByteArray().Data);
}
return _blockLight[x << 11 | z << 7 | y];
@ -196,7 +234,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.ToNBTByteArray().Data);
_blockLight = new NibbleArray(GetTree().Root["Level"].ToNBTCompound()["BlockLight"].ToNBTByteArray().Data);
}
int index = x << 11 | z << 7 | y;
@ -210,19 +248,19 @@ namespace NBToolkit
return true;
}
public int GetSkyLight (int x, int y, int z)
public int GetBlockSkyLight (int x, int y, int z)
{
if (_skyLight == null) {
_skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").Value.ToNBTByteArray().Data);
_skyLight = new NibbleArray(GetTree().Root["Level"].ToNBTCompound()["SkyLight"].ToNBTByteArray().Data);
}
return _skyLight[x << 11 | z << 7 | y];
}
public bool SetSkyLight (int x, int y, int z, int light)
public bool SetBlockSkyLight (int x, int y, int z, int light)
{
if (_skyLight == null) {
_skyLight = new NibbleArray(GetTree().Root.FindTagByName("Level").FindTagByName("SkyLight").Value.ToNBTByteArray().Data);
_skyLight = new NibbleArray(GetTree().Root["Level"].ToNBTCompound()["SkyLight"].ToNBTByteArray().Data);
}
int index = x << 11 | z << 7 | y;
@ -238,7 +276,34 @@ namespace NBToolkit
public bool IsPopulated ()
{
return GetTree().Root.FindTagByName("Level").FindTagByName("TerrainPopulated").Value.ToNBTByte().Data == 1;
return GetTree().Root["Level"].ToNBTCompound()["TerrainPopulated"].ToNBTByte().Data == 1;
}
public NBT_Compound GetTileEntity (int x, int y, int z)
{
NBT_List telist = GetTree().Root["Level"].ToNBTCompound()["TileEntities"].ToNBTList();
foreach (NBT_Compound te in telist) {
if (te["x"].ToNBTInt().Data == x &&
te["y"].ToNBTInt().Data == y &&
te["z"].ToNBTInt().Data == z) {
return te;
}
}
return null;
}
public bool RemoveTileEntity (int x, int y, int z)
{
NBT_Compound te = GetTileEntity(x, y, z);
if (te == null) {
return false;
}
NBT_List telist = GetTree().Root["Level"].ToNBTCompound()["TileEntities"].ToNBTList();
return telist.Remove(te);
}
protected bool MarkDirty ()
@ -252,22 +317,22 @@ namespace NBToolkit
return true;
}
public Chunk GetNorthNeighbor ()
public ChunkRef GetNorthNeighbor ()
{
return _chunkMan.GetChunk(_cx - 1, _cz);
}
public Chunk GetSouthNeighbor ()
public ChunkRef GetSouthNeighbor ()
{
return _chunkMan.GetChunk(_cx + 1, _cz);
}
public Chunk GetEastNeighbor ()
public ChunkRef GetEastNeighbor ()
{
return _chunkMan.GetChunk(_cx, _cz - 1);
}
public Chunk GetWestNeighbor ()
public ChunkRef GetWestNeighbor ()
{
return _chunkMan.GetChunk(_cx, _cz + 1);
}

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
using NBT;
public class ChunkVerifier : NBTVerifier
{
protected static NBTCompoundNode levelSchema = new NBTCompoundNode()
{
new NBTCompoundNode("Level")
{
new NBTArrayNode("Blocks", 32768),
new NBTArrayNode("Data", 16384),
new NBTArrayNode("SkyLight", 16384),
new NBTArrayNode("BlockLight", 16384),
new NBTArrayNode("HeightMap", 256),
new NBTListNode("Entities", NBT_Type.TAG_COMPOUND),
new NBTListNode("TileEntities", NBT_Type.TAG_COMPOUND),
new NBTScalerNode("LastUpdate", NBT_Type.TAG_LONG),
new NBTScalerNode("xPos", NBT_Type.TAG_INT),
new NBTScalerNode("zPos", NBT_Type.TAG_INT),
new NBTScalerNode("TerrainPopulated", NBT_Type.TAG_BYTE),
},
};
public ChunkVerifier (NBT_Tree tree)
: base(tree.Root, levelSchema)
{
}
}
}

View file

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public struct RegionKey : IEquatable<RegionKey>
{
readonly int rx;
readonly int rz;
public RegionKey (int _rx, int _rz)
{
rx = _rx;
rz = _rz;
}
public bool Equals (RegionKey ck)
{
return this.rx == ck.rx && this.rz == ck.rz;
}
public override bool Equals (Object o)
{
try {
return this == (RegionKey)o;
}
catch {
return false;
}
}
public override int GetHashCode ()
{
int hash = 23;
hash = hash * 37 + rx;
hash = hash * 37 + rz;
return hash;
}
public static bool operator == (RegionKey k1, RegionKey k2)
{
return k1.rx == k2.rx && k1.rz == k2.rz;
}
public static bool operator != (RegionKey k1, RegionKey k2)
{
return k1.rx != k2.rx || k1.rz != k2.rz;
}
}
}

View file

@ -9,7 +9,7 @@ using System.Text;
namespace NBToolkit
{
class MathHelper
public class MathHelper
{
private static float[] trigTable = new float[65536];

View file

@ -1,471 +0,0 @@
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

@ -1,86 +0,0 @@
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

@ -1,373 +0,0 @@
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

@ -57,34 +57,39 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BlockManager.cs" />
<Compile Include="BlockRef.cs" />
<Compile Include="Chunk.cs" />
<Compile Include="ChunkEnumerator.cs" />
<Compile Include="Map\Block.cs" />
<Compile Include="Map\BlockInfo.cs" />
<Compile Include="Map\BlockKey.cs" />
<Compile Include="Map\BlockManager.cs" />
<Compile Include="Map\BlockRef.cs" />
<Compile Include="Map\ChunkRef.cs" />
<Compile Include="Map\ChunkEnumerator.cs" />
<Compile Include="ChunkFilter.cs" />
<Compile Include="ChunkKey.cs" />
<Compile Include="ChunkManager.cs" />
<Compile Include="Map\ChunkKey.cs" />
<Compile Include="Map\ChunkManager.cs" />
<Compile Include="Map\ChunkVerifier.cs" />
<Compile Include="FilteredChunkEnumerator.cs" />
<Compile Include="GenOres.cs" />
<Compile Include="MathHelper.cs" />
<Compile Include="NBT\NBT.cs" />
<Compile Include="NBT\NBTTag.cs" />
<Compile Include="NBT\NBTSchema.cs" />
<Compile Include="NBT\NBTValues.cs" />
<Compile Include="NBT\NBTVerifier.cs" />
<Compile Include="NDesk\Options.cs" />
<Compile Include="NibbleArray.cs" />
<Compile Include="Oregen.cs" />
<Compile Include="Purge.cs" />
<Compile Include="Region.cs" />
<Compile Include="RegionEnumerator.cs" />
<Compile Include="RegionKey.cs" />
<Compile Include="RegionManager.cs" />
<Compile Include="Map\Region.cs" />
<Compile Include="Map\RegionEnumerator.cs" />
<Compile Include="Map\RegionKey.cs" />
<Compile Include="Map\RegionManager.cs" />
<Compile Include="Replace.cs" />
<Compile Include="TKFilter.cs" />
<Compile Include="TKOptions.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegionFile.cs" />
<Compile Include="World.cs" />
<Compile Include="Map\RegionFile.cs" />
<Compile Include="Map\World.cs" />
<Compile Include="Zlib\Crc32.cs" />
<Compile Include="Zlib\Deflate.cs" />
<Compile Include="Zlib\DeflateStream.cs" />

View file

@ -185,7 +185,7 @@ namespace NBToolkit
World world = new World(opt.OPT_WORLD);
int affectedChunks = 0;
foreach (Chunk chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
foreach (ChunkRef chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
if (chunk == null || !chunk.IsPopulated()) {
continue;
}
@ -204,7 +204,7 @@ namespace NBToolkit
Console.WriteLine("Affected Chunks: " + affectedChunks);
}
public void ApplyChunk (World world, Chunk chunk)
public void ApplyChunk (World world, ChunkRef chunk)
{
if (opt.OPT_V) {
Console.WriteLine("Generating {0} size {1} deposits of {2} between {3} and {4}",
@ -257,7 +257,42 @@ namespace NBToolkit
opt = o;
}
public override BlockRef GetBlockRef (int x, int y, int z)
protected override bool Check (int x, int y, int z)
{
if (!base.Check(x, y, z)) {
return false;
}
int blockID = base.GetBlockID(x, y, z);
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 false;
}
// Check for any excluded block
if (opt.OPT_OB_EXCLUDE.Contains(blockID)) {
return false;
}
// We're allowed to update the block
return true;
}
return false;
}
/*public override BlockRef GetBlockRef (int x, int y, int z)
{
BlockRef block;
try {
@ -267,6 +302,10 @@ namespace NBToolkit
return null;
}
if (block == null) {
return null;
}
int blockID = block.ID;
if (
@ -331,6 +370,6 @@ namespace NBToolkit
}
return false;
}
}*/
}
}

View file

@ -59,7 +59,7 @@ namespace NBToolkit
}
}
class Purge : TKFilter
public class Purge : TKFilter
{
private PurgeOptions opt;
@ -73,7 +73,7 @@ namespace NBToolkit
World world = new World(opt.OPT_WORLD);
int affectedChunks = 0;
foreach (Chunk chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
foreach (ChunkRef chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
affectedChunks++;
world.GetChunkManager().DeleteChunk(chunk.X, chunk.Z);
}

View file

@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit
{
public class RegionKey : IEquatable<RegionKey>
{
protected int rx;
protected int rz;
public RegionKey (int _rx, int _rz)
{
rx = _rx;
rz = _rz;
}
public bool Equals (RegionKey ck)
{
return this.rx == ck.rx && this.rz == ck.rz;
}
public override int GetHashCode ()
{
return rx ^ rz;
}
}
}

View file

@ -136,7 +136,7 @@ namespace NBToolkit
}
}
class Replace : TKFilter
public class Replace : TKFilter
{
private ReplaceOptions opt;
@ -152,7 +152,7 @@ namespace NBToolkit
World world = new World(opt.OPT_WORLD);
int affectedChunks = 0;
foreach (Chunk chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
foreach (ChunkRef chunk in new FilteredChunkList(world.GetChunkManager(), opt.GetChunkFilter())) {
affectedChunks++;
ApplyChunk(world, chunk);
@ -162,7 +162,7 @@ namespace NBToolkit
Console.WriteLine("Affected Chunks: " + affectedChunks);
}
public void ApplyChunk (World world, Chunk chunk)
public void ApplyChunk (World world, ChunkRef chunk)
{
int xBase = chunk.X * BlockManager.CHUNK_XLEN;
int zBase = chunk.Z * BlockManager.CHUNK_ZLEN;

View file

@ -91,7 +91,7 @@ namespace NBToolkit
}
}
class TKOptionException : Exception
public class TKOptionException : Exception
{
public TKOptionException () { }