mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[DatFile, FileTools, Style] Slight code reorganization
This commit is contained in:
@@ -5583,7 +5583,7 @@ namespace SabreTools.Library.DatFiles
|
||||
}
|
||||
|
||||
// Get the outfile names
|
||||
Dictionary<DatFormat, string> outfiles = Style.CreateOutfileNames(outDir, this, overwrite);
|
||||
Dictionary<DatFormat, string> outfiles = CreateOutfileNames(outDir, overwrite);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -5667,6 +5667,202 @@ namespace SabreTools.Library.DatFiles
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a proper outfile name based on a DAT and output directory
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output directory</param>
|
||||
/// <param name="overwrite">True if we ignore existing files (default), false otherwise</param>
|
||||
/// <returns>Dictionary of output formats mapped to file names</returns>
|
||||
private Dictionary<DatFormat, string> CreateOutfileNames(string outDir, bool overwrite = true)
|
||||
{
|
||||
// Create the output dictionary
|
||||
Dictionary<DatFormat, string> outfileNames = new Dictionary<DatFormat, string>();
|
||||
|
||||
// Double check the outDir for the end delim
|
||||
if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
outDir += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
// Get the extensions from the output type
|
||||
|
||||
// AttractMode
|
||||
if ((DatFormat & DatFormat.AttractMode) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.AttractMode, CreateOutfileNamesHelper(outDir, ".txt", overwrite));
|
||||
}
|
||||
|
||||
// ClrMamePro
|
||||
if ((DatFormat & DatFormat.ClrMamePro) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.ClrMamePro, CreateOutfileNamesHelper(outDir, ".dat", overwrite));
|
||||
};
|
||||
|
||||
// CSV
|
||||
if ((DatFormat & DatFormat.CSV) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.CSV, CreateOutfileNamesHelper(outDir, ".csv", overwrite));
|
||||
};
|
||||
|
||||
// DOSCenter
|
||||
if ((DatFormat & DatFormat.DOSCenter) != 0
|
||||
&& (DatFormat & DatFormat.ClrMamePro) == 0
|
||||
&& (DatFormat & DatFormat.RomCenter) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dat", overwrite));
|
||||
};
|
||||
if ((DatFormat & DatFormat.DOSCenter) != 0
|
||||
&& ((DatFormat & DatFormat.ClrMamePro) != 0
|
||||
|| (DatFormat & DatFormat.RomCenter) != 0))
|
||||
{
|
||||
outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dc.dat", overwrite));
|
||||
}
|
||||
|
||||
//MAME Listroms
|
||||
if ((DatFormat & DatFormat.Listroms) != 0
|
||||
&& (DatFormat & DatFormat.AttractMode) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".txt", overwrite));
|
||||
}
|
||||
if ((DatFormat & DatFormat.Listroms) != 0
|
||||
&& (DatFormat & DatFormat.AttractMode) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".lr.txt", overwrite));
|
||||
}
|
||||
|
||||
// Logiqx XML
|
||||
if ((DatFormat & DatFormat.Logiqx) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.Logiqx, CreateOutfileNamesHelper(outDir, ".xml", overwrite));
|
||||
}
|
||||
|
||||
// Missfile
|
||||
if ((DatFormat & DatFormat.MissFile) != 0
|
||||
&& (DatFormat & DatFormat.AttractMode) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".txt", overwrite));
|
||||
}
|
||||
if ((DatFormat & DatFormat.MissFile) != 0
|
||||
&& (DatFormat & DatFormat.AttractMode) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".miss.txt", overwrite));
|
||||
}
|
||||
|
||||
// OfflineList
|
||||
if (((DatFormat & DatFormat.OfflineList) != 0)
|
||||
&& (DatFormat & DatFormat.Logiqx) == 0
|
||||
&& (DatFormat & DatFormat.SabreDat) == 0
|
||||
&& (DatFormat & DatFormat.SoftwareList) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".xml", overwrite));
|
||||
}
|
||||
if (((DatFormat & DatFormat.OfflineList) != 0
|
||||
&& ((DatFormat & DatFormat.Logiqx) != 0
|
||||
|| (DatFormat & DatFormat.SabreDat) != 0
|
||||
|| (DatFormat & DatFormat.SoftwareList) != 0)))
|
||||
{
|
||||
outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".ol.xml", overwrite));
|
||||
}
|
||||
|
||||
// Redump MD5
|
||||
if ((DatFormat & DatFormat.RedumpMD5) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpMD5, CreateOutfileNamesHelper(outDir, ".md5", overwrite));
|
||||
};
|
||||
|
||||
// Redump SFV
|
||||
if ((DatFormat & DatFormat.RedumpSFV) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpSFV, CreateOutfileNamesHelper(outDir, ".sfv", overwrite));
|
||||
};
|
||||
|
||||
// Redump SHA-1
|
||||
if ((DatFormat & DatFormat.RedumpSHA1) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpSHA1, CreateOutfileNamesHelper(outDir, ".sha1", overwrite));
|
||||
};
|
||||
|
||||
// Redump SHA-256
|
||||
if ((DatFormat & DatFormat.RedumpSHA256) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpSHA256, CreateOutfileNamesHelper(outDir, ".sha256", overwrite));
|
||||
};
|
||||
|
||||
// RomCenter
|
||||
if ((DatFormat & DatFormat.RomCenter) != 0
|
||||
&& (DatFormat & DatFormat.ClrMamePro) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".dat", overwrite));
|
||||
};
|
||||
if ((DatFormat & DatFormat.RomCenter) != 0
|
||||
&& (DatFormat & DatFormat.ClrMamePro) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".rc.dat", overwrite));
|
||||
};
|
||||
|
||||
// SabreDAT
|
||||
if ((DatFormat & DatFormat.SabreDat) != 0 && (DatFormat & DatFormat.Logiqx) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".xml", overwrite));
|
||||
};
|
||||
if ((DatFormat & DatFormat.SabreDat) != 0 && (DatFormat & DatFormat.Logiqx) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".sd.xml", overwrite));
|
||||
};
|
||||
|
||||
// Software List
|
||||
if ((DatFormat & DatFormat.SoftwareList) != 0
|
||||
&& (DatFormat & DatFormat.Logiqx) == 0
|
||||
&& (DatFormat & DatFormat.SabreDat) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".xml", overwrite));
|
||||
}
|
||||
if ((DatFormat & DatFormat.SoftwareList) != 0
|
||||
&& ((DatFormat & DatFormat.Logiqx) != 0
|
||||
|| (DatFormat & DatFormat.SabreDat) != 0))
|
||||
{
|
||||
outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".sl.xml", overwrite));
|
||||
}
|
||||
|
||||
// TSV
|
||||
if ((DatFormat & DatFormat.TSV) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.TSV, CreateOutfileNamesHelper(outDir, ".tsv", overwrite));
|
||||
};
|
||||
|
||||
return outfileNames;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Help generating the outfile name
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output directory</param>
|
||||
/// <param name="extension">Extension to use for the file</param>
|
||||
/// <param name="overwrite">True if we ignore existing files, false otherwise</param>
|
||||
/// <returns>String containing the new filename</returns>
|
||||
private string CreateOutfileNamesHelper(string outDir, string extension, bool overwrite)
|
||||
{
|
||||
string filename = (String.IsNullOrEmpty(FileName) ? Description : FileName);
|
||||
string outfile = outDir + filename + extension;
|
||||
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
|
||||
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
|
||||
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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion // Instance Methods
|
||||
@@ -5701,7 +5897,7 @@ namespace SabreTools.Library.DatFiles
|
||||
outDir = Path.GetFullPath(outDir);
|
||||
|
||||
// Get the dictionary of desired output report names
|
||||
Dictionary<StatReportFormat, string> outputs = Style.CreateOutStatsNames(outDir, statDatFormat, reportName);
|
||||
Dictionary<StatReportFormat, string> outputs = CreateOutStatsNames(outDir, statDatFormat, reportName);
|
||||
|
||||
// Make sure we have all files and then order them
|
||||
List<string> files = FileTools.GetOnlyFilesFromInputs(inputs);
|
||||
@@ -5851,6 +6047,80 @@ namespace SabreTools.Library.DatFiles
|
||||
Please check the log folder if the stats scrolled offscreen", false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the proper extension for the stat output format
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output path to use</param>
|
||||
/// <param name="statDatFormat">StatDatFormat to get the extension for</param>
|
||||
/// <param name="reportName">Name of the input file to use</param>
|
||||
/// <returns>Dictionary of output formats mapped to file names</returns>
|
||||
private static Dictionary<StatReportFormat, string> CreateOutStatsNames(string outDir, StatReportFormat statDatFormat, string reportName, bool overwrite = true)
|
||||
{
|
||||
Dictionary<StatReportFormat, string> output = new Dictionary<StatReportFormat, string>();
|
||||
|
||||
// First try to create the output directory if we need to
|
||||
if (!Directory.Exists(outDir))
|
||||
{
|
||||
Directory.CreateDirectory(outDir);
|
||||
}
|
||||
|
||||
// Double check the outDir for the end delim
|
||||
if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
outDir += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
// For each output format, get the appropriate stream writer
|
||||
if ((statDatFormat & StatReportFormat.None) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.None, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite));
|
||||
}
|
||||
if ((statDatFormat & StatReportFormat.CSV) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite));
|
||||
}
|
||||
if ((statDatFormat & StatReportFormat.HTML) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite));
|
||||
}
|
||||
if ((statDatFormat & StatReportFormat.TSV) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Help generating the outstats name
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output directory</param>
|
||||
/// <param name="extension">Extension to use for the file</param>
|
||||
/// <param name="reportName">Name of the input file to use</param>
|
||||
/// <param name="overwrite">True if we ignore existing files, false otherwise</param>
|
||||
/// <returns>String containing the new filename</returns>
|
||||
private static string CreateOutStatsNamesHelper(string outDir, string extension, string reportName, bool overwrite)
|
||||
{
|
||||
string outfile = outDir + reportName + extension;
|
||||
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
|
||||
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
|
||||
outfile);
|
||||
if (!overwrite)
|
||||
{
|
||||
int i = 1;
|
||||
while (File.Exists(outfile))
|
||||
{
|
||||
outfile = outDir + reportName + "_" + 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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion // Static Methods
|
||||
|
||||
@@ -41,6 +41,162 @@ namespace SabreTools.Library.Tools
|
||||
/// </summary>
|
||||
public static class FileTools
|
||||
{
|
||||
#region BinaryReader Extensions
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of bytes from the stream, starting from a specified point in the byte array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer to read data into.</param>
|
||||
/// <param name="index">The starting point in the buffer at which to begin reading into the buffer.</param>
|
||||
/// <param name="count">The number of bytes to read.</param>
|
||||
/// <returns>The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached.</returns>
|
||||
public static int ReadReverse(this BinaryReader reader, byte[] buffer, int index, int count)
|
||||
{
|
||||
int retval = reader.Read(buffer, index, count);
|
||||
buffer = buffer.Reverse().ToArray();
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of characters from the stream, starting from a specified point in the character array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer to read data into.</param>
|
||||
/// <param name="index">The starting point in the buffer at which to begin reading into the buffer.</param>
|
||||
/// <param name="count">The number of characters to read.</param>
|
||||
/// <returns>The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached.</returns>
|
||||
public static int ReadReverse(this BinaryReader reader, char[] buffer, int index, int count)
|
||||
|
||||
{
|
||||
int retval = reader.Read(buffer, index, count);
|
||||
buffer = buffer.Reverse().ToArray();
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.</param>
|
||||
/// <returns>A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.</returns>
|
||||
public static byte[] ReadBytesReverse(this BinaryReader reader, int count)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(count);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes.
|
||||
/// </summary>
|
||||
/// <returns>A decimal value read from the current stream.</returns>
|
||||
public static decimal ReadDecimalReverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(16);
|
||||
retval = retval.Reverse().ToArray();
|
||||
|
||||
int i1 = BitConverter.ToInt32(retval, 0);
|
||||
int i2 = BitConverter.ToInt32(retval, 4);
|
||||
int i3 = BitConverter.ToInt32(retval, 8);
|
||||
int i4 = BitConverter.ToInt32(retval, 12);
|
||||
|
||||
return new decimal(new int[] { i1, i2, i3, i4 });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// eads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
|
||||
/// </summary>
|
||||
/// <returns>An 8-byte floating point value read from the current stream.</returns>
|
||||
public static double ReadDoubleReverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(8);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToDouble(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
|
||||
/// </summary>
|
||||
/// <returns>A 2-byte signed integer read from the current stream.</returns>
|
||||
public static short ReadInt16Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(2);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToInt16(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
|
||||
/// </summary>
|
||||
/// <returns>A 4-byte signed integer read from the current stream.</returns>
|
||||
public static int ReadInt32Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(4);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToInt32(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
|
||||
/// </summary>
|
||||
/// <returns>An 8-byte signed integer read from the current stream.</returns>
|
||||
public static long ReadInt64Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(8);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToInt64(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
|
||||
/// </summary>
|
||||
/// <returns>A 4-byte floating point value read from the current stream.</returns>
|
||||
public static float ReadSingleReverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(4);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToSingle(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.
|
||||
///
|
||||
/// This API is not CLS-compliant.
|
||||
/// </summary>
|
||||
/// <returns>A 2-byte unsigned integer read from this stream.</returns>
|
||||
public static ushort ReadUInt16Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(2);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToUInt16(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.
|
||||
///
|
||||
/// This API is not CLS-compliant.
|
||||
/// </summary>
|
||||
/// <returns>A 4-byte unsigned integer read from this stream.</returns>
|
||||
public static uint ReadUInt32Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(4);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToUInt32(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.
|
||||
///
|
||||
/// This API is not CLS-compliant.
|
||||
/// </summary>
|
||||
/// <returns>An 8-byte unsigned integer read from this stream.</returns>
|
||||
public static ulong ReadUInt64Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(8);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToUInt64(retval, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Factories
|
||||
|
||||
/// <summary>
|
||||
@@ -1017,7 +1173,7 @@ namespace SabreTools.Library.Tools
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to open a file for write, optionally throwing the error
|
||||
/// Try to open an existing file for write, optionally throwing the error
|
||||
/// </summary>
|
||||
/// <param name="file">Name of the file to open</param>
|
||||
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
||||
|
||||
@@ -7,14 +7,12 @@ using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
|
||||
using SabreTools.Library.Data;
|
||||
using SabreTools.Library.DatFiles;
|
||||
|
||||
#if MONO
|
||||
using System.IO;
|
||||
#else
|
||||
using Alphaleonis.Win32.Filesystem;
|
||||
|
||||
using BinaryReader = System.IO.BinaryReader;
|
||||
using FileStream = System.IO.FileStream;
|
||||
#endif
|
||||
|
||||
@@ -126,434 +124,6 @@ namespace SabreTools.Library.Tools
|
||||
return hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a proper outfile name based on a DAT and output directory
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output directory</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>
|
||||
public static Dictionary<DatFormat, string> CreateOutfileNames(string outDir, DatFile datdata, bool overwrite = true)
|
||||
{
|
||||
// Create the output dictionary
|
||||
Dictionary<DatFormat, string> outfileNames = new Dictionary<DatFormat, string>();
|
||||
|
||||
// Double check the outDir for the end delim
|
||||
if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
outDir += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
// Get the extensions from the output type
|
||||
|
||||
// AttractMode
|
||||
if ((datdata.DatFormat & DatFormat.AttractMode) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.AttractMode, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite));
|
||||
}
|
||||
|
||||
// ClrMamePro
|
||||
if ((datdata.DatFormat & DatFormat.ClrMamePro) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.ClrMamePro, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite));
|
||||
};
|
||||
|
||||
// CSV
|
||||
if ((datdata.DatFormat & DatFormat.CSV) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.CSV, CreateOutfileNamesHelper(outDir, ".csv", datdata, overwrite));
|
||||
};
|
||||
|
||||
// DOSCenter
|
||||
if ((datdata.DatFormat & DatFormat.DOSCenter) != 0
|
||||
&& (datdata.DatFormat & DatFormat.ClrMamePro) == 0
|
||||
&& (datdata.DatFormat & DatFormat.RomCenter) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite));
|
||||
};
|
||||
if ((datdata.DatFormat & DatFormat.DOSCenter) != 0
|
||||
&& ((datdata.DatFormat & DatFormat.ClrMamePro) != 0
|
||||
|| (datdata.DatFormat & DatFormat.RomCenter) != 0))
|
||||
{
|
||||
outfileNames.Add(DatFormat.DOSCenter, CreateOutfileNamesHelper(outDir, ".dc.dat", datdata, overwrite));
|
||||
}
|
||||
|
||||
//MAME Listroms
|
||||
if ((datdata.DatFormat & DatFormat.Listroms) != 0
|
||||
&& (datdata.DatFormat & DatFormat.AttractMode) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite));
|
||||
}
|
||||
if ((datdata.DatFormat & DatFormat.Listroms) != 0
|
||||
&& (datdata.DatFormat & DatFormat.AttractMode) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.Listroms, CreateOutfileNamesHelper(outDir, ".lr.txt", datdata, overwrite));
|
||||
}
|
||||
|
||||
// Logiqx XML
|
||||
if ((datdata.DatFormat & DatFormat.Logiqx) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.Logiqx, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite));
|
||||
}
|
||||
|
||||
// Missfile
|
||||
if ((datdata.DatFormat & DatFormat.MissFile) != 0
|
||||
&& (datdata.DatFormat & DatFormat.AttractMode) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".txt", datdata, overwrite));
|
||||
}
|
||||
if ((datdata.DatFormat & DatFormat.MissFile) != 0
|
||||
&& (datdata.DatFormat & DatFormat.AttractMode) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.MissFile, CreateOutfileNamesHelper(outDir, ".miss.txt", datdata, overwrite));
|
||||
}
|
||||
|
||||
// OfflineList
|
||||
if (((datdata.DatFormat & DatFormat.OfflineList) != 0)
|
||||
&& (datdata.DatFormat & DatFormat.Logiqx) == 0
|
||||
&& (datdata.DatFormat & DatFormat.SabreDat) == 0
|
||||
&& (datdata.DatFormat & DatFormat.SoftwareList) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite));
|
||||
}
|
||||
if (((datdata.DatFormat & DatFormat.OfflineList) != 0
|
||||
&& ((datdata.DatFormat & DatFormat.Logiqx) != 0
|
||||
|| (datdata.DatFormat & DatFormat.SabreDat) != 0
|
||||
|| (datdata.DatFormat & DatFormat.SoftwareList) != 0)))
|
||||
{
|
||||
outfileNames.Add(DatFormat.OfflineList, CreateOutfileNamesHelper(outDir, ".ol.xml", datdata, overwrite));
|
||||
}
|
||||
|
||||
// Redump MD5
|
||||
if ((datdata.DatFormat & DatFormat.RedumpMD5) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpMD5, CreateOutfileNamesHelper(outDir, ".md5", datdata, overwrite));
|
||||
};
|
||||
|
||||
// Redump SFV
|
||||
if ((datdata.DatFormat & DatFormat.RedumpSFV) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpSFV, CreateOutfileNamesHelper(outDir, ".sfv", datdata, overwrite));
|
||||
};
|
||||
|
||||
// Redump SHA-1
|
||||
if ((datdata.DatFormat & DatFormat.RedumpSHA1) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpSHA1, CreateOutfileNamesHelper(outDir, ".sha1", datdata, overwrite));
|
||||
};
|
||||
|
||||
// Redump SHA-256
|
||||
if ((datdata.DatFormat & DatFormat.RedumpSHA256) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RedumpSHA256, CreateOutfileNamesHelper(outDir, ".sha256", datdata, overwrite));
|
||||
};
|
||||
|
||||
// RomCenter
|
||||
if ((datdata.DatFormat & DatFormat.RomCenter) != 0
|
||||
&& (datdata.DatFormat & DatFormat.ClrMamePro) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".dat", datdata, overwrite));
|
||||
};
|
||||
if ((datdata.DatFormat & DatFormat.RomCenter) != 0
|
||||
&& (datdata.DatFormat & DatFormat.ClrMamePro) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.RomCenter, CreateOutfileNamesHelper(outDir, ".rc.dat", datdata, overwrite));
|
||||
};
|
||||
|
||||
// SabreDAT
|
||||
if ((datdata.DatFormat & DatFormat.SabreDat) != 0 && (datdata.DatFormat & DatFormat.Logiqx) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite));
|
||||
};
|
||||
if ((datdata.DatFormat & DatFormat.SabreDat) != 0 && (datdata.DatFormat & DatFormat.Logiqx) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.SabreDat, CreateOutfileNamesHelper(outDir, ".sd.xml", datdata, overwrite));
|
||||
};
|
||||
|
||||
// Software List
|
||||
if ((datdata.DatFormat & DatFormat.SoftwareList) != 0
|
||||
&& (datdata.DatFormat & DatFormat.Logiqx) == 0
|
||||
&& (datdata.DatFormat & DatFormat.SabreDat) == 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".xml", datdata, overwrite));
|
||||
}
|
||||
if ((datdata.DatFormat & DatFormat.SoftwareList) != 0
|
||||
&& ((datdata.DatFormat & DatFormat.Logiqx) != 0
|
||||
|| (datdata.DatFormat & DatFormat.SabreDat) != 0))
|
||||
{
|
||||
outfileNames.Add(DatFormat.SoftwareList, CreateOutfileNamesHelper(outDir, ".sl.xml", datdata, overwrite));
|
||||
}
|
||||
|
||||
// TSV
|
||||
if ((datdata.DatFormat & DatFormat.TSV) != 0)
|
||||
{
|
||||
outfileNames.Add(DatFormat.TSV, CreateOutfileNamesHelper(outDir, ".tsv", datdata, overwrite));
|
||||
};
|
||||
|
||||
return outfileNames;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Help generating the outfile name
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output directory</param>
|
||||
/// <param name="extension">Extension to use for the file</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>
|
||||
private static string CreateOutfileNamesHelper(string outDir, string extension, DatFile datdata, bool overwrite)
|
||||
{
|
||||
string filename = (String.IsNullOrEmpty(datdata.FileName) ? datdata.Description : datdata.FileName);
|
||||
string outfile = outDir + filename + extension;
|
||||
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
|
||||
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the proper extension for the stat output format
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output path to use</param>
|
||||
/// <param name="statDatFormat">StatDatFormat to get the extension for</param>
|
||||
/// <param name="reportName">Name of the input file to use</param>
|
||||
/// <returns>Dictionary of output formats mapped to file names</returns>
|
||||
public static Dictionary<StatReportFormat, string> CreateOutStatsNames(string outDir, StatReportFormat statDatFormat, string reportName, bool overwrite = true)
|
||||
{
|
||||
Dictionary<StatReportFormat, string> output = new Dictionary<StatReportFormat, string>();
|
||||
|
||||
// First try to create the output directory if we need to
|
||||
if (!Directory.Exists(outDir))
|
||||
{
|
||||
Directory.CreateDirectory(outDir);
|
||||
}
|
||||
|
||||
// Double check the outDir for the end delim
|
||||
if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
outDir += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
// For each output format, get the appropriate stream writer
|
||||
if ((statDatFormat & StatReportFormat.None) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.None, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite));
|
||||
}
|
||||
if ((statDatFormat & StatReportFormat.CSV) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite));
|
||||
}
|
||||
if ((statDatFormat & StatReportFormat.HTML) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite));
|
||||
}
|
||||
if ((statDatFormat & StatReportFormat.TSV) != 0)
|
||||
{
|
||||
output.Add(StatReportFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Help generating the outstats name
|
||||
/// </summary>
|
||||
/// <param name="outDir">Output directory</param>
|
||||
/// <param name="extension">Extension to use for the file</param>
|
||||
/// <param name="reportName">Name of the input file to use</param>
|
||||
/// <param name="overwrite">True if we ignore existing files, false otherwise</param>
|
||||
/// <returns>String containing the new filename</returns>
|
||||
private static string CreateOutStatsNamesHelper(string outDir, string extension, string reportName, bool overwrite)
|
||||
{
|
||||
string outfile = outDir + reportName + extension;
|
||||
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
|
||||
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
|
||||
outfile);
|
||||
if (!overwrite)
|
||||
{
|
||||
int i = 1;
|
||||
while (File.Exists(outfile))
|
||||
{
|
||||
outfile = outDir + reportName + "_" + 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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Extensions
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of bytes from the stream, starting from a specified point in the byte array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer to read data into.</param>
|
||||
/// <param name="index">The starting point in the buffer at which to begin reading into the buffer.</param>
|
||||
/// <param name="count">The number of bytes to read.</param>
|
||||
/// <returns>The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached.</returns>
|
||||
public static int ReadReverse(this BinaryReader reader, byte[] buffer, int index, int count)
|
||||
{
|
||||
int retval = reader.Read(buffer, index, count);
|
||||
buffer = buffer.Reverse().ToArray();
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of characters from the stream, starting from a specified point in the character array.
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer to read data into.</param>
|
||||
/// <param name="index">The starting point in the buffer at which to begin reading into the buffer.</param>
|
||||
/// <param name="count">The number of characters to read.</param>
|
||||
/// <returns>The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached.</returns>
|
||||
public static int ReadReverse(this BinaryReader reader, char[] buffer, int index, int count)
|
||||
|
||||
{
|
||||
int retval = reader.Read(buffer, index, count);
|
||||
buffer = buffer.Reverse().ToArray();
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.</param>
|
||||
/// <returns>A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.</returns>
|
||||
public static byte[] ReadBytesReverse(this BinaryReader reader, int count)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(count);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes.
|
||||
/// </summary>
|
||||
/// <returns>A decimal value read from the current stream.</returns>
|
||||
public static decimal ReadDecimalReverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(16);
|
||||
retval = retval.Reverse().ToArray();
|
||||
|
||||
int i1 = BitConverter.ToInt32(retval, 0);
|
||||
int i2 = BitConverter.ToInt32(retval, 4);
|
||||
int i3 = BitConverter.ToInt32(retval, 8);
|
||||
int i4 = BitConverter.ToInt32(retval, 12);
|
||||
|
||||
return new decimal(new int[] { i1, i2, i3, i4 });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// eads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
|
||||
/// </summary>
|
||||
/// <returns>An 8-byte floating point value read from the current stream.</returns>
|
||||
public static double ReadDoubleReverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(8);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToDouble(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
|
||||
/// </summary>
|
||||
/// <returns>A 2-byte signed integer read from the current stream.</returns>
|
||||
public static short ReadInt16Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(2);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToInt16(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
|
||||
/// </summary>
|
||||
/// <returns>A 4-byte signed integer read from the current stream.</returns>
|
||||
public static int ReadInt32Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(4);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToInt32(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
|
||||
/// </summary>
|
||||
/// <returns>An 8-byte signed integer read from the current stream.</returns>
|
||||
public static long ReadInt64Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(8);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToInt64(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
|
||||
/// </summary>
|
||||
/// <returns>A 4-byte floating point value read from the current stream.</returns>
|
||||
public static float ReadSingleReverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(4);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToSingle(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.
|
||||
///
|
||||
/// This API is not CLS-compliant.
|
||||
/// </summary>
|
||||
/// <returns>A 2-byte unsigned integer read from this stream.</returns>
|
||||
public static ushort ReadUInt16Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(2);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToUInt16(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.
|
||||
///
|
||||
/// This API is not CLS-compliant.
|
||||
/// </summary>
|
||||
/// <returns>A 4-byte unsigned integer read from this stream.</returns>
|
||||
public static uint ReadUInt32Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(4);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToUInt32(retval, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.
|
||||
///
|
||||
/// This API is not CLS-compliant.
|
||||
/// </summary>
|
||||
/// <returns>An 8-byte unsigned integer read from this stream.</returns>
|
||||
public static ulong ReadUInt64Reverse(this BinaryReader reader)
|
||||
{
|
||||
byte[] retval = reader.ReadBytes(8);
|
||||
retval = retval.Reverse().ToArray();
|
||||
return BitConverter.ToUInt64(retval, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region String Manipulation
|
||||
|
||||
Reference in New Issue
Block a user