diff --git a/NBToolkit/FilteredChunkManager.cs b/NBToolkit/FilteredChunkManager.cs index 0fe81ca..e75d184 100644 --- a/NBToolkit/FilteredChunkManager.cs +++ b/NBToolkit/FilteredChunkManager.cs @@ -189,6 +189,11 @@ namespace NBToolkit return _cm.GetChunkRef(cx, cz); } + public ChunkRef CreateChunk (int cx, int cz) + { + return _cm.CreateChunk(cx, cz); + } + public bool ChunkExists (int cx, int cz) { return _cm.ChunkExists(cx, cz); diff --git a/Substrate/SubstrateCS/Source/ChunkFileManager.cs b/Substrate/SubstrateCS/Source/ChunkFileManager.cs index 48a8bbf..bb412d8 100644 --- a/Substrate/SubstrateCS/Source/ChunkFileManager.cs +++ b/Substrate/SubstrateCS/Source/ChunkFileManager.cs @@ -123,6 +123,19 @@ namespace Substrate } } + public ChunkRef CreateChunk (int cx, int cz) + { + DeleteChunk(cx, cz); + Chunk c = new Chunk(cx, cz); + c.Save(GetChunkOutStream(cx, cz)); + + ChunkRef cr = new ChunkRef(this, this, cx, cz); + ChunkKey k = new ChunkKey(cx, cz); + _cache[k] = new WeakReference(cr); + + return cr; + } + public bool ChunkExists (int cx, int cz) { return new ChunkFile(_mapPath, cx, cz).Exists(); diff --git a/Substrate/SubstrateCS/Source/ChunkInterface.cs b/Substrate/SubstrateCS/Source/ChunkInterface.cs index db10dc6..f053502 100644 --- a/Substrate/SubstrateCS/Source/ChunkInterface.cs +++ b/Substrate/SubstrateCS/Source/ChunkInterface.cs @@ -39,6 +39,7 @@ namespace Substrate Chunk GetChunk (int cx, int cz); ChunkRef GetChunkRef (int cx, int cz); + ChunkRef CreateChunk (int cx, int cz); bool ChunkExists (int cx, int cz); diff --git a/Substrate/SubstrateCS/Source/ChunkManager.cs b/Substrate/SubstrateCS/Source/ChunkManager.cs index be4c243..2157f54 100644 --- a/Substrate/SubstrateCS/Source/ChunkManager.cs +++ b/Substrate/SubstrateCS/Source/ChunkManager.cs @@ -85,6 +85,18 @@ namespace Substrate return r.ChunkExists(cx & REGION_XMASK, cz & REGION_ZMASK); } + public ChunkRef CreateChunk (int cx, int cz) + { + Region r = GetRegion(cx, cz); + if (r == null) { + int rx = cx >> REGION_XLOG; + int rz = cz >> REGION_ZLOG; + r = _regionMan.CreateRegion(rx, rz); + } + + return r.CreateChunk(cx & REGION_XMASK, cz & REGION_ZMASK, this); + } + public bool MarkChunkDirty (ChunkRef chunk) { Region r = GetRegion(chunk.X, chunk.Z); diff --git a/Substrate/SubstrateCS/Source/Region.cs b/Substrate/SubstrateCS/Source/Region.cs index 3a14891..4c6c20b 100644 --- a/Substrate/SubstrateCS/Source/Region.cs +++ b/Substrate/SubstrateCS/Source/Region.cs @@ -213,6 +213,25 @@ namespace Substrate } } + public ChunkRef CreateChunk (int lcx, int lcz) + { + return CreateChunk(lcx, lcz, this); + } + + public ChunkRef CreateChunk (int lcx, int lcz, IChunkCache cache) + { + DeleteChunk(lcx, lcz); + + Chunk c = new Chunk(ChunkGlobalX(lcx), ChunkGlobalZ(lcz)); + c.Save(GetChunkOutStream(lcx, lcz)); + + ChunkRef cr = new ChunkRef(this, cache, lcx, lcz); + ChunkKey k = new ChunkKey(lcx, lcz); + _cache[k] = new WeakReference(cr); + + return cr; + } + #region IChunkCollection Members diff --git a/Substrate/SubstrateCS/Source/RegionFile.cs b/Substrate/SubstrateCS/Source/RegionFile.cs index 24d7d76..758eed1 100644 --- a/Substrate/SubstrateCS/Source/RegionFile.cs +++ b/Substrate/SubstrateCS/Source/RegionFile.cs @@ -71,7 +71,7 @@ namespace Substrate protected void ReadFile () { // Get last udpate time - long newModified = 0; + long newModified = -1; try { if (File.Exists(fileName)) { newModified = Timestamp(File.GetLastWriteTime(fileName)); diff --git a/Substrate/SubstrateCS/Source/RegionManager.cs b/Substrate/SubstrateCS/Source/RegionManager.cs index 912af10..4f173d3 100644 --- a/Substrate/SubstrateCS/Source/RegionManager.cs +++ b/Substrate/SubstrateCS/Source/RegionManager.cs @@ -5,7 +5,17 @@ using System.Collections; namespace Substrate { - public class RegionManager : IEnumerable + public interface IRegionContainer + { + bool RegionExists (int rx, int rz); + + Region GetRegion (int rx, int rz); + Region CreateRegion (int rx, int rz); + + bool DeleteRegion (int rx, int rz); + } + + public class RegionManager : IRegionContainer, IEnumerable { protected string _regionPath; @@ -35,6 +45,26 @@ namespace Substrate } } + public bool RegionExists (int rx, int rz) + { + Region r = GetRegion(rx, rz); + return r != null; + } + + public Region CreateRegion (int rx, int rz) + { + Region r = GetRegion(rx, rz); + if (r == null) { + string fp = "r." + rx + "." + rz + ".mcr"; + new RegionFile(Path.Combine(_regionPath, fp)); + + r = new Region(this, rx, rz); + } + + return r; + } + + public Region GetRegion (string filename) { int rx, rz; @@ -45,6 +75,7 @@ namespace Substrate return GetRegion(rx, rz); } + public string GetRegionPath () { return _regionPath; diff --git a/Substrate/SubstrateCS/Source/World.cs b/Substrate/SubstrateCS/Source/World.cs index c0bf520..1fa476b 100644 --- a/Substrate/SubstrateCS/Source/World.cs +++ b/Substrate/SubstrateCS/Source/World.cs @@ -119,6 +119,11 @@ namespace Substrate private string _dim; private string _regionDir; + public RegionManager RegionManager + { + get { return _regionMan; } + } + public BetaWorld (string path) : this(path, "region", "") {