From 4b4027f285ec8d819042b131ecf3d00b7315ee88 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 22 Aug 2024 13:18:47 -0400 Subject: [PATCH] Make helper class more robust --- CHANGELIST.md | 1 + MPF.Processors/OutputFile.cs | 163 ++++++++++++++++++++++++++++++++--- 2 files changed, 150 insertions(+), 14 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index c2c5e015..2a32d7b1 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -32,6 +32,7 @@ - Remove redundant drive calls - Start preparing for better output file checks - Create currently-unused helper class +- Make helper class more robust ### 3.2.1 (2024-08-05) diff --git a/MPF.Processors/OutputFile.cs b/MPF.Processors/OutputFile.cs index c9df24b7..7f8f9a85 100644 --- a/MPF.Processors/OutputFile.cs +++ b/MPF.Processors/OutputFile.cs @@ -1,9 +1,49 @@ +using System; +using System.IO; + namespace MPF.Processors { + /// + /// Represents attributes about an + /// + [Flags] + public enum OutputFileFlags : ushort + { + /// + /// Default state + /// + None = 0x0000, + + /// + /// File is required to exist + /// + Required = 0x0001, + + /// + /// File is included as an artifact + /// + Artifact = 0x0002, + + /// + /// File is included as a binary artifact + /// + Binary = 0x0004, + + /// + /// File can be deleted after processing + /// + Deleteable = 0x0008, + + /// + /// File can be zipped after processing + /// + Zippable = 0x0010, + } + /// /// Represents a single output file /// - internal class OutputFile + public class OutputFile { /// /// Set of all filename variants @@ -11,32 +51,127 @@ namespace MPF.Processors public string[] Filenames { get; private set; } /// - /// Indicates if the file is a required output + /// Indicates if the file is required /// - public bool Required { get; private set; } + public bool IsRequired + { + get + { +#if NET20 || NET35 + return (_flags & OutputFileFlags.Required) != 0; +#else + return _flags.HasFlag(OutputFileFlags.Required); +#endif + } + } /// - /// Indicates if the file should be included as an artifact + /// Indicates if the file is an artifact /// - public bool IsArtifact { get; private set; } + public bool IsArtifact + { + get + { +#if NET20 || NET35 + return (_flags & OutputFileFlags.Artifact) != 0 + || (_flags & OutputFileFlags.Binary) != 0; +#else + return _flags.HasFlag(OutputFileFlags.Artifact) + || _flags.HasFlag(OutputFileFlags.Binary); +#endif + } + } /// - /// Indicates if the file can be deleted after processing is finished + /// Indicates if the file is a binary artifact /// - public bool IsDeleteable { get; private set; } + public bool IsBinaryArtifact + { + get + { +#if NET20 || NET35 + return (_flags & OutputFileFlags.Binary) != 0; +#else + return _flags.HasFlag(OutputFileFlags.Binary); +#endif + } + } /// - /// Indicates if the file can be zipped after processing is finished + /// Indicates if the file is deleteable after processing /// - public bool IsZippable { get; private set; } + public bool IsDeleteable + { + get + { +#if NET20 || NET35 + return (_flags & OutputFileFlags.Deleteable) != 0; +#else + return _flags.HasFlag(OutputFileFlags.Deleteable); +#endif + } + } - public OutputFile(string filename, bool required, bool isArtifact, bool isDeleteable, bool isZippable) + /// + /// Indicates if the file is zippable after processing + /// + public bool IsZippable + { + get + { +#if NET20 || NET35 + return (_flags & OutputFileFlags.Zippable) != 0; +#else + return _flags.HasFlag(OutputFileFlags.Zippable); +#endif + } + } + + /// + /// Represents attributes about the current file + /// + private readonly OutputFileFlags _flags; + + /// + /// Create an OutputFile with a single filename + /// + public OutputFile(string filename, OutputFileFlags flags) { Filenames = [filename]; - Required = required; - IsArtifact = isArtifact; - IsDeleteable = IsDeleteable; - IsZippable = IsZippable; + _flags = flags; + } + + /// + /// Create an OutputFile with set of filenames + /// + public OutputFile(string[] filenames, OutputFileFlags flags) + { + Filenames = filenames; + _flags = flags; + } + + /// + /// Indicates if an output file exists in a base directory + /// + /// Base directory to check in + public 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 (File.Exists(possiblePath)) + return true; + } + catch { } + } + + return false; } } } \ No newline at end of file