Use the new Binary Editor as the default editor... it's pretty badass so far

This commit is contained in:
Michael Becker 2019-09-09 00:50:01 -04:00
parent 7394363623
commit 578ef369d5
5 changed files with 356 additions and 1 deletions

View File

@ -0,0 +1,41 @@
//
// BinaryObjectModel.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.Binary
{
public class BinaryObjectModel : ObjectModel
{
public byte[] Data { get; set; } = new byte[0];
public override void Clear()
{
Data = new byte[0];
}
public override void CopyTo(ObjectModel where)
{
BinaryObjectModel clone = (where as BinaryObjectModel);
if (clone == null) throw new ObjectModelNotSupportedException();
clone.Data = (Data.Clone() as byte[]);
}
}
}

View File

@ -165,6 +165,7 @@
<Compile Include="DataFormats\PropertyList\JavaScriptObjectNotation\JSONDataFormat.cs" />
<Compile Include="ObjectModels\FileSystem\FileSources\CompressedEmbeddedFileSource.cs" />
<Compile Include="ObjectModels\PropertyList\IPropertyListContainer.cs" />
<Compile Include="ObjectModels\Binary\BinaryObjectModel.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversalEditor.Core\UniversalEditor.Core.csproj">
@ -179,6 +180,7 @@
<ItemGroup />
<ItemGroup>
<Folder Include="DataFormats\PropertyList\JavaScriptObjectNotation\" />
<Folder Include="ObjectModels\Binary\" />
</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

@ -0,0 +1,295 @@
//
// BinaryEditor.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 System.Text;
using MBS.Framework.Drawing;
using UniversalEditor.ObjectModels.Binary;
using UniversalEditor.UserInterface;
using UniversalWidgetToolkit;
using UniversalWidgetToolkit.Controls;
using UniversalWidgetToolkit.Controls.HexEditor;
using UniversalWidgetToolkit.Layouts;
namespace UniversalEditor.Editors.Binary
{
public class BinaryEditor : Editor
{
public override void UpdateSelections()
{
throw new NotImplementedException();
}
protected override EditorSelection CreateSelectionInternal(object content)
{
throw new NotImplementedException();
}
private static EditorReference _er = null;
public override EditorReference MakeReference()
{
if (_er == null)
{
_er = base.MakeReference();
_er.SupportedObjectModels.Add(typeof(BinaryObjectModel));
}
return _er;
}
private HexEditorControl hexedit = null;
private Container conversionPanel = null;
private Label lblSigned8Bit = null;
private TextBox txtSigned8Bit = null;
private struct CONVERSION_DATA
{
public string Label;
public Func<byte[], string> ConversionFunc;
public TextBox TextBox;
public int MaximumSize;
public CONVERSION_DATA(string label, Func<byte[], string> conversionFunc, int maximumSize)
{
Label = label;
ConversionFunc = conversionFunc;
TextBox = null;
MaximumSize = maximumSize;
}
}
private CONVERSION_DATA[] converters = new CONVERSION_DATA[]
{
new CONVERSION_DATA("Signed 8-bit", delegate(byte[] input)
{
if (input.Length < 1)
return null;
sbyte b = (sbyte)input[0];
return b.ToString();
}, 1),
new CONVERSION_DATA("Unsigned 8-bit", delegate(byte[] input)
{
if (input.Length < 1)
return null;
byte b = input[0];
return b.ToString();
}, 1),
new CONVERSION_DATA("Signed 16-bit", delegate(byte[] input)
{
if (input.Length < 2)
return null;
short b = BitConverter.ToInt16(input, 0);
return b.ToString();
}, 2),
new CONVERSION_DATA("Unsigned 16-bit", delegate(byte[] input)
{
if (input.Length < 2)
return null;
ushort b = BitConverter.ToUInt16(input, 0);
return b.ToString();
}, 2),
// Second column
new CONVERSION_DATA("Signed 32-bit", delegate(byte[] input)
{
if (input.Length < 4)
return null;
int b = BitConverter.ToInt32(input, 0);
return b.ToString();
}, 4),
new CONVERSION_DATA("Unsigned 32-bit", delegate(byte[] input)
{
if (input.Length < 4)
return null;
uint b = BitConverter.ToUInt32(input, 0);
return b.ToString();
}, 4),
new CONVERSION_DATA("Float 32-bit", delegate(byte[] input)
{
if (input.Length < 4)
return null;
float b = BitConverter.ToSingle(input, 0);
return b.ToString();
}, 4),
new CONVERSION_DATA("Float 64-bit", delegate(byte[] input)
{
if (input.Length < 8)
return null;
double b = BitConverter.ToDouble(input, 0);
return b.ToString();
}, 8),
// Third column
new CONVERSION_DATA("Hexadecimal", delegate(byte[] input)
{
StringBuilder sb = new StringBuilder();
if (input.Length < 1)
return null;
int len = Math.Min(input.Length, 4);
for (int i = 0; i < len; i++)
{
sb.Append(input[i].ToString("X").PadLeft(2, '0'));
if (i < len - 1)
sb.Append(' ');
}
return sb.ToString();
}, 4),
new CONVERSION_DATA("Decimal", delegate(byte[] input)
{
StringBuilder sb = new StringBuilder();
if (input.Length < 1)
return null;
int len = Math.Min(input.Length, 4);
for (int i = 0; i < len; i++)
{
sb.Append(input[i].ToString().PadLeft(3, '0'));
if (i < len - 1)
sb.Append(' ');
}
return sb.ToString();
}, 4),
new CONVERSION_DATA("Octal", delegate(byte[] input)
{
StringBuilder sb = new StringBuilder();
if (input.Length < 1)
return null;
int len = Math.Min(input.Length, 4);
for (int i = 0; i < len; i++)
{
sb.Append(Convert.ToString(input[i], 8).PadLeft(3, '0'));
if (i < len - 1)
sb.Append(' ');
}
return sb.ToString();
}, 4),
new CONVERSION_DATA("Binary", delegate(byte[] input)
{
StringBuilder sb = new StringBuilder();
if (input.Length < 1)
return null;
int len = Math.Min(input.Length, 4);
for (int i = 0; i < len; i++)
{
sb.Append(Convert.ToString(input[i], 2).PadLeft(8, '0'));
if (i < len - 1)
sb.Append(' ');
}
return sb.ToString();
}, 4)
};
public BinaryEditor()
{
this.Layout = new BoxLayout(UniversalWidgetToolkit.Orientation.Vertical);
this.hexedit = new HexEditorControl();
this.hexedit.SelectionChanged += Hexedit_SelectionChanged;
this.Controls.Add(hexedit, new BoxLayout.Constraints(true, true));
this.conversionPanel = new Container();
this.conversionPanel.Layout = new GridLayout();
int r = 0, c = 0;
for (int i = 0; i < converters.Length; i++)
{
Label lbl = new Label();
lbl.Text = converters[i].Label;
this.conversionPanel.Controls.Add(lbl, new GridLayout.Constraints(r, c));
TextBox txt = new TextBox();
txt.GotFocus += Txt_GotFocus;
txt.LostFocus += Txt_LostFocus;
txt.Text = "---";
this.conversionPanel.Controls.Add(txt, new GridLayout.Constraints(r, c + 1));
converters[i].TextBox = txt;
txt.SetExtraData<CONVERSION_DATA>("converter", converters[i]);
if ((i + 1) % 4 == 0)
{
c += 2; // damn
r = 0;
}
else
{
r++;
}
}
this.Controls.Add(conversionPanel, new BoxLayout.Constraints(false, false, 0, BoxLayout.PackType.End));
}
void Txt_LostFocus(object sender, EventArgs e)
{
HexEditorHighlightArea area = hexedit.HighlightAreas["conversion"];
if (area != null)
hexedit.HighlightAreas.Remove(area);
}
void Txt_GotFocus(object sender, EventArgs e)
{
TextBox ctl = sender as TextBox;
CONVERSION_DATA converter = ctl.GetExtraData<CONVERSION_DATA>("converter");
HexEditorHighlightArea area = hexedit.HighlightAreas["conversion"];
if (area == null) area = new HexEditorHighlightArea();
area.Start = hexedit.SelectionStart.ByteIndex;
area.Length = converter.MaximumSize;
area.Color = Colors.LightGray;
hexedit.HighlightAreas["conversion"] = area;
}
private void Hexedit_SelectionChanged(object sender, EventArgs e)
{
// this actually worked on the very first try. holy crap.
byte[] data = new byte[Math.Min(hexedit.Data.Length - hexedit.SelectionStart.ByteIndex, 8)];
Array.Copy(hexedit.Data, hexedit.SelectionStart.ByteIndex, data, 0, data.Length);
for (int i = 0; i < converters.Length; i++)
{
if (converters[i].TextBox != null)
converters[i].TextBox.Text = converters[i].ConversionFunc(data);
}
}
protected override void OnObjectModelChanged(EventArgs e)
{
base.OnObjectModelChanged(e);
BinaryObjectModel om = (ObjectModel as BinaryObjectModel);
if (om == null) return;
this.hexedit.Data = om.Data;
}
}
}

View File

@ -25,6 +25,7 @@ using UniversalWidgetToolkit.Controls.Ribbon;
using UniversalEditor.Printing;
using UniversalWidgetToolkit.Printing;
using UniversalEditor.UserInterface.Pages;
using UniversalEditor.ObjectModels.Binary;
namespace UniversalEditor.UserInterface
{
@ -379,13 +380,14 @@ namespace UniversalEditor.UserInterface
}
}
private EditorReference DefaultEditor = new EditorReference(typeof(Editors.Text.Plain.PlainTextEditor));
private EditorReference DefaultEditor = new EditorReference(typeof(Editors.Binary.BinaryEditor));
private void OpenDefaultEditor(string filename)
{
if (DefaultEditor == null) return;
Editor ed = DefaultEditor.Create();
/*
PlainTextObjectModel om1 = new PlainTextObjectModel();
if (System.IO.File.Exists(filename))
{
@ -397,6 +399,19 @@ namespace UniversalEditor.UserInterface
}
}
ed.ObjectModel = om1;
*/
BinaryObjectModel om1 = new BinaryObjectModel();
if (System.IO.File.Exists(filename))
{
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
if (fi.Length < Math.Pow(1024, 2))
{
byte[] content = System.IO.File.ReadAllBytes(filename);
om1.Data = content;
}
}
ed.ObjectModel = om1;
InitDocTab(System.IO.Path.GetFileName(filename), ed);
}

View File

@ -118,6 +118,7 @@
<Compile Include="Editors\Text\TextEditorSelection.cs" />
<Compile Include="Editors\FileSystem\FileSystemSelection.cs" />
<Compile Include="Editors\Text\TextEditor.cs" />
<Compile Include="Editors\Binary\BinaryEditor.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
@ -168,6 +169,7 @@
<Folder Include="Editors\FileSystem\" />
<Folder Include="Editors\PropertyList\" />
<Folder Include="Controls\" />
<Folder Include="Editors\Binary\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.