diff --git a/NBTModel/Data/Nodes/AbstractFileDataNode.cs b/NBTModel/Data/Nodes/AbstractFileDataNode.cs new file mode 100644 index 0000000..0f8d909 --- /dev/null +++ b/NBTModel/Data/Nodes/AbstractFileDataNode.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; + +namespace NBTExplorer.Model +{ + /// + /// Common functionality for a few nodes that represent files. + /// + public class AbstractFileDataNode : DataNode + { + protected string _path; + + protected AbstractFileDataNode(string path) + { + _path = path; + } + + /// + /// Not only the name of the node but also (in some cases, but not all) the value (or part of the value) that is displayed to the user. + /// Return the full path if this node is the root, otherwise return only the file/folder name + /// + public override string NodeName + { + get => Parent == null ? _path : Path.GetFileName(_path); + } + + /// + /// Just return NodeName, since that is where the display name is build. + /// Side note: I believe NodeName and NodeDisplay should be separated, this would then allow to replace NodePathName with NodeName, but that + /// would require changing a lot of unfamiliar (to me) code. + /// + public override string NodeDisplay + { + get => NodeName; + } + + /// + /// The actual name of the node in a path. Return only the name of the file or folder, not the whole path. + /// + public override string NodePathName + { + get => Path.GetFileName(_path); + } + } +} diff --git a/NBTModel/Data/Nodes/CubicRegionDataNode.cs b/NBTModel/Data/Nodes/CubicRegionDataNode.cs index 0865e44..76d8316 100644 --- a/NBTModel/Data/Nodes/CubicRegionDataNode.cs +++ b/NBTModel/Data/Nodes/CubicRegionDataNode.cs @@ -5,17 +5,14 @@ using NBTModel.Interop; namespace NBTExplorer.Model { - public class CubicRegionDataNode : DataNode - { - private string _path; + public class CubicRegionDataNode : AbstractFileDataNode + { private CubicRegionFile _region; private static Regex _namePattern = new Regex(@"^r2(\.-?\d+){3}\.(mcr|mca)$"); - private CubicRegionDataNode (string path) - { - _path = path; - } + private CubicRegionDataNode (string path) : base(path) + {} public static CubicRegionDataNode TryCreateFrom (string path) { @@ -47,16 +44,6 @@ namespace NBTExplorer.Model get { return true; } } - public override string NodePathName - { - get { return Path.GetFileName(_path); } - } - - public override string NodeDisplay - { - get { return Path.GetFileName(_path); } - } - protected override void ExpandCore () { try { diff --git a/NBTModel/Data/Nodes/DirectoryDataNode.cs b/NBTModel/Data/Nodes/DirectoryDataNode.cs index 4884e03..00d4597 100644 --- a/NBTModel/Data/Nodes/DirectoryDataNode.cs +++ b/NBTModel/Data/Nodes/DirectoryDataNode.cs @@ -4,14 +4,10 @@ using System; namespace NBTExplorer.Model { - public class DirectoryDataNode : DataNode + public class DirectoryDataNode : AbstractFileDataNode { - private string _path; - - public DirectoryDataNode (string path) - { - _path = path; - } + public DirectoryDataNode (string path) : base(path) + {} protected override NodeCapabilities Capabilities { @@ -40,11 +36,6 @@ namespace NBTExplorer.Model } } - public override string NodeDisplay - { - get { return Path.GetFileName(_path); } - } - public override bool HasUnexpandedChildren { get { return !IsExpanded; } diff --git a/NBTModel/Data/Nodes/NbtFileDataNode.cs b/NBTModel/Data/Nodes/NbtFileDataNode.cs index 866d812..f20cac6 100644 --- a/NBTModel/Data/Nodes/NbtFileDataNode.cs +++ b/NBTModel/Data/Nodes/NbtFileDataNode.cs @@ -8,19 +8,17 @@ using NBTModel.Interop; namespace NBTExplorer.Model { - public class NbtFileDataNode : DataNode, IMetaTagContainer + public class NbtFileDataNode : AbstractFileDataNode, IMetaTagContainer { private NbtTree _tree; - private string _path; private CompressionType _compressionType; private CompoundTagContainer _container; private static Regex _namePattern = new Regex(@"\.(dat|nbt|schematic|dat_mcr|dat_old|bpt|rc)$"); - private NbtFileDataNode (string path, CompressionType compressionType) + private NbtFileDataNode (string path, CompressionType compressionType) : base(path) { - _path = path; _compressionType = compressionType; _container = new CompoundTagContainer(new TagNodeCompound()); } @@ -66,16 +64,6 @@ namespace NBTExplorer.Model } } - public override string NodeName - { - get { return Path.GetFileName(_path); } - } - - public override string NodePathName - { - get { return Path.GetFileName(_path); } - } - public override string NodeDisplay { get diff --git a/NBTModel/Data/Nodes/RegionFileDataNode.cs b/NBTModel/Data/Nodes/RegionFileDataNode.cs index 036d918..4cb0c4b 100644 --- a/NBTModel/Data/Nodes/RegionFileDataNode.cs +++ b/NBTModel/Data/Nodes/RegionFileDataNode.cs @@ -7,18 +7,15 @@ using System; namespace NBTExplorer.Model { - public class RegionFileDataNode : DataNode + public class RegionFileDataNode : AbstractFileDataNode { - private string _path; private RegionFile _region; private List _deleteQueue = new List(); private static Regex _namePattern = new Regex(@"^r\.(-?\d+)\.(-?\d+)\.(mcr|mca)$"); - private RegionFileDataNode (string path) - { - _path = path; - } + private RegionFileDataNode (string path) : base(path) + {} public static RegionFileDataNode TryCreateFrom (string path) { @@ -64,16 +61,6 @@ namespace NBTExplorer.Model get { return true; } } - public override string NodePathName - { - get { return Path.GetFileName(_path); } - } - - public override string NodeDisplay - { - get { return Path.GetFileName(_path); } - } - protected override void ExpandCore () { try {