From 4e82b63ea694544968d7e1ab91cd97b025a28ed9 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 29 Aug 2016 16:48:36 -0700 Subject: [PATCH] [ALL] More work on conversion --- SabreTools.Helper/Data/Structs.cs | 29 +++++++- SabreTools.Helper/Tools/ArchiveTools.cs | 34 ++++----- SabreTools.Helper/Tools/DatTools.cs | 96 +++++++++++-------------- SabreTools.Helper/Tools/RomTools.cs | 66 +++++++++++++++++ 4 files changed, 151 insertions(+), 74 deletions(-) diff --git a/SabreTools.Helper/Data/Structs.cs b/SabreTools.Helper/Data/Structs.cs index 36f7d87f..0cc7f901 100644 --- a/SabreTools.Helper/Data/Structs.cs +++ b/SabreTools.Helper/Data/Structs.cs @@ -25,13 +25,40 @@ namespace SabreTools.Helper /// /// Intermediate struct for holding and processing Hash data (NEW SYSTEM) /// - public struct HashData + public struct HashData : IEquatable { public long Size; public byte[] CRC; public byte[] MD5; public byte[] SHA1; public List Roms; + + public bool Equals(HashData other) + { + return this.Equals(other, false); + } + + public bool Equals(HashData other, bool IsDisk) + { + bool equals = false; + + if (!IsDisk && + ((this.MD5 == null || other.MD5 == null) || this.MD5 == other.MD5) && + ((this.SHA1 == null || other.SHA1 == null) || this.SHA1 == other.SHA1)) + { + equals = true; + } + else if (!IsDisk && + (this.Size == other.Size) && + ((this.CRC == null || other.CRC != null) || this.CRC == other.CRC) && + ((this.MD5 == null || other.MD5 == null) || this.MD5 == other.MD5) && + ((this.SHA1 == null || other.SHA1 == null) || this.SHA1 == other.SHA1)) + { + equals = true; + } + + return equals; + } } /// diff --git a/SabreTools.Helper/Tools/ArchiveTools.cs b/SabreTools.Helper/Tools/ArchiveTools.cs index 9997fa5c..ffe40eed 100644 --- a/SabreTools.Helper/Tools/ArchiveTools.cs +++ b/SabreTools.Helper/Tools/ArchiveTools.cs @@ -853,9 +853,9 @@ namespace SabreTools.Helper /// Output directory to build to /// RomData representing the new information /// This uses the new system that is not implemented anywhere yet - public static void WriteToArchive(string input, string output, RomData rom, int machineIndex) + public static void WriteToArchive(string input, string output, RomData rom) { - string archiveFileName = Path.Combine(output, rom.Machines[machineIndex] + ".zip"); + string archiveFileName = Path.Combine(output, rom.Machine + ".zip"); ZipArchive outarchive = null; try @@ -904,9 +904,9 @@ namespace SabreTools.Helper /// Output directory to build to /// RomData representing the new information /// This uses the new system that is not implemented anywhere yet - public static void WriteToManagedArchive(string input, string output, RomData rom, int machineIndex) + public static void WriteToManagedArchive(string input, string output, RomData rom) { - string archiveFileName = Path.Combine(output, rom.Machines[machineIndex] + ".zip"); + string archiveFileName = Path.Combine(output, rom.Machine + ".zip"); // Delete an empty file first if (File.Exists(archiveFileName) && new FileInfo(archiveFileName).Length == 0) @@ -1013,18 +1013,16 @@ namespace SabreTools.Helper + (size == 0 ? reader.Entry.Size : size) + ", " + (crc == null ? BitConverter.GetBytes(reader.Entry.Crc) : crc)); - MachineData tempmachine = new MachineData - { - Name = gamename, - Description = gamename, - }; RomData temprom = new RomData { Type = ItemType.Rom, Name = reader.Entry.Key, - Machines = new List(), + Machine = new MachineData + { + Name = gamename, + Description = gamename, + }, }; - temprom.Machines.Add(tempmachine); HashData temphash = new HashData { Size = (size == 0 ? reader.Entry.Size : size), @@ -1105,24 +1103,22 @@ namespace SabreTools.Helper // Now convert the size and get the right position long extractedsize = (long)BitConverter.ToUInt64(headersz.Reverse().ToArray(), 0); - MachineData tempmachine = new MachineData - { - Name = sha1, - Description = sha1, - }; RomData temprom = new RomData { Type = ItemType.Rom, Name = sha1, - Machines = new List(), + Machine = new MachineData + { + Name = sha1, + Description = sha1, + }, }; - temprom.Machines.Add(tempmachine); HashData temphash = new HashData { Size = extractedsize, CRC = headercrc, MD5 = headermd5, - SHA1 = StringToByteArray(sha1), + SHA1 = Style.StringToByteArray(sha1), Roms = new List(), }; temphash.Roms.Add(temprom); diff --git a/SabreTools.Helper/Tools/DatTools.cs b/SabreTools.Helper/Tools/DatTools.cs index e7012dd3..cac4e613 100644 --- a/SabreTools.Helper/Tools/DatTools.cs +++ b/SabreTools.Helper/Tools/DatTools.cs @@ -2389,19 +2389,17 @@ namespace SabreTools.Helper // If we're in cleaning mode, sanitize the game name gamename = (clean ? Style.CleanGameName(gamename) : gamename); - MachineData machineData = new MachineData - { - Name = gamename, - Description = gamedesc, - SystemID = sysid, - SourceID = srcid, - }; RomData romData = new RomData { Type = (line.Trim().StartsWith("disk (") ? ItemType.Disk : ItemType.Rom), - Machines = new List(), + Machine = new MachineData + { + Name = gamename, + Description = gamedesc, + SystemID = sysid, + SourceID = srcid, + }, }; - romData.Machines.Add(machineData); HashData hashData = new HashData { Roms = new List(), @@ -2788,21 +2786,19 @@ namespace SabreTools.Helper // If we're in cleaning mode, sanitize the game name rominfo[3] = (clean ? Style.CleanGameName(rominfo[3]) : rominfo[3]); - MachineData machineData = new MachineData - { - Name = rominfo[3], - Description = rominfo[4], - CloneOf = rominfo[1], - RomOf = rominfo[1], - SystemID = sysid, - SourceID = srcid, - }; RomData romData = new RomData { Name = rominfo[5], - Machines = new List(), + Machine = new MachineData + { + Name = rominfo[3], + Description = rominfo[4], + CloneOf = rominfo[1], + RomOf = rominfo[1], + SystemID = sysid, + SourceID = srcid, + }, }; - romData.Machines.Add(machineData); HashData hashData = new HashData { Size = Int64.Parse(rominfo[7]), @@ -2900,18 +2896,16 @@ namespace SabreTools.Helper // If we're in cleaning mode, sanitize the game name tempgame = (clean ? Style.CleanGameName(tempgame) : tempgame); - MachineData machineData = new MachineData - { - Name = tempgame, - Description = tempgame, - }; RomData romData = new RomData { Type = ItemType.Rom, Name = "null", - Machines = new List(), + Machine = new MachineData + { + Name = tempgame, + Description = tempgame, + }, }; - romData.Machines.Add(machineData); HashData hashData = new HashData { Size = -1, @@ -3341,23 +3335,21 @@ namespace SabreTools.Helper } // Get the new values to add - MachineData machineData = new MachineData - { - Name = tempname, - Description = gamedesc, - SystemID = sysid, - System = filename, - SourceID = srcid, - }; RomData romData = new RomData { Name = subreader.GetAttribute("name"), Type = (subreader.Name.ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom), Nodump = nodump, Date = date, - Machines = new List(), + Machine = new MachineData + { + Name = tempname, + Description = gamedesc, + SystemID = sysid, + System = filename, + SourceID = srcid, + }, }; - romData.Machines.Add(machineData); HashData hashData = new HashData { Size = size, @@ -3396,18 +3388,16 @@ namespace SabreTools.Helper // If we're in cleaning mode, sanitize the game name tempname = (clean ? Style.CleanGameName(tempname.Split(Path.DirectorySeparatorChar)) : tempname); - MachineData machineData = new MachineData - { - Name = tempname, - Description = tempname, - }; RomData romData = new RomData { Type = ItemType.Rom, Name = "null", - Machines = new List(), + Machine = new MachineData + { + Name = tempname, + Description = tempname, + }, }; - romData.Machines.Add(machineData); HashData hashData = new HashData { Size = -1, @@ -3568,22 +3558,20 @@ namespace SabreTools.Helper } // Get the new values to add - MachineData machineData = new MachineData - { - Name = tempname, - SystemID = sysid, - System = filename, - SourceID = srcid, - }; RomData romData = new RomData { Name = xtr.GetAttribute("name"), Type = (xtr.GetAttribute("type").ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom), Nodump = nodump, Date = date, - Machines = new List(), + Machine = new MachineData + { + Name = tempname, + SystemID = sysid, + System = filename, + SourceID = srcid, + }, }; - romData.Machines.Add(machineData); HashData hashData = new HashData { Size = size, @@ -3682,7 +3670,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(hash); sortable.Add(newkey, temp); } diff --git a/SabreTools.Helper/Tools/RomTools.cs b/SabreTools.Helper/Tools/RomTools.cs index 55cab2cc..65297061 100644 --- a/SabreTools.Helper/Tools/RomTools.cs +++ b/SabreTools.Helper/Tools/RomTools.cs @@ -350,6 +350,26 @@ namespace SabreTools.Helper 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 + /// 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) + { + bool dupefound = hash.Equals(lasthash); + + // More wonderful SHA-1 logging that has to be done + if (hash.SHA1 == lasthash.SHA1 && hash.Size != lasthash.Size) + { + logger.User("SHA-1 mismatch - Hash: " + hash.SHA1); + } + + return dupefound; + } + /// /// Return the duplicate status of two roms /// @@ -396,6 +416,52 @@ namespace SabreTools.Helper return output; } + /// + /// Return the duplicate status of two hashes + /// + /// Current hash to check + /// Last hash to check against + /// Logger object for console and/or file output + /// The DupeType corresponding to the relationship between the two + public static DupeType GetDuplicateStatus(HashData hash, HashData lasthash, Logger logger) + { + 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) + { + if (lasthash.Roms[0].Machine.Name == hash.Roms[0].Machine.Name && lasthash.Roms[0].Name == hash.Roms[0].Name) + { + output = DupeType.ExternalAll; + } + else + { + output = DupeType.ExternalHash; + } + } + + // Otherwise, it's considered an internal dupe + else + { + if (lasthash.Roms[0].Machine.Name == hash.Roms[0].Machine.Name && lasthash.Roms[0].Name == hash.Roms[0].Name) + { + output = DupeType.InternalAll; + } + else + { + output = DupeType.InternalHash; + } + } + + return output; + } + /// /// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order) ///