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)) if (item.ContainsKey(Models.Metadata.Machine.DeviceRefKey))
{ {
var items = item.ReadItemArray<Models.Metadata.DeviceRef>(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)) if (item.ContainsKey(Models.Metadata.Machine.DipSwitchKey))
{ {
@@ -361,7 +361,7 @@ namespace SabreTools.DatFiles
if (item.ContainsKey(Models.Metadata.Machine.SlotKey)) if (item.ContainsKey(Models.Metadata.Machine.SlotKey))
{ {
var items = item.ReadItemArray<Models.Metadata.Slot>(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)) 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="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="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="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filterRunner">Optional FilterRunner to filter items on parse</param> /// <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, FilterRunner? filterRunner) 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 the array is null or empty, return without processing
if (items == null || items.Length == 0) if (items == null || items.Length == 0)
@@ -591,10 +591,6 @@ namespace SabreTools.DatFiles
// Loop through the items and add // Loop through the items and add
foreach (var item in items) 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); var datItem = new DeviceRef(item);
datItem.SetFieldValue<Source?>(DatItem.SourceKey, source); datItem.SetFieldValue<Source?>(DatItem.SourceKey, source);
datItem.CopyMachineInformation(machine); datItem.CopyMachineInformation(machine);
@@ -1300,8 +1296,8 @@ namespace SabreTools.DatFiles
/// <param name="source">Source to use with the converted items</param> /// <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="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="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filterRunner">Optional FilterRunner to filter items on parse</param> /// <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, FilterRunner? filterRunner) 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 the array is null or empty, return without processing
if (items == null || items.Length == 0) if (items == null || items.Length == 0)
@@ -1310,10 +1306,6 @@ namespace SabreTools.DatFiles
// Loop through the items and add // Loop through the items and add
foreach (var item in items) 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); var datItem = new Slot(item);
datItem.SetFieldValue<Source?>(DatItem.SourceKey, source); datItem.SetFieldValue<Source?>(DatItem.SourceKey, source);
datItem.CopyMachineInformation(machine); datItem.CopyMachineInformation(machine);

View File

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