Fix ExtraIni setter issues

This commit is contained in:
Matt Nadareski
2024-03-05 17:33:02 -05:00
parent 6987ec2641
commit 3ec85cf04a
4 changed files with 49 additions and 99 deletions

View File

@@ -79,6 +79,31 @@ namespace SabreTools.DatFiles
watch.Stop(); watch.Stop();
} }
/// <summary>
/// Populate the setters using a set of field names
/// </summary>
/// <param name="mappings">Dictionary of mappings</param>
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();
}
/// <summary> /// <summary>
/// Set remover from a value /// Set remover from a value
/// </summary> /// </summary>

View File

@@ -100,11 +100,8 @@ namespace SabreTools.Filtering
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None); datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None);
// Create mappings based on the extra items // Create mappings based on the extra items
var combinedMachineMaps = CombineMachineExtras(); var combinedMaps = CombineExtras();
var combinedDatItemMaps = CombineDatItemExtras(); var machines = combinedMaps.Keys;
// Now get the combined set of keys
var machines = combinedMachineMaps.Keys.Concat(combinedDatItemMaps.Keys).Distinct();
// Apply the mappings // Apply the mappings
foreach (string machine in machines) foreach (string machine in machines)
@@ -119,16 +116,11 @@ namespace SabreTools.Filtering
continue; continue;
// Try to get the map values, if possible // Try to get the map values, if possible
combinedMachineMaps.TryGetValue(machine, out var machineMappings); combinedMaps.TryGetValue(machine, out var mappings);
combinedDatItemMaps.TryGetValue(machine, out var datItemMappings);
// Create a setter with the new mappings // Create a setter with the new mappings
var setter = new Setter(); var setter = new Setter();
setter.PopulateSettersFromList() setter.PopulateSettersFromDictionary(mappings);
{
MachineFieldMappings = machineMappings,
ItemFieldMappings = datItemMappings,
};
// Loop through and set the fields accordingly // Loop through and set the fields accordingly
foreach (var datItem in datItems) foreach (var datItem in datItems)
@@ -155,83 +147,28 @@ namespace SabreTools.Filtering
/// Combine ExtraIni fields /// Combine ExtraIni fields
/// </summary> /// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns> /// <returns>Mapping dictionary from machine name to field mapping</returns>
private (List<string> Keys, List<string> Values) CombineExtras() private Dictionary<string, Dictionary<(string, string), string>> CombineExtras()
{ {
var keys = new List<string>(); var machineMap = new Dictionary<string, Dictionary<(string, string), string>>();
var values = new List<string>();
// Loop through each of the extras // Loop through each of the extras
foreach (ExtraIniItem item in Items) foreach (ExtraIniItem item in Items)
{ {
foreach (var mapping in item.Mappings) foreach (var mapping in item.Mappings)
{ {
string machineName = mapping.Key; string machineName = mapping.Key;
string value = mapping.Value; string value = mapping.Value;
mapping[machineName] = new Dictionary<string, string> if (!machineMap.ContainsKey(machineName))
{ machineMap[machineName] = [];
[item.FieldName!] = value,
};
}
}
return mapping; machineMap[machineName][item.FieldName!] = value;
}
/// <summary>
/// Combine MachineField-based ExtraIni fields
/// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns>
private Dictionary<string, Dictionary<string, string>> CombineMachineExtras()
{
var machineMap = new Dictionary<string, Dictionary<string, string>>();
// 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<string, string>
{
[item.MachineField!] = value,
};
} }
} }
return machineMap; return machineMap;
} }
/// <summary>
/// Combine DatItemField-based ExtraIni fields
/// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns>
private Dictionary<string, Dictionary<string, string>> CombineDatItemExtras()
{
var datItemMap = new Dictionary<string, Dictionary<string, string>>();
// 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<string, string>()
{
[item.ItemField!] = value,
};
}
}
return datItemMap;
}
#endregion #endregion
} }
} }

View File

@@ -1,6 +1,3 @@
using System.Collections.Generic;
using SabreTools.Core;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
@@ -14,10 +11,8 @@ namespace SabreTools.Test.DatFiles
public void SetFieldsDatItemTest() public void SetFieldsDatItemTest()
{ {
var datItem = CreateDatItem(); var datItem = CreateDatItem();
Setter setter = new() var setter = new Setter();
{ setter.PopulateSetters("datitem.name", "bar");
ItemFieldMappings = new Dictionary<DatItemField, string> { [DatItemField.Name] = "bar" }
};
setter.SetFields(datItem); setter.SetFields(datItem);
Assert.Equal("bar", datItem.GetName()); Assert.Equal("bar", datItem.GetName());
} }
@@ -26,10 +21,8 @@ namespace SabreTools.Test.DatFiles
public void SetFieldsMachineTest() public void SetFieldsMachineTest()
{ {
var datItem = CreateDatItem(); var datItem = CreateDatItem();
Setter setter = new() var setter = new Setter();
{ setter.PopulateSetters("machine.name", "foo");
MachineFieldMappings = new Dictionary<MachineField, string> { [MachineField.Name] = "foo" }
};
setter.SetFields(datItem.Machine); setter.SetFields(datItem.Machine);
Assert.Equal("foo", datItem.Machine.Name); Assert.Equal("foo", datItem.Machine.Name);
} }

View File

@@ -341,17 +341,12 @@ Reset the internal state: reset();";
public override void Process(BatchState batchState) public override void Process(BatchState batchState)
{ {
// Read in the individual arguments // Read in the individual arguments
MachineField extraMachineField = Arguments[0].AsMachineField(); (string?, string?) fieldName = SabreTools.Filter.FilterParser.ParseFilterId(Arguments[0]);
DatItemField extraDatItemField = Arguments[0].AsDatItemField();
string extraFile = Arguments[1]; string extraFile = Arguments[1];
// Create the extra INI // Create the extra INI
ExtraIni extraIni = new(); var extraIni = new ExtraIni();
ExtraIniItem extraIniItem = new() var extraIniItem = new ExtraIniItem() { FieldName = fieldName };
{
MachineField = extraMachineField,
ItemField = extraDatItemField,
};
extraIniItem.PopulateFromFile(extraFile); extraIniItem.PopulateFromFile(extraFile);
extraIni.Items.Add(extraIniItem); extraIni.Items.Add(extraIniItem);