mirror of
https://github.com/SabreTools/MPF.git
synced 2026-07-02 17:24:48 +00:00
Add and use CustomOutputFile
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
92
MPF.Processors/CustomOutputFile.cs
Normal file
92
MPF.Processors/CustomOutputFile.cs
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -141,36 +141,30 @@ namespace MPF.Processors
|
||||
/// </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)
|
||||
@@ -182,12 +176,11 @@ namespace MPF.Processors
|
||||
/// <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)
|
||||
@@ -211,16 +204,8 @@ namespace MPF.Processors
|
||||
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 @@ namespace MPF.Processors
|
||||
|
||||
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;
|
||||
|
||||
@@ -385,7 +385,7 @@ namespace MPF.Processors
|
||||
| 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 @@ namespace MPF.Processors
|
||||
| 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 @@ namespace MPF.Processors
|
||||
| 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
|
||||
|
||||
@@ -16,36 +16,32 @@ namespace MPF.Processors
|
||||
/// <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)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -63,7 +59,6 @@ namespace MPF.Processors
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#if NET452_OR_GREATER || NETCOREAPP
|
||||
/// <summary>
|
||||
/// Indicates if an output file exists in an archive
|
||||
|
||||
Reference in New Issue
Block a user