2012-03-13 05:19:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Be.Windows.Forms;
|
2013-05-25 07:30:52 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
2012-03-13 05:19:48 +00:00
|
|
|
|
|
2012-11-05 03:32:18 +00:00
|
|
|
|
namespace NBTExplorer.Windows
|
2012-03-13 05:19:48 +00:00
|
|
|
|
{
|
|
|
|
|
public partial class HexEditor : Form
|
|
|
|
|
{
|
2012-08-26 21:33:54 +00:00
|
|
|
|
private int _bytesPerElem;
|
2012-08-24 03:58:11 +00:00
|
|
|
|
private byte[] _data;
|
|
|
|
|
private bool _modified;
|
|
|
|
|
DynamicByteProvider _byteProvider;
|
|
|
|
|
|
|
|
|
|
private class FixedByteProvider : DynamicByteProvider
|
|
|
|
|
{
|
|
|
|
|
public FixedByteProvider (byte[] data)
|
|
|
|
|
: base(data)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public override bool SupportsInsertBytes ()
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-26 21:33:54 +00:00
|
|
|
|
public HexEditor (string tagName, byte[] data, int bytesPerElem)
|
2012-03-13 05:19:48 +00:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
2012-08-24 03:58:11 +00:00
|
|
|
|
this.Text = "Editing: " + tagName;
|
|
|
|
|
|
2012-08-26 21:33:54 +00:00
|
|
|
|
_bytesPerElem = bytesPerElem;
|
|
|
|
|
_curPositionLabel.Text = "0x0000";
|
|
|
|
|
_curElementLabel.Text = "Element 0";
|
|
|
|
|
|
2012-08-24 03:58:11 +00:00
|
|
|
|
_data = new byte[data.Length];
|
|
|
|
|
Array.Copy(data, _data, data.Length);
|
2012-03-13 05:19:48 +00:00
|
|
|
|
|
2013-02-06 04:35:01 +00:00
|
|
|
|
_byteProvider = new DynamicByteProvider(_data);
|
2012-08-24 03:58:11 +00:00
|
|
|
|
_byteProvider.Changed += (o, e) => { _modified = true; };
|
|
|
|
|
|
|
|
|
|
hexBox1.ByteProvider = _byteProvider;
|
2012-03-13 05:19:48 +00:00
|
|
|
|
|
|
|
|
|
hexBox1.HorizontalByteCountChanged += HexBox_HorizontalByteCountChanged;
|
|
|
|
|
hexBox1.CurrentLineChanged += HexBox_CurrentLineChanged;
|
|
|
|
|
hexBox1.CurrentPositionInLineChanged += HexBox_CurrentPositionInLineChanged;
|
2013-02-06 04:35:01 +00:00
|
|
|
|
hexBox1.InsertActiveChanged += HexBox_InsertActiveChanged;
|
2012-08-24 03:58:11 +00:00
|
|
|
|
|
|
|
|
|
hexBox1.ReadOnly = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] Data
|
|
|
|
|
{
|
|
|
|
|
get { return _data; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Modified
|
|
|
|
|
{
|
|
|
|
|
get { return _modified; }
|
2012-03-13 05:19:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HexBox_HorizontalByteCountChanged (object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdatePosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HexBox_CurrentLineChanged (object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdatePosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HexBox_CurrentPositionInLineChanged (object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdatePosition();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-06 04:35:01 +00:00
|
|
|
|
private void HexBox_InsertActiveChanged (object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (hexBox1.InsertActive)
|
|
|
|
|
_insertStateLabel.Text = "Insert";
|
|
|
|
|
else
|
|
|
|
|
_insertStateLabel.Text = "Overwrite";
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 05:19:48 +00:00
|
|
|
|
private void UpdatePosition ()
|
|
|
|
|
{
|
2012-08-26 21:33:54 +00:00
|
|
|
|
long pos = (hexBox1.CurrentLine - 1) * hexBox1.HorizontalByteCount + hexBox1.CurrentPositionInLine - 1;
|
2012-03-13 05:19:48 +00:00
|
|
|
|
|
2012-08-26 21:33:54 +00:00
|
|
|
|
_curPositionLabel.Text = "0x" + pos.ToString("X4");
|
|
|
|
|
_curElementLabel.Text = "Element " + pos / _bytesPerElem;
|
2012-03-13 05:19:48 +00:00
|
|
|
|
}
|
2012-08-24 03:58:11 +00:00
|
|
|
|
|
|
|
|
|
private void Apply ()
|
|
|
|
|
{
|
2013-02-06 04:35:01 +00:00
|
|
|
|
if (_data.Length != _byteProvider.Length)
|
|
|
|
|
_data = new byte[_byteProvider.Length];
|
2012-08-24 03:58:11 +00:00
|
|
|
|
|
2013-02-06 04:35:01 +00:00
|
|
|
|
for (int i = 0; i < _data.Length; i++) {
|
2012-08-24 03:58:11 +00:00
|
|
|
|
_data[i] = _byteProvider.Bytes[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 07:30:52 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 03:58:11 +00:00
|
|
|
|
private void _buttonOK_Click (object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Apply();
|
|
|
|
|
}
|
2013-05-25 07:30:52 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-03-13 05:19:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|