From 0ccf30396d55065c9f58492984c5897ab9316617 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 19 May 2020 17:43:35 +1000 Subject: [PATCH] Use modern `??=`, `is`, casts, etc. in DiscSystem --- .../CDFS/ISODirectoryNode.cs | 14 ++++---- .../CDFS/ISOFile.cs | 12 +++---- .../DiscDecoding.cs | 12 ++----- .../DiscExtensions.cs | 6 +--- .../DiscFormats/Blobs/Blob_RawFile.cs | 10 ++---- .../DiscFormats/Blobs/Blob_WaveFile.cs | 15 +++------ .../DiscFormats/Blobs/RiffMaster.cs | 15 ++++----- .../DiscFormats/CUE/CUE_Compile.cs | 32 +++++++++---------- .../DiscFormats/MDS_Format.cs | 16 +++------- .../DiscHasher.cs | 4 +-- .../DiscIdentifier.cs | 2 +- .../DiscMountJob.cs | 3 +- 12 files changed, 54 insertions(+), 87 deletions(-) diff --git a/src/BizHawk.Emulation.DiscSystem/CDFS/ISODirectoryNode.cs b/src/BizHawk.Emulation.DiscSystem/CDFS/ISODirectoryNode.cs index a36a22a16e..c22a9973f8 100644 --- a/src/BizHawk.Emulation.DiscSystem/CDFS/ISODirectoryNode.cs +++ b/src/BizHawk.Emulation.DiscSystem/CDFS/ISODirectoryNode.cs @@ -98,10 +98,11 @@ namespace BizHawk.Emulation.DiscSystem foreach (KeyValuePair child in this.Children) { // Parse this node - if (child.Key != ISONodeRecord.CURRENT_DIRECTORY && - child.Key != ISONodeRecord.PARENT_DIRECTORY) + if (child.Key != ISONodeRecord.CURRENT_DIRECTORY + && child.Key != ISONodeRecord.PARENT_DIRECTORY + && child.Value is ISODirectoryNode dirNode) { - (child.Value as ISODirectoryNode)?.Parse(s, visited); + dirNode.Parse(s, visited); } } @@ -130,10 +131,11 @@ namespace BizHawk.Emulation.DiscSystem { ISONode n = this.Children[s]; Console.WriteLine(tabs + s); - if (s != ISONodeRecord.CURRENT_DIRECTORY && - s != ISONodeRecord.PARENT_DIRECTORY) + if (s != ISONodeRecord.CURRENT_DIRECTORY + && s != ISONodeRecord.PARENT_DIRECTORY + && n is ISODirectoryNode dirNode) { - (n as ISODirectoryNode)?.Print(depth + 1); + dirNode.Print(depth + 1); } } } diff --git a/src/BizHawk.Emulation.DiscSystem/CDFS/ISOFile.cs b/src/BizHawk.Emulation.DiscSystem/CDFS/ISOFile.cs index 613c0d3496..950584b2db 100644 --- a/src/BizHawk.Emulation.DiscSystem/CDFS/ISOFile.cs +++ b/src/BizHawk.Emulation.DiscSystem/CDFS/ISOFile.cs @@ -167,7 +167,7 @@ namespace BizHawk.Emulation.DiscSystem foreach (var d in dirs) { // process all files in this directory (and recursively process files in sub folders - ISODirectoryNode idn = d.Value as ISODirectoryNode; + ISODirectoryNode idn = (ISODirectoryNode) d.Value; if (dirsParsed.Where(a => a == idn).Count() > 0) continue; @@ -182,13 +182,11 @@ namespace BizHawk.Emulation.DiscSystem { foreach (var n in idn) { - if (n.Value.GetType() == typeof(ISODirectoryNode)) + if (n.Value is ISODirectoryNode subdirNode) { - if (dirsParsed.Where(a => a == n.Value).Count() > 0) - continue; - - dirsParsed.Add(n.Value as ISODirectoryNode); - ProcessDirectoryFiles((n.Value as ISODirectoryNode).Children); + if (dirsParsed.Where(a => a == subdirNode).Count() > 0) continue; + dirsParsed.Add(subdirNode); + ProcessDirectoryFiles(subdirNode.Children); } else { diff --git a/src/BizHawk.Emulation.DiscSystem/DiscDecoding.cs b/src/BizHawk.Emulation.DiscSystem/DiscDecoding.cs index 84ead9a768..1763f5a25d 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscDecoding.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscDecoding.cs @@ -160,15 +160,7 @@ namespace BizHawk.Emulation.DiscSystem } /// could not find source audio for - public byte[] AcquireWaveData(string audioPath) - { - string path = FindAudio(audioPath); - if (path == null) - { - throw new AudioDecoder_Exception($"Could not find source audio for: {Path.GetFileName(audioPath)}"); - } - return new FFMpeg().DecodeAudio(path); - } - + public byte[] AcquireWaveData(string audioPath) => new FFMpeg() + .DecodeAudio(FindAudio(audioPath) ?? throw new AudioDecoder_Exception($"Could not find source audio for: {Path.GetFileName(audioPath)}")); } } \ No newline at end of file diff --git a/src/BizHawk.Emulation.DiscSystem/DiscExtensions.cs b/src/BizHawk.Emulation.DiscSystem/DiscExtensions.cs index b6a48b3789..98aa45c001 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscExtensions.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscExtensions.cs @@ -9,11 +9,7 @@ namespace BizHawk.Emulation.DiscSystem //--- load the disc in a context which will let us abort if it's going to take too long var discMountJob = new DiscMountJob { IN_FromPath = path, IN_SlowLoadAbortThreshold = 8 }; discMountJob.Run(); - var disc = discMountJob.OUT_Disc; - if (disc == null) - { - throw new InvalidOperationException($"Can't find the file specified: {path}"); - } + var disc = discMountJob.OUT_Disc ?? throw new InvalidOperationException($"Can't find the file specified: {path}"); if (discMountJob.OUT_SlowLoadAborted) { diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_RawFile.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_RawFile.cs index 9aa7c2088a..5bbe58f8b6 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_RawFile.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_RawFile.cs @@ -24,11 +24,8 @@ namespace BizHawk.Emulation.DiscSystem private BufferedStream fs; public void Dispose() { - if (fs != null) - { - fs.Dispose(); - fs = null; - } + fs?.Dispose(); + fs = null; } public int Read(long byte_pos, byte[] buffer, int offset, int count) { @@ -39,8 +36,7 @@ namespace BizHawk.Emulation.DiscSystem //really, we need a smarter asynchronous read-ahead buffer. that requires substantially more engineering, some kind of 'DiscUniverse' of carefully managed threads and such. const int buffersize = 2352 * 75 * 2; - if (fs == null) - fs = new BufferedStream(new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read), buffersize); + fs ??= new BufferedStream(new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read), buffersize); long target = byte_pos + Offset; if (fs.Position != target) fs.Position = target; diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs index 02c4360966..ff1085b7aa 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs @@ -45,19 +45,15 @@ namespace BizHawk.Emulation.DiscSystem private BufferedStream fs; public void Dispose() { - if (fs != null) - { - fs.Dispose(); - fs = null; - } + fs?.Dispose(); + fs = null; } public int Read(long byte_pos, byte[] buffer, int offset, int count) { //use quite a large buffer, because normally we will be reading these sequentially but in small chunks. //this enhances performance considerably const int buffersize = 2352 * 75 * 2; - if (fs == null) - fs = new BufferedStream(new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read), buffersize); + fs ??= new BufferedStream(new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read), buffersize); long target = byte_pos + Offset; if (fs.Position != target) fs.Position = target; @@ -93,8 +89,7 @@ namespace BizHawk.Emulation.DiscSystem throw new Blob_WaveFile_Exception("Not a RIFF WAVE file"); } - var fmt = rm.riff.subchunks.FirstOrDefault(chunk => chunk.tag == "fmt ") as RiffMaster.RiffSubchunk_fmt; - if (fmt == null) + if (!(rm.riff.subchunks.FirstOrDefault(chunk => chunk.tag == "fmt ") is RiffMaster.RiffSubchunk_fmt fmt)) { throw new Blob_WaveFile_Exception("Not a valid RIFF WAVE file (missing fmt chunk"); } @@ -116,7 +111,7 @@ namespace BizHawk.Emulation.DiscSystem } //acquire the start of the data chunk - var dataChunk = rm.riff.subchunks.FirstOrDefault(chunk => chunk.tag == "data") as RiffMaster.RiffSubchunk; + var dataChunk = (RiffMaster.RiffSubchunk) rm.riff.subchunks.First(chunk => chunk.tag == "data"); waveDataStreamPos = dataChunk.Position; mDataLength = dataChunk.Length; } diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs index 2aac2497ee..da96c9d60a 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/RiffMaster.cs @@ -181,13 +181,11 @@ namespace BizHawk.Emulation.DiscSystem public RiffChunk GetSubchunk(string tag, string type) { foreach (RiffChunk rc in subchunks) - if (rc.tag == tag) - { - if (type == null) return rc; - RiffContainer cont = rc as RiffContainer; - if (cont != null && cont.type == type) - return rc; - } + { + if (rc.tag != tag) continue; + if (type == null) return rc; + if (rc is RiffContainer cont && cont.type == type) return cont; + } return null; } @@ -242,8 +240,7 @@ namespace BizHawk.Emulation.DiscSystem foreach (RiffChunk chunk in subchunks) { RiffSubchunk rsc = chunk as RiffSubchunk; - if (chunk == null) - throw new FormatException("Invalid subchunk of INFO list"); + if (chunk == null) throw new FormatException("Invalid subchunk of INFO list"); //TODO is this supposed to be a check on `rsc` (i.e. as a type check)? --yoshi dictionary[rsc.tag] = System.Text.Encoding.ASCII.GetString(rsc.ReadAll()); } } diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs index b2b33febff..01bf011c15 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/CUE/CUE_Compile.cs @@ -433,49 +433,49 @@ namespace BizHawk.Emulation.DiscSystem.CUE if (cmd is CUE_File.Command.COMMENT) continue; //CD-text and related - if (cmd is CUE_File.Command.PERFORMER) curr_cdtext.Performer = (cmd as CUE_File.Command.PERFORMER).Value; - if (cmd is CUE_File.Command.SONGWRITER) curr_cdtext.Songwriter = (cmd as CUE_File.Command.SONGWRITER).Value; - if (cmd is CUE_File.Command.TITLE) curr_cdtext.Title = (cmd as CUE_File.Command.TITLE).Value; - if (cmd is CUE_File.Command.ISRC) curr_cdtext.ISRC = (cmd as CUE_File.Command.ISRC).Value; + if (cmd is CUE_File.Command.PERFORMER performerCmd) curr_cdtext.Performer = performerCmd.Value; + if (cmd is CUE_File.Command.SONGWRITER songwriterCmd) curr_cdtext.Songwriter = songwriterCmd.Value; + if (cmd is CUE_File.Command.TITLE titleCmd) curr_cdtext.Title = titleCmd.Value; + if (cmd is CUE_File.Command.ISRC isrcCmd) curr_cdtext.ISRC = isrcCmd.Value; //flags can only be set when a track command is running - if (cmd is CUE_File.Command.FLAGS) + if (cmd is CUE_File.Command.FLAGS flagsCmd) { if (curr_track == null) Warn("Ignoring invalid flag commands outside of a track command"); else //take care to |= it here, so the data flag doesn't get cleared - curr_track.Flags |= (cmd as CUE_File.Command.FLAGS).Flags; + curr_track.Flags |= flagsCmd.Flags; } - if (cmd is CUE_File.Command.TRACK) + if (cmd is CUE_File.Command.TRACK trackCmd) { CloseTrack(); - OpenTrack(cmd as CUE_File.Command.TRACK); + OpenTrack(trackCmd); } - if (cmd is CUE_File.Command.FILE) + if (cmd is CUE_File.Command.FILE fileCmd) { CloseFile(); - OpenFile(cmd as CUE_File.Command.FILE); + OpenFile(fileCmd); } - if (cmd is CUE_File.Command.INDEX) + if (cmd is CUE_File.Command.INDEX indexCmd) { //todo - validate no postgap specified - AddIndex(cmd as CUE_File.Command.INDEX); + AddIndex(indexCmd); } - if (cmd is CUE_File.Command.PREGAP) + if (cmd is CUE_File.Command.PREGAP pregapCmd) { //validate track open //validate no indexes - curr_track.PregapLength = (cmd as CUE_File.Command.PREGAP).Length; + curr_track.PregapLength = pregapCmd.Length; } - if (cmd is CUE_File.Command.POSTGAP) + if (cmd is CUE_File.Command.POSTGAP postgapCmd) { - curr_track.PostgapLength = (cmd as CUE_File.Command.POSTGAP).Length; + curr_track.PostgapLength = postgapCmd.Length; } } diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs index d025ba98bd..21b842c35c 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs @@ -288,16 +288,13 @@ namespace BizHawk.Emulation.DiscSystem } /// header is malformed or identifies file as MDS 2.x, or any track has a DVD mode - public AFile Parse(Stream stream) + public AFile Parse(FileStream stream) { EndianBitConverter bc = EndianBitConverter.CreateForLittleEndian(); EndianBitConverter bcBig = EndianBitConverter.CreateForBigEndian(); bool isDvd = false; - var aFile = new AFile - { - MDSPath = (stream as FileStream).Name - }; + var aFile = new AFile { MDSPath = stream.Name }; stream.Seek(0, SeekOrigin.Begin); @@ -738,12 +735,7 @@ namespace BizHawk.Emulation.DiscSystem // get the blob(s) for this track // its probably a safe assumption that there will be only one blob per track, // but i'm still not 100% sure on this - var tr = (from a in mdsf.TOCEntries - where a.Point == i - select a).FirstOrDefault(); - - if (tr == null) - throw new MDSParseException("BLOB Error!"); + var tr = mdsf.TOCEntries.FirstOrDefault(a => a.Point == i) ?? throw new MDSParseException("BLOB Error!"); List blobstrings = new List(); foreach (var t in tr.ImageFileNamePaths) @@ -817,7 +809,7 @@ namespace BizHawk.Emulation.DiscSystem CUE.SS_Base sBase = null; // get the current blob from the BlobIndex - Disc.Blob_RawFile currBlob = BlobIndex[currBlobIndex] as Disc.Blob_RawFile; + Disc.Blob_RawFile currBlob = (Disc.Blob_RawFile) BlobIndex[currBlobIndex]; long currBlobLength = currBlob.Length; long currBlobPosition = sector; if (currBlobPosition == currBlobLength) diff --git a/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs b/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs index 83bae205b1..d75773ef26 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs @@ -207,8 +207,8 @@ namespace BizHawk.Emulation.DiscSystem //algorithm from zlib's crc32_combine. read http://www.leapsecond.com/tools/crcomb.c for more private uint crc32_combine(uint crc1, uint crc2, int len2) { - if (even == null) even = new uint[32]; // even-power-of-two zeros operator - if (odd == null) odd = new uint[32]; // odd-power-of-two zeros operator + even ??= new uint[32]; // even-power-of-two zeros operator + odd ??= new uint[32]; // odd-power-of-two zeros operator // degenerate case if (len2 == 0) diff --git a/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs b/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs index 8644e61dc3..190dc10e03 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs @@ -188,7 +188,7 @@ namespace BizHawk.Emulation.DiscSystem if (appId == "PSP GAME") return DiscType.SonyPSP; // in case the appId is not set correctly... - if (iso.Root.Children.Where(a => a.Key == "PSP_GAME").FirstOrDefault().Value as ISODirectoryNode != null) + if (iso.Root.Children.TryGetValue("PSP_GAME", out var node) && node is ISODirectoryNode) return DiscType.SonyPSP; if (sysId == "SEGA SEGASATURN") diff --git a/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs b/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs index 96c8306135..4f1029e248 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscMountJob.cs @@ -133,8 +133,7 @@ namespace BizHawk.Emulation.DiscSystem //parse the cue file var parseJob = new ParseCueJob(); - if (cue_content == null) - cue_content = File.ReadAllText(cuePath); + cue_content ??= File.ReadAllText(cuePath); parseJob.IN_CueString = cue_content; bool okParse = true; try { parseJob.Run(parseJob); }