/// <summary> /// Return the duplicate status of two items /// </summary> /// <param name="lastItem">DatItem to check against</param> /// <returns>The DupeType corresponding to the relationship between the two</returns> public DupeType GetDuplicateStatus(DatItem lastItem) { DupeType output = 0x00; // If we don't have a duplicate at all, return none if (!this.Equals(lastItem)) { return(output); } // If the duplicate is external already or should be, set it if ((lastItem.DupeType & DupeType.External) != 0 || lastItem.SystemID != this.SystemID || lastItem.SourceID != this.SourceID) { if (lastItem.MachineName == this.MachineName && lastItem.Name == this.Name) { output = DupeType.External | DupeType.All; } else { output = DupeType.External | DupeType.Hash; } } // Otherwise, it's considered an internal dupe else { if (lastItem.MachineName == this.MachineName && lastItem.Name == this.Name) { output = DupeType.Internal | DupeType.All; } else { output = DupeType.Internal | DupeType.Hash; } } return(output); }
/// <summary> /// Merge an arbitrary set of ROMs based on the supplied information /// </summary> /// <param name="infiles">List of File objects representing the roms to be merged</param> /// <returns>A List of DatItem objects representing the merged roms</returns> public static List <DatItem> Merge(List <DatItem> infiles) { // Check for null or blank roms first if (infiles == null || infiles.Count == 0) { return(new List <DatItem>()); } // Create output list List <DatItem> outfiles = new List <DatItem>(); // Then deduplicate them by checking to see if data matches previous saved roms int nodumpCount = 0; foreach (DatItem file in infiles) { // If we don't have a Rom or a Disk, we skip checking for duplicates if (file.ItemType != ItemType.Rom && file.ItemType != ItemType.Disk) { continue; } // If it's a nodump, add and skip if (file.ItemType == ItemType.Rom && ((Rom)file).ItemStatus == ItemStatus.Nodump) { outfiles.Add(file); nodumpCount++; continue; } else if (file.ItemType == ItemType.Disk && ((Disk)file).ItemStatus == ItemStatus.Nodump) { outfiles.Add(file); nodumpCount++; continue; } // If it's the first non-nodump rom in the list, don't touch it else if (outfiles.Count == 0 || outfiles.Count == nodumpCount) { outfiles.Add(file); continue; } // Check if the rom is a duplicate DupeType dupetype = 0x00; DatItem saveditem = new Rom(); int pos = -1; for (int i = 0; i < outfiles.Count; i++) { DatItem lastrom = outfiles[i]; // Get the duplicate status dupetype = file.GetDuplicateStatus(lastrom); // If it's a duplicate, skip adding it to the output but add any missing information if (dupetype != 0x00) { saveditem = lastrom; pos = i; // Roms have more infomration to save if (file.ItemType == ItemType.Rom) { ((Rom)saveditem).Size = (((Rom)saveditem).Size == -1 && ((Rom)file).Size != -1 ? ((Rom)file).Size : ((Rom)saveditem).Size); ((Rom)saveditem).CRC = (String.IsNullOrWhiteSpace(((Rom)saveditem).CRC) && !String.IsNullOrWhiteSpace(((Rom)file).CRC) ? ((Rom)file).CRC : ((Rom)saveditem).CRC); ((Rom)saveditem).MD5 = (String.IsNullOrWhiteSpace(((Rom)saveditem).MD5) && !String.IsNullOrWhiteSpace(((Rom)file).MD5) ? ((Rom)file).MD5 : ((Rom)saveditem).MD5); ((Rom)saveditem).SHA1 = (String.IsNullOrWhiteSpace(((Rom)saveditem).SHA1) && !String.IsNullOrWhiteSpace(((Rom)file).SHA1) ? ((Rom)file).SHA1 : ((Rom)saveditem).SHA1); ((Rom)saveditem).SHA256 = (String.IsNullOrWhiteSpace(((Rom)saveditem).SHA256) && !String.IsNullOrWhiteSpace(((Rom)file).SHA256) ? ((Rom)file).SHA256 : ((Rom)saveditem).SHA256); ((Rom)saveditem).SHA384 = (String.IsNullOrWhiteSpace(((Rom)saveditem).SHA384) && !String.IsNullOrWhiteSpace(((Rom)file).SHA384) ? ((Rom)file).SHA384 : ((Rom)saveditem).SHA384); ((Rom)saveditem).SHA512 = (String.IsNullOrWhiteSpace(((Rom)saveditem).SHA512) && !String.IsNullOrWhiteSpace(((Rom)file).SHA512) ? ((Rom)file).SHA512 : ((Rom)saveditem).SHA512); } else if (file.ItemType == ItemType.Disk) { ((Disk)saveditem).MD5 = (String.IsNullOrWhiteSpace(((Disk)saveditem).MD5) && !String.IsNullOrWhiteSpace(((Disk)file).MD5) ? ((Disk)file).MD5 : ((Disk)saveditem).MD5); ((Disk)saveditem).SHA1 = (String.IsNullOrWhiteSpace(((Disk)saveditem).SHA1) && !String.IsNullOrWhiteSpace(((Disk)file).SHA1) ? ((Disk)file).SHA1 : ((Disk)saveditem).SHA1); ((Disk)saveditem).SHA256 = (String.IsNullOrWhiteSpace(((Disk)saveditem).SHA256) && !String.IsNullOrWhiteSpace(((Disk)file).SHA256) ? ((Disk)file).SHA256 : ((Disk)saveditem).SHA256); ((Disk)saveditem).SHA384 = (String.IsNullOrWhiteSpace(((Disk)saveditem).SHA384) && !String.IsNullOrWhiteSpace(((Disk)file).SHA384) ? ((Disk)file).SHA384 : ((Disk)saveditem).SHA384); ((Disk)saveditem).SHA512 = (String.IsNullOrWhiteSpace(((Disk)saveditem).SHA512) && !String.IsNullOrWhiteSpace(((Disk)file).SHA512) ? ((Disk)file).SHA512 : ((Disk)saveditem).SHA512); } saveditem.DupeType = dupetype; // If the current system has a lower ID than the previous, set the system accordingly if (file.SystemID < saveditem.SystemID) { saveditem.SystemID = file.SystemID; saveditem.System = file.System; saveditem.CopyMachineInformation(file); saveditem.Name = file.Name; } // If the current source has a lower ID than the previous, set the source accordingly if (file.SourceID < saveditem.SourceID) { saveditem.SourceID = file.SourceID; saveditem.Source = file.Source; saveditem.CopyMachineInformation(file); saveditem.Name = file.Name; } // If the current machine is a child of the new machine, use the new machine instead if (saveditem.CloneOf == file.MachineName || saveditem.RomOf == file.MachineName) { saveditem.CopyMachineInformation(file); saveditem.Name = file.Name; } break; } } // If no duplicate is found, add it to the list if (dupetype == 0x00) { outfiles.Add(file); } // Otherwise, if a new rom information is found, add that else { outfiles.RemoveAt(pos); outfiles.Insert(pos, saveditem); } } // Then return the result return(outfiles); }