From 17df5673d244e4371a77f883ea632c663b951efe Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Thu, 21 Jul 2022 04:09:50 +1000 Subject: [PATCH] Enable MA0098 and fix noncompliance "Use indexer instead of LINQ methods" --- Common.ruleset | 2 +- src/BizHawk.Common/Extensions/PathExtensions.cs | 15 ++------------- .../Extensions/StringExtensions.cs | 17 +++++++++++++++++ .../CallbackBasedTraceBuffer.cs | 3 +-- .../Base Implementations/MemoryDomainList.cs | 2 +- .../Arcades/MAME/MAME.IInputPollable.cs | 9 ++++----- .../Hardware/Display/AmstradGateArray.cs | 14 ++------------ .../AmstradCPC/Media/Disk/FloppyDisk.cs | 2 +- .../AmstradCPC/Media/Tape/CDT/CdtConverter.cs | 12 ++++++------ .../Computers/Commodore64/C64.cs | 2 +- .../SinclairSpectrum/Media/Disk/FloppyDisk.cs | 2 +- .../Consoles/Atari/2600/Atari2600.cs | 4 ++-- .../Consoles/Fairchild/ChannelF/ChannelF.cs | 2 +- .../StringExtensions/StringExtensionTests.cs | 9 +++++++++ 14 files changed, 49 insertions(+), 46 deletions(-) diff --git a/Common.ruleset b/Common.ruleset index cbbeb53ab7..b8b254bf9d 100644 --- a/Common.ruleset +++ b/Common.ruleset @@ -402,7 +402,7 @@ - + diff --git a/src/BizHawk.Common/Extensions/PathExtensions.cs b/src/BizHawk.Common/Extensions/PathExtensions.cs index 7ec59db415..e091a0cad3 100644 --- a/src/BizHawk.Common/Extensions/PathExtensions.cs +++ b/src/BizHawk.Common/Extensions/PathExtensions.cs @@ -2,7 +2,6 @@ using System.Text; using System.IO; using System.Reflection; -using System.Linq; using BizHawk.Common.StringExtensions; @@ -79,18 +78,8 @@ namespace BizHawk.Common.PathExtensions //TODO merge this with the Windows implementation in MakeRelativeTo static FileAttributes GetPathAttribute(string path1) { - var di = new DirectoryInfo(path1.Split('|').First()); - if (di.Exists) - { - return FileAttributes.Directory; - } - - var fi = new FileInfo(path1.Split('|').First()); - if (fi.Exists) - { - return FileAttributes.Normal; - } - + if (Directory.Exists(path1.SubstringBefore('|'))) return FileAttributes.Directory; + if (File.Exists(path1.SubstringBefore('|'))) return FileAttributes.Normal; throw new FileNotFoundException(); } var path = new StringBuilder(260 /* = MAX_PATH */); diff --git a/src/BizHawk.Common/Extensions/StringExtensions.cs b/src/BizHawk.Common/Extensions/StringExtensions.cs index 2b835ae066..dbed3436d5 100644 --- a/src/BizHawk.Common/Extensions/StringExtensions.cs +++ b/src/BizHawk.Common/Extensions/StringExtensions.cs @@ -82,6 +82,23 @@ namespace BizHawk.Common.StringExtensions return index < 0 ? notFoundValue : str.Substring(index + delimiter.Length, str.Length - index - delimiter.Length); } + /// + /// the substring of after the last occurrence of , or + /// the original if not found + /// + public static string SubstringAfterLast(this string str, char delimiter) + => str.SubstringAfterLast(delimiter, notFoundValue: str); + + /// + /// the substring of after the last occurrence of , or + /// if not found + /// + public static string SubstringAfterLast(this string str, char delimiter, string notFoundValue) + { + var index = str.LastIndexOf(delimiter); + return index < 0 ? notFoundValue : str.Substring(index + 1, str.Length - index - 1); + } + /// /// the substring of before the first occurrence of , or /// the original if not found diff --git a/src/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs b/src/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs index 3ffc681755..7f65081db9 100644 --- a/src/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs +++ b/src/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace BizHawk.Emulation.Common { @@ -58,7 +57,7 @@ namespace BizHawk.Emulation.Common if (_sink != null) { - var scope = DebuggableCore.MemoryCallbacks.AvailableScopes.First(); // This will be an issue when cores use this trace buffer and utilize multiple scopes + var scope = DebuggableCore.MemoryCallbacks.AvailableScopes[0]; // This will be an issue when cores use this trace buffer and utilize multiple scopes DebuggableCore.MemoryCallbacks.Add(new TracingMemoryCallback(TraceFromCallback, scope)); } } diff --git a/src/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs b/src/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs index 65e10564cd..2261f44272 100644 --- a/src/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs +++ b/src/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs @@ -30,7 +30,7 @@ namespace BizHawk.Emulation.Common public MemoryDomain MainMemory { - get => _mainMemory ?? this.First(); + get => _mainMemory ?? this[0]; set => _mainMemory = value; } diff --git a/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.IInputPollable.cs b/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.IInputPollable.cs index 4ab313a87a..e62bb473cf 100644 --- a/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.IInputPollable.cs +++ b/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.IInputPollable.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; + +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Arcades.MAME @@ -31,10 +32,8 @@ namespace BizHawk.Emulation.Cores.Arcades.MAME { if (portField != string.Empty) { - string[] substrings = portField.Split(','); - string tag = substrings.First(); - string field = substrings.Last(); - + var tag = portField.SubstringBefore(','); + var field = portField.SubstringAfterLast(','); _fieldsPorts.Add(field, tag); MAMEController.BoolButtons.Add(field); } diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs index fb519369e1..7384e398f0 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Display/AmstradGateArray.cs @@ -1007,18 +1007,8 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC var padPos = pad * -1; var excessL = padPos / 2; var excessR = excessL + (padPos % 2); - for (int i = 0; i < excessL; i++) - { - var lThing = lCop.First(); - - lCop.Remove(lThing); - } - for (int i = 0; i < excessL; i++) - { - var lThing = lCop.Last(); - - lCop.Remove(lThing); - } + for (var i = 0; i < excessL; i++) lCop.RemoveAt(0); + for (var i = 0; i < excessL; i++) lCop.RemoveAt(lCop.Count - 1); //TODO should be using excessR? } var lPad = pad / 2; diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs index db000dc794..d4b4bf3f6a 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Disk/FloppyDisk.cs @@ -662,7 +662,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC for (int i = 0; i < size - ActualDataByteLength; i++) { //l.Add(SectorData[i]); - l.Add(SectorData.Last()); + l.Add(SectorData[SectorData.Length - 1]); } return l.ToArray(); diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs index dd03153e30..911d53f129 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs @@ -367,7 +367,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _position += blockLen; // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); + CreatePauseBlock(_datacorder.DataBlocks[_datacorder.DataBlocks.Count - 1]); } /* length: [0F,10,11]+12 @@ -427,7 +427,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _position += blockLen; // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); + CreatePauseBlock(_datacorder.DataBlocks[_datacorder.DataBlocks.Count - 1]); } /* length: 04 @@ -551,7 +551,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _position += blockLen; // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); + CreatePauseBlock(_datacorder.DataBlocks[_datacorder.DataBlocks.Count - 1]); } /* length: [05,06,07]+08 @@ -657,7 +657,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _datacorder.DataBlocks.Add(t); // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); + CreatePauseBlock(_datacorder.DataBlocks[_datacorder.DataBlocks.Count - 1]); } /* length: [00,01,02,03]+04 @@ -728,7 +728,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _datacorder.DataBlocks.Add(t); // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); + CreatePauseBlock(_datacorder.DataBlocks[_datacorder.DataBlocks.Count - 1]); } /* length: [00,01,02,03]+04 @@ -880,7 +880,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC _position += 2; // generate PAUSE block - CreatePauseBlock(_datacorder.DataBlocks.Last()); + CreatePauseBlock(_datacorder.DataBlocks[_datacorder.DataBlocks.Count - 1]); } diff --git a/src/BizHawk.Emulation.Cores/Computers/Commodore64/C64.cs b/src/BizHawk.Emulation.Cores/Computers/Commodore64/C64.cs index 8300896c6d..43e1bec4e1 100644 --- a/src/BizHawk.Emulation.Cores/Computers/Commodore64/C64.cs +++ b/src/BizHawk.Emulation.Cores/Computers/Commodore64/C64.cs @@ -252,7 +252,7 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64 private void Init(VicType initRegion, BorderType borderType, SidType sidType, TapeDriveType tapeDriveType, DiskDriveType diskDriveType) { // Force certain drive types to be available depending on ROM type - var rom = _roms.First(); + var rom = _roms[0]; switch (C64FormatFinder.GetFormat(rom)) { diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs index 2823c9591c..a94f1e6e9c 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/FloppyDisk.cs @@ -665,7 +665,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum for (int i = 0; i < size - ActualDataByteLength; i++) { //l.Add(SectorData[i]); - l.Add(SectorData.Last()); + l.Add(SectorData[SectorData.Length - 1]); } return l.ToArray(); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs index 67c85a4d86..56c8739e5a 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs @@ -1,7 +1,7 @@ using System; -using System.Linq; using BizHawk.Common; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Atari.Atari2600 @@ -81,7 +81,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 { Name = _game.Name, System = VSystemID.Raw.A26, - MetaData = "m=" + _mapper.GetType().ToString().Split('.').Last(), + MetaData = "m=" + _mapper.GetType().ToString().SubstringAfterLast('.'), Hash = SHA1Checksum.ComputeDigestHex(Rom), Region = _game.Region, Status = RomStatus.Unknown diff --git a/src/BizHawk.Emulation.Cores/Consoles/Fairchild/ChannelF/ChannelF.cs b/src/BizHawk.Emulation.Cores/Consoles/Fairchild/ChannelF/ChannelF.cs index 26aa57162f..a838db5fa7 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Fairchild/ChannelF/ChannelF.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Fairchild/ChannelF/ChannelF.cs @@ -33,7 +33,7 @@ namespace BizHawk.Emulation.Cores.Consoles.ChannelF var bios02 = CoreComm.CoreFileProvider.GetFirmwareOrThrow(new("ChannelF", "ChannelF_sl131254")); //var bios02 = CoreComm.CoreFileProvider.GetFirmwareOrThrow(new("ChannelF", "ChannelF_sl90025")); - Cartridge = VesCartBase.Configure(_gameInfo.First(), _files.First()); + Cartridge = VesCartBase.Configure(_gameInfo[0], _files[0]); BIOS01 = bios01; BIOS02 = bios02; diff --git a/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs b/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs index 1428d7f9a4..811f6dcff5 100644 --- a/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs +++ b/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs @@ -67,6 +67,15 @@ namespace BizHawk.Tests.Common.StringExtensions Assert.AreEqual(qrs, string.Empty.SubstringAfter("abc", qrs)); } + [TestMethod] + public void TestSubstringAfterLast() + { + // fewer tests for SubstringAfterLast as its implementation should match SubstringAfter, save for using LastIndexOf + + Assert.AreEqual("ab", "abcdabcdab".SubstringAfterLast('d', qrs)); + Assert.AreEqual(qrs, "abcdabcdab".SubstringAfterLast('x', qrs)); + } + [TestMethod] public void TestSubstringBefore() {