From d8dc06cbd2a81ce7ce437329008f6aca41c0e513 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 3 Oct 2021 15:48:50 +1000 Subject: [PATCH] Don't unnecessarily recompute rom hashes --- .../Database/Database.cs | 19 +++++------- .../Consoles/Atari/2600/Atari2600.cs | 9 +++--- .../Consoles/Atari/A7800Hawk/A7800Hawk.cs | 2 +- .../Consoles/GCE/Vectrex/VectrexHawk.cs | 5 +-- .../Consoles/Nintendo/GBHawk/GBHawk.cs | 31 +++++++++---------- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/BizHawk.Emulation.Common/Database/Database.cs b/src/BizHawk.Emulation.Common/Database/Database.cs index 05cfd23255..7418fa5f2e 100644 --- a/src/BizHawk.Emulation.Common/Database/Database.cs +++ b/src/BizHawk.Emulation.Common/Database/Database.cs @@ -191,20 +191,20 @@ namespace BizHawk.Emulation.Common { acquire.WaitOne(); - var hash = $"{CRC32.Calculate(romData):X8}"; - if (DB.TryGetValue(hash, out var cgi)) + var hashCRC32 = $"{CRC32.Calculate(romData):X8}"; + if (DB.TryGetValue(hashCRC32, out var cgi)) { return new GameInfo(cgi); } - hash = romData.HashMD5(); - if (DB.TryGetValue(hash, out cgi)) + var hashMD5 = romData.HashMD5(); + if (DB.TryGetValue(hashMD5, out cgi)) { return new GameInfo(cgi); } - hash = romData.HashSHA1(); - if (DB.TryGetValue(hash, out cgi)) + var hashSHA1 = romData.HashSHA1(); + if (DB.TryGetValue(hashSHA1, out cgi)) { return new GameInfo(cgi); } @@ -212,15 +212,12 @@ namespace BizHawk.Emulation.Common // rom is not in database. make some best-guesses var game = new GameInfo { - Hash = hash, + Hash = hashSHA1, Status = RomStatus.NotInDatabase, NotInDatabase = true }; - Console.WriteLine( - "Game was not in DB. CRC: {0:X8} MD5: {1}", - CRC32.Calculate(romData), - System.Security.Cryptography.MD5.Create().ComputeHash(romData).BytesToHexString()); + Console.WriteLine($"Game was not in DB. CRC: {hashCRC32} MD5: {hashMD5}"); var ext = Path.GetExtension(fileName)?.ToUpperInvariant(); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs index e8c6b10593..c6330bf145 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs @@ -34,16 +34,17 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 game.AddOption("m", DetectMapper(rom)); } - if (Rom.HashSHA1() == "3A77DB43B6583E8689435F0F14AA04B9E57BDDED" || - Rom.HashSHA1() == "E986E1818E747BEB9B33CE4DFF1CDC6B55BDB620" || - Rom.HashSHA1() == "982B8016B393A9AA7DD110295A53C4612ECF2141") + var romHashSHA1 = Rom.HashSHA1(); + if (romHashSHA1 == "3A77DB43B6583E8689435F0F14AA04B9E57BDDED" || + romHashSHA1 == "E986E1818E747BEB9B33CE4DFF1CDC6B55BDB620" || + romHashSHA1 == "982B8016B393A9AA7DD110295A53C4612ECF2141") { game.RemoveOption("m"); game.AddOption("m", "F8_sega"); } Console.WriteLine("Game uses mapper " + game.GetOptions()["m"]); - Console.WriteLine(Rom.HashSHA1()); + Console.WriteLine(romHashSHA1); RebootCore(); SetupMemoryDomains(); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.cs index 9b19670c79..23d3ace876 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.cs @@ -216,7 +216,7 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk small_flag = true; // additionally, PAL Karateka has bank 6 (actually 2) at 0x4000 - if (rom.HashMD5()=="5E0A1E832BBCEA6FACB832FDE23A440A") + if (hash_md5 == "md5:5E0A1E832BBCEA6FACB832FDE23A440A") { PAL_Kara = true; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawk.cs b/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawk.cs index 27cd16e360..3b615fbf2e 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawk.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawk.cs @@ -52,12 +52,13 @@ namespace BizHawk.Emulation.Cores.Consoles.Vectrex /*var Bios =*/ _bios = comm.CoreFileProvider.GetFirmwareOrThrow(new("VEC", "Bios"), "BIOS Not Found, Cannot Load"); /*var Mine =*/ minestorm = comm.CoreFileProvider.GetFirmwareOrThrow(new("VEC", "Minestorm"), "Minestorm Not Found, Cannot Load"); - Console.WriteLine("SHA1:" + rom.HashSHA1(0, rom.Length)); + var romHashSHA1 = rom.HashSHA1(); + Console.WriteLine($"SHA1:{romHashSHA1}"); _rom = rom; // If the game is minstorm, then no cartridge is inserted, retun 0xFF - if (rom.HashSHA1(0, rom.Length) == "65D07426B520DDD3115D40F255511E0FD2E20AE7") + if (romHashSHA1 == "65D07426B520DDD3115D40F255511E0FD2E20AE7") { _rom = new byte[0x8000]; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs index 4e90a16f73..e40ad7aaa5 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs @@ -161,10 +161,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk is_GB_in_GBC = true; // for movie files } - Console.WriteLine("MD5: " + rom.HashMD5(0, rom.Length)); + var romHashMD5 = rom.HashMD5(); + Console.WriteLine($"MD5: {romHashMD5}"); Console.WriteLine("SHA1: " + rom.HashSHA1(0, rom.Length)); _rom = rom; - string mppr = Setup_Mapper(); + var mppr = Setup_Mapper(romHashMD5); if (cart_RAM != null) { cart_RAM_vbls = new byte[cart_RAM.Length]; } if (mppr == "MBC7") @@ -482,7 +483,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } } - public string Setup_Mapper() + public string Setup_Mapper(string romHashMD5) { // setup up mapper based on header entry string mppr; @@ -541,23 +542,21 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } // special case for multi cart mappers - if ((_rom.HashMD5(0, _rom.Length) == "97122B9B183AAB4079C8D36A4CE6E9C1") || - (_rom.HashMD5(0, _rom.Length) == "9FB9C42CF52DCFDCFBAD5E61AE1B5777") || - (_rom.HashMD5(0, _rom.Length) == "CF1F58AB72112716D3C615A553B2F481") || - (_rom.HashMD5(0, _rom.Length) == "D0C6FFC3602D91C0B2B1B0461553DE33") // Bomberman Selection - ) + if (romHashMD5 == "97122B9B183AAB4079C8D36A4CE6E9C1" + || romHashMD5 == "9FB9C42CF52DCFDCFBAD5E61AE1B5777" + || romHashMD5 == "CF1F58AB72112716D3C615A553B2F481" + || romHashMD5 == "D0C6FFC3602D91C0B2B1B0461553DE33") // Bomberman Selection { Console.WriteLine("Using Multi-Cart Mapper"); mapper = new MapperMBC1Multi(); } // Wisdom Tree does not identify their mapper, so use hash instead - if ((_rom.HashMD5(0, _rom.Length) == "2C07CAEE51A1F0C91C72C7C6F380B0F6") || // Joshua - (_rom.HashMD5(0, _rom.Length) == "37E017C8D1A45BAB609FB5B43FB64337") || // Spiritual Warfare - (_rom.HashMD5(0, _rom.Length) == "AB1FA0ED0207B1D0D5F401F0CD17BEBF") || // Exodus - (_rom.HashMD5(0, _rom.Length) == "BA2AC3587B3E1B36DE52E740274071B0") || // Bible - KJV - (_rom.HashMD5(0, _rom.Length) == "8CDDB8B2DCD3EC1A3FDD770DF8BDA07C") // Bible - NIV - ) + if (romHashMD5 == "2C07CAEE51A1F0C91C72C7C6F380B0F6" // Joshua + || romHashMD5 == "37E017C8D1A45BAB609FB5B43FB64337" // Spiritual Warfare + || romHashMD5 == "AB1FA0ED0207B1D0D5F401F0CD17BEBF" // Exodus + || romHashMD5 == "BA2AC3587B3E1B36DE52E740274071B0" // Bible - KJV + || romHashMD5 == "8CDDB8B2DCD3EC1A3FDD770DF8BDA07C") // Bible - NIV { Console.WriteLine("Using Wisdom Tree Mapper"); mapper = new MapperWT(); @@ -565,12 +564,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } // special case for bootlegs - if ((_rom.HashMD5(0, _rom.Length) == "CAE0998A899DF2EE6ABA8E7695C2A096")) + if (romHashMD5 == "CAE0998A899DF2EE6ABA8E7695C2A096") { Console.WriteLine("Using RockMan 8 (Unlicensed) Mapper"); mapper = new MapperRM8(); } - if ((_rom.HashMD5(0, _rom.Length) == "D3C1924D847BC5D125BF54C2076BE27A")) + if (romHashMD5 == "D3C1924D847BC5D125BF54C2076BE27A") { Console.WriteLine("Using Sachen 1 (Unlicensed) Mapper"); mapper = new MapperSachen1();