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();
}
/// <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>
/// Set remover from a value
/// </summary>

View File

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

View File

@@ -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, string> { [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, string> { [MachineField.Name] = "foo" }
};
var setter = new Setter();
setter.PopulateSetters("machine.name", "foo");
setter.SetFields(datItem.Machine);
Assert.Equal("foo", datItem.Machine.Name);
}

View File

@@ -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);
}
}
/// <summary>
/// Apply an extra INI
/// </summary>
@@ -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();";
/// <inheritdoc/>
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();";
/// <inheritdoc/>
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();";
/// <inheritdoc/>
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<DatHeaderField, string> { [field] = value });
}