[SimpleSort, DatTools, DATFromDir] Add verification

This is a rather complex change that allows a very simple fixdat to be created from an input DAT and an output folder. It seems to work as intended so here's hoping that it actually does. It needs more testing, to say the least.
This commit is contained in:
Matt Nadareski
2016-08-24 21:19:05 -07:00
parent 1339e6e121
commit 42148b7fc8
4 changed files with 255 additions and 105 deletions

View File

@@ -1418,7 +1418,7 @@ namespace SabreTools.Helper
foreach (Rom rom in roms) foreach (Rom rom in roms)
{ {
count++; count++;
string newkey = (norename ? "" : rom.Metadata.SystemID.ToString().PadLeft(10, '0') + "-" + rom.Metadata.SourceID.ToString().PadLeft(10, '0') + "-") + rom.Game.ToLowerInvariant(); string newkey = (norename ? "" : rom.Metadata.SystemID.ToString().PadLeft(10, '0') + "-" + rom.Metadata.SourceID.ToString().PadLeft(10, '0') + "-") + (String.IsNullOrEmpty(rom.Game) ? "" : rom.Game.ToLowerInvariant());
if (sortable.ContainsKey(newkey)) if (sortable.ContainsKey(newkey))
{ {
sortable[newkey].Add(rom); sortable[newkey].Add(rom);

View File

@@ -25,10 +25,17 @@ namespace SabreTools
private bool _bare; private bool _bare;
private bool _archivesAsFiles; private bool _archivesAsFiles;
private bool _enableGzip; private bool _enableGzip;
private bool _nowrite;
// Other required variables // Other required variables
private Logger _logger; private Logger _logger;
// Public variables
public Dat DatData
{
get { return _datdata; }
}
/// <summary> /// <summary>
/// Create a new DATFromDir object /// Create a new DATFromDir object
/// </summary> /// </summary>
@@ -38,10 +45,11 @@ namespace SabreTools
/// <param name="noSHA1">True if SHA-1 hashes should be skipped over, false otherwise</param> /// <param name="noSHA1">True if SHA-1 hashes should be skipped over, false otherwise</param>
/// <param name="bare">True if the date should be omitted from the DAT, false otherwise</param> /// <param name="bare">True if the date should be omitted from the DAT, false otherwise</param>
/// <param name="archivesAsFiles">True if archives should be treated as files, false otherwise</param> /// <param name="archivesAsFiles">True if archives should be treated as files, false otherwise</param>
/// <param name="enableGzip">True if GZIP archives should be treated as files, false otherwise</param>> /// <param name="enableGzip">True if GZIP archives should be treated as files, false otherwise</param>
/// <param name="logger">Logger object for console and file output</param>
/// <param name="tempDir">Name of the directory to create a temp folder in (blank is current directory)</param> /// <param name="tempDir">Name of the directory to create a temp folder in (blank is current directory)</param>
public DATFromDir(List<String> inputs, Dat datdata, bool noMD5, bool noSHA1, bool bare, bool archivesAsFiles, bool enableGzip, string tempDir, Logger logger) /// <param name="nowrite">True if the file should not be written out, false otherwise (default)</param>
/// <param name="logger">Logger object for console and file output</param>
public DATFromDir(List<String> inputs, Dat datdata, bool noMD5, bool noSHA1, bool bare, bool archivesAsFiles, bool enableGzip, string tempDir, Logger logger, bool nowrite = false)
{ {
_inputs = inputs; _inputs = inputs;
_datdata = datdata; _datdata = datdata;
@@ -52,6 +60,7 @@ namespace SabreTools
_enableGzip = enableGzip; _enableGzip = enableGzip;
_tempDir = tempDir; _tempDir = tempDir;
_logger = logger; _logger = logger;
_nowrite = nowrite;
} }
/// <summary> /// <summary>
@@ -98,10 +107,18 @@ namespace SabreTools
_datdata.Description = _datdata.Name + (_bare ? "" : " (" + _datdata.Date + ")"); _datdata.Description = _datdata.Name + (_bare ? "" : " (" + _datdata.Date + ")");
} }
StreamWriter sw;
if (_nowrite)
{
sw = new StreamWriter(new MemoryStream());
}
else
{
// Create and open the output file for writing // Create and open the output file for writing
FileStream fs = File.Create(Style.CreateOutfileName(Environment.CurrentDirectory, _datdata)); FileStream fs = File.Create(Style.CreateOutfileName(Environment.CurrentDirectory, _datdata));
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); sw = new StreamWriter(fs, Encoding.UTF8);
sw.AutoFlush = true; sw.AutoFlush = true;
}
// Write out the initial file header // Write out the initial file header
Output.WriteHeader(sw, _datdata, _logger); Output.WriteHeader(sw, _datdata, _logger);
@@ -227,8 +244,10 @@ namespace SabreTools
// Now output any empties to the stream (if not in Romba mode) // Now output any empties to the stream (if not in Romba mode)
if (!_datdata.Romba) if (!_datdata.Romba)
{ {
foreach (List<Rom> roms in _datdata.Roms.Values) List<string> keys = _datdata.Roms.Keys.ToList();
foreach (string key in keys)
{ {
List<Rom> roms = _datdata.Roms[key];
for (int i = 0; i < roms.Count; i++) for (int i = 0; i < roms.Count; i++)
{ {
Rom rom = roms[i]; Rom rom = roms[i];
@@ -244,6 +263,22 @@ namespace SabreTools
rom.SHA1 = Constants.SHA1Zero; rom.SHA1 = Constants.SHA1Zero;
} }
if (_nowrite)
{
string inkey = rom.Size + "-" + rom.CRC;
if (_datdata.Roms.ContainsKey(inkey))
{
_datdata.Roms[inkey].Add(rom);
}
else
{
List<Rom> temp = new List<Rom>();
temp.Add(rom);
_datdata.Roms.Add(inkey, temp);
}
}
else
{
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
int last = 0; int last = 0;
if (lastparent != null && lastparent.ToLowerInvariant() != rom.Game.ToLowerInvariant()) if (lastparent != null && lastparent.ToLowerInvariant() != rom.Game.ToLowerInvariant())
@@ -262,6 +297,7 @@ namespace SabreTools
{ {
Output.WriteRomData(sw, rom, lastparent, _datdata, 0, _logger); Output.WriteRomData(sw, rom, lastparent, _datdata, 0, _logger);
} }
}
lastparent = rom.Game; lastparent = rom.Game;
} }
@@ -304,10 +340,28 @@ namespace SabreTools
if (rom.Name != null) if (rom.Name != null)
{ {
int last = 0; int last = 0;
if (_nowrite)
{
string key = rom.Size + "-" + rom.CRC;
if (_datdata.Roms.ContainsKey(key))
{
_datdata.Roms[key].Add(rom);
}
else
{
List<Rom> temp = new List<Rom>();
temp.Add(rom);
_datdata.Roms.Add(key, temp);
}
}
else
{
Output.WriteStartGame(sw, rom, new List<string>(), "", _datdata, 0, 0, _logger); Output.WriteStartGame(sw, rom, new List<string>(), "", _datdata, 0, 0, _logger);
Output.WriteRomData(sw, rom, "", _datdata, 0, _logger); Output.WriteRomData(sw, rom, "", _datdata, 0, _logger);
Output.WriteEndGame(sw, rom, new List<string>(), new List<string>(), "", _datdata, 0, out last, _logger); Output.WriteEndGame(sw, rom, new List<string>(), new List<string>(), "", _datdata, 0, out last, _logger);
} }
}
else else
{ {
return string.Empty; return string.Empty;
@@ -357,10 +411,14 @@ namespace SabreTools
_logger.Log(Path.GetFileName(item) + " treated like an archive"); _logger.Log(Path.GetFileName(item) + " treated like an archive");
foreach (string entry in Directory.EnumerateFiles(tempdir, "*", SearchOption.AllDirectories)) foreach (string entry in Directory.EnumerateFiles(tempdir, "*", SearchOption.AllDirectories))
{ {
string tempbasepath = (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar);
lastparent = ProcessFile(Path.GetFullPath(entry), sw, Path.GetFullPath(tempdir), lastparent = ProcessFile(Path.GetFullPath(entry), sw, Path.GetFullPath(tempdir),
Path.Combine((Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, _basePath.Length) + (String.IsNullOrEmpty(tempbasepath)
Path.GetFileNameWithoutExtension(item) ? ""
), _datdata, lastparent); : (tempbasepath.Length < _basePath.Length
? tempbasepath
: tempbasepath.Remove(0, _basePath.Length))) +
Path.GetFileNameWithoutExtension(item), _datdata, lastparent);
} }
// Clear the temp directory // Clear the temp directory
@@ -455,6 +513,22 @@ namespace SabreTools
rom.Game = rom.Game.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()); rom.Game = rom.Game.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString());
rom.Name = actualitem; rom.Name = actualitem;
if (_nowrite)
{
string key = rom.Size + "-" + rom.CRC;
if (_datdata.Roms.ContainsKey(key))
{
_datdata.Roms[key].Add(rom);
}
else
{
List<Rom> temp = new List<Rom>();
temp.Add(rom);
_datdata.Roms.Add(key, temp);
}
}
else
{
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
int last = 0; int last = 0;
if (lastparent != null && lastparent.ToLowerInvariant() != rom.Game.ToLowerInvariant()) if (lastparent != null && lastparent.ToLowerInvariant() != rom.Game.ToLowerInvariant())
@@ -470,6 +544,7 @@ namespace SabreTools
// Write out the rom data // Write out the rom data
Output.WriteRomData(sw, rom, lastparent, datdata, 0, _logger); Output.WriteRomData(sw, rom, lastparent, datdata, 0, _logger);
}
_logger.User("File added: " + actualitem + Environment.NewLine); _logger.User("File added: " + actualitem + Environment.NewLine);
return rom.Game; return rom.Game;

View File

@@ -270,21 +270,19 @@ namespace SabreTools
Dat datdata = new Dat(); Dat datdata = new Dat();
foreach (string datfile in datfiles) foreach (string datfile in datfiles)
{ {
datdata = DatTools.Parse(datfile, 0, 0, datdata, logger); datdata = DatTools.Parse(datfile, 99, 99, datdata, logger);
} }
SimpleSort ss = new SimpleSort(datdata, inputs, outdir, tempdir, quickScan, toFolder, verify, sevenzip, gz, rar, zip, logger); SimpleSort ss = new SimpleSort(datdata, inputs, outdir, tempdir, quickScan, toFolder, verify, sevenzip, gz, rar, zip, logger);
ss.RebuildToOutput(); ss.StartProcessing();
} }
/// <summary> /// <summary>
/// Process the DAT and find all matches in input files and folders /// Pick the appropriate action based on the inputs
/// </summary> /// </summary>
/// <returns>True if rebuilding was a success, false otherwise</returns> /// <returns>True if success, false otherwise</returns>
public bool RebuildToOutput() public bool StartProcessing()
{ {
bool success = true;
// First, check that the output directory exists // First, check that the output directory exists
if (!Directory.Exists(_outdir)) if (!Directory.Exists(_outdir))
{ {
@@ -302,21 +300,95 @@ namespace SabreTools
Output.CleanDirectory(_tempdir); Output.CleanDirectory(_tempdir);
} }
// If we're in verification mode, only check the output directory
if (_verify) if (_verify)
{ {
// Create a list of files from the output directory return VerifyDirectory();
}
else
{
return RebuildToOutput();
}
}
/// <summary>
/// Process the DAT and verify the output directory
/// </summary>
/// <returns>True if verification was a success, false otherwise</returns>
public bool VerifyDirectory()
{
bool success = true;
// Enumerate all files from the output directory
List<string> files = new List<string>(); List<string> files = new List<string>();
foreach (string file in Directory.EnumerateFiles(_outdir, "*", SearchOption.AllDirectories)) foreach (string file in Directory.EnumerateFiles(_outdir, "*", SearchOption.AllDirectories))
{ {
_logger.Log("File found: '" + file + "'"); _logger.Log("File found: '" + file + "'");
files.Add(Path.GetFullPath(file)); files.Add(Path.GetFullPath(file));
} }
}
// Otherwise, run the rebuilder /*
We want the cross section of what's the folder and what's in the DAT. Right now, it just has what's in the DAT that's not in the folder
*/
// Then, loop through and check each of the inputs
_logger.User("Processing files:\n");
DATFromDir dfd = new DATFromDir(files, _datdata, false, false, false, false, true, "", _logger, true);
dfd.Start();
// Setup the fixdat
_matched = (Dat)_datdata.CloneHeader();
_matched.Roms = new Dictionary<string, List<Rom>>();
_matched.FileName = "fixDat_" + _matched.FileName;
_matched.Name = "fixDat_" + _matched.Name;
_matched.Description = "fixDat_" + _matched.Description;
_matched.OutputFormat = OutputFormat.Xml;
// Now that all files are parsed, get only files found in directory
bool found = false;
foreach (List<Rom> roms in _datdata.Roms.Values)
{
List<Rom> newroms = RomTools.Merge(roms, _logger);
foreach (Rom rom in newroms)
{
if (rom.Metadata.SourceID == 99)
{
found = true;
string key = rom.Size + "-" + rom.CRC;
if (_matched.Roms.ContainsKey(key))
{
_matched.Roms[key].Add(rom);
}
else else
{ {
List<Rom> temp = new List<Rom>();
temp.Add(rom);
_matched.Roms.Add(key, temp);
}
}
}
}
// Now output the fixdat to the main folder
if (found)
{
Output.WriteDatfile(_matched, "", _logger, stats: true);
}
else
{
_logger.User("No fixDat needed");
}
return success;
}
/// <summary>
/// Process the DAT and find all matches in input files and folders
/// </summary>
/// <returns>True if rebuilding was a success, false otherwise</returns>
public bool RebuildToOutput()
{
bool success = true;
// Create a list of just files from inputs // Create a list of just files from inputs
List<string> files = new List<string>(); List<string> files = new List<string>();
foreach (string input in _inputs) foreach (string input in _inputs)
@@ -368,13 +440,12 @@ namespace SabreTools
Console.SetCursorPosition(0, Constants.HeaderHeight + 1); Console.SetCursorPosition(0, Constants.HeaderHeight + 1);
_logger.User("Stats of the matched ROMs:"); _logger.User("Stats of the matched ROMs:");
Stats.OutputStats(_matched, _logger, true); Stats.OutputStats(_matched, _logger, true);
}
return success; return success;
} }
/// <summary> /// <summary>
/// Process an individual file against the DAT /// Process an individual file against the DAT for rebuilding
/// </summary> /// </summary>
/// <param name="input">Name of the input file</param> /// <param name="input">Name of the input file</param>
/// <param name="index">Index of the current file</param> /// <param name="index">Index of the current file</param>

View File

@@ -79,6 +79,10 @@
<Project>{225a1afd-0890-44e8-b779-7502665c23a5}</Project> <Project>{225a1afd-0890-44e8-b779-7502665c23a5}</Project>
<Name>SabreTools.Helper</Name> <Name>SabreTools.Helper</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\SabreTools\SabreTools.csproj">
<Project>{3b615702-1866-4d7b-8af1-7b43fd0cc1d0}</Project>
<Name>SabreTools</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.