From a35c13bd104eb13f802a3faae7c97c5be439123a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 23 Aug 2024 21:36:04 -0400 Subject: [PATCH] Add and use CustomOutputFile --- CHANGELIST.md | 1 + MPF.Processors/CustomOutputFile.cs | 92 ++++++++++++++++++++++++++++++ MPF.Processors/OutputFile.cs | 35 +++--------- MPF.Processors/Redumper.cs | 6 +- MPF.Processors/RegexOutputFile.cs | 21 +++---- 5 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 MPF.Processors/CustomOutputFile.cs diff --git a/CHANGELIST.md b/CHANGELIST.md index 6c53c97f..7cac1971 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -60,6 +60,7 @@ - Less confusing implmentation of DatfileExists - Forgot the other locations - Add future XGD output files +- Add and use CustomOutputFile ### 3.2.1 (2024-08-05) diff --git a/MPF.Processors/CustomOutputFile.cs b/MPF.Processors/CustomOutputFile.cs new file mode 100644 index 00000000..f1734bc2 --- /dev/null +++ b/MPF.Processors/CustomOutputFile.cs @@ -0,0 +1,92 @@ +using System; +using System.IO; +#if NET452_OR_GREATER || NETCOREAPP +using System.IO.Compression; +#endif +using System.Linq; + +namespace MPF.Processors +{ + /// + /// Represents a single output file with custom detection rules + /// + public class CustomOutputFile : OutputFile + { + /// + /// Optional func for determining if a file exists + /// + private readonly Func _existsFunc; + + /// + /// Create an OutputFile with a single filename + /// + public CustomOutputFile(string filename, OutputFileFlags flags, Func existsFunc) + : base([filename], flags) + { + _existsFunc = existsFunc; + } + + /// + /// Create an OutputFile with a single filename + /// + public CustomOutputFile(string filename, OutputFileFlags flags, string artifactKey, Func existsFunc) + : base([filename], flags, artifactKey) + { + _existsFunc = existsFunc; + } + + /// + /// Create an OutputFile with set of filenames + /// + public CustomOutputFile(string[] filenames, OutputFileFlags flags, Func existsFunc) + : base(filenames, flags) + { + _existsFunc = existsFunc; + } + + /// + /// Create an OutputFile with set of filenames + /// + public CustomOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func existsFunc) + : base(filenames, flags, artifactKey) + { + _existsFunc = existsFunc; + } + + /// + /// Indicates if an output file exists in a base directory + /// + /// Base directory to check in + public override bool Exists(string baseDirectory) + { + foreach (string filename in Filenames) + { + // Check for invalid filenames + if (string.IsNullOrEmpty(filename)) + continue; + + try + { + string possiblePath = Path.Combine(baseDirectory, filename); + if (_existsFunc(possiblePath)) + return true; + } + catch { } + } + + return false; + } + +#if NET452_OR_GREATER || NETCOREAPP + /// + /// Indicates if an output file exists in an archive + /// + /// Zip archive to check in + public override bool Exists(ZipArchive? archive) + { + // Files aren't extracted so this check can't be done + return false; + } +#endif + } +} \ No newline at end of file diff --git a/MPF.Processors/OutputFile.cs b/MPF.Processors/OutputFile.cs index 021b5842..895169bd 100644 --- a/MPF.Processors/OutputFile.cs +++ b/MPF.Processors/OutputFile.cs @@ -141,36 +141,30 @@ public bool IsZippable /// protected readonly OutputFileFlags _flags; - /// - /// Optional func for determining if a file exists - /// - protected readonly Func? _existsFunc; - /// /// Create an OutputFile with a single filename /// - public OutputFile(string filename, OutputFileFlags flags, Func? existsFunc = null) - : this([filename], flags, existsFunc) + public OutputFile(string filename, OutputFileFlags flags) + : this([filename], flags) { } /// /// Create an OutputFile with a single filename /// - public OutputFile(string filename, OutputFileFlags flags, string artifactKey, Func? existsFunc = null) - : this([filename], flags, artifactKey, existsFunc) + public OutputFile(string filename, OutputFileFlags flags, string artifactKey) + : this([filename], flags, artifactKey) { } /// /// Create an OutputFile with set of filenames /// - public OutputFile(string[] filenames, OutputFileFlags flags, Func? existsFunc = null) + public OutputFile(string[] filenames, OutputFileFlags flags) { Filenames = filenames; ArtifactKey = null; _flags = flags; - _existsFunc = existsFunc; // Validate the inputs if (filenames.Length == 0) @@ -182,12 +176,11 @@ public OutputFile(string[] filenames, OutputFileFlags flags, Func? /// /// Create an OutputFile with set of filenames /// - public OutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func? existsFunc = null) + public OutputFile(string[] filenames, OutputFileFlags flags, string artifactKey) { Filenames = filenames; ArtifactKey = artifactKey; _flags = flags; - _existsFunc = existsFunc; // Validate the inputs if (filenames.Length == 0) @@ -211,16 +204,8 @@ public virtual bool Exists(string baseDirectory) try { string possiblePath = Path.Combine(baseDirectory, filename); - if (_existsFunc != null) - { - if (_existsFunc(possiblePath)) - return true; - } - else - { - if (File.Exists(possiblePath)) - return true; - } + if (File.Exists(possiblePath)) + return true; } catch { } } @@ -247,10 +232,6 @@ public virtual bool Exists(ZipArchive? archive) try { - // Existence function can't be used, so those files are skipped - if (_existsFunc != null) - return false; - // Check all entries on filename alone if (archive.Entries.Any(e => e.Name == filename)) return true; diff --git a/MPF.Processors/Redumper.cs b/MPF.Processors/Redumper.cs index cdf27416..2d4763e6 100644 --- a/MPF.Processors/Redumper.cs +++ b/MPF.Processors/Redumper.cs @@ -385,7 +385,7 @@ public override List GetOutputFiles(string baseFilename) | OutputFileFlags.Artifact | OutputFileFlags.Zippable, "log"), - new($"{baseFilename}.log", OutputFileFlags.Required, + new CustomOutputFile($"{baseFilename}.log", OutputFileFlags.Required, DatfileExists), new($"{baseFilename}.pma", OutputFileFlags.Binary | OutputFileFlags.Zippable, @@ -450,7 +450,7 @@ public override List GetOutputFiles(string baseFilename) | OutputFileFlags.Artifact | OutputFileFlags.Zippable, "log"), - new($"{baseFilename}.log", OutputFileFlags.Required, + new CustomOutputFile($"{baseFilename}.log", OutputFileFlags.Required, DatfileExists), new([$"{baseFilename}.manufacturer", $"{baseFilename}.1.manufacturer"], OutputFileFlags.Required | OutputFileFlags.Binary @@ -503,7 +503,7 @@ public override List GetOutputFiles(string baseFilename) | OutputFileFlags.Artifact | OutputFileFlags.Zippable, "log"), - new($"{baseFilename}.log", OutputFileFlags.Required, + new CustomOutputFile($"{baseFilename}.log", OutputFileFlags.Required, DatfileExists), new([$"{baseFilename}.physical", $"{baseFilename}.0.physical"], OutputFileFlags.Required | OutputFileFlags.Binary diff --git a/MPF.Processors/RegexOutputFile.cs b/MPF.Processors/RegexOutputFile.cs index 87df9d62..60e518c1 100644 --- a/MPF.Processors/RegexOutputFile.cs +++ b/MPF.Processors/RegexOutputFile.cs @@ -16,36 +16,32 @@ public class RegexOutputFile : OutputFile /// /// Create an OutputFile with a single filename /// - /// is unused in this constructor - public RegexOutputFile(string filename, OutputFileFlags flags, Func? existsFunc = null) - : base([filename], flags, existsFunc: null) + public RegexOutputFile(string filename, OutputFileFlags flags) + : base([filename], flags) { } /// /// Create an OutputFile with a single filename /// - /// is unused in this constructor - public RegexOutputFile(string filename, OutputFileFlags flags, string artifactKey, Func? existsFunc = null) - : base([filename], flags, artifactKey, existsFunc: null) + public RegexOutputFile(string filename, OutputFileFlags flags, string artifactKey) + : base([filename], flags, artifactKey) { } /// /// Create an OutputFile with set of filenames /// - /// is unused in this constructor - public RegexOutputFile(string[] filenames, OutputFileFlags flags, Func? existsFunc = null) - : base(filenames, flags, existsFunc: null) + public RegexOutputFile(string[] filenames, OutputFileFlags flags) + : base(filenames, flags) { } /// /// Create an OutputFile with set of filenames /// - /// is unused in this constructor - public RegexOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func? existsFunc = null) - : base(filenames, flags, artifactKey, existsFunc: null) + public RegexOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey) + : base(filenames, flags, artifactKey) { } @@ -63,7 +59,6 @@ public override bool Exists(string baseDirectory) return false; } - #if NET452_OR_GREATER || NETCOREAPP /// /// Indicates if an output file exists in an archive