From 68fd5a2aa0a7dba8eed3327c21bfad4abea55768 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 16 Nov 2024 01:07:47 -0500 Subject: [PATCH] Be smarter about some data types --- CHANGELIST.md | 1 + .../Aaru/ExecutionContext.cs | 8 ++-- MPF.ExecutionContexts/BaseExecutionContext.cs | 3 +- MPF.Frontend/ComboBoxItems/Element.cs | 6 +-- .../ComboBoxItems/RedumpSystemComboBoxItem.cs | 8 ++-- MPF.Frontend/Drive.cs | 10 +++-- MPF.Frontend/ViewModels/MainViewModel.cs | 45 +++++-------------- MPF.Processors/DiscImageCreator.cs | 2 +- MPF.Processors/RegexOutputFile.cs | 2 +- 9 files changed, 31 insertions(+), 54 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index a87f8851..8e7b064c 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -3,6 +3,7 @@ - Update Redumper to build 438 - Be smarter about Linq usage - Add .NET 9 to target frameworks +- Be smarter about some data types ### 3.2.3 (2024-11-06) diff --git a/MPF.ExecutionContexts/Aaru/ExecutionContext.cs b/MPF.ExecutionContexts/Aaru/ExecutionContext.cs index fc285108..6c6806b5 100644 --- a/MPF.ExecutionContexts/Aaru/ExecutionContext.cs +++ b/MPF.ExecutionContexts/Aaru/ExecutionContext.cs @@ -1292,7 +1292,7 @@ namespace MPF.ExecutionContexts.Aaru for (start = 0; start < parts.Count; start++) { // Keep a count of keys to determine if we should break out to command handling or not - int keyCount = Keys.Count(); + int keyCount = Keys.Count; // Debug ProcessBooleanParameter(parts, FlagStrings.DebugShort, FlagStrings.DebugLong, ref start, true); @@ -1311,7 +1311,7 @@ namespace MPF.ExecutionContexts.Aaru ProcessBooleanParameter(parts, FlagStrings.HelpShortAlt, FlagStrings.HelpLong, ref start, true); // If we didn't add any new flags, break out since we might be at command handling - if (keyCount == Keys.Count()) + if (keyCount == Keys.Count) break; } @@ -1339,7 +1339,7 @@ namespace MPF.ExecutionContexts.Aaru string? stringValue = null; // Keep a count of keys to determine if we should break out to filename handling or not - int keyCount = Keys.Count(); + int keyCount = Keys.Count; #region Boolean flags @@ -1706,7 +1706,7 @@ namespace MPF.ExecutionContexts.Aaru #endregion // If we didn't add any new flags, break out since we might be at filename handling - if (keyCount == Keys.Count()) + if (keyCount == Keys.Count) break; } diff --git a/MPF.ExecutionContexts/BaseExecutionContext.cs b/MPF.ExecutionContexts/BaseExecutionContext.cs index d6a1ee9d..450ab5b8 100644 --- a/MPF.ExecutionContexts/BaseExecutionContext.cs +++ b/MPF.ExecutionContexts/BaseExecutionContext.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; -using System.Text.RegularExpressions; using SabreTools.RedumpLib.Data; namespace MPF.ExecutionContexts @@ -20,7 +19,7 @@ namespace MPF.ExecutionContexts /// Set of flags to pass to the executable /// protected Dictionary flags = []; - protected internal IEnumerable Keys => flags.Keys; + protected internal List Keys => [.. flags.Keys]; /// /// Safe access to currently set flags diff --git a/MPF.Frontend/ComboBoxItems/Element.cs b/MPF.Frontend/ComboBoxItems/Element.cs index 7d7a80fc..4378b39e 100644 --- a/MPF.Frontend/ComboBoxItems/Element.cs +++ b/MPF.Frontend/ComboBoxItems/Element.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace MPF.Frontend.ComboBoxItems { @@ -42,9 +41,8 @@ namespace MPF.Frontend.ComboBoxItems /// public static IEnumerable> GenerateElements() { - return Enum.GetValues(typeof(T)) - .OfType() - .Select(e => new Element(e)); + var enumArr = (T[])Enum.GetValues(typeof(T)); + return Array.ConvertAll(enumArr, e => new Element(e)); } /// diff --git a/MPF.Frontend/ComboBoxItems/RedumpSystemComboBoxItem.cs b/MPF.Frontend/ComboBoxItems/RedumpSystemComboBoxItem.cs index fd05f0f2..b8469131 100644 --- a/MPF.Frontend/ComboBoxItems/RedumpSystemComboBoxItem.cs +++ b/MPF.Frontend/ComboBoxItems/RedumpSystemComboBoxItem.cs @@ -52,10 +52,10 @@ namespace MPF.Frontend.ComboBoxItems /// public static IEnumerable GenerateElements() { - var knownSystems = Enum.GetValues(typeof(RedumpSystem)) - .OfType() - .Where(s => !s.IsMarker() && s.GetCategory() != SystemCategory.NONE) - .ToList(); + var enumArr = (RedumpSystem[])Enum.GetValues(typeof(RedumpSystem)); + var nullableArr = Array.ConvertAll(enumArr, s => (RedumpSystem?)s); + var knownSystems = Array.FindAll(nullableArr, + s => !s.IsMarker() && s.GetCategory() != SystemCategory.NONE); Dictionary> mapping = knownSystems .GroupBy(s => s.GetCategory()) diff --git a/MPF.Frontend/Drive.cs b/MPF.Frontend/Drive.cs index 75a486a3..06afe365 100644 --- a/MPF.Frontend/Drive.cs +++ b/MPF.Frontend/Drive.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; #if NET462_OR_GREATER || NETCOREAPP using Microsoft.Management.Infrastructure; using Microsoft.Management.Infrastructure.Generic; @@ -137,8 +136,13 @@ namespace MPF.Frontend public static List CreateListOfDrives(bool ignoreFixedDrives) { var drives = GetDriveList(ignoreFixedDrives); - drives = [.. drives.OrderBy(i => i == null ? "\0" : i.Name)]; - return drives; + drives.Sort((d1, d2) => + { + string d1Name = d1?.Name == null ? "\0" : d1.Name; + string d2Name = d2?.Name == null ? "\0" : d2.Name; + return d1Name.CompareTo(d2Name); + }); + return [.. drives]; } /// diff --git a/MPF.Frontend/ViewModels/MainViewModel.cs b/MPF.Frontend/ViewModels/MainViewModel.cs index 7bd70eae..204fdfd0 100644 --- a/MPF.Frontend/ViewModels/MainViewModel.cs +++ b/MPF.Frontend/ViewModels/MainViewModel.cs @@ -2,12 +2,12 @@ using System.Collections.Generic; using System.ComponentModel; using System.IO; -using System.Linq; using System.Threading.Tasks; using BinaryObjectScanner; using MPF.Frontend.ComboBoxItems; using MPF.Frontend.Tools; using SabreTools.IO; +using SabreTools.IO.Extensions; using SabreTools.RedumpLib.Data; namespace MPF.Frontend.ViewModels @@ -708,10 +708,9 @@ namespace MPF.Frontend.ViewModels DisableEventHandlers(); // Create a static list of supported programs, not everything - InternalPrograms = Enum.GetValues(typeof(InternalProgram)) - .Cast() - .Where(ip => InternalProgramExists(ip)) - .Select(ip => new Element(ip)).ToList(); + var ipArr = (InternalProgram[])Enum.GetValues(typeof(InternalProgram)); + ipArr = Array.FindAll(ipArr, ip => InternalProgramExists(ip)); + InternalPrograms = [.. Array.ConvertAll(ipArr, ip => new Element(ip))]; // Get the current internal program InternalProgram internalProgram = Options.InternalProgram; @@ -1556,11 +1555,7 @@ namespace MPF.Frontend.ViewModels try { if (Directory.Exists(Path.Combine(drive.Name, "$SystemUpdate")) -#if NET20 || NET35 - && Directory.GetFiles(Path.Combine(drive.Name, "$SystemUpdate")).Any() -#else - && Directory.EnumerateFiles(Path.Combine(drive.Name, "$SystemUpdate")).Any() -#endif + && IOExtensions.SafeGetFiles(Path.Combine(drive.Name, "$SystemUpdate")).Length > 0 && drive.TotalSize <= 500_000_000) { return RedumpSystem.MicrosoftXbox360; @@ -1712,21 +1707,13 @@ namespace MPF.Frontend.ViewModels try { if (Directory.Exists(Path.Combine(drive.Name, "AUDIO_TS")) -#if NET20 || NET35 - && Directory.GetFiles(Path.Combine(drive.Name, "AUDIO_TS")).Any()) -#else - && Directory.EnumerateFiles(Path.Combine(drive.Name, "AUDIO_TS")).Any()) -#endif + && IOExtensions.SafeGetFiles(Path.Combine(drive.Name, "AUDIO_TS")).Length > 0) { return RedumpSystem.DVDAudio; } else if (Directory.Exists(Path.Combine(drive.Name, "VIDEO_TS")) -#if NET20 || NET35 - && Directory.GetFiles(Path.Combine(drive.Name, "VIDEO_TS")).Any()) -#else - && Directory.EnumerateFiles(Path.Combine(drive.Name, "VIDEO_TS")).Any()) -#endif + && IOExtensions.SafeGetFiles(Path.Combine(drive.Name, "VIDEO_TS")).Length > 0) { return RedumpSystem.DVDVideo; } @@ -1737,11 +1724,7 @@ namespace MPF.Frontend.ViewModels try { if (Directory.Exists(Path.Combine(drive.Name, "HVDVD_TS")) -#if NET20 || NET35 - && Directory.GetFiles(Path.Combine(drive.Name, "HVDVD_TS")).Any()) -#else - && Directory.EnumerateFiles(Path.Combine(drive.Name, "HVDVD_TS")).Any()) -#endif + && IOExtensions.SafeGetFiles(Path.Combine(drive.Name, "HVDVD_TS")).Length > 0) { return RedumpSystem.HDDVDVideo; } @@ -1752,11 +1735,7 @@ namespace MPF.Frontend.ViewModels try { if (Directory.Exists(Path.Combine(drive.Name, "PHOTO_CD")) -#if NET20 || NET35 - && Directory.GetFiles(Path.Combine(drive.Name, "PHOTO_CD")).Any()) -#else - && Directory.EnumerateFiles(Path.Combine(drive.Name, "PHOTO_CD")).Any()) -#endif + && IOExtensions.SafeGetFiles(Path.Combine(drive.Name, "PHOTO_CD")).Length > 0) { return RedumpSystem.PhotoCD; } @@ -1767,11 +1746,7 @@ namespace MPF.Frontend.ViewModels try { if (Directory.Exists(Path.Combine(drive.Name, "VCD")) -#if NET20 || NET35 - && Directory.GetFiles(Path.Combine(drive.Name, "drive.VCD")).Any()) -#else - && Directory.EnumerateFiles(Path.Combine(drive.Name, "VCD")).Any()) -#endif + && IOExtensions.SafeGetFiles(Path.Combine(drive.Name, "VCD")).Length > 0) { return RedumpSystem.VideoCD; } diff --git a/MPF.Processors/DiscImageCreator.cs b/MPF.Processors/DiscImageCreator.cs index 6396a630..43f758de 100644 --- a/MPF.Processors/DiscImageCreator.cs +++ b/MPF.Processors/DiscImageCreator.cs @@ -1316,7 +1316,7 @@ namespace MPF.Processors } // If we have all Session 1, we can just skip out - if (trackSessionMapping.All(kvp => kvp.Value == "1")) + if (trackSessionMapping.Values.All(v => v == "1")) return null; // Seek to the multisession data diff --git a/MPF.Processors/RegexOutputFile.cs b/MPF.Processors/RegexOutputFile.cs index 7943925d..9d08d7c7 100644 --- a/MPF.Processors/RegexOutputFile.cs +++ b/MPF.Processors/RegexOutputFile.cs @@ -78,7 +78,7 @@ namespace MPF.Processors var archiveFiles = archive.Entries.Select(e => e.Name).ToList(); foreach (string file in archiveFiles) { - if (Filenames.Any(pattern => Regex.IsMatch(file, pattern))) + if (Array.Exists(Filenames, pattern => Regex.IsMatch(file, pattern))) return true; }