using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.IO; using Substrate.Nbt; using Substrate.Core; namespace Substrate.Core { /// /// Represents a single region containing 32x32 chunks. /// public abstract class Region : IDisposable, IRegion { protected const int XDIM = 32; protected const int ZDIM = 32; protected const int XMASK = XDIM - 1; protected const int ZMASK = ZDIM - 1; protected const int XLOG = 5; protected const int ZLOG = 5; protected int _rx; protected int _rz; private bool _disposed = false; protected RegionManager _regionMan; private static Regex _namePattern = new Regex("r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.mca$"); private WeakReference _regionFile; protected ChunkCache _cache; protected abstract IChunk CreateChunkCore (int cx, int cz); protected abstract IChunk CreateChunkVerifiedCore (NbtTree tree); protected abstract bool ParseFileNameCore (string filename, out int x, out int z); /// public int X { get { return _rx; } } /// public int Z { get { return _rz; } } /// /// Gets the length of the X-dimension of the region in chunks. /// public int XDim { get { return XDIM; } } /// /// Gets the length of the Z-dimension of the region in chunks. /// public int ZDim { get { return ZDIM; } } public abstract string GetFileName (); public abstract string GetFilePath (); /// /// Creates an instance of a for a given set of coordinates. /// /// The that should be managing this region. /// A shared cache for holding chunks. /// The global X-coordinate of the region. /// The global Z-coordinate of the region. /// The constructor will not actually open or parse any region files. Given just the region coordinates, the /// region will be able to determien the correct region file to look for based on the naming pattern for regions: /// r.x.z.mcr, given x and z are integers representing the region's coordinates. /// Regions require a to be provided because they do not actually store any chunks or references /// to chunks on their own. This allows regions to easily pass off requests outside of their bounds, if necessary. public Region (RegionManager rm, ChunkCache cache, int rx, int rz) { _regionMan = rm; _cache = cache; _regionFile = new WeakReference(null); _rx = rx; _rz = rz; if (!File.Exists(GetFilePath())) { throw new FileNotFoundException(); } } /// /// Creates an instance of a for the given region file. /// /// The that should be managing this region. /// A shared cache for holding chunks. /// The region file to derive the region from. /// The constructor will not actually open or parse the region file. It will only read the file's name in order /// to derive the region's coordinates, based on a strict naming pattern for regions: r.x.z.mcr, given x and z are integers /// representing the region's coordinates. /// Regions require a to be provided because they do not actually store any chunks or references /// to chunks on their own. This allows regions to easily pass off requests outside of their bounds, if necessary. public Region (RegionManager rm, ChunkCache cache, string filename) { _regionMan = rm; _cache = cache; _regionFile = new WeakReference(null); ParseFileNameCore(filename, out _rx, out _rz); if (!File.Exists(Path.Combine(_regionMan.GetRegionPath(), filename))) { throw new FileNotFoundException(); } } /// /// Region finalizer that ensures any resources are cleaned up /// ~Region () { Dispose(false); } /// /// Disposes any managed and unmanaged resources held by the region. /// public void Dispose () { Dispose(true); System.GC.SuppressFinalize(this); } /// /// Conditionally dispose managed or unmanaged resources. /// /// True if the call to Dispose was explicit. protected virtual void Dispose (bool disposing) { if (!_disposed) { if (disposing) { // Cleanup managed resources RegionFile rf = _regionFile.Target as RegionFile; if (rf != null) { rf.Dispose(); rf = null; } } // Cleanup unmanaged resources } _disposed = true; } private RegionFile GetRegionFile () { RegionFile rf = _regionFile.Target as RegionFile; if (rf == null) { rf = new RegionFile(GetFilePath()); _regionFile.Target = rf; } return rf; } /// public NbtTree GetChunkTree (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? null : alt.GetChunkTree(ForeignX(lcx), ForeignZ(lcz)); } RegionFile rf = GetRegionFile(); Stream nbtstr = rf.GetChunkDataInputStream(lcx, lcz); if (nbtstr == null) { return null; } NbtTree tree = new NbtTree(nbtstr); nbtstr.Close(); return tree; } // XXX: Exceptions /// public bool SaveChunkTree (int lcx, int lcz, NbtTree tree) { return SaveChunkTree(lcx, lcz, tree, null); } /// public bool SaveChunkTree (int lcx, int lcz, NbtTree tree, int timestamp) { return SaveChunkTree(lcx, lcz, tree, timestamp); } private bool SaveChunkTree (int lcx, int lcz, NbtTree tree, int? timestamp) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? false : alt.SaveChunkTree(ForeignX(lcx), ForeignZ(lcz), tree); } RegionFile rf = GetRegionFile(); Stream zipstr = (timestamp == null) ? rf.GetChunkDataOutputStream(lcx, lcz) : rf.GetChunkDataOutputStream(lcx, lcz, (int)timestamp); if (zipstr == null) { return false; } tree.WriteTo(zipstr); zipstr.Close(); return true; } /// public Stream GetChunkOutStream (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? null : alt.GetChunkOutStream(ForeignX(lcx), ForeignZ(lcz)); } RegionFile rf = GetRegionFile(); return rf.GetChunkDataOutputStream(lcx, lcz); } /// public int ChunkCount () { RegionFile rf = GetRegionFile(); int count = 0; for (int x = 0; x < XDIM; x++) { for (int z = 0; z < ZDIM; z++) { if (rf.HasChunk(x, z)) { count++; } } } return count; } // XXX: Consider revising foreign lookup support /// public ChunkRef GetChunkRef (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? null : alt.GetChunkRef(ForeignX(lcx), ForeignZ(lcz)); } int cx = lcx + _rx * XDIM; int cz = lcz + _rz * ZDIM; ChunkKey k = new ChunkKey(cx, cz); ChunkRef c = _cache.Fetch(k); if (c != null) { return c; } c = ChunkRef.Create(this, lcx, lcz); if (c != null) { _cache.Insert(c); } return c; } /// public ChunkRef CreateChunk (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? null : alt.CreateChunk(ForeignX(lcx), ForeignZ(lcz)); } DeleteChunk(lcx, lcz); int cx = lcx + _rx * XDIM; int cz = lcz + _rz * ZDIM; AlphaChunk c = AlphaChunk.Create(cx, cz); c.Save(GetChunkOutStream(lcx, lcz)); ChunkRef cr = ChunkRef.Create(this, lcx, lcz); _cache.Insert(cr); return cr; } #region IChunkCollection Members // XXX: This also feels dirty. /// /// Gets the global X-coordinate of a chunk given an internal coordinate handed out by a container. /// /// An internal X-coordinate given to a by any instance of a container. /// The global X-coordinate of the corresponding chunk. public int ChunkGlobalX (int cx) { return _rx * XDIM + cx; } /// /// Gets the global Z-coordinate of a chunk given an internal coordinate handed out by a container. /// /// An internal Z-coordinate given to a by any instance of a container. /// The global Z-coordinate of the corresponding chunk. public int ChunkGlobalZ (int cz) { return _rz * ZDIM + cz; } /// /// Gets the region-local X-coordinate of a chunk given an internal coordinate handed out by a container. /// /// An internal X-coordinate given to a by any instance of a container. /// The region-local X-coordinate of the corresponding chunk. public int ChunkLocalX (int cx) { return cx; } /// /// Gets the region-local Z-coordinate of a chunk given an internal coordinate handed out by a container. /// /// An internal Z-coordinate given to a by any instance of a container. /// The region-local Z-coordinate of the corresponding chunk. public int ChunkLocalZ (int cz) { return cz; } /// /// Returns a given local coordinates relative to this region. /// /// The local X-coordinate of a chunk relative to this region. /// The local Z-coordinate of a chunk relative to this region. /// A object for the given coordinates, or null if the chunk does not exist. /// If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region /// transparently. The returned object may either come from cache, or be regenerated from disk. public IChunk GetChunk (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? null : alt.GetChunk(ForeignX(lcx), ForeignZ(lcz)); } if (!ChunkExists(lcx, lcz)) { return null; } return CreateChunkVerifiedCore(GetChunkTree(lcx, lcz)); } /// /// Checks if a chunk exists at the given local coordinates relative to this region. /// /// The local X-coordinate of a chunk relative to this region. /// The local Z-coordinate of a chunk relative to this region. /// True if there is a chunk at the given coordinates; false otherwise. /// If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region /// transparently. public bool ChunkExists (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? false : alt.ChunkExists(ForeignX(lcx), ForeignZ(lcz)); } RegionFile rf = GetRegionFile(); return rf.HasChunk(lcx, lcz); } /// /// Deletes a chunk from the underlying data store at the given local coordinates relative to this region. /// /// The local X-coordinate of a chunk relative to this region. /// The local Z-coordinate of a chunk relative to this region. /// True if there is a chunk was deleted; false otherwise. /// If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region /// transparently. public bool DeleteChunk (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? false : alt.DeleteChunk(ForeignX(lcx), ForeignZ(lcz)); } RegionFile rf = GetRegionFile(); if (!rf.HasChunk(lcx, lcz)) { return false; } rf.DeleteChunk(lcx, lcz); ChunkKey k = new ChunkKey(ChunkGlobalX(lcx), ChunkGlobalZ(lcz)); _cache.Remove(k); if (ChunkCount() == 0) { _regionMan.DeleteRegion(X, Z); _regionFile.Target = null; } return true; } /// /// Saves an existing to the region at the given local coordinates. /// /// The local X-coordinate of a chunk relative to this region. /// The local Z-coordinate of a chunk relative to this region. /// A to save to the given location. /// A represneting the at its new location. /// If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region /// transparently. The 's internal global coordinates will be updated to reflect the new location. public ChunkRef SetChunk (int lcx, int lcz, IChunk chunk) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? null : alt.CreateChunk(ForeignX(lcx), ForeignZ(lcz)); } DeleteChunk(lcx, lcz); int cx = lcx + _rx * XDIM; int cz = lcz + _rz * ZDIM; chunk.SetLocation(cx, cz); chunk.Save(GetChunkOutStream(lcx, lcz)); ChunkRef cr = ChunkRef.Create(this, lcx, lcz); _cache.Insert(cr); return cr; } /// /// Saves all chunks within this region that have been marked as dirty. /// /// The number of chunks that were saved. public int Save () { _cache.SyncDirty(); int saved = 0; IEnumerator en = _cache.GetDirtyEnumerator(); while (en.MoveNext()) { ChunkRef chunk = en.Current; if (!ChunkExists(chunk.LocalX, chunk.LocalZ)) { throw new MissingChunkException(); } if (chunk.Save(GetChunkOutStream(chunk.LocalX, chunk.LocalZ))) { saved++; } } _cache.ClearDirty(); return saved; } // XXX: Allows a chunk not part of this region to be saved to it /// public bool SaveChunk (IChunk chunk) { //Console.WriteLine("Region[{0}, {1}].Save({2}, {3})", _rx, _rz, ForeignX(chunk.X),ForeignZ(chunk.Z)); return chunk.Save(GetChunkOutStream(ForeignX(chunk.X), ForeignZ(chunk.Z))); } /// /// Checks if this container supports delegating an action on out-of-bounds coordinates to another container. /// public bool CanDelegateCoordinates { get { return true; } } /// public int GetChunkTimestamp (int lcx, int lcz) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); return (alt == null) ? 0 : alt.GetChunkTimestamp(ForeignX(lcx), ForeignZ(lcz)); } RegionFile rf = GetRegionFile(); return rf.GetTimestamp(lcx, lcz); } /// public void SetChunkTimestamp (int lcx, int lcz, int timestamp) { if (!LocalBoundsCheck(lcx, lcz)) { IRegion alt = GetForeignRegion(lcx, lcz); if (alt != null) alt.SetChunkTimestamp(ForeignX(lcx), ForeignZ(lcz), timestamp); } RegionFile rf = GetRegionFile(); rf.SetTimestamp(lcx, lcz, timestamp); } #endregion protected bool LocalBoundsCheck (int lcx, int lcz) { return (lcx >= 0 && lcx < XDIM && lcz >= 0 && lcz < ZDIM); } protected IRegion GetForeignRegion (int lcx, int lcz) { return _regionMan.GetRegion(_rx + (lcx >> XLOG), _rz + (lcz >> ZLOG)); } protected int ForeignX (int lcx) { return (lcx + XDIM * 10000) & XMASK; } protected int ForeignZ (int lcz) { return (lcz + ZDIM * 10000) & ZMASK; } } }