From 5234915d9adc1157f4e2a6a95d0b7b0607916fe1 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 9 Apr 2011 00:56:40 +0000 Subject: [PATCH] Refactoring World class to separate invocations of Alpha and Beta maps. Dead code removal. --- NBToolkit/Dump.cs | 6 +- NBToolkit/Oregen.cs | 12 +- NBToolkit/Purge.cs | 8 +- NBToolkit/Replace.cs | 8 +- NBToolkit/TKFilter.cs | 24 +- NBToolkit/TKOptions.cs | 24 +- Substrate/SubstrateCS/Source/Block.cs | 123 --------- .../SubstrateCS/Source/BlockInterface.cs | 50 ---- Substrate/SubstrateCS/Source/BlockManager.cs | 245 ------------------ Substrate/SubstrateCS/Source/BlockRef.cs | 200 -------------- Substrate/SubstrateCS/Source/World.cs | 142 +++++++--- 11 files changed, 148 insertions(+), 694 deletions(-) diff --git a/NBToolkit/Dump.cs b/NBToolkit/Dump.cs index 4b5ca14..bc4f277 100644 --- a/NBToolkit/Dump.cs +++ b/NBToolkit/Dump.cs @@ -96,10 +96,8 @@ namespace NBToolkit public override void Run () { - World world = new World(opt.OPT_WORLD); - - ChunkManager cm = world.GetChunkManager() as ChunkManager; - FilteredChunkManager fcm = new FilteredChunkManager(cm, opt.GetChunkFilter()); + NBTWorld world = GetWorld(opt); + FilteredChunkManager fcm = new FilteredChunkManager(world.ChunkManager, opt.GetChunkFilter()); StreamWriter fstr; try { diff --git a/NBToolkit/Oregen.cs b/NBToolkit/Oregen.cs index b67931a..b30d25d 100644 --- a/NBToolkit/Oregen.cs +++ b/NBToolkit/Oregen.cs @@ -181,10 +181,8 @@ namespace NBToolkit public override void Run () { - World world = new World(opt.OPT_WORLD); - - IChunkManager cm = world.GetChunkManager(); - FilteredChunkManager fcm = new FilteredChunkManager(cm, opt.GetChunkFilter()); + NBTWorld world = GetWorld(opt); + FilteredChunkManager fcm = new FilteredChunkManager(world.ChunkManager, opt.GetChunkFilter()); int affectedChunks = 0; foreach (ChunkRef chunk in fcm) { @@ -200,13 +198,13 @@ namespace NBToolkit ApplyChunk(world, chunk); - world.GetChunkManager().Save(); + fcm.Save(); } Console.WriteLine("Affected Chunks: " + affectedChunks); } - public void ApplyChunk (World world, ChunkRef chunk) + public void ApplyChunk (NBTWorld world, ChunkRef chunk) { if (opt.OPT_V) { Console.WriteLine("Generating {0} size {1} deposits of {2} between {3} and {4}", @@ -223,7 +221,7 @@ namespace NBToolkit ((NativeGenOre)generator).MathFix = opt.OPT_MATHFIX; } - BlockManager bm = new GenOreBlockManager(world.GetChunkManager(), opt); + BlockManager bm = new GenOreBlockManager(world.ChunkManager, opt); for (int i = 0; i < opt.OPT_ROUNDS; i++) { if (opt.OPT_VV) { diff --git a/NBToolkit/Purge.cs b/NBToolkit/Purge.cs index 2b5736d..e5718ec 100644 --- a/NBToolkit/Purge.cs +++ b/NBToolkit/Purge.cs @@ -69,15 +69,13 @@ namespace NBToolkit public override void Run () { - World world = new World(opt.OPT_WORLD); - - ChunkManager cm = world.GetChunkManager() as ChunkManager; - FilteredChunkManager fcm = new FilteredChunkManager(cm, opt.GetChunkFilter()); + NBTWorld world = GetWorld(opt); + FilteredChunkManager fcm = new FilteredChunkManager(world.ChunkManager, opt.GetChunkFilter()); int affectedChunks = 0; foreach (ChunkRef chunk in fcm) { affectedChunks++; - world.GetChunkManager().DeleteChunk(chunk.X, chunk.Z); + fcm.DeleteChunk(chunk.X, chunk.Z); } Console.WriteLine("Purged Chunks: " + affectedChunks); diff --git a/NBToolkit/Replace.cs b/NBToolkit/Replace.cs index 3833c77..759f702 100644 --- a/NBToolkit/Replace.cs +++ b/NBToolkit/Replace.cs @@ -149,10 +149,8 @@ namespace NBToolkit public override void Run () { - World world = new World(opt.OPT_WORLD); - ChunkManager cm = world.GetChunkManager() as ChunkManager; - - FilteredChunkManager fcm = new FilteredChunkManager(cm, opt.GetChunkFilter()); + NBTWorld world = GetWorld(opt); + FilteredChunkManager fcm = new FilteredChunkManager(world.ChunkManager, opt.GetChunkFilter()); int affectedChunks = 0; foreach (ChunkRef chunk in fcm) { @@ -160,7 +158,7 @@ namespace NBToolkit ApplyChunk(world, chunk); - world.GetChunkManager().Save(); + fcm.Save(); } Console.WriteLine("Affected Chunks: " + affectedChunks); diff --git a/NBToolkit/TKFilter.cs b/NBToolkit/TKFilter.cs index 61b5de5..90293a7 100644 --- a/NBToolkit/TKFilter.cs +++ b/NBToolkit/TKFilter.cs @@ -1,13 +1,33 @@ using System; -using System.Collections.Generic; -using System.Text; +using Substrate; namespace NBToolkit { + + public abstract class TKFilter { //public abstract void ApplyChunk (NBT_Tree root); public abstract void Run (); + + public NBTWorld GetWorld (TKOptions opt) + { + NBTWorld world = null; + try { + if (opt.OPT_ALPHA) { + world = new AlphaWorld(opt.OPT_WORLD, opt.OPT_DIM); + } + else { + world = new BetaWorld(opt.OPT_WORLD, opt.OPT_REGION, opt.OPT_DIM); + } + } + catch (Exception ex) { + Console.WriteLine("Error: " + ex.Message); + Environment.Exit(1); + } + + return world; + } } } diff --git a/NBToolkit/TKOptions.cs b/NBToolkit/TKOptions.cs index cc13d8b..5361e48 100644 --- a/NBToolkit/TKOptions.cs +++ b/NBToolkit/TKOptions.cs @@ -18,11 +18,13 @@ namespace NBToolkit public string OPT_WORLD = ""; public string OPT_REGION = "region"; + public string OPT_DIM = ""; // Verbosity public bool OPT_V = false; public bool OPT_VV = false; public bool OPT_HELP = false; + public bool OPT_ALPHA = false; public TKOptions () { @@ -32,8 +34,12 @@ namespace NBToolkit v => OPT_WORLD = v }, { "h|help", "Print this help message", v => OPT_HELP = true }, + { "alpha", "Specify that the world is stored as individual chunk files", + v => OPT_ALPHA = true }, { "nether", "Update the Nether instead of the main region", - v => OPT_REGION = "DIM-1/region" }, + v => OPT_DIM = "DIM-1" }, + { "region", "Specify the name of the region directory", + v => OPT_REGION = v }, { "v", "Verbose output", v => OPT_V = true }, { "vv", "Very verbose output", @@ -72,22 +78,6 @@ namespace NBToolkit throw new TKOptionException(); } - - /*if (!File.Exists(Path.Combine(OPT_WORLD, "level.dat"))) { - Console.WriteLine("Error: The supplied world path is invalid"); - Console.WriteLine(); - this.PrintUsage(); - - throw new TKOptionException(); - } - - if (!Directory.Exists(Path.Combine(OPT_WORLD, OPT_REGION))) { - Console.WriteLine("Error: The supplied world path does not contain region: " + OPT_REGION); - Console.WriteLine(); - this.PrintUsage(); - - throw new TKOptionException(); - }*/ } } diff --git a/Substrate/SubstrateCS/Source/Block.cs b/Substrate/SubstrateCS/Source/Block.cs index 3b52c07..6ca126b 100644 --- a/Substrate/SubstrateCS/Source/Block.cs +++ b/Substrate/SubstrateCS/Source/Block.cs @@ -122,127 +122,4 @@ namespace Substrate #endregion } - - /*public class Block : IBlock, ICopyable - { - private int _id; - private int _data; - private int _skylight; - private int _blocklight; - - private TileEntity _tileEntity; - - public BlockInfo Info - { - get { return BlockInfo.BlockTable[_id]; } - } - - public int ID - { - get { return _id; } - set - { - BlockInfoEx info1 = BlockInfo.BlockTable[_id] as BlockInfoEx; - BlockInfoEx info2 = BlockInfo.BlockTable[value] as BlockInfoEx; - - if (info1 != info2) { - if (info1 != null) { - _tileEntity = null; - } - - if (info2 != null) { - _tileEntity = TileEntityFactory.Create(info2.TileEntityName); - } - } - - _id = value; - } - } - - public int Data - { - get { return _data; } - set - { - if (BlockManager.EnforceDataLimits && BlockInfo.BlockTable[_id] != null) { - if (!BlockInfo.BlockTable[_id].TestData(value)) { - return; - } - } - _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 (IChunk 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); - _tileEntity = chunk.GetTileEntity(lx, ly, lz).Copy(); - } - - public TileEntity GetTileEntity () - { - return _tileEntity; - } - - public bool SetTileEntity (TileEntity te) - { - BlockInfoEx info = BlockInfo.BlockTable[_id] as BlockInfoEx; - if (info == null) { - return false; - } - - if (te.GetType() != TileEntityFactory.Lookup(info.TileEntityName)) { - return false; - } - - _tileEntity = te; - return true; - } - - public bool ClearTileEntity () - { - _tileEntity = null; - return true; - } - - #region ICopyable Members - - public Block Copy () - { - Block block = new Block(_id, _data); - block._blocklight = _blocklight; - block._skylight = _skylight; - block._tileEntity = _tileEntity.Copy(); - - return block; - } - - #endregion - }*/ } diff --git a/Substrate/SubstrateCS/Source/BlockInterface.cs b/Substrate/SubstrateCS/Source/BlockInterface.cs index 3a5cb54..e0c258a 100644 --- a/Substrate/SubstrateCS/Source/BlockInterface.cs +++ b/Substrate/SubstrateCS/Source/BlockInterface.cs @@ -87,54 +87,4 @@ namespace Substrate public interface IBlockManager : IAlphaBlockContainer { } - - /*public interface IBlock - { - BlockInfo Info { get; } - int ID { get; set; } - int Data { get; set; } - int BlockLight { get; set; } - int SkyLight { get; set; } - - TileEntity GetTileEntity (); - bool SetTileEntity (TileEntity te); - bool ClearTileEntity (); - } - - public interface IBlockContainer - { - int BlockGlobalX (int x); - int BlockGlobalY (int y); - int BlockGlobalZ (int z); - - int BlockLocalX (int x); - int BlockLocalY (int y); - int BlockLocalZ (int z); - - Block GetBlock (int lx, int ly, int lz); - BlockRef GetBlockRef (int lx, int ly, int lz); - - 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 SetBlock (int lx, int ly, int lz, Block block); - - bool SetBlockID (int lx, int ly, int lz, int id); - bool SetBlockData (int lx, int ly, int lz, int data); - bool SetBlockLight (int lx, int ly, int lz, int light); - bool SetBlockSkyLight (int lx, int ly, int lz, int light); - - TileEntity GetTileEntity (int lx, int ly, int lz); - bool SetTileEntity (int lx, int ly, int lz, TileEntity te); - bool ClearTileEntity (int lx, int ly, int lz); - } - - public interface IBlockManager : IBlockContainer - { - - }*/ } diff --git a/Substrate/SubstrateCS/Source/BlockManager.cs b/Substrate/SubstrateCS/Source/BlockManager.cs index 29587d3..dcc7ce5 100644 --- a/Substrate/SubstrateCS/Source/BlockManager.cs +++ b/Substrate/SubstrateCS/Source/BlockManager.cs @@ -283,249 +283,4 @@ namespace Substrate #endregion } - - /*public interface IBlockManager : IBlockContainer - { - Block GetBlock (int x, int y, int z); - BlockRef GetBlockRef (int x, int y, int z); - BlockInfo GetBlockInfo (int x, int y, int z); - - bool SetBlock (int x, int y, int z, Block block); - }*/ - - /*public class BlockManager : IBlockManager - { - 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; - - public static bool EnforceDataLimits = true; - - protected IChunkManager _chunkMan; - - protected ChunkRef _cache; - - public BlockManager (IChunkManager cm) - { - _chunkMan = cm; - } - - public BlockManager (BlockManager bm) - { - _chunkMan = bm._chunkMan; - } - - public int BlockGlobalX (int x) - { - return x; - } - - public int BlockGlobalY (int y) - { - return y; - } - - public int BlockGlobalZ (int z) - { - return z; - } - - public int BlockLocalX (int x) - { - return x & CHUNK_XMASK; - } - - public int BlockLocalY (int y) - { - return y & CHUNK_YMASK; - } - - public int BlockLocalZ (int z) - { - return z & CHUNK_ZMASK; - } - - public virtual Block GetBlock (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return null; - } - - return _cache.GetBlock(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - 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 _cache.GetBlockRef(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual BlockInfo GetBlockInfo (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return null; - } - - return _cache.GetBlockInfo(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual int GetBlockID (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null) { - return 0; - } - - return _cache.GetBlockID(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual int GetBlockData (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null) { - return 0; - } - - return _cache.GetBlockData(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual int GetBlockLight (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null) { - return 0; - } - - return _cache.GetBlockLight(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual int GetBlockSkyLight (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null) { - return 0; - } - - return _cache.GetBlockSkyLight(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual void SetBlock (int x, int y, int z, Block block) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return; - } - - _cache.SetBlock(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, block); - } - - 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 bool SetBlockLight (int x, int y, int z, int light) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return false; - } - - return _cache.SetBlockID(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, light); - } - - public bool SetBlockSkyLight (int x, int y, int z, int light) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return false; - } - - return _cache.SetBlockSkyLight(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, light); - } - - public virtual TileEntity GetTileEntity (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return null; - } - - return _cache.GetTileEntity(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - public virtual bool SetTileEntity (int x, int y, int z, TileEntity te) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return false; - } - - return _cache.SetTileEntity(x & CHUNK_XMASK, y, z & CHUNK_ZMASK, te); - } - - public virtual bool ClearTileEntity (int x, int y, int z) - { - _cache = GetChunk(x, y, z); - if (_cache == null || !Check(x, y, z)) { - return false; - } - - return _cache.ClearTileEntity(x & CHUNK_XMASK, y, z & CHUNK_ZMASK); - } - - protected ChunkRef GetChunk (int x, int y, int z) - { - x >>= CHUNK_XLOG; - z >>= CHUNK_ZLOG; - return _chunkMan.GetChunkRef(x, z); - } - - /// - /// 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. - /// - 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); - } - }*/ } diff --git a/Substrate/SubstrateCS/Source/BlockRef.cs b/Substrate/SubstrateCS/Source/BlockRef.cs index 3cae2b1..5414d81 100644 --- a/Substrate/SubstrateCS/Source/BlockRef.cs +++ b/Substrate/SubstrateCS/Source/BlockRef.cs @@ -110,204 +110,4 @@ namespace Substrate #endregion } - - /*public class BlockRef : IBlock - { - protected IBlockContainer _container; - - protected int _x; - protected int _y; - protected int _z; - - public int X - { - get { return _container.BlockGlobalX(_x); } - } - - public int Y - { - get { return _container.BlockGlobalY(_y); } - } - - public int Z - { - get { return _container.BlockGlobalZ(_z); } - } - - public int LocalX - { - get { return _container.BlockLocalX(_x); } - } - - public int LocalY - { - get { return _container.BlockLocalZ(_z); } - } - - public int LocalZ - { - get { return _z; } - } - - public BlockInfo Info - { - get { return BlockInfo.BlockTable[_container.GetBlockID(_x, _y, _z)]; } - } - - public int ID - { - get { return _container.GetBlockID(_x, _y, _z); } - set { _container.SetBlockID(_x, _y, _z, value); } - } - - public int Data - { - get { return _container.GetBlockData(_x, _y, _z); } - set { _container.SetBlockData(_x, _y, _z, value); } - } - - public int BlockLight - { - get { return _container.GetBlockLight(_x, _y, _z); } - set { _container.SetBlockLight(_x, _y, _z, value); } - } - - public int SkyLight - { - get { return _container.GetBlockSkyLight(_x, _y, _z); } - set { _container.SetBlockSkyLight(_x, _y, _z, value); } - } - - public BlockRef (IBlockContainer container, int x, int y, int z) - { - _container = container; - _x = x; - _y = y; - _z = z; - } - - public void CopyFrom (IBlock block) - { - ID = block.ID; - Data = block.Data; - BlockLight = block.BlockLight; - SkyLight = block.SkyLight; - - SetTileEntity(block.GetTileEntity().Copy()); - } - - public TileEntity GetTileEntity () - { - return _container.GetTileEntity(_x, _y, _z); - } - - public bool SetTileEntity (TileEntity te) - { - return _container.SetTileEntity(_x, _y, _z, te); - } - - public bool ClearTileEntity () - { - return _container.ClearTileEntity(_x, _y, _z); - } - }*/ - - /*public class BlockRef : IBlock - { - protected IChunk _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 (IChunk 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; - } - - public TileEntity GetTileEntity () - { - return _chunk.GetTileEntity(_lx, _ly, _lz); - } - - public bool SetTileEntity (TileEntity te) - { - return _chunk.SetTileEntity(_lx, _ly, _lz, te); - } - - public bool ClearTileEntity () - { - return _chunk.ClearTileEntity(_lx, _ly, _lz); - } - }*/ } diff --git a/Substrate/SubstrateCS/Source/World.cs b/Substrate/SubstrateCS/Source/World.cs index d77bc6b..c0bf520 100644 --- a/Substrate/SubstrateCS/Source/World.cs +++ b/Substrate/SubstrateCS/Source/World.cs @@ -7,55 +7,58 @@ namespace Substrate { using NBT; - public class World + public abstract class World { - protected RegionManager _regionMan; - protected IChunkManager _chunkMan; - protected IBlockManager _blockMan; - protected PlayerManager _playerMan; - - protected string _worldPath; - - protected Level _level; + private string _worldPath; public string WorldPath { get { return _worldPath; } } - public World (string world) + protected World (string path) { - _worldPath = world; + _worldPath = path; - if (!File.Exists(Path.Combine(_worldPath, "level.dat"))) { + if (!File.Exists(Path.Combine(WorldPath, "level.dat"))) { throw new Exception("Could not locate level.dat"); } + } + } + public abstract class NBTWorld : World + { + private Level _level; + private PlayerManager _playerMan; + + public Level Level + { + get { return _level; } + } + + public PlayerManager PlayerManager + { + get { return _playerMan; } + } + + public abstract IChunkManager ChunkManager { get; } + public abstract IBlockManager BlockManager { get; } + + protected NBTWorld (string path) + : base(path) + { if (!LoadLevel()) { throw new Exception("Failed to load level.dat"); } - if (Directory.Exists(Path.Combine(world, "region"))) { - _regionMan = new RegionManager(Path.Combine(world, "region")); - _chunkMan = new ChunkManager(_regionMan); - } - else if (Directory.Exists(Path.Combine(world, "0"))) { - _chunkMan = new ChunkFileManager(world); - } - else { - throw new Exception("Could not locate any world data"); - } - - _blockMan = new BlockManager(_chunkMan); - - if (Directory.Exists(Path.Combine(world, "players"))) { - _playerMan = new PlayerManager(Path.Combine(world, "players")); + if (Directory.Exists(Path.Combine(path, "players"))) { + _playerMan = new PlayerManager(Path.Combine(path, "players")); } } protected bool LoadLevel () { - NBTFile nf = new NBTFile(Path.Combine(_worldPath, "level.dat")); + NBTFile nf = new NBTFile(Path.Combine(WorldPath, "level.dat")); Stream nbtstr = nf.GetDataInputStream(); if (nbtstr == null) { return false; @@ -68,25 +71,92 @@ namespace Substrate return _level != null; } + } - public RegionManager GetRegionManager () + public class AlphaWorld : NBTWorld + { + private ChunkFileManager _chunkMan; + private BlockManager _blockMan; + + private string _dim; + + public AlphaWorld (string path) + : base(path) { - return _regionMan; + _chunkMan = new ChunkFileManager(path); + _blockMan = new BlockManager(_chunkMan); } - public IChunkManager GetChunkManager () + public AlphaWorld (string path, string dim) + : base(path) { - return _chunkMan; + _dim = dim; + if (_dim.Length > 0) { + path = Path.Combine(path, dim); + } + + _chunkMan = new ChunkFileManager(path); + _blockMan = new BlockManager(_chunkMan); } - public IBlockManager GetBlockManager () + public override IChunkManager ChunkManager { - return _blockMan; + get { return _chunkMan; } } - public PlayerManager GetPlayerManager () + public override IBlockManager BlockManager { - return _playerMan; + get { return _blockMan; } } } + + public class BetaWorld : NBTWorld + { + private RegionManager _regionMan; + private ChunkManager _chunkMan; + private BlockManager _blockMan; + + private string _dim; + private string _regionDir; + + public BetaWorld (string path) + : this(path, "region", "") + { + } + + public BetaWorld (string path, string region) + : this(path, region, "") + { + } + + public BetaWorld (string path, string region, string dim) + : base(path) + { + _regionDir = region; + + _dim = dim; + if (_dim.Length > 0) { + path = Path.Combine(path, dim); + } + + if (!Directory.Exists(Path.Combine(path, _regionDir))) { + throw new Exception("Could not find region directory"); + } + + _regionMan = new RegionManager(Path.Combine(path, _regionDir)); + _chunkMan = new ChunkManager(_regionMan); + _blockMan = new BlockManager(_chunkMan); + } + + public override IChunkManager ChunkManager + { + get { return _chunkMan; } + } + + public override IBlockManager BlockManager + { + get { return _blockMan; } + } + } + }