From 0cf047c51d3af7c89632b2727921d7257a260221 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Fri, 8 Apr 2011 20:52:28 +0000 Subject: [PATCH] ChunkRef enumerators moved into Manager class namespaces; Manager classes now offer enumerators directly. --- NBToolkit/Dump.cs | 4 +- NBToolkit/FilteredChunkEnumerator.cs | 116 ------------- NBToolkit/NBToolkit.csproj | 3 +- NBToolkit/Oregen.cs | 4 +- NBToolkit/Purge.cs | 3 +- NBToolkit/Replace.cs | 4 +- NBToolkit/TKOptions.cs | 4 +- .../SubstrateCS/Source/ChunkEnumerator.cs | 156 ------------------ .../SubstrateCS/Source/ChunkFileManager.cs | 18 ++ .../SubstrateCS/Source/ChunkInterface.cs | 2 +- Substrate/SubstrateCS/Source/ChunkManager.cs | 113 ++++++++++++- .../SubstrateCS/Source/RegionEnumerator.cs | 131 --------------- Substrate/SubstrateCS/Source/RegionManager.cs | 101 +++++++++++- Substrate/SubstrateCS/Substrate.csproj | 2 - Substrate/SubstrateCS/Substrate.sln | 6 - 15 files changed, 245 insertions(+), 422 deletions(-) delete mode 100644 NBToolkit/FilteredChunkEnumerator.cs delete mode 100644 Substrate/SubstrateCS/Source/ChunkEnumerator.cs delete mode 100644 Substrate/SubstrateCS/Source/RegionEnumerator.cs diff --git a/NBToolkit/Dump.cs b/NBToolkit/Dump.cs index 8de6089..4b5ca14 100644 --- a/NBToolkit/Dump.cs +++ b/NBToolkit/Dump.cs @@ -97,7 +97,9 @@ 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()); StreamWriter fstr; try { @@ -111,7 +113,7 @@ namespace NBToolkit fstr.WriteLine("["); bool first = true; - foreach (ChunkRef chunk in new FilteredChunkList(cm, opt.GetChunkFilter())) { + foreach (ChunkRef chunk in fcm) { if (!first) { fstr.Write(","); } diff --git a/NBToolkit/FilteredChunkEnumerator.cs b/NBToolkit/FilteredChunkEnumerator.cs deleted file mode 100644 index 523b3b8..0000000 --- a/NBToolkit/FilteredChunkEnumerator.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Substrate; - -namespace NBToolkit -{ - public class FilteredChunkList : ChunkList - { - protected IChunkFilter _filter; - - public FilteredChunkList (ChunkManager cm, IChunkFilter filter) - : base(cm) - { - _filter = filter; - } - - public FilteredChunkList (ChunkManager cm, Region reg, IChunkFilter filter) - : base(cm, reg) - { - _filter = filter; - } - - public override ChunkEnumerator GetEnumerator () - { - return new FilteredChunkEnumerator(_cm, _region, _filter); - } - } - - public class FilteredChunkEnumerator : ChunkEnumerator - { - protected IChunkFilter _filter; - - public FilteredChunkEnumerator (ChunkManager cm, IChunkFilter filter) - : base(cm) - { - _filter = filter; - } - - public FilteredChunkEnumerator (ChunkManager cm, Region reg, IChunkFilter filter) - : base(cm, reg) - { - _filter = filter; - } - - public override bool MoveNext () - { - while (true) { - if (base.MoveNext() == false) { - return false; - } - - // Filter by coordinates - if (_filter.InvertXZ) { - if (_filter.XAboveEq != null && _filter.XBelowEq != null && - _filter.ZAboveEq != null && _filter.ZBelowEq != null && - Current.X >= _filter.XAboveEq && Current.X <= _filter.XBelowEq && - Current.Z >= _filter.ZAboveEq && Current.Z <= _filter.ZBelowEq) { - continue; - } - } - else { - if (_filter.XAboveEq != null && Current.X < _filter.XAboveEq) { - continue; - } - if (_filter.XBelowEq != null && Current.X > _filter.XBelowEq) { - continue; - } - if (_filter.ZAboveEq != null && Current.Z < _filter.ZAboveEq) { - continue; - } - if (_filter.ZBelowEq != null && Current.Z > _filter.ZBelowEq) { - continue; - } - } - - // Filter out chunks that do not contain required blocks (included list) - if (_filter.IncludedBlockCount > 0) { - int matchCount = 0; - foreach (int block in _filter.IncludedBlocks) { - if (Current.CountBlockID(block) > 0) { - matchCount++; - } - } - - if (_filter.IncludeMatchAny && matchCount == 0) { - continue; - } - if (_filter.IncludeMatchAll && matchCount != _filter.IncludedBlockCount) { - continue; - } - } - - // Filter out chunks that contain forbiddon blocks (excluded list) - if (_filter.ExcludedBlockCount > 0) { - int matchCount = 0; - foreach (int block in _filter.ExcludedBlocks) { - if (Current.CountBlockID(block) > 0) { - matchCount++; - } - } - - if (_filter.ExcludeMatchAny && matchCount > 0) { - continue; - } - if (_filter.ExcludeMatchAll && matchCount == _filter.ExcludedBlockCount) { - continue; - } - } - - - return true; - } - } - } -} diff --git a/NBToolkit/NBToolkit.csproj b/NBToolkit/NBToolkit.csproj index 16233fe..afe642c 100644 --- a/NBToolkit/NBToolkit.csproj +++ b/NBToolkit/NBToolkit.csproj @@ -54,11 +54,12 @@ + - + diff --git a/NBToolkit/Oregen.cs b/NBToolkit/Oregen.cs index 3df9d86..150128f 100644 --- a/NBToolkit/Oregen.cs +++ b/NBToolkit/Oregen.cs @@ -182,10 +182,12 @@ 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()); int affectedChunks = 0; - foreach (ChunkRef chunk in new FilteredChunkList(cm, opt.GetChunkFilter())) { + foreach (ChunkRef chunk in fcm) { if (chunk == null || !chunk.IsTerrainPopulated) { continue; } diff --git a/NBToolkit/Purge.cs b/NBToolkit/Purge.cs index ba87513..2b5736d 100644 --- a/NBToolkit/Purge.cs +++ b/NBToolkit/Purge.cs @@ -72,9 +72,10 @@ namespace NBToolkit World world = new World(opt.OPT_WORLD); ChunkManager cm = world.GetChunkManager() as ChunkManager; + FilteredChunkManager fcm = new FilteredChunkManager(cm, opt.GetChunkFilter()); int affectedChunks = 0; - foreach (ChunkRef chunk in new FilteredChunkList(cm, opt.GetChunkFilter())) { + foreach (ChunkRef chunk in fcm) { affectedChunks++; world.GetChunkManager().DeleteChunk(chunk.X, chunk.Z); } diff --git a/NBToolkit/Replace.cs b/NBToolkit/Replace.cs index 600e39e..3833c77 100644 --- a/NBToolkit/Replace.cs +++ b/NBToolkit/Replace.cs @@ -152,8 +152,10 @@ namespace NBToolkit World world = new World(opt.OPT_WORLD); ChunkManager cm = world.GetChunkManager() as ChunkManager; + FilteredChunkManager fcm = new FilteredChunkManager(cm, opt.GetChunkFilter()); + int affectedChunks = 0; - foreach (ChunkRef chunk in new FilteredChunkList(cm, opt.GetChunkFilter())) { + foreach (ChunkRef chunk in fcm) { affectedChunks++; ApplyChunk(world, chunk); diff --git a/NBToolkit/TKOptions.cs b/NBToolkit/TKOptions.cs index 0b005df..cc13d8b 100644 --- a/NBToolkit/TKOptions.cs +++ b/NBToolkit/TKOptions.cs @@ -73,7 +73,7 @@ namespace NBToolkit throw new TKOptionException(); } - if (!File.Exists(Path.Combine(OPT_WORLD, "level.dat"))) { + /*if (!File.Exists(Path.Combine(OPT_WORLD, "level.dat"))) { Console.WriteLine("Error: The supplied world path is invalid"); Console.WriteLine(); this.PrintUsage(); @@ -87,7 +87,7 @@ namespace NBToolkit this.PrintUsage(); throw new TKOptionException(); - } + }*/ } } diff --git a/Substrate/SubstrateCS/Source/ChunkEnumerator.cs b/Substrate/SubstrateCS/Source/ChunkEnumerator.cs deleted file mode 100644 index 82e1d01..0000000 --- a/Substrate/SubstrateCS/Source/ChunkEnumerator.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; -using System.IO; - -namespace Substrate -{ - public class ChunkList : IEnumerable - { - //private List _regions; - - protected ChunkManager _cm = null; - protected Region _region = null; - - // Constructor to enumerate a single region - public ChunkList (ChunkManager cm, Region region) - { - _cm = cm; - _region = region; - } - - // Constructor to enumerate all regions - public ChunkList (ChunkManager cm) - { - _cm = cm; - } - - IEnumerator IEnumerable.GetEnumerator () - { - return (IEnumerator)GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator () - { - return (IEnumerator)GetEnumerator(); - } - - public virtual ChunkEnumerator GetEnumerator () - { - return new ChunkEnumerator(_cm, _region); - } - } - - public class ChunkEnumerator : IEnumerator - { - protected Region _region; - protected ChunkManager _cm; - - protected ChunkRef _chunk; - - protected RegionEnumerator _enum = null; - protected int _x = 0; - protected int _z = -1; - - public ChunkEnumerator (ChunkManager cm, Region region) - { - _cm = cm; - _region = region; - - if (_region == null) { - _enum = new RegionEnumerator(_cm.GetRegionManager()); - _enum.MoveNext(); - _region = _enum.Current; - } - } - - public ChunkEnumerator (ChunkManager cm) - { - _cm = cm; - _enum = new RegionEnumerator(_cm.GetRegionManager()); - _enum.MoveNext(); - _region = _enum.Current; - } - - public virtual bool MoveNext () - { - if (_enum == null) { - return MoveNextInRegion(); - } - else { - while (true) { - if (_x >= ChunkManager.REGION_XLEN) { - if (!_enum.MoveNext()) { - return false; - } - _x = 0; - _z = -1; - _region = _enum.Current; - } - if (MoveNextInRegion()) { - _chunk = _cm.GetChunkRefInRegion(_region, _x, _z); - return true; - } - } - } - } - - protected bool MoveNextInRegion () - { - for (; _x < ChunkManager.REGION_XLEN; _x++) { - for (_z++; _z < ChunkManager.REGION_ZLEN; _z++) { - if (_region.ChunkExists(_x, _z)) { - goto FoundNext; - } - } - _z = -1; - } - - FoundNext: - - return (_x < ChunkManager.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.Current - { - get - { - return Current; - } - } - - public ChunkRef Current - { - get - { - if (_x >= ChunkManager.REGION_XLEN) { - throw new InvalidOperationException(); - } - return _chunk; - } - } - } -} diff --git a/Substrate/SubstrateCS/Source/ChunkFileManager.cs b/Substrate/SubstrateCS/Source/ChunkFileManager.cs index b3184b0..2c84706 100644 --- a/Substrate/SubstrateCS/Source/ChunkFileManager.cs +++ b/Substrate/SubstrateCS/Source/ChunkFileManager.cs @@ -184,5 +184,23 @@ namespace Substrate #endregion + + #region IEnumerable Members + + public IEnumerator GetEnumerator () + { + return null; + } + + #endregion + + #region IEnumerable Members + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () + { + return null; + } + + #endregion } } diff --git a/Substrate/SubstrateCS/Source/ChunkInterface.cs b/Substrate/SubstrateCS/Source/ChunkInterface.cs index 9fdc51e..db10dc6 100644 --- a/Substrate/SubstrateCS/Source/ChunkInterface.cs +++ b/Substrate/SubstrateCS/Source/ChunkInterface.cs @@ -48,7 +48,7 @@ namespace Substrate bool SaveChunk (Chunk chunk); } - public interface IChunkManager : IChunkContainer + public interface IChunkManager : IChunkContainer, IEnumerable { } diff --git a/Substrate/SubstrateCS/Source/ChunkManager.cs b/Substrate/SubstrateCS/Source/ChunkManager.cs index 6b07f72..06d2364 100644 --- a/Substrate/SubstrateCS/Source/ChunkManager.cs +++ b/Substrate/SubstrateCS/Source/ChunkManager.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.Text; +using System.Collections; namespace Substrate { @@ -28,6 +28,13 @@ namespace Substrate _dirty = new Dictionary(); } + public ChunkManager (ChunkManager cm) + { + _regionMan = cm._regionMan; + _cache = new Dictionary(); + _dirty = new Dictionary(); + } + public int ChunkGlobalX (int cx) { return cx; @@ -170,6 +177,7 @@ namespace Substrate return _regionMan.GetRegion(cx, cz); } + #region IEnumerable Members public IEnumerator GetEnumerator () @@ -179,6 +187,7 @@ namespace Substrate #endregion + #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () @@ -187,6 +196,108 @@ namespace Substrate } #endregion + + + public class ChunkEnumerator : IEnumerator + { + private ChunkManager _cm; + + private IEnumerator _enum; + private Region _region; + private ChunkRef _chunk; + + private int _x = 0; + private int _z = -1; + + //protected RegionEnumerator _enum = null; + + public ChunkEnumerator (ChunkManager cm) + { + _cm = cm; + _enum = _cm.GetRegionManager().GetEnumerator(); + _enum.MoveNext(); + _region = _enum.Current; + } + + public virtual bool MoveNext () + { + if (_enum == null) { + return MoveNextInRegion(); + } + else { + while (true) { + if (_x >= ChunkManager.REGION_XLEN) { + if (!_enum.MoveNext()) { + return false; + } + _x = 0; + _z = -1; + _region = _enum.Current; + } + if (MoveNextInRegion()) { + _chunk = _cm.GetChunkRefInRegion(_region, _x, _z); + return true; + } + } + } + } + + protected bool MoveNextInRegion () + { + for (; _x < ChunkManager.REGION_XLEN; _x++) { + for (_z++; _z < ChunkManager.REGION_ZLEN; _z++) { + if (_region.ChunkExists(_x, _z)) { + goto FoundNext; + } + } + _z = -1; + } + + FoundNext: + + return (_x < ChunkManager.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.Current + { + get + { + return Current; + } + } + + public ChunkRef Current + { + get + { + if (_x >= ChunkManager.REGION_XLEN) { + throw new InvalidOperationException(); + } + return _chunk; + } + } + } } public class MissingChunkException : Exception diff --git a/Substrate/SubstrateCS/Source/RegionEnumerator.cs b/Substrate/SubstrateCS/Source/RegionEnumerator.cs deleted file mode 100644 index cf11872..0000000 --- a/Substrate/SubstrateCS/Source/RegionEnumerator.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; -using System.IO; - -namespace Substrate -{ - public class RegionList : IEnumerable - { - private List _regions; - - public RegionList (List regs) - { - _regions = regs; - } - - public RegionList (RegionManager rm) - { - _regions = new List(); - - if (!Directory.Exists(rm.GetRegionPath())) { - throw new DirectoryNotFoundException(); - } - - string[] files = Directory.GetFiles(rm.GetRegionPath()); - _regions.Capacity = files.Length; - - foreach (string file in files) { - try { - Region r = rm.GetRegion(file); - _regions.Add(r); - } - catch (ArgumentException) { - continue; - } - } - } - - IEnumerator IEnumerable.GetEnumerator () { - return (IEnumerator)GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator () - { - return (IEnumerator)GetEnumerator(); - } - - public RegionEnumerator GetEnumerator () - { - return new RegionEnumerator(_regions); - } - } - - public class RegionEnumerator : IEnumerator - { - protected List _regions; - - protected int _pos = -1; - - public RegionEnumerator (List regs) - { - _regions = regs; - } - - public RegionEnumerator (RegionManager rm) - { - _regions = new List(); - - if (!Directory.Exists(rm.GetRegionPath())) { - throw new DirectoryNotFoundException(); - } - - string[] files = Directory.GetFiles(rm.GetRegionPath()); - _regions.Capacity = files.Length; - - foreach (string file in files) { - try { - Region 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; - } - } - - Region IEnumerator.Current - { - get - { - return Current; - } - } - - public Region Current - { - get - { - try { - return _regions[_pos]; - } - catch (IndexOutOfRangeException) { - throw new InvalidOperationException(); - } - } - } - } -} diff --git a/Substrate/SubstrateCS/Source/RegionManager.cs b/Substrate/SubstrateCS/Source/RegionManager.cs index d0ffa2b..912af10 100644 --- a/Substrate/SubstrateCS/Source/RegionManager.cs +++ b/Substrate/SubstrateCS/Source/RegionManager.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Generic; -using System.Text; using System.IO; +using System.Collections; namespace Substrate { - public class RegionManager + public class RegionManager : IEnumerable { protected string _regionPath; @@ -72,5 +72,102 @@ namespace Substrate return true; } + + + #region IEnumerable Members + + public IEnumerator GetEnumerator () + { + return new RegionEnumerator(this); + } + + #endregion + + #region IEnumerable Members + + IEnumerator IEnumerable.GetEnumerator () + { + return new RegionEnumerator(this); + } + + #endregion + + + public class RegionEnumerator : IEnumerator + { + protected List _regions; + + protected int _pos = -1; + + public RegionEnumerator (List regs) + { + _regions = regs; + } + + public RegionEnumerator (RegionManager rm) + { + _regions = new List(); + + if (!Directory.Exists(rm.GetRegionPath())) { + throw new DirectoryNotFoundException(); + } + + string[] files = Directory.GetFiles(rm.GetRegionPath()); + _regions.Capacity = files.Length; + + foreach (string file in files) { + try { + Region 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; + } + } + + Region IEnumerator.Current + { + get + { + return Current; + } + } + + public Region Current + { + get + { + try { + return _regions[_pos]; + } + catch (IndexOutOfRangeException) { + throw new InvalidOperationException(); + } + } + } + } + } } diff --git a/Substrate/SubstrateCS/Substrate.csproj b/Substrate/SubstrateCS/Substrate.csproj index deef722..c748430 100644 --- a/Substrate/SubstrateCS/Substrate.csproj +++ b/Substrate/SubstrateCS/Substrate.csproj @@ -73,7 +73,6 @@ - @@ -118,7 +117,6 @@ - diff --git a/Substrate/SubstrateCS/Substrate.sln b/Substrate/SubstrateCS/Substrate.sln index 73b0b82..eb0b547 100644 --- a/Substrate/SubstrateCS/Substrate.sln +++ b/Substrate/SubstrateCS/Substrate.sln @@ -5,8 +5,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Substrate", "Substrate.cspr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBToolkit", "..\..\NBToolkit\NBToolkit.csproj", "{68207314-C080-4823-97F1-A6623145AA00}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ionic.Zlib", "..\..\Ionic.Zlib\Ionic.Zlib.csproj", "{5A137E43-7E7B-4696-8BFC-844CACAB144B}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,10 +19,6 @@ Global {68207314-C080-4823-97F1-A6623145AA00}.Debug|Any CPU.Build.0 = Debug|Any CPU {68207314-C080-4823-97F1-A6623145AA00}.Release|Any CPU.ActiveCfg = Release|Any CPU {68207314-C080-4823-97F1-A6623145AA00}.Release|Any CPU.Build.0 = Release|Any CPU - {5A137E43-7E7B-4696-8BFC-844CACAB144B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5A137E43-7E7B-4696-8BFC-844CACAB144B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5A137E43-7E7B-4696-8BFC-844CACAB144B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5A137E43-7E7B-4696-8BFC-844CACAB144B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE