[DatTools, Style] Make output names even better

This commit is contained in:
Matt Nadareski
2016-09-09 15:37:15 -07:00
parent 02c7853361
commit 35e8e8a61b
4 changed files with 34 additions and 47 deletions

View File

@@ -244,7 +244,7 @@ namespace SabreTools
return false; return false;
} }
// Otherwise, apply the rule ot the file // Otherwise, apply the rule to the file
string newfile = file + ".new"; string newfile = file + ".new";
Skippers.TransformFile(file, newfile, rule, _logger); Skippers.TransformFile(file, newfile, rule, _logger);

View File

@@ -1778,17 +1778,10 @@ namespace SabreTools.Helper
romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single,
root, logger, true, clean, softlist, keepext:(innerDatdata.XSV != null)); root, logger, true, clean, softlist, keepext:(innerDatdata.XSV != null));
// If the extension matches, append ".new" to the filename
string extension = ((innerDatdata.OutputFormatFlag & OutputFormatFlag.Xml) != 0 || (innerDatdata.OutputFormatFlag & OutputFormatFlag.SabreDat) != 0 ? ".xml" : ".dat");
if (outputDirectory == "" && Path.GetExtension(inputFileName) == extension)
{
innerDatdata.FileName += ".new";
}
// If we have roms, output them // If we have roms, output them
if (innerDatdata.Files.Count != 0) if (innerDatdata.Files.Count != 0)
{ {
WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(inputFileName) : outputDirectory), logger); WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(inputFileName) : outputDirectory), logger, overwrite: (outputDirectory == ""));
} }
} }
else if (Directory.Exists(inputFileName)) else if (Directory.Exists(inputFileName))
@@ -1803,17 +1796,10 @@ namespace SabreTools.Helper
innerDatdata = Parse(file, 0, 0, innerDatdata, gamename, romname, romtype, sgt, innerDatdata = Parse(file, 0, 0, innerDatdata, gamename, romname, romtype, sgt,
slt, seq, crc, md5, sha1, nodump, trim, single, root, logger, true, clean, keepext:(datdata.XSV != null)); slt, seq, crc, md5, sha1, nodump, trim, single, root, logger, true, clean, keepext:(datdata.XSV != null));
// If the extension matches, append ".new" to the filename
string extension = ((innerDatdata.OutputFormatFlag & OutputFormatFlag.Xml) != 0 || (innerDatdata.OutputFormatFlag & OutputFormatFlag.SabreDat) != 0 ? ".xml" : ".dat");
if (outputDirectory == "" && Path.GetExtension(file) == extension)
{
innerDatdata.FileName += ".new";
}
// If we have roms, output them // If we have roms, output them
if (innerDatdata.Files != null && innerDatdata.Files.Count != 0) if (innerDatdata.Files != null && innerDatdata.Files.Count != 0)
{ {
WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(file) : outputDirectory + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), logger); WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(file) : outputDirectory + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), logger, overwrite: (outputDirectory == ""));
} }
} }
} }
@@ -2545,17 +2531,10 @@ namespace SabreTools.Helper
romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single,
root, logger, true, clean, softlist, keepext: (innerDatdata.XSV != null)); root, logger, true, clean, softlist, keepext: (innerDatdata.XSV != null));
// If the extension matches, append ".new" to the filename
string extension = ((innerDatdata.OutputFormatFlag & OutputFormatFlag.Xml) != 0 || (innerDatdata.OutputFormatFlag & OutputFormatFlag.SabreDat) != 0 ? ".xml" : ".dat");
if (outputDirectory == "" && Path.GetExtension(inputFileName) == extension)
{
innerDatdata.FileName += ".new";
}
// If we have roms, output them // If we have roms, output them
if (innerDatdata.Files.Count != 0) if (innerDatdata.Files.Count != 0)
{ {
WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(inputFileName) : outputDirectory), logger); WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(inputFileName) : outputDirectory), logger, overwrite: (outputDirectory == ""));
} }
} }
else if (Directory.Exists(inputFileName)) else if (Directory.Exists(inputFileName))
@@ -2572,17 +2551,10 @@ namespace SabreTools.Helper
innerDatdata = Parse(file, 0, 0, innerDatdata, gamename, romname, romtype, sgt, innerDatdata = Parse(file, 0, 0, innerDatdata, gamename, romname, romtype, sgt,
slt, seq, crc, md5, sha1, nodump, trim, single, root, logger, true, clean, keepext: (datdata.XSV != null)); slt, seq, crc, md5, sha1, nodump, trim, single, root, logger, true, clean, keepext: (datdata.XSV != null));
// If the extension matches, append ".new" to the filename
string extension = ((innerDatdata.OutputFormatFlag & OutputFormatFlag.Xml) != 0 || (innerDatdata.OutputFormatFlag & OutputFormatFlag.SabreDat) != 0 ? ".xml" : ".dat");
if (outputDirectory == "" && Path.GetExtension(file) == extension)
{
innerDatdata.FileName += ".new";
}
// If we have roms, output them // If we have roms, output them
if (innerDatdata.Files != null && innerDatdata.Files.Count != 0) if (innerDatdata.Files != null && innerDatdata.Files.Count != 0)
{ {
WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(file) : outputDirectory + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), logger); WriteDatfile(innerDatdata, (outputDirectory == "" ? Path.GetDirectoryName(file) : outputDirectory + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), logger, overwrite: (outputDirectory == ""));
} }
}); });
} }
@@ -2685,12 +2657,13 @@ namespace SabreTools.Helper
/// <param name="norename">True if games should only be compared on game and file name (default), false if system and source are counted</param> /// <param name="norename">True if games should only be compared on game and file name (default), false if system and source are counted</param>
/// <param name="stats">True if DAT statistics should be output on write, false otherwise (default)</param> /// <param name="stats">True if DAT statistics should be output on write, false otherwise (default)</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param> /// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <param name="overwrite">True if files should be overwritten (default), false if they should be renamed instead</param>
/// <returns>True if the DAT was written correctly, false otherwise</returns> /// <returns>True if the DAT was written correctly, false otherwise</returns>
/// <remarks> /// <remarks>
/// The following features have been requested for file output: /// The following features have been requested for file output:
/// - Have the ability to strip special (non-ASCII) characters from rom information /// - Have the ability to strip special (non-ASCII) characters from rom information
/// </remarks> /// </remarks>
public static bool WriteDatfile(Dat datdata, string outDir, Logger logger, bool norename = true, bool stats = false, bool ignoreblanks = false) public static bool WriteDatfile(Dat datdata, string outDir, Logger logger, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true)
{ {
// If there's nothing there, abort // If there's nothing there, abort
if (datdata.Files == null || datdata.Files.Count == 0) if (datdata.Files == null || datdata.Files.Count == 0)
@@ -2760,7 +2733,7 @@ namespace SabreTools.Helper
SortedDictionary<string, List<Rom>> sortable = BucketByGame(datdata.Files, datdata.MergeRoms, norename, logger); SortedDictionary<string, List<Rom>> sortable = BucketByGame(datdata.Files, datdata.MergeRoms, norename, logger);
// Get the outfile name // Get the outfile name
Dictionary<OutputFormatFlag, string> outfiles = Style.CreateOutfileNames(outDir, datdata); Dictionary<OutputFormatFlag, string> outfiles = Style.CreateOutfileNames(outDir, datdata, overwrite);
try try
{ {

View File

@@ -152,8 +152,9 @@ namespace SabreTools.Helper
/// </summary> /// </summary>
/// <param name="outDir">Output directory</param> /// <param name="outDir">Output directory</param>
/// <param name="datdata">DAT information</param> /// <param name="datdata">DAT information</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 static Dictionary<OutputFormatFlag, string> CreateOutfileNames(string outDir, Dat datdata) public static Dictionary<OutputFormatFlag, string> CreateOutfileNames(string outDir, Dat datdata, bool overwrite = true)
{ {
// Create the output dictionary // Create the output dictionary
Dictionary<OutputFormatFlag, string> outfileNames = new Dictionary<OutputFormatFlag, string>(); Dictionary<OutputFormatFlag, string> outfileNames = new Dictionary<OutputFormatFlag, string>();
@@ -167,39 +168,39 @@ namespace SabreTools.Helper
// Get the extensions from the output type // Get the extensions from the output type
if ((datdata.OutputFormatFlag & OutputFormatFlag.Xml) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.Xml) != 0)
{ {
outfileNames.Add(OutputFormatFlag.Xml, CreateOutfileNamesHelper(outDir, ".xml", datdata)); outfileNames.Add(OutputFormatFlag.Xml, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.ClrMamePro) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.ClrMamePro) != 0)
{ {
outfileNames.Add(OutputFormatFlag.ClrMamePro, CreateOutfileNamesHelper(outDir, ".dat", datdata)); outfileNames.Add(OutputFormatFlag.ClrMamePro, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.RomCenter) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.RomCenter) != 0)
{ {
outfileNames.Add(OutputFormatFlag.RomCenter, CreateOutfileNamesHelper(outDir, ".rc.dat", datdata)); outfileNames.Add(OutputFormatFlag.RomCenter, CreateOutfileNamesHelper(outDir, ".rc.dat", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.DOSCenter) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.DOSCenter) != 0)
{ {
outfileNames.Add(OutputFormatFlag.DOSCenter, CreateOutfileNamesHelper(outDir, ".dc.dat", datdata)); outfileNames.Add(OutputFormatFlag.DOSCenter, CreateOutfileNamesHelper(outDir, ".dc.dat", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.MissFile) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.MissFile) != 0)
{ {
outfileNames.Add(OutputFormatFlag.MissFile, CreateOutfileNamesHelper(outDir, ".txt", datdata)); outfileNames.Add(OutputFormatFlag.MissFile, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.SabreDat) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.SabreDat) != 0)
{ {
outfileNames.Add(OutputFormatFlag.SabreDat, CreateOutfileNamesHelper(outDir, ".sd.xml", datdata)); outfileNames.Add(OutputFormatFlag.SabreDat, CreateOutfileNamesHelper(outDir, ".sd.xml", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.RedumpMD5) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.RedumpMD5) != 0)
{ {
outfileNames.Add(OutputFormatFlag.RedumpMD5, CreateOutfileNamesHelper(outDir, ".md5", datdata)); outfileNames.Add(OutputFormatFlag.RedumpMD5, CreateOutfileNamesHelper(outDir, ".md5", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.RedumpSHA1) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.RedumpSHA1) != 0)
{ {
outfileNames.Add(OutputFormatFlag.RedumpSHA1, CreateOutfileNamesHelper(outDir, ".sha1", datdata)); outfileNames.Add(OutputFormatFlag.RedumpSHA1, CreateOutfileNamesHelper(outDir, ".sha1", datdata, overwrite));
}; };
if ((datdata.OutputFormatFlag & OutputFormatFlag.RedumpSFV) != 0) if ((datdata.OutputFormatFlag & OutputFormatFlag.RedumpSFV) != 0)
{ {
outfileNames.Add(OutputFormatFlag.RedumpSFV, CreateOutfileNamesHelper(outDir, ".sfv", datdata)); outfileNames.Add(OutputFormatFlag.RedumpSFV, CreateOutfileNamesHelper(outDir, ".sfv", datdata, overwrite));
}; };
return outfileNames; return outfileNames;
@@ -211,14 +212,27 @@ namespace SabreTools.Helper
/// <param name="outDir">Output directory</param> /// <param name="outDir">Output directory</param>
/// <param name="extension">Extension to use for the file</param> /// <param name="extension">Extension to use for the file</param>
/// <param name="datdata">DAT information</param> /// <param name="datdata">DAT information</param>
/// <param name="overwrite">True if we ignore existing files, false otherwise</param>
/// <returns>String containing the new filename</returns> /// <returns>String containing the new filename</returns>
private static string CreateOutfileNamesHelper(string outDir, string extension, Dat datdata) private static string CreateOutfileNamesHelper(string outDir, string extension, Dat datdata, bool overwrite)
{ {
string filename = (String.IsNullOrEmpty(datdata.FileName) ? datdata.Description : datdata.FileName); string filename = (String.IsNullOrEmpty(datdata.FileName) ? datdata.Description : datdata.FileName);
string outfile = outDir + filename + extension; string outfile = outDir + filename + extension;
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ? outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) : outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
outfile); outfile);
if (!overwrite)
{
int i = 1;
while (File.Exists(outfile))
{
outfile = outDir + filename + "_" + i + extension;
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
outfile);
i++;
}
}
return outfile; return outfile;
} }

View File

@@ -571,7 +571,7 @@ namespace SabreTools
// If we have have a non-empty rule, apply it // If we have have a non-empty rule, apply it
if (rule.Tests != null && rule.Tests.Count != 0) if (rule.Tests != null && rule.Tests.Count != 0)
{ {
// Otherwise, apply the rule ot the file // Otherwise, apply the rule to the file
string newinput = input + ".new"; string newinput = input + ".new";
Skippers.TransformFile(input, newinput, rule, _logger); Skippers.TransformFile(input, newinput, rule, _logger);
Rom drom = FileTools.GetSingleFileInfo(newinput); Rom drom = FileTools.GetSingleFileInfo(newinput);