Middle step for outfile names

This commit is contained in:
Matt Nadareski
2025-01-29 13:16:20 -05:00
parent 8c8dd4ea83
commit cf657138fc
3 changed files with 79 additions and 8 deletions

View File

@@ -507,18 +507,19 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Generate a proper outfile name based on a DAT and output directory /// Generate a proper outfile name based on a DAT and output directory
/// </summary> /// </summary>
/// <param name="datHeader">DatHeader value to pull information from</param>
/// <param name="outDir">Output directory</param> /// <param name="outDir">Output directory</param>
/// <param name="overwrite">True if we ignore existing files (default), false otherwise</param> /// <param name="overwrite">True if we ignore existing files (default), false otherwise</param>
/// <returns>Dictionary of output formats mapped to file names</returns> /// <returns>Dictionary of output formats mapped to file names</returns>
public Dictionary<DatFormat, string> CreateOutFileNames(string outDir, bool overwrite = true) public static Dictionary<DatFormat, string> CreateOutFileNames(DatHeader datHeader, string outDir, bool overwrite = true)
{ {
// Create the output dictionary // Create the output dictionary
Dictionary<DatFormat, string> outfileNames = []; Dictionary<DatFormat, string> outfileNames = [];
// Get the filename to use // Get the filename to use
string? filename = string.IsNullOrEmpty(GetStringFieldValue(DatHeader.FileNameKey)) string? filename = string.IsNullOrEmpty(datHeader.GetStringFieldValue(DatHeader.FileNameKey))
? GetStringFieldValue(Models.Metadata.Header.DescriptionKey) ? datHeader.GetStringFieldValue(Models.Metadata.Header.DescriptionKey)
: GetStringFieldValue(DatHeader.FileNameKey); : datHeader.GetStringFieldValue(DatHeader.FileNameKey);
// Strip off the extension if it's a holdover from the DAT // Strip off the extension if it's a holdover from the DAT
if (Utilities.HasValidDatExtension(filename)) if (Utilities.HasValidDatExtension(filename))
@@ -529,7 +530,7 @@ namespace SabreTools.DatFiles
outDir += Path.DirectorySeparatorChar; outDir += Path.DirectorySeparatorChar;
// Get the current format types // Get the current format types
DatFormat datFormat = GetFieldValue<DatFormat>(DatHeader.DatFormatKey); DatFormat datFormat = datHeader.GetFieldValue<DatFormat>(DatHeader.DatFormatKey);
List<DatFormat> usedFormats = SplitFormats(datFormat); List<DatFormat> usedFormats = SplitFormats(datFormat);
// Get the extensions from the output type // Get the extensions from the output type

View File

@@ -16,6 +16,76 @@ namespace SabreTools.DatTools
/// </summary> /// </summary>
public class Writer public class Writer
{ {
#region Private Constants
/// <summary>
/// Map of all formats to extensions, including "backup" extensions
/// </summary>
private static readonly Dictionary<DatFormat, string[]> ExtensionMappings = new()
{
// .csv
{ DatFormat.CSV, new string[] { ".csv" } },
// .dat
{ DatFormat.ClrMamePro, new string[] { ".dat" } },
{ DatFormat.RomCenter, new string[] { ".dat", ".rc.dat" } },
{ DatFormat.DOSCenter, new string[] { ".dat", ".dc.dat" } },
// .json
{ DatFormat.SabreJSON, new string[] { ".json" } },
// .md2
{ DatFormat.RedumpMD2, new string[] { ".md2" } },
// .md4
{ DatFormat.RedumpMD4, new string[] { ".md4" } },
// .md5
{ DatFormat.RedumpMD5, new string[] { ".md5" } },
// .sfv
{ DatFormat.RedumpSFV, new string[] { ".sfv" } },
// .sha1
{ DatFormat.RedumpSHA1, new string[] { ".sha1" } },
// .sha256
{ DatFormat.RedumpSHA256, new string[] { ".sha256" } },
// .sha384
{ DatFormat.RedumpSHA384, new string[] { ".sha384" } },
// .sha512
{ DatFormat.RedumpSHA512, new string[] { ".sha512" } },
// .spamsum
{ DatFormat.RedumpSpamSum, new string[] { ".spamsum" } },
// .ssv
{ DatFormat.SSV, new string[] { ".ssv" } },
// .tsv
{ DatFormat.TSV, new string[] { ".tsv" } },
// .txt
{ DatFormat.AttractMode, new string[] { ".txt" } },
{ DatFormat.Listrom, new string[] { ".txt", ".lr.txt" } },
{ DatFormat.MissFile, new string[] { ".txt", ".miss.txt" } },
{ DatFormat.EverdriveSMDB, new string[] { ".txt", ".smdb.txt" } },
// .xml
{ DatFormat.Logiqx, new string[] { ".xml" } },
{ DatFormat.LogiqxDeprecated, new string[] { ".xml", ".xml" } }, // Intentional duplicate
{ DatFormat.SabreXML, new string[] { ".xml", ".sd.xml" } },
{ DatFormat.SoftwareList, new string[] { ".xml", ".sl.xml" } },
{ DatFormat.Listxml, new string[] { ".xml", ".mame.xml" } },
{ DatFormat.OfflineList, new string[] { ".xml", ".ol.xml" } },
{ DatFormat.OpenMSX, new string[] { ".xml", ".msx.xml" } },
{ DatFormat.ArchiveDotOrg, new string[] { ".xml", ".ado.xml" } },
};
#endregion
#region Logging #region Logging
/// <summary> /// <summary>
@@ -83,7 +153,7 @@ namespace SabreTools.DatTools
_staticLogger.User($"A total of {datFile.DatStatistics.TotalCount - datFile.DatStatistics.RemovedCount} items will be written out to '{datFile.Header.GetStringFieldValue(DatHeader.FileNameKey)}'"); _staticLogger.User($"A total of {datFile.DatStatistics.TotalCount - datFile.DatStatistics.RemovedCount} items will be written out to '{datFile.Header.GetStringFieldValue(DatHeader.FileNameKey)}'");
// Get the outfile names // Get the outfile names
Dictionary<DatFormat, string> outfiles = datFile.Header.CreateOutFileNames(outDir!, overwrite); Dictionary<DatFormat, string> outfiles = DatHeader.CreateOutFileNames(datFile.Header, outDir!, overwrite);
try try
{ {

View File

@@ -20,7 +20,7 @@ namespace SabreTools.Test.DatFiles
// Invoke the method // Invoke the method
string outDir = "C:\\Test"; string outDir = "C:\\Test";
var actual = datHeader.CreateOutFileNames(outDir, overwrite: true); var actual = DatHeader.CreateOutFileNames(datHeader, outDir, overwrite: true);
// Check the result // Check the result
string expected = $"{outDir}{System.IO.Path.DirectorySeparatorChar}test.{extension}"; string expected = $"{outDir}{System.IO.Path.DirectorySeparatorChar}test.{extension}";
@@ -38,7 +38,7 @@ namespace SabreTools.Test.DatFiles
// Invoke the method // Invoke the method
string outDir = "C:\\Test"; string outDir = "C:\\Test";
var actual = datHeader.CreateOutFileNames(outDir, overwrite: true); var actual = DatHeader.CreateOutFileNames(datHeader, outDir, overwrite: true);
// Check the result // Check the result
Assert.Equal(28, actual.Count); Assert.Equal(28, actual.Count);