diff --git a/SabreTools.Helper/Tools/RomTools.cs b/SabreTools.Helper/Tools/RomTools.cs index 9bbc7758..3632cddb 100644 --- a/SabreTools.Helper/Tools/RomTools.cs +++ b/SabreTools.Helper/Tools/RomTools.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; namespace SabreTools.Helper { public class RomTools { + #region Rom-based sorting and merging + /// /// Merge an arbitrary set of ROMs based on the supplied information /// @@ -103,6 +104,149 @@ namespace SabreTools.Helper return outroms; } + /// + /// List all duplicates found in a DAT based on a rom + /// + /// Rom 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 RomData objects + public static List GetDuplicates(Rom lastrom, Dat 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 (Rom rom in roms) + { + if (IsDuplicate(rom, lastrom, 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; + } + + /// + /// Determine if a file is a duplicate using partial matching logic + /// + /// Rom to check for duplicate status + /// Rom to use as a baseline + /// Logger object for console and/or file output + /// True if the roms are duplicates, false otherwise + public static bool IsDuplicate(Rom rom, Rom lastrom, Logger logger) + { + bool dupefound = rom.Equals(lastrom); + + // More wonderful SHA-1 logging that has to be done + if (rom.HashData.SHA1 == lastrom.HashData.SHA1 && rom.HashData.Size != lastrom.HashData.Size) + { + logger.User("SHA-1 mismatch - Hash: " + rom.HashData.SHA1); + } + + return dupefound; + } + + /// + /// Return the duplicate status of two roms + /// + /// Current rom to check + /// Last rom to check against + /// Logger object for console and/or file output + /// The DupeType corresponding to the relationship between the two + public static DupeType GetDuplicateStatus(Rom rom, Rom lastrom, Logger logger) + { + DupeType output = DupeType.None; + + // If we don't have a duplicate at all, return none + if (!IsDuplicate(rom, lastrom, logger)) + { + return output; + } + + // If the duplicate is external already or should be, set it + if (lastrom.Dupe >= DupeType.ExternalHash || lastrom.Metadata.SystemID != rom.Metadata.SystemID || lastrom.Metadata.SourceID != rom.Metadata.SourceID) + { + if (lastrom.Machine.Name == rom.Machine.Name && lastrom.Name == rom.Name) + { + output = DupeType.ExternalAll; + } + else + { + output = DupeType.ExternalHash; + } + } + + // Otherwise, it's considered an internal dupe + else + { + if (lastrom.Machine.Name == rom.Machine.Name && lastrom.Name == rom.Name) + { + output = DupeType.InternalAll; + } + else + { + output = DupeType.InternalHash; + } + } + + return output; + } + + /// + /// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order) + /// + /// List of RomData objects representing the roms to be sorted + /// True if files are not renamed, false otherwise + /// True if it sorted correctly, false otherwise + public static bool Sort(List roms, bool norename) + { + roms.Sort(delegate (Rom x, Rom y) + { + if (x.Metadata.SystemID == y.Metadata.SystemID) + { + if (x.Metadata.SourceID == y.Metadata.SourceID) + { + if (x.Machine.Name == y.Machine.Name) + { + return String.Compare(x.Name, y.Name); + } + return String.Compare(x.Machine.Name, y.Machine.Name); + } + return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SourceID - y.Metadata.SourceID); + } + return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SystemID - y.Metadata.SystemID); + }); + return true; + } + + #endregion + + #region HashData-based sorting and merging + /// /// Merge an arbitrary set of ROMs based on the supplied information /// @@ -201,28 +345,29 @@ namespace SabreTools.Helper return outroms; } + /* /// /// List all duplicates found in a DAT based on a rom /// - /// Rom to use as a base + /// Hash 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 RomData objects - public static List GetDuplicates(Rom lastrom, Dat datdata, Logger logger, bool remove = false) + /// List of matched HashData objects + public static List GetDuplicates(HashData lastrom, DatData datdata, Logger logger, bool remove = false) { - List output = new List(); + List output = new List(); // Check for an empty rom list first - if (datdata.Files == null || datdata.Files.Count == 0) + if (datdata.Hashes == null || datdata.Hashes.Count == 0) { return output; } // Try to find duplicates - List keys = datdata.Files.Keys.ToList(); - foreach (string key in keys) + for (int i = 0; i < datdata.Hashes.Count; i++) { + List roms = datdata.Files[key]; List left = new List(); foreach (Rom rom in roms) @@ -246,40 +391,21 @@ namespace SabreTools.Helper return output; } - - /// - /// Determine if a file is a duplicate using partial matching logic - /// - /// Rom to check for duplicate status - /// Rom to use as a baseline - /// Logger object for console and/or file output - /// True if the roms are duplicates, false otherwise - public static bool IsDuplicate(Rom rom, Rom lastrom, Logger logger) - { - bool dupefound = rom.Equals(lastrom); - - // More wonderful SHA-1 logging that has to be done - if (rom.HashData.SHA1 == lastrom.HashData.SHA1 && rom.HashData.Size != lastrom.HashData.Size) - { - logger.User("SHA-1 mismatch - Hash: " + rom.HashData.SHA1); - } - - return dupefound; - } + */ /// /// Determine if a file is a duplicate using partial matching logic /// /// Hash to check for duplicate status - /// Hash to use as a baseline + /// Hash to use as a baseline /// Logger object for console and/or file output /// True if the hashes are duplicates, false otherwise - public static bool IsDuplicate(HashData hash, HashData lasthash, Logger logger) + public static bool IsDuplicate(HashData hash, int hashIndex, HashData lastHash, int lastHashIndex, Logger logger) { - bool dupefound = hash.Equals(lasthash); + bool dupefound = hash.Equals(lastHash); // More wonderful SHA-1 logging that has to be done - if (hash.SHA1 == lasthash.SHA1 && hash.Size != lasthash.Size) + if (hash.SHA1 == lastHash.SHA1 && hash.Size != lastHash.Size) { logger.User("SHA-1 mismatch - Hash: " + hash.SHA1); } @@ -287,52 +413,6 @@ namespace SabreTools.Helper return dupefound; } - /// - /// Return the duplicate status of two roms - /// - /// Current rom to check - /// Last rom to check against - /// Logger object for console and/or file output - /// The DupeType corresponding to the relationship between the two - public static DupeType GetDuplicateStatus(Rom rom, Rom lastrom, Logger logger) - { - DupeType output = DupeType.None; - - // If we don't have a duplicate at all, return none - if (!IsDuplicate(rom, lastrom, logger)) - { - return output; - } - - // If the duplicate is external already or should be, set it - if (lastrom.Dupe >= DupeType.ExternalHash || lastrom.Metadata.SystemID != rom.Metadata.SystemID || lastrom.Metadata.SourceID != rom.Metadata.SourceID) - { - if (lastrom.Machine.Name == rom.Machine.Name && lastrom.Name == rom.Name) - { - output = DupeType.ExternalAll; - } - else - { - output = DupeType.ExternalHash; - } - } - - // Otherwise, it's considered an internal dupe - else - { - if (lastrom.Machine.Name == rom.Machine.Name && lastrom.Name == rom.Name) - { - output = DupeType.InternalAll; - } - else - { - output = DupeType.InternalHash; - } - } - - return output; - } - /// /// Return the duplicate status of two hashes /// @@ -344,11 +424,13 @@ namespace SabreTools.Helper { DupeType output = DupeType.None; + /* // If we don't have a duplicate at all, return none if (!IsDuplicate(hash, lasthash, logger)) { return output; } + */ // If the duplicate is external already or should be, set it if (lasthash.Roms[0].DupeType >= DupeType.ExternalHash || lasthash.Roms[0].Machine.SystemID != hash.Roms[0].Machine.SystemID || lasthash.Roms[0].Machine.SourceID != hash.Roms[0].Machine.SourceID) @@ -379,31 +461,6 @@ namespace SabreTools.Helper return output; } - /// - /// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order) - /// - /// List of RomData objects representing the roms to be sorted - /// True if files are not renamed, false otherwise - /// True if it sorted correctly, false otherwise - public static bool Sort(List roms, bool norename) - { - roms.Sort(delegate (Rom x, Rom y) - { - if (x.Metadata.SystemID == y.Metadata.SystemID) - { - if (x.Metadata.SourceID == y.Metadata.SourceID) - { - if (x.Machine.Name == y.Machine.Name) - { - return String.Compare(x.Name, y.Name); - } - return String.Compare(x.Machine.Name, y.Machine.Name); - } - return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SourceID - y.Metadata.SourceID); - } - return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SystemID - y.Metadata.SystemID); - }); - return true; - } + #endregion } }