Skip to content

Commit

Permalink
File organization; added wildcard and glob matching to NBTUtil paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jaquadro committed Feb 20, 2014
1 parent c5debf1 commit 16ec47a
Show file tree
Hide file tree
Showing 33 changed files with 203 additions and 109 deletions.
4 changes: 0 additions & 4 deletions NBTExplorer/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,5 @@ internal static class NativeInterop

[DllImport("user32.dll")]
public static extern IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);




}
}
4 changes: 2 additions & 2 deletions NBTExplorer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.7.0.0")]
[assembly: AssemblyFileVersion("2.7.0.0")]
[assembly: AssemblyVersion("2.7.1.0")]
[assembly: AssemblyFileVersion("2.7.1.0")]
5 changes: 2 additions & 3 deletions NBTExplorer/SearchWorker.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using NBTExplorer.Model;
using System.Collections.Generic;
using System.Diagnostics;
using NBTExplorer.Model;

namespace NBTExplorer
{
Expand Down
4 changes: 1 addition & 3 deletions NBTExplorer/Windows/SearchStateWin.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using NBTExplorer.Model;
using System.Windows.Forms;
using NBTExplorer.Model;

namespace NBTExplorer.Windows
{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions NBTModel/DataNode.cs → NBTModel/Data/Nodes/DataNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public DataNode Parent
internal set { _parent = value; }
}

public DataNode Root
{
get { return (_parent == null) ? this : _parent.Root; }
}

public DataNodeCollection Nodes
{
get { return _children; }
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
53 changes: 27 additions & 26 deletions NBTModel/NBTModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,38 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CompoundTagContainer.cs" />
<Compile Include="CubicRegionDataNode.cs" />
<Compile Include="CubicRegionFile.cs" />
<Compile Include="DataNode.cs" />
<Compile Include="DataNodeCollection.cs" />
<Compile Include="DirectoryDataNode.cs" />
<Compile Include="FileTypeRegistry.cs" />
<Compile Include="Data\CompoundTagContainer.cs" />
<Compile Include="Data\Nodes\CubicRegionDataNode.cs" />
<Compile Include="Data\CubicRegionFile.cs" />
<Compile Include="Data\Nodes\DataNode.cs" />
<Compile Include="Data\DataNodeCollection.cs" />
<Compile Include="Data\Nodes\DirectoryDataNode.cs" />
<Compile Include="Data\FileTypeRegistry.cs" />
<Compile Include="Interop\FormRegistry.cs" />
<Compile Include="Interop\NbtClipboardController.cs" />
<Compile Include="Interop\NbtClipboardData.cs" />
<Compile Include="ListTagContainer.cs" />
<Compile Include="NbtFileDataNode.cs" />
<Compile Include="NodeCapabilities.cs" />
<Compile Include="Data\ListTagContainer.cs" />
<Compile Include="Data\Nodes\NbtFileDataNode.cs" />
<Compile Include="Data\Nodes\NodeCapabilities.cs" />
<Compile Include="NbtPath.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegionChunkDataNode.cs" />
<Compile Include="RegionFileDataNode.cs" />
<Compile Include="Data\Nodes\RegionChunkDataNode.cs" />
<Compile Include="Data\Nodes\RegionFileDataNode.cs" />
<Compile Include="Search\SearchRule.cs" />
<Compile Include="TagByteArrayDataNode.cs" />
<Compile Include="TagByteDataNode.cs" />
<Compile Include="TagCompoundDataNode.cs" />
<Compile Include="TagContainerInterface.cs" />
<Compile Include="TagDataNode.cs" />
<Compile Include="TagDoubleDataNode.cs" />
<Compile Include="TagFloatDataNode.cs" />
<Compile Include="TagIntArrayDataNode.cs" />
<Compile Include="TagIntDataNode.cs" />
<Compile Include="TagKey.cs" />
<Compile Include="TagListDataNode.cs" />
<Compile Include="TagLongDataNode.cs" />
<Compile Include="TagShortDataNode.cs" />
<Compile Include="TagStringDataNode.cs" />
<Compile Include="Data\Nodes\TagByteArrayDataNode.cs" />
<Compile Include="Data\Nodes\TagByteDataNode.cs" />
<Compile Include="Data\Nodes\TagCompoundDataNode.cs" />
<Compile Include="Data\TagContainerInterface.cs" />
<Compile Include="Data\Nodes\TagDataNode.cs" />
<Compile Include="Data\Nodes\TagDoubleDataNode.cs" />
<Compile Include="Data\Nodes\TagFloatDataNode.cs" />
<Compile Include="Data\Nodes\TagIntArrayDataNode.cs" />
<Compile Include="Data\Nodes\TagIntDataNode.cs" />
<Compile Include="Data\TagKey.cs" />
<Compile Include="Data\Nodes\TagListDataNode.cs" />
<Compile Include="Data\Nodes\TagLongDataNode.cs" />
<Compile Include="Data\Nodes\TagShortDataNode.cs" />
<Compile Include="Data\Nodes\TagStringDataNode.cs" />
<Compile Include="Utility\SnapshotList.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
147 changes: 147 additions & 0 deletions NBTModel/NbtPath.cs
Original file line number Diff line number Diff line change
@@ -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;
}*/
}
}
90 changes: 19 additions & 71 deletions NBTUtil/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,30 @@ public bool Run (string[] args)
if (!_commandTable.ContainsKey(_options.Command))
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];
if (!op.OptionsValid(_options))
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;

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

Console.WriteLine("The operation completed successfully");
targetNode.Root.Save();

Console.WriteLine(targetNode.NodePath + ": OK");
successCount++;
}

Console.WriteLine("Operation complete. Nodes succeeded: {0} Nodes failed: {1}", successCount, failCount);

return true;
}
Expand Down Expand Up @@ -119,63 +126,4 @@ private bool PrintError (string error)
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;
}
}
}

0 comments on commit 16ec47a

Please sign in to comment.