Skip to content

Commit

Permalink
Add and use CustomOutputFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Aug 24, 2024
1 parent 5e1777a commit a35c13b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
92 changes: 92 additions & 0 deletions MPF.Processors/CustomOutputFile.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Represents a single output file with custom detection rules
/// </summary>
public class CustomOutputFile : OutputFile
{
/// <summary>
/// Optional func for determining if a file exists
/// </summary>
private readonly Func<string, bool> _existsFunc;

/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
public CustomOutputFile(string filename, OutputFileFlags flags, Func<string, bool> existsFunc)
: base([filename], flags)
{
_existsFunc = existsFunc;
}

/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
public CustomOutputFile(string filename, OutputFileFlags flags, string artifactKey, Func<string, bool> existsFunc)
: base([filename], flags, artifactKey)
{
_existsFunc = existsFunc;
}

/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
public CustomOutputFile(string[] filenames, OutputFileFlags flags, Func<string, bool> existsFunc)
: base(filenames, flags)
{
_existsFunc = existsFunc;
}

/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
public CustomOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func<string, bool> existsFunc)
: base(filenames, flags, artifactKey)
{
_existsFunc = existsFunc;
}

/// <summary>
/// Indicates if an output file exists in a base directory
/// </summary>
/// <param name="baseDirectory">Base directory to check in</param>
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
/// <summary>
/// Indicates if an output file exists in an archive
/// </summary>
/// <param name="archive">Zip archive to check in</param>
public override bool Exists(ZipArchive? archive)
{
// Files aren't extracted so this check can't be done
return false;
}
#endif
}
}
35 changes: 8 additions & 27 deletions MPF.Processors/OutputFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,30 @@ public bool IsZippable
/// </summary>
protected readonly OutputFileFlags _flags;

/// <summary>
/// Optional func for determining if a file exists
/// </summary>
protected readonly Func<string, bool>? _existsFunc;

/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
public OutputFile(string filename, OutputFileFlags flags, Func<string, bool>? existsFunc = null)
: this([filename], flags, existsFunc)
public OutputFile(string filename, OutputFileFlags flags)
: this([filename], flags)
{
}

/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
public OutputFile(string filename, OutputFileFlags flags, string artifactKey, Func<string, bool>? existsFunc = null)
: this([filename], flags, artifactKey, existsFunc)
public OutputFile(string filename, OutputFileFlags flags, string artifactKey)
: this([filename], flags, artifactKey)
{
}

/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
public OutputFile(string[] filenames, OutputFileFlags flags, Func<string, bool>? existsFunc = null)
public OutputFile(string[] filenames, OutputFileFlags flags)
{
Filenames = filenames;
ArtifactKey = null;
_flags = flags;
_existsFunc = existsFunc;

// Validate the inputs
if (filenames.Length == 0)
Expand All @@ -182,12 +176,11 @@ public OutputFile(string[] filenames, OutputFileFlags flags, Func<string, bool>?
/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
public OutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func<string, bool>? 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)
Expand All @@ -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 { }
}
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions MPF.Processors/Redumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public override List<OutputFile> 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,
Expand Down Expand Up @@ -450,7 +450,7 @@ public override List<OutputFile> 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
Expand Down Expand Up @@ -503,7 +503,7 @@ public override List<OutputFile> 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
Expand Down
21 changes: 8 additions & 13 deletions MPF.Processors/RegexOutputFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,32 @@ public class RegexOutputFile : OutputFile
/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
/// <remarks><paramref name="existFunc">is unused in this constructor</remarks>
public RegexOutputFile(string filename, OutputFileFlags flags, Func<string, bool>? existsFunc = null)
: base([filename], flags, existsFunc: null)
public RegexOutputFile(string filename, OutputFileFlags flags)
: base([filename], flags)
{
}

/// <summary>
/// Create an OutputFile with a single filename
/// </summary>
/// <remarks><paramref name="existFunc">is unused in this constructor</remarks>
public RegexOutputFile(string filename, OutputFileFlags flags, string artifactKey, Func<string, bool>? existsFunc = null)
: base([filename], flags, artifactKey, existsFunc: null)
public RegexOutputFile(string filename, OutputFileFlags flags, string artifactKey)
: base([filename], flags, artifactKey)
{
}

/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
/// <remarks><paramref name="existFunc">is unused in this constructor</remarks>
public RegexOutputFile(string[] filenames, OutputFileFlags flags, Func<string, bool>? existsFunc = null)
: base(filenames, flags, existsFunc: null)
public RegexOutputFile(string[] filenames, OutputFileFlags flags)
: base(filenames, flags)
{
}

/// <summary>
/// Create an OutputFile with set of filenames
/// </summary>
/// <remarks><paramref name="existFunc">is unused in this constructor</remarks>
public RegexOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey, Func<string, bool>? existsFunc = null)
: base(filenames, flags, artifactKey, existsFunc: null)
public RegexOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey)
: base(filenames, flags, artifactKey)
{
}

Expand All @@ -63,7 +59,6 @@ public override bool Exists(string baseDirectory)
return false;
}


#if NET452_OR_GREATER || NETCOREAPP
/// <summary>
/// Indicates if an output file exists in an archive
Expand Down

0 comments on commit a35c13b

Please sign in to comment.