Move and rename new protection scan method

This commit is contained in:
Matt Nadareski
2025-11-10 13:52:31 -05:00
parent 2037ded792
commit 2f0471d596
3 changed files with 91 additions and 82 deletions

View File

@@ -67,6 +67,7 @@
- Update packages
- Scan disc image if not multi-track
- Scan multi-track images for protection
- Move and rename new protection scan method
### 3.5.0 (2025-10-10)

View File

@@ -65,6 +65,69 @@ namespace MPF.Frontend.Tools
#endregion
];
/// <summary>
/// Run comprehensive protection scans based on both the
/// physical media as well as the image
/// </summary>
/// <param name="basePath">Base output image path</param>
/// <param name="drive">Drive object representing the current drive</param>
/// <param name="options">Options object that determines what to scan</param>
/// <param name="progress">Optional progress callback</param>
public async static Task<Dictionary<string, List<string>>> RunCombinedProtectionScans(string basePath,
Drive? drive,
Options options,
IProgress<ProtectionProgress>? protectionProgress = null)
{
// Setup the output protections dictionary
Dictionary<string, List<string>> protections = [];
// Scan the mounted drive path
if (drive?.Name != null)
protections = await RunProtectionScanOnPath(drive.Name, options, protectionProgress);
// Scan the disc image, if possible
if (File.Exists($"{basePath}.iso"))
{
var imageProtections = await RunProtectionScanOnImage($"{basePath}.iso", options, protectionProgress);
MergeDictionaries(protections, imageProtections);
}
else if (File.Exists($"{basePath}.bin"))
{
var imageProtections = await RunProtectionScanOnImage($"{basePath}.bin", options, protectionProgress);
MergeDictionaries(protections, imageProtections);
}
else if (File.Exists($"{basePath}.cue"))
{
string[] cueLines = File.ReadAllLines($"{basePath}.cue");
foreach (string cueLine in cueLines)
{
// Skip all non-FILE lines
if (!cueLine.StartsWith("FILE"))
continue;
// Extract the information
var match = Regex.Match(cueLine, @"FILE ""(.*?)"" BINARY");
if (!match.Success || match.Groups.Count == 0)
continue;
// Get the track name from the matches
string trackName = match.Groups[1].Value;
trackName = Path.GetFileNameWithoutExtension(trackName);
string baseDir = Path.GetDirectoryName(basePath) ?? string.Empty;
string trackPath = Path.Combine(baseDir, trackName);
// Scan the track for protections, if it exists
if (File.Exists($"{trackPath}.bin"))
{
var trackProtections = await RunProtectionScanOnImage($"{trackPath}.bin", options, protectionProgress);
MergeDictionaries(protections, trackProtections);
}
}
}
return protections;
}
/// <summary>
/// Run protection scan on a given path
/// </summary>
@@ -119,7 +182,7 @@ namespace MPF.Frontend.Tools
#endif
{
var scanner = new Scanner(
false, // Disable extracting disc images for now
scanArchives: false, // Disable extracting disc images for now
scanContents: false, // Disabled for image scanning
scanPaths: false, // Disabled for image scanning
scanSubdirectories: false, // Disabled for image scanning
@@ -593,5 +656,30 @@ namespace MPF.Frontend.Tools
foundProtections.Sort();
return string.Join(", ", [.. foundProtections]);
}
/// <summary>
/// Merge two dictionaries together based on keys
/// </summary>
/// <param name="original">Source dictionary to add to</param>
/// <param name="add">Second dictionary to add from</param>
private static void MergeDictionaries(Dictionary<string, List<string>> original, Dictionary<string, List<string>> add)
{
// Ignore if there are no values to append
if (add.Count == 0)
return;
// Loop through and add from the new dictionary
foreach (var kvp in add)
{
// Ignore empty values
if (kvp.Value.Count == 0)
continue;
if (!original.ContainsKey(kvp.Key))
original[kvp.Key] = [];
original[kvp.Key].AddRange(kvp.Value);
}
}
}
}

View File

@@ -132,7 +132,7 @@ namespace MPF.Frontend.Tools
{
Dictionary<string, List<string>>? protections = null;
if (options.ScanForProtection)
protections = await GetProtections(basePath, drive, options, protectionProgress);
protections = await ProtectionTool.RunCombinedProtectionScans(basePath, drive, options, protectionProgress);
var protectionString = ProtectionTool.FormatProtections(protections);
@@ -1072,86 +1072,6 @@ namespace MPF.Frontend.Tools
info.VersionAndEditions.Version = valueFunc(drive) ?? string.Empty;
}
/// <summary>
/// Get the protections for a disc based on the mounted path and disc image
/// </summary>
private async static Task<Dictionary<string, List<string>>> GetProtections(string basePath,
Drive? drive,
Options options,
IProgress<ProtectionProgress>? protectionProgress = null)
{
// Setup the output protections dictionary
Dictionary<string, List<string>> protections = [];
// Scan the mounted drive path
if (drive?.Name != null)
protections = await ProtectionTool.RunProtectionScanOnPath(drive.Name, options, protectionProgress);
// Scan the disc image, if possible
Dictionary<string, List<string>> imageProtections = [];
if (File.Exists($"{basePath}.iso"))
{
imageProtections = await ProtectionTool.RunProtectionScanOnImage($"{basePath}.iso", options, protectionProgress);
}
else if (File.Exists($"{basePath}.bin"))
{
imageProtections = await ProtectionTool.RunProtectionScanOnImage($"{basePath}.bin", options, protectionProgress);
}
else if (File.Exists($"{basePath}.cue"))
{
string[] cueLines = File.ReadAllLines($"{basePath}.cue");
foreach (string cueLine in cueLines)
{
// Skip all non-FILE lines
if (!cueLine.StartsWith("FILE"))
continue;
// Extract the information
var match = Regex.Match(cueLine, @"FILE ""(.*?)"" BINARY");
if (!match.Success || match.Groups.Count == 0)
continue;
// Get the track name from the matches
string trackName = match.Groups[1].Value;
trackName = Path.GetFileNameWithoutExtension(trackName);
string baseDir = Path.GetDirectoryName(basePath) ?? string.Empty;
string trackPath = Path.Combine(baseDir, trackName);
// Scan the track for protections, if it exists
if (File.Exists($"{trackPath}.bin"))
{
var trackProtections = await ProtectionTool.RunProtectionScanOnImage($"{trackPath}.bin", options, protectionProgress);
// Add track protections, if any exist
if (trackProtections.Count > 0)
{
foreach (var kvp in trackProtections)
{
if (!imageProtections.ContainsKey(kvp.Key))
imageProtections[kvp.Key] = [];
imageProtections[kvp.Key].AddRange(kvp.Value);
}
}
}
}
}
// Add image protections, if any exist
if (imageProtections.Count > 0)
{
foreach (var kvp in imageProtections)
{
if (!protections.ContainsKey(kvp.Key))
protections[kvp.Key] = [];
protections[kvp.Key].AddRange(kvp.Value);
}
}
return protections;
}
/// <summary>
/// Reformat a protection dictionary for submission info
/// </summary>