Raw byte array import/export

This commit is contained in:
Justin Aquadro 2013-05-25 03:30:52 -04:00
parent 119ba72091
commit e319c519dc
3 changed files with 137 additions and 41 deletions

View file

@ -27,33 +27,19 @@
/// </summary>
private void InitializeComponent ()
{
this.hexBox1 = new Be.Windows.Forms.HexBox();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this._curPositionLabel = new System.Windows.Forms.ToolStripStatusLabel();
this._curElementLabel = new System.Windows.Forms.ToolStripStatusLabel();
this._buttonCancel = new System.Windows.Forms.Button();
this._buttonOK = new System.Windows.Forms.Button();
this._space = new System.Windows.Forms.ToolStripStatusLabel();
this._insertStateLabel = new System.Windows.Forms.ToolStripStatusLabel();
this._buttonCancel = new System.Windows.Forms.Button();
this._buttonOK = new System.Windows.Forms.Button();
this.hexBox1 = new Be.Windows.Forms.HexBox();
this._buttonImport = new System.Windows.Forms.Button();
this._buttonExport = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// hexBox1
//
this.hexBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.hexBox1.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.hexBox1.LineInfoForeColor = System.Drawing.Color.Empty;
this.hexBox1.LineInfoVisible = true;
this.hexBox1.Location = new System.Drawing.Point(12, 12);
this.hexBox1.Name = "hexBox1";
this.hexBox1.ReadOnly = true;
this.hexBox1.ShadowSelectionColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(60)))), ((int)(((byte)(188)))), ((int)(((byte)(255)))));
this.hexBox1.Size = new System.Drawing.Size(492, 289);
this.hexBox1.TabIndex = 0;
this.hexBox1.VScrollBarVisible = true;
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
@ -81,6 +67,18 @@
this._curElementLabel.Text = "Element 0";
this._curElementLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// _space
//
this._space.Name = "_space";
this._space.Size = new System.Drawing.Size(284, 17);
this._space.Spring = true;
//
// _insertStateLabel
//
this._insertStateLabel.Name = "_insertStateLabel";
this._insertStateLabel.Size = new System.Drawing.Size(58, 17);
this._insertStateLabel.Text = "Overwrite";
//
// _buttonCancel
//
this._buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -103,17 +101,41 @@
this._buttonOK.UseVisualStyleBackColor = true;
this._buttonOK.Click += new System.EventHandler(this._buttonOK_Click);
//
// _space
// hexBox1
//
this._space.Name = "_space";
this._space.Size = new System.Drawing.Size(253, 17);
this._space.Spring = true;
this.hexBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.hexBox1.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.hexBox1.LineInfoForeColor = System.Drawing.Color.Empty;
this.hexBox1.LineInfoVisible = true;
this.hexBox1.Location = new System.Drawing.Point(12, 12);
this.hexBox1.Name = "hexBox1";
this.hexBox1.ReadOnly = true;
this.hexBox1.ShadowSelectionColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(60)))), ((int)(((byte)(188)))), ((int)(((byte)(255)))));
this.hexBox1.Size = new System.Drawing.Size(492, 289);
this.hexBox1.TabIndex = 0;
this.hexBox1.VScrollBarVisible = true;
//
// _insertStateLabel
// _buttonImport
//
this._insertStateLabel.Name = "_insertStateLabel";
this._insertStateLabel.Size = new System.Drawing.Size(58, 17);
this._insertStateLabel.Text = "Overwrite";
this._buttonImport.Location = new System.Drawing.Point(12, 307);
this._buttonImport.Name = "_buttonImport";
this._buttonImport.Size = new System.Drawing.Size(75, 23);
this._buttonImport.TabIndex = 14;
this._buttonImport.Text = "Import";
this._buttonImport.UseVisualStyleBackColor = true;
this._buttonImport.Click += new System.EventHandler(this._buttonImport_Click);
//
// _buttonExport
//
this._buttonExport.Location = new System.Drawing.Point(93, 307);
this._buttonExport.Name = "_buttonExport";
this._buttonExport.Size = new System.Drawing.Size(75, 23);
this._buttonExport.TabIndex = 15;
this._buttonExport.Text = "Export";
this._buttonExport.UseVisualStyleBackColor = true;
this._buttonExport.Click += new System.EventHandler(this._buttonExport_Click);
//
// HexEditor
//
@ -121,6 +143,8 @@
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this._buttonCancel;
this.ClientSize = new System.Drawing.Size(516, 355);
this.Controls.Add(this._buttonExport);
this.Controls.Add(this._buttonImport);
this.Controls.Add(this._buttonCancel);
this.Controls.Add(this._buttonOK);
this.Controls.Add(this.statusStrip1);
@ -144,5 +168,7 @@
private System.Windows.Forms.ToolStripStatusLabel _curElementLabel;
private System.Windows.Forms.ToolStripStatusLabel _space;
private System.Windows.Forms.ToolStripStatusLabel _insertStateLabel;
private System.Windows.Forms.Button _buttonImport;
private System.Windows.Forms.Button _buttonExport;
}
}

View file

@ -1,6 +1,8 @@
using System;
using System.Windows.Forms;
using Be.Windows.Forms;
using System.IO;
using System.Collections.Generic;
namespace NBTExplorer.Windows
{
@ -103,9 +105,75 @@ namespace NBTExplorer.Windows
Close();
}
private void ImportRaw (string path)
{
try {
using (FileStream fstr = File.OpenRead(path)) {
_data = new byte[fstr.Length];
fstr.Read(_data, 0, (int)fstr.Length);
_byteProvider = new DynamicByteProvider(_data);
_byteProvider.Changed += (o, e) => { _modified = true; };
hexBox1.ByteProvider = _byteProvider;
_modified = true;
}
}
catch (Exception e) {
MessageBox.Show("Failed to import data from \"" + path + "\"\n\nException: " + e.Message);
}
}
private void ExportRaw (string path)
{
try {
using (FileStream fstr = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None)) {
byte[] data = _byteProvider.Bytes.ToArray();
fstr.Write(data, 0, data.Length);
}
}
catch (Exception e) {
MessageBox.Show("Failed to export data to \"" + path + "\"\n\nException: " + e.Message);
}
}
private void _buttonOK_Click (object sender, EventArgs e)
{
Apply();
}
private void _buttonImport_Click (object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog()) {
ofd.RestoreDirectory = true;
ofd.Multiselect = false;
ofd.Filter = "Binary Data|*|Text List (*.txt)|*.txt";
ofd.FilterIndex = 0;
if (ofd.ShowDialog() == DialogResult.OK) {
if (Path.GetExtension(ofd.FileName) == "txt")
return;
else
ImportRaw(ofd.FileName);
}
}
}
private void _buttonExport_Click (object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog()) {
sfd.RestoreDirectory = true;
sfd.Filter = "Binary Data|*|Text List (*.txt)|*.txt";
sfd.FilterIndex = 0;
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() == DialogResult.OK) {
if (Path.GetExtension(sfd.FileName) == "txt")
return;
else
ExportRaw(sfd.FileName);
}
}
}
}
}

View file

@ -135,16 +135,17 @@ namespace NBTExplorer.Windows
if (!ConfirmAction("Open new file anyway?"))
return;
OpenFileDialog ofd = new OpenFileDialog();
ofd.RestoreDirectory = true;
ofd.Multiselect = true;
ofd.Filter = "All Files|*|NBT Files (*.dat, *.schematic)|*.dat;*.nbt;*.schematic|Region Files (*.mca, *.mcr)|*.mca;*.mcr";
ofd.FilterIndex = 0;
using (OpenFileDialog ofd = new OpenFileDialog()) {
ofd.RestoreDirectory = true;
ofd.Multiselect = true;
ofd.Filter = "All Files|*|NBT Files (*.dat, *.schematic)|*.dat;*.nbt;*.schematic|Region Files (*.mca, *.mcr)|*.mca;*.mcr";
ofd.FilterIndex = 0;
if (ofd.ShowDialog() == DialogResult.OK) {
OpenPaths(ofd.FileNames);
if (ofd.ShowDialog() == DialogResult.OK) {
OpenPaths(ofd.FileNames);
}
}
UpdateUI();
}
@ -153,13 +154,14 @@ namespace NBTExplorer.Windows
if (!ConfirmAction("Open new folder anyway?"))
return;
FolderBrowserDialog ofd = new FolderBrowserDialog();
if (_openFolderPath != null)
ofd.SelectedPath = _openFolderPath;
using (FolderBrowserDialog ofd = new FolderBrowserDialog()) {
if (_openFolderPath != null)
ofd.SelectedPath = _openFolderPath;
if (ofd.ShowDialog() == DialogResult.OK) {
_openFolderPath = ofd.SelectedPath;
OpenPaths(new string[] { ofd.SelectedPath });
if (ofd.ShowDialog() == DialogResult.OK) {
_openFolderPath = ofd.SelectedPath;
OpenPaths(new string[] { ofd.SelectedPath });
}
}
UpdateUI();