From 01584431c41b7057e45bf2c5fe94e28505d03641 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 19 Mar 2020 21:28:32 -0500 Subject: [PATCH] NesHawk - cleanup Unif.cs --- .../Consoles/Nintendo/NES/NES.cs | 14 +-- .../Consoles/Nintendo/NES/Unif.cs | 93 ++++++++++--------- BizHawk.sln.DotSettings | 1 + 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index 6f3cbacdd5..125841f4c3 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -339,9 +339,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { unif = new Unif(new MemoryStream(file)); LoadWriteLine("Found UNIF header:"); - LoadWriteLine(unif.CartInfo); + LoadWriteLine(unif.Cart); LoadWriteLine("Since this is UNIF we can confidently parse PRG/CHR banks to hash."); - hash_sha1 = unif.CartInfo.Sha1; + hash_sha1 = unif.Cart.Sha1; hash_sha1_several.Add(hash_sha1); LoadWriteLine("headerless rom hash: {0}", hash_sha1); } @@ -519,8 +519,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } else if (unif != null) { - if (choice.PrgSize == -1) choice.PrgSize = unif.CartInfo.PrgSize; - if (choice.ChrSize == -1) choice.ChrSize = unif.CartInfo.ChrSize; + if (choice.PrgSize == -1) choice.PrgSize = unif.Cart.PrgSize; + if (choice.ChrSize == -1) choice.ChrSize = unif.Cart.ChrSize; // unif has no wram\vram sizes; hope the board impl can figure it out... if (choice.VramSize == -1) choice.VramSize = 0; if (choice.WramSize == -1) choice.WramSize = 0; @@ -549,7 +549,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES if (unif != null) { LoadWriteLine("Using information from UNIF header"); - choice = unif.CartInfo; + choice = unif.Cart; //ok, i have this Q-Boy rom with no VROM and no VRAM. //we also certainly have games with VROM and no VRAM. //looks like FCEUX policy is to allocate 8KB of chr ram no matter what UNLESS certain flags are set. but what's the justification for this? please leave a note if you go debugging in it again. @@ -684,8 +684,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } else if (unif != null) { - Board.Rom = unif.PRG; - Board.Vrom = unif.CHR; + Board.Rom = unif.Prg; + Board.Vrom = unif.Chr; } else { diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs index f916a47097..35c8f608ad 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs @@ -15,101 +15,106 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES /// public class Unif { - CartInfo ci = new CartInfo(); - byte[] prgrom; - byte[] chrrom; + private Dictionary Chunks { get; } = new Dictionary(); - Dictionary chunks = new Dictionary(); - - void TryAdd(Stream s, string key) + private void TryAdd(Stream s, string key) { - if (!chunks.TryGetValue(key, out var data)) + if (!Chunks.TryGetValue(key, out var data)) + { return; + } + s.Write(data, 0, data.Length); } public Unif(Stream s) { - BinaryReader br = new BinaryReader(s, Encoding.ASCII); + var br = new BinaryReader(s, Encoding.ASCII); - if (!Encoding.ASCII.GetBytes("UNIF").SequenceEqual(br.ReadBytes(4))) + if (!Encoding.ASCII.GetBytes("UNIF") + .SequenceEqual(br.ReadBytes(4))) + { throw new Exception("Missing \"UNIF\" header mark!"); + } + int ver = br.ReadInt32(); - //if (ver != 7) - // throw new Exception($"Unknown UNIF version {ver}!"); + Console.WriteLine("Processing Version {0} UNIF...", ver); br.ReadBytes(32 - 4 - 4); while (br.PeekChar() > 0) { - string chunkid = Encoding.ASCII.GetString(br.ReadBytes(4)); + string chunkId = Encoding.ASCII.GetString(br.ReadBytes(4)); int length = br.ReadInt32(); - byte[] chunkdata = br.ReadBytes(length); - chunks.Add(chunkid, chunkdata); + byte[] chunkData = br.ReadBytes(length); + Chunks.Add(chunkId, chunkData); } - MemoryStream prgs = new MemoryStream(); - MemoryStream chrs = new MemoryStream(); + var prgs = new MemoryStream(); + var chrs = new MemoryStream(); for (int i = 0; i < 16; i++) { TryAdd(prgs, $"PRG{i:X1}"); TryAdd(chrs, $"CHR{i:X1}"); } + prgs.Close(); chrs.Close(); - prgrom = prgs.ToArray(); - chrrom = chrs.ToArray(); + Prg = prgs.ToArray(); + Chr = chrs.ToArray(); - ci.PrgSize = (short)(prgrom.Length / 1024); - ci.ChrSize = (short)(chrrom.Length / 1024); + Cart.PrgSize = (short)(Prg.Length / 1024); + Cart.ChrSize = (short)(Chr.Length / 1024); - if (chunks.TryGetValue("MIRR", out var tmp)) + if (Chunks.TryGetValue("MIRR", out var tmp)) { switch (tmp[0]) { - case 0: // hmirror - ci.PadH = 0; - ci.PadV = 1; + case 0: // h mirror + Cart.PadH = 0; + Cart.PadV = 1; break; - case 1: // vmirror - ci.PadH = 1; - ci.PadV = 0; + case 1: // v mirror + Cart.PadH = 1; + Cart.PadV = 0; break; } } - if (chunks.TryGetValue("MAPR", out tmp)) + if (Chunks.TryGetValue("MAPR", out tmp)) { - ci.BoardType = new BinaryReader(new MemoryStream(tmp)).ReadStringUtf8NullTerminated(); + Cart.BoardType = new BinaryReader(new MemoryStream(tmp)).ReadStringUtf8NullTerminated(); } - ci.BoardType = ci.BoardType.TrimEnd('\0'); - ci.BoardType = "UNIF_" + ci.BoardType; + Cart.BoardType = Cart.BoardType.TrimEnd('\0'); + Cart.BoardType = "UNIF_" + Cart.BoardType; - if (chunks.TryGetValue("BATR", out tmp)) + if (Chunks.TryGetValue("BATR", out _)) { // apparently, this chunk just existing means battery is yes - ci.WramBattery = true; + Cart.WramBattery = 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 + // prg concatenated with chr? i couldn't figure it out, so this implementation is dumb { - MemoryStream ms = new MemoryStream(); - ms.Write(prgrom, 0, prgrom.Length); - ms.Write(chrrom, 0, chrrom.Length); + var ms = new MemoryStream(); + ms.Write(Prg, 0, Prg.Length); + ms.Write(Chr, 0, Chr.Length); ms.Close(); - byte[] all = ms.ToArray(); - ci.Sha1 = "sha1:" + all.HashSHA1(0, all.Length); + var all = ms.ToArray(); + Cart.Sha1 = "sha1:" + all.HashSHA1(0, all.Length); } // other code will expect this - if (chrrom.Length == 0) - chrrom = null; + if (Chr.Length == 0) + { + Chr = null; + } } - public CartInfo CartInfo => ci; - public byte[] PRG => prgrom; - public byte[] CHR => chrrom; + public CartInfo Cart { get; } = new CartInfo(); + public byte[] Prg { get; } + public byte[] Chr { get; } } } diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 58fa7bfce8..e3c4f9b9a3 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -533,6 +533,7 @@ True True True + True True True True