From 4cf29c5169d81900c3c57b9293d2b0b3b2d1d92a Mon Sep 17 00:00:00 2001 From: adelikat Date: Wed, 26 Feb 2020 14:53:36 -0600 Subject: [PATCH] Emulation.DiscSystem - cleanup - use extension method, using declarations --- .../DiscFormats/Blobs/RiffMaster.cs | 6 +- .../DiscFormats/CCD_format.cs | 24 +++-- .../DiscFormats/CUE/CUE_Compile.cs | 18 ++-- .../DiscIdentifier.cs | 4 +- .../Internal/Jobs/LoadSBIJob.cs | 90 +++++++++---------- 5 files changed, 66 insertions(+), 76 deletions(-) diff --git a/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs b/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs index 2453b54907..2debb85c31 100644 --- a/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs +++ b/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs @@ -19,10 +19,8 @@ namespace BizHawk.Emulation.DiscSystem public void WriteFile(string fname) { - using (FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write, FileShare.Read)) - { - WriteStream(fs); - } + using FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write, FileShare.Read); + WriteStream(fs); } public Stream BaseStream; diff --git a/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs b/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs index 15f50476c6..3e2fdb54ec 100644 --- a/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs +++ b/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs @@ -421,21 +421,17 @@ namespace BizHawk.Emulation.DiscSystem string imgPath = Path.ChangeExtension(path, ".img"); string subPath = Path.ChangeExtension(path, ".sub"); var buf2448 = new byte[2448]; - DiscSectorReader dsr = new DiscSectorReader(disc); + DiscSectorReader dsr = new DiscSectorReader(disc); - using (var imgFile = File.OpenWrite(imgPath)) - using (var subFile = File.OpenWrite(subPath)) - { - - int nLBA = disc.Session1.LeadoutLBA; - for (int lba = 0; lba < nLBA; lba++) - { - dsr.ReadLBA_2448(lba, buf2448, 0); - imgFile.Write(buf2448, 0, 2352); - subFile.Write(buf2448, 2352, 96); - } - } - + using var imgFile = File.OpenWrite(imgPath); + using var subFile = File.OpenWrite(subPath); + int nLBA = disc.Session1.LeadoutLBA; + for (int lba = 0; lba < nLBA; lba++) + { + dsr.ReadLBA_2448(lba, buf2448, 0); + imgFile.Write(buf2448, 0, 2352); + subFile.Write(buf2448, 2352, 96); + } } class SS_CCD : ISectorSynthJob2448 diff --git a/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs b/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs index f65e958261..f031849a86 100644 --- a/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs +++ b/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs @@ -251,17 +251,15 @@ namespace BizHawk.Emulation.DiscSystem.CUE //TODO - fix exception-throwing inside //TODO - verify stream-disposing semantics var fs = File.OpenRead(choice); - using (var blob = new Disc.Blob_WaveFile()) + using var blob = new Disc.Blob_WaveFile(); + try { - try - { - blob.Load(fs); - cfi.Type = CompiledCueFileType.WAVE; - } - catch - { - cfi.Type = CompiledCueFileType.DecodeAudio; - } + blob.Load(fs); + cfi.Type = CompiledCueFileType.WAVE; + } + catch + { + cfi.Type = CompiledCueFileType.DecodeAudio; } } else if (blobPathExt == ".APE") cfi.Type = CompiledCueFileType.DecodeAudio; diff --git a/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs b/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs index 8e7e0d30ff..a7c1896f9b 100644 --- a/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs +++ b/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs @@ -357,10 +357,10 @@ namespace BizHawk.Emulation.DiscSystem { var data = ReadSectorCached(lba); if (data == null) return false; - byte[] cmp = System.Text.Encoding.ASCII.GetBytes(s); + byte[] cmp = Encoding.ASCII.GetBytes(s); byte[] cmp2 = new byte[cmp.Length]; Buffer.BlockCopy(data, n, cmp2, 0, cmp.Length); - return System.Linq.Enumerable.SequenceEqual(cmp, cmp2); + return cmp.SequenceEqual(cmp2); } private bool SectorContains(string s, int lba = 0) diff --git a/BizHawk.Emulation.DiscSystem/Internal/Jobs/LoadSBIJob.cs b/BizHawk.Emulation.DiscSystem/Internal/Jobs/LoadSBIJob.cs index 2413825e10..9a8cfee4e7 100644 --- a/BizHawk.Emulation.DiscSystem/Internal/Jobs/LoadSBIJob.cs +++ b/BizHawk.Emulation.DiscSystem/Internal/Jobs/LoadSBIJob.cs @@ -23,58 +23,56 @@ namespace BizHawk.Emulation.DiscSystem.SBI /// file at does not contain valid header or contains misformatted record public void Run() { - using (var fs = File.OpenRead(IN_Path)) + using var fs = File.OpenRead(IN_Path); + BinaryReader br = new BinaryReader(fs); + string sig = br.ReadStringFixedAscii(4); + if (sig != "SBI\0") + throw new SBIParseException("Missing magic number"); + + SubQPatchData ret = new SubQPatchData(); + List bytes = new List(); + + //read records until done + for (; ; ) { - BinaryReader br = new BinaryReader(fs); - string sig = br.ReadStringFixedAscii(4); - if (sig != "SBI\0") - throw new SBIParseException("Missing magic number"); + //graceful end + if (fs.Position == fs.Length) + break; - SubQPatchData ret = new SubQPatchData(); - List bytes = new List(); - - //read records until done - for (; ; ) + if (fs.Position + 4 > fs.Length) throw new SBIParseException("Broken record"); + var m = BCD2.BCDToInt(br.ReadByte()); + var s = BCD2.BCDToInt(br.ReadByte()); + var f = BCD2.BCDToInt(br.ReadByte()); + var ts = new Timestamp(m, s, f); + ret.ABAs.Add(ts.Sector); + int type = br.ReadByte(); + switch (type) { - //graceful end - if (fs.Position == fs.Length) + case 1: //Q0..Q9 + if (fs.Position + 10 > fs.Length) throw new SBIParseException("Broken record"); + for (int i = 0; i <= 9; i++) bytes.Add(br.ReadByte()); + for (int i = 10; i <= 11; i++) bytes.Add(-1); break; - - if (fs.Position + 4 > fs.Length) throw new SBIParseException("Broken record"); - var m = BCD2.BCDToInt(br.ReadByte()); - var s = BCD2.BCDToInt(br.ReadByte()); - var f = BCD2.BCDToInt(br.ReadByte()); - var ts = new Timestamp(m, s, f); - ret.ABAs.Add(ts.Sector); - int type = br.ReadByte(); - switch (type) - { - case 1: //Q0..Q9 - if (fs.Position + 10 > fs.Length) throw new SBIParseException("Broken record"); - for (int i = 0; i <= 9; i++) bytes.Add(br.ReadByte()); - for (int i = 10; i <= 11; i++) bytes.Add(-1); - break; - case 2: //Q3..Q5 - if (fs.Position + 3 > fs.Length) throw new SBIParseException("Broken record"); - for (int i = 0; i <= 2; i++) bytes.Add(-1); - for (int i = 3; i <= 5; i++) bytes.Add(br.ReadByte()); - for (int i = 6; i <= 11; i++) bytes.Add(-1); - break; - case 3: //Q7..Q9 - if (fs.Position + 3 > fs.Length) throw new SBIParseException("Broken record"); - for (int i = 0; i <= 6; i++) bytes.Add(-1); - for (int i = 7; i <= 9; i++) bytes.Add(br.ReadByte()); - for (int i = 10; i <= 11; i++) bytes.Add(-1); - break; - default: - throw new SBIParseException("Broken record"); - } + case 2: //Q3..Q5 + if (fs.Position + 3 > fs.Length) throw new SBIParseException("Broken record"); + for (int i = 0; i <= 2; i++) bytes.Add(-1); + for (int i = 3; i <= 5; i++) bytes.Add(br.ReadByte()); + for (int i = 6; i <= 11; i++) bytes.Add(-1); + break; + case 3: //Q7..Q9 + if (fs.Position + 3 > fs.Length) throw new SBIParseException("Broken record"); + for (int i = 0; i <= 6; i++) bytes.Add(-1); + for (int i = 7; i <= 9; i++) bytes.Add(br.ReadByte()); + for (int i = 10; i <= 11; i++) bytes.Add(-1); + break; + default: + throw new SBIParseException("Broken record"); } - - ret.subq = bytes.ToArray(); - - OUT_Data = ret; } + + ret.subq = bytes.ToArray(); + + OUT_Data = ret; } } } \ No newline at end of file