diff --git a/SabreTools.Helper/Objects/Dat/DatFile.cs b/SabreTools.Helper/Objects/Dat/DatFile.cs index 21840b23..c48356d7 100644 --- a/SabreTools.Helper/Objects/Dat/DatFile.cs +++ b/SabreTools.Helper/Objects/Dat/DatFile.cs @@ -2850,7 +2850,7 @@ namespace SabreTools.Helper } // If the rom passes the filter, include it - if (DatItem.Filter(item, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, logger)) + if (item.Filter(gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, logger)) { // If we are in single game mode, rename all games if (single) diff --git a/SabreTools.Helper/Objects/Dat/DatItem.cs b/SabreTools.Helper/Objects/Dat/DatItem.cs index cc720016..829f3df3 100644 --- a/SabreTools.Helper/Objects/Dat/DatItem.cs +++ b/SabreTools.Helper/Objects/Dat/DatItem.cs @@ -234,12 +234,13 @@ namespace SabreTools.Helper #endregion - #region Sorting and merging + #region Instance Methods + + #region Sorting and Merging /// /// Determine if a rom should be included based on filters /// - /// User supplied item to check /// Name of the game to match (can use asterisk-partials) /// Name of the rom to match (can use asterisk-partials) /// Type of the rom to match @@ -252,13 +253,13 @@ namespace SabreTools.Helper /// Select roms with the given status /// Logging object for console and file output /// Returns true if it should be included, false otherwise - public static bool Filter(DatItem itemdata, string gamename, string romname, string romtype, long sgt, + public bool Filter(string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, ItemStatus itemStatus, Logger logger) { // Take care of Rom and Disk specific differences - if (itemdata.Type == ItemType.Rom) + if (Type == ItemType.Rom) { - Rom rom = (Rom)itemdata; + Rom rom = (Rom)this; // Filter on status if (itemStatus != ItemStatus.NULL) @@ -389,9 +390,9 @@ namespace SabreTools.Helper } } } - else if (itemdata.Type == ItemType.Disk) + else if (Type == ItemType.Disk) { - Disk rom = (Disk)itemdata; + Disk rom = (Disk)this; // Filter on status if (itemStatus != ItemStatus.NULL && rom.ItemStatus != itemStatus) @@ -471,28 +472,28 @@ namespace SabreTools.Helper { if (gamename.StartsWith("*") && gamename.EndsWith("*")) { - if (!itemdata.MachineName.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", ""))) + if (!MachineName.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", ""))) { return false; } } else if (gamename.StartsWith("*")) { - if (!itemdata.MachineName.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + if (!MachineName.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) { return false; } } else if (gamename.EndsWith("*")) { - if (!itemdata.MachineName.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + if (!MachineName.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) { return false; } } else { - if (!String.Equals(itemdata.MachineName, gamename, StringComparison.InvariantCultureIgnoreCase)) + if (!String.Equals(MachineName, gamename, StringComparison.InvariantCultureIgnoreCase)) { return false; } @@ -504,28 +505,28 @@ namespace SabreTools.Helper { if (romname.StartsWith("*") && romname.EndsWith("*")) { - if (!itemdata.Name.ToLowerInvariant().Contains(romname.ToLowerInvariant().Replace("*", ""))) + if (!Name.ToLowerInvariant().Contains(romname.ToLowerInvariant().Replace("*", ""))) { return false; } } else if (romname.StartsWith("*")) { - if (!itemdata.Name.EndsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + if (!Name.EndsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) { return false; } } else if (romname.EndsWith("*")) { - if (!itemdata.Name.StartsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + if (!Name.StartsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) { return false; } } else { - if (!String.Equals(itemdata.Name, romname, StringComparison.InvariantCultureIgnoreCase)) + if (!String.Equals(Name, romname, StringComparison.InvariantCultureIgnoreCase)) { return false; } @@ -533,11 +534,11 @@ namespace SabreTools.Helper } // Filter on rom type - if (String.IsNullOrEmpty(romtype) && itemdata.Type != ItemType.Rom && itemdata.Type != ItemType.Disk) + if (String.IsNullOrEmpty(romtype) && Type != ItemType.Rom && Type != ItemType.Disk) { return false; } - if (!String.IsNullOrEmpty(romtype) && !String.Equals(itemdata.Type.ToString(), romtype, StringComparison.InvariantCultureIgnoreCase)) + if (!String.IsNullOrEmpty(romtype) && !String.Equals(Type.ToString(), romtype, StringComparison.InvariantCultureIgnoreCase)) { return false; } @@ -545,6 +546,60 @@ namespace SabreTools.Helper return true; } + /// + /// List all duplicates found in a DAT based on a rom + /// + /// Dat to match against + /// Logger object for console and/or file output + /// True to remove matched roms from the input, false otherwise (default) + /// List of matched DatItem objects + public List GetDuplicates(DatFile datdata, Logger logger, bool remove = false) + { + List output = new List(); + + // Check for an empty rom list first + if (datdata.Files == null || datdata.Files.Count == 0) + { + return output; + } + + // Try to find duplicates + List keys = datdata.Files.Keys.ToList(); + foreach (string key in keys) + { + List roms = datdata.Files[key]; + List left = new List(); + + foreach (DatItem rom in roms) + { + if (IsDuplicate(rom, logger)) + { + output.Add(rom); + } + else + { + left.Add(rom); + } + } + + // If we're in removal mode, replace the list with the new one + if (remove) + { + datdata.Files[key] = left; + } + } + + return output; + } + + #endregion + + #endregion // Instance Methods + + #region Static Methods + + #region Sorting and Merging + /// /// Merge an arbitrary set of ROMs based on the supplied information /// @@ -673,53 +728,6 @@ namespace SabreTools.Helper return outfiles; } - /// - /// List all duplicates found in a DAT based on a rom - /// - /// Item to use as a base - /// Dat to match against - /// Logger object for console and/or file output - /// True to remove matched roms from the input, false otherwise (default) - /// List of matched DatItem objects - public static List GetDuplicates(DatItem lastitem, DatFile datdata, Logger logger, bool remove = false) - { - List output = new List(); - - // Check for an empty rom list first - if (datdata.Files == null || datdata.Files.Count == 0) - { - return output; - } - - // Try to find duplicates - List keys = datdata.Files.Keys.ToList(); - foreach (string key in keys) - { - List roms = datdata.Files[key]; - List left = new List(); - - foreach (DatItem rom in roms) - { - if (rom.IsDuplicate(lastitem, logger)) - { - output.Add(rom); - } - else - { - left.Add(rom); - } - } - - // If we're in removal mode, replace the list with the new one - if (remove) - { - datdata.Files[key] = left; - } - } - - return output; - } - /// /// Sort a list of File objects by SystemID, SourceID, Game, and Name (in order) /// @@ -779,5 +787,7 @@ namespace SabreTools.Helper } #endregion + + #endregion // Static Methods } } diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index ee35edf5..387acff2 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -414,7 +414,7 @@ namespace SabreTools.Helper { foreach (DatItem rom in matchdat.Files[key]) { - List matched = DatItem.GetDuplicates(rom, _datdata, _logger, true); + List matched = rom.GetDuplicates(_datdata, _logger, true); foreach (DatItem match in matched) { try @@ -551,7 +551,7 @@ namespace SabreTools.Helper } // Try to find the matches to the file that was found - List foundroms = DatItem.GetDuplicates(rom, _datdata, _logger); + List foundroms = rom.GetDuplicates(_datdata, _logger); _logger.Log("File '" + input + "' had " + foundroms.Count + " matches in the DAT!"); foreach (Rom found in foundroms) { @@ -621,7 +621,7 @@ namespace SabreTools.Helper } // Try to find the matches to the file that was found - List founddroms = DatItem.GetDuplicates(drom, _datdata, _logger); + List founddroms = drom.GetDuplicates(_datdata, _logger); _logger.Log("File '" + newinput + "' had " + founddroms.Count + " matches in the DAT!"); foreach (Rom found in founddroms) { @@ -756,7 +756,7 @@ namespace SabreTools.Helper foreach (Rom rom in internalRomData) { // Try to find the matches to the file that was found - List foundroms = DatItem.GetDuplicates(rom, _datdata, _logger); + List foundroms = rom.GetDuplicates(_datdata, _logger); _logger.Log("File '" + rom.Name + "' had " + foundroms.Count + " matches in the DAT!"); foreach (Rom found in foundroms) {