Convert some more util methods to extension methods

This commit is contained in:
adelikat 2014-07-03 19:05:56 +00:00
parent 1c0eca190e
commit bf88be8c72
6 changed files with 51 additions and 43 deletions

View File

@ -5,7 +5,9 @@ using System.Text;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Common.IOExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common.MovieConversionExtensions;
@ -403,7 +405,7 @@ namespace BizHawk.Client.Common
byte[] md5 = DecodeBlob(blob);
if (md5 != null && md5.Length == 16)
{
m.Header[MD5] = Util.BytesToHexString(md5).ToLower();
m.Header[MD5] = md5.BytesToHexString().ToLower();
}
else
{
@ -596,7 +598,7 @@ namespace BizHawk.Client.Common
uint firstFrameOffset = r.ReadUInt32();
// 020 16-byte md5sum of the ROM used
byte[] md5 = r.ReadBytes(16);
m.Header[MD5] = Util.BytesToHexString(md5).ToLower();
m.Header[MD5] = md5.BytesToHexString().ToLower();
// 030 4-byte little-endian unsigned int: version of the emulator used
uint emuVersion = r.ReadUInt32();
m.Comments.Add(EMULATIONORIGIN + " FCEU " + emuVersion);
@ -1294,7 +1296,7 @@ namespace BizHawk.Client.Common
byte[] md5 = r.ReadBytes(16);
// Discard the second 16 bytes.
r.ReadBytes(16);
m.Header[MD5] = Util.BytesToHexString(md5).ToLower();
m.Header[MD5] = md5.BytesToHexString().ToLower();
// 030 64-byte Filename of the ROM used (with extension)
string gameName = NullTerminated(r.ReadStringFixedAscii(64));
m.Header[HeaderKeys.GAMENAME] = gameName;
@ -1460,7 +1462,7 @@ namespace BizHawk.Client.Common
m.Header[HeaderKeys.GAMENAME] = gameName;
// 00e4-00f3: binary: rom MD5 digest
byte[] md5 = r.ReadBytes(16);
m.Header[MD5] = string.Format("{0:x8}", Util.BytesToHexString(md5).ToLower());
m.Header[MD5] = string.Format("{0:x8}", md5.BytesToHexString().ToLower());
var controllers = new SimpleController { Type = new ControllerDefinition { Name = "SMS Controller" }};
/*
76543210

View File

@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;
namespace BizHawk.Common.BufferExtensions
{
@ -12,6 +13,7 @@ namespace BizHawk.Common.BufferExtensions
{
writer.Write("{0:X2}", b);
}
writer.WriteLine();
}
@ -155,6 +157,32 @@ namespace BizHawk.Common.BufferExtensions
}
}
/// <summary>
/// Converts bytes to an uppercase string of hex numbers in upper case without any spacing or anything
/// </summary>
public static string BytesToHexString(this byte[] bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
{
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}
public static bool FindBytes(this byte[] array, byte[] pattern)
{
var fidx = 0;
int result = Array.FindIndex(array, 0, array.Length, (byte b) =>
{
fidx = (b == pattern[fidx]) ? fidx + 1 : 0;
return (fidx == pattern.Length);
});
return (result >= pattern.Length - 1);
}
#region Helpers
private static int Hex2Int(char c)

View File

@ -4,6 +4,7 @@ using System.Globalization;
using System.IO;
using BizHawk.Common.IOExtensions;
using BizHawk.Common.BufferExtensions;
namespace BizHawk.Common
{
@ -226,7 +227,7 @@ namespace BizHawk.Common
else
{
var temp = val ?? new byte[0];
_tw.WriteLine("{0} {1}", name, Util.BytesToHexString(temp));
_tw.WriteLine("{0} {1}", name, temp.BytesToHexString());
}
}
@ -288,7 +289,7 @@ namespace BizHawk.Common
else
{
var temp = val ?? new short[0];
_tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.ShortBufferToByteBuffer(temp)));
_tw.WriteLine("{0} {1}", name, Util.ShortBufferToByteBuffer(temp).BytesToHexString());
}
}
@ -310,7 +311,7 @@ namespace BizHawk.Common
else
{
var temp = val ?? new ushort[0];
_tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UshortBufferToByteBuffer(temp)));
_tw.WriteLine("{0} {1}", name, Util.UshortBufferToByteBuffer(temp).BytesToHexString());
}
}
@ -352,7 +353,7 @@ namespace BizHawk.Common
else
{
var temp = val ?? new int[0];
_tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.IntBufferToByteBuffer(temp)));
_tw.WriteLine("{0} {1}", name, Util.IntBufferToByteBuffer(temp).BytesToHexString());
}
}
@ -394,7 +395,7 @@ namespace BizHawk.Common
else
{
var temp = val ?? new uint[0];
_tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UintBufferToByteBuffer(temp)));
_tw.WriteLine("{0} {1}", name, Util.UintBufferToByteBuffer(temp).BytesToHexString());
}
}

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using BizHawk.Common.BufferExtensions;
namespace BizHawk.Common
{
public static unsafe class Util
@ -16,18 +18,6 @@ namespace BizHawk.Common
HexConvPtr = (char*)HexConvHandle.AddrOfPinnedObject().ToPointer();
}
public static bool FindBytes(byte[] array, byte[] pattern)
{
int fidx = 0;
int result = Array.FindIndex(array, 0, array.Length, (byte b) =>
{
fidx = (b == pattern[fidx]) ? fidx + 1 : 0;
return (fidx == pattern.Length);
});
return (result >= pattern.Length - 1);
}
public static char* HexConvPtr { get; set; }
public static string Hash_MD5(byte[] data, int offset, int len)
@ -35,7 +25,7 @@ namespace BizHawk.Common
using (var md5 = System.Security.Cryptography.MD5.Create())
{
md5.ComputeHash(data, offset, len);
return BytesToHexString(md5.Hash);
return md5.Hash.BytesToHexString();
}
}
@ -49,7 +39,7 @@ namespace BizHawk.Common
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
sha1.ComputeHash(data, offset, len);
return BytesToHexString(sha1.Hash);
return sha1.Hash.BytesToHexString();
}
}
@ -81,21 +71,6 @@ namespace BizHawk.Common
return 0;
}
/// <summary>
/// Converts bytes to an uppercase string of hex numbers in upper case without any spacing or anything
/// //could be extension method
/// </summary>
public static string BytesToHexString(byte[] bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
{
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}
// Could be extension method
public static byte[] HexStringToBytes(string str)
{

View File

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
namespace BizHawk.Emulation.Common
{
@ -238,7 +240,7 @@ namespace BizHawk.Emulation.Common
Console.WriteLine(
"Game was not in DB. CRC: {0:X8} MD5: {1}",
CRC32.Calculate(romData),
Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(romData)));
System.Security.Cryptography.MD5.Create().ComputeHash(romData).BytesToHexString());
var ext = Path.GetExtension(fileName).ToUpperInvariant();

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
namespace BizHawk.Emulation.Cores.Atari.Atari2600
{
@ -257,7 +257,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
// 3E cart bankswitching is triggered by storing the bank number
// in address 3E using 'STA $3E', commonly followed by an
// immediate mode LDA
return Util.FindBytes(rom, new byte[] { 0x85, 0x3E, 0xA9, 0x00 }); // STA $3E; LDA #$00
return rom.FindBytes(new byte[] { 0x85, 0x3E, 0xA9, 0x00 }); // STA $3E; LDA #$00
}
private static bool IsProbably3F(byte[] rom)
@ -443,12 +443,12 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
private static bool ContainsAny(byte[] rom, IEnumerable<byte[]> signatures)
{
return signatures.Any(signature => Util.FindBytes(rom, signature));
return signatures.Any(signature => rom.FindBytes(signature));
}
private static bool ContainsAll(byte[] rom, IEnumerable<byte[]> signatures)
{
return signatures.All(signature => Util.FindBytes(rom, signature));
return signatures.All(signature => rom.FindBytes(signature));
}
}
}