More Util methods -> extension methods

This commit is contained in:
adelikat 2014-07-03 19:20:34 +00:00
parent bf88be8c72
commit fd80c62cdb
15 changed files with 76 additions and 58 deletions
BizHawk.Client.Common
BizHawk.Client.EmuHawk
BizHawk.Common
BizHawk.Emulation.Common/Database
BizHawk.Emulation.Cores/Consoles
BizHawk.Emulation.DiscSystem

View File

@ -2,7 +2,7 @@
using System.IO;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
// IDEA: put filesizes in DB too. then scans can go real quick by only scanning filesizes that match (and then scanning filesizes that dont match, in case of an emergency)
@ -81,7 +81,7 @@ namespace BizHawk.Client.Common
fs.Read(buffer, 0, (int)len);
}
rff.Hash = Util.Hash_SHA1(buffer, 0, (int)len);
rff.Hash = buffer.HashSHA1(0, (int)len);
dict[rff.Hash] = rff;
_files.Add(rff);
return rff;

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Xml;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Common.IOExtensions;
using BizHawk.Emulation.Common;
@ -94,7 +95,7 @@ namespace BizHawk.Client.Common
}
}
ret.GI.Hash = Util.Hash_SHA1(HashStream.GetBuffer(), 0, (int)HashStream.Length);
ret.GI.Hash = HashStream.GetBuffer().HashSHA1(0, (int)HashStream.Length);
HashStream.Close();
if (OriginalIndex != null)
{

View File

@ -8,10 +8,13 @@ using System.Text;
using System.Threading;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Common.IOExtensions;
using BizHawk.Client.Common;
using BizHawk.Bizware.BizwareGL;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
using BizHawk.Emulation.Cores.Atari.Atari2600;
@ -3080,8 +3083,8 @@ namespace BizHawk.Client.EmuHawk
Global.Emulator.CoreComm.RomStatusDetails = string.Format(
"{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n",
loader.Game.Name,
Util.Hash_SHA1(loader.Rom.RomData),
Util.Hash_MD5(loader.Rom.RomData));
loader.Rom.RomData.HashSHA1(),
loader.Rom.RomData.HashMD5());
}
if (Global.Emulator.BoardName != null)

View File

@ -8,9 +8,8 @@ using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.SNES;
using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Common.Components;
@ -93,7 +92,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var s in waveform)
bw.Write(s);
bw.Flush();
string md5 = Util.Hash_MD5(waveformTemp);
string md5 = waveformTemp.HashMD5();
if (!PSGEntryTable.ContainsKey(md5))
{

View File

@ -2,6 +2,7 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace BizHawk.Common.BufferExtensions
{
@ -183,6 +184,34 @@ namespace BizHawk.Common.BufferExtensions
return (result >= pattern.Length - 1);
}
public static string HashMD5(this byte[] data, int offset, int len)
{
using (var md5 = MD5.Create())
{
md5.ComputeHash(data, offset, len);
return md5.Hash.BytesToHexString();
}
}
public static string HashMD5(this byte[] data)
{
return HashMD5(data, 0, data.Length);
}
public static string HashSHA1(this byte[] data, int offset, int len)
{
using (var sha1 = SHA1.Create())
{
sha1.ComputeHash(data, offset, len);
return sha1.Hash.BytesToHexString();
}
}
public static string HashSHA1(this byte[] data)
{
return HashSHA1(data, 0, data.Length);
}
#region Helpers
private static int Hex2Int(char c)

View File

@ -20,34 +20,6 @@ namespace BizHawk.Common
public static char* HexConvPtr { get; set; }
public static string Hash_MD5(byte[] data, int offset, int len)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
md5.ComputeHash(data, offset, len);
return md5.Hash.BytesToHexString();
}
}
public static string Hash_MD5(byte[] data)
{
return Hash_MD5(data, 0, data.Length);
}
public static string Hash_SHA1(byte[] data, int offset, int len)
{
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
sha1.ComputeHash(data, offset, len);
return sha1.Hash.BytesToHexString();
}
}
public static string Hash_SHA1(byte[] data)
{
return Hash_SHA1(data, 0, data.Length);
}
public static bool IsPowerOfTwo(int x)
{
if (x == 0 || x == 1)

View File

@ -217,13 +217,13 @@ namespace BizHawk.Emulation.Common
return new GameInfo(cgi);
}
hash = Util.Hash_MD5(romData);
hash = romData.HashMD5();
if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi);
}
hash = Util.Hash_SHA1(romData);
hash = romData.HashSHA1();
if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi);

View File

@ -2,6 +2,8 @@
using BizHawk.Common;
using BizHawk.Common.NumberExtensions;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Components.M6502;
@ -311,8 +313,8 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
string.Format(
"{0}\r\nSHA1:{1}\r\nMD5:{2}\r\nMapper Impl \"{3}\"",
this._game.Name,
Util.Hash_SHA1(Rom),
Util.Hash_MD5(Rom),
Rom.HashSHA1(),
Rom.HashMD5(),
_mapper.GetType());
}

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Atari.Atari2600
@ -165,7 +166,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
Name = _game.Name,
System = "A26",
MetaData = "m=" + _mapper.GetType().ToString().Split('.').ToList().Last(),
Hash = Util.Hash_SHA1(Rom),
Hash = Rom.HashSHA1(),
Region = _game.Region,
Status = RomStatus.Unknown
};

View File

@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
using Newtonsoft.Json;
using System.ComponentModel;
namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
{
@ -145,8 +146,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
CoreComm.RomStatusDetails = string.Format("{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n",
game.Name,
Util.Hash_SHA1(romdata), Util.Hash_MD5(romdata)
);
romdata.HashSHA1(),
romdata.HashMD5());
{
byte[] buff = new byte[32];

View File

@ -4,9 +4,11 @@ using System.IO;
using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Emulation.Common;
//TODO - redo all timekeeping in terms of master clock
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
//TODO - redo all timekeeping in terms of master clock
namespace BizHawk.Emulation.Cores.Nintendo.NES
{
[CoreAttributes(
@ -546,9 +548,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
//now that we know we have an iNES header, we can try to ignore it.
hash_sha1 = "sha1:" + Util.Hash_SHA1(file, 16, file.Length - 16);
hash_sha1 = "sha1:" + file.HashSHA1(16, file.Length - 16);
hash_sha1_several.Add(hash_sha1);
hash_md5 = "md5:" + Util.Hash_MD5(file, 16, file.Length - 16);
hash_md5 = "md5:" + file.HashMD5(16, file.Length - 16);
LoadWriteLine("Found iNES header:");
LoadWriteLine(iNesHeaderInfo.ToString());
@ -572,10 +574,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
msTemp.Write(file, 16 + 16 * 1024, iNesHeaderInfo.chr_size * 1024); //add chr
msTemp.Flush();
var bytes = msTemp.ToArray();
var hash = "sha1:" + Util.Hash_SHA1(bytes, 0, bytes.Length);
var hash = "sha1:" + bytes.HashSHA1(0, bytes.Length);
LoadWriteLine(" PRG (8KB) + CHR hash: {0}", hash);
hash_sha1_several.Add(hash);
hash = "md5:" + Util.Hash_MD5(bytes, 0, bytes.Length);
hash = "md5:" + bytes.HashMD5(0, bytes.Length);
LoadWriteLine(" PRG (8KB) + CHR hash: {0}", hash);
}
}

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
namespace BizHawk.Emulation.Cores.Nintendo.NES
{
@ -80,13 +80,18 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
}
if (chunks.TryGetValue("MAPR", out tmp))
{
ci.board_type = Encoding.ASCII.GetString(tmp);
}
ci.board_type = ci.board_type.TrimEnd('\0');
ci.board_type = "UNIF_" + ci.board_type;
if (chunks.TryGetValue("BATR", out tmp))
{
// apparently, this chunk just existing means battery is yes
ci.wram_battery = true;
}
// is there any way using System.Security.Cryptography.SHA1 to compute the hash of
// prg concatentated with chr? i couldn't figure it out, so this implementation is dumb
@ -96,7 +101,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
ms.Write(chrrom, 0, chrrom.Length);
ms.Close();
byte[] all = ms.ToArray();
ci.sha1 = "sha1:" + Util.Hash_SHA1(all, 0, all.Length);
ci.sha1 = "sha1:" + all.HashSHA1(0, all.Length);
}
// other code will expect this

View File

@ -333,7 +333,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
if (game["SGB"])
{
sgbRomData = CoreComm.CoreFileProvider.GetFirmware("SNES", "Rom_SGB", true, "SGB Rom is required for SGB emulation.");
game.FirmwareHash = Util.Hash_SHA1(sgbRomData);
game.FirmwareHash = sgbRomData.HashSHA1();
}
ScanlineHookManager = new MyScanlineHookManager(this);

View File

@ -4,8 +4,11 @@ using System.Globalization;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.Components;
using BizHawk.Emulation.Cores.Components.H6280;
using BizHawk.Emulation.DiscSystem;
@ -131,7 +134,7 @@ namespace BizHawk.Emulation.Cores.PCEngine
throw new Exception();
}
game.FirmwareHash = Util.Hash_SHA1(rom);
game.FirmwareHash = rom.HashSHA1();
Init(game, rom);
// the default RomStatusDetails don't do anything with Disc

View File

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
//main apis for emulator core routine use
@ -297,7 +297,7 @@ namespace BizHawk.Emulation.DiscSystem
for (int s = 0; s < 512 && s < track.length_aba; s++)
ReadABA_2352(track.Indexes[1].aba + s, buffer, s * 2352);
return Util.Hash_MD5(buffer, 0, lba_len * 2352);
return buffer.HashMD5(0, lba_len * 2352);
}
return "no data track found";
}