Enhance BinaryEditor grammar definition support (still incomplete)

This commit is contained in:
Michael Becker 2019-10-31 02:52:09 -04:00
parent 1a6d44d792
commit 34914358e0
9 changed files with 310 additions and 7 deletions

View File

@ -0,0 +1,57 @@
//
// BinaryGrammarObjectModel.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using UniversalEditor.ObjectModels.BinaryGrammar.GrammarItems;
namespace UniversalEditor.ObjectModels.BinaryGrammar
{
public class BinaryGrammarObjectModel : ObjectModel
{
public string Name { get; set; } = String.Empty;
public string Author { get; set; } = String.Empty;
public string FileExtension { get; set; } = String.Empty;
public string UniversalTypeIdentifier { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
public GrammarItemStructure InitialStructure { get; set; } = null;
public GrammarItemStructure.GrammarItemStructureCollection Structures { get; } = new GrammarItemStructure.GrammarItemStructureCollection();
public bool IsComplete { get; set; } = false;
public override void Clear()
{
Name = String.Empty;
Author = String.Empty;
FileExtension = String.Empty;
UniversalTypeIdentifier = String.Empty;
Description = String.Empty;
InitialStructure = null;
Structures.Clear();
}
public override void CopyTo(ObjectModel where)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,34 @@
//
// FixedValue.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.ObjectModels.BinaryGrammar
{
public class FixedValue
{
public class FixedValueCollection
: System.Collections.ObjectModel.Collection<FixedValue>
{
}
public string Name { get; set; } = null;
public string Value { get; set; } = null;
}
}

View File

@ -0,0 +1,37 @@
//
// GrammarItem.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.ObjectModels.BinaryGrammar
{
public class GrammarItem
{
public class GrammarItemCollection
: System.Collections.ObjectModel.Collection<GrammarItem>
{
}
public string ID { get; set; } = null;
public string Name { get; set; } = null;
public string Length { get; set; } = "0";
public FixedValue.FixedValueCollection FixedValues { get; } = new FixedValue.FixedValueCollection();
}
}

View File

@ -0,0 +1,28 @@
//
// GrammarItemNumber.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.ObjectModels.BinaryGrammar.GrammarItems
{
public class GrammarItemNumber : GrammarItem
{
}
}

View File

@ -0,0 +1,27 @@
//
// GrammarItemString.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.ObjectModels.BinaryGrammar.GrammarItems
{
public class GrammarItemString : GrammarItem
{
}
}

View File

@ -0,0 +1,39 @@
//
// GrammarStructure.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using UniversalEditor.IO;
namespace UniversalEditor.ObjectModels.BinaryGrammar.GrammarItems
{
public class GrammarItemStructure : GrammarItem
{
public class GrammarItemStructureCollection
: System.Collections.ObjectModel.Collection<GrammarItemStructure>
{
}
public string Encoding { get; set; } = null;
public Endianness Endianness { get; set; } = Endianness.LittleEndian;
public GrammarItem.GrammarItemCollection Items { get; } = new GrammarItemCollection();
public bool Signed { get; set; } = true;
public string Extends { get; set; } = null;
}
}

View File

@ -0,0 +1,28 @@
//
// GrammarStructureReference.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.ObjectModels.BinaryGrammar.GrammarItems
{
public class GrammarItemStructureReference : GrammarItem
{
public string Structure { get; set; } = null;
}
}

View File

@ -181,6 +181,13 @@
<Compile Include="DataFormats\Text\Plain\PlainTextDataFormat.cs" />
<Compile Include="DataFormats\Shortcut\Linux\LinuxShortcutDataFormat.cs" />
<Compile Include="ObjectModels\Project\ProjectItem.cs" />
<Compile Include="ObjectModels\BinaryGrammar\BinaryGrammarObjectModel.cs" />
<Compile Include="ObjectModels\BinaryGrammar\GrammarItem.cs" />
<Compile Include="ObjectModels\BinaryGrammar\GrammarItems\GrammarItemStructure.cs" />
<Compile Include="ObjectModels\BinaryGrammar\GrammarItems\GrammarItemStructureReference.cs" />
<Compile Include="ObjectModels\BinaryGrammar\GrammarItems\GrammarItemNumber.cs" />
<Compile Include="ObjectModels\BinaryGrammar\GrammarItems\GrammarItemString.cs" />
<Compile Include="ObjectModels\BinaryGrammar\FixedValue.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversalEditor.Core\UniversalEditor.Core.csproj">
@ -202,6 +209,8 @@
<Folder Include="ObjectModels\JSON\Fields\" />
<Folder Include="DataFormats\Text\Plain\" />
<Folder Include="DataFormats\Shortcut\Linux\" />
<Folder Include="ObjectModels\BinaryGrammar\" />
<Folder Include="ObjectModels\BinaryGrammar\GrammarItems\" />
</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

@ -29,6 +29,9 @@ using MBS.Framework.UserInterface.Controls.HexEditor;
using MBS.Framework.UserInterface.Dialogs;
using MBS.Framework.UserInterface.Drawing;
using MBS.Framework.UserInterface.Layouts;
using System.Collections.Generic;
using UniversalEditor.Accessors;
using UniversalEditor.ObjectModels.BinaryGrammar;
namespace UniversalEditor.Editors.Binary
{
@ -36,7 +39,6 @@ namespace UniversalEditor.Editors.Binary
{
public override void UpdateSelections()
{
throw new NotImplementedException();
}
protected override EditorSelection CreateSelectionInternal(object content)
@ -289,8 +291,28 @@ namespace UniversalEditor.Editors.Binary
return sb.ToString();
}, delegate(string input)
{
// unsupported right now
return null;
if (input.ToLower().StartsWith("0x"))
{
input = input.Substring(2);
}
else if (input.ToLower().StartsWith("&h") && input.ToLower().EndsWith("&"))
{
input = input.Substring(2, input.Length - 3);
}
input = input.Replace(" ", String.Empty);
List<byte> list = new List<byte>();
if ((input.Length % 2) == 0)
{
int nbytes = (input.Length / 2);
for (int i = 0; i < nbytes; i++)
{
string s = input.Substring(i * 2, 2);
byte b = Byte.Parse(s, System.Globalization.NumberStyles.HexNumber);
list.Add(b);
}
}
return list.ToArray();
}, 4),
new CONVERSION_DATA(null, "Decimal", delegate(byte[] input)
{
@ -308,8 +330,8 @@ namespace UniversalEditor.Editors.Binary
return sb.ToString();
}, delegate(string input)
{
// unsupported right now
return null;
long b = Int64.Parse(input);
return BitConverter.GetBytes(b);
}, 4),
new CONVERSION_DATA(null, "Octal", delegate(byte[] input)
{
@ -407,7 +429,8 @@ namespace UniversalEditor.Editors.Binary
this.tbFieldDefinitions.Items.Add(new ToolbarItemButton("tsbFieldDefinitionEdit", StockType.Edit, tsbFieldDefinitionEdit_Click));
this.tbFieldDefinitions.Items.Add(new ToolbarItemButton("tsbFieldDefinitionRemove", StockType.Remove, tsbFieldDefinitionRemove_Click));
this.tbFieldDefinitions.Items.Add(new ToolbarItemSeparator());
this.tbFieldDefinitions.Items.Add(new ToolbarItemButton("tsbFieldDefinitionLoadFromDefinition", "Open Definition File", tsbFieldDefinitionLoad_Click));
this.tbFieldDefinitions.Items.Add(new ToolbarItemButton("tsbFieldDefinitionLoad", StockType.Open, tsbFieldDefinitionLoad_Click));
this.tbFieldDefinitions.Items.Add(new ToolbarItemButton("tsbFieldDefinitionSave", StockType.Save, 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) });
@ -438,7 +461,9 @@ namespace UniversalEditor.Editors.Binary
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));
if (definition.Offset < hexedit.Data.Length)
Array.Copy(hexedit.Data, definition.Offset, data, 0, Math.Min(data.Length, hexedit.Data.Length - definition.Offset));
string value = converter.ByteToStringFunc(data);
return value;
}
@ -485,6 +510,19 @@ namespace UniversalEditor.Editors.Binary
}
private void tsbFieldDefinitionLoad_Click(object sender, EventArgs e)
{
FileDialog dlg = new FileDialog();
dlg.Mode = FileDialogMode.Open;
BinaryGrammarObjectModel om = new BinaryGrammarObjectModel();
Association[] assocs = Association.FromObjectModelOrDataFormat(om.MakeReference());
dlg.AddFileNameFilterFromAssociations("Grammar definition files", assocs);
if (dlg.ShowDialog() == DialogResult.OK)
{
FileAccessor fa = new FileAccessor(dlg.SelectedFileNames[dlg.SelectedFileNames.Count - 1]);
DataFormat df = assocs[0].DataFormats[0].Create(); // FIXME: THIS SHOULD BE PROPERLY INFERRED
Document.Load(om, df, fa);
}
}
void Txt_KeyDown(object sender, MBS.Framework.UserInterface.Input.Keyboard.KeyEventArgs e)
@ -528,6 +566,12 @@ namespace UniversalEditor.Editors.Binary
if (data != null)
{
if (hexedit.SelectionStart + data.Length >= hexedit.Data.Length)
{
byte[] odata = hexedit.Data;
Array.Resize<byte>(ref odata, odata.Length + data.Length);
hexedit.Data = odata;
}
Array.Copy(data, 0, hexedit.Data, hexedit.SelectionStart.ByteIndex, data.Length);
Refresh();
}