From d885a7d7679a264d879e9dbe8632c2ad6407cb74 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 4 Dec 2011 00:09:49 -0500 Subject: [PATCH] Edit, rename, add -- mostly feature complete --- EditValue.Designer.cs | 62 +++++ EditValue.cs | 181 +++++++++++++ EditValue.resx | 120 +++++++++ Form1.Designer.cs | 172 ++++++++++--- Form1.cs | 426 +++++++++++++++++++++++++++++-- Form1.resx | 33 +-- NBTExplorer.csproj | 24 ++ Properties/Resources.Designer.cs | 35 +++ Properties/Resources.resx | 19 +- Resources/disk--pencil.png | Bin 0 -> 677 bytes Resources/disk.png | Bin 0 -> 507 bytes Resources/door.png | Bin 0 -> 503 bytes Resources/information-frame.png | Bin 0 -> 898 bytes Resources/question-frame.png | Bin 0 -> 925 bytes 14 files changed, 992 insertions(+), 80 deletions(-) create mode 100644 EditValue.Designer.cs create mode 100644 EditValue.cs create mode 100644 EditValue.resx create mode 100644 Resources/disk--pencil.png create mode 100644 Resources/disk.png create mode 100644 Resources/door.png create mode 100644 Resources/information-frame.png create mode 100644 Resources/question-frame.png diff --git a/EditValue.Designer.cs b/EditValue.Designer.cs new file mode 100644 index 0000000..7c96167 --- /dev/null +++ b/EditValue.Designer.cs @@ -0,0 +1,62 @@ +namespace NBTPlus +{ + partial class EditValue + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose (bool disposing) + { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent () + { + this.textBox1 = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // textBox1 + // + this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.textBox1.Location = new System.Drawing.Point(0, 0); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(246, 20); + this.textBox1.TabIndex = 0; + // + // EditValue + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(246, 20); + this.Controls.Add(this.textBox1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.KeyPreview = true; + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "EditValue"; + this.Text = "Edit Value..."; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox textBox1; + } +} \ No newline at end of file diff --git a/EditValue.cs b/EditValue.cs new file mode 100644 index 0000000..aa6be80 --- /dev/null +++ b/EditValue.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using Substrate.Nbt; + +namespace NBTPlus +{ + public enum EditValueType + { + Name, + Value, + } + + public partial class EditValue : Form + { + private string _name; + private TagNode _tag; + private EditValueType _type; + + private List _invalidNames = new List(); + + public EditValue (TagNode tag) + { + InitializeComponent(); + + _type = EditValueType.Value; + _tag = tag; + + if (tag == null) { + DialogResult = DialogResult.Abort; + Close(); + return; + } + + SetTitle(); + + textBox1.Text = _tag.ToString(); + } + + public EditValue (string name) + { + InitializeComponent(); + + _type = EditValueType.Name; + _name = name ?? ""; + + SetTitle(); + + textBox1.Text = _name.ToString(); + } + + public string NodeName + { + get { return _name; } + } + + public TagNode NodeTag + { + get { return _tag; } + } + + public List InvalidNames + { + get { return _invalidNames; } + } + + private void SetTitle () + { + switch (_type) { + case EditValueType.Name: + base.Text = "Edit Name..."; + break; + + case EditValueType.Value: + base.Text = "Edit Value..."; + break; + } + } + + private bool ValidateInput () + { + switch (_type) { + case EditValueType.Name: + return ValidateNameInput(); + case EditValueType.Value: + return ValidateValueInput(); + } + + return false; + } + + private bool ValidateNameInput () + { + string text = textBox1.Text.Trim(); + if (String.IsNullOrWhiteSpace(text)) { + MessageBox.Show("You must provide a nonempty name."); + return false; + } + + if (_invalidNames.Contains(text)) { + MessageBox.Show("Duplicate name provided."); + return false; + } + + _name = textBox1.Text.Trim(); + return true; + } + + private bool ValidateValueInput () + { + try { + switch (_tag.GetTagType()) { + case TagType.TAG_BYTE: + _tag.ToTagByte().Data = byte.Parse(textBox1.Text); + break; + + case TagType.TAG_SHORT: + _tag.ToTagShort().Data = short.Parse(textBox1.Text); + break; + + case TagType.TAG_INT: + _tag.ToTagInt().Data = int.Parse(textBox1.Text); + break; + + case TagType.TAG_LONG: + _tag.ToTagLong().Data = long.Parse(textBox1.Text); + break; + + case TagType.TAG_FLOAT: + _tag.ToTagFloat().Data = float.Parse(textBox1.Text); + break; + + case TagType.TAG_DOUBLE: + _tag.ToTagDouble().Data = double.Parse(textBox1.Text); + break; + + case TagType.TAG_STRING: + _tag.ToTagString().Data = textBox1.Text; + break; + } + } + catch (FormatException) { + MessageBox.Show("The value is formatted incorrectly for the given type."); + return false; + } + catch (OverflowException) { + MessageBox.Show("The value is outside the acceptable range for the given type."); + return false; + } + catch { + return false; + } + + return true; + } + + protected override void OnKeyDown (KeyEventArgs e) + { + switch (e.KeyCode) { + case Keys.Escape: + DialogResult = DialogResult.Cancel; + Close(); + return; + + case Keys.Enter: + if (ValidateInput()) { + DialogResult = DialogResult.OK; + Close(); + } + return; + } + + base.OnKeyDown(e); + } + } +} diff --git a/EditValue.resx b/EditValue.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/EditValue.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 6eaf46b..c6211af 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -31,23 +31,23 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.openFolderToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator(); + this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator(); this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.searchToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripContainer1 = new System.Windows.Forms.ToolStripContainer(); this._nodeTree = new System.Windows.Forms.TreeView(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); + this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); + this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); + this._nodeContainerContext = new System.Windows.Forms.ContextMenuStrip(this.components); this._buttonOpen = new System.Windows.Forms.ToolStripButton(); this._buttonSave = new System.Windows.Forms.ToolStripButton(); - this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this._buttonRename = new System.Windows.Forms.ToolStripButton(); this._buttonEdit = new System.Windows.Forms.ToolStripButton(); this._buttonDelete = new System.Windows.Forms.ToolStripButton(); - this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); this._buttonAddTagByte = new System.Windows.Forms.ToolStripButton(); this._buttonAddTagShort = new System.Windows.Forms.ToolStripButton(); this._buttonAddTagInt = new System.Windows.Forms.ToolStripButton(); @@ -58,11 +58,20 @@ this._buttonAddTagString = new System.Windows.Forms.ToolStripButton(); this._buttonAddTagList = new System.Windows.Forms.ToolStripButton(); this._buttonAddTagCompound = new System.Windows.Forms.ToolStripButton(); + this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFolderToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveItemAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openSubTreeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveItemToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.toolStripContainer1.ContentPanel.SuspendLayout(); this.toolStripContainer1.TopToolStripPanel.SuspendLayout(); this.toolStripContainer1.SuspendLayout(); this.toolStrip1.SuspendLayout(); + this._nodeContainerContext.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 @@ -82,29 +91,25 @@ // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.openToolStripMenuItem, - this.openFolderToolStripMenuItem}); + this.openFolderToolStripMenuItem, + this.toolStripSeparator3, + this.saveToolStripMenuItem, + this.saveItemAsToolStripMenuItem, + this.toolStripSeparator4, + this.exitToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); this.fileToolStripMenuItem.Text = "&File"; // - // openToolStripMenuItem + // toolStripSeparator3 // - this.openToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.folder_open_document; - this.openToolStripMenuItem.Name = "openToolStripMenuItem"; - this.openToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); - this.openToolStripMenuItem.Size = new System.Drawing.Size(223, 22); - this.openToolStripMenuItem.Text = "&Open..."; - this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click); + this.toolStripSeparator3.Name = "toolStripSeparator3"; + this.toolStripSeparator3.Size = new System.Drawing.Size(220, 6); // - // openFolderToolStripMenuItem + // toolStripSeparator4 // - this.openFolderToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.folder_open; - this.openFolderToolStripMenuItem.Name = "openFolderToolStripMenuItem"; - this.openFolderToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift) - | System.Windows.Forms.Keys.O))); - this.openFolderToolStripMenuItem.Size = new System.Drawing.Size(223, 22); - this.openFolderToolStripMenuItem.Text = "Open Folder..."; - this.openFolderToolStripMenuItem.Click += new System.EventHandler(this.openFolderToolStripMenuItem_Click); + this.toolStripSeparator4.Name = "toolStripSeparator4"; + this.toolStripSeparator4.Size = new System.Drawing.Size(220, 6); // // editToolStripMenuItem // @@ -126,13 +131,6 @@ this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20); this.helpToolStripMenuItem.Text = "&Help"; // - // aboutToolStripMenuItem - // - this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; - this.aboutToolStripMenuItem.Size = new System.Drawing.Size(107, 22); - this.aboutToolStripMenuItem.Text = "&About"; - this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); - // // toolStripContainer1 // // @@ -208,6 +206,24 @@ this.toolStrip1.Stretch = true; this.toolStrip1.TabIndex = 0; // + // toolStripSeparator1 + // + this.toolStripSeparator1.Name = "toolStripSeparator1"; + this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25); + // + // toolStripSeparator2 + // + this.toolStripSeparator2.Name = "toolStripSeparator2"; + this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25); + // + // _nodeContainerContext + // + this._nodeContainerContext.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.openSubTreeToolStripMenuItem, + this.saveItemToolStripMenuItem}); + this._nodeContainerContext.Name = "contextMenuStrip1"; + this._nodeContainerContext.Size = new System.Drawing.Size(180, 48); + // // _buttonOpen // this._buttonOpen.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; @@ -228,11 +244,6 @@ this._buttonSave.Text = "Save All Modified Tags"; this._buttonSave.Click += new System.EventHandler(this._buttonSave_Click); // - // toolStripSeparator1 - // - this.toolStripSeparator1.Name = "toolStripSeparator1"; - this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25); - // // _buttonRename // this._buttonRename.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; @@ -241,6 +252,7 @@ this._buttonRename.Name = "_buttonRename"; this._buttonRename.Size = new System.Drawing.Size(23, 22); this._buttonRename.Text = "Rename Tag"; + this._buttonRename.Click += new System.EventHandler(this._buttonRename_Click); // // _buttonEdit // @@ -250,6 +262,7 @@ this._buttonEdit.Name = "_buttonEdit"; this._buttonEdit.Size = new System.Drawing.Size(23, 22); this._buttonEdit.Text = "Edit Tag Value"; + this._buttonEdit.Click += new System.EventHandler(this._buttonEdit_Click); // // _buttonDelete // @@ -259,12 +272,7 @@ this._buttonDelete.Name = "_buttonDelete"; this._buttonDelete.Size = new System.Drawing.Size(23, 22); this._buttonDelete.Text = "Delete Tag"; - this._buttonDelete.Click += new System.EventHandler(this.toolStripButton5_Click); - // - // toolStripSeparator2 - // - this.toolStripSeparator2.Name = "toolStripSeparator2"; - this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25); + this._buttonDelete.Click += new System.EventHandler(this._buttonDelete_Click); // // _buttonAddTagByte // @@ -274,6 +282,7 @@ this._buttonAddTagByte.Name = "_buttonAddTagByte"; this._buttonAddTagByte.Size = new System.Drawing.Size(23, 22); this._buttonAddTagByte.Text = "Add Byte Tag"; + this._buttonAddTagByte.Click += new System.EventHandler(this._buttonAddTagByte_Click); // // _buttonAddTagShort // @@ -283,6 +292,7 @@ this._buttonAddTagShort.Name = "_buttonAddTagShort"; this._buttonAddTagShort.Size = new System.Drawing.Size(23, 22); this._buttonAddTagShort.Text = "Add Short Tag"; + this._buttonAddTagShort.Click += new System.EventHandler(this._buttonAddTagShort_Click); // // _buttonAddTagInt // @@ -292,6 +302,7 @@ this._buttonAddTagInt.Name = "_buttonAddTagInt"; this._buttonAddTagInt.Size = new System.Drawing.Size(23, 22); this._buttonAddTagInt.Text = "Add Int Tag"; + this._buttonAddTagInt.Click += new System.EventHandler(this._buttonAddTagInt_Click); // // _buttonAddTagLong // @@ -301,6 +312,7 @@ this._buttonAddTagLong.Name = "_buttonAddTagLong"; this._buttonAddTagLong.Size = new System.Drawing.Size(23, 22); this._buttonAddTagLong.Text = "Add Long Tag"; + this._buttonAddTagLong.Click += new System.EventHandler(this._buttonAddTagLong_Click); // // _buttonAddTagFloat // @@ -310,6 +322,7 @@ this._buttonAddTagFloat.Name = "_buttonAddTagFloat"; this._buttonAddTagFloat.Size = new System.Drawing.Size(23, 22); this._buttonAddTagFloat.Text = "Add Float Tag"; + this._buttonAddTagFloat.Click += new System.EventHandler(this._buttonAddTagFloat_Click); // // _buttonAddTagDouble // @@ -319,6 +332,7 @@ this._buttonAddTagDouble.Name = "_buttonAddTagDouble"; this._buttonAddTagDouble.Size = new System.Drawing.Size(23, 22); this._buttonAddTagDouble.Text = "Add Double Tag"; + this._buttonAddTagDouble.Click += new System.EventHandler(this._buttonAddTagDouble_Click); // // _buttonAddTagByteArray // @@ -328,6 +342,7 @@ this._buttonAddTagByteArray.Name = "_buttonAddTagByteArray"; this._buttonAddTagByteArray.Size = new System.Drawing.Size(23, 22); this._buttonAddTagByteArray.Text = "Add Byte Array Tag"; + this._buttonAddTagByteArray.Click += new System.EventHandler(this._buttonAddTagByteArray_Click); // // _buttonAddTagString // @@ -337,6 +352,7 @@ this._buttonAddTagString.Name = "_buttonAddTagString"; this._buttonAddTagString.Size = new System.Drawing.Size(23, 22); this._buttonAddTagString.Text = "Add String Tag"; + this._buttonAddTagString.Click += new System.EventHandler(this._buttonAddTagString_Click); // // _buttonAddTagList // @@ -346,6 +362,7 @@ this._buttonAddTagList.Name = "_buttonAddTagList"; this._buttonAddTagList.Size = new System.Drawing.Size(23, 22); this._buttonAddTagList.Text = "Add List Tag"; + this._buttonAddTagList.Click += new System.EventHandler(this._buttonAddTagList_Click); // // _buttonAddTagCompound // @@ -355,6 +372,74 @@ this._buttonAddTagCompound.Name = "_buttonAddTagCompound"; this._buttonAddTagCompound.Size = new System.Drawing.Size(23, 22); this._buttonAddTagCompound.Text = "Add Compound Tag"; + this._buttonAddTagCompound.Click += new System.EventHandler(this._buttonAddTagCompound_Click); + // + // openToolStripMenuItem + // + this.openToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.folder_open_document; + this.openToolStripMenuItem.Name = "openToolStripMenuItem"; + this.openToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); + this.openToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.openToolStripMenuItem.Text = "&Open..."; + this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click); + // + // openFolderToolStripMenuItem + // + this.openFolderToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.folder_open; + this.openFolderToolStripMenuItem.Name = "openFolderToolStripMenuItem"; + this.openFolderToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift) + | System.Windows.Forms.Keys.O))); + this.openFolderToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.openFolderToolStripMenuItem.Text = "Open &Folder..."; + this.openFolderToolStripMenuItem.Click += new System.EventHandler(this.openFolderToolStripMenuItem_Click); + // + // saveToolStripMenuItem + // + this.saveToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.disk; + this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + this.saveToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S))); + this.saveToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.saveToolStripMenuItem.Text = "&Save"; + // + // saveItemAsToolStripMenuItem + // + this.saveItemAsToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.disk__pencil; + this.saveItemAsToolStripMenuItem.Name = "saveItemAsToolStripMenuItem"; + this.saveItemAsToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift) + | System.Windows.Forms.Keys.S))); + this.saveItemAsToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.saveItemAsToolStripMenuItem.Text = "Save Item &As..."; + // + // exitToolStripMenuItem + // + this.exitToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.door; + this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; + this.exitToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.F4))); + this.exitToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.exitToolStripMenuItem.Text = "E&xit"; + // + // aboutToolStripMenuItem + // + this.aboutToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.information_frame; + this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; + this.aboutToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F1; + this.aboutToolStripMenuItem.Size = new System.Drawing.Size(126, 22); + this.aboutToolStripMenuItem.Text = "&About"; + this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); + // + // openSubTreeToolStripMenuItem + // + this.openSubTreeToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.folder_open; + this.openSubTreeToolStripMenuItem.Name = "openSubTreeToolStripMenuItem"; + this.openSubTreeToolStripMenuItem.Size = new System.Drawing.Size(179, 22); + this.openSubTreeToolStripMenuItem.Text = "Open Node As Root"; + // + // saveItemToolStripMenuItem + // + this.saveItemToolStripMenuItem.Image = global::NBTPlus.Properties.Resources.disk; + this.saveItemToolStripMenuItem.Name = "saveItemToolStripMenuItem"; + this.saveItemToolStripMenuItem.Size = new System.Drawing.Size(179, 22); + this.saveItemToolStripMenuItem.Text = "Save Node"; // // Form1 // @@ -375,6 +460,7 @@ this.toolStripContainer1.PerformLayout(); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); + this._nodeContainerContext.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -411,6 +497,14 @@ private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem openFolderToolStripMenuItem; + private System.Windows.Forms.ContextMenuStrip _nodeContainerContext; + private System.Windows.Forms.ToolStripMenuItem openSubTreeToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveItemToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator3; + private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveItemAsToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator4; + private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; } } diff --git a/Form1.cs b/Form1.cs index d89cc0d..fac5d71 100644 --- a/Form1.cs +++ b/Form1.cs @@ -23,6 +23,8 @@ namespace NBTPlus _nodeTree.BeforeExpand += NodeExpand; _nodeTree.AfterCollapse += NodeCollapse; _nodeTree.AfterSelect += NodeSelected; + _nodeTree.NodeMouseClick += NodeClicked; + _nodeTree.NodeMouseDoubleClick += NodeDoubleClicked; string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); path = Path.Combine(path, ".minecraft"); @@ -72,12 +74,26 @@ namespace NBTPlus private TreeNode NodeFromTag (TagNode tag, string name) { - string text = String.IsNullOrEmpty(name) ? "" : name + ": "; - TreeNode node = new TreeNode(); node.ImageIndex = _tagIconIndex[tag.GetTagType()]; node.SelectedImageIndex = _tagIconIndex[tag.GetTagType()]; node.Tag = tag; + node.Text = GetNodeText(name, tag); + + if (tag.GetTagType() == TagType.TAG_LIST) { + PopulateNodeFromTag(node, tag.ToTagList()); + } + else if (tag.GetTagType() == TagType.TAG_COMPOUND) { + PopulateNodeFromTag(node, tag.ToTagCompound()); + } + + return node; + } + + private string GetTagNodeText (TagNode tag) + { + if (tag == null) + return null; switch (tag.GetTagType()) { case TagType.TAG_BYTE: @@ -87,25 +103,46 @@ namespace NBTPlus case TagType.TAG_FLOAT: case TagType.TAG_DOUBLE: case TagType.TAG_STRING: - node.Text = text + tag.ToString(); - break; + return tag.ToString(); case TagType.TAG_BYTE_ARRAY: - node.Text = text + tag.ToTagByteArray().Length + " bytes"; - break; + return tag.ToTagByteArray().Length + " bytes"; case TagType.TAG_LIST: - node.Text = text + tag.ToTagList().Count + " entries"; - PopulateNodeFromTag(node, tag.ToTagList()); - break; + return tag.ToTagList().Count + " entries"; case TagType.TAG_COMPOUND: - node.Text = text + tag.ToTagCompound().Count + " entries"; - PopulateNodeFromTag(node, tag.ToTagCompound()); - break; + return tag.ToTagCompound().Count + " entries"; } - return node; + return null; + } + + private string GetNodeText (string name, string value) + { + name = String.IsNullOrEmpty(name) ? "" : name + ": "; + value = value ?? ""; + + return name + value; + } + + private string GetNodeText (string name, TagNode tag) + { + return GetNodeText(name, GetTagNodeText(tag)); + } + + private string GetNodeText (TreeNode node) + { + return GetNodeText(GetTagNodeName(node), GetTagNodeText(node)); + } + + private string GetTagNodeText (TreeNode node) + { + TagNode tag = GetTagNode(node); + if (tag == null) + return null; + + return GetTagNodeText(tag); } private void PopulateNodeFromTag (TreeNode node, TagNodeList list) @@ -156,7 +193,9 @@ namespace NBTPlus for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { if (rf.HasChunk(x, y)) { - node.Nodes.Add(CreateLazyChunk(rf, x, y)); + TreeNode child = CreateLazyChunk(rf, x, y); + node.Nodes.Add(child); + LinkDataNodeParent(child, node); } } } @@ -331,14 +370,32 @@ namespace NBTPlus UpdateToolbar(); } + private void NodeClicked (object sender, TreeNodeMouseClickEventArgs e) + { + + } + + private void NodeDoubleClicked (object sender, TreeNodeMouseClickEventArgs e) + { + EditNodeValue(_nodeTree.SelectedNode); + } + private void UpdateToolbar () { TreeNode node = _nodeTree.SelectedNode; TagNode tag = node.Tag as TagNode; - _buttonRename.Enabled = tag != null; - _buttonDelete.Enabled = tag != null; + if (tag == null && node.Tag is NbtDataNode) { + NbtDataNode data = node.Tag as NbtDataNode; + if (data.Tree != null) + tag = data.Tree.Root; + } + + _buttonRename.Enabled = tag != null && node.Tag is TagNode; + _buttonDelete.Enabled = tag != null && node.Tag is TagNode; _buttonEdit.Enabled = tag != null + && node.Tag is TagNode + && tag.GetTagType() != TagType.TAG_BYTE_ARRAY && tag.GetTagType() != TagType.TAG_COMPOUND && tag.GetTagType() != TagType.TAG_LIST; @@ -427,7 +484,9 @@ namespace NBTPlus public void TryLoadFile (TreeNodeCollection parent, string path) { if (Path.GetExtension(path) == ".mcr") { - parent.Add(CreateLazyRegion(path)); + TreeNode node = CreateLazyRegion(path); + parent.Add(node); + LinkDataNodeParent(node, node.Parent); return; } @@ -501,6 +560,9 @@ namespace NBTPlus if (node.Tag is NbtFileData) { SaveNbtFileNode(node); } + else if (node.Tag is RegionChunkData) { + SaveRegionChunkNode(node); + } } private void SaveNbtFileNode (TreeNode node) @@ -516,6 +578,18 @@ namespace NBTPlus data.Modified = false; } + private void SaveRegionChunkNode (TreeNode node) + { + RegionChunkData data = node.Tag as RegionChunkData; + if (data == null || !data.Modified) + return; + + using (Stream str = data.Region.GetChunkDataOutputStream(data.X, data.Z)) { + data.Tree.WriteTo(str); + } + data.Modified = false; + } + private void aboutToolStripMenuItem_Click (object sender, EventArgs e) { new About().Show(); @@ -540,25 +614,43 @@ namespace NBTPlus OpenFile(); } - private void toolStripButton5_Click (object sender, EventArgs e) + private TreeNode BaseNode (TreeNode node) { - if (_nodeTree.SelectedNode == null) - return; + if (node == null) + return null; - TreeNode baseNode = _nodeTree.SelectedNode; + TreeNode baseNode = node; while (baseNode.Tag == null || !(baseNode.Tag is DataNode)) { baseNode = baseNode.Parent; } + return baseNode; + } + + #region Tag Deletion + + private void DeleteNode (TreeNode node) + { + TreeNode baseNode = BaseNode(node); + // Can only delete internal NBT nodes - if (baseNode == null || baseNode == _nodeTree.SelectedNode) + if (baseNode == null || baseNode == node) return; (baseNode.Tag as DataNode).Modified = true; - DeleteNodeNbtTag(_nodeTree.SelectedNode); + DeleteNodeNbtTag(node); - _nodeTree.SelectedNode.Remove(); + if (node.Parent != null) { + node.Parent.Text = GetNodeText(node.Parent); + } + + node.Remove(); + } + + private void _buttonDelete_Click (object sender, EventArgs e) + { + DeleteNode(_nodeTree.SelectedNode); } private void DeleteNodeNbtTag (TreeNode node) @@ -604,10 +696,296 @@ namespace NBTPlus } } + #endregion + private void _buttonSave_Click (object sender, EventArgs e) { SaveAll(); } + + private TagNode GetTagNode (TreeNode node) + { + if (node == null) + return null; + + if (node.Tag is TagNode) { + return node.Tag as TagNode; + } + else if (node.Tag is NbtDataNode) { + NbtDataNode data = node.Tag as NbtDataNode; + if (data == null) + return null; + + return data.Tree.Root; + } + + return null; + } + + private TagNode GetParentTagNode (TreeNode node) + { + if (GetTagNode(node) == null) + return null; + + return GetTagNode(node.Parent); + } + + private string GetTagNodeName (TreeNode node) + { + TagNode tag = GetTagNode(node); + if (tag == null) + return null; + + TagNode parentTag = GetTagNode(node.Parent); + if (parentTag == null) + return null; + + if (parentTag.GetTagType() != TagType.TAG_COMPOUND) + return null; + + foreach (KeyValuePair sub in parentTag.ToTagCompound()) { + if (sub.Value == tag) + return sub.Key; + } + + return null; + } + + private bool SetTagNodeName (TreeNode node, string name) + { + TagNode tag = GetTagNode(node); + if (tag == null) + return false; + + TagNode parentTag = GetTagNode(node.Parent); + if (parentTag == null) + return false; + + if (parentTag.GetTagType() != TagType.TAG_COMPOUND) + return false; + + if (parentTag.ToTagCompound().ContainsKey(name)) + return false; + + string oldName = null; + foreach (KeyValuePair sub in parentTag.ToTagCompound()) { + if (sub.Value == tag) { + oldName = sub.Key; + break; + } + } + + parentTag.ToTagCompound().Remove(oldName); + parentTag.ToTagCompound().Add(name, tag); + + return true; + } + + private void EditNodeValue (TreeNode node) + { + if (node == null) + return; + + TagNode tag = GetTagNode(node); + if (tag == null) + return; + + if (tag.GetTagType() == TagType.TAG_BYTE_ARRAY || + tag.GetTagType() == TagType.TAG_LIST || + tag.GetTagType() == TagType.TAG_COMPOUND) + return; + + EditValue form = new EditValue(tag); + if (form.ShowDialog() == DialogResult.OK) { + TreeNode baseNode = BaseNode(node); + if (baseNode != null) { + (baseNode.Tag as DataNode).Modified = true; + } + + node.Text = GetNodeText(node); + } + } + + private void _buttonEdit_Click (object sender, EventArgs e) + { + EditNodeValue(_nodeTree.SelectedNode); + } + + private void EditNodeName (TreeNode node) + { + if (node == null) + return; + + TagNode tag = GetTagNode(node); + if (tag == null) + return; + + string name = GetTagNodeName(node); + if (name == null) + return; + + EditValue form = new EditValue(name); + + TagNode parentTag = GetParentTagNode(node); + foreach (string key in parentTag.ToTagCompound().Keys) { + form.InvalidNames.Add(key); + } + + if (form.ShowDialog() == DialogResult.OK) { + TreeNode baseNode = BaseNode(node); + if (baseNode != null) { + (baseNode.Tag as DataNode).Modified = true; + } + + SetTagNodeName(node, form.NodeName); + node.Text = GetNodeText(node); + } + } + + private void _buttonRename_Click (object sender, EventArgs e) + { + EditNodeName(_nodeTree.SelectedNode); + } + + private void AddTagToNode (TreeNode node, TagType type) + { + TagNode tag = GetTagNode(node); + if (tag == null) + return; + + if (tag.GetTagType() != TagType.TAG_COMPOUND && + tag.GetTagType() != TagType.TAG_LIST) + return; + + if (tag.GetTagType() == TagType.TAG_LIST && + tag.ToTagList().ValueType != type && + tag.ToTagList().Count > 0) + return; + + TagNode newNode = null; + switch (type) { + case TagType.TAG_BYTE: + newNode = new TagNodeByte(); + break; + case TagType.TAG_SHORT: + newNode = new TagNodeShort(); + break; + case TagType.TAG_INT: + newNode = new TagNodeInt(); + break; + case TagType.TAG_LONG: + newNode = new TagNodeLong(); + break; + case TagType.TAG_FLOAT: + newNode = new TagNodeFloat(); + break; + case TagType.TAG_DOUBLE: + newNode = new TagNodeDouble(); + break; + case TagType.TAG_BYTE_ARRAY: + newNode = new TagNodeByteArray(); + break; + case TagType.TAG_STRING: + newNode = new TagNodeString(); + break; + case TagType.TAG_LIST: + newNode = new TagNodeList(TagType.TAG_BYTE); + break; + case TagType.TAG_COMPOUND: + newNode = new TagNodeCompound(); + break; + } + + if (tag is TagNodeCompound) { + TagNodeCompound ctag = tag as TagNodeCompound; + + EditValue form = new EditValue(""); + foreach (string key in ctag.Keys) { + form.InvalidNames.Add(key); + } + + if (form.ShowDialog() != DialogResult.OK) + return; + + ctag.Add(form.NodeName, newNode); + + TreeNode tnode = NodeFromTag(newNode, form.NodeName); + node.Nodes.Add(tnode); + + _nodeTree.SelectedNode = tnode; + tnode.Expand(); + } + else if (tag is TagNodeList) { + TagNodeList ltag = tag as TagNodeList; + if (ltag.ValueType != type) + ltag.ChangeValueType(type); + + ltag.Add(newNode); + + TreeNode tnode = NodeFromTag(newNode); + node.Nodes.Add(tnode); + + _nodeTree.SelectedNode = tnode; + tnode.Expand(); + } + + node.Text = GetNodeText(node); + + TreeNode baseNode = BaseNode(node); + if (baseNode != null) { + (baseNode.Tag as DataNode).Modified = true; + } + } + + private void _buttonAddTagByte_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_BYTE); + } + + private void _buttonAddTagShort_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_SHORT); + } + + private void _buttonAddTagInt_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_INT); + } + + private void _buttonAddTagLong_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_LONG); + } + + private void _buttonAddTagFloat_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_FLOAT); + } + + private void _buttonAddTagDouble_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_DOUBLE); + } + + private void _buttonAddTagByteArray_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_BYTE_ARRAY); + } + + private void _buttonAddTagString_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_STRING); + } + + private void _buttonAddTagList_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_LIST); + } + + private void _buttonAddTagCompound_Click (object sender, EventArgs e) + { + AddTagToNode(_nodeTree.SelectedNode, TagType.TAG_COMPOUND); + } } public class TagKey : IComparable diff --git a/Form1.resx b/Form1.resx index 92a1c2a..2ac3d75 100644 --- a/Form1.resx +++ b/Form1.resx @@ -128,7 +128,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADW - LgAAAk1TRnQBSQFMAgEBDgEAASgBAAEoAQABEAEAARABAAT/ARkBAAj/AUIBTQE2BwABNgMAASgDAAFA + LgAAAk1TRnQBSQFMAgEBDgEAAXABAAFwAQABEAEAARABAAT/ARkBAAj/AUIBTQE2BwABNgMAASgDAAFA AwABQAMAAQEBAAEYBgABMBIAA/wD+SH4A/kD/AMAA/0D+SH4A/kD/WMAAZYBqQG8AVwBh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AAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFySURBVDhPY2CgBthfz8CyP4kl40AiczcM19sxnEHGyHIg tSA9cLv3xrErn6rWXPp+T+f/d7s7wHh2hikKhomD1IDUgvTADdiVwOx+c4r3EWQDlpc4/ofhJYV2cINB - akBqQXrgBuyIZ8m9vyz7wbPNDf9heEuj338YXlftARcHyYPUgvTADdgawzz157Xlf//fXfUfhu+uKgcy + akBqQXrgBuyIZ8m9vyz7wbPNDf9heEuj338YXlftARcHyYPUgvTADdgawzz1w9nlf//fXfUfhu+uKgcy IfjMzDS4OEgepBakB27A5ijmGT8vzf//+cRUON7dEfofhjc3+KLIgdSC9MAN2BDFPPvbudn/PxyZCMcg zavKXcCGLCt2QJEDqQXpgRuwOoJ5/qeT0/6/2d8DxyDNID6MRpYDqQXpgRuwPIx58btD/f9f7monCoPU gvTADVgSwrjsFTDun25tIgqD1IL0wA2YH8C4+smWxv8PN9QShUFqQXrgBkzxZNzxYF31//trq4jCILUg - PTAD5FKNGee1uTAeJQWD9AANkAMZwg/E4kAsRSIG6QHppQwAAIR13ahrUo3PAAAAAElFTkSuQmCC + PTAD5FKNGee1uTAeJQWD9AANkAMZwg/E4kAsRSIG6QHppQwAACRX3YQSNn5eAAAAAElFTkSuQmCC + + 347, 17 + \ No newline at end of file diff --git a/NBTExplorer.csproj b/NBTExplorer.csproj index 053496f..4c8310f 100644 --- a/NBTExplorer.csproj +++ b/NBTExplorer.csproj @@ -58,6 +58,12 @@ About.cs + + Form + + + EditValue.cs + Form @@ -69,6 +75,9 @@ About.cs + + EditValue.cs + Form1.cs @@ -98,6 +107,21 @@ + + + + + + + + + + + + + + +