diff --git a/RombaSharp/Partials/RombaSharp_Helpers.cs b/RombaSharp/Partials/RombaSharp_Helpers.cs index ae9a999e..f38ae571 100644 --- a/RombaSharp/Partials/RombaSharp_Helpers.cs +++ b/RombaSharp/Partials/RombaSharp_Helpers.cs @@ -255,7 +255,7 @@ namespace SabreTools // Total number of CRCs query = "SELECT COUNT(crc) FROM data WHERE NOT crc=\"null\""; slc = new SqliteCommand(query, dbc); - _logger.User("Total CRCs: " + (long)slc.ExecuteScalar()); + _logger.User("Total CRCs: " + (long)slc.ExecuteScalar()); // Total number of MD5s query = "SELECT COUNT(md5) FROM data WHERE NOT md5=\"null\""; @@ -268,7 +268,7 @@ namespace SabreTools _logger.User("Total SHA1s: " + (long)slc.ExecuteScalar()); // Total number of DATs - query = "SELECT COUNT(*) FROM dats"; + query = "SELECT COUNT(*) FROM dats GROUP BY hash"; slc = new SqliteCommand(query, dbc); _logger.User("Total DATs: " + (long)slc.ExecuteScalar()); @@ -498,13 +498,14 @@ namespace SabreTools private static Dictionary GetValidDats(List inputs) { // Get a dictionary of filenames that actually exist in the DATRoot, logging which ones are not - List datRootDats = Directory.EnumerateFiles(_dats, "*", SearchOption.AllDirectories).ToList().ConvertAll(i => i.ToLowerInvariant()); + List datRootDats = Directory.EnumerateFiles(_dats, "*", SearchOption.AllDirectories).ToList(); + List lowerCaseDats = datRootDats.ConvertAll(i => Path.GetFileName(i).ToLowerInvariant()); Dictionary foundDats = new Dictionary(); foreach (string input in inputs) { - if (datRootDats.Contains(input.ToLowerInvariant())) + if (lowerCaseDats.Contains(input.ToLowerInvariant())) { - string fullpath = Path.GetFullPath(datRootDats[datRootDats.IndexOf(input.ToLowerInvariant())]); + string fullpath = Path.GetFullPath(datRootDats[lowerCaseDats.IndexOf(input.ToLowerInvariant())]); string sha1 = FileTools.GetFileInfo(fullpath, _logger).SHA1; foundDats.Add(sha1, fullpath); } diff --git a/RombaSharp/Partials/RombaSharp_Inits.cs b/RombaSharp/Partials/RombaSharp_Inits.cs index 48bdb697..c494e9fa 100644 --- a/RombaSharp/Partials/RombaSharp_Inits.cs +++ b/RombaSharp/Partials/RombaSharp_Inits.cs @@ -47,10 +47,10 @@ namespace SabreTools List datItems = df.Files[key]; foreach (Rom rom in datItems) { - string query = "SELECT id FROM data WHERE size=" + rom.Size + " AND (" - + "(crc=\"" + rom.CRC + "\" OR value=\"null\")" - + " AND (md5=\"" + rom.MD5 + "\" OR value=\"null\")" - + " AND (sha1=\"" + rom.SHA1 + "\" OR value=\"null\")" + string query = "SELECT id FROM data WHERE size=" + rom.Size + + " AND (crc=\"" + rom.CRC + "\" OR crc=\"null\")" + + " AND (md5=\"" + rom.MD5 + "\" OR md5=\"null\")" + + " AND (sha1=\"" + rom.SHA1 + "\" OR sha1=\"null\")" + " AND indepot=0"; SqliteCommand slc = new SqliteCommand(query, dbc); SqliteDataReader sldr = slc.ExecuteReader(); @@ -119,13 +119,69 @@ namespace SabreTools /// private static void InitBuild(List inputs) { - _logger.User("This feature is not yet implemented: build"); - // Verify the filenames Dictionary foundDats = GetValidDats(inputs); - // Now that we have the dictionary, we can loop through and output to a new folder for each + // Create a base output folder + if (!Directory.Exists("out")) + { + Directory.CreateDirectory("out"); + } + // Open the database + SqliteConnection dbc = new SqliteConnection(_connectionString); + dbc.Open(); + + // Now that we have the dictionary, we can loop through and output to a new folder for each + foreach (string key in foundDats.Keys) + { + // Get the DAT file associated with the key + DatFile datFile = new DatFile(); + datFile.Parse(Path.Combine(_dats, foundDats[key]), 0, 0, _logger, softlist: true); + ArchiveScanLevel asl = ArchiveTools.GetArchiveScanLevelFromNumbers(0, 0, 0, 0); + + // Create the new output directory if it doesn't exist + string outputFolder = Path.Combine("out", Path.GetFileNameWithoutExtension(foundDats[key])); + if (!Directory.Exists(outputFolder)) + { + Directory.CreateDirectory(outputFolder); + } + + // Then get all hashes associated with this DAT + string query = "SELECT sha1 FROM dats JOIN data ON dats.id=data.id WHERE hash=\"" + key + "\""; + SqliteCommand slc = new SqliteCommand(query, dbc); + SqliteDataReader sldr = slc.ExecuteReader(); + if (sldr.HasRows) + { + while (sldr.Read()) + { + string sha1 = sldr.GetString(0); + string filename = Path.Combine(sha1.Substring(0, 2), sha1.Substring(2, 2), sha1.Substring(4, 2), sha1.Substring(6, 2), sha1 + ".gz"); + + // Find the first depot that contains the folder + foreach (string depot in _depots.Keys) + { + // If the depot is online, check it + if (_depots[depot].Item2) + { + if (File.Exists(Path.Combine(depot, filename))) + { + ArchiveTools.ExtractArchive(Path.Combine(depot, filename), _tmpdir, asl, _logger); + continue; + } + } + } + } + } + + // Now that we have extracted everything, we rebuild to the output folder + List temp = new List(); + temp.Add(_tmpdir); + SimpleSort ss = new SimpleSort(datFile, temp, outputFolder, "", false, false, false, true, false, false, asl, false, _logger); + ss.StartProcessing(); + } + + dbc.Dispose(); } /// diff --git a/RombaSharp/RombaSharp.cs b/RombaSharp/RombaSharp.cs index 4e70e3c6..777bebb7 100644 --- a/RombaSharp/RombaSharp.cs +++ b/RombaSharp/RombaSharp.cs @@ -155,19 +155,9 @@ namespace SabreTools { outdat = temparg.Split('=')[1]; } - else if (File.Exists(temparg) || Directory.Exists(temparg)) - { - inputs.Add(temparg); - } else { - _logger.Error("Invalid input detected: " + arg); - Console.WriteLine(); - Build.Help(); - Console.WriteLine(); - _logger.Error("Invalid input detected: " + arg); - _logger.Close(); - return; + inputs.Add(temparg); } break; } diff --git a/SabreTools.Helper/Objects/Dat/DatFile.cs b/SabreTools.Helper/Objects/Dat/DatFile.cs index 614adc49..d00c7136 100644 --- a/SabreTools.Helper/Objects/Dat/DatFile.cs +++ b/SabreTools.Helper/Objects/Dat/DatFile.cs @@ -5381,7 +5381,16 @@ namespace SabreTools.Helper // Add the file information to the DAT lock (Files) { - Files[key].Add(datItem); + if (Files.ContainsKey(key)) + { + Files[key].Add(datItem); + } + else + { + List temp = new List(); + temp.Add(datItem); + Files.Add(key, temp); + } } logger.User("File added: " + romname + Environment.NewLine); diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index 031bb1fe..81bf4901 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -108,7 +108,7 @@ namespace SabreTools.Helper /// Process the DAT and verify the output directory /// /// True if verification was a success, false otherwise - public bool VerifyDirectory() + private bool VerifyDirectory() { bool success = true; @@ -194,7 +194,7 @@ namespace SabreTools.Helper /// /// This is actually rather slow and inefficient. See below for more correct implemenation /// - public bool RebuildToOutput() + private bool RebuildToOutput() { bool success = true; @@ -232,6 +232,14 @@ namespace SabreTools.Helper { FileTools.CleanDirectory(_tempDir); } + if (success && _delete) + { + try + { + File.Delete(files[i]); + } + catch { } + } } // Now one final delete of the temp directory @@ -615,7 +623,7 @@ namespace SabreTools.Helper /// 4) Order by output game /// 5) Rebuild all files /// - public bool RebuiltToOutputAlternate() + private bool RebuiltToOutputAlternate() { bool success = true; @@ -781,7 +789,7 @@ namespace SabreTools.Helper /// Reference to the Dat to add to /// Logger object for file and console output /// True if the file could be added, false otherwise - public bool RebuildToOutputAlternateParseRomHelper(string file, ref DatFile matchdat, Logger logger) + private bool RebuildToOutputAlternateParseRomHelper(string file, ref DatFile matchdat, Logger logger) { Rom rom = FileTools.GetFileInfo(file, logger); @@ -851,7 +859,7 @@ namespace SabreTools.Helper /// /// True if the cleaning succeeded, false otherwise /// This method is incomplete, it need to be finished before it can be used - public bool InplaceRebuild() + private bool InplaceRebuild() { bool success = true;