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

@@ -77,7 +77,7 @@ namespace SabreTools.Filtering
} }
#endregion #endregion
#region Running #region Running
/// <summary> /// <summary>
@@ -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>
{
[item.FieldName!] = value,
};
}
}
return mapping; if (!machineMap.ContainsKey(machineName))
} machineMap[machineName] = [];
/// <summary> machineMap[machineName][item.FieldName!] = value;
/// 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

@@ -179,7 +179,7 @@ Reset the internal state: reset();";
return commandName.ToLowerInvariant() switch return commandName.ToLowerInvariant() switch
{ {
"1g1r" => new OneGamePerRegionCommand(arguments), "1g1r" => new OneGamePerRegionCommand(arguments),
"d2d" => new DFDCommand(arguments), "d2d" => new DFDCommand(arguments),
"dfd" => new DFDCommand(arguments), "dfd" => new DFDCommand(arguments),
"descname" => new DescriptionAsNameCommand(arguments), "descname" => new DescriptionAsNameCommand(arguments),
@@ -292,7 +292,7 @@ Reset the internal state: reset();";
dfdRemover.ApplyRemovals(batchState.DatFile); dfdRemover.ApplyRemovals(batchState.DatFile);
} }
} }
/// <summary> /// <summary>
/// Apply an extra INI /// Apply an extra INI
/// </summary> /// </summary>
@@ -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);
@@ -446,7 +441,7 @@ Reset the internal state: reset();";
// Cleanup after the filter // Cleanup after the filter
// TODO: We might not want to remove immediately // TODO: We might not want to remove immediately
batchState.DatFile.Items.ClearMarked(); batchState.DatFile.Items.ClearMarked();
batchState.DatFile.Items.ClearEmpty(); batchState.DatFile.Items.ClearEmpty();
} }
} }
@@ -621,7 +616,7 @@ Reset the internal state: reset();";
/// <inheritdoc/> /// <inheritdoc/>
public override void Process(BatchState batchState) public override void Process(BatchState batchState)
{ {
Cleaner ogorCleaner = new() { OneGamePerRegion = true, RegionList = Arguments }; Cleaner ogorCleaner = new() { OneGamePerRegion = true, RegionList = Arguments };
ogorCleaner.ApplyCleaning(batchState.DatFile); ogorCleaner.ApplyCleaning(batchState.DatFile);
} }
} }
@@ -655,7 +650,7 @@ Reset the internal state: reset();";
/// <inheritdoc/> /// <inheritdoc/>
public override void Process(BatchState batchState) public override void Process(BatchState batchState)
{ {
Cleaner orpgCleaner = new() { OneRomPerGame = true }; Cleaner orpgCleaner = new() { OneRomPerGame = true };
orpgCleaner.ApplyCleaning(batchState.DatFile); orpgCleaner.ApplyCleaning(batchState.DatFile);
} }
} }
@@ -790,7 +785,7 @@ Reset the internal state: reset();";
/// <inheritdoc/> /// <inheritdoc/>
public override void Process(BatchState batchState) public override void Process(BatchState batchState)
{ {
Cleaner stripCleaner = new() { SceneDateStrip = true }; Cleaner stripCleaner = new() { SceneDateStrip = true };
stripCleaner.ApplyCleaning(batchState.DatFile); stripCleaner.ApplyCleaning(batchState.DatFile);
} }
} }
@@ -836,7 +831,7 @@ Reset the internal state: reset();";
// Read in the individual arguments // Read in the individual arguments
DatHeaderField field = Arguments[0].AsDatHeaderField(); DatHeaderField field = Arguments[0].AsDatHeaderField();
string value = Arguments[1]; string value = Arguments[1];
// Set the header field // Set the header field
batchState.DatFile.Header.SetFields(new Dictionary<DatHeaderField, string> { [field] = value }); batchState.DatFile.Header.SetFields(new Dictionary<DatHeaderField, string> { [field] = value });
} }