diff --git a/SabreTools.DatTools/Rebuilder.cs b/SabreTools.DatTools/Rebuilder.cs index 03e4cb72..d71ba29e 100644 --- a/SabreTools.DatTools/Rebuilder.cs +++ b/SabreTools.DatTools/Rebuilder.cs @@ -413,6 +413,7 @@ namespace SabreTools.DatTools // If either we have duplicates or we're filtering if (ShouldRebuild(datFile, datItem, fileStream, inverse, out ConcurrentList dupes)) + //if (ShouldRebuildDB(datFile, datItem, fileStream, inverse, out ConcurrentList dupes)) { // If we have a very specific TGZ->TGZ case, just copy it accordingly if (RebuildTorrentGzip(datFile, datItem, file, outDir, outputFormat, isZip)) @@ -477,6 +478,7 @@ namespace SabreTools.DatTools // If we have duplicates and we're not filtering if (ShouldRebuild(datFile, headerless, transformStream, false, out dupes)) + //if (ShouldRebuildDB(datFile, headerless, transformStream, false, out dupes)) { logger.User($"Headerless matches found for '{Path.GetFileName(datItem.GetName() ?? datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue().AsStringValue())}', rebuilding accordingly..."); rebuilt = true; @@ -568,6 +570,67 @@ namespace SabreTools.DatTools } } + /// + /// Get the rebuild state for a given item + /// + /// Current DatFile object to rebuild from + /// Information for the current file to rebuild from + /// Stream representing the input file + /// True if the DAT should be used as a filter instead of a template, false otherwise + /// Output list of duplicate items to rebuild to + /// True if the item should be rebuilt, false otherwise + private static bool ShouldRebuildDB(DatFile datFile, DatItem datItem, Stream? stream, bool inverse, out ConcurrentList<(long, DatItem)> dupes) + { + // Find if the file has duplicates in the DAT + dupes = datFile.ItemsDB.GetDuplicates(datItem); + bool hasDuplicates = dupes.Count > 0; + + // If we have duplicates but we're filtering + if (hasDuplicates && inverse) + { + return false; + } + + // If we have duplicates without filtering + else if (hasDuplicates && !inverse) + { + return true; + } + + // TODO: Figure out how getting a set of duplicates works with IDDB + + // If we have no duplicates and we're filtering + else if (!hasDuplicates && inverse) + { + string? machinename = null; + + // Get the item from the current file + var item = new Rom(BaseFile.GetInfo(stream, keepReadOpen: true)); + + // Create a machine for the current item + var machine = new Machine(); + machine.SetFieldValue(Models.Metadata.Machine.DescriptionKey, Path.GetFileNameWithoutExtension(item.GetName())); + machine.SetFieldValue(Models.Metadata.Machine.NameKey, Path.GetFileNameWithoutExtension(item.GetName())); + long machineIndex = datFile.ItemsDB.AddMachine(machine); + + // If we are coming from an archive, set the correct machine name + if (machinename != null) + { + machine.SetFieldValue(Models.Metadata.Machine.DescriptionKey, machinename); + machine.SetFieldValue(Models.Metadata.Machine.NameKey, machinename); + } + + dupes.Add(item); + return true; + } + + // If we have no duplicates and we're not filtering + else + { + return false; + } + } + /// /// Rebuild from TorrentGzip to TorrentGzip /// diff --git a/SabreTools.DatTools/Statistics.cs b/SabreTools.DatTools/Statistics.cs index ce23e4d4..05860eff 100644 --- a/SabreTools.DatTools/Statistics.cs +++ b/SabreTools.DatTools/Statistics.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using System.Runtime.InteropServices.ComTypes; - #if NET40_OR_GREATER || NETCOREAPP using System.Threading.Tasks; #endif @@ -97,6 +95,7 @@ namespace SabreTools.DatTools if (single) { DatStatistics individualStats = datdata.Items.DatStatistics; + //DatStatistics individualStats = datdata.ItemsDB.DatStatistics; individualStats.DisplayName = datdata.Header.GetStringFieldValue(DatHeader.FileNameKey); individualStats.MachineCount = datdata.Items.Keys.Count; individualStats.IsDirectory = false; @@ -105,10 +104,12 @@ namespace SabreTools.DatTools // Add single DAT stats to dir dirStats.AddStatistics(datdata.Items.DatStatistics); + //dirStats.AddStatistics(datdata.ItemsDB.DatStatistics); dirStats.GameCount += datdata.Items.Keys.Count; // Add single DAT stats to totals totalStats.AddStatistics(datdata.Items.DatStatistics); + //totalStats.AddStatistics(datdata.ItemsDB.DatStatistics); totalStats.GameCount += datdata.Items.Keys.Count; // Make sure to assign the new directory