forked from mirrors/NBTExplorer
Organizational
This commit is contained in:
parent
63e0bc1876
commit
66dfc9da95
8 changed files with 1591 additions and 1601 deletions
82
SubstrateCS/Source/AnvilRegion.cs
Normal file
82
SubstrateCS/Source/AnvilRegion.cs
Normal file
|
@ -0,0 +1,82 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using Substrate.Core;
|
||||
using Substrate.Nbt;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
public class AnvilRegion : Region
|
||||
{
|
||||
private static Regex _namePattern = new Regex("r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.mcr$");
|
||||
|
||||
public AnvilRegion (AnvilRegionManager rm, ChunkCache cache, int rx, int rz)
|
||||
: base(rm, cache, rx, rz)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public override string GetFileName ()
|
||||
{
|
||||
return "r." + _rx + "." + _rz + ".mcr";
|
||||
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public override string GetFilePath ()
|
||||
{
|
||||
return System.IO.Path.Combine(_regionMan.GetRegionPath(), GetFileName());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if the given filename conforms to the general naming pattern for any region.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename to test.</param>
|
||||
/// <returns>True if the filename is a valid region name; false if it does not conform to the pattern.</returns>
|
||||
public static bool TestFileName (string filename)
|
||||
{
|
||||
Match match = _namePattern.Match(filename);
|
||||
if (!match.Success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool ParseFileName (string filename, out int x, out int z)
|
||||
{
|
||||
x = 0;
|
||||
z = 0;
|
||||
|
||||
Match match = _namePattern.Match(filename);
|
||||
if (!match.Success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
x = Convert.ToInt32(match.Groups[1].Value);
|
||||
z = Convert.ToInt32(match.Groups[2].Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the given filename to extract encoded region coordinates.
|
||||
/// </summary>
|
||||
/// <param name="filename">The region filename to parse.</param>
|
||||
/// <param name="x">This parameter will contain the X-coordinate of a region.</param>
|
||||
/// <param name="z">This parameter will contain the Z-coordinate of a region.</param>
|
||||
/// <returns>True if the filename could be correctly parse; false otherwise.</returns>
|
||||
protected override bool ParseFileNameCore (string filename, out int x, out int z)
|
||||
{
|
||||
return ParseFileName(filename, out x, out z);
|
||||
}
|
||||
|
||||
protected override IChunk CreateChunkCore (int cx, int cz)
|
||||
{
|
||||
return AlphaChunk.Create(cz, cz);
|
||||
}
|
||||
|
||||
protected override IChunk CreateChunkVerifiedCore (NbtTree tree)
|
||||
{
|
||||
return AlphaChunk.CreateVerified(tree);
|
||||
}
|
||||
}
|
||||
}
|
43
SubstrateCS/Source/AnvilRegionManager.cs
Normal file
43
SubstrateCS/Source/AnvilRegionManager.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Substrate.Core;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
public class AnvilRegionManager : RegionManager
|
||||
{
|
||||
public AnvilRegionManager (string regionDir, ChunkCache cache)
|
||||
: base(regionDir, cache)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IRegion CreateRegionCore (int rx, int rz)
|
||||
{
|
||||
return new AnvilRegion(this, _chunkCache, rx, rz);
|
||||
}
|
||||
|
||||
protected override RegionFile CreateRegionFileCore (int rx, int rz)
|
||||
{
|
||||
string fp = "r." + rx + "." + rz + ".mca";
|
||||
return new RegionFile(Path.Combine(_regionPath, fp));
|
||||
}
|
||||
|
||||
protected override void DeleteRegionCore (IRegion region)
|
||||
{
|
||||
AnvilRegion r = region as AnvilRegion;
|
||||
if (r != null) {
|
||||
r.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override IRegion GetRegion (string filename)
|
||||
{
|
||||
int rx, rz;
|
||||
if (!AnvilRegion.ParseFileName(filename, out rx, out rz)) {
|
||||
throw new ArgumentException("Malformed region file name: " + filename, "filename");
|
||||
}
|
||||
|
||||
return GetRegion(rx, rz);
|
||||
}
|
||||
}
|
||||
}
|
82
SubstrateCS/Source/BetaRegion.cs
Normal file
82
SubstrateCS/Source/BetaRegion.cs
Normal file
|
@ -0,0 +1,82 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using Substrate.Core;
|
||||
using Substrate.Nbt;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
public class BetaRegion : Region
|
||||
{
|
||||
private static Regex _namePattern = new Regex("r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.mcr$");
|
||||
|
||||
public BetaRegion (BetaRegionManager rm, ChunkCache cache, int rx, int rz)
|
||||
: base(rm, cache, rx, rz)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public override string GetFileName ()
|
||||
{
|
||||
return "r." + _rx + "." + _rz + ".mcr";
|
||||
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public override string GetFilePath ()
|
||||
{
|
||||
return System.IO.Path.Combine(_regionMan.GetRegionPath(), GetFileName());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests if the given filename conforms to the general naming pattern for any region.
|
||||
/// </summary>
|
||||
/// <param name="filename">The filename to test.</param>
|
||||
/// <returns>True if the filename is a valid region name; false if it does not conform to the pattern.</returns>
|
||||
public static bool TestFileName (string filename)
|
||||
{
|
||||
Match match = _namePattern.Match(filename);
|
||||
if (!match.Success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool ParseFileName (string filename, out int x, out int z)
|
||||
{
|
||||
x = 0;
|
||||
z = 0;
|
||||
|
||||
Match match = _namePattern.Match(filename);
|
||||
if (!match.Success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
x = Convert.ToInt32(match.Groups[1].Value);
|
||||
z = Convert.ToInt32(match.Groups[2].Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the given filename to extract encoded region coordinates.
|
||||
/// </summary>
|
||||
/// <param name="filename">The region filename to parse.</param>
|
||||
/// <param name="x">This parameter will contain the X-coordinate of a region.</param>
|
||||
/// <param name="z">This parameter will contain the Z-coordinate of a region.</param>
|
||||
/// <returns>True if the filename could be correctly parse; false otherwise.</returns>
|
||||
protected override bool ParseFileNameCore (string filename, out int x, out int z)
|
||||
{
|
||||
return ParseFileName(filename, out x, out z);
|
||||
}
|
||||
|
||||
protected override IChunk CreateChunkCore (int cx, int cz)
|
||||
{
|
||||
return AlphaChunk.Create(cz, cz);
|
||||
}
|
||||
|
||||
protected override IChunk CreateChunkVerifiedCore (NbtTree tree)
|
||||
{
|
||||
return AlphaChunk.CreateVerified(tree);
|
||||
}
|
||||
}
|
||||
}
|
43
SubstrateCS/Source/BetaRegionManager.cs
Normal file
43
SubstrateCS/Source/BetaRegionManager.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Substrate.Core;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
public class BetaRegionManager : RegionManager
|
||||
{
|
||||
public BetaRegionManager (string regionDir, ChunkCache cache)
|
||||
: base(regionDir, cache)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IRegion CreateRegionCore (int rx, int rz)
|
||||
{
|
||||
return new BetaRegion(this, _chunkCache, rx, rz);
|
||||
}
|
||||
|
||||
protected override RegionFile CreateRegionFileCore (int rx, int rz)
|
||||
{
|
||||
string fp = "r." + rx + "." + rz + ".mcr";
|
||||
return new RegionFile(Path.Combine(_regionPath, fp));
|
||||
}
|
||||
|
||||
protected override void DeleteRegionCore (IRegion region)
|
||||
{
|
||||
BetaRegion r = region as BetaRegion;
|
||||
if (r != null) {
|
||||
r.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override IRegion GetRegion (string filename)
|
||||
{
|
||||
int rx, rz;
|
||||
if (!BetaRegion.ParseFileName(filename, out rx, out rz)) {
|
||||
throw new ArgumentException("Malformed region file name: " + filename, "filename");
|
||||
}
|
||||
|
||||
return GetRegion(rx, rz);
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -1,9 +1,120 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Substrate.Nbt;
|
||||
|
||||
namespace Substrate.Core
|
||||
{
|
||||
public interface IRegion : IChunkContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the global X-coordinate of the region.
|
||||
/// </summary>
|
||||
int X { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the global Z-coordinate of the region.
|
||||
/// </summary>
|
||||
int Z { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the appropriate filename for this region.
|
||||
/// </summary>
|
||||
/// <returns>The filename of the region with encoded coordinates.</returns>
|
||||
string GetFileName ();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full path of the region's backing file.
|
||||
/// </summary>
|
||||
/// <returns>Gets the path of the region's file based on the <see cref="IRegionManager"/>'s region path and the region's on filename.</returns>
|
||||
string GetFilePath ();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="NbtTree"/> for a chunk given local coordinates into the region.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of a chunk within the region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of a chunk within the region.</param>
|
||||
/// <returns>An <see cref="NbtTree"/> for a local chunk, or null if there is no chunk at the given coordinates.</returns>
|
||||
NbtTree GetChunkTree (int lcx, int lcz);
|
||||
|
||||
/// <summary>
|
||||
/// Saves an <see cref="NbtTree"/> for a chunk back to the region's data store at the given local coordinates.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of the chunk within the region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of the chunk within the region.</param>
|
||||
/// <param name="tree">The <see cref="NbtTree"/> of a chunk to write back to the region.</param>
|
||||
/// <returns>True if the save succeeded.</returns>
|
||||
/// <remarks>It is up to the programmer to ensure that the global coordinates defined within the chunk's tree
|
||||
/// are consistent with the local coordinates of the region being written into.</remarks>
|
||||
bool SaveChunkTree (int lcx, int lcz, NbtTree tree);
|
||||
|
||||
/// <summary>
|
||||
/// Saves an <see cref="NbtTree"/> for a chunk back to the region's data store at the given local coordinates and with the given timestamp.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of the chunk within the region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of the chunk within the region.</param>
|
||||
/// <param name="tree">The <see cref="NbtTree"/> of a chunk to write back to the region.</param>
|
||||
/// <param name="timestamp">The timestamp to write to the underlying region file for this chunk.</param>
|
||||
/// <returns>True if the save succeeded.</returns>
|
||||
/// <remarks>It is up to the programmer to ensure that the global coordinates defined within the chunk's tree
|
||||
/// are consistent with the local coordinates of the region being written into.</remarks>
|
||||
bool SaveChunkTree (int lcx, int lcz, NbtTree tree, int timestamp);
|
||||
|
||||
/// <summary>
|
||||
/// Gets an output stream for replacing chunk data at the given coordinates within the region.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of the chunk to replace within the region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of the chunk to replace within the region.</param>
|
||||
/// <returns>An output stream that can be written to on demand.</returns>
|
||||
/// <remarks>There is no guarantee that any data will be saved until the stream is closed.</remarks>
|
||||
Stream GetChunkOutStream (int lcx, int lcz);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the count of valid chunks stored in this region.
|
||||
/// </summary>
|
||||
/// <returns>The count of currently stored chunks.</returns>
|
||||
int ChunkCount ();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="ChunkRef"/> for a chunk at the given local coordinates relative to this region.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
|
||||
/// <returns>A <see cref="ChunkRef"/> at the given local coordinates, or null if no chunk exists.</returns>
|
||||
/// <remarks>The local coordinates do not strictly need to be within the bounds of the region. If coordinates are detected
|
||||
/// as being out of bounds, the lookup will be delegated to the correct region and the lookup will be performed there
|
||||
/// instead. This allows any <see cref="Region"/> to perform a similar task to <see cref="RegionChunkManager"/>, but with a
|
||||
/// region-local frame of reference instead of a global frame of reference.</remarks>
|
||||
ChunkRef GetChunkRef (int lcx, int lcz);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new chunk at the given local coordinates relative to this region and returns a new <see cref="ChunkRef"/> for it.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
|
||||
/// <returns>A <see cref="ChunkRef"/> for the newly created chunk.</returns>
|
||||
/// <remarks>If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region
|
||||
/// transparently.</remarks>
|
||||
ChunkRef CreateChunk (int lcx, int lcz);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the timestamp of a chunk from the underlying region file.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
|
||||
/// <returns>The timestamp of the chunk slot in the region.</returns>
|
||||
/// <remarks>The value returned may differ from any timestamp stored in the chunk data itself.</remarks>
|
||||
int GetChunkTimestamp (int lcx, int lcz);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the timestamp of a chunk in the underlying region file.
|
||||
/// </summary>
|
||||
/// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
|
||||
/// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
|
||||
/// <param name="timestamp">The new timestamp value.</param>
|
||||
/// <remarks>This function will only update the timestamp of the chunk slot in the underlying region file. It will not update
|
||||
/// any timestamp information in the chunk data itself.</remarks>
|
||||
void SetChunkTimestamp (int lcx, int lcz, int timestamp);
|
||||
}
|
||||
|
||||
public interface IRegionContainer
|
||||
{
|
||||
|
|
|
@ -1,298 +1,219 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Substrate.Core;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
public class BetaRegionManager : RegionManager
|
||||
{
|
||||
public BetaRegionManager (string regionDir, ChunkCache cache)
|
||||
: base(regionDir, cache)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IRegion CreateRegionCore (int rx, int rz)
|
||||
{
|
||||
return new BetaRegion(this, _chunkCache, rx, rz);
|
||||
}
|
||||
|
||||
protected override RegionFile CreateRegionFileCore (int rx, int rz)
|
||||
{
|
||||
string fp = "r." + rx + "." + rz + ".mcr";
|
||||
return new RegionFile(Path.Combine(_regionPath, fp));
|
||||
}
|
||||
|
||||
protected override void DeleteRegionCore (IRegion region)
|
||||
{
|
||||
BetaRegion r = region as BetaRegion;
|
||||
if (r != null) {
|
||||
r.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override IRegion GetRegion (string filename)
|
||||
{
|
||||
int rx, rz;
|
||||
if (!BetaRegion.ParseFileName(filename, out rx, out rz)) {
|
||||
throw new ArgumentException("Malformed region file name: " + filename, "filename");
|
||||
}
|
||||
|
||||
return GetRegion(rx, rz);
|
||||
}
|
||||
}
|
||||
|
||||
public class AnvilRegionManager : RegionManager
|
||||
{
|
||||
public AnvilRegionManager (string regionDir, ChunkCache cache)
|
||||
: base(regionDir, cache)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IRegion CreateRegionCore (int rx, int rz)
|
||||
{
|
||||
return new AnvilRegion(this, _chunkCache, rx, rz);
|
||||
}
|
||||
|
||||
protected override RegionFile CreateRegionFileCore (int rx, int rz)
|
||||
{
|
||||
string fp = "r." + rx + "." + rz + ".mca";
|
||||
return new RegionFile(Path.Combine(_regionPath, fp));
|
||||
}
|
||||
|
||||
protected override void DeleteRegionCore (IRegion region)
|
||||
{
|
||||
AnvilRegion r = region as AnvilRegion;
|
||||
if (r != null) {
|
||||
r.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override IRegion GetRegion (string filename)
|
||||
{
|
||||
int rx, rz;
|
||||
if (!AnvilRegion.ParseFileName(filename, out rx, out rz)) {
|
||||
throw new ArgumentException("Malformed region file name: " + filename, "filename");
|
||||
}
|
||||
|
||||
return GetRegion(rx, rz);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manages the regions of a Beta-compatible world.
|
||||
/// </summary>
|
||||
public abstract class RegionManager : IRegionManager
|
||||
{
|
||||
protected string _regionPath;
|
||||
|
||||
protected Dictionary<RegionKey, IRegion> _cache;
|
||||
|
||||
protected ChunkCache _chunkCache;
|
||||
|
||||
|
||||
protected abstract IRegion CreateRegionCore (int rx, int rz);
|
||||
|
||||
protected abstract RegionFile CreateRegionFileCore (int rx, int rz);
|
||||
|
||||
protected abstract void DeleteRegionCore (IRegion region);
|
||||
|
||||
public abstract IRegion GetRegion (string filename);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of a <see cref="RegionManager"/> for the given region directory and chunk cache.
|
||||
/// </summary>
|
||||
/// <param name="regionDir">The path to a directory containing region files.</param>
|
||||
/// <param name="cache">The shared chunk cache to hold chunk data in.</param>
|
||||
public RegionManager (string regionDir, ChunkCache cache)
|
||||
{
|
||||
_regionPath = regionDir;
|
||||
_chunkCache = cache;
|
||||
_cache = new Dictionary<RegionKey, IRegion>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Region"/> at the given coordinates.
|
||||
/// </summary>
|
||||
/// <param name="rx">The global X-coordinate of a region.</param>
|
||||
/// <param name="rz">The global Z-coordinate of a region.</param>
|
||||
/// <returns>A <see cref="Region"/> representing a region at the given coordinates, or null if the region does not exist.</returns>
|
||||
public IRegion GetRegion (int rx, int rz)
|
||||
{
|
||||
RegionKey k = new RegionKey(rx, rz);
|
||||
IRegion r;
|
||||
|
||||
try {
|
||||
if (_cache.TryGetValue(k, out r) == false) {
|
||||
r = CreateRegionCore(rz, rz);
|
||||
_cache.Add(k, r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
catch (FileNotFoundException) {
|
||||
_cache.Add(k, null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public bool RegionExists (int rx, int rz)
|
||||
{
|
||||
IRegion r = GetRegion(rx, rz);
|
||||
return r != null;
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public IRegion CreateRegion (int rx, int rz)
|
||||
{
|
||||
IRegion r = GetRegion(rx, rz);
|
||||
if (r == null) {
|
||||
string fp = "r." + rx + "." + rz + ".mca";
|
||||
using (RegionFile rf = CreateRegionFileCore(rx, rz)) {
|
||||
|
||||
}
|
||||
|
||||
r = CreateRegionCore(rx, rz);
|
||||
|
||||
RegionKey k = new RegionKey(rx, rz);
|
||||
_cache[k] = r;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current region directory path.
|
||||
/// </summary>
|
||||
/// <returns>The path to the region directory.</returns>
|
||||
public string GetRegionPath ()
|
||||
{
|
||||
return _regionPath;
|
||||
}
|
||||
|
||||
// XXX: Exceptions
|
||||
/// <inherits />
|
||||
public bool DeleteRegion (int rx, int rz)
|
||||
{
|
||||
IRegion r = GetRegion(rx, rz);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RegionKey k = new RegionKey(rx, rz);
|
||||
_cache.Remove(k);
|
||||
|
||||
DeleteRegionCore(r);
|
||||
|
||||
try {
|
||||
File.Delete(r.GetFilePath());
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("NOTICE: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#region IEnumerable<IRegion> Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates over all of the regions in the underlying dimension.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator instance.</returns>
|
||||
public IEnumerator<IRegion> GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates over all of the regions in the underlying dimension.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator instance.</returns>
|
||||
IEnumerator IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private struct Enumerator : IEnumerator<IRegion>
|
||||
{
|
||||
private List<IRegion> _regions;
|
||||
private int _pos;
|
||||
|
||||
public Enumerator (RegionManager rm)
|
||||
{
|
||||
_regions = new List<IRegion>();
|
||||
_pos = -1;
|
||||
|
||||
if (!Directory.Exists(rm.GetRegionPath())) {
|
||||
throw new DirectoryNotFoundException();
|
||||
}
|
||||
|
||||
string[] files = Directory.GetFiles(rm.GetRegionPath());
|
||||
_regions.Capacity = files.Length;
|
||||
|
||||
foreach (string file in files) {
|
||||
try {
|
||||
IRegion r = rm.GetRegion(file);
|
||||
_regions.Add(r);
|
||||
}
|
||||
catch (ArgumentException) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool MoveNext ()
|
||||
{
|
||||
_pos++;
|
||||
return (_pos < _regions.Count);
|
||||
}
|
||||
|
||||
public void Reset ()
|
||||
{
|
||||
_pos = -1;
|
||||
}
|
||||
|
||||
void IDisposable.Dispose () { }
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
IRegion IEnumerator<IRegion>.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public IRegion Current
|
||||
{
|
||||
get
|
||||
{
|
||||
try {
|
||||
return _regions[_pos];
|
||||
}
|
||||
catch (IndexOutOfRangeException) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Substrate.Core;
|
||||
|
||||
namespace Substrate.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the regions of a Beta-compatible world.
|
||||
/// </summary>
|
||||
public abstract class RegionManager : IRegionManager
|
||||
{
|
||||
protected string _regionPath;
|
||||
|
||||
protected Dictionary<RegionKey, IRegion> _cache;
|
||||
|
||||
protected ChunkCache _chunkCache;
|
||||
|
||||
|
||||
protected abstract IRegion CreateRegionCore (int rx, int rz);
|
||||
|
||||
protected abstract RegionFile CreateRegionFileCore (int rx, int rz);
|
||||
|
||||
protected abstract void DeleteRegionCore (IRegion region);
|
||||
|
||||
public abstract IRegion GetRegion (string filename);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of a <see cref="RegionManager"/> for the given region directory and chunk cache.
|
||||
/// </summary>
|
||||
/// <param name="regionDir">The path to a directory containing region files.</param>
|
||||
/// <param name="cache">The shared chunk cache to hold chunk data in.</param>
|
||||
public RegionManager (string regionDir, ChunkCache cache)
|
||||
{
|
||||
_regionPath = regionDir;
|
||||
_chunkCache = cache;
|
||||
_cache = new Dictionary<RegionKey, IRegion>();
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public IRegion GetRegion (int rx, int rz)
|
||||
{
|
||||
RegionKey k = new RegionKey(rx, rz);
|
||||
IRegion r;
|
||||
|
||||
try {
|
||||
if (_cache.TryGetValue(k, out r) == false) {
|
||||
r = CreateRegionCore(rz, rz);
|
||||
_cache.Add(k, r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
catch (FileNotFoundException) {
|
||||
_cache.Add(k, null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public bool RegionExists (int rx, int rz)
|
||||
{
|
||||
IRegion r = GetRegion(rx, rz);
|
||||
return r != null;
|
||||
}
|
||||
|
||||
/// <inherits />
|
||||
public IRegion CreateRegion (int rx, int rz)
|
||||
{
|
||||
IRegion r = GetRegion(rx, rz);
|
||||
if (r == null) {
|
||||
string fp = "r." + rx + "." + rz + ".mca";
|
||||
using (RegionFile rf = CreateRegionFileCore(rx, rz)) {
|
||||
|
||||
}
|
||||
|
||||
r = CreateRegionCore(rx, rz);
|
||||
|
||||
RegionKey k = new RegionKey(rx, rz);
|
||||
_cache[k] = r;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current region directory path.
|
||||
/// </summary>
|
||||
/// <returns>The path to the region directory.</returns>
|
||||
public string GetRegionPath ()
|
||||
{
|
||||
return _regionPath;
|
||||
}
|
||||
|
||||
// XXX: Exceptions
|
||||
/// <inherits />
|
||||
public bool DeleteRegion (int rx, int rz)
|
||||
{
|
||||
IRegion r = GetRegion(rx, rz);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RegionKey k = new RegionKey(rx, rz);
|
||||
_cache.Remove(k);
|
||||
|
||||
DeleteRegionCore(r);
|
||||
|
||||
try {
|
||||
File.Delete(r.GetFilePath());
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine("NOTICE: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#region IEnumerable<IRegion> Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates over all of the regions in the underlying dimension.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator instance.</returns>
|
||||
public IEnumerator<IRegion> GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates over all of the regions in the underlying dimension.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator instance.</returns>
|
||||
IEnumerator IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private struct Enumerator : IEnumerator<IRegion>
|
||||
{
|
||||
private List<IRegion> _regions;
|
||||
private int _pos;
|
||||
|
||||
public Enumerator (RegionManager rm)
|
||||
{
|
||||
_regions = new List<IRegion>();
|
||||
_pos = -1;
|
||||
|
||||
if (!Directory.Exists(rm.GetRegionPath())) {
|
||||
throw new DirectoryNotFoundException();
|
||||
}
|
||||
|
||||
string[] files = Directory.GetFiles(rm.GetRegionPath());
|
||||
_regions.Capacity = files.Length;
|
||||
|
||||
foreach (string file in files) {
|
||||
try {
|
||||
IRegion r = rm.GetRegion(file);
|
||||
_regions.Add(r);
|
||||
}
|
||||
catch (ArgumentException) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool MoveNext ()
|
||||
{
|
||||
_pos++;
|
||||
return (_pos < _regions.Count);
|
||||
}
|
||||
|
||||
public void Reset ()
|
||||
{
|
||||
_pos = -1;
|
||||
}
|
||||
|
||||
void IDisposable.Dispose () { }
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
IRegion IEnumerator<IRegion>.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public IRegion Current
|
||||
{
|
||||
get
|
||||
{
|
||||
try {
|
||||
return _regions[_pos];
|
||||
}
|
||||
catch (IndexOutOfRangeException) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,466 +1,466 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Substrate.Core;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a Beta-compatible interface for globally managing chunks.
|
||||
/// </summary>
|
||||
public class RegionChunkManager : IChunkManager, IEnumerable<ChunkRef>
|
||||
{
|
||||
private const int REGION_XLEN = 32;
|
||||
private const int REGION_ZLEN = 32;
|
||||
|
||||
private const int REGION_XLOG = 5;
|
||||
private const int REGION_ZLOG = 5;
|
||||
|
||||
private const int REGION_XMASK = 0x1F;
|
||||
private const int REGION_ZMASK = 0x1F;
|
||||
|
||||
private IRegionManager _regionMan;
|
||||
|
||||
private ChunkCache _cache;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RegionChunkManager"/> instance given a backing <see cref="RegionManager"/> and <see cref="ChunkCache"/>.
|
||||
/// </summary>
|
||||
/// <param name="rm">A <see cref="RegionManager"/> exposing access to regions.</param>
|
||||
/// <param name="cache">A shared cache for storing chunks read in.</param>
|
||||
public RegionChunkManager (IRegionManager rm, ChunkCache cache)
|
||||
{
|
||||
_regionMan = rm;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RegionChunkManager"/> instance from another.
|
||||
/// </summary>
|
||||
/// <param name="cm">A <see cref="RegionChunkManager"/> to get a <see cref="RegionManager"/> and <see cref="ChunkCache"/> from.</param>
|
||||
public RegionChunkManager (RegionChunkManager cm)
|
||||
{
|
||||
_regionMan = cm._regionMan;
|
||||
_cache = cm._cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="RegionManager"/> backing this manager.
|
||||
/// </summary>
|
||||
public IRegionManager RegionManager
|
||||
{
|
||||
get { return _regionMan; }
|
||||
}
|
||||
|
||||
#region IChunkContainer
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkGlobalX (int cx)
|
||||
{
|
||||
return cx;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkGlobalZ (int cz)
|
||||
{
|
||||
return cz;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkLocalX (int cx)
|
||||
{
|
||||
return cx & REGION_XMASK;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkLocalZ (int cz)
|
||||
{
|
||||
return cz & REGION_ZMASK;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IChunk GetChunk (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return r.GetChunk(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ChunkRef GetChunkRef (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return r.GetChunkRef(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool ChunkExists (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return r.ChunkExists(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ChunkRef CreateChunk (int cx, int cz)
|
||||
{
|
||||
IRegion 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);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ChunkRef SetChunk (int cx, int cz, IChunk chunk)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
int rx = cx >> REGION_XLOG;
|
||||
int rz = cz >> REGION_ZLOG;
|
||||
r = _regionMan.CreateRegion(rx, rz);
|
||||
}
|
||||
|
||||
chunk.SetLocation(cx, cz);
|
||||
r.SaveChunk(chunk);
|
||||
|
||||
return r.GetChunkRef(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Save ()
|
||||
{
|
||||
_cache.SyncDirty();
|
||||
|
||||
int saved = 0;
|
||||
IEnumerator<ChunkRef> en = _cache.GetDirtyEnumerator();
|
||||
while (en.MoveNext()) {
|
||||
ChunkRef chunk = en.Current;
|
||||
|
||||
IRegion r = GetRegion(chunk.X, chunk.Z);
|
||||
if (r == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
chunk.Save(r.GetChunkOutStream(chunk.LocalX, chunk.LocalZ));
|
||||
saved++;
|
||||
}
|
||||
|
||||
_cache.ClearDirty();
|
||||
return saved;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool SaveChunk (IChunk chunk)
|
||||
{
|
||||
IRegion r = GetRegion(chunk.X, chunk.Z);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return r.SaveChunk(chunk);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool DeleteChunk (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!r.DeleteChunk(cx & REGION_XMASK, cz & REGION_ZMASK)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (r.ChunkCount() == 0) {
|
||||
_regionMan.DeleteRegion(r.X, r.Z);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool CanDelegateCoordinates
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Copies a chunk from one location to another.
|
||||
/// </summary>
|
||||
/// <param name="src_cx">The global X-coordinate of the source chunk.</param>
|
||||
/// <param name="src_cz">The global Z-coordinate of the source chunk.</param>
|
||||
/// <param name="dst_cx">The global X-coordinate of the destination chunk.</param>
|
||||
/// <param name="dst_cz">The global Z-coordinate of the destination chunk.</param>
|
||||
/// <returns>A <see cref="ChunkRef"/> for the destination chunk.</returns>
|
||||
public ChunkRef CopyChunk (int src_cx, int src_cz, int dst_cx, int dst_cz)
|
||||
{
|
||||
IRegion src_r = GetRegion(src_cx, src_cz);
|
||||
if (src_r == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
IRegion dst_r = GetRegion(dst_cx, dst_cz);
|
||||
if (dst_r == null) {
|
||||
int rx = dst_cx >> REGION_XLOG;
|
||||
int rz = dst_cz >> REGION_ZLOG;
|
||||
dst_r = _regionMan.CreateRegion(rx, rz);
|
||||
}
|
||||
|
||||
IChunk c = src_r.GetChunk(src_cx & REGION_XMASK, src_cz & REGION_ZMASK);
|
||||
c.SetLocation(dst_cx, dst_cz);
|
||||
|
||||
dst_r.SaveChunk(c);
|
||||
|
||||
return dst_r.GetChunkRef(dst_cx & REGION_XMASK, dst_cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a full chunk relight sequence on all modified chunks.
|
||||
/// </summary>
|
||||
public void RelightDirtyChunks ()
|
||||
{
|
||||
//List<ChunkRef> dirty = new List<ChunkRef>();
|
||||
Dictionary<ChunkKey, ChunkRef> dirty = new Dictionary<ChunkKey, ChunkRef>();
|
||||
|
||||
_cache.SyncDirty();
|
||||
|
||||
IEnumerator<ChunkRef> en = _cache.GetDirtyEnumerator();
|
||||
while (en.MoveNext()) {
|
||||
ChunkKey key = new ChunkKey(en.Current.X, en.Current.Z);
|
||||
dirty[key] = en.Current;
|
||||
}
|
||||
|
||||
foreach (ChunkRef chunk in dirty.Values) {
|
||||
chunk.Blocks.ResetBlockLight();
|
||||
chunk.Blocks.ResetSkyLight();
|
||||
}
|
||||
|
||||
foreach (ChunkRef chunk in dirty.Values) {
|
||||
chunk.Blocks.RebuildBlockLight();
|
||||
chunk.Blocks.RebuildSkyLight();
|
||||
}
|
||||
|
||||
foreach (ChunkRef chunk in dirty.Values) {
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z - 1))) {
|
||||
ChunkRef east = chunk.GetEastNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(east.Blocks, BlockCollectionEdge.EAST);
|
||||
chunk.Blocks.StitchSkyLight(east.Blocks, BlockCollectionEdge.EAST);
|
||||
}
|
||||
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z + 1))) {
|
||||
ChunkRef west = chunk.GetWestNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(west.Blocks, BlockCollectionEdge.WEST);
|
||||
chunk.Blocks.StitchSkyLight(west.Blocks, BlockCollectionEdge.WEST);
|
||||
}
|
||||
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X - 1, chunk.Z))) {
|
||||
ChunkRef north = chunk.GetNorthNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(north.Blocks, BlockCollectionEdge.NORTH);
|
||||
chunk.Blocks.StitchSkyLight(north.Blocks, BlockCollectionEdge.NORTH);
|
||||
}
|
||||
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X + 1, chunk.Z))) {
|
||||
ChunkRef south = chunk.GetSouthNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(south.Blocks, BlockCollectionEdge.SOUTH);
|
||||
chunk.Blocks.StitchSkyLight(south.Blocks, BlockCollectionEdge.SOUTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the timestamp of the chunk from its underlying region file.
|
||||
/// </summary>
|
||||
/// <param name="cx">The global X-coordinate of a chunk.</param>
|
||||
/// <param name="cz">The global Z-coordinate of a chunk.</param>
|
||||
/// <returns>The timestamp of the chunk from its underlying region file.</returns>
|
||||
/// <remarks>The value returned may differ from any timestamp stored in the chunk data itself.</remarks>
|
||||
public int GetChunkTimestamp (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return r.GetChunkTimestamp(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the timestamp of the chunk in its underlying region file.
|
||||
/// </summary>
|
||||
/// <param name="cx">The global X-coordinate of a chunk.</param>
|
||||
/// <param name="cz">The global Z-coordinate of a chunk.</param>
|
||||
/// <param name="timestamp">The new timestamp value.</param>
|
||||
/// <remarks>This function will only update the timestamp of the chunk slot in the underlying region file. It will not update
|
||||
/// any timestamp information in the chunk data itself.</remarks>
|
||||
public void SetChunkTimestamp (int cx, int cz, int timestamp)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
r.SetChunkTimestamp(cx & REGION_XMASK, cz & REGION_ZMASK, timestamp);
|
||||
}
|
||||
|
||||
private ChunkRef GetChunkRefInRegion (IRegion r, int lcx, int lcz)
|
||||
{
|
||||
int cx = r.X * REGION_XLEN + lcx;
|
||||
int cz = r.Z * REGION_ZLEN + lcz;
|
||||
return GetChunkRef(cx, cz);
|
||||
}
|
||||
|
||||
private IRegion GetRegion (int cx, int cz)
|
||||
{
|
||||
cx >>= REGION_XLOG;
|
||||
cz >>= REGION_ZLOG;
|
||||
return _regionMan.GetRegion(cx, cz);
|
||||
}
|
||||
|
||||
|
||||
#region IEnumerable<ChunkRef> Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates through all chunks in all regions of the world.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator for this manager.</returns>
|
||||
public IEnumerator<ChunkRef> GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
/// <inheritdoc/>
|
||||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private class Enumerator : IEnumerator<ChunkRef>
|
||||
{
|
||||
private RegionChunkManager _cm;
|
||||
|
||||
private IEnumerator<IRegion> _enum;
|
||||
private IRegion _region;
|
||||
private ChunkRef _chunk;
|
||||
|
||||
private int _x = 0;
|
||||
private int _z = -1;
|
||||
|
||||
public Enumerator (RegionChunkManager cm)
|
||||
{
|
||||
_cm = cm;
|
||||
_enum = _cm.RegionManager.GetEnumerator();
|
||||
_enum.MoveNext();
|
||||
_region = _enum.Current;
|
||||
}
|
||||
|
||||
public virtual bool MoveNext ()
|
||||
{
|
||||
if (_enum == null) {
|
||||
return MoveNextInRegion();
|
||||
}
|
||||
else {
|
||||
while (true) {
|
||||
if (_x >= RegionChunkManager.REGION_XLEN) {
|
||||
if (!_enum.MoveNext()) {
|
||||
return false;
|
||||
}
|
||||
_x = 0;
|
||||
_z = -1;
|
||||
_region = _enum.Current;
|
||||
}
|
||||
if (MoveNextInRegion()) {
|
||||
_chunk = _region.GetChunkRef(_x, _z);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected bool MoveNextInRegion ()
|
||||
{
|
||||
for (; _x < RegionChunkManager.REGION_XLEN; _x++) {
|
||||
for (_z++; _z < RegionChunkManager.REGION_ZLEN; _z++) {
|
||||
if (_region.ChunkExists(_x, _z)) {
|
||||
goto FoundNext;
|
||||
}
|
||||
}
|
||||
_z = -1;
|
||||
}
|
||||
|
||||
FoundNext:
|
||||
|
||||
return (_x < RegionChunkManager.REGION_XLEN);
|
||||
}
|
||||
|
||||
public void Reset ()
|
||||
{
|
||||
if (_enum != null) {
|
||||
_enum.Reset();
|
||||
_enum.MoveNext();
|
||||
_region = _enum.Current;
|
||||
}
|
||||
_x = 0;
|
||||
_z = -1;
|
||||
}
|
||||
|
||||
void IDisposable.Dispose () { }
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
ChunkRef IEnumerator<ChunkRef>.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public ChunkRef Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_x >= RegionChunkManager.REGION_XLEN) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return _chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MissingChunkException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Substrate.Core;
|
||||
|
||||
namespace Substrate
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a Beta-compatible interface for globally managing chunks.
|
||||
/// </summary>
|
||||
public class RegionChunkManager : IChunkManager, IEnumerable<ChunkRef>
|
||||
{
|
||||
private const int REGION_XLEN = 32;
|
||||
private const int REGION_ZLEN = 32;
|
||||
|
||||
private const int REGION_XLOG = 5;
|
||||
private const int REGION_ZLOG = 5;
|
||||
|
||||
private const int REGION_XMASK = 0x1F;
|
||||
private const int REGION_ZMASK = 0x1F;
|
||||
|
||||
private IRegionManager _regionMan;
|
||||
|
||||
private ChunkCache _cache;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RegionChunkManager"/> instance given a backing <see cref="RegionManager"/> and <see cref="ChunkCache"/>.
|
||||
/// </summary>
|
||||
/// <param name="rm">A <see cref="RegionManager"/> exposing access to regions.</param>
|
||||
/// <param name="cache">A shared cache for storing chunks read in.</param>
|
||||
public RegionChunkManager (IRegionManager rm, ChunkCache cache)
|
||||
{
|
||||
_regionMan = rm;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RegionChunkManager"/> instance from another.
|
||||
/// </summary>
|
||||
/// <param name="cm">A <see cref="RegionChunkManager"/> to get a <see cref="RegionManager"/> and <see cref="ChunkCache"/> from.</param>
|
||||
public RegionChunkManager (RegionChunkManager cm)
|
||||
{
|
||||
_regionMan = cm._regionMan;
|
||||
_cache = cm._cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="RegionManager"/> backing this manager.
|
||||
/// </summary>
|
||||
public IRegionManager RegionManager
|
||||
{
|
||||
get { return _regionMan; }
|
||||
}
|
||||
|
||||
#region IChunkContainer
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkGlobalX (int cx)
|
||||
{
|
||||
return cx;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkGlobalZ (int cz)
|
||||
{
|
||||
return cz;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkLocalX (int cx)
|
||||
{
|
||||
return cx & REGION_XMASK;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int ChunkLocalZ (int cz)
|
||||
{
|
||||
return cz & REGION_ZMASK;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IChunk GetChunk (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return r.GetChunk(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ChunkRef GetChunkRef (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return r.GetChunkRef(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool ChunkExists (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return r.ChunkExists(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ChunkRef CreateChunk (int cx, int cz)
|
||||
{
|
||||
IRegion 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);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ChunkRef SetChunk (int cx, int cz, IChunk chunk)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
int rx = cx >> REGION_XLOG;
|
||||
int rz = cz >> REGION_ZLOG;
|
||||
r = _regionMan.CreateRegion(rx, rz);
|
||||
}
|
||||
|
||||
chunk.SetLocation(cx, cz);
|
||||
r.SaveChunk(chunk);
|
||||
|
||||
return r.GetChunkRef(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Save ()
|
||||
{
|
||||
_cache.SyncDirty();
|
||||
|
||||
int saved = 0;
|
||||
IEnumerator<ChunkRef> en = _cache.GetDirtyEnumerator();
|
||||
while (en.MoveNext()) {
|
||||
ChunkRef chunk = en.Current;
|
||||
|
||||
IRegion r = GetRegion(chunk.X, chunk.Z);
|
||||
if (r == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
chunk.Save(r.GetChunkOutStream(chunk.LocalX, chunk.LocalZ));
|
||||
saved++;
|
||||
}
|
||||
|
||||
_cache.ClearDirty();
|
||||
return saved;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool SaveChunk (IChunk chunk)
|
||||
{
|
||||
IRegion r = GetRegion(chunk.X, chunk.Z);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return r.SaveChunk(chunk);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool DeleteChunk (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!r.DeleteChunk(cx & REGION_XMASK, cz & REGION_ZMASK)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (r.ChunkCount() == 0) {
|
||||
_regionMan.DeleteRegion(r.X, r.Z);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool CanDelegateCoordinates
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Copies a chunk from one location to another.
|
||||
/// </summary>
|
||||
/// <param name="src_cx">The global X-coordinate of the source chunk.</param>
|
||||
/// <param name="src_cz">The global Z-coordinate of the source chunk.</param>
|
||||
/// <param name="dst_cx">The global X-coordinate of the destination chunk.</param>
|
||||
/// <param name="dst_cz">The global Z-coordinate of the destination chunk.</param>
|
||||
/// <returns>A <see cref="ChunkRef"/> for the destination chunk.</returns>
|
||||
public ChunkRef CopyChunk (int src_cx, int src_cz, int dst_cx, int dst_cz)
|
||||
{
|
||||
IRegion src_r = GetRegion(src_cx, src_cz);
|
||||
if (src_r == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
IRegion dst_r = GetRegion(dst_cx, dst_cz);
|
||||
if (dst_r == null) {
|
||||
int rx = dst_cx >> REGION_XLOG;
|
||||
int rz = dst_cz >> REGION_ZLOG;
|
||||
dst_r = _regionMan.CreateRegion(rx, rz);
|
||||
}
|
||||
|
||||
IChunk c = src_r.GetChunk(src_cx & REGION_XMASK, src_cz & REGION_ZMASK);
|
||||
c.SetLocation(dst_cx, dst_cz);
|
||||
|
||||
dst_r.SaveChunk(c);
|
||||
|
||||
return dst_r.GetChunkRef(dst_cx & REGION_XMASK, dst_cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a full chunk relight sequence on all modified chunks.
|
||||
/// </summary>
|
||||
public void RelightDirtyChunks ()
|
||||
{
|
||||
//List<ChunkRef> dirty = new List<ChunkRef>();
|
||||
Dictionary<ChunkKey, ChunkRef> dirty = new Dictionary<ChunkKey, ChunkRef>();
|
||||
|
||||
_cache.SyncDirty();
|
||||
|
||||
IEnumerator<ChunkRef> en = _cache.GetDirtyEnumerator();
|
||||
while (en.MoveNext()) {
|
||||
ChunkKey key = new ChunkKey(en.Current.X, en.Current.Z);
|
||||
dirty[key] = en.Current;
|
||||
}
|
||||
|
||||
foreach (ChunkRef chunk in dirty.Values) {
|
||||
chunk.Blocks.ResetBlockLight();
|
||||
chunk.Blocks.ResetSkyLight();
|
||||
}
|
||||
|
||||
foreach (ChunkRef chunk in dirty.Values) {
|
||||
chunk.Blocks.RebuildBlockLight();
|
||||
chunk.Blocks.RebuildSkyLight();
|
||||
}
|
||||
|
||||
foreach (ChunkRef chunk in dirty.Values) {
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z - 1))) {
|
||||
ChunkRef east = chunk.GetEastNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(east.Blocks, BlockCollectionEdge.EAST);
|
||||
chunk.Blocks.StitchSkyLight(east.Blocks, BlockCollectionEdge.EAST);
|
||||
}
|
||||
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z + 1))) {
|
||||
ChunkRef west = chunk.GetWestNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(west.Blocks, BlockCollectionEdge.WEST);
|
||||
chunk.Blocks.StitchSkyLight(west.Blocks, BlockCollectionEdge.WEST);
|
||||
}
|
||||
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X - 1, chunk.Z))) {
|
||||
ChunkRef north = chunk.GetNorthNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(north.Blocks, BlockCollectionEdge.NORTH);
|
||||
chunk.Blocks.StitchSkyLight(north.Blocks, BlockCollectionEdge.NORTH);
|
||||
}
|
||||
|
||||
if (!dirty.ContainsKey(new ChunkKey(chunk.X + 1, chunk.Z))) {
|
||||
ChunkRef south = chunk.GetSouthNeighbor();
|
||||
chunk.Blocks.StitchBlockLight(south.Blocks, BlockCollectionEdge.SOUTH);
|
||||
chunk.Blocks.StitchSkyLight(south.Blocks, BlockCollectionEdge.SOUTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the timestamp of the chunk from its underlying region file.
|
||||
/// </summary>
|
||||
/// <param name="cx">The global X-coordinate of a chunk.</param>
|
||||
/// <param name="cz">The global Z-coordinate of a chunk.</param>
|
||||
/// <returns>The timestamp of the chunk from its underlying region file.</returns>
|
||||
/// <remarks>The value returned may differ from any timestamp stored in the chunk data itself.</remarks>
|
||||
public int GetChunkTimestamp (int cx, int cz)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return r.GetChunkTimestamp(cx & REGION_XMASK, cz & REGION_ZMASK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the timestamp of the chunk in its underlying region file.
|
||||
/// </summary>
|
||||
/// <param name="cx">The global X-coordinate of a chunk.</param>
|
||||
/// <param name="cz">The global Z-coordinate of a chunk.</param>
|
||||
/// <param name="timestamp">The new timestamp value.</param>
|
||||
/// <remarks>This function will only update the timestamp of the chunk slot in the underlying region file. It will not update
|
||||
/// any timestamp information in the chunk data itself.</remarks>
|
||||
public void SetChunkTimestamp (int cx, int cz, int timestamp)
|
||||
{
|
||||
IRegion r = GetRegion(cx, cz);
|
||||
if (r == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
r.SetChunkTimestamp(cx & REGION_XMASK, cz & REGION_ZMASK, timestamp);
|
||||
}
|
||||
|
||||
private ChunkRef GetChunkRefInRegion (IRegion r, int lcx, int lcz)
|
||||
{
|
||||
int cx = r.X * REGION_XLEN + lcx;
|
||||
int cz = r.Z * REGION_ZLEN + lcz;
|
||||
return GetChunkRef(cx, cz);
|
||||
}
|
||||
|
||||
private IRegion GetRegion (int cx, int cz)
|
||||
{
|
||||
cx >>= REGION_XLOG;
|
||||
cz >>= REGION_ZLOG;
|
||||
return _regionMan.GetRegion(cx, cz);
|
||||
}
|
||||
|
||||
|
||||
#region IEnumerable<ChunkRef> Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates through all chunks in all regions of the world.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator for this manager.</returns>
|
||||
public IEnumerator<ChunkRef> GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
/// <inheritdoc/>
|
||||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private class Enumerator : IEnumerator<ChunkRef>
|
||||
{
|
||||
private RegionChunkManager _cm;
|
||||
|
||||
private IEnumerator<IRegion> _enum;
|
||||
private IRegion _region;
|
||||
private ChunkRef _chunk;
|
||||
|
||||
private int _x = 0;
|
||||
private int _z = -1;
|
||||
|
||||
public Enumerator (RegionChunkManager cm)
|
||||
{
|
||||
_cm = cm;
|
||||
_enum = _cm.RegionManager.GetEnumerator();
|
||||
_enum.MoveNext();
|
||||
_region = _enum.Current;
|
||||
}
|
||||
|
||||
public virtual bool MoveNext ()
|
||||
{
|
||||
if (_enum == null) {
|
||||
return MoveNextInRegion();
|
||||
}
|
||||
else {
|
||||
while (true) {
|
||||
if (_x >= RegionChunkManager.REGION_XLEN) {
|
||||
if (!_enum.MoveNext()) {
|
||||
return false;
|
||||
}
|
||||
_x = 0;
|
||||
_z = -1;
|
||||
_region = _enum.Current;
|
||||
}
|
||||
if (MoveNextInRegion()) {
|
||||
_chunk = _region.GetChunkRef(_x, _z);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected bool MoveNextInRegion ()
|
||||
{
|
||||
for (; _x < RegionChunkManager.REGION_XLEN; _x++) {
|
||||
for (_z++; _z < RegionChunkManager.REGION_ZLEN; _z++) {
|
||||
if (_region.ChunkExists(_x, _z)) {
|
||||
goto FoundNext;
|
||||
}
|
||||
}
|
||||
_z = -1;
|
||||
}
|
||||
|
||||
FoundNext:
|
||||
|
||||
return (_x < RegionChunkManager.REGION_XLEN);
|
||||
}
|
||||
|
||||
public void Reset ()
|
||||
{
|
||||
if (_enum != null) {
|
||||
_enum.Reset();
|
||||
_enum.MoveNext();
|
||||
_region = _enum.Current;
|
||||
}
|
||||
_x = 0;
|
||||
_z = -1;
|
||||
}
|
||||
|
||||
void IDisposable.Dispose () { }
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
ChunkRef IEnumerator<ChunkRef>.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public ChunkRef Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_x >= RegionChunkManager.REGION_XLEN) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return _chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MissingChunkException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue