Use modern `??=`, `is`, casts, etc. in DiscSystem

This commit is contained in:
YoshiRulz 2020-05-19 17:43:35 +10:00 committed by James Groom
parent ae22cd9f1b
commit 0ccf30396d
12 changed files with 54 additions and 87 deletions

View File

@ -98,10 +98,11 @@ namespace BizHawk.Emulation.DiscSystem
foreach (KeyValuePair<string, ISONode> 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);
}
}
}

View File

@ -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
{

View File

@ -160,15 +160,7 @@ namespace BizHawk.Emulation.DiscSystem
}
/// <exception cref="AudioDecoder_Exception">could not find source audio for <paramref name="audioPath"/></exception>
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)}"));
}
}

View File

@ -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)
{

View File

@ -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;

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -288,16 +288,13 @@ namespace BizHawk.Emulation.DiscSystem
}
/// <exception cref="MDSParseException">header is malformed or identifies file as MDS 2.x, or any track has a DVD mode</exception>
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<string> blobstrings = new List<string>();
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)

View File

@ -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)

View File

@ -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")

View File

@ -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); }