VenomRat
This commit is contained in:
66
Helper/HexEditor/ByteCollection.cs
Normal file
66
Helper/HexEditor/ByteCollection.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Helper.HexEditor;
|
||||
|
||||
public class ByteCollection
|
||||
{
|
||||
private List<byte> _bytes;
|
||||
|
||||
public int Length => _bytes.Count;
|
||||
|
||||
public ByteCollection()
|
||||
{
|
||||
_bytes = new List<byte>();
|
||||
}
|
||||
|
||||
public ByteCollection(byte[] bytes)
|
||||
{
|
||||
_bytes = new List<byte>(bytes);
|
||||
}
|
||||
|
||||
public void Add(byte item)
|
||||
{
|
||||
_bytes.Add(item);
|
||||
}
|
||||
|
||||
public void Insert(int index, byte item)
|
||||
{
|
||||
_bytes.Insert(index, item);
|
||||
}
|
||||
|
||||
public void Remove(byte item)
|
||||
{
|
||||
_bytes.Remove(item);
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
_bytes.RemoveAt(index);
|
||||
}
|
||||
|
||||
public void RemoveRange(int startIndex, int count)
|
||||
{
|
||||
_bytes.RemoveRange(startIndex, count);
|
||||
}
|
||||
|
||||
public byte GetAt(int index)
|
||||
{
|
||||
return _bytes[index];
|
||||
}
|
||||
|
||||
public void SetAt(int index, byte item)
|
||||
{
|
||||
_bytes[index] = item;
|
||||
}
|
||||
|
||||
public char GetCharAt(int index)
|
||||
{
|
||||
return Convert.ToChar(_bytes[index]);
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
return _bytes.ToArray();
|
||||
}
|
||||
}
|
184
Helper/HexEditor/Caret.cs
Normal file
184
Helper/HexEditor/Caret.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Server.Helper.HexEditor;
|
||||
|
||||
public class Caret
|
||||
{
|
||||
private int _startIndex;
|
||||
|
||||
private int _endIndex;
|
||||
|
||||
private bool _isCaretActive;
|
||||
|
||||
private bool _isCaretHidden;
|
||||
|
||||
private Point _location;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
public int SelectionStart
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_endIndex < _startIndex)
|
||||
{
|
||||
return _endIndex;
|
||||
}
|
||||
return _startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public int SelectionLength
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_endIndex < _startIndex)
|
||||
{
|
||||
return _startIndex - _endIndex;
|
||||
}
|
||||
return _endIndex - _startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Focused => _isCaretActive;
|
||||
|
||||
public int CurrentIndex => _endIndex;
|
||||
|
||||
public Point Location => _location;
|
||||
|
||||
public event EventHandler SelectionStartChanged;
|
||||
|
||||
public event EventHandler SelectionLengthChanged;
|
||||
|
||||
public Caret(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
_isCaretActive = false;
|
||||
_startIndex = 0;
|
||||
_endIndex = 0;
|
||||
_isCaretHidden = true;
|
||||
_location = new Point(0, 0);
|
||||
}
|
||||
|
||||
private bool Create(IntPtr hWHandler)
|
||||
{
|
||||
if (!_isCaretActive)
|
||||
{
|
||||
_isCaretActive = true;
|
||||
return CreateCaret(hWHandler, IntPtr.Zero, 0, (int)_editor.CharSize.Height - 2);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool Show(IntPtr hWnd)
|
||||
{
|
||||
if (_isCaretActive)
|
||||
{
|
||||
_isCaretHidden = false;
|
||||
return ShowCaret(hWnd);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Hide(IntPtr hWnd)
|
||||
{
|
||||
if (_isCaretActive && !_isCaretHidden)
|
||||
{
|
||||
_isCaretHidden = true;
|
||||
return HideCaret(hWnd);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Destroy()
|
||||
{
|
||||
if (_isCaretActive)
|
||||
{
|
||||
_isCaretActive = false;
|
||||
DeSelect();
|
||||
DestroyCaret();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetStartIndex(int index)
|
||||
{
|
||||
_startIndex = index;
|
||||
_endIndex = _startIndex;
|
||||
if (this.SelectionStartChanged != null)
|
||||
{
|
||||
this.SelectionStartChanged(this, EventArgs.Empty);
|
||||
}
|
||||
if (this.SelectionLengthChanged != null)
|
||||
{
|
||||
this.SelectionLengthChanged(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetEndIndex(int index)
|
||||
{
|
||||
_endIndex = index;
|
||||
if (this.SelectionStartChanged != null)
|
||||
{
|
||||
this.SelectionStartChanged(this, EventArgs.Empty);
|
||||
}
|
||||
if (this.SelectionLengthChanged != null)
|
||||
{
|
||||
this.SelectionLengthChanged(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCaretLocation(Point start)
|
||||
{
|
||||
Create(_editor.Handle);
|
||||
_location = start;
|
||||
SetCaretPos(_location.X, _location.Y);
|
||||
Show(_editor.Handle);
|
||||
}
|
||||
|
||||
public bool IsSelected(int byteIndex)
|
||||
{
|
||||
if (SelectionStart <= byteIndex)
|
||||
{
|
||||
return byteIndex < SelectionStart + SelectionLength;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void DeSelect()
|
||||
{
|
||||
if (_endIndex < _startIndex)
|
||||
{
|
||||
_startIndex = _endIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
_endIndex = _startIndex;
|
||||
}
|
||||
if (this.SelectionStartChanged != null)
|
||||
{
|
||||
this.SelectionStartChanged(this, EventArgs.Empty);
|
||||
}
|
||||
if (this.SelectionLengthChanged != null)
|
||||
{
|
||||
this.SelectionLengthChanged(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool DestroyCaret();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool SetCaretPos(int x, int y);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool ShowCaret(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool HideCaret(IntPtr hWnd);
|
||||
}
|
140
Helper/HexEditor/EditView.cs
Normal file
140
Helper/HexEditor/EditView.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Server.Helper.HexEditor;
|
||||
|
||||
public class EditView : IKeyMouseEventHandler
|
||||
{
|
||||
private HexViewHandler _hexView;
|
||||
|
||||
private StringViewHandler _stringView;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
public EditView(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
_hexView = new HexViewHandler(editor);
|
||||
_stringView = new StringViewHandler(editor);
|
||||
}
|
||||
|
||||
public void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
if (InHexView(_editor.CaretPosX))
|
||||
{
|
||||
_hexView.OnKeyPress(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnKeyPress(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (InHexView(_editor.CaretPosX))
|
||||
{
|
||||
_hexView.OnKeyDown(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnKeyDown(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyUp(KeyEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnMouseDown(MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
if (InHexView(e.X))
|
||||
{
|
||||
_hexView.OnMouseDown(e.X, e.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnMouseDown(e.X, e.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMouseDragged(MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
if (InHexView(e.X))
|
||||
{
|
||||
_hexView.OnMouseDragged(e.X, e.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnMouseDragged(e.X, e.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMouseUp(MouseEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnMouseDoubleClick(MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
if (InHexView(e.X))
|
||||
{
|
||||
_hexView.OnMouseDoubleClick();
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnMouseDoubleClick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnGotFocus(EventArgs e)
|
||||
{
|
||||
if (InHexView(_editor.CaretPosX))
|
||||
{
|
||||
_hexView.Focus();
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.Focus();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLowerCase()
|
||||
{
|
||||
_hexView.SetLowerCase();
|
||||
}
|
||||
|
||||
public void SetUpperCase()
|
||||
{
|
||||
_hexView.SetUpperCase();
|
||||
}
|
||||
|
||||
public void Update(int startPositionX, Rectangle area)
|
||||
{
|
||||
_hexView.Update(startPositionX, area);
|
||||
_stringView.Update(_hexView.MaxWidth, area);
|
||||
}
|
||||
|
||||
public void Paint(Graphics g, int startIndex, int endIndex)
|
||||
{
|
||||
for (int i = 0; i + startIndex < endIndex; i++)
|
||||
{
|
||||
_hexView.Paint(g, i, startIndex);
|
||||
_stringView.Paint(g, i, startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private bool InHexView(int x)
|
||||
{
|
||||
return x < _hexView.MaxWidth + _editor.EntityMargin - 2;
|
||||
}
|
||||
}
|
1064
Helper/HexEditor/HexEditor.cs
Normal file
1064
Helper/HexEditor/HexEditor.cs
Normal file
File diff suppressed because it is too large
Load Diff
316
Helper/HexEditor/HexViewHandler.cs
Normal file
316
Helper/HexEditor/HexViewHandler.cs
Normal file
@@ -0,0 +1,316 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Server.Helper.HexEditor;
|
||||
|
||||
public class HexViewHandler
|
||||
{
|
||||
private bool _isEditing;
|
||||
|
||||
private string _hexType = "X2";
|
||||
|
||||
private Rectangle _recHexValue;
|
||||
|
||||
private StringFormat _stringFormat;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
public int MaxWidth => _recHexValue.X + _recHexValue.Width * _editor.BytesPerLine;
|
||||
|
||||
public HexViewHandler(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
|
||||
_stringFormat.Alignment = StringAlignment.Center;
|
||||
_stringFormat.LineAlignment = StringAlignment.Center;
|
||||
}
|
||||
|
||||
public void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
if (IsHex(e.KeyChar))
|
||||
{
|
||||
HandleUserInput(e.KeyChar);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
|
||||
{
|
||||
if (_editor.SelectionLength > 0)
|
||||
{
|
||||
HandleUserRemove();
|
||||
int caretIndex = _editor.CaretIndex;
|
||||
Point caretLocation = GetCaretLocation(caretIndex);
|
||||
_editor.SetCaretStart(caretIndex, caretLocation);
|
||||
}
|
||||
else if (_editor.CaretIndex < _editor.LastVisibleByte && e.KeyCode == Keys.Delete)
|
||||
{
|
||||
_editor.RemoveByteAt(_editor.CaretIndex);
|
||||
Point caretLocation2 = GetCaretLocation(_editor.CaretIndex);
|
||||
_editor.SetCaretStart(_editor.CaretIndex, caretLocation2);
|
||||
}
|
||||
else if (_editor.CaretIndex > 0 && e.KeyCode == Keys.Back)
|
||||
{
|
||||
int index = _editor.CaretIndex - 1;
|
||||
if (_isEditing)
|
||||
{
|
||||
index = _editor.CaretIndex;
|
||||
}
|
||||
_editor.RemoveByteAt(index);
|
||||
Point caretLocation3 = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, caretLocation3);
|
||||
}
|
||||
_isEditing = false;
|
||||
}
|
||||
else if (e.KeyCode == Keys.Up && _editor.CaretIndex - _editor.BytesPerLine >= 0)
|
||||
{
|
||||
int num = _editor.CaretIndex - _editor.BytesPerLine;
|
||||
if (num % _editor.BytesPerLine == 0 && _editor.CaretPosX >= _recHexValue.X + _recHexValue.Width * _editor.BytesPerLine)
|
||||
{
|
||||
Point location = new Point(_editor.CaretPosX, _editor.CaretPosY - _recHexValue.Height);
|
||||
if (num == 0)
|
||||
{
|
||||
location = new Point(_editor.CaretPosX, _editor.CaretPosY);
|
||||
num = _editor.BytesPerLine;
|
||||
}
|
||||
if (e.Shift)
|
||||
{
|
||||
_editor.SetCaretEnd(num, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.SetCaretStart(num, location);
|
||||
}
|
||||
_isEditing = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleArrowKeys(num, e.Shift);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Down && (_editor.CaretIndex - 1) / _editor.BytesPerLine < _editor.HexTableLength / _editor.BytesPerLine)
|
||||
{
|
||||
int num2 = _editor.CaretIndex + _editor.BytesPerLine;
|
||||
if (num2 > _editor.HexTableLength)
|
||||
{
|
||||
num2 = _editor.HexTableLength;
|
||||
HandleArrowKeys(num2, e.Shift);
|
||||
return;
|
||||
}
|
||||
Point location2 = new Point(_editor.CaretPosX, _editor.CaretPosY + _recHexValue.Height);
|
||||
if (e.Shift)
|
||||
{
|
||||
_editor.SetCaretEnd(num2, location2);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.SetCaretStart(num2, location2);
|
||||
}
|
||||
_isEditing = false;
|
||||
}
|
||||
else if (e.KeyCode == Keys.Left && _editor.CaretIndex - 1 >= 0)
|
||||
{
|
||||
int index2 = _editor.CaretIndex - 1;
|
||||
HandleArrowKeys(index2, e.Shift);
|
||||
}
|
||||
else if (e.KeyCode == Keys.Right && _editor.CaretIndex + 1 <= _editor.HexTableLength)
|
||||
{
|
||||
int index3 = _editor.CaretIndex + 1;
|
||||
HandleArrowKeys(index3, e.Shift);
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleArrowKeys(int index, bool isShiftDown)
|
||||
{
|
||||
Point caretLocation = GetCaretLocation(index);
|
||||
if (isShiftDown)
|
||||
{
|
||||
_editor.SetCaretEnd(index, caretLocation);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.SetCaretStart(index, caretLocation);
|
||||
}
|
||||
_isEditing = false;
|
||||
}
|
||||
|
||||
public void OnMouseDown(int x, int y)
|
||||
{
|
||||
int num = (x - _recHexValue.X) / _recHexValue.Width;
|
||||
int num2 = (y - _recHexValue.Y) / _recHexValue.Height;
|
||||
num = ((num > _editor.BytesPerLine) ? _editor.BytesPerLine : num);
|
||||
num = ((num >= 0) ? num : 0);
|
||||
num2 = ((num2 > _editor.MaxBytesV) ? _editor.MaxBytesV : num2);
|
||||
num2 = ((num2 >= 0) ? num2 : 0);
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= num2)
|
||||
{
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= num)
|
||||
{
|
||||
num = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
num2 = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + num + num2 * _editor.BytesPerLine);
|
||||
int x2 = num * _recHexValue.Width + _recHexValue.X;
|
||||
int y2 = num2 * _recHexValue.Height + _recHexValue.Y;
|
||||
_editor.SetCaretStart(index, new Point(x2, y2));
|
||||
_isEditing = false;
|
||||
}
|
||||
|
||||
public void OnMouseDragged(int x, int y)
|
||||
{
|
||||
int num = (x - _recHexValue.X) / _recHexValue.Width;
|
||||
int num2 = (y - _recHexValue.Y) / _recHexValue.Height;
|
||||
num = ((num > _editor.BytesPerLine) ? _editor.BytesPerLine : num);
|
||||
num = ((num >= 0) ? num : 0);
|
||||
num2 = ((num2 > _editor.MaxBytesV) ? _editor.MaxBytesV : num2);
|
||||
num2 = ((_editor.FirstVisibleByte <= 0) ? ((num2 >= 0) ? num2 : 0) : ((num2 < 0) ? (-1) : num2));
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= num2)
|
||||
{
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= num)
|
||||
{
|
||||
num = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
num2 = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + num + num2 * _editor.BytesPerLine);
|
||||
int x2 = num * _recHexValue.Width + _recHexValue.X;
|
||||
int y2 = num2 * _recHexValue.Height + _recHexValue.Y;
|
||||
_editor.SetCaretEnd(index, new Point(x2, y2));
|
||||
}
|
||||
|
||||
public void OnMouseDoubleClick()
|
||||
{
|
||||
if (_editor.CaretIndex < _editor.LastVisibleByte)
|
||||
{
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point caretLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretEnd(index, caretLocation);
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(int startPositionX, Rectangle area)
|
||||
{
|
||||
_recHexValue = new Rectangle(startPositionX, area.Y, (int)(_editor.CharSize.Width * 3f), (int)_editor.CharSize.Height - 2);
|
||||
_recHexValue.X += _editor.EntityMargin;
|
||||
}
|
||||
|
||||
public void Paint(Graphics g, int index, int startIndex)
|
||||
{
|
||||
Point byteColumnAndRow = GetByteColumnAndRow(index);
|
||||
if (_editor.IsSelected(index + startIndex))
|
||||
{
|
||||
PaintByteAsSelected(g, byteColumnAndRow, index + startIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
PaintByte(g, byteColumnAndRow, index + startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private void PaintByteAsSelected(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush brush = new SolidBrush(_editor.SelectionBackColor);
|
||||
SolidBrush brush2 = new SolidBrush(_editor.SelectionForeColor);
|
||||
RectangleF bound = GetBound(point);
|
||||
string s = _editor.GetByte(index).ToString(_hexType);
|
||||
g.FillRectangle(brush, bound);
|
||||
g.DrawString(s, _editor.Font, brush2, bound, _stringFormat);
|
||||
}
|
||||
|
||||
private void PaintByte(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush brush = new SolidBrush(_editor.ForeColor);
|
||||
RectangleF bound = GetBound(point);
|
||||
string s = _editor.GetByte(index).ToString(_hexType);
|
||||
g.DrawString(s, _editor.Font, brush, bound, _stringFormat);
|
||||
}
|
||||
|
||||
public void SetLowerCase()
|
||||
{
|
||||
_hexType = "x2";
|
||||
}
|
||||
|
||||
public void SetUpperCase()
|
||||
{
|
||||
_hexType = "X2";
|
||||
}
|
||||
|
||||
public void Focus()
|
||||
{
|
||||
int caretIndex = _editor.CaretIndex;
|
||||
Point caretLocation = GetCaretLocation(caretIndex);
|
||||
_editor.SetCaretStart(caretIndex, caretLocation);
|
||||
}
|
||||
|
||||
private Point GetCaretLocation(int index)
|
||||
{
|
||||
int x = _recHexValue.X + _recHexValue.Width * (index % _editor.BytesPerLine);
|
||||
int y = _recHexValue.Y + _recHexValue.Height * ((index - (_editor.FirstVisibleByte + index % _editor.BytesPerLine)) / _editor.BytesPerLine);
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
private void HandleUserRemove()
|
||||
{
|
||||
int selectionStart = _editor.SelectionStart;
|
||||
Point caretLocation = GetCaretLocation(selectionStart);
|
||||
_editor.RemoveSelectedBytes();
|
||||
_editor.SetCaretStart(selectionStart, caretLocation);
|
||||
}
|
||||
|
||||
private void HandleUserInput(char key)
|
||||
{
|
||||
if (!_editor.CaretFocused)
|
||||
{
|
||||
return;
|
||||
}
|
||||
HandleUserRemove();
|
||||
if (_isEditing)
|
||||
{
|
||||
_isEditing = false;
|
||||
byte @byte = _editor.GetByte(_editor.CaretIndex);
|
||||
@byte = (byte)(@byte + Convert.ToByte(key.ToString(), 16));
|
||||
_editor.SetByte(_editor.CaretIndex, @byte);
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point caretLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, caretLocation);
|
||||
return;
|
||||
}
|
||||
_isEditing = true;
|
||||
byte item = Convert.ToByte(key + "0", 16);
|
||||
if (_editor.HexTable.Length == 0)
|
||||
{
|
||||
_editor.AppendByte(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.InsertByte(_editor.CaretIndex, item);
|
||||
}
|
||||
int x = _recHexValue.X + _recHexValue.Width * (_editor.CaretIndex % _editor.BytesPerLine) + _recHexValue.Width / 2;
|
||||
int y = _recHexValue.Y + _recHexValue.Height * ((_editor.CaretIndex - (_editor.FirstVisibleByte + _editor.CaretIndex % _editor.BytesPerLine)) / _editor.BytesPerLine);
|
||||
_editor.SetCaretStart(_editor.CaretIndex, new Point(x, y));
|
||||
}
|
||||
|
||||
private Point GetByteColumnAndRow(int index)
|
||||
{
|
||||
int x = index % _editor.BytesPerLine;
|
||||
int y = index / _editor.BytesPerLine;
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
private RectangleF GetBound(Point point)
|
||||
{
|
||||
return new RectangleF(_recHexValue.X + point.X * _recHexValue.Width, _recHexValue.Y + point.Y * _recHexValue.Height, _recHexValue.Width, _recHexValue.Height);
|
||||
}
|
||||
|
||||
private bool IsHex(char c)
|
||||
{
|
||||
if ((c < 'a' || c > 'f') && (c < 'A' || c > 'F'))
|
||||
{
|
||||
return char.IsDigit(c);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
23
Helper/HexEditor/IKeyMouseEventHandler.cs
Normal file
23
Helper/HexEditor/IKeyMouseEventHandler.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Server.Helper.HexEditor;
|
||||
|
||||
public interface IKeyMouseEventHandler
|
||||
{
|
||||
void OnKeyPress(KeyPressEventArgs e);
|
||||
|
||||
void OnKeyDown(KeyEventArgs e);
|
||||
|
||||
void OnKeyUp(KeyEventArgs e);
|
||||
|
||||
void OnMouseDown(MouseEventArgs e);
|
||||
|
||||
void OnMouseDragged(MouseEventArgs e);
|
||||
|
||||
void OnMouseUp(MouseEventArgs e);
|
||||
|
||||
void OnMouseDoubleClick(MouseEventArgs e);
|
||||
|
||||
void OnGotFocus(EventArgs e);
|
||||
}
|
273
Helper/HexEditor/StringViewHandler.cs
Normal file
273
Helper/HexEditor/StringViewHandler.cs
Normal file
@@ -0,0 +1,273 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Server.Helper.HexEditor;
|
||||
|
||||
public class StringViewHandler
|
||||
{
|
||||
private Rectangle _recStringView;
|
||||
|
||||
private StringFormat _stringFormat;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
public int MaxWidth => _recStringView.X + _recStringView.Width;
|
||||
|
||||
public StringViewHandler(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
|
||||
_stringFormat.Alignment = StringAlignment.Center;
|
||||
_stringFormat.LineAlignment = StringAlignment.Center;
|
||||
}
|
||||
|
||||
public void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
if (!char.IsControl(e.KeyChar))
|
||||
{
|
||||
HandleUserInput(e.KeyChar);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
|
||||
{
|
||||
if (_editor.SelectionLength > 0)
|
||||
{
|
||||
HandleUserRemove();
|
||||
int caretIndex = _editor.CaretIndex;
|
||||
Point caretLocation = GetCaretLocation(caretIndex);
|
||||
_editor.SetCaretStart(caretIndex, caretLocation);
|
||||
}
|
||||
else if (_editor.CaretIndex < _editor.LastVisibleByte && e.KeyCode == Keys.Delete)
|
||||
{
|
||||
_editor.RemoveByteAt(_editor.CaretIndex);
|
||||
Point caretLocation2 = GetCaretLocation(_editor.CaretIndex);
|
||||
_editor.SetCaretStart(_editor.CaretIndex, caretLocation2);
|
||||
}
|
||||
else if (_editor.CaretIndex > 0 && e.KeyCode == Keys.Back)
|
||||
{
|
||||
int index = _editor.CaretIndex - 1;
|
||||
_editor.RemoveByteAt(index);
|
||||
Point caretLocation3 = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, caretLocation3);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Up && _editor.CaretIndex - _editor.BytesPerLine >= 0)
|
||||
{
|
||||
int num = _editor.CaretIndex - _editor.BytesPerLine;
|
||||
if (num % _editor.BytesPerLine == 0 && _editor.CaretPosX >= _recStringView.X + _recStringView.Width)
|
||||
{
|
||||
Point location = new Point(_editor.CaretPosX, _editor.CaretPosY - _recStringView.Height);
|
||||
if (num == 0)
|
||||
{
|
||||
location = new Point(_editor.CaretPosX, _editor.CaretPosY);
|
||||
num = _editor.BytesPerLine;
|
||||
}
|
||||
if (e.Shift)
|
||||
{
|
||||
_editor.SetCaretEnd(num, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.SetCaretStart(num, location);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleArrowKeys(num, e.Shift);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Down && (_editor.CaretIndex - 1) / _editor.BytesPerLine < _editor.HexTableLength / _editor.BytesPerLine)
|
||||
{
|
||||
int num2 = _editor.CaretIndex + _editor.BytesPerLine;
|
||||
if (num2 > _editor.HexTableLength)
|
||||
{
|
||||
num2 = _editor.HexTableLength;
|
||||
HandleArrowKeys(num2, e.Shift);
|
||||
return;
|
||||
}
|
||||
Point location2 = new Point(_editor.CaretPosX, _editor.CaretPosY + _recStringView.Height);
|
||||
if (e.Shift)
|
||||
{
|
||||
_editor.SetCaretEnd(num2, location2);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.SetCaretStart(num2, location2);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Left && _editor.CaretIndex - 1 >= 0)
|
||||
{
|
||||
int index2 = _editor.CaretIndex - 1;
|
||||
HandleArrowKeys(index2, e.Shift);
|
||||
}
|
||||
else if (e.KeyCode == Keys.Right && _editor.CaretIndex + 1 <= _editor.LastVisibleByte)
|
||||
{
|
||||
int index3 = _editor.CaretIndex + 1;
|
||||
HandleArrowKeys(index3, e.Shift);
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleArrowKeys(int index, bool isShiftDown)
|
||||
{
|
||||
Point caretLocation = GetCaretLocation(index);
|
||||
if (isShiftDown)
|
||||
{
|
||||
_editor.SetCaretEnd(index, caretLocation);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.SetCaretStart(index, caretLocation);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMouseDown(int x, int y)
|
||||
{
|
||||
int num = (x - _recStringView.X) / (int)_editor.CharSize.Width;
|
||||
int num2 = (y - _recStringView.Y) / _recStringView.Height;
|
||||
num = ((num > _editor.BytesPerLine) ? _editor.BytesPerLine : num);
|
||||
num = ((num >= 0) ? num : 0);
|
||||
num2 = ((num2 > _editor.MaxBytesV) ? _editor.MaxBytesV : num2);
|
||||
num2 = ((num2 >= 0) ? num2 : 0);
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= num2)
|
||||
{
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= num)
|
||||
{
|
||||
num = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
num2 = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + num + num2 * _editor.BytesPerLine);
|
||||
int x2 = num * (int)_editor.CharSize.Width + _recStringView.X;
|
||||
int y2 = num2 * _recStringView.Height + _recStringView.Y;
|
||||
_editor.SetCaretStart(index, new Point(x2, y2));
|
||||
}
|
||||
|
||||
public void OnMouseDragged(int x, int y)
|
||||
{
|
||||
int num = (x - _recStringView.X) / (int)_editor.CharSize.Width;
|
||||
int num2 = (y - _recStringView.Y) / _recStringView.Height;
|
||||
num = ((num > _editor.BytesPerLine) ? _editor.BytesPerLine : num);
|
||||
num = ((num >= 0) ? num : 0);
|
||||
num2 = ((num2 > _editor.MaxBytesV) ? _editor.MaxBytesV : num2);
|
||||
num2 = ((_editor.FirstVisibleByte <= 0) ? ((num2 >= 0) ? num2 : 0) : ((num2 < 0) ? (-1) : num2));
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= num2)
|
||||
{
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= num)
|
||||
{
|
||||
num = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
num2 = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + num + num2 * _editor.BytesPerLine);
|
||||
int x2 = num * (int)_editor.CharSize.Width + _recStringView.X;
|
||||
int y2 = num2 * _recStringView.Height + _recStringView.Y;
|
||||
_editor.SetCaretEnd(index, new Point(x2, y2));
|
||||
}
|
||||
|
||||
public void OnMouseDoubleClick()
|
||||
{
|
||||
if (_editor.CaretIndex < _editor.LastVisibleByte)
|
||||
{
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point caretLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretEnd(index, caretLocation);
|
||||
}
|
||||
}
|
||||
|
||||
public void Focus()
|
||||
{
|
||||
int caretIndex = _editor.CaretIndex;
|
||||
Point caretLocation = GetCaretLocation(caretIndex);
|
||||
_editor.SetCaretStart(caretIndex, caretLocation);
|
||||
}
|
||||
|
||||
public void Update(int startPositionX, Rectangle area)
|
||||
{
|
||||
_recStringView = new Rectangle(startPositionX, area.Y, (int)(_editor.CharSize.Width * (float)_editor.BytesPerLine), (int)_editor.CharSize.Height - 2);
|
||||
_recStringView.X += _editor.EntityMargin;
|
||||
}
|
||||
|
||||
public void Paint(Graphics g, int index, int startIndex)
|
||||
{
|
||||
Point byteColumnAndRow = GetByteColumnAndRow(index);
|
||||
if (_editor.IsSelected(index + startIndex))
|
||||
{
|
||||
PaintByteAsSelected(g, byteColumnAndRow, index + startIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
PaintByte(g, byteColumnAndRow, index + startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private void PaintByteAsSelected(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush brush = new SolidBrush(_editor.SelectionBackColor);
|
||||
SolidBrush brush2 = new SolidBrush(_editor.SelectionForeColor);
|
||||
RectangleF bound = GetBound(point);
|
||||
char byteAsChar = _editor.GetByteAsChar(index);
|
||||
string s = (char.IsControl(byteAsChar) ? "." : byteAsChar.ToString());
|
||||
g.FillRectangle(brush, bound);
|
||||
g.DrawString(s, _editor.Font, brush2, bound, _stringFormat);
|
||||
}
|
||||
|
||||
private void PaintByte(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush brush = new SolidBrush(_editor.ForeColor);
|
||||
RectangleF bound = GetBound(point);
|
||||
char byteAsChar = _editor.GetByteAsChar(index);
|
||||
string s = (char.IsControl(byteAsChar) ? "." : byteAsChar.ToString());
|
||||
g.DrawString(s, _editor.Font, brush, bound, _stringFormat);
|
||||
}
|
||||
|
||||
private Point GetCaretLocation(int index)
|
||||
{
|
||||
int x = _recStringView.X + (int)_editor.CharSize.Width * (index % _editor.BytesPerLine);
|
||||
int y = _recStringView.Y + _recStringView.Height * ((index - (_editor.FirstVisibleByte + index % _editor.BytesPerLine)) / _editor.BytesPerLine);
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
private void HandleUserRemove()
|
||||
{
|
||||
int selectionStart = _editor.SelectionStart;
|
||||
Point caretLocation = GetCaretLocation(selectionStart);
|
||||
_editor.RemoveSelectedBytes();
|
||||
_editor.SetCaretStart(selectionStart, caretLocation);
|
||||
}
|
||||
|
||||
private void HandleUserInput(char key)
|
||||
{
|
||||
if (_editor.CaretFocused)
|
||||
{
|
||||
HandleUserRemove();
|
||||
byte item = Convert.ToByte(key);
|
||||
if (_editor.HexTableLength <= 0)
|
||||
{
|
||||
_editor.AppendByte(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.InsertByte(_editor.CaretIndex, item);
|
||||
}
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point caretLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, caretLocation);
|
||||
}
|
||||
}
|
||||
|
||||
private Point GetByteColumnAndRow(int index)
|
||||
{
|
||||
int x = index % _editor.BytesPerLine;
|
||||
int y = index / _editor.BytesPerLine;
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
private RectangleF GetBound(Point point)
|
||||
{
|
||||
return new RectangleF(_recStringView.X + point.X * (int)_editor.CharSize.Width, _recStringView.Y + point.Y * _recStringView.Height, _editor.CharSize.Width, _recStringView.Height);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user