From 2c9d8bba4081ebf80a88592019128521d6f423e5 Mon Sep 17 00:00:00 2001 From: adelikat Date: Wed, 26 Feb 2020 15:51:29 -0600 Subject: [PATCH] cleanup DSKIDentifier.cs, misc cleanups in Emulation.Common --- .../BasicServiceProvider.cs | 7 +- .../Base Implementations/CodeDataLog.cs | 26 ++- .../Base Implementations/MemoryDomainList.cs | 17 +- BizHawk.Emulation.Common/DSKIdentifier.cs | 160 +++++++++--------- BizHawk.Emulation.Common/Database/Database.cs | 43 ++--- BizHawk.Emulation.Common/Database/GameInfo.cs | 7 +- BizHawk.Emulation.Common/Extensions.cs | 7 +- .../Interfaces/IMemoryCallbackSystem.cs | 2 +- .../Utilities/ISynchronizingAudioBuffer.cs | 2 +- .../Sound/Utilities/SpeexResampler.cs | 4 +- BizHawk.Emulation.Common/TextState.cs | 12 +- BizHawk.sln.DotSettings | 3 + 12 files changed, 124 insertions(+), 166 deletions(-) diff --git a/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs b/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs index 1c2dcdb6ab..ce6110fcd5 100644 --- a/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs +++ b/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs @@ -65,12 +65,7 @@ namespace BizHawk.Emulation.Common public object GetService(Type t) { - if (_services.TryGetValue(t, out var service)) - { - return service; - } - - return null; + return _services.TryGetValue(t, out var service) ? service : null; } public bool HasService() diff --git a/BizHawk.Emulation.Common/Base Implementations/CodeDataLog.cs b/BizHawk.Emulation.Common/Base Implementations/CodeDataLog.cs index 3634edc9fc..dcd542b7bf 100644 --- a/BizHawk.Emulation.Common/Base Implementations/CodeDataLog.cs +++ b/BizHawk.Emulation.Common/Base Implementations/CodeDataLog.cs @@ -61,10 +61,7 @@ namespace BizHawk.Emulation.Common /// /// Whether the CDL is tracking a block with the given name /// - public bool Has(string blockName) - { - return ContainsKey(blockName); - } + public bool Has(string blockName) => ContainsKey(blockName); /// /// This is just a hook, if needed, to readily suspend logging, without having to rewire the core @@ -195,17 +192,16 @@ namespace BizHawk.Emulation.Common { var br = new BinaryReader(s); string id = br.ReadString(); - if (id == "BIZHAWK-CDL-1") + switch (id) { - SubType = "PCE"; - } - else if (id == "BIZHAWK-CDL-2") - { - SubType = br.ReadString().TrimEnd(' '); - } - else - { - throw new InvalidDataException("File is not a BizHawk CDL file!"); + case "BIZHAWK-CDL-1": + SubType = "PCE"; + break; + case "BIZHAWK-CDL-2": + SubType = br.ReadString().TrimEnd(' '); + break; + default: + throw new InvalidDataException("File is not a BizHawk CDL file!"); } int count = br.ReadInt32(); @@ -216,9 +212,7 @@ namespace BizHawk.Emulation.Common byte[] data = br.ReadBytes(len); this[key] = data; } - } - } } diff --git a/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs b/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs index 9158786005..2eb561a5d8 100644 --- a/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs +++ b/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs @@ -16,7 +16,7 @@ namespace BizHawk.Emulation.Common public bool Has(string name) { - return this.FirstOrDefault((md) => md.Name == name) != null; + return this.Any(md => md.Name == name); } public MemoryDomainList(IList domains) @@ -28,16 +28,7 @@ namespace BizHawk.Emulation.Common public MemoryDomain MainMemory { - get - { - if (_mainMemory != null) - { - return _mainMemory; - } - - return this.First(); - } - + get => _mainMemory ?? this.First(); set => _mainMemory = value; } @@ -83,9 +74,7 @@ namespace BizHawk.Emulation.Common private static void TryMerge(MemoryDomain dest, MemoryDomain src, Action func) where T : MemoryDomain { - var d1 = dest as T; - var s1 = src as T; - if (d1 != null && s1 != null) + if (dest is T d1 && src is T s1) { func(d1, s1); } diff --git a/BizHawk.Emulation.Common/DSKIdentifier.cs b/BizHawk.Emulation.Common/DSKIdentifier.cs index 6a3e85a0a6..e35a59802f 100644 --- a/BizHawk.Emulation.Common/DSKIdentifier.cs +++ b/BizHawk.Emulation.Common/DSKIdentifier.cs @@ -7,15 +7,15 @@ namespace BizHawk.Emulation.Common /// This is here because (for probably good reason) there does not appear to be a route /// to BizHawk.Emulation.Cores from BizHawk.Emulation.Common /// - public class DSKIdentifier + public class DskIdentifier { + private readonly byte[] _data; + private string _possibleIdent = ""; + /// /// Default fallthrough to AppleII /// - public string IdentifiedSystem = "AppleII"; - private string PossibleIdent = ""; - - private readonly byte[] data; + public string IdentifiedSystem { get; set; } = "AppleII"; // dsk header public byte NumberOfTracks { get; set; } @@ -23,28 +23,27 @@ namespace BizHawk.Emulation.Common public int[] TrackSizes { get; set; } // state - public int CylinderCount; - public int SideCount; - public int BytesPerTrack; + public int SideCount { get; set; } + public int BytesPerTrack { get; set; } - public Track[] Tracks = null; + public Track[] Tracks { get; set; } - public DSKIdentifier(byte[] imageData) + public DskIdentifier(byte[] imageData) { - data = imageData; + _data = imageData; ParseDskImage(); } private void ParseDskImage() { - string ident = Encoding.ASCII.GetString(data, 0, 16).ToUpper(); + string ident = Encoding.ASCII.GetString(_data, 0, 16).ToUpper(); if (ident.Contains("MV - CPC")) { - ParseDSK(); + ParseDsk(); } else if (ident.Contains("EXTENDED CPC DSK")) { - ParseEDSK(); + ParseEDsk(); } else { @@ -66,7 +65,7 @@ namespace BizHawk.Emulation.Common if (trk.Sectors[0].SectorData[0] == 0 && trk.Sectors[0].SectorData[1] == 0 && trk.Sectors[0].SectorData[2] == 40) { - PossibleIdent = "ZXSpectrum"; + _possibleIdent = "ZXSpectrum"; } } @@ -90,9 +89,7 @@ namespace BizHawk.Emulation.Common // check for bootable status if (trk.Sectors[0].SectorData != null && trk.Sectors[0].SectorData.Length > 0) { - int chksm = trk.Sectors[0].GetChecksum256(); - - switch (chksm) + switch (trk.Sectors[0].GetChecksum256()) { case 3: IdentifiedSystem = "ZXSpectrum"; @@ -155,7 +152,7 @@ namespace BizHawk.Emulation.Common case 65: case 193: // possible CPC custom - PossibleIdent = "AmstradCPC"; + _possibleIdent = "AmstradCPC"; break; } @@ -177,13 +174,13 @@ namespace BizHawk.Emulation.Common IdentifiedSystem = "ZXSpectrum"; return; default: - PossibleIdent = "ZXSpectrum"; + _possibleIdent = "ZXSpectrum"; break; } } else { - PossibleIdent = "ZXSpectrum"; + _possibleIdent = "ZXSpectrum"; } break; case 1: @@ -199,7 +196,7 @@ namespace BizHawk.Emulation.Common } else { - PossibleIdent = "ZXSpectrum"; + _possibleIdent = "ZXSpectrum"; } break; } @@ -216,56 +213,59 @@ namespace BizHawk.Emulation.Common } // last chance. use the possible value - if (IdentifiedSystem == "AppleII" && PossibleIdent != "") + if (IdentifiedSystem == "AppleII" && _possibleIdent != "") { IdentifiedSystem = "ZXSpectrum"; } } - private void ParseDSK() + private void ParseDsk() { - NumberOfTracks = data[0x30]; - NumberOfSides = data[0x31]; + NumberOfTracks = _data[0x30]; + NumberOfSides = _data[0x31]; TrackSizes = new int[NumberOfTracks * NumberOfSides]; Tracks = new Track[NumberOfTracks * NumberOfSides]; int pos = 0x32; for (int i = 0; i < NumberOfTracks * NumberOfSides; i++) { - TrackSizes[i] = (ushort)(data[pos] | data[pos + 1] << 8); + TrackSizes[i] = (ushort)(_data[pos] | _data[pos + 1] << 8); } pos = 0x100; for (int i = 0; i < NumberOfTracks * NumberOfSides; i++) { if (TrackSizes[i] == 0) { - Tracks[i] = new Track(); - Tracks[i].Sectors = new Sector[0]; + Tracks[i] = new Track { Sectors = new Sector[0] }; continue; } int p = pos; - Tracks[i] = new Track(); - Tracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); + Tracks[i] = new Track + { + TrackIdent = Encoding.ASCII.GetString(_data, p, 12) + }; p += 16; - Tracks[i].TrackNumber = data[p++]; - Tracks[i].SideNumber = data[p++]; + Tracks[i].TrackNumber = _data[p++]; + Tracks[i].SideNumber = _data[p++]; p += 2; - Tracks[i].SectorSize = data[p++]; - Tracks[i].NumberOfSectors = data[p++]; - Tracks[i].GAP3Length = data[p++]; - Tracks[i].FillerByte = data[p++]; - int dpos = pos + 0x100; + Tracks[i].SectorSize = _data[p++]; + Tracks[i].NumberOfSectors = _data[p++]; + Tracks[i].Gap3Length = _data[p++]; + Tracks[i].FillerByte = _data[p++]; + int dPos = pos + 0x100; Tracks[i].Sectors = new Sector[Tracks[i].NumberOfSectors]; for (int s = 0; s < Tracks[i].NumberOfSectors; s++) { - Tracks[i].Sectors[s] = new Sector(); + Tracks[i].Sectors[s] = new Sector + { + TrackNumber = _data[p++], + SideNumber = _data[p++], + SectorID = _data[p++], + SectorSize = _data[p++], + Status1 = _data[p++], + Status2 = _data[p++], + ActualDataByteLength = (ushort) (_data[p] | _data[p + 1] << 8) + }; - Tracks[i].Sectors[s].TrackNumber = data[p++]; - Tracks[i].Sectors[s].SideNumber = data[p++]; - Tracks[i].Sectors[s].SectorID = data[p++]; - Tracks[i].Sectors[s].SectorSize = data[p++]; - Tracks[i].Sectors[s].Status1 = data[p++]; - Tracks[i].Sectors[s].Status2 = data[p++]; - Tracks[i].Sectors[s].ActualDataByteLength = (ushort)(data[p] | data[p + 1] << 8); p += 2; if (Tracks[i].Sectors[s].SectorSize == 0) { @@ -286,64 +286,64 @@ namespace BizHawk.Emulation.Common Tracks[i].Sectors[s].SectorData = new byte[Tracks[i].Sectors[s].ActualDataByteLength]; for (int b = 0; b < Tracks[i].Sectors[s].ActualDataByteLength; b++) { - Tracks[i].Sectors[s].SectorData[b] = data[dpos + b]; + Tracks[i].Sectors[s].SectorData[b] = _data[dPos + b]; } - dpos += Tracks[i].Sectors[s].ActualDataByteLength; + dPos += Tracks[i].Sectors[s].ActualDataByteLength; } pos += TrackSizes[i]; } } - private void ParseEDSK() + private void ParseEDsk() { - NumberOfTracks = data[0x30]; - NumberOfSides = data[0x31]; + NumberOfTracks = _data[0x30]; + NumberOfSides = _data[0x31]; TrackSizes = new int[NumberOfTracks * NumberOfSides]; Tracks = new Track[NumberOfTracks * NumberOfSides]; int pos = 0x34; for (int i = 0; i < NumberOfTracks * NumberOfSides; i++) { - TrackSizes[i] = data[pos++] * 256; + TrackSizes[i] = _data[pos++] * 256; } pos = 0x100; for (int i = 0; i < NumberOfTracks * NumberOfSides; i++) { if (TrackSizes[i] == 0) { - Tracks[i] = new Track(); - Tracks[i].Sectors = new Sector[0]; + Tracks[i] = new Track { Sectors = new Sector[0] }; continue; } int p = pos; - Tracks[i] = new Track(); - Tracks[i].TrackIdent = Encoding.ASCII.GetString(data, p, 12); + Tracks[i] = new Track { TrackIdent = Encoding.ASCII.GetString(_data, p, 12) }; p += 16; - Tracks[i].TrackNumber = data[p++]; - Tracks[i].SideNumber = data[p++]; - Tracks[i].DataRate = data[p++]; - Tracks[i].RecordingMode = data[p++]; - Tracks[i].SectorSize = data[p++]; - Tracks[i].NumberOfSectors = data[p++]; - Tracks[i].GAP3Length = data[p++]; - Tracks[i].FillerByte = data[p++]; - int dpos = pos + 0x100; + Tracks[i].TrackNumber = _data[p++]; + Tracks[i].SideNumber = _data[p++]; + Tracks[i].DataRate = _data[p++]; + Tracks[i].RecordingMode = _data[p++]; + Tracks[i].SectorSize = _data[p++]; + Tracks[i].NumberOfSectors = _data[p++]; + Tracks[i].Gap3Length = _data[p++]; + Tracks[i].FillerByte = _data[p++]; + int dPos = pos + 0x100; Tracks[i].Sectors = new Sector[Tracks[i].NumberOfSectors]; for (int s = 0; s < Tracks[i].NumberOfSectors; s++) { - Tracks[i].Sectors[s] = new Sector(); + Tracks[i].Sectors[s] = new Sector + { + TrackNumber = _data[p++], + SideNumber = _data[p++], + SectorID = _data[p++], + SectorSize = _data[p++], + Status1 = _data[p++], + Status2 = _data[p++], + ActualDataByteLength = (ushort) (_data[p] | _data[p + 1] << 8) + }; - Tracks[i].Sectors[s].TrackNumber = data[p++]; - Tracks[i].Sectors[s].SideNumber = data[p++]; - Tracks[i].Sectors[s].SectorID = data[p++]; - Tracks[i].Sectors[s].SectorSize = data[p++]; - Tracks[i].Sectors[s].Status1 = data[p++]; - Tracks[i].Sectors[s].Status2 = data[p++]; - Tracks[i].Sectors[s].ActualDataByteLength = (ushort)(data[p] | data[p + 1] << 8); p += 2; Tracks[i].Sectors[s].SectorData = new byte[Tracks[i].Sectors[s].ActualDataByteLength]; for (int b = 0; b < Tracks[i].Sectors[s].ActualDataByteLength; b++) { - Tracks[i].Sectors[s].SectorData[b] = data[dpos + b]; + Tracks[i].Sectors[s].SectorData[b] = _data[dPos + b]; } if (Tracks[i].Sectors[s].SectorSize <= 7) { @@ -356,7 +356,7 @@ namespace BizHawk.Emulation.Common } } } - dpos += Tracks[i].Sectors[s].ActualDataByteLength; + dPos += Tracks[i].Sectors[s].ActualDataByteLength; } pos += TrackSizes[i]; } @@ -373,7 +373,7 @@ namespace BizHawk.Emulation.Common public byte RecordingMode { get; set; } public byte SectorSize { get; set; } public byte NumberOfSectors { get; set; } - public byte GAP3Length { get; set; } + public byte Gap3Length { get; set; } public byte FillerByte { get; set; } public Sector[] Sectors { get; set; } @@ -383,8 +383,11 @@ namespace BizHawk.Emulation.Common foreach (var s in Sectors) { if (s.SectorID < res) + { res = s.SectorID; + } } + return res; } } @@ -404,10 +407,11 @@ namespace BizHawk.Emulation.Common public int GetChecksum256() { int res = 0; - for (int i = 0; i < SectorData.Length; i++) + foreach (var b in SectorData) { - res += SectorData[i] % 256; + res += b % 256; } + return res; } } diff --git a/BizHawk.Emulation.Common/Database/Database.cs b/BizHawk.Emulation.Common/Database/Database.cs index b69cf7f84a..76bc0d7f0f 100644 --- a/BizHawk.Emulation.Common/Database/Database.cs +++ b/BizHawk.Emulation.Common/Database/Database.cs @@ -140,36 +140,19 @@ namespace BizHawk.Emulation.Common }; // remove a hash type identifier. well don't really need them for indexing (they're just there for human purposes) - switch (items[1].Trim()) + game.Status = items[1].Trim() + switch { - case "B": - game.Status = RomStatus.BadDump; - break; - case "V": - game.Status = RomStatus.BadDump; - break; - case "T": - game.Status = RomStatus.TranslatedRom; - break; - case "O": - game.Status = RomStatus.Overdump; - break; - case "I": - game.Status = RomStatus.Bios; - break; - case "D": - game.Status = RomStatus.Homebrew; - break; - case "H": - game.Status = RomStatus.Hack; - break; - case "U": - game.Status = RomStatus.Unknown; - break; - default: - game.Status = RomStatus.GoodDump; - break; - } + "B" => RomStatus.BadDump, + "V" => RomStatus.BadDump, + "T" => RomStatus.TranslatedRom, + "O" => RomStatus.Overdump, + "I" => RomStatus.Bios, + "D" => RomStatus.Homebrew, + "H" => RomStatus.Hack, + "U" => RomStatus.Unknown, + _ => RomStatus.GoodDump + }; game.Name = items[2]; game.System = items[3]; @@ -342,7 +325,7 @@ namespace BizHawk.Emulation.Common break; case ".DSK": - var dId = new DSKIdentifier(romData); + var dId = new DskIdentifier(romData); game.System = dId.IdentifiedSystem; break; diff --git a/BizHawk.Emulation.Common/Database/GameInfo.cs b/BizHawk.Emulation.Common/Database/GameInfo.cs index 59e2338c1e..f6837cd0e7 100644 --- a/BizHawk.Emulation.Common/Database/GameInfo.cs +++ b/BizHawk.Emulation.Common/Database/GameInfo.cs @@ -82,12 +82,7 @@ namespace BizHawk.Emulation.Common public string OptionValue(string option) { - if (Options.ContainsKey(option)) - { - return Options[option]; - } - - return null; + return Options.ContainsKey(option) ? Options[option] : null; } public int GetIntValue(string option) diff --git a/BizHawk.Emulation.Common/Extensions.cs b/BizHawk.Emulation.Common/Extensions.cs index d56f37b1bd..75bb9da515 100644 --- a/BizHawk.Emulation.Common/Extensions.cs +++ b/BizHawk.Emulation.Common/Extensions.cs @@ -188,12 +188,7 @@ namespace BizHawk.Emulation.Common public static bool CanDisassemble(this IEmulator core) { - if (core == null) - { - return false; - } - - return core.ServiceProvider.HasService(); + return core != null && core.ServiceProvider.HasService(); } public static IDisassemblable AsDisassembler(this IEmulator core) diff --git a/BizHawk.Emulation.Common/Interfaces/IMemoryCallbackSystem.cs b/BizHawk.Emulation.Common/Interfaces/IMemoryCallbackSystem.cs index 97fdf1c5ca..d851b9325f 100644 --- a/BizHawk.Emulation.Common/Interfaces/IMemoryCallbackSystem.cs +++ b/BizHawk.Emulation.Common/Interfaces/IMemoryCallbackSystem.cs @@ -126,6 +126,6 @@ namespace BizHawk.Emulation.Common AccessExecute = 0x04 << 12, CPUUnknown = 0x00 << 8, CPUZero = 0x01 << 8, - DomainUnknown = 0x00, + DomainUnknown = 0x00 } } diff --git a/BizHawk.Emulation.Common/Sound/Utilities/ISynchronizingAudioBuffer.cs b/BizHawk.Emulation.Common/Sound/Utilities/ISynchronizingAudioBuffer.cs index 06f782f64f..ea238ad361 100644 --- a/BizHawk.Emulation.Common/Sound/Utilities/ISynchronizingAudioBuffer.cs +++ b/BizHawk.Emulation.Common/Sound/Utilities/ISynchronizingAudioBuffer.cs @@ -11,7 +11,7 @@ namespace BizHawk.Emulation.Common // returns the number of samples actually supplied, which may not match the number requested // ^^ what the hell is that supposed to mean. - // the entire point of an ISynchronzingAudioBuffer + // the entire point of an ISynchronizingAudioBuffer // is to provide exact amounts of output samples, // even when the input provided varies.... int OutputSamples(short[] buf, int samplesRequested); diff --git a/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs b/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs index 49a287b957..309a9fd8f2 100644 --- a/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs +++ b/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs @@ -10,7 +10,7 @@ namespace BizHawk.Emulation.Common /// public class SpeexResampler : IDisposable, ISoundProvider { - // to accept an ISyncSoundProvder input + // to accept an ISyncSoundProvider input private readonly ISoundProvider _input; // function to call to dispatch output @@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Common QUALITY_MIN = 0, QUALITY_DEFAULT = 4, QUALITY_VOIP = 3, - QUALITY_DESKTOP = 5, + QUALITY_DESKTOP = 5 } private static class LibSpeexDSP diff --git a/BizHawk.Emulation.Common/TextState.cs b/BizHawk.Emulation.Common/TextState.cs index ac8a820f5b..d33a612920 100644 --- a/BizHawk.Emulation.Common/TextState.cs +++ b/BizHawk.Emulation.Common/TextState.cs @@ -113,10 +113,10 @@ namespace BizHawk.Emulation.Common { return new TextStateFPtrs { - Save = new TextStateFPtrs.DataFunction(Save), + Save = Save, Load = null, - EnterSection = new TextStateFPtrs.SectionFunction(EnterSectionSave), - ExitSection = new TextStateFPtrs.SectionFunction(ExitSection) + EnterSection = EnterSectionSave, + ExitSection = ExitSection }; } @@ -125,9 +125,9 @@ namespace BizHawk.Emulation.Common return new TextStateFPtrs { Save = null, - Load = new TextStateFPtrs.DataFunction(Load), - EnterSection = new TextStateFPtrs.SectionFunction(EnterSectionLoad), - ExitSection = new TextStateFPtrs.SectionFunction(ExitSection) + Load = Load, + EnterSection = EnterSectionLoad, + ExitSection = ExitSection }; } } diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 81a024d5f2..3c6986b9f6 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -220,6 +220,8 @@ True True True + True + True True True True @@ -492,6 +494,7 @@ True True True + True True True \ No newline at end of file