diff --git a/IconRegistry.cs b/IconRegistry.cs index fad1765..14ec075 100644 --- a/IconRegistry.cs +++ b/IconRegistry.cs @@ -5,7 +5,7 @@ namespace NBTExplorer { public class IconRegistry { - private static Dictionary _iconRegistry; + private Dictionary _iconRegistry; public IconRegistry () { diff --git a/MainForm.cs b/MainForm.cs index 348946a..3d7b0ce 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -119,6 +119,7 @@ namespace NBTExplorer _iconRegistry.Register(typeof(RegionChunkDataNode), 9); _iconRegistry.Register(typeof(DirectoryDataNode), 10); _iconRegistry.Register(typeof(RegionFileDataNode), 11); + _iconRegistry.Register(typeof(CubicRegionDataNode), 11); _iconRegistry.Register(typeof(NbtFileDataNode), 12); _iconRegistry.Register(typeof(TagIntArrayDataNode), 14); } @@ -162,10 +163,16 @@ namespace NBTExplorer AddPathToHistory(Settings.Default.RecentDirectories, path); } else if (File.Exists(path)) { - NbtFileDataNode node = NbtFileDataNode.TryCreateFrom(path); - _nodeTree.Nodes.Add(CreateUnexpandedNode(node)); + DataNode node = null; + foreach (var item in FileTypeRegistry.RegisteredTypes) { + if (item.Value.NamePatternTest(path)) + node = item.Value.NodeCreate(path); + } - AddPathToHistory(Settings.Default.RecentFiles, path); + if (node != null) { + _nodeTree.Nodes.Add(CreateUnexpandedNode(node)); + AddPathToHistory(Settings.Default.RecentFiles, path); + } } } diff --git a/Model/CubicRegionDataNode.cs b/Model/CubicRegionDataNode.cs new file mode 100644 index 0000000..51de6f5 --- /dev/null +++ b/Model/CubicRegionDataNode.cs @@ -0,0 +1,75 @@ +using System.IO; +using System.Text.RegularExpressions; +using System.Windows.Forms; + +namespace NBTExplorer.Model +{ + public class CubicRegionDataNode : DataNode + { + private string _path; + private CubicRegionFile _region; + + private static Regex _namePattern = new Regex(@"^r2(\.-?\d+){3}\.(mcr|mca)$"); + + private CubicRegionDataNode (string path) + { + _path = path; + } + + public static CubicRegionDataNode TryCreateFrom (string path) + { + return new CubicRegionDataNode(path); + } + + public static bool SupportedNamePattern (string path) + { + path = Path.GetFileName(path); + return _namePattern.IsMatch(path); + } + + protected override NodeCapabilities Capabilities + { + get + { + return NodeCapabilities.Search; + } + } + + public override bool HasUnexpandedChildren + { + get { return !IsExpanded; } + } + + public override string NodeDisplay + { + get { return Path.GetFileName(_path); } + } + + protected override void ExpandCore () + { + try { + if (_region == null) + _region = new CubicRegionFile(_path); + + for (int x = 0; x < 32; x++) { + for (int z = 0; z < 32; z++) { + if (_region.HasChunk(x, z)) { + Nodes.Add(new RegionChunkDataNode(_region, x, z)); + } + } + } + } + catch { + MessageBox.Show("Not a valid cubic region file.", "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + + protected override void ReleaseCore () + { + if (_region != null) + _region.Close(); + _region = null; + Nodes.Clear(); + } + } +} diff --git a/Model/CubicRegionFile.cs b/Model/CubicRegionFile.cs new file mode 100644 index 0000000..c54cf7a --- /dev/null +++ b/Model/CubicRegionFile.cs @@ -0,0 +1,24 @@ +using Substrate.Core; + +namespace NBTExplorer.Model +{ + public class CubicRegionFile : RegionFile + { + private const int _sectorBytes = 256; + private static byte[] _emptySector = new byte[_sectorBytes]; + + public CubicRegionFile (string path) + : base(path) + { } + + protected override int SectorBytes + { + get { return _sectorBytes; } + } + + protected override byte[] EmptySector + { + get { return _emptySector; } + } + } +} diff --git a/Model/DirectoryDataNode.cs b/Model/DirectoryDataNode.cs index f385c0b..8d2d272 100644 --- a/Model/DirectoryDataNode.cs +++ b/Model/DirectoryDataNode.cs @@ -36,20 +36,10 @@ namespace NBTExplorer.Model } foreach (string filepath in Directory.GetFiles(_path)) { - string ext = Path.GetExtension(filepath); DataNode node = null; - - switch (ext) { - case ".mcr": - case ".mca": - node = RegionFileDataNode.TryCreateFrom(filepath); - break; - - case ".dat": - case ".nbt": - case ".schematic": - node = NbtFileDataNode.TryCreateFrom(filepath); - break; + foreach (var item in FileTypeRegistry.RegisteredTypes) { + if (item.Value.NamePatternTest(filepath)) + node = item.Value.NodeCreate(filepath); } if (node != null) diff --git a/Model/FileTypeRegistry.cs b/Model/FileTypeRegistry.cs new file mode 100644 index 0000000..2d745f0 --- /dev/null +++ b/Model/FileTypeRegistry.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; + +namespace NBTExplorer.Model +{ + public delegate bool NamePatternTestFunc (string path); + public delegate DataNode NodeCreateFunc (string path); + + public class FileTypeRecord + { + public NamePatternTestFunc NamePatternTest { get; set; } + public NodeCreateFunc NodeCreate { get; set; } + } + + public class FileTypeRegistry + { + private static Dictionary _registry = new Dictionary(); + + public static FileTypeRecord Lookup (Type type) + { + if (_registry.ContainsKey(type)) + return _registry[type]; + else + return null; + } + + public static void Register (Type type, FileTypeRecord record) + { + _registry[type] = record; + } + + public static void Register (FileTypeRecord record) + { + Register(typeof(T), record); + } + + public static IEnumerable> RegisteredTypes + { + get + { + foreach (var item in _registry) + yield return item; + } + } + + static FileTypeRegistry () + { + Register(new FileTypeRecord() { + NamePatternTest = NbtFileDataNode.SupportedNamePattern, + NodeCreate = NbtFileDataNode.TryCreateFrom, + }); + + Register(new FileTypeRecord() { + NamePatternTest = RegionFileDataNode.SupportedNamePattern, + NodeCreate = RegionFileDataNode.TryCreateFrom, + }); + + Register(new FileTypeRecord() { + NamePatternTest = CubicRegionDataNode.SupportedNamePattern, + NodeCreate = CubicRegionDataNode.TryCreateFrom, + }); + } + } +} diff --git a/Model/NbtFileDataNode.cs b/Model/NbtFileDataNode.cs index 177c79a..6d4681f 100644 --- a/Model/NbtFileDataNode.cs +++ b/Model/NbtFileDataNode.cs @@ -1,6 +1,7 @@ using System.IO; using Substrate.Core; using Substrate.Nbt; +using System.Text.RegularExpressions; namespace NBTExplorer.Model { @@ -12,6 +13,8 @@ namespace NBTExplorer.Model private CompoundTagContainer _container; + private static Regex _namePattern = new Regex(@"\.(dat|nbt|schematic)$"); + private NbtFileDataNode (string path, CompressionType compressionType) { _path = path; @@ -42,6 +45,12 @@ namespace NBTExplorer.Model } } + public static bool SupportedNamePattern (string path) + { + path = Path.GetFileName(path); + return _namePattern.IsMatch(path); + } + protected override NodeCapabilities Capabilities { get diff --git a/Model/RegionFileDataNode.cs b/Model/RegionFileDataNode.cs index cfc0215..4fb1a2d 100644 --- a/Model/RegionFileDataNode.cs +++ b/Model/RegionFileDataNode.cs @@ -1,6 +1,7 @@ using System.IO; using System.Windows.Forms; using Substrate.Core; +using System.Text.RegularExpressions; namespace NBTExplorer.Model { @@ -9,6 +10,8 @@ namespace NBTExplorer.Model private string _path; private RegionFile _region; + private static Regex _namePattern = new Regex(@"^r(\.-?\d+){2}\.(mcr|mca)$"); + private RegionFileDataNode (string path) { _path = path; @@ -19,6 +22,12 @@ namespace NBTExplorer.Model return new RegionFileDataNode(path); } + public static bool SupportedNamePattern (string path) + { + path = Path.GetFileName(path); + return _namePattern.IsMatch(path); + } + protected override NodeCapabilities Capabilities { get @@ -58,28 +67,10 @@ namespace NBTExplorer.Model protected override void ReleaseCore () { + if (_region != null) + _region.Close(); _region = null; Nodes.Clear(); } } - - public class RegionFile256 : RegionFile - { - private const int _sectorBytes = 256; - private static byte[] _emptySector = new byte[_sectorBytes]; - - public RegionFile256 (string path) - : base(path) - { } - - protected override int SectorBytes - { - get { return _sectorBytes; } - } - - protected override byte[] EmptySector - { - get { return _emptySector; } - } - } } diff --git a/NBTExplorer.csproj b/NBTExplorer.csproj index 734b01d..3b17f28 100644 --- a/NBTExplorer.csproj +++ b/NBTExplorer.csproj @@ -137,9 +137,12 @@ EditHex.cs + + +