diff --git a/SabreTools.DatFiles/Setter.cs b/SabreTools.DatFiles/Setter.cs index c53864c0..da6d70f0 100644 --- a/SabreTools.DatFiles/Setter.cs +++ b/SabreTools.DatFiles/Setter.cs @@ -79,6 +79,31 @@ namespace SabreTools.DatFiles watch.Stop(); } + /// + /// Populate the setters using a set of field names + /// + /// Dictionary of mappings + public void PopulateSettersFromDictionary(Dictionary<(string, string), string>? mappings) + { + // If the dictionary is null or empty, just return + if (mappings == null || mappings.Count == 0) + return; + + var watch = new InternalStopwatch("Populating setters from dictionary"); + + // Now we loop through and get values for everything + foreach (var mapping in mappings) + { + string field = $"{mapping.Key.Item1}.{mapping.Key.Item2}"; + string value = mapping.Value; + + if (!SetSetter(field, value)) + logger.Warning($"The value {value} did not match any known field names. Please check the wiki for more details on supported field names."); + } + + watch.Stop(); + } + /// /// Set remover from a value /// diff --git a/SabreTools.Filtering/ExtraIni.cs b/SabreTools.Filtering/ExtraIni.cs index 2d99d790..6ccc0a1c 100644 --- a/SabreTools.Filtering/ExtraIni.cs +++ b/SabreTools.Filtering/ExtraIni.cs @@ -77,7 +77,7 @@ namespace SabreTools.Filtering } #endregion - + #region Running /// @@ -100,11 +100,8 @@ namespace SabreTools.Filtering datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None); // Create mappings based on the extra items - var combinedMachineMaps = CombineMachineExtras(); - var combinedDatItemMaps = CombineDatItemExtras(); - - // Now get the combined set of keys - var machines = combinedMachineMaps.Keys.Concat(combinedDatItemMaps.Keys).Distinct(); + var combinedMaps = CombineExtras(); + var machines = combinedMaps.Keys; // Apply the mappings foreach (string machine in machines) @@ -119,16 +116,11 @@ namespace SabreTools.Filtering continue; // Try to get the map values, if possible - combinedMachineMaps.TryGetValue(machine, out var machineMappings); - combinedDatItemMaps.TryGetValue(machine, out var datItemMappings); + combinedMaps.TryGetValue(machine, out var mappings); // Create a setter with the new mappings var setter = new Setter(); - setter.PopulateSettersFromList() - { - MachineFieldMappings = machineMappings, - ItemFieldMappings = datItemMappings, - }; + setter.PopulateSettersFromDictionary(mappings); // Loop through and set the fields accordingly foreach (var datItem in datItems) @@ -155,83 +147,28 @@ namespace SabreTools.Filtering /// Combine ExtraIni fields /// /// Mapping dictionary from machine name to field mapping - private (List Keys, List Values) CombineExtras() + private Dictionary> CombineExtras() { - var keys = new List(); - var values = new List(); + var machineMap = new Dictionary>(); // Loop through each of the extras foreach (ExtraIniItem item in Items) { - - foreach (var mapping in item.Mappings) { string machineName = mapping.Key; string value = mapping.Value; - - mapping[machineName] = new Dictionary - { - [item.FieldName!] = value, - }; - } - } - return mapping; - } + if (!machineMap.ContainsKey(machineName)) + machineMap[machineName] = []; - /// - /// 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 != null)) - { - foreach (var mapping in item.Mappings) - { - string machineName = mapping.Key; - string value = mapping.Value; - - machineMap[machineName] = new Dictionary - { - [item.MachineField!] = value, - }; + machineMap[machineName][item.FieldName!] = 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.ItemField != null)) - { - foreach (var mapping in item.Mappings) - { - string machineName = mapping.Key; - string value = mapping.Value; - - datItemMap[machineName] = new Dictionary() - { - [item.ItemField!] = value, - }; - } - } - - return datItemMap; - } - #endregion } } diff --git a/SabreTools.Test/DatFiles/SetterTests.cs b/SabreTools.Test/DatFiles/SetterTests.cs index 12667dce..96ef6dd9 100644 --- a/SabreTools.Test/DatFiles/SetterTests.cs +++ b/SabreTools.Test/DatFiles/SetterTests.cs @@ -1,6 +1,3 @@ -using System.Collections.Generic; - -using SabreTools.Core; using SabreTools.DatFiles; using SabreTools.DatItems; using SabreTools.DatItems.Formats; @@ -14,10 +11,8 @@ namespace SabreTools.Test.DatFiles public void SetFieldsDatItemTest() { var datItem = CreateDatItem(); - Setter setter = new() - { - ItemFieldMappings = new Dictionary { [DatItemField.Name] = "bar" } - }; + var setter = new Setter(); + setter.PopulateSetters("datitem.name", "bar"); setter.SetFields(datItem); Assert.Equal("bar", datItem.GetName()); } @@ -26,10 +21,8 @@ namespace SabreTools.Test.DatFiles public void SetFieldsMachineTest() { var datItem = CreateDatItem(); - Setter setter = new() - { - MachineFieldMappings = new Dictionary { [MachineField.Name] = "foo" } - }; + var setter = new Setter(); + setter.PopulateSetters("machine.name", "foo"); setter.SetFields(datItem.Machine); Assert.Equal("foo", datItem.Machine.Name); } diff --git a/SabreTools/Features/Batch.cs b/SabreTools/Features/Batch.cs index 82e1bcd2..99b67edf 100644 --- a/SabreTools/Features/Batch.cs +++ b/SabreTools/Features/Batch.cs @@ -179,7 +179,7 @@ Reset the internal state: reset();"; return commandName.ToLowerInvariant() switch { - "1g1r" => new OneGamePerRegionCommand(arguments), + "1g1r" => new OneGamePerRegionCommand(arguments), "d2d" => new DFDCommand(arguments), "dfd" => new DFDCommand(arguments), "descname" => new DescriptionAsNameCommand(arguments), @@ -292,7 +292,7 @@ Reset the internal state: reset();"; dfdRemover.ApplyRemovals(batchState.DatFile); } } - + /// /// Apply an extra INI /// @@ -341,17 +341,12 @@ Reset the internal state: reset();"; public override void Process(BatchState batchState) { // Read in the individual arguments - MachineField extraMachineField = Arguments[0].AsMachineField(); - DatItemField extraDatItemField = Arguments[0].AsDatItemField(); + (string?, string?) fieldName = SabreTools.Filter.FilterParser.ParseFilterId(Arguments[0]); string extraFile = Arguments[1]; // Create the extra INI - ExtraIni extraIni = new(); - ExtraIniItem extraIniItem = new() - { - MachineField = extraMachineField, - ItemField = extraDatItemField, - }; + var extraIni = new ExtraIni(); + var extraIniItem = new ExtraIniItem() { FieldName = fieldName }; extraIniItem.PopulateFromFile(extraFile); extraIni.Items.Add(extraIniItem); @@ -446,7 +441,7 @@ Reset the internal state: reset();"; // Cleanup after the filter // TODO: We might not want to remove immediately - batchState.DatFile.Items.ClearMarked(); + batchState.DatFile.Items.ClearMarked(); batchState.DatFile.Items.ClearEmpty(); } } @@ -621,7 +616,7 @@ Reset the internal state: reset();"; /// public override void Process(BatchState batchState) { - Cleaner ogorCleaner = new() { OneGamePerRegion = true, RegionList = Arguments }; + Cleaner ogorCleaner = new() { OneGamePerRegion = true, RegionList = Arguments }; ogorCleaner.ApplyCleaning(batchState.DatFile); } } @@ -655,7 +650,7 @@ Reset the internal state: reset();"; /// public override void Process(BatchState batchState) { - Cleaner orpgCleaner = new() { OneRomPerGame = true }; + Cleaner orpgCleaner = new() { OneRomPerGame = true }; orpgCleaner.ApplyCleaning(batchState.DatFile); } } @@ -790,7 +785,7 @@ Reset the internal state: reset();"; /// public override void Process(BatchState batchState) { - Cleaner stripCleaner = new() { SceneDateStrip = true }; + Cleaner stripCleaner = new() { SceneDateStrip = true }; stripCleaner.ApplyCleaning(batchState.DatFile); } } @@ -836,7 +831,7 @@ Reset the internal state: reset();"; // Read in the individual arguments DatHeaderField field = Arguments[0].AsDatHeaderField(); string value = Arguments[1]; - + // Set the header field batchState.DatFile.Header.SetFields(new Dictionary { [field] = value }); }