Move verifying to new class

This commit is contained in:
Matt Nadareski
2020-12-10 11:11:59 -08:00
parent 6a2e35d552
commit 6aa88afa9a
2 changed files with 29 additions and 26 deletions

View File

@@ -11,14 +11,16 @@ using SabreTools.Logging;
// This file represents all methods related to verifying with a DatFile // This file represents all methods related to verifying with a DatFile
namespace SabreTools.DatFiles namespace SabreTools.DatFiles
{ {
public abstract partial class DatFile // TODO: Re-evaluate if these should be made static instead of instanced
public partial class DatTool
{ {
/// <summary> /// <summary>
/// Verify a DatFile against a set of depots, leaving only missing files /// Verify a DatFile against a set of depots, leaving only missing files
/// </summary> /// </summary>
/// <param name="datFile">Current DatFile object to verify against</param>
/// <param name="inputs">List of input directories to compare against</param> /// <param name="inputs">List of input directories to compare against</param>
/// <returns>True if verification was a success, false otherwise</returns> /// <returns>True if verification was a success, false otherwise</returns>
public bool VerifyDepot(List<string> inputs) public bool VerifyDepot(DatFile datFile, List<string> inputs)
{ {
bool success = true; bool success = true;
@@ -41,10 +43,10 @@ namespace SabreTools.DatFiles
return success; return success;
// Now that we have a list of depots, we want to bucket the input DAT by SHA-1 // Now that we have a list of depots, we want to bucket the input DAT by SHA-1
Items.BucketBy(Field.DatItem_SHA1, DedupeType.None); datFile.Items.BucketBy(Field.DatItem_SHA1, DedupeType.None);
// Then we want to loop through each of the hashes and see if we can rebuild // Then we want to loop through each of the hashes and see if we can rebuild
var keys = Items.SortedKeys.ToList(); var keys = datFile.Items.SortedKeys.ToList();
foreach (string hash in keys) foreach (string hash in keys)
{ {
// Pre-empt any issues that could arise from string length // Pre-empt any issues that could arise from string length
@@ -54,7 +56,7 @@ namespace SabreTools.DatFiles
logger.User($"Checking hash '{hash}'"); logger.User($"Checking hash '{hash}'");
// Get the extension path for the hash // Get the extension path for the hash
string subpath = PathExtensions.GetDepotPath(hash, Header.InputDepot.Depth); string subpath = PathExtensions.GetDepotPath(hash, datFile.Header.InputDepot.Depth);
// Find the first depot that includes the hash // Find the first depot that includes the hash
string foundpath = null; string foundpath = null;
@@ -80,17 +82,17 @@ namespace SabreTools.DatFiles
continue; continue;
// Now we want to remove all duplicates from the DAT // Now we want to remove all duplicates from the DAT
Items.GetDuplicates(new Rom(fileinfo)) datFile.Items.GetDuplicates(new Rom(fileinfo))
.AddRange(Items.GetDuplicates(new Disk(fileinfo))); .AddRange(datFile.Items.GetDuplicates(new Disk(fileinfo)));
} }
watch.Stop(); watch.Stop();
// Set fixdat headers in case of writing out // Set fixdat headers in case of writing out
Header.FileName = $"fixDAT_{Header.FileName}"; datFile.Header.FileName = $"fixDAT_{datFile.Header.FileName}";
Header.Name = $"fixDAT_{Header.Name}"; datFile.Header.Name = $"fixDAT_{datFile.Header.Name}";
Header.Description = $"fixDAT_{Header.Description}"; datFile.Header.Description = $"fixDAT_{datFile.Header.Description}";
Items.ClearMarked(); datFile.Items.ClearMarked();
return success; return success;
} }
@@ -98,24 +100,25 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Verify a DatFile against a set of inputs, leaving only missing files /// Verify a DatFile against a set of inputs, leaving only missing files
/// </summary> /// </summary>
/// <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> /// <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> /// <returns>True if verification was a success, false otherwise</returns>
public bool VerifyGeneric(bool hashOnly) public bool VerifyGeneric(DatFile datFile, bool hashOnly)
{ {
bool success = true; bool success = true;
// Force bucketing according to the flags // Force bucketing according to the flags
Items.SetBucketedBy(Field.NULL); datFile.Items.SetBucketedBy(Field.NULL);
if (hashOnly) if (hashOnly)
Items.BucketBy(Field.DatItem_CRC, DedupeType.Full); datFile.Items.BucketBy(Field.DatItem_CRC, DedupeType.Full);
else else
Items.BucketBy(Field.Machine_Name, DedupeType.Full); datFile.Items.BucketBy(Field.Machine_Name, DedupeType.Full);
// Then mark items for removal // Then mark items for removal
var keys = Items.SortedKeys.ToList(); var keys = datFile.Items.SortedKeys.ToList();
foreach (string key in keys) foreach (string key in keys)
{ {
List<DatItem> items = Items[key]; List<DatItem> items = datFile.Items[key];
for (int i = 0; i < items.Count; i++) for (int i = 0; i < items.Count; i++)
{ {
// Unmatched items will have a source ID of int.MaxValue, remove all others // Unmatched items will have a source ID of int.MaxValue, remove all others
@@ -124,14 +127,14 @@ namespace SabreTools.DatFiles
} }
// Set the list back, just in case // Set the list back, just in case
Items[key] = items; datFile.Items[key] = items;
} }
// Set fixdat headers in case of writing out // Set fixdat headers in case of writing out
Header.FileName = $"fixDAT_{Header.FileName}"; datFile.Header.FileName = $"fixDAT_{datFile.Header.FileName}";
Header.Name = $"fixDAT_{Header.Name}"; datFile.Header.Name = $"fixDAT_{datFile.Header.Name}";
Header.Description = $"fixDAT_{Header.Description}"; datFile.Header.Description = $"fixDAT_{datFile.Header.Description}";
Items.ClearMarked(); datFile.Items.ClearMarked();
return success; return success;
} }

View File

@@ -79,7 +79,7 @@ namespace SabreTools.Features
// If we have the depot flag, respect it // If we have the depot flag, respect it
if (Header.InputDepot?.IsActive ?? false) if (Header.InputDepot?.IsActive ?? false)
{ {
datdata.VerifyDepot(Inputs); dt.VerifyDepot(datdata, Inputs);
} }
else else
{ {
@@ -90,7 +90,7 @@ namespace SabreTools.Features
dt.PopulateFromDir(datdata, input, asFiles: asFiles, hashes: quickScan ? Hash.CRC : Hash.Standard); dt.PopulateFromDir(datdata, input, asFiles: asFiles, hashes: quickScan ? Hash.CRC : Hash.Standard);
} }
datdata.VerifyGeneric(hashOnly); dt.VerifyGeneric(datdata, hashOnly);
} }
// Now write out if there are any items left // Now write out if there are any items left
@@ -128,7 +128,7 @@ namespace SabreTools.Features
// If we have the depot flag, respect it // If we have the depot flag, respect it
if (Header.InputDepot?.IsActive ?? false) if (Header.InputDepot?.IsActive ?? false)
{ {
datdata.VerifyDepot(Inputs); dt.VerifyDepot(datdata, Inputs);
} }
else else
{ {
@@ -139,7 +139,7 @@ namespace SabreTools.Features
dt.PopulateFromDir(datdata, input, asFiles: asFiles, hashes: quickScan ? Hash.CRC : Hash.Standard); dt.PopulateFromDir(datdata, input, asFiles: asFiles, hashes: quickScan ? Hash.CRC : Hash.Standard);
} }
datdata.VerifyGeneric(hashOnly); dt.VerifyGeneric(datdata, hashOnly);
} }
// Now write out if there are any items left // Now write out if there are any items left