diff --git a/SabreTools.Filtering/ExtraIni.cs b/SabreTools.Filtering/ExtraIni.cs index 14aa5779..ab42d89d 100644 --- a/SabreTools.Filtering/ExtraIni.cs +++ b/SabreTools.Filtering/ExtraIni.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; - +using System.Linq; using SabreTools.Core; using SabreTools.Core.Tools; using SabreTools.DatFiles; @@ -88,67 +88,38 @@ namespace SabreTools.Filtering // Bucket by game first datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None); - // Create a new set of mappings based on the items - var machineMap = new Dictionary>(); - var datItemMap = new Dictionary>(); + // Create mappings based on the extra items + var combinedMachineMaps = CombineMachineExtras(); + var combinedDatItemMaps = CombineDatItemExtras(); - // Loop through each of the extras - foreach (ExtraIniItem item in Items) - { - foreach (var mapping in item.Mappings) - { - string key = mapping.Key; - List machineNames = mapping.Value; + // Now get the combined set of keys + var machines = combinedMachineMaps.Keys.Concat(combinedDatItemMaps.Keys).Distinct(); - // Loop through the machines and add the new mappings - foreach (string machine in machineNames) - { - if (item.MachineField != MachineField.NULL) - { - if (!machineMap.ContainsKey(machine)) - machineMap[machine] = new Dictionary(); - - machineMap[machine][item.MachineField] = key; - } - else if (item.DatItemField != DatItemField.NULL) - { - if (!datItemMap.ContainsKey(machine)) - datItemMap[machine] = new Dictionary(); - - datItemMap[machine][item.DatItemField] = key; - } - } - } - } - - // Now apply the new set of Machine mappings - foreach (string key in machineMap.Keys) + // Apply the mappings + foreach (string machine in machines) { // If the key doesn't exist, continue - if (!datFile.Items.ContainsKey(key)) + if (!datFile.Items.ContainsKey(machine)) continue; - List datItems = datFile.Items[key]; - Setter setter = new Setter { MachineMappings = machineMap[key] }; + // Get the list of DatItems for the machine + List datItems = datFile.Items[machine]; + // Try to get the map values, if possible + combinedMachineMaps.TryGetValue(machine, out Dictionary machineMappings); + combinedDatItemMaps.TryGetValue(machine, out Dictionary datItemMappings); + + // Create a setter with the new mappings + Setter setter = new Setter + { + MachineMappings = machineMappings, + DatItemMappings = datItemMappings, + }; + + // Loop through and set the fields accordingly foreach (var datItem in datItems) { setter.SetFields(datItem.Machine); - } - } - - // Now apply the new set of DatItem mappings - foreach (string key in datItemMap.Keys) - { - // If the key doesn't exist, continue - if (!datFile.Items.ContainsKey(key)) - continue; - - List datItems = datFile.Items[key]; - Setter setter = new Setter { DatItemMappings = datItemMap[key] }; - - foreach (var datItem in datItems) - { setter.SetFields(datItem); } } @@ -162,6 +133,58 @@ namespace SabreTools.Filtering return true; } + /// + /// Combine MachineField-based ExtraIni fields + /// + /// Mapping dictionary from machine name to field mapping + private Dictionary> CombineMachineExtras() + { + var machineMap = new Dictionary>(); + + // Loop through each of the extras + foreach (ExtraIniItem item in Items.Where(i => i.MachineField != MachineField.NULL)) + { + foreach (var mapping in item.Mappings) + { + string machineName = mapping.Key; + string value = mapping.Value; + + machineMap[machineName] = new Dictionary + { + [item.MachineField] = value, + }; + } + } + + return machineMap; + } + + /// + /// Combine DatItemField-based ExtraIni fields + /// + /// Mapping dictionary from machine name to field mapping + private Dictionary> CombineDatItemExtras() + { + var datItemMap = new Dictionary>(); + + // Loop through each of the extras + foreach (ExtraIniItem item in Items.Where(i => i.DatItemField != DatItemField.NULL)) + { + foreach (var mapping in item.Mappings) + { + string machineName = mapping.Key; + string value = mapping.Value; + + datItemMap[machineName] = new Dictionary() + { + [item.DatItemField] = value, + }; + } + } + + return datItemMap; + } + #endregion } } diff --git a/SabreTools.Filtering/ExtraIniItem.cs b/SabreTools.Filtering/ExtraIniItem.cs index 303cb8a9..5d1fa692 100644 --- a/SabreTools.Filtering/ExtraIniItem.cs +++ b/SabreTools.Filtering/ExtraIniItem.cs @@ -22,9 +22,9 @@ namespace SabreTools.Filtering public DatItemField DatItemField { get; set; } = DatItemField.NULL; /// - /// Mappings from value to machine name + /// Mappings from machine names to value /// - public Dictionary> Mappings { get; set; } = new Dictionary>(); + public Dictionary Mappings { get; set; } = new Dictionary(); #endregion @@ -77,22 +77,18 @@ namespace SabreTools.Filtering // If we have a value, then we start populating the dictionary else if (foundRootFolder) { - // Get the key and value - string key = ir.Section; - string value = ir.CurrentLine.Trim(); + // Get the value and machine name + string value = ir.Section; + string machineName = ir.CurrentLine.Trim(); // If the section is "ROOT_FOLDER", then we use the value "true" instead. // This is done because some INI files use the name of the file as the // category to be assigned to the items included. - if (key == "ROOT_FOLDER") - key = "true"; - - // Ensure the key exists - if (!Mappings.ContainsKey(key)) - Mappings[key] = new List(); + if (value == "ROOT_FOLDER") + value = "true"; // Add the new mapping - Mappings[key].Add(value); + Mappings[machineName] = value; } } }