Improve basic HexViewer

This commit is contained in:
Benito Palacios Sánchez 2016-03-25 18:31:47 +01:00
parent 353c96ba39
commit 23b0246e60
2 changed files with 87 additions and 45 deletions

View File

@ -36,7 +36,7 @@
// txtHex
//
this.txtHex.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtHex.Dock = System.Windows.Forms.DockStyle.Left;
this.txtHex.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtHex.Font = new System.Drawing.Font("Consolas", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtHex.Location = new System.Drawing.Point(0, 0);
this.txtHex.Multiline = true;
@ -57,7 +57,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(809, 272);
this.ClientSize = new System.Drawing.Size(809, 275);
this.Controls.Add(this.vScrollBar1);
this.Controls.Add(this.txtHex);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

View File

@ -1,86 +1,128 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
// VisorHexBasic.cs
//
// Author:
// Benito Palacios Sánchez (aka pleonex) <benito356@gmail.com>
//
// Copyright (c) 2016 Benito Palacios Sánchez
//
// 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.IO;
using System.Windows.Forms;
using Ekona;
using System.Drawing;
using System.Text;
namespace Tinke
{
public partial class VisorHexBasic : Form
{
string file;
UInt32 offset;
UInt32 size;
private const int BytesPerRow = 0x10; // Not tested with different values.
private string file;
private uint offset;
private uint size;
public VisorHexBasic(string file, UInt32 offset, UInt32 size)
{
InitializeComponent();
this.Text = Tools.Helper.GetTranslation("Sistema", "S41");
this.file = file;
this.offset = offset;
this.size = size;
this.offset = offset;
vScrollBar1.Maximum = (int)size / 0x10;
Show_Hex(0);
Initialize();
}
txtHex.Select(0, 0);
public VisorHexBasic(sFile gameFile)
{
this.file = gameFile.path;
this.size = gameFile.size;
this.offset = gameFile.offset;
Initialize();
}
private void Initialize()
{
InitializeComponent();
Text = Tools.Helper.GetTranslation("Sistema", "S41");
txtHex.Font = new Font(FontFamily.GenericMonospace, 11F);
txtHex.ReadOnly = true;
txtHex.HideSelection = false;
this.Resize += new System.EventHandler(this.VisorHex_Resize);
//txtHex.MouseWheel += (sender, e) =>
// vScrollBar1.Value += vScrollBar1.SmallChange * e.Delta;
Resize += VisorHex_Resize;
vScrollBar1.Maximum = (int)size / BytesPerRow;
ShowHex(0);
}
public void Clear()
{
txtHex.Text = "";
txtHex.Text = string.Empty;
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
Show_Hex(e.NewValue);
ShowHex(e.NewValue);
}
private void Show_Hex(int pos)
private void ShowHex(int pos)
{
BinaryReader br = new BinaryReader(File.OpenRead(file));
br.BaseStream.Position = offset + pos * 16;
br.BaseStream.Position = offset + pos * BytesPerRow;
txtHex.ReadOnly = true;
txtHex.Text = "Offset 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n\r\n";
// Create the header
StringBuilder hexBuilder = new StringBuilder();
hexBuilder.Append("Offset".PadRight(13));
for (int i = 0; i < BytesPerRow; i++)
hexBuilder.AppendFormat(" {0:X2}", i);
hexBuilder.AppendLine();
hexBuilder.AppendLine();
for (int i = 0; i < txtHex.Height / 17; i++)
{
if (br.BaseStream.Length == br.BaseStream.Position ||
br.BaseStream.Position >= offset + size)
break;
int numRows = txtHex.Height / txtHex.Font.Height - 2;
for (int r = 0; r < numRows; r++) {
hexBuilder.AppendFormat("0x{0:X8} ", (pos + r) * BytesPerRow);
string text, ascii; text = ascii = "";
for (int j = 0; j < 0x10; j++)
{
if (br.BaseStream.Position == offset + size)
var asciiBuilder = new StringBuilder(" ");
for (int c = 0; c < BytesPerRow; c++) {
if (br.BaseStream.Position >= offset + size)
break;
byte value = br.ReadByte();
string c = String.Format("{0:X}", value);
text += (c.Length == 2 ? c : '0' + c) + ' ';
ascii += (value > 0x1F && value < 0x7F ? Char.ConvertFromUtf32(value).ToString() + ' ' : ". ");
hexBuilder.AppendFormat(" {0:X2}", value);
if (value > 0x1F && value < 0x7F)
asciiBuilder.Append(" " + (char)value);
else
asciiBuilder.Append(" .");
}
txtHex.Text += "0x" + String.Format("{0:X}", (i + pos) * 16).PadLeft(8, '0') + " " + text.PadRight(52, ' ') + ascii + "\r\n";
text = "";
ascii = "";
hexBuilder.Append(asciiBuilder.ToString());
if (r != numRows - 1)
hexBuilder.AppendLine();
}
br.Close();
txtHex.Text = hexBuilder.ToString();
txtHex.Select(0, 0);
}
private void VisorHex_Resize(object sender, EventArgs e)
{
txtHex.Width = this.Width - 30;
Show_Hex(vScrollBar1.Value);
ShowHex(vScrollBar1.Value);
}
}
}