Cubic chunks support, more generic file handling

This commit is contained in:
Justin Aquadro 2012-09-02 23:25:23 -04:00
parent e036dbfacc
commit 36317ba95b
9 changed files with 200 additions and 37 deletions

View file

@ -5,7 +5,7 @@ namespace NBTExplorer
{
public class IconRegistry
{
private static Dictionary<Type, int> _iconRegistry;
private Dictionary<Type, int> _iconRegistry;
public IconRegistry ()
{

View file

@ -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);
}
}
}

View file

@ -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();
}
}
}

24
Model/CubicRegionFile.cs Normal file
View file

@ -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; }
}
}
}

View file

@ -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)

64
Model/FileTypeRegistry.cs Normal file
View file

@ -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<Type, FileTypeRecord> _registry = new Dictionary<Type, FileTypeRecord>();
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<T> (FileTypeRecord record)
{
Register(typeof(T), record);
}
public static IEnumerable<KeyValuePair<Type, FileTypeRecord>> RegisteredTypes
{
get
{
foreach (var item in _registry)
yield return item;
}
}
static FileTypeRegistry ()
{
Register<NbtFileDataNode>(new FileTypeRecord() {
NamePatternTest = NbtFileDataNode.SupportedNamePattern,
NodeCreate = NbtFileDataNode.TryCreateFrom,
});
Register<RegionFileDataNode>(new FileTypeRecord() {
NamePatternTest = RegionFileDataNode.SupportedNamePattern,
NodeCreate = RegionFileDataNode.TryCreateFrom,
});
Register<CubicRegionDataNode>(new FileTypeRecord() {
NamePatternTest = CubicRegionDataNode.SupportedNamePattern,
NodeCreate = CubicRegionDataNode.TryCreateFrom,
});
}
}
}

View file

@ -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

View file

@ -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; }
}
}
}

View file

@ -137,9 +137,12 @@
<DependentUpon>EditHex.cs</DependentUpon>
</Compile>
<Compile Include="Model\CompoundTagContainer.cs" />
<Compile Include="Model\CubicRegionDataNode.cs" />
<Compile Include="Model\CubicRegionFile.cs" />
<Compile Include="Model\DataNode.cs" />
<Compile Include="Model\DataNodeCollection.cs" />
<Compile Include="Model\DirectoryDataNode.cs" />
<Compile Include="Model\FileTypeRegistry.cs" />
<Compile Include="Model\ListTagContainer.cs" />
<Compile Include="Model\NbtFileDataNode.cs" />
<Compile Include="Model\NodeCapabilities.cs" />