From 3f2a07e5823cc8c15b347ae54f71c51a7d4d719a Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 22 Jul 2022 04:17:02 +1000 Subject: [PATCH] Fix remaining MA0098 code style warnings "Use indexer instead of LINQ methods" fixes 17df5673d, forgot I was in the middle of this when I pushed --- .../DisplayManager/OSDManager.cs | 2 +- src/BizHawk.Client.Common/RecentFiles.cs | 2 +- src/BizHawk.Client.Common/XmlGame.cs | 7 +++--- .../lua/LuaHelperLibs/EventsLuaLibrary.cs | 2 +- .../movie/tasproj/TasMovie.History.cs | 25 +++++++++++-------- .../MainForm.FileLoader.cs | 2 +- .../ZXSpectrum/ZXSpectrumJoystickSettings.cs | 19 +++++++------- src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs | 2 +- .../tools/Cheats/Cheats.cs | 3 ++- .../Debugger/GenericDebugger.Disassembler.cs | 2 +- .../tools/HexEditor/HexEditor.cs | 20 +++------------ .../tools/Lua/LuaConsole.cs | 3 ++- .../tools/Lua/Win32LuaLibraries.cs | 10 ++------ .../MultiDiskBundler/MultiDiskBundler.cs | 4 +-- .../tools/Watch/RamWatch.cs | 3 ++- .../AmstradCPC/SoundProviderMixer.cs | 2 +- 16 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs b/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs index 04f8645e44..d8916c78f0 100644 --- a/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs +++ b/src/BizHawk.Client.Common/DisplayManager/OSDManager.cs @@ -162,7 +162,7 @@ namespace BizHawk.Client.Common } else { - var message = _messages.Last(); + var message = _messages[_messages.Count - 1]; DrawMessage(g, message, 0); } } diff --git a/src/BizHawk.Client.Common/RecentFiles.cs b/src/BizHawk.Client.Common/RecentFiles.cs index b28074f3c1..84ab49a945 100644 --- a/src/BizHawk.Client.Common/RecentFiles.cs +++ b/src/BizHawk.Client.Common/RecentFiles.cs @@ -62,7 +62,7 @@ namespace BizHawk.Client.Common if (recentlist.Count > MAX_RECENT_FILES) { - recentlist.Remove(recentlist.Last()); + recentlist.RemoveAt(recentlist.Count - 1); } } } diff --git a/src/BizHawk.Client.Common/XmlGame.cs b/src/BizHawk.Client.Common/XmlGame.cs index 0e8d97f307..9a4034304d 100644 --- a/src/BizHawk.Client.Common/XmlGame.cs +++ b/src/BizHawk.Client.Common/XmlGame.cs @@ -6,6 +6,7 @@ using System.Xml; using BizHawk.Common; using BizHawk.Common.IOExtensions; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.Common @@ -71,8 +72,8 @@ namespace BizHawk.Client.Common else { // relative path - fullPath = Path.GetDirectoryName(f.CanonicalFullPath.Split('|').First()) ?? ""; - fullPath = Path.Combine(fullPath, filename.Split('|').First()); + fullPath = Path.GetDirectoryName(f.CanonicalFullPath.SubstringBefore('|')) ?? ""; + fullPath = Path.Combine(fullPath, filename.SubstringBefore('|')); try { using var hf = new HawkFile(fullPath); @@ -87,7 +88,7 @@ namespace BizHawk.Client.Common } else { - data = File.ReadAllBytes(fullPath.Split('|').First()); + data = File.ReadAllBytes(fullPath.SubstringBefore('|')); } } catch (Exception e) diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs index 522a5f234a..bebcba44a5 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs @@ -301,7 +301,7 @@ namespace BizHawk.Client.Common } else { - scope = DebuggableCore.MemoryCallbacks.AvailableScopes.First(); + scope = DebuggableCore.MemoryCallbacks.AvailableScopes[0]; } } diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.History.cs b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.History.cs index fecd4ef578..ffa3579bf1 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.History.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.History.cs @@ -46,6 +46,9 @@ namespace BizHawk.Client.Common private int _totalSteps; private bool _recordingBatch; + private List LatestBatch + => _history[_history.Count - 1]; + public List Names { get; } = new List(); public int UndoIndex { get; private set; } = -1; @@ -163,7 +166,7 @@ namespace BizHawk.Client.Common } _recordingBatch = false; - List last = _history.Last(); + var last = LatestBatch; if (last.Count == 0) // Remove batch if it's empty. { _history.RemoveAt(_history.Count - 1); @@ -328,8 +331,8 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieAction(first, last, _movie)); - _lastGeneral = _history.Last().Count - 1; + LatestBatch.Add(new MovieAction(first, last, _movie)); + _lastGeneral = LatestBatch.Count - 1; } } @@ -337,7 +340,7 @@ namespace BizHawk.Client.Common { if (IsRecording || force) { - (_history.Last()[_lastGeneral] as MovieAction).SetRedoLog(_movie); + ((MovieAction) LatestBatch[_lastGeneral]).SetRedoLog(_movie); } } @@ -346,7 +349,7 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieActionFrameEdit(frame, button, oldState, !oldState)); + LatestBatch.Add(new MovieActionFrameEdit(frame, button, oldState, !oldState)); } } @@ -355,7 +358,7 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieActionFrameEdit(frame, button, oldState, newState)); + LatestBatch.Add(new MovieActionFrameEdit(frame, button, oldState, newState)); } } @@ -368,7 +371,7 @@ namespace BizHawk.Client.Common : $"Remove Marker at frame {oldPosition}"; AddMovieAction(name); - _history.Last().Add(new MovieActionMarker(newMarker, oldPosition, oldMessage)); + LatestBatch.Add(new MovieActionMarker(newMarker, oldPosition, oldMessage)); } } @@ -377,7 +380,7 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieActionBindInput(_movie, frame, isDelete)); + LatestBatch.Add(new MovieActionBindInput(_movie, frame, isDelete)); } } @@ -386,7 +389,7 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieActionInsertFrames(frame, count)); + LatestBatch.Add(new MovieActionInsertFrames(frame, count)); } } @@ -395,7 +398,7 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieActionInsertFrames(frame, newInputs)); + LatestBatch.Add(new MovieActionInsertFrames(frame, newInputs)); } } @@ -404,7 +407,7 @@ namespace BizHawk.Client.Common if (IsRecording || force) { AddMovieAction(name); - _history.Last().Add(new MovieActionRemoveFrames(removeStart, removeUpTo, oldInputs, removedMarkers)); + LatestBatch.Add(new MovieActionRemoveFrames(removeStart, removeUpTo, oldInputs, removedMarkers)); } } } diff --git a/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs b/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs index 56445fb2a7..a67cba1c34 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs @@ -266,7 +266,7 @@ namespace BizHawk.Client.EmuHawk case 0: break; case 1: - var fileInformation = sortedFiles[value].First(); + var fileInformation = sortedFiles[value][0]; string filename = Path.Combine(new[] { fileInformation.DirectoryName, fileInformation.FileName }); switch (value) diff --git a/src/BizHawk.Client.EmuHawk/config/ZXSpectrum/ZXSpectrumJoystickSettings.cs b/src/BizHawk.Client.EmuHawk/config/ZXSpectrum/ZXSpectrumJoystickSettings.cs index 7c09673812..2acc1ab7ef 100644 --- a/src/BizHawk.Client.EmuHawk/config/ZXSpectrum/ZXSpectrumJoystickSettings.cs +++ b/src/BizHawk.Client.EmuHawk/config/ZXSpectrum/ZXSpectrumJoystickSettings.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Windows.Forms; using BizHawk.Client.Common; @@ -56,46 +55,46 @@ namespace BizHawk.Client.EmuHawk bool selectionValid = true; var j1 = Port1ComboBox.SelectedItem.ToString(); - if (j1 != _possibleControllers.First()) + if (j1 != _possibleControllers[0]) { if (j1 == Port2ComboBox.SelectedItem.ToString()) { - Port2ComboBox.SelectedItem = _possibleControllers.First(); + Port2ComboBox.SelectedItem = _possibleControllers[0]; selectionValid = false; } if (j1 == Port3ComboBox.SelectedItem.ToString()) { - Port3ComboBox.SelectedItem = _possibleControllers.First(); + Port3ComboBox.SelectedItem = _possibleControllers[0]; selectionValid = false; } } var j2 = Port2ComboBox.SelectedItem.ToString(); - if (j2 != _possibleControllers.First()) + if (j2 != _possibleControllers[0]) { if (j2 == Port1ComboBox.SelectedItem.ToString()) { - Port1ComboBox.SelectedItem = _possibleControllers.First(); + Port1ComboBox.SelectedItem = _possibleControllers[0]; selectionValid = false; } if (j2 == Port3ComboBox.SelectedItem.ToString()) { - Port3ComboBox.SelectedItem = _possibleControllers.First(); + Port3ComboBox.SelectedItem = _possibleControllers[0]; selectionValid = false; } } var j3 = Port3ComboBox.SelectedItem.ToString(); - if (j3 != _possibleControllers.First()) + if (j3 != _possibleControllers[0]) { if (j3 == Port1ComboBox.SelectedItem.ToString()) { - Port1ComboBox.SelectedItem = _possibleControllers.First(); + Port1ComboBox.SelectedItem = _possibleControllers[0]; selectionValid = false; } if (j3 == Port2ComboBox.SelectedItem.ToString()) { - Port2ComboBox.SelectedItem = _possibleControllers.First(); + Port2ComboBox.SelectedItem = _possibleControllers[0]; selectionValid = false; } } diff --git a/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs b/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs index 6c694be948..3be9e40b33 100644 --- a/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs +++ b/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs @@ -221,7 +221,7 @@ namespace BizHawk.Client.EmuHawk // Final tie breaker - Last used file var file = new FileInfo(_movieList[indices[0]].Filename); var time = file.LastAccessTime; - var mostRecent = indices.First(); + var mostRecent = indices[0]; for (var i = 1; i < indices.Count; i++) { file = new FileInfo(_movieList[indices[0]].Filename); diff --git a/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs b/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs index 20fa025e29..066ace7025 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs @@ -448,7 +448,8 @@ namespace BizHawk.Client.EmuHawk private void MoveDownMenuItem_Click(object sender, EventArgs e) { var indices = SelectedIndices.ToList(); - if (indices.Count == 0 || indices.Last() == MainForm.CheatList.Count - 1) + if (indices.Count == 0 + || indices[indices.Count - 1] == MainForm.CheatList.Count - 1) // at end already { return; } diff --git a/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.Disassembler.cs b/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.Disassembler.cs index 19a7f0e3be..80defa3ac5 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.Disassembler.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.Disassembler.cs @@ -133,7 +133,7 @@ namespace BizHawk.Client.EmuHawk private void IncrementCurrentAddress() { - _currentDisassemblerAddress += (uint)_disassemblyLines.First().Size; + _currentDisassemblerAddress += (uint) _disassemblyLines[0].Size; if (_currentDisassemblerAddress >= BusMaxValue) { _currentDisassemblerAddress = (uint)(BusMaxValue - 1); diff --git a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 6169b435dd..1e79cbc52a 100644 --- a/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -898,13 +898,7 @@ namespace BizHawk.Client.EmuHawk { return ""; } - - if (path.Contains("|")) - { - path = path.Split('|').First(); - } - - return Path.GetDirectoryName(path); + return Path.GetDirectoryName(path.SubstringBefore('|')); } } @@ -918,13 +912,7 @@ namespace BizHawk.Client.EmuHawk { return ""; } - - if (path.Contains("|")) - { - path = path.Split('|').Last(); - } - - return Path.GetFileName(path); + return Path.GetFileName(path.SubstringAfterLast('|')); } } @@ -1328,9 +1316,7 @@ namespace BizHawk.Client.EmuHawk private void LoadTableFileMenuItem_Click(object sender, EventArgs e) { string initialDirectory = Config.PathEntries.ToolsAbsolutePath(); - var romName = Config.RecentRoms.MostRecent.Contains('|') - ? Config.RecentRoms.MostRecent.Split('|').Last() - : Config.RecentRoms.MostRecent; + var romName = Config.RecentRoms.MostRecent.SubstringAfterLast('|'); using var ofd = new OpenFileDialog { diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 8af3a87536..92ced6a889 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -1063,7 +1063,8 @@ namespace BizHawk.Client.EmuHawk private void MoveDownMenuItem_Click(object sender, EventArgs e) { var indices = LuaListView.SelectedRows.ToList(); - if (indices.Count == 0 || indices.Last() == LuaImp.ScriptList.Count - 1) + if (indices.Count == 0 + || indices[indices.Count - 1] == LuaImp.ScriptList.Count - 1) // at end already { return; } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index 300557cf6d..ee232bf5a9 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; +using System.Reflection; using System.Threading; using NLua; @@ -73,14 +74,7 @@ namespace BizHawk.Client.EmuHawk foreach (var lib in Client.Common.ReflectionCache.Types.Concat(EmuHawk.ReflectionCache.Types) .Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t) && t.IsSealed && ServiceInjector.IsAvailable(serviceProvider, t))) { - bool addLibrary = true; - var attributes = lib.GetCustomAttributes(typeof(LuaLibraryAttribute), false); - if (attributes.Any()) - { - addLibrary = VersionInfo.DeveloperBuild || ((LuaLibraryAttribute)attributes.First()).Released; - } - - if (addLibrary) + if (VersionInfo.DeveloperBuild || lib.GetCustomAttribute(inherit: false)?.Released == true) { var instance = (LuaLibraryBase) Activator.CreateInstance(lib, this, _apiContainer, (Action) LogToLuaConsole); ServiceInjector.UpdateServices(serviceProvider, instance); diff --git a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs index 88cf23bd97..7d19d58bc5 100644 --- a/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs +++ b/src/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs @@ -11,6 +11,7 @@ using BizHawk.Emulation.Common; using BizHawk.Client.Common; using BizHawk.Common; using BizHawk.Common.PathExtensions; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Cores.Sega.MasterSystem; namespace BizHawk.Client.EmuHawk @@ -199,8 +200,7 @@ namespace BizHawk.Client.EmuHawk throw new Exception("System Id can not be blank"); } - var basePath = Path.GetDirectoryName(name.Split('|').First()); - + var basePath = Path.GetDirectoryName(name.SubstringBefore('|')); if (string.IsNullOrEmpty(basePath)) { var fileInfo = new FileInfo(name); diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs index f7ca0f4cb4..57addaaa41 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs @@ -877,7 +877,8 @@ namespace BizHawk.Client.EmuHawk private void MoveDownMenuItem_Click(object sender, EventArgs e) { var indices = SelectedIndices.ToList(); - if (indices.Count == 0 || indices.Last() == _watches.Count - 1) + if (indices.Count == 0 + || indices[indices.Count - 1] == _watches.Count - 1) // at end already { return; } diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs index 263a2f5c32..eda39bce9e 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs @@ -142,7 +142,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC } // are all the sample lengths the same? - var firstEntry = SoundProviders.First(); + var firstEntry = SoundProviders[0]; bool sameCount = SoundProviders.All(s => s.NSamp == firstEntry.NSamp); if (!sameCount)