using System.Collections.Generic; using System.Linq; using SabreTools.Core; using SabreTools.Core.Tools; using SabreTools.DatItems; using SabreTools.DatItems.Formats; namespace SabreTools.Filtering { /// /// Replace fields in DatItems /// public static class Replacer { /// /// Replace fields with given values /// /// Machine to replace fields in /// Machine to pull new information from /// List of fields representing what should be updated /// True if descriptions should only be replaced if the game name is the same, false otherwise public static void ReplaceFields(Machine machine, Machine repMachine, List machineFieldNames, bool onlySame) { // If we have an invalid input, return if (machine == null || repMachine == null || machineFieldNames == null) return; // Loop through and replace fields foreach (string fieldName in machineFieldNames) { // Special case for description if (machineFieldNames.Contains(Models.Metadata.Machine.DescriptionKey)) { if (!onlySame || (onlySame && machine.Name == machine.Description)) machine.Description = repMachine.Description; continue; } machine.ReplaceField(repMachine, fieldName); } } /// /// Replace fields with given values /// /// DatItem to replace fields in /// DatItem to pull new information from /// List of fields representing what should be updated public static void ReplaceFields(DatItem datItem, DatItem repDatItem, Dictionary> itemFieldNames) { // If we have an invalid input, return if (datItem == null || repDatItem == null || itemFieldNames == null) return; #region Common if (datItem.ItemType != repDatItem.ItemType) return; // If there are no field names for this type or generic, return string? itemType = datItem.ItemType.AsStringValue(); if (itemType == null || (!itemFieldNames.ContainsKey(itemType) && !itemFieldNames.ContainsKey("item"))) return; // Get the combined list of fields to remove var fieldNames = new List(); if (itemFieldNames.ContainsKey(itemType)) fieldNames.AddRange(itemFieldNames[itemType]); if (itemFieldNames.ContainsKey("item")) fieldNames.AddRange(itemFieldNames["item"]); fieldNames = fieldNames.Distinct().ToList(); // If the field specifically contains Name, set it separately if (fieldNames.Contains(Models.Metadata.Rom.NameKey)) datItem.SetName(repDatItem.GetName()); #endregion #region Item-Specific // Handle unnested sets first foreach (var fieldName in fieldNames) { datItem.ReplaceField(repDatItem, fieldName); } // Handle nested sets switch (datItem, repDatItem) { case (Disk disk, Disk repDisk): ReplaceFields(disk, repDisk, fieldNames); break; case (Media media, Media repMedia): ReplaceFields(media, repMedia, fieldNames); break; case (Rom rom, Rom repRom): ReplaceFields(rom, repRom, fieldNames); break; } #endregion } /// /// Replace fields with given values /// /// Disk to remove replace fields in /// Disk to pull new information from /// List of fields representing what should be updated private static void ReplaceFields(Disk disk, Disk newItem, List datItemFields) { if (datItemFields.Contains(Models.Metadata.Disk.MD5Key)) { if (string.IsNullOrEmpty(disk.MD5) && !string.IsNullOrEmpty(newItem.MD5)) disk.MD5 = newItem.MD5; } if (datItemFields.Contains(Models.Metadata.Disk.SHA1Key)) { if (string.IsNullOrEmpty(disk.SHA1) && !string.IsNullOrEmpty(newItem.SHA1)) disk.SHA1 = newItem.SHA1; } } /// /// Replace fields with given values /// /// Media to remove replace fields in /// Media to pull new information from /// List of fields representing what should be updated private static void ReplaceFields(Media media, Media newItem, List datItemFields) { if (datItemFields.Contains(Models.Metadata.Media.MD5Key)) { if (string.IsNullOrEmpty(media.MD5) && !string.IsNullOrEmpty(newItem.MD5)) media.MD5 = newItem.MD5; } if (datItemFields.Contains(Models.Metadata.Media.SHA1Key)) { if (string.IsNullOrEmpty(media.SHA1) && !string.IsNullOrEmpty(newItem.SHA1)) media.SHA1 = newItem.SHA1; } if (datItemFields.Contains(Models.Metadata.Media.SHA256Key)) { if (string.IsNullOrEmpty(media.SHA256) && !string.IsNullOrEmpty(newItem.SHA256)) media.SHA256 = newItem.SHA256; } if (datItemFields.Contains(Models.Metadata.Media.SpamSumKey)) { if (string.IsNullOrEmpty(media.SpamSum) && !string.IsNullOrEmpty(newItem.SpamSum)) media.SpamSum = newItem.SpamSum; } } /// /// Replace fields with given values /// /// Rom to remove replace fields in /// Rom to pull new information from /// List of fields representing what should be updated private static void ReplaceFields(Rom rom, Rom newItem, List datItemFields) { if (datItemFields.Contains(Models.Metadata.Rom.CRCKey)) { if (string.IsNullOrEmpty(rom.CRC) && !string.IsNullOrEmpty(newItem.CRC)) rom.CRC = newItem.CRC; } if (datItemFields.Contains(Models.Metadata.Rom.MD5Key)) { if (string.IsNullOrEmpty(rom.MD5) && !string.IsNullOrEmpty(newItem.MD5)) rom.MD5 = newItem.MD5; } if (datItemFields.Contains(Models.Metadata.Rom.SHA1Key)) { if (string.IsNullOrEmpty(rom.SHA1) && !string.IsNullOrEmpty(newItem.SHA1)) rom.SHA1 = newItem.SHA1; } if (datItemFields.Contains(Models.Metadata.Rom.SHA256Key)) { if (string.IsNullOrEmpty(rom.SHA256) && !string.IsNullOrEmpty(newItem.SHA256)) rom.SHA256 = newItem.SHA256; } if (datItemFields.Contains(Models.Metadata.Rom.SHA384Key)) { if (string.IsNullOrEmpty(rom.SHA384) && !string.IsNullOrEmpty(newItem.SHA384)) rom.SHA384 = newItem.SHA384; } if (datItemFields.Contains(Models.Metadata.Rom.SHA512Key)) { if (string.IsNullOrEmpty(rom.SHA512) && !string.IsNullOrEmpty(newItem.SHA512)) rom.SHA512 = newItem.SHA512; } if (datItemFields.Contains(Models.Metadata.Rom.SpamSumKey)) { if (string.IsNullOrEmpty(rom.SpamSum) && !string.IsNullOrEmpty(newItem.SpamSum)) rom.SpamSum = newItem.SpamSum; } } } }