From 4ce9b214b04537d7f3ecb73920a9f83b680fa47f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 18 Oct 2023 22:19:25 -0400 Subject: [PATCH] Wire through filename suffix --- CHANGELIST.md | 1 + MPF.Core/DumpEnvironment.cs | 11 ++-- MPF.Core/InfoTool.cs | 111 +++++++++++++++++++++++++----------- 3 files changed, 85 insertions(+), 38 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index a2333bc9..112b831f 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -17,6 +17,7 @@ - Rearrange OptionsWindow to be easier to navigate - Fix up DumpEnvironment a bit - Add filename suffix setting (nw) +- Wire through filename suffix ### 2.7.2 (2023-10-17) diff --git a/MPF.Core/DumpEnvironment.cs b/MPF.Core/DumpEnvironment.cs index ab78e17e..c81ece1f 100644 --- a/MPF.Core/DumpEnvironment.cs +++ b/MPF.Core/DumpEnvironment.cs @@ -448,9 +448,12 @@ namespace MPF.Core else resultProgress?.Report(Result.Failure(formatResult)); + // Get the filename suffix for auto-generated files + var filenameSuffix = Options.AddFilenameSuffix ? Path.GetFileNameWithoutExtension(outputFilename) : null; + // Write the text output resultProgress?.Report(Result.Success("Writing information to !submissionInfo.txt...")); - (bool txtSuccess, string txtResult) = InfoTool.WriteOutputData(outputDirectory, formattedValues); + (bool txtSuccess, string txtResult) = InfoTool.WriteOutputData(outputDirectory, filenameSuffix, formattedValues); if (txtSuccess) resultProgress?.Report(Result.Success(txtResult)); else @@ -460,7 +463,7 @@ namespace MPF.Core if (Options.ScanForProtection && Options.OutputSeparateProtectionFile) { resultProgress?.Report(Result.Success("Writing protection to !protectionInfo.txt...")); - bool scanSuccess = InfoTool.WriteProtectionData(outputDirectory, submissionInfo); + bool scanSuccess = InfoTool.WriteProtectionData(outputDirectory, filenameSuffix, submissionInfo); if (scanSuccess) resultProgress?.Report(Result.Success("Writing complete!")); else @@ -471,7 +474,7 @@ namespace MPF.Core if (Options.OutputSubmissionJSON) { resultProgress?.Report(Result.Success($"Writing information to !submissionInfo.json{(Options.IncludeArtifacts ? ".gz" : string.Empty)}...")); - bool jsonSuccess = InfoTool.WriteOutputData(outputDirectory, submissionInfo, Options.IncludeArtifacts); + bool jsonSuccess = InfoTool.WriteOutputData(outputDirectory, filenameSuffix, submissionInfo, Options.IncludeArtifacts); if (jsonSuccess) resultProgress?.Report(Result.Success("Writing complete!")); else @@ -482,7 +485,7 @@ namespace MPF.Core if (Options.CompressLogFiles) { resultProgress?.Report(Result.Success("Compressing log files...")); - (bool compressSuccess, string compressResult) = InfoTool.CompressLogFiles(outputDirectory, outputFilename, Parameters); + (bool compressSuccess, string compressResult) = InfoTool.CompressLogFiles(outputDirectory, filenameSuffix, outputFilename, Parameters); if (compressSuccess) resultProgress?.Report(Result.Success(compressResult)); else diff --git a/MPF.Core/InfoTool.cs b/MPF.Core/InfoTool.cs index 05ca66f3..0a76712c 100644 --- a/MPF.Core/InfoTool.cs +++ b/MPF.Core/InfoTool.cs @@ -1224,13 +1224,14 @@ namespace MPF.Core /// 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 /// Parameters object to use to derive log file paths /// True if the process succeeded, false otherwise #if NET48 - public static (bool, string) CompressLogFiles(string outputDirectory, string outputFilename, BaseParameters parameters) + public static (bool, string) CompressLogFiles(string outputDirectory, string filenameSuffix, string outputFilename, BaseParameters parameters) #else - public static (bool, string) CompressLogFiles(string? outputDirectory, string outputFilename, BaseParameters? parameters) + public static (bool, string) CompressLogFiles(string? outputDirectory, string? filenameSuffix, string outputFilename, BaseParameters? parameters) #endif { // If there are no parameters @@ -1251,7 +1252,7 @@ namespace MPF.Core var files = parameters.GetLogFilePaths(combinedBase); // Add on generated log files if they exist - var mpfFiles = GetGeneratedFilePaths(outputDirectory); + var mpfFiles = GetGeneratedFilePaths(outputDirectory, filenameSuffix); files.AddRange(mpfFiles); if (!files.Any()) @@ -1750,12 +1751,13 @@ namespace MPF.Core /// Write the data to the output folder /// /// Output folder to write to + /// Optional suffix to append to the filename /// Preformatted list of lines to write out to the file /// True on success, false on error #if NET48 - public static (bool, string) WriteOutputData(string outputDirectory, List lines) + public static (bool, string) WriteOutputData(string outputDirectory, string filenameSuffix, List lines) #else - public static (bool, string) WriteOutputData(string? outputDirectory, List? lines) + public static (bool, string) WriteOutputData(string? outputDirectory, string? filenameSuffix, List? lines) #endif { // Check to see if the inputs are valid @@ -1766,11 +1768,15 @@ namespace MPF.Core try { // Get the file path - string path; - if (string.IsNullOrWhiteSpace(outputDirectory)) + var path = string.Empty; + if (string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) path = "!submissionInfo.txt"; - else + else if (string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = $"!submissionInfo_{filenameSuffix}.txt"; + else if (!string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) path = Path.Combine(outputDirectory, "!submissionInfo.txt"); + else if (!string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.txt"); using (var sw = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write))) { @@ -1792,13 +1798,14 @@ namespace MPF.Core /// Write the data to the output folder /// /// Output folder to write to + /// Optional suffix to append to the filename /// SubmissionInfo object representing the JSON to write out to the file /// True if artifacts were included, false otherwise /// True on success, false on error #if NET48 - public static bool WriteOutputData(string outputDirectory, SubmissionInfo info, bool includedArtifacts) + public static bool WriteOutputData(string outputDirectory, string filenameSuffix, SubmissionInfo info, bool includedArtifacts) #else - public static bool WriteOutputData(string? outputDirectory, SubmissionInfo? info, bool includedArtifacts) + public static bool WriteOutputData(string? outputDirectory, string? filenameSuffix, SubmissionInfo? info, bool includedArtifacts) #endif { // Check to see if the input is valid @@ -1814,13 +1821,17 @@ namespace MPF.Core // If we included artifacts, write to a GZip-compressed file if (includedArtifacts) { - string file; - if (string.IsNullOrWhiteSpace(outputDirectory)) - file = "!submissionInfo.json.gz"; - else - file = Path.Combine(outputDirectory, "!submissionInfo.json.gz"); + var path = string.Empty; + if (string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) + path = "!submissionInfo.json.gz"; + else if (string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = $"!submissionInfo_{filenameSuffix}.json.gz"; + else if (!string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, "!submissionInfo.json.gz"); + else if (!string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json.gz"); - using (var fs = File.Create(file)) + using (var fs = File.Create(path)) using (var gs = new GZipStream(fs, CompressionMode.Compress)) { gs.Write(jsonBytes, 0, jsonBytes.Length); @@ -1830,13 +1841,17 @@ namespace MPF.Core // Otherwise, write out to a normal JSON else { - string file; - if (string.IsNullOrWhiteSpace(outputDirectory)) - file = "!submissionInfo.json"; - else - file = Path.Combine(outputDirectory, "!submissionInfo.json"); + var path = string.Empty; + if (string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) + path = "!submissionInfo.json"; + else if (string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = $"!submissionInfo_{filenameSuffix}.json"; + else if (!string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, "!submissionInfo.json"); + else if (!string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, $"!submissionInfo_{filenameSuffix}.json"); - using (var fs = File.Create(file)) + using (var fs = File.Create(path)) { fs.Write(jsonBytes, 0, jsonBytes.Length); } @@ -1855,12 +1870,13 @@ namespace MPF.Core /// Write the protection data to the output folder /// /// Output folder to write to + /// Optional suffix to append to the filename /// SubmissionInfo object containing the protection information /// True on success, false on error #if NET48 - public static bool WriteProtectionData(string outputDirectory, SubmissionInfo info) + public static bool WriteProtectionData(string outputDirectory, string filenameSuffix, SubmissionInfo info) #else - public static bool WriteProtectionData(string? outputDirectory, SubmissionInfo? info) + public static bool WriteProtectionData(string? outputDirectory, string? filenameSuffix, SubmissionInfo? info) #endif { // Check to see if the inputs are valid @@ -1870,13 +1886,17 @@ namespace MPF.Core // Now write out to a generic file try { - string file; - if (string.IsNullOrWhiteSpace(outputDirectory)) - file = "!protectionInfo.txt"; - else - file = Path.Combine(outputDirectory, "!protectionInfo.txt"); + var path = string.Empty; + if (string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) + path = "!protectionInfo.txt"; + else if (string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = $"!protectionInfo{filenameSuffix}.txt"; + else if (!string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, "!protectionInfo.txt"); + else if (!string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(filenameSuffix)) + path = Path.Combine(outputDirectory, $"!protectionInfo{filenameSuffix}.txt"); - using (var sw = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.Write))) + using (var sw = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write))) { foreach (var kvp in info.CopyProtection.FullProtections) { @@ -1998,16 +2018,17 @@ namespace MPF.Core /// 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 #if NET48 - private static List GetGeneratedFilePaths(string outputDirectory) + private static List GetGeneratedFilePaths(string outputDirectory, string filenameSuffix) #else - private static List GetGeneratedFilePaths(string? outputDirectory) + private static List GetGeneratedFilePaths(string? outputDirectory, string? filenameSuffix) #endif { var files = new List(); - if (string.IsNullOrWhiteSpace(outputDirectory)) + if (string.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) { if (File.Exists("!submissionInfo.txt")) files.Add("!submissionInfo.txt"); @@ -2018,7 +2039,18 @@ namespace MPF.Core if (File.Exists("!protectionInfo.txt")) files.Add("!protectionInfo.txt"); } - else + else if (string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(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.IsNullOrWhiteSpace(outputDirectory) && string.IsNullOrWhiteSpace(filenameSuffix)) { if (File.Exists(Path.Combine(outputDirectory, "!submissionInfo.txt"))) files.Add(Path.Combine(outputDirectory, "!submissionInfo.txt")); @@ -2029,6 +2061,17 @@ namespace MPF.Core if (File.Exists(Path.Combine(outputDirectory, "!protectionInfo.txt"))) files.Add(Path.Combine(outputDirectory, "!protectionInfo.txt")); } + else if (!string.IsNullOrWhiteSpace(outputDirectory) && !string.IsNullOrWhiteSpace(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; }