File organization; added wildcard and glob matching to NBTUtil paths

This commit is contained in:
Justin Aquadro 2014-02-20 01:58:40 -05:00
parent c5debf12d4
commit 16ec47a411
33 changed files with 203 additions and 109 deletions

View file

@ -56,9 +56,5 @@ namespace NBTExplorer
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); public static extern IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
} }
} }

View file

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.7.0.0")] [assembly: AssemblyVersion("2.7.1.0")]
[assembly: AssemblyFileVersion("2.7.0.0")] [assembly: AssemblyFileVersion("2.7.1.0")]

View file

@ -1,7 +1,6 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using NBTExplorer.Model;
using System.Diagnostics; using System.Diagnostics;
using NBTExplorer.Model;
namespace NBTExplorer namespace NBTExplorer
{ {

View file

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
using NBTExplorer.Model;
using System.Windows.Forms; using System.Windows.Forms;
using NBTExplorer.Model;
namespace NBTExplorer.Windows namespace NBTExplorer.Windows
{ {

View file

@ -65,6 +65,11 @@ namespace NBTExplorer.Model
internal set { _parent = value; } internal set { _parent = value; }
} }
public DataNode Root
{
get { return (_parent == null) ? this : _parent.Root; }
}
public DataNodeCollection Nodes public DataNodeCollection Nodes
{ {
get { return _children; } get { return _children; }

View file

@ -39,37 +39,38 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CompoundTagContainer.cs" /> <Compile Include="Data\CompoundTagContainer.cs" />
<Compile Include="CubicRegionDataNode.cs" /> <Compile Include="Data\Nodes\CubicRegionDataNode.cs" />
<Compile Include="CubicRegionFile.cs" /> <Compile Include="Data\CubicRegionFile.cs" />
<Compile Include="DataNode.cs" /> <Compile Include="Data\Nodes\DataNode.cs" />
<Compile Include="DataNodeCollection.cs" /> <Compile Include="Data\DataNodeCollection.cs" />
<Compile Include="DirectoryDataNode.cs" /> <Compile Include="Data\Nodes\DirectoryDataNode.cs" />
<Compile Include="FileTypeRegistry.cs" /> <Compile Include="Data\FileTypeRegistry.cs" />
<Compile Include="Interop\FormRegistry.cs" /> <Compile Include="Interop\FormRegistry.cs" />
<Compile Include="Interop\NbtClipboardController.cs" /> <Compile Include="Interop\NbtClipboardController.cs" />
<Compile Include="Interop\NbtClipboardData.cs" /> <Compile Include="Interop\NbtClipboardData.cs" />
<Compile Include="ListTagContainer.cs" /> <Compile Include="Data\ListTagContainer.cs" />
<Compile Include="NbtFileDataNode.cs" /> <Compile Include="Data\Nodes\NbtFileDataNode.cs" />
<Compile Include="NodeCapabilities.cs" /> <Compile Include="Data\Nodes\NodeCapabilities.cs" />
<Compile Include="NbtPath.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegionChunkDataNode.cs" /> <Compile Include="Data\Nodes\RegionChunkDataNode.cs" />
<Compile Include="RegionFileDataNode.cs" /> <Compile Include="Data\Nodes\RegionFileDataNode.cs" />
<Compile Include="Search\SearchRule.cs" /> <Compile Include="Search\SearchRule.cs" />
<Compile Include="TagByteArrayDataNode.cs" /> <Compile Include="Data\Nodes\TagByteArrayDataNode.cs" />
<Compile Include="TagByteDataNode.cs" /> <Compile Include="Data\Nodes\TagByteDataNode.cs" />
<Compile Include="TagCompoundDataNode.cs" /> <Compile Include="Data\Nodes\TagCompoundDataNode.cs" />
<Compile Include="TagContainerInterface.cs" /> <Compile Include="Data\TagContainerInterface.cs" />
<Compile Include="TagDataNode.cs" /> <Compile Include="Data\Nodes\TagDataNode.cs" />
<Compile Include="TagDoubleDataNode.cs" /> <Compile Include="Data\Nodes\TagDoubleDataNode.cs" />
<Compile Include="TagFloatDataNode.cs" /> <Compile Include="Data\Nodes\TagFloatDataNode.cs" />
<Compile Include="TagIntArrayDataNode.cs" /> <Compile Include="Data\Nodes\TagIntArrayDataNode.cs" />
<Compile Include="TagIntDataNode.cs" /> <Compile Include="Data\Nodes\TagIntDataNode.cs" />
<Compile Include="TagKey.cs" /> <Compile Include="Data\TagKey.cs" />
<Compile Include="TagListDataNode.cs" /> <Compile Include="Data\Nodes\TagListDataNode.cs" />
<Compile Include="TagLongDataNode.cs" /> <Compile Include="Data\Nodes\TagLongDataNode.cs" />
<Compile Include="TagShortDataNode.cs" /> <Compile Include="Data\Nodes\TagShortDataNode.cs" />
<Compile Include="TagStringDataNode.cs" /> <Compile Include="Data\Nodes\TagStringDataNode.cs" />
<Compile Include="Utility\SnapshotList.cs" /> <Compile Include="Utility\SnapshotList.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

147
NBTModel/NbtPath.cs Normal file
View file

@ -0,0 +1,147 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace NBTExplorer.Model
{
public class NbtPathEnumerator : IEnumerable<DataNode>
{
private class PathPartDesc
{
public string Name;
public DataNode Node;
}
private string _pathRoot;
private List<string> _pathParts = new List<string>();
public NbtPathEnumerator (string path)
{
_pathRoot = Path.GetPathRoot(path);
_pathParts = new List<string>(path.Substring(_pathRoot.Length).Split('/', '\\'));
if (string.IsNullOrEmpty(_pathRoot))
_pathRoot = Directory.GetCurrentDirectory();
}
public IEnumerator<DataNode> GetEnumerator ()
{
DataNode dataNode = new DirectoryDataNode(_pathRoot);
dataNode.Expand();
foreach (DataNode childNode in EnumerateNodes(dataNode, _pathParts))
yield return childNode;
}
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator();
}
private IEnumerable<DataNode> EnumerateNodes (DataNode containerNode, List<string> nextLevels)
{
containerNode.Expand();
if (nextLevels.Count == 0) {
yield return containerNode;
yield break;
}
if (containerNode.Nodes.Count == 0)
yield break;
string part = nextLevels[0];
List<string> remainingLevels = nextLevels.GetRange(1, nextLevels.Count - 1);
if (part == "*") {
foreach (DataNode childNode in containerNode.Nodes) {
foreach (DataNode grandChildNode in EnumerateNodes(childNode, remainingLevels))
yield return grandChildNode;
}
}
else if (part == "**") {
foreach (DataNode childNode in containerNode.Nodes) {
foreach (DataNode grandChildNode in EnumerateNodes(childNode, remainingLevels))
yield return grandChildNode;
foreach (DataNode grandChildNode in EnumerateNodes(childNode, nextLevels))
yield return grandChildNode;
}
}
else {
foreach (var childNode in containerNode.Nodes) {
if (childNode.NodePathName == part) {
foreach (DataNode grandChildNode in EnumerateNodes(childNode, remainingLevels))
yield return grandChildNode;
}
}
}
}
}
public class NbtPath
{
private class PathPart
{
public string Name;
public DataNode Node;
}
//private List<PathPart> _pathParts = new List<PathPart>();
private List<DataNode> _nodes;
internal NbtPath (List<DataNode> nodes)
{
_nodes = nodes;
}
/*public NbtPath (string path)
{
Path = path;
string[] parts = path.Split('/', '\\');
foreach (var p in parts) {
_pathParts.Add(new PathPart() {
Name = p,
});
}
}
public string Path { get; private set; }
public DataNode RootNode
{
get { return (_pathParts.Count == 0) ? null : _pathParts[0].Node; }
}
public DataNode TargetNode
{
get { return (_pathParts.Count == 0) ? null : _pathParts[_pathParts.Count - 1].Node; }
}
public DataNode Open ()
{
DataNode dataNode = new DirectoryDataNode(Directory.GetCurrentDirectory());
dataNode.Expand();
foreach (var part in _pathParts) {
DataNode match = null;
foreach (var child in dataNode.Nodes) {
if (child.NodePathName == part.Name)
match = child;
}
if (match == null)
return null;
part.Node = match;
dataNode = match;
dataNode.Expand();
}
return dataNode;
}*/
}
}

View file

@ -37,23 +37,30 @@ namespace NBTUtil
if (!_commandTable.ContainsKey(_options.Command)) if (!_commandTable.ContainsKey(_options.Command))
return PrintUsage("Error: No command specified"); return PrintUsage("Error: No command specified");
NbtPath path = new NbtPath(_options.Path);
DataNode targetNode = path.Open();
if (targetNode == null)
return PrintError("Error: Invalid path");
ConsoleOperation op = _commandTable[_options.Command]; ConsoleOperation op = _commandTable[_options.Command];
if (!op.OptionsValid(_options)) if (!op.OptionsValid(_options))
return PrintError("Error: Invalid options specified for the given command"); return PrintError("Error: Invalid options specified for the given command");
if (!op.CanProcess(targetNode))
return PrintError("Error: The given command can't be applied to the given tag");
if (!op.Process(targetNode, _options))
return PrintError("Error: Problem encountered applying the given command");
path.RootNode.Save(); int successCount = 0;
int failCount = 0;
Console.WriteLine("The operation completed successfully"); foreach (var targetNode in new NbtPathEnumerator(_options.Path)) {
if (!op.CanProcess(targetNode)) {
Console.WriteLine(targetNode.NodePath + ": ERROR (invalid command)");
failCount++;
}
if (!op.Process(targetNode, _options)) {
Console.WriteLine(targetNode.NodePath + ": ERROR (apply)");
failCount++;
}
targetNode.Root.Save();
Console.WriteLine(targetNode.NodePath + ": OK");
successCount++;
}
Console.WriteLine("Operation complete. Nodes succeeded: {0} Nodes failed: {1}", successCount, failCount);
return true; return true;
} }
@ -119,63 +126,4 @@ namespace NBTUtil
return false; return false;
} }
} }
class NbtPath
{
private class PathPart
{
public string Name;
public DataNode Node;
}
private List<PathPart> _pathParts = new List<PathPart>();
public NbtPath (string path)
{
Path = path;
string[] parts = path.Split('/', '\\');
foreach (var p in parts) {
_pathParts.Add(new PathPart() {
Name = p,
});
}
}
public string Path { get; private set; }
public DataNode RootNode
{
get { return (_pathParts.Count == 0) ? null : _pathParts[0].Node; }
}
public DataNode TargetNode
{
get { return (_pathParts.Count == 0) ? null : _pathParts[_pathParts.Count - 1].Node; }
}
public DataNode Open ()
{
DataNode dataNode = new DirectoryDataNode(Directory.GetCurrentDirectory());
dataNode.Expand();
foreach (var part in _pathParts) {
DataNode match = null;
foreach (var child in dataNode.Nodes) {
if (child.NodePathName == part.Name)
match = child;
}
if (match == null)
return null;
part.Node = match;
dataNode = match;
dataNode.Expand();
}
return dataNode;
}
}
} }