Enhancements to field definitions with binary editor

This commit is contained in:
Michael Becker 2019-09-14 22:43:51 -04:00
parent 763f1ccce3
commit 60d3cdab86
3 changed files with 162 additions and 21 deletions

View File

@ -410,15 +410,15 @@ namespace UniversalEditor.Editors.Binary
this.tbFieldDefinitions.Items.Add(new ToolbarItemButton("tsbFieldDefinitionLoadFromDefinition", "Open Definition File", tsbFieldDefinitionLoad_Click));
tabPageFields.Controls.Add(this.tbFieldDefinitions, new BoxLayout.Constraints(false, true));
this.tmFieldDefinitions = new DefaultTreeModel(new Type[] { typeof(string), typeof(string), typeof(string), typeof(string), typeof(string) });
this.tmFieldDefinitions = new DefaultTreeModel(new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });
this.lvFieldDefinitions = new ListView();
this.lvFieldDefinitions.Model = tmFieldDefinitions;
this.lvFieldDefinitions.RowActivated += lvFieldDefinitions_RowActivated;
this.lvFieldDefinitions.Columns.Add(new ListViewColumnText(tmFieldDefinitions.Columns[0], "Name"));
this.lvFieldDefinitions.Columns.Add(new ListViewColumnText(tmFieldDefinitions.Columns[1], "Offset"));
this.lvFieldDefinitions.Columns.Add(new ListViewColumnText(tmFieldDefinitions.Columns[2], "Data Type [Size]"));
this.lvFieldDefinitions.Columns.Add(new ListViewColumnText(tmFieldDefinitions.Columns[3], "Color"));
this.lvFieldDefinitions.Columns.Add(new ListViewColumnText(tmFieldDefinitions.Columns[4], "Value"));
this.lvFieldDefinitions.Columns.Add(new ListViewColumnText(tmFieldDefinitions.Columns[3], "Value"));
tabPageFields.Controls.Add(this.lvFieldDefinitions, new BoxLayout.Constraints(true, true));
this.tabs.TabPages.Add(tabPageFields);
@ -426,6 +426,26 @@ namespace UniversalEditor.Editors.Binary
this.Controls.Add(tabs, new BoxLayout.Constraints(false, false, 0, BoxLayout.PackType.End));
}
private void lvFieldDefinitions_RowActivated(object sender, ListViewRowActivatedEventArgs e)
{
tsbFieldDefinitionEdit_Click(sender, e);
}
private string GetFieldValue(FieldDefinition definition)
{
foreach (CONVERSION_DATA converter in converters)
{
if (converter.DataType == definition.DataType)
{
byte[] data = new byte[converter.MaximumSize];
Array.Copy(hexedit.Data, definition.Offset, data, 0, Math.Min(data.Length, hexedit.Data.Length - definition.Offset));
string value = converter.ByteToStringFunc(data);
return value;
}
}
return String.Empty;
}
private void tsbFieldDefinitionAdd_Click(object sender, EventArgs e)
{
FieldDefinitionPropertiesDialog dlg = new FieldDefinitionPropertiesDialog();
@ -436,11 +456,12 @@ namespace UniversalEditor.Editors.Binary
{
new TreeModelRowColumn(tmFieldDefinitions.Columns[0], dlg.FieldDefinition.Name),
new TreeModelRowColumn(tmFieldDefinitions.Columns[1], dlg.FieldDefinition.Offset),
new TreeModelRowColumn(tmFieldDefinitions.Columns[2], dlg.FieldDefinition.Length)
// new TreeModelRowColumn(tmFieldDefinitions.Columns[0], dlg.cmdColor.Text)
new TreeModelRowColumn(tmFieldDefinitions.Columns[2], dlg.FieldDefinition.DataType.Name + " [" + dlg.FieldDefinition.DataTypeSizeString + "]"),
new TreeModelRowColumn(tmFieldDefinitions.Columns[3], GetFieldValue(dlg.FieldDefinition))
}));
tmFieldDefinitions.Rows[tmFieldDefinitions.Rows.Count - 1].SetExtraData<FieldDefinition>("def", dlg.FieldDefinition);
hexedit.HighlightAreas.Add(new HexEditorHighlightArea(dlg.FieldDefinition.Name, dlg.FieldDefinition.Name, dlg.FieldDefinition.Offset, dlg.FieldDefinition.Length, (dlg.cmdColor.BackgroundBrush as SolidBrush).Color));
hexedit.HighlightAreas.Add(new HexEditorHighlightArea(dlg.FieldDefinition.Name, dlg.FieldDefinition.Name, dlg.FieldDefinition.Offset, dlg.FieldDefinition.DataTypeSize, dlg.FieldDefinition.Color));
}
}
private void tsbFieldDefinitionEdit_Click(object sender, EventArgs e)
@ -452,8 +473,10 @@ namespace UniversalEditor.Editors.Binary
if (def != null)
{
FieldDefinitionPropertiesDialog dlg = new FieldDefinitionPropertiesDialog();
dlg.FieldDefinition = def;
if (dlg.ShowDialog() == DialogResult.OK)
{
def = dlg.FieldDefinition;
}
}
}
@ -554,6 +577,19 @@ namespace UniversalEditor.Editors.Binary
if (converters[i].TextBox != null)
converters[i].TextBox.Text = converters[i].ByteToStringFunc(data);
}
UpdateFieldDefinitions();
}
private void UpdateFieldDefinitions()
{
foreach (TreeModelRow row in tmFieldDefinitions.Rows)
{
FieldDefinition def = row.GetExtraData<FieldDefinition>("def");
if (def == null) continue;
row.RowColumns[3].Value = GetFieldValue(def);
}
}
@ -564,7 +600,7 @@ namespace UniversalEditor.Editors.Binary
BinaryObjectModel om = (ObjectModel as BinaryObjectModel);
if (om == null) return;
this.hexedit.Data = om.Data;
hexedit.Data = om.Data;
}
}
}

View File

@ -27,7 +27,41 @@ namespace UniversalEditor.Editors.Binary
{
public string Name;
public int Offset;
public int Length;
public Type DataType;
public Color Color;
public int DataTypeSize
{
get
{
int len = -1;
if (DataType == typeof(sbyte) || DataType == typeof(byte))
{
len = 1;
}
else if (DataType == typeof(short) || DataType == typeof(ushort))
{
len = 2;
}
else if (DataType == typeof(int) || DataType == typeof(uint) || DataType == typeof(float))
{
len = 4;
}
else if (DataType == typeof(long) || DataType == typeof(ulong) || DataType == typeof(double) || DataType == typeof(Guid))
{
len = 8;
}
return len;
}
}
public string DataTypeSizeString
{
get
{
if (DataTypeSize > -1)
return DataTypeSize.ToString();
return "*";
}
}
}
}

View File

@ -31,13 +31,15 @@ namespace UniversalEditor.Editors.Binary
public class FieldDefinitionPropertiesDialog : Dialog
{
private Label lblName;
internal TextBox txtName;
private TextBox txtName;
private Label lblOffset;
internal TextBox txtOffset;
private Label lblLength;
internal TextBox txtLength;
private TextBox txtOffset;
private Label lblDataType;
private ComboBox cboDataType;
private Label lblColor;
internal Button cmdColor;
private Button cmdColor;
private DefaultTreeModel tmDataType = null;
public FieldDefinition FieldDefinition = new FieldDefinition();
@ -69,12 +71,77 @@ namespace UniversalEditor.Editors.Binary
this.txtOffset = new TextBox();
this.Controls.Add(this.txtOffset, new GridLayout.Constraints(1, 1));
this.lblLength = new Label();
this.lblLength.Text = "_Length";
this.Controls.Add(this.lblLength, new GridLayout.Constraints(2, 0));
this.lblDataType = new Label();
this.lblDataType.Text = "_Data type";
this.Controls.Add(this.lblDataType, new GridLayout.Constraints(2, 0));
this.txtLength = new TextBox();
this.Controls.Add(this.txtLength, new GridLayout.Constraints(2, 1));
tmDataType = new DefaultTreeModel(new Type[] { typeof(string) });
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Signed 8-bit integer (SByte)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(sbyte));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Unsigned 8-bit integer (Byte)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(byte));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Signed 16-bit integer (Short)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(short));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Unsigned 16-bit integer (UShort)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(ushort));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Signed 32-bit integer (Int)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(int));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Unsigned 32-bit integer (UInt)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(uint));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Signed 64-bit integer (Long)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(long));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Unsigned 64-bit integer (ULong)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(long));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "32-bit floating-point (Float/Single)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(float));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "64-bit floating-point (Double)")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(double));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Fixed-length string")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(string));
tmDataType.Rows.Add(new TreeModelRow(new TreeModelRowColumn[]
{
new TreeModelRowColumn(tmDataType.Columns[0], "Length-prefixed string")
}));
tmDataType.Rows[tmDataType.Rows.Count - 1].SetExtraData<Type>("type", typeof(string));
this.cboDataType = new ComboBox();
this.cboDataType.ReadOnly = true;
this.cboDataType.Model = tmDataType;
this.Controls.Add(this.cboDataType, new GridLayout.Constraints(2, 1));
this.lblColor = new Label();
this.lblColor.Text = "_Color";
@ -114,14 +181,18 @@ namespace UniversalEditor.Editors.Binary
this.DialogResult = DialogResult.None;
return;
}
if (!Int32.TryParse(txtLength.Text, out length))
if (cboDataType.SelectedItem != null)
{
MessageDialog.ShowDialog("length must be a 32-bit integer");
FieldDefinition.DataType = cboDataType.SelectedItem.GetExtraData<Type>("type");
}
else
{
MessageDialog.ShowDialog("please select a data type");
this.DialogResult = DialogResult.None;
return;
}
FieldDefinition.Color = (cmdColor.BackgroundBrush as SolidBrush).Color;
FieldDefinition.Offset = offset;
FieldDefinition.Length = length;
}
}