mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Clean up Extra INI operations
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using SabreTools.Core;
|
using SabreTools.Core;
|
||||||
using SabreTools.Core.Tools;
|
using SabreTools.Core.Tools;
|
||||||
using SabreTools.DatFiles;
|
using SabreTools.DatFiles;
|
||||||
@@ -88,67 +88,38 @@ namespace SabreTools.Filtering
|
|||||||
// Bucket by game first
|
// Bucket by game first
|
||||||
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None);
|
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None);
|
||||||
|
|
||||||
// Create a new set of mappings based on the items
|
// Create mappings based on the extra items
|
||||||
var machineMap = new Dictionary<string, Dictionary<MachineField, string>>();
|
var combinedMachineMaps = CombineMachineExtras();
|
||||||
var datItemMap = new Dictionary<string, Dictionary<DatItemField, string>>();
|
var combinedDatItemMaps = CombineDatItemExtras();
|
||||||
|
|
||||||
// Loop through each of the extras
|
// Now get the combined set of keys
|
||||||
foreach (ExtraIniItem item in Items)
|
var machines = combinedMachineMaps.Keys.Concat(combinedDatItemMaps.Keys).Distinct();
|
||||||
{
|
|
||||||
foreach (var mapping in item.Mappings)
|
|
||||||
{
|
|
||||||
string key = mapping.Key;
|
|
||||||
List<string> machineNames = mapping.Value;
|
|
||||||
|
|
||||||
// Loop through the machines and add the new mappings
|
// Apply the mappings
|
||||||
foreach (string machine in machineNames)
|
foreach (string machine in machines)
|
||||||
{
|
|
||||||
if (item.MachineField != MachineField.NULL)
|
|
||||||
{
|
|
||||||
if (!machineMap.ContainsKey(machine))
|
|
||||||
machineMap[machine] = new Dictionary<MachineField, string>();
|
|
||||||
|
|
||||||
machineMap[machine][item.MachineField] = key;
|
|
||||||
}
|
|
||||||
else if (item.DatItemField != DatItemField.NULL)
|
|
||||||
{
|
|
||||||
if (!datItemMap.ContainsKey(machine))
|
|
||||||
datItemMap[machine] = new Dictionary<DatItemField, string>();
|
|
||||||
|
|
||||||
datItemMap[machine][item.DatItemField] = key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now apply the new set of Machine mappings
|
|
||||||
foreach (string key in machineMap.Keys)
|
|
||||||
{
|
{
|
||||||
// If the key doesn't exist, continue
|
// If the key doesn't exist, continue
|
||||||
if (!datFile.Items.ContainsKey(key))
|
if (!datFile.Items.ContainsKey(machine))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
List<DatItem> datItems = datFile.Items[key];
|
// Get the list of DatItems for the machine
|
||||||
Setter setter = new Setter { MachineMappings = machineMap[key] };
|
List<DatItem> datItems = datFile.Items[machine];
|
||||||
|
|
||||||
|
// Try to get the map values, if possible
|
||||||
|
combinedMachineMaps.TryGetValue(machine, out Dictionary<MachineField, string> machineMappings);
|
||||||
|
combinedDatItemMaps.TryGetValue(machine, out Dictionary<DatItemField, string> 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)
|
foreach (var datItem in datItems)
|
||||||
{
|
{
|
||||||
setter.SetFields(datItem.Machine);
|
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<DatItem> datItems = datFile.Items[key];
|
|
||||||
Setter setter = new Setter { DatItemMappings = datItemMap[key] };
|
|
||||||
|
|
||||||
foreach (var datItem in datItems)
|
|
||||||
{
|
|
||||||
setter.SetFields(datItem);
|
setter.SetFields(datItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,6 +133,58 @@ namespace SabreTools.Filtering
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Combine MachineField-based ExtraIni fields
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Mapping dictionary from machine name to field mapping</returns>
|
||||||
|
private Dictionary<string, Dictionary<MachineField, string>> CombineMachineExtras()
|
||||||
|
{
|
||||||
|
var machineMap = new Dictionary<string, Dictionary<MachineField, string>>();
|
||||||
|
|
||||||
|
// 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<MachineField, string>
|
||||||
|
{
|
||||||
|
[item.MachineField] = value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return machineMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Combine DatItemField-based ExtraIni fields
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Mapping dictionary from machine name to field mapping</returns>
|
||||||
|
private Dictionary<string, Dictionary<DatItemField, string>> CombineDatItemExtras()
|
||||||
|
{
|
||||||
|
var datItemMap = new Dictionary<string, Dictionary<DatItemField, string>>();
|
||||||
|
|
||||||
|
// 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<DatItemField, string>()
|
||||||
|
{
|
||||||
|
[item.DatItemField] = value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return datItemMap;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ namespace SabreTools.Filtering
|
|||||||
public DatItemField DatItemField { get; set; } = DatItemField.NULL;
|
public DatItemField DatItemField { get; set; } = DatItemField.NULL;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Mappings from value to machine name
|
/// Mappings from machine names to value
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Dictionary<string, List<string>> Mappings { get; set; } = new Dictionary<string, List<string>>();
|
public Dictionary<string, string> Mappings { get; set; } = new Dictionary<string, string>();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -77,22 +77,18 @@ namespace SabreTools.Filtering
|
|||||||
// If we have a value, then we start populating the dictionary
|
// If we have a value, then we start populating the dictionary
|
||||||
else if (foundRootFolder)
|
else if (foundRootFolder)
|
||||||
{
|
{
|
||||||
// Get the key and value
|
// Get the value and machine name
|
||||||
string key = ir.Section;
|
string value = ir.Section;
|
||||||
string value = ir.CurrentLine.Trim();
|
string machineName = ir.CurrentLine.Trim();
|
||||||
|
|
||||||
// If the section is "ROOT_FOLDER", then we use the value "true" instead.
|
// 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
|
// This is done because some INI files use the name of the file as the
|
||||||
// category to be assigned to the items included.
|
// category to be assigned to the items included.
|
||||||
if (key == "ROOT_FOLDER")
|
if (value == "ROOT_FOLDER")
|
||||||
key = "true";
|
value = "true";
|
||||||
|
|
||||||
// Ensure the key exists
|
|
||||||
if (!Mappings.ContainsKey(key))
|
|
||||||
Mappings[key] = new List<string>();
|
|
||||||
|
|
||||||
// Add the new mapping
|
// Add the new mapping
|
||||||
Mappings[key].Add(value);
|
Mappings[machineName] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user