Refactoring project

This commit is contained in:
Justin Aquadro 2012-08-26 17:33:54 -04:00
parent 6781be7b2f
commit ecb4ac898c
27 changed files with 294 additions and 255 deletions

118
DataNode.cs Normal file
View file

@ -0,0 +1,118 @@
using Substrate.Core;
using Substrate.Nbt;
namespace NBTExplorer
{
public class DataNode
{
public DataNode ()
{
}
public DataNode (DataNode parent)
{
Parent = parent;
}
public DataNode Parent { get; set; }
private bool _modified;
public bool Modified
{
get { return _modified; }
set
{
if (value && Parent != null) {
Parent.Modified = value;
}
_modified = value;
}
}
public bool Expanded { get; set; }
}
public class NbtDataNode : DataNode
{
public NbtDataNode ()
{
}
public NbtDataNode (DataNode parent)
: base(parent)
{
}
public NbtTree Tree { get; set; }
}
public class RegionChunkData : NbtDataNode
{
public RegionChunkData (RegionFile file, int x, int z)
: this(null, file, x, z)
{
}
public RegionChunkData (DataNode parent, RegionFile file, int x, int z)
: base(parent)
{
Region = file;
X = x;
Z = z;
}
public RegionFile Region { get; private set; }
public int X { get; private set; }
public int Z { get; private set; }
}
public class RegionData : DataNode
{
public RegionData (string path)
: this(null, path)
{
}
public RegionData (DataNode parent, string path)
: base(parent)
{
Path = path;
}
public string Path { get; private set; }
}
public class NbtFileData : NbtDataNode
{
public NbtFileData (string path, CompressionType cztype)
: this(null, path, cztype)
{
}
public NbtFileData (DataNode parent, string path, CompressionType cztype)
: base(parent)
{
Path = path;
CompressionType = cztype;
}
public string Path { get; private set; }
public CompressionType CompressionType { get; private set; }
}
public class DirectoryData : DataNode
{
public DirectoryData (string path)
: this(null, path)
{
}
public DirectoryData (DataNode parent, string path)
: base(parent)
{
Path = path;
}
public string Path { get; private set; }
}
}

213
Form1.cs
View file

@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Substrate;
using Substrate.Nbt;
using System.IO;
using Substrate.Core;
using Substrate.Nbt;
namespace NBTExplorer
{
@ -923,7 +920,7 @@ namespace NBTExplorer
}
}
else if (tag.GetTagType() == TagType.TAG_BYTE_ARRAY) {
HexEditor form = new HexEditor(GetTagNodeName(node), tag.ToTagByteArray().Data);
HexEditor form = new HexEditor(GetTagNodeName(node), tag.ToTagByteArray().Data, 1);
if (form.ShowDialog() == DialogResult.OK && form.Modified) {
TreeNode baseNode = BaseNode(node);
if (baseNode != null) {
@ -941,7 +938,7 @@ namespace NBTExplorer
Array.Copy(buf, 0, data, 4 * i, 4);
}
HexEditor form = new HexEditor(GetTagNodeName(node), data);
HexEditor form = new HexEditor(GetTagNodeName(node), data, 4);
if (form.ShowDialog() == DialogResult.OK && form.Modified) {
TreeNode baseNode = BaseNode(node);
if (baseNode != null) {
@ -1432,206 +1429,4 @@ namespace NBTExplorer
PasteNode();
}
}
public class TagKey : IComparable<TagKey>
{
public TagKey (string name, TagType type)
{
Name = name;
TagType = type;
}
public string Name { get; set; }
public TagType TagType { get; set; }
#region IComparer<TagKey> Members
public int Compare(TagKey x, TagKey y)
{
int typeDiff = (int)x.TagType - (int)y.TagType;
if (typeDiff != 0)
return typeDiff;
return String.Compare(x.Name, y.Name, true);
}
#endregion
#region IComparable<TagKey> Members
public int CompareTo (TagKey other)
{
return Compare(this, other);
}
#endregion
}
public class DataNode
{
public DataNode ()
{
}
public DataNode (DataNode parent)
{
Parent = parent;
}
public DataNode Parent { get; set; }
private bool _modified;
public bool Modified
{
get { return _modified; }
set
{
if (value && Parent != null) {
Parent.Modified = value;
}
_modified = value;
}
}
public bool Expanded { get; set; }
}
public class NbtDataNode : DataNode
{
public NbtDataNode ()
{
}
public NbtDataNode (DataNode parent)
: base(parent)
{
}
public NbtTree Tree { get; set; }
}
public class RegionChunkData : NbtDataNode
{
public RegionChunkData (RegionFile file, int x, int z)
: this(null, file, x, z)
{
}
public RegionChunkData (DataNode parent, RegionFile file, int x, int z)
: base(parent)
{
Region = file;
X = x;
Z = z;
}
public RegionFile Region { get; private set; }
public int X { get; private set; }
public int Z { get; private set; }
}
public class RegionData : DataNode
{
public RegionData (string path)
: this(null, path)
{
}
public RegionData (DataNode parent, string path)
: base(parent)
{
Path = path;
}
public string Path { get; private set; }
}
public class NbtFileData : NbtDataNode
{
public NbtFileData (string path, CompressionType cztype)
: this(null, path, cztype)
{
}
public NbtFileData (DataNode parent, string path, CompressionType cztype)
: base(parent)
{
Path = path;
CompressionType = cztype;
}
public string Path { get; private set; }
public CompressionType CompressionType { get; private set; }
}
public class DirectoryData : DataNode
{
public DirectoryData (string path)
: this(null, path)
{
}
public DirectoryData (DataNode parent, string path)
: base(parent)
{
Path = path;
}
public string Path { get; private set; }
}
[Serializable]
public class NbtClipboardData
{
public string Name;
private byte[] _data;
[NonSerialized]
public TagNode Node;
public NbtClipboardData (String name, TagNode node)
{
Name = name;
TagNodeCompound root = new TagNodeCompound();
root.Add("root", node);
NbtTree tree = new NbtTree(root);
using (MemoryStream ms = new MemoryStream()) {
tree.WriteTo(ms);
_data = new byte[ms.Length];
Array.Copy(ms.GetBuffer(), _data, ms.Length);
}
}
public static bool ContainsData
{
get { return Clipboard.ContainsData(typeof(NbtClipboardData).FullName); }
}
public void CopyToClipboard ()
{
Clipboard.SetData(typeof(NbtClipboardData).FullName, this);
}
public static NbtClipboardData CopyFromClipboard ()
{
NbtClipboardData clip = Clipboard.GetData(typeof(NbtClipboardData).FullName) as NbtClipboardData;
if (clip == null)
return null;
NbtTree tree = new NbtTree();
using (MemoryStream ms = new MemoryStream(clip._data)) {
tree.ReadFrom(ms);
}
TagNodeCompound root = tree.Root;
if (root == null || !root.ContainsKey("root"))
return null;
clip.Node = root["root"];
return clip;
}
}
}

View file

@ -49,40 +49,41 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="About.cs">
<Compile Include="DataNode.cs" />
<Compile Include="Forms\About.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="About.Designer.cs">
<Compile Include="Forms\About.Designer.cs">
<DependentUpon>About.cs</DependentUpon>
</Compile>
<Compile Include="EditName.cs">
<Compile Include="Forms\EditName.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="EditName.Designer.cs">
<Compile Include="Forms\EditName.Designer.cs">
<DependentUpon>EditName.cs</DependentUpon>
</Compile>
<Compile Include="EditString.cs">
<Compile Include="Forms\EditString.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="EditString.Designer.cs">
<Compile Include="Forms\EditString.Designer.cs">
<DependentUpon>EditString.cs</DependentUpon>
</Compile>
<Compile Include="CreateNode.cs">
<Compile Include="Forms\CreateNode.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="CreateNode.Designer.cs">
<Compile Include="Forms\CreateNode.Designer.cs">
<DependentUpon>CreateNode.cs</DependentUpon>
</Compile>
<Compile Include="EditValue.cs">
<Compile Include="Forms\EditValue.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="EditValue.Designer.cs">
<Compile Include="Forms\EditValue.Designer.cs">
<DependentUpon>EditValue.cs</DependentUpon>
</Compile>
<Compile Include="Find.cs">
<Compile Include="Forms\Find.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Find.Designer.cs">
<Compile Include="Forms\Find.Designer.cs">
<DependentUpon>Find.cs</DependentUpon>
</Compile>
<Compile Include="Form1.cs">
@ -91,38 +92,60 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="HexEditor.cs">
<Compile Include="Forms\EditHex.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="HexEditor.Designer.cs">
<DependentUpon>HexEditor.cs</DependentUpon>
<Compile Include="Forms\EditHex.Designer.cs">
<DependentUpon>EditHex.cs</DependentUpon>
</Compile>
<Compile Include="NbtClipboardData.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="About.resx">
<Compile Include="TagKey.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\BuiltInContextMenu.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Vendor\Be.Windows.Forms.HexBox\ByteCharConverters.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\ByteCollection.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\BytePositionInfo.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\DataBlock.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\DataMap.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\DynamicByteProvider.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\DynamicFileByteProvider.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\FileByteProvider.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\FileDataBlock.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\HexBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Vendor\Be.Windows.Forms.HexBox\HexCasing.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\IByteProvider.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\MemoryDataBlock.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\NativeMethods.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\Util.cs" />
<EmbeddedResource Include="Forms\About.resx">
<DependentUpon>About.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="EditName.resx">
<EmbeddedResource Include="Forms\EditName.resx">
<DependentUpon>EditName.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="EditString.resx">
<EmbeddedResource Include="Forms\EditString.resx">
<DependentUpon>EditString.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="CreateNode.resx">
<EmbeddedResource Include="Forms\CreateNode.resx">
<DependentUpon>CreateNode.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="EditValue.resx">
<EmbeddedResource Include="Forms\EditValue.resx">
<DependentUpon>EditValue.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Find.resx">
<EmbeddedResource Include="Forms\Find.resx">
<DependentUpon>Find.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="HexEditor.resx">
<DependentUpon>HexEditor.cs</DependentUpon>
<EmbeddedResource Include="Forms\EditHex.resx">
<DependentUpon>EditHex.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
@ -134,16 +157,14 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<EmbeddedResource Include="Vendor\Be.Windows.Forms.HexBox\HexBox.resx">
<DependentUpon>HexBox.cs</DependentUpon>
</EmbeddedResource>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Content Include="dead_bush.ico" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="Vendor\Be.Windows.Forms.HexBox\Be.Windows.Forms.HexBox.csproj">
<Project>{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}</Project>
<Name>Be.Windows.Forms.HexBox</Name>
</ProjectReference>
<Content Include="Vendor\Be.Windows.Forms.HexBox\HexBox.bmp" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View file

@ -1,10 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual C# Express 2010
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBTExplorer", "NBTExplorer.csproj", "{8A458245-8176-4599-95CD-3CA39F2435CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Be.Windows.Forms.HexBox", "Vendor\Be.Windows.Forms.HexBox\Be.Windows.Forms.HexBox.csproj", "{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -25,16 +23,6 @@ Global
{8A458245-8176-4599-95CD-3CA39F2435CE}.Release|Mixed Platforms.Build.0 = Release|x86
{8A458245-8176-4599-95CD-3CA39F2435CE}.Release|x86.ActiveCfg = Release|x86
{8A458245-8176-4599-95CD-3CA39F2435CE}.Release|x86.Build.0 = Release|x86
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Debug|x86.ActiveCfg = Debug|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Release|Any CPU.Build.0 = Release|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{26C5F25F-B450-4CAF-AD8B-B8D11AE73457}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

62
NbtClipboardData.cs Normal file
View file

@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Windows.Forms;
using Substrate.Nbt;
namespace NBTExplorer
{
[Serializable]
public class NbtClipboardData
{
public string Name;
private byte[] _data;
[NonSerialized]
public TagNode Node;
public NbtClipboardData (String name, TagNode node)
{
Name = name;
TagNodeCompound root = new TagNodeCompound();
root.Add("root", node);
NbtTree tree = new NbtTree(root);
using (MemoryStream ms = new MemoryStream()) {
tree.WriteTo(ms);
_data = new byte[ms.Length];
Array.Copy(ms.GetBuffer(), _data, ms.Length);
}
}
public static bool ContainsData
{
get { return Clipboard.ContainsData(typeof(NbtClipboardData).FullName); }
}
public void CopyToClipboard ()
{
Clipboard.SetData(typeof(NbtClipboardData).FullName, this);
}
public static NbtClipboardData CopyFromClipboard ()
{
NbtClipboardData clip = Clipboard.GetData(typeof(NbtClipboardData).FullName) as NbtClipboardData;
if (clip == null)
return null;
NbtTree tree = new NbtTree();
using (MemoryStream ms = new MemoryStream(clip._data)) {
tree.ReadFrom(ms);
}
TagNodeCompound root = tree.Root;
if (root == null || !root.ContainsKey("root"))
return null;
clip.Node = root["root"];
return clip;
}
}
}

39
TagKey.cs Normal file
View file

@ -0,0 +1,39 @@
using System;
using Substrate.Nbt;
namespace NBTExplorer
{
public class TagKey : IComparable<TagKey>
{
public TagKey (string name, TagType type)
{
Name = name;
TagType = type;
}
public string Name { get; set; }
public TagType TagType { get; set; }
#region IComparer<TagKey> Members
public int Compare (TagKey x, TagKey y)
{
int typeDiff = (int)x.TagType - (int)y.TagType;
if (typeDiff != 0)
return typeDiff;
return String.Compare(x.Name, y.Name, true);
}
#endregion
#region IComparable<TagKey> Members
public int CompareTo (TagKey other)
{
return Compare(this, other);
}
#endregion
}
}

View file

@ -32,6 +32,7 @@
this._curPositionLabel = new System.Windows.Forms.ToolStripStatusLabel();
this._buttonCancel = new System.Windows.Forms.Button();
this._buttonOK = new System.Windows.Forms.Button();
this._curElementLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
@ -54,7 +55,8 @@
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this._curPositionLabel});
this._curPositionLabel,
this._curElementLabel});
this.statusStrip1.Location = new System.Drawing.Point(0, 333);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(516, 22);
@ -90,6 +92,13 @@
this._buttonOK.UseVisualStyleBackColor = true;
this._buttonOK.Click += new System.EventHandler(this._buttonOK_Click);
//
// _curElementLabel
//
this._curElementLabel.Name = "_curElementLabel";
this._curElementLabel.Size = new System.Drawing.Size(59, 17);
this._curElementLabel.Text = "Element 0";
this._curElementLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// HexEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -116,5 +125,6 @@
private System.Windows.Forms.ToolStripStatusLabel _curPositionLabel;
private System.Windows.Forms.Button _buttonCancel;
private System.Windows.Forms.Button _buttonOK;
private System.Windows.Forms.ToolStripStatusLabel _curElementLabel;
}
}

View file

@ -11,6 +11,7 @@ namespace NBTExplorer
{
public partial class HexEditor : Form
{
private int _bytesPerElem;
private byte[] _data;
private bool _modified;
DynamicByteProvider _byteProvider;
@ -27,12 +28,16 @@ namespace NBTExplorer
}
}
public HexEditor (string tagName, byte[] data)
public HexEditor (string tagName, byte[] data, int bytesPerElem)
{
InitializeComponent();
this.Text = "Editing: " + tagName;
_bytesPerElem = bytesPerElem;
_curPositionLabel.Text = "0x0000";
_curElementLabel.Text = "Element 0";
_data = new byte[data.Length];
Array.Copy(data, _data, data.Length);
@ -75,9 +80,10 @@ namespace NBTExplorer
private void UpdatePosition ()
{
long pos = (hexBox1.CurrentLine - 1) * hexBox1.HorizontalByteCount + hexBox1.CurrentPositionInLine;
long pos = (hexBox1.CurrentLine - 1) * hexBox1.HorizontalByteCount + hexBox1.CurrentPositionInLine - 1;
_curPositionLabel.Text = pos.ToString();
_curPositionLabel.Text = "0x" + pos.ToString("X4");
_curElementLabel.Text = "Element " + pos / _bytesPerElem;
}
private void Apply ()