refactor: new code style in Util.cs

This commit is contained in:
goat 2021-03-31 15:41:22 +02:00
parent 6085952f26
commit b50a977738

View file

@ -2,24 +2,49 @@ using System;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using Dalamud.Game; using Dalamud.Game;
using Serilog; using Serilog;
namespace Dalamud { namespace Dalamud
internal static class Util { {
public static void DumpMemory(IntPtr offset, int len = 512) { /// <summary>
/// Class providing various helper methods for use in Dalamud and plugins.
/// </summary>
public static class Util
{
/// <summary>
/// Gets the assembly version of Dalamud.
/// </summary>
public static string AssemblyVersion { get; } = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString();
/// <summary>
/// Read memory from an offset and hexdump them via Serilog.
/// </summary>
/// <param name="offset">The offset to read from.</param>
/// <param name="len">The length to read.</param>
public static void DumpMemory(IntPtr offset, int len = 512)
{
var data = new byte[len]; var data = new byte[len];
Marshal.Copy(offset, data, 0, len); Marshal.Copy(offset, data, 0, len);
Log.Information(ByteArrayToHex(data)); Log.Information(ByteArrayToHex(data));
} }
public static string ByteArrayToHex(byte[] bytes, int offset = 0, int bytesPerLine = 16) { /// <summary>
/// Create a hexdump of the provided bytes.
/// </summary>
/// <param name="bytes">The bytes to hexdump.</param>
/// <param name="offset">The offset in the byte array to start at.</param>
/// <param name="bytesPerLine">The amount of bytes to display per line.</param>
/// <returns>The generated hexdump in string form.</returns>
public static string ByteArrayToHex(byte[] bytes, int offset = 0, int bytesPerLine = 16)
{
if (bytes == null) return string.Empty; if (bytes == null) return string.Empty;
var hexChars = "0123456789ABCDEF".ToCharArray(); var hexChars = "0123456789ABCDEF".ToCharArray();
var offsetBlock = 8 + 3; var offsetBlock = 8 + 3;
var byteBlock = offsetBlock + bytesPerLine * 3 + (bytesPerLine - 1) / 8 + 2; var byteBlock = offsetBlock + (bytesPerLine * 3) + ((bytesPerLine - 1) / 8) + 2;
var lineLength = byteBlock + bytesPerLine + Environment.NewLine.Length; var lineLength = byteBlock + bytesPerLine + Environment.NewLine.Length;
var line = (new string(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray(); var line = (new string(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
@ -27,7 +52,8 @@ namespace Dalamud {
var sb = new StringBuilder(numLines * lineLength); var sb = new StringBuilder(numLines * lineLength);
for (var i = 0; i < bytes.Length; i += bytesPerLine) { for (var i = 0; i < bytes.Length; i += bytesPerLine)
{
var h = i + offset; var h = i + offset;
line[0] = hexChars[(h >> 28) & 0xF]; line[0] = hexChars[(h >> 28) & 0xF];
@ -42,18 +68,22 @@ namespace Dalamud {
var hexColumn = offsetBlock; var hexColumn = offsetBlock;
var charColumn = byteBlock; var charColumn = byteBlock;
for (var j = 0; j < bytesPerLine; j++) { for (var j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++; if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytes.Length) { if (i + j >= bytes.Length)
{
line[hexColumn] = ' '; line[hexColumn] = ' ';
line[hexColumn + 1] = ' '; line[hexColumn + 1] = ' ';
line[charColumn] = ' '; line[charColumn] = ' ';
} else { }
else
{
var by = bytes[i + j]; var by = bytes[i + j];
line[hexColumn] = hexChars[(by >> 4) & 0xF]; line[hexColumn] = hexChars[(by >> 4) & 0xF];
line[hexColumn + 1] = hexChars[by & 0xF]; line[hexColumn + 1] = hexChars[by & 0xF];
line[charColumn] = by < 32 ? '.' : (char) by; line[charColumn] = by < 32 ? '.' : (char)by;
} }
hexColumn += 3; hexColumn += 3;
@ -65,7 +95,5 @@ namespace Dalamud {
return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray()); return sb.ToString().TrimEnd(Environment.NewLine.ToCharArray());
} }
public static string AssemblyVersion { get; } = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString();
} }
} }