From a0825f276b61feb87b5950eb9efebccdb40ac322 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 3 Nov 2024 22:15:42 -0500 Subject: [PATCH] Use new ProtectionDictionary type --- CHANGELIST.md | 1 + MPF.Frontend/Tools/ProtectionTool.cs | 40 ++++------------------- MPF.Frontend/Tools/SubmissionGenerator.cs | 25 ++++++++++++-- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index 5a624675..033f148e 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -30,6 +30,7 @@ - Remove unused CompilerServices.Unsafe library - Simplify GetMediaType return - Reduce call complexity for login result +- Use new ProtectionDictionary type ### 3.2.2 (2024-09-24) diff --git a/MPF.Frontend/Tools/ProtectionTool.cs b/MPF.Frontend/Tools/ProtectionTool.cs index 48f1db85..57ec4d23 100644 --- a/MPF.Frontend/Tools/ProtectionTool.cs +++ b/MPF.Frontend/Tools/ProtectionTool.cs @@ -6,8 +6,6 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using BinaryObjectScanner; -#pragma warning disable SYSLIB1045 // Convert to 'GeneratedRegexAttribute'. - namespace MPF.Frontend.Tools { public static class ProtectionTool @@ -19,7 +17,7 @@ namespace MPF.Frontend.Tools /// Options object that determines what to scan /// Optional progress callback /// Detected copy protection(s) if possible, null on error - public static async Task<(string?, Dictionary>?)> GetCopyProtection(Drive? drive, + public static async Task<(string?, ProtectionDictionary?)> GetCopyProtection(Drive? drive, Frontend.Options options, IProgress? progress = null) { @@ -39,7 +37,7 @@ namespace MPF.Frontend.Tools /// Options object that determines what to scan /// Optional progress callback /// Set of all detected copy protections with an optional error string - public static async Task<(Dictionary>?, string?)> RunProtectionScanOnPath(string path, + public static async Task<(ProtectionDictionary?, string?)> RunProtectionScanOnPath(string path, Frontend.Options options, IProgress? progress = null) { @@ -47,20 +45,9 @@ namespace MPF.Frontend.Tools { #if NET40 var found = await Task.Factory.StartNew(() => - { - var scanner = new Scanner( - options.ScanArchivesForProtection, - scanContents: true, // Hardcoded value to avoid issues - scanGameEngines: false, // Hardcoded value to avoid issues - options.ScanPackersForProtection, - scanPaths: true, // Hardcoded value to avoid issues - options.IncludeDebugProtectionInformation, - progress); - - return scanner.GetProtections(path); - }); #else var found = await Task.Run(() => +#endif { var scanner = new Scanner( options.ScanArchivesForProtection, @@ -73,29 +60,16 @@ namespace MPF.Frontend.Tools return scanner.GetProtections(path); }); -#endif // If nothing was returned, return -#if NET20 || NET35 if (found == null || found.Count == 0) -#else - if (found == null || found.IsEmpty) -#endif return (null, null); // Filter out any empty protections - var filteredProtections = found -#if NET20 || NET35 - .Where(kvp => kvp.Value != null && kvp.Value.Count > 0) -#else - .Where(kvp => kvp.Value != null && !kvp.Value.IsEmpty) -#endif - .ToDictionary( - kvp => kvp.Key, - kvp => kvp.Value.OrderBy(s => s).ToList()); + found.ClearEmptyKeys(); // Return the filtered set of protections - return (filteredProtections, null); + return (found, null); } catch (Exception ex) { @@ -108,7 +82,7 @@ namespace MPF.Frontend.Tools /// /// Dictionary of file to list of protection mappings /// Detected protections, if any - public static string? FormatProtections(Dictionary>? protections) + public static string? FormatProtections(ProtectionDictionary? protections) { // If the filtered list is empty in some way, return if (protections == null || !protections.Any()) @@ -390,7 +364,7 @@ namespace MPF.Frontend.Tools .Where(p => p != "SafeDisc 2+") .Where(p => p != "SafeDisc 3+ (DVD)"); } - + // Only SafeDisc Lite is found. else if (foundProtections.Any(p => p == "SafeDisc Lite")) { diff --git a/MPF.Frontend/Tools/SubmissionGenerator.cs b/MPF.Frontend/Tools/SubmissionGenerator.cs index 5b450cbe..7b1e4854 100644 --- a/MPF.Frontend/Tools/SubmissionGenerator.cs +++ b/MPF.Frontend/Tools/SubmissionGenerator.cs @@ -118,10 +118,10 @@ namespace MPF.Frontend.Tools if (system.SupportsCopyProtectionScans()) { resultProgress?.Report(ResultEventArgs.Success("Running copy protection scan... this might take a while!")); - var (protectionString, fullProtections) = await ProtectionTool.GetCopyProtection(drive, options, protectionProgress); + var (protectionString, protections) = await ProtectionTool.GetCopyProtection(drive, options, protectionProgress); info.CopyProtection!.Protection += protectionString; - info.CopyProtection.FullProtections = fullProtections as Dictionary?> ?? []; + info.CopyProtection.FullProtections = ReformatProtectionDictionary(protections); resultProgress?.Report(ResultEventArgs.Success("Copy protection scan complete!")); } @@ -899,6 +899,27 @@ namespace MPF.Frontend.Tools info.VersionAndEditions.Version = valueFunc(drive) ?? string.Empty; } + /// + /// Reformat a protection dictionary for submission info + /// + /// ProtectionDictionary to format + /// Reformatted dictionary on success, empty on error + private static Dictionary?> ReformatProtectionDictionary(ProtectionDictionary? oldDict) + { + // Null or empty protections return empty + if (oldDict == null || oldDict.Count == 0) + return []; + + // Reformat each set into a List + var newDict = new Dictionary?>(); + foreach (string key in oldDict.Keys) + { + newDict[key] = [.. oldDict[key]]; + } + + return newDict; + } + #endregion } }