From c31eeb001a99fbb724b940949e423f1f81dec8d6 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 23 May 2024 14:34:32 -0400 Subject: [PATCH] Decouple InfoTool from processors --- CHANGELIST.md | 1 + MPF.Core/Frontend/DumpEnvironment.cs | 4 +- MPF.Core/Processors/BaseProcessor.cs | 197 +++++++++++++++++++++++++- MPF.Core/Utilities/InfoTool.cs | 200 --------------------------- 4 files changed, 199 insertions(+), 203 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index 99480db2..a0c53e79 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -94,6 +94,7 @@ - Remove useless using statement - Move OptionsLoader to Core.Frontend - Move Logging to Core.Frontend +- Decouple InfoTool from processors ### 3.1.9a (2024-05-21) diff --git a/MPF.Core/Frontend/DumpEnvironment.cs b/MPF.Core/Frontend/DumpEnvironment.cs index e48e61d0..9ba443b1 100644 --- a/MPF.Core/Frontend/DumpEnvironment.cs +++ b/MPF.Core/Frontend/DumpEnvironment.cs @@ -503,7 +503,7 @@ namespace MPF.Core.Frontend if (_options.CompressLogFiles) { resultProgress?.Report(ResultEventArgs.Success("Compressing log files...")); - (bool compressSuccess, string compressResult) = InfoTool.CompressLogFiles(outputDirectory, filenameSuffix, outputFilename, _processor); + (bool compressSuccess, string compressResult) = _processor?.CompressLogFiles(outputDirectory, filenameSuffix, outputFilename) ?? (false, "No processor provided!"); if (compressSuccess) resultProgress?.Report(ResultEventArgs.Success(compressResult)); else @@ -514,7 +514,7 @@ namespace MPF.Core.Frontend if (_options.DeleteUnnecessaryFiles) { resultProgress?.Report(ResultEventArgs.Success("Deleting unnecessary files...")); - (bool deleteSuccess, string deleteResult) = InfoTool.DeleteUnnecessaryFiles(outputDirectory, outputFilename, _processor); + (bool deleteSuccess, string deleteResult) = _processor?.DeleteUnnecessaryFiles(outputDirectory, outputFilename) ?? (false, "No processor provided!"); if (deleteSuccess) resultProgress?.Report(ResultEventArgs.Success(deleteResult)); else diff --git a/MPF.Core/Processors/BaseProcessor.cs b/MPF.Core/Processors/BaseProcessor.cs index a2abc0b7..5494e872 100644 --- a/MPF.Core/Processors/BaseProcessor.cs +++ b/MPF.Core/Processors/BaseProcessor.cs @@ -1,5 +1,8 @@ +using System; using System.Collections.Generic; using System.IO; +using System.IO.Compression; +using System.Linq; using System.Text.RegularExpressions; using MPF.Core.Utilities; using SabreTools.RedumpLib.Data; @@ -85,7 +88,141 @@ namespace MPF.Core.Processors #endregion - #region Methods to Move + #region Shared Methods + + /// + /// Compress log files to save space + /// + /// Output folder to write to + /// Output filename to use as the base path + /// Output filename to use as the base path + /// Processor object representing how to process the outputs + /// True if the process succeeded, false otherwise + public (bool, string) CompressLogFiles(string? outputDirectory, string? filenameSuffix, string outputFilename) + { +#if NET20 || NET35 || NET40 + return (false, "Log compression is not available for this framework version"); +#else + + // Prepare the necessary paths + outputFilename = Path.GetFileNameWithoutExtension(outputFilename); + string combinedBase; + if (string.IsNullOrEmpty(outputDirectory)) + combinedBase = outputFilename; + else + combinedBase = Path.Combine(outputDirectory, outputFilename); + + string archiveName = combinedBase + "_logs.zip"; + + // Get the list of log files from the parameters object + var files = GetLogFilePaths(combinedBase); + + // Add on generated log files if they exist + var mpfFiles = GetGeneratedFilePaths(outputDirectory, filenameSuffix); + files.AddRange(mpfFiles); + + if (!files.Any()) + return (true, "No files to compress!"); + + // If the file already exists, we want to delete the old one + try + { + if (File.Exists(archiveName)) + File.Delete(archiveName); + } + catch + { + return (false, "Could not delete old archive!"); + } + + // Add the log files to the archive and delete the uncompressed file after + ZipArchive? zf = null; + try + { + zf = ZipFile.Open(archiveName, ZipArchiveMode.Create); + foreach (string file in files) + { + if (string.IsNullOrEmpty(outputDirectory)) + { + zf.CreateEntryFromFile(file, file, CompressionLevel.Optimal); + } + else + { + string entryName = file[outputDirectory!.Length..].TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + +#if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0 + zf.CreateEntryFromFile(file, entryName, CompressionLevel.Optimal); +#else + zf.CreateEntryFromFile(file, entryName, CompressionLevel.SmallestSize); +#endif + } + + // If the file is MPF-specific, don't delete + if (mpfFiles.Contains(file)) + continue; + + try + { + File.Delete(file); + } + catch { } + } + + return (true, "Compression complete!"); + } + catch (Exception ex) + { + return (false, $"Compression could not complete: {ex}"); + } + finally + { + zf?.Dispose(); + } +#endif + } + + /// + /// Compress log files to save space + /// + /// Output folder to write to + /// Output filename to use as the base path + /// Processor object representing how to process the outputs + /// True if the process succeeded, false otherwise + public (bool, string) DeleteUnnecessaryFiles(string? outputDirectory, string outputFilename) + { + // Prepare the necessary paths + outputFilename = Path.GetFileNameWithoutExtension(outputFilename); + string combinedBase; + if (string.IsNullOrEmpty(outputDirectory)) + combinedBase = outputFilename; + else + combinedBase = Path.Combine(outputDirectory, outputFilename); + + // Get the list of deleteable files from the parameters object + var files = GetDeleteableFilePaths(combinedBase); + + if (!files.Any()) + return (true, "No files to delete!"); + + // Attempt to delete all of the files + try + { + foreach (string file in files) + { + try + { + File.Delete(file); + } + catch { } + } + + return (true, "Deletion complete!"); + } + catch (Exception ex) + { + return (false, $"Deletion could not complete: {ex}"); + } + } /// /// Ensures that all required output files have been created @@ -198,6 +335,64 @@ namespace MPF.Core.Processors } } + /// + /// Generate a list of all MPF-specific log files generated + /// + /// Output folder to write to + /// Optional suffix to append to the filename + /// List of all log file paths, empty otherwise + private static List GetGeneratedFilePaths(string? outputDirectory, string? filenameSuffix) + { + var files = new List(); + + if (string.IsNullOrEmpty(outputDirectory) && string.IsNullOrEmpty(filenameSuffix)) + { + if (File.Exists("!submissionInfo.txt")) + files.Add("!submissionInfo.txt"); + if (File.Exists("!submissionInfo.json")) + files.Add("!submissionInfo.json"); + if (File.Exists("!submissionInfo.json.gz")) + files.Add("!submissionInfo.json.gz"); + if (File.Exists("!protectionInfo.txt")) + files.Add("!protectionInfo.txt"); + } + else if (string.IsNullOrEmpty(outputDirectory) && !string.IsNullOrEmpty(filenameSuffix)) + { + if (File.Exists($"!submissionInfo_{filenameSuffix}.txt")) + files.Add($"!submissionInfo_{filenameSuffix}.txt"); + if (File.Exists($"!submissionInfo_{filenameSuffix}.json")) + files.Add($"!submissionInfo_{filenameSuffix}.json"); + if (File.Exists($"!submissionInfo_{filenameSuffix}.json.gz")) + files.Add($"!submissionInfo_{filenameSuffix}.json.gz"); + if (File.Exists($"!protectionInfo_{filenameSuffix}.txt")) + files.Add($"!protectionInfo_{filenameSuffix}.txt"); + } + else if (!string.IsNullOrEmpty(outputDirectory) && string.IsNullOrEmpty(filenameSuffix)) + { + if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.txt"))) + files.Add(Path.Combine(outputDirectory, "!submissionInfo.txt")); + if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.json"))) + files.Add(Path.Combine(outputDirectory, "!submissionInfo.json")); + if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.json.gz"))) + files.Add(Path.Combine(outputDirectory, "!submissionInfo.json.gz")); + if (File.Exists(Path.Combine(outputDirectory, "!protectionInfo.txt"))) + files.Add(Path.Combine(outputDirectory, "!protectionInfo.txt")); + } + else if (!string.IsNullOrEmpty(outputDirectory) && !string.IsNullOrEmpty(filenameSuffix)) + { + if (File.Exists(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.txt"))) + files.Add(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.txt")); + if (File.Exists(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json"))) + files.Add(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json")); + if (File.Exists(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json.gz"))) + files.Add(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json.gz")); + if (File.Exists(Path.Combine(outputDirectory, $"!protectionInfo_{filenameSuffix}.txt"))) + files.Add(Path.Combine(outputDirectory, $"!protectionInfo_{filenameSuffix}.txt")); + } + + return files; + } + #endregion } } diff --git a/MPF.Core/Utilities/InfoTool.cs b/MPF.Core/Utilities/InfoTool.cs index c5886197..3c9a668f 100644 --- a/MPF.Core/Utilities/InfoTool.cs +++ b/MPF.Core/Utilities/InfoTool.cs @@ -10,7 +10,6 @@ using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using BinaryObjectScanner; -using MPF.Core.Processors; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SabreTools.IO; @@ -1135,147 +1134,6 @@ namespace MPF.Core.Utilities #region Information Output - /// - /// Compress log files to save space - /// - /// Output folder to write to - /// Output filename to use as the base path - /// Output filename to use as the base path - /// Processor object representing how to process the outputs - /// True if the process succeeded, false otherwise - public static (bool, string) CompressLogFiles(string? outputDirectory, string? filenameSuffix, string outputFilename, BaseProcessor? processor) - { -#if NET20 || NET35 || NET40 - return (false, "Log compression is not available for this framework version"); -#else - // If there are no parameters - if (processor == null) - return (false, "No parameters provided!"); - - // Prepare the necessary paths - outputFilename = Path.GetFileNameWithoutExtension(outputFilename); - string combinedBase; - if (string.IsNullOrEmpty(outputDirectory)) - combinedBase = outputFilename; - else - combinedBase = Path.Combine(outputDirectory, outputFilename); - - string archiveName = combinedBase + "_logs.zip"; - - // Get the list of log files from the parameters object - var files = processor.GetLogFilePaths(combinedBase); - - // Add on generated log files if they exist - var mpfFiles = GetGeneratedFilePaths(outputDirectory, filenameSuffix); - files.AddRange(mpfFiles); - - if (!files.Any()) - return (true, "No files to compress!"); - - // If the file already exists, we want to delete the old one - try - { - if (File.Exists(archiveName)) - File.Delete(archiveName); - } - catch - { - return (false, "Could not delete old archive!"); - } - - // Add the log files to the archive and delete the uncompressed file after - ZipArchive? zf = null; - try - { - zf = ZipFile.Open(archiveName, ZipArchiveMode.Create); - foreach (string file in files) - { - if (string.IsNullOrEmpty(outputDirectory)) - { - zf.CreateEntryFromFile(file, file, CompressionLevel.Optimal); - } - else - { - string entryName = file[outputDirectory!.Length..].TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - -#if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0 - zf.CreateEntryFromFile(file, entryName, CompressionLevel.Optimal); -#else - zf.CreateEntryFromFile(file, entryName, CompressionLevel.SmallestSize); -#endif - } - - // If the file is MPF-specific, don't delete - if (mpfFiles.Contains(file)) - continue; - - try - { - File.Delete(file); - } - catch { } - } - - return (true, "Compression complete!"); - } - catch (Exception ex) - { - return (false, $"Compression could not complete: {ex}"); - } - finally - { - zf?.Dispose(); - } -#endif - } - - /// - /// Compress log files to save space - /// - /// Output folder to write to - /// Output filename to use as the base path - /// Processor object representing how to process the outputs - /// True if the process succeeded, false otherwise - public static (bool, string) DeleteUnnecessaryFiles(string? outputDirectory, string outputFilename, BaseProcessor? processor) - { - // If there are no parameters - if (processor == null) - return (false, "No parameters provided!"); - - // Prepare the necessary paths - outputFilename = Path.GetFileNameWithoutExtension(outputFilename); - string combinedBase; - if (string.IsNullOrEmpty(outputDirectory)) - combinedBase = outputFilename; - else - combinedBase = Path.Combine(outputDirectory, outputFilename); - - // Get the list of deleteable files from the parameters object - var files = processor.GetDeleteableFilePaths(combinedBase); - - if (!files.Any()) - return (true, "No files to delete!"); - - // Attempt to delete all of the files - try - { - foreach (string file in files) - { - try - { - File.Delete(file); - } - catch { } - } - - return (true, "Deletion complete!"); - } - catch (Exception ex) - { - return (false, $"Deletion could not complete: {ex}"); - } - } - /// /// Write the data to the output folder /// @@ -1436,64 +1294,6 @@ namespace MPF.Core.Utilities return true; } - /// - /// Generate a list of all MPF-specific log files generated - /// - /// Output folder to write to - /// Optional suffix to append to the filename - /// List of all log file paths, empty otherwise - private static List GetGeneratedFilePaths(string? outputDirectory, string? filenameSuffix) - { - var files = new List(); - - if (string.IsNullOrEmpty(outputDirectory) && string.IsNullOrEmpty(filenameSuffix)) - { - if (File.Exists("!submissionInfo.txt")) - files.Add("!submissionInfo.txt"); - if (File.Exists("!submissionInfo.json")) - files.Add("!submissionInfo.json"); - if (File.Exists("!submissionInfo.json.gz")) - files.Add("!submissionInfo.json.gz"); - if (File.Exists("!protectionInfo.txt")) - files.Add("!protectionInfo.txt"); - } - else if (string.IsNullOrEmpty(outputDirectory) && !string.IsNullOrEmpty(filenameSuffix)) - { - if (File.Exists($"!submissionInfo_{filenameSuffix}.txt")) - files.Add($"!submissionInfo_{filenameSuffix}.txt"); - if (File.Exists($"!submissionInfo_{filenameSuffix}.json")) - files.Add($"!submissionInfo_{filenameSuffix}.json"); - if (File.Exists($"!submissionInfo_{filenameSuffix}.json.gz")) - files.Add($"!submissionInfo_{filenameSuffix}.json.gz"); - if (File.Exists($"!protectionInfo_{filenameSuffix}.txt")) - files.Add($"!protectionInfo_{filenameSuffix}.txt"); - } - else if (!string.IsNullOrEmpty(outputDirectory) && string.IsNullOrEmpty(filenameSuffix)) - { - if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.txt"))) - files.Add(Path.Combine(outputDirectory, "!submissionInfo.txt")); - if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.json"))) - files.Add(Path.Combine(outputDirectory, "!submissionInfo.json")); - if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.json.gz"))) - files.Add(Path.Combine(outputDirectory, "!submissionInfo.json.gz")); - if (File.Exists(Path.Combine(outputDirectory, "!protectionInfo.txt"))) - files.Add(Path.Combine(outputDirectory, "!protectionInfo.txt")); - } - else if (!string.IsNullOrEmpty(outputDirectory) && !string.IsNullOrEmpty(filenameSuffix)) - { - if (File.Exists(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.txt"))) - files.Add(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.txt")); - if (File.Exists(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json"))) - files.Add(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json")); - if (File.Exists(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json.gz"))) - files.Add(Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json.gz")); - if (File.Exists(Path.Combine(outputDirectory, $"!protectionInfo_{filenameSuffix}.txt"))) - files.Add(Path.Combine(outputDirectory, $"!protectionInfo_{filenameSuffix}.txt")); - } - - return files; - } - /// /// Create an IRD and write it to the specified output directory with optional filename suffix ///