Files
SabreTools/SabreTools.DatTools/Verification.cs

159 lines
5.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Core;
2020-12-10 22:16:53 -08:00
using SabreTools.Core.Tools;
2020-12-10 23:24:09 -08:00
using SabreTools.DatFiles;
using SabreTools.DatItems;
2021-02-02 10:23:43 -08:00
using SabreTools.DatItems.Formats;
using SabreTools.FileTypes;
2020-12-10 22:31:23 -08:00
using SabreTools.FileTypes.Archives;
using SabreTools.Logging;
2020-12-10 23:24:09 -08:00
namespace SabreTools.DatTools
{
/// <summary>
/// Helper methods for verifying data from DatFiles
/// </summary>
2020-12-10 13:30:08 -08:00
public class Verification
{
2020-12-10 13:30:08 -08:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
private static readonly Logger logger = new Logger();
#endregion
/// <summary>
/// Verify a DatFile against a set of depots, leaving only missing files
/// </summary>
2020-12-10 11:11:59 -08:00
/// <param name="datFile">Current DatFile object to verify against</param>
/// <param name="inputs">List of input directories to compare against</param>
/// <returns>True if verification was a success, false otherwise</returns>
2020-12-10 11:58:46 -08:00
public static bool VerifyDepot(DatFile datFile, List<string> inputs)
{
bool success = true;
InternalStopwatch watch = new InternalStopwatch("Verifying all from supplied depots");
// Now loop through and get only directories from the input paths
List<string> directories = new List<string>();
foreach (string input in inputs)
{
// Add to the list if the input is a directory
if (Directory.Exists(input))
{
logger.Verbose($"Adding depot: {input}");
directories.Add(input);
}
}
// If we don't have any directories, we want to exit
if (directories.Count == 0)
return success;
// Now that we have a list of depots, we want to bucket the input DAT by SHA-1
2020-12-14 15:43:01 -08:00
datFile.Items.BucketBy(ItemKey.SHA1, DedupeType.None);
// Then we want to loop through each of the hashes and see if we can rebuild
2020-12-10 11:11:59 -08:00
var keys = datFile.Items.SortedKeys.ToList();
foreach (string hash in keys)
{
// Pre-empt any issues that could arise from string length
if (hash.Length != Constants.SHA1Length)
continue;
logger.User($"Checking hash '{hash}'");
// Get the extension path for the hash
2020-12-10 22:16:53 -08:00
string subpath = Utilities.GetDepotPath(hash, datFile.Header.InputDepot.Depth);
// Find the first depot that includes the hash
string foundpath = null;
foreach (string directory in directories)
{
2023-04-07 16:13:15 -04:00
if (System.IO.File.Exists(Path.Combine(directory, subpath)))
{
foundpath = Path.Combine(directory, subpath);
break;
}
}
// If we didn't find a path, then we continue
if (foundpath == null)
continue;
// If we have a path, we want to try to get the rom information
GZipArchive tgz = new GZipArchive(foundpath);
BaseFile fileinfo = tgz.GetTorrentGZFileInfo();
// If the file information is null, then we continue
if (fileinfo == null)
continue;
// Now we want to remove all duplicates from the DAT
2020-12-10 11:11:59 -08:00
datFile.Items.GetDuplicates(new Rom(fileinfo))
.AddRange(datFile.Items.GetDuplicates(new Disk(fileinfo)));
}
watch.Stop();
// Set fixdat headers in case of writing out
2020-12-10 11:11:59 -08:00
datFile.Header.FileName = $"fixDAT_{datFile.Header.FileName}";
datFile.Header.Name = $"fixDAT_{datFile.Header.Name}";
datFile.Header.Description = $"fixDAT_{datFile.Header.Description}";
datFile.Items.ClearMarked();
return success;
}
/// <summary>
/// Verify a DatFile against a set of inputs, leaving only missing files
/// </summary>
2020-12-10 11:11:59 -08:00
/// <param name="datFile">Current DatFile object to verify against</param>
/// <param name="hashOnly">True if only hashes should be checked, false for full file information</param>
/// <returns>True if verification was a success, false otherwise</returns>
2020-12-10 11:58:46 -08:00
public static bool VerifyGeneric(DatFile datFile, bool hashOnly)
{
bool success = true;
2021-02-02 14:09:49 -08:00
InternalStopwatch watch = new InternalStopwatch("Verifying all from supplied paths");
// Force bucketing according to the flags
2020-12-14 15:43:01 -08:00
datFile.Items.SetBucketedBy(ItemKey.NULL);
if (hashOnly)
2020-12-14 15:43:01 -08:00
datFile.Items.BucketBy(ItemKey.CRC, DedupeType.Full);
else
2020-12-14 15:43:01 -08:00
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.Full);
// Then mark items for removal
2020-12-10 11:11:59 -08:00
var keys = datFile.Items.SortedKeys.ToList();
foreach (string key in keys)
{
ConcurrentList<DatItem> items = datFile.Items[key];
for (int i = 0; i < items.Count; i++)
{
// Unmatched items will have a source ID of int.MaxValue, remove all others
if (items[i].Source.Index != int.MaxValue)
items[i].Remove = true;
}
// Set the list back, just in case
2020-12-10 11:11:59 -08:00
datFile.Items[key] = items;
}
2021-02-02 14:09:49 -08:00
watch.Stop();
// Set fixdat headers in case of writing out
2020-12-10 11:11:59 -08:00
datFile.Header.FileName = $"fixDAT_{datFile.Header.FileName}";
datFile.Header.Name = $"fixDAT_{datFile.Header.Name}";
datFile.Header.Description = $"fixDAT_{datFile.Header.Description}";
datFile.Items.ClearMarked();
return success;
}
}
}