Fix the mess that is devices and slots

This commit is contained in:
Matt Nadareski
2025-05-14 12:48:14 -04:00
parent e0868632ff
commit 4a5c4d104d
2 changed files with 24 additions and 27 deletions

View File

@@ -275,7 +275,7 @@ namespace SabreTools.DatFiles
if (item.ContainsKey(Models.Metadata.Machine.DeviceRefKey))
{
var items = item.ReadItemArray<Models.Metadata.DeviceRef>(Models.Metadata.Machine.DeviceRefKey);
ProcessItems(items, machine, machineIndex: 0, source, sourceIndex, statsOnly, filterRunner);
ProcessItems(items, machine, machineIndex: 0, source, sourceIndex, statsOnly);
}
if (item.ContainsKey(Models.Metadata.Machine.DipSwitchKey))
{
@@ -361,7 +361,7 @@ namespace SabreTools.DatFiles
if (item.ContainsKey(Models.Metadata.Machine.SlotKey))
{
var items = item.ReadItemArray<Models.Metadata.Slot>(Models.Metadata.Machine.SlotKey);
ProcessItems(items, machine, machineIndex: 0, source, sourceIndex, statsOnly, filterRunner);
ProcessItems(items, machine, machineIndex: 0, source, sourceIndex, statsOnly);
}
if (item.ContainsKey(Models.Metadata.Machine.SoftwareListKey))
{
@@ -581,8 +581,8 @@ namespace SabreTools.DatFiles
/// <param name="source">Source to use with the converted items</param>
/// <param name="sourceIndex">Index of the Source to use with the converted items</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filterRunner">Optional FilterRunner to filter items on parse</param>
private void ProcessItems(Models.Metadata.DeviceRef[]? items, Machine machine, long machineIndex, Source source, long sourceIndex, bool statsOnly, FilterRunner? filterRunner)
/// <remarks>Does not get filtered here just in case merging or splitting is done</remarks>
private void ProcessItems(Models.Metadata.DeviceRef[]? items, Machine machine, long machineIndex, Source source, long sourceIndex, bool statsOnly)
{
// If the array is null or empty, return without processing
if (items == null || items.Length == 0)
@@ -591,10 +591,6 @@ namespace SabreTools.DatFiles
// Loop through the items and add
foreach (var item in items)
{
// If the item doesn't pass the filter
if (filterRunner != null && !filterRunner.Run(item))
continue;
var datItem = new DeviceRef(item);
datItem.SetFieldValue<Source?>(DatItem.SourceKey, source);
datItem.CopyMachineInformation(machine);
@@ -1300,8 +1296,8 @@ namespace SabreTools.DatFiles
/// <param name="source">Source to use with the converted items</param>
/// <param name="sourceIndex">Index of the Source to use with the converted items</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filterRunner">Optional FilterRunner to filter items on parse</param>
private void ProcessItems(Models.Metadata.Slot[]? items, Machine machine, long machineIndex, Source source, long sourceIndex, bool statsOnly, FilterRunner? filterRunner)
/// <remarks>Does not get filtered here just in case merging or splitting is done</remarks>
private void ProcessItems(Models.Metadata.Slot[]? items, Machine machine, long machineIndex, Source source, long sourceIndex, bool statsOnly)
{
// If the array is null or empty, return without processing
if (items == null || items.Length == 0)
@@ -1310,10 +1306,6 @@ namespace SabreTools.DatFiles
// Loop through the items and add
foreach (var item in items)
{
// If the item doesn't pass the filter
if (filterRunner != null && !filterRunner.Run(item))
continue;
var datItem = new Slot(item);
datItem.SetFieldValue<Source?>(DatItem.SourceKey, source);
datItem.CopyMachineInformation(machine);

View File

@@ -276,6 +276,7 @@ namespace SabreTools.DatFiles
copyFrom = parentItems[0];
}
items = GetItemsForBucket(bucket);
foreach (DatItem item in items)
{
// Special disk handling
@@ -398,6 +399,7 @@ namespace SabreTools.DatFiles
if (cloneOfMachine.Value == null)
continue;
items = GetItemsForBucketDB(bucket);
foreach (var item in items)
{
// Get the source for the current item
@@ -640,26 +642,24 @@ namespace SabreTools.DatFiles
if (deviceOnly ^ (datItems[0].GetMachine()!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
continue;
// Get the first item for the machine
// Get the first item from the bucket
DatItem copyFrom = datItems[0];
// Get all device reference names from the current machine
List<string?> deviceReferences = datItems
HashSet<string?> deviceReferences = [];
deviceReferences.UnionWith(datItems
.FindAll(i => i is DeviceRef)
.ConvertAll(i => i as DeviceRef)
.ConvertAll(dr => dr!.GetName())
.Distinct()
.ToList();
.ConvertAll(dr => dr!.GetName()));
// Get all slot option names from the current machine
List<string?> slotOptions = datItems
.FindAll(i => i is Slot)
.ConvertAll(i => i as Slot)
.FindAll(s => s!.SlotOptionsSpecified)
.SelectMany(s => s!.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
.Select(so => so.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey))
.Distinct()
.ToList();
HashSet<string?> slotOptions = [];
slotOptions.UnionWith(datItems
.FindAll(i => i is Slot)
.ConvertAll(i => i as Slot)
.FindAll(s => s!.SlotOptionsSpecified)
.SelectMany(s => s!.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
.Select(so => so.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey)));
// If we're checking device references
if (deviceReferences.Count > 0)
@@ -689,6 +689,7 @@ namespace SabreTools.DatFiles
// Clone the item and then add it
DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom);
datItems.Add(datItem);
AddItem(datItem, statsOnly: false);
}
}
@@ -699,6 +700,7 @@ namespace SabreTools.DatFiles
{
if (!deviceReferences.Contains(deviceReference))
{
deviceReferences.Add(deviceReference);
var deviceRef = new DeviceRef();
deviceRef.SetName(deviceReference);
deviceRef.CopyMachineInformation(copyFrom);
@@ -737,6 +739,7 @@ namespace SabreTools.DatFiles
// Clone the item and then add it
DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom);
datItems.Add(datItem);
AddItem(datItem, statsOnly: false);
}
}
@@ -747,8 +750,10 @@ namespace SabreTools.DatFiles
{
if (!slotOptions.Contains(slotOption))
{
slotOptions.Add(slotOption);
var slotOptionItem = new SlotOption();
slotOptionItem.SetFieldValue<string?>(Models.Metadata.SlotOption.DevNameKey, slotOption);
slotOptionItem.CopyMachineInformation(copyFrom);
var slotItem = new Slot();
slotItem.SetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey, [slotOptionItem]);