Fix AddRomsFromDevices

This commit is contained in:
Matt Nadareski
2020-09-03 20:58:10 -07:00
parent 45fb1ebfe7
commit 180dcb13d6

View File

@@ -1341,126 +1341,125 @@ namespace SabreTools.Library.DatFiles
/// Use device_ref and optionally slotoption tags to add roms to the children /// Use device_ref and optionally slotoption tags to add roms to the children
/// </summary> /// </summary>
/// <param name="dev">True if only child device sets are touched, false for non-device sets (default)</param> /// <param name="dev">True if only child device sets are touched, false for non-device sets (default)</param>
/// <param name="slotoptions">True if slotoptions tags are used as well, false otherwise</param> /// <param name="useSlotOptions">True if slotoptions tags are used as well, false otherwise</param>
private bool AddRomsFromDevices(bool dev = false, bool slotoptions = false) private bool AddRomsFromDevices(bool dev = false, bool useSlotOptions = false)
{ {
bool foundnew = false; bool foundnew = false;
List<string> games = Items.Keys.OrderBy(g => g).ToList(); List<string> machines = Items.Keys.OrderBy(g => g).ToList();
foreach (string game in games) foreach (string machine in machines)
{ {
// If the game doesn't have items, we continue // If the machine doesn't have items, we continue
if (Items[game] == null || Items[game].Count == 0) if (Items[machine] == null || Items[machine].Count == 0)
continue; continue;
// If the game (is/is not) a device, we want to continue // If the machine (is/is not) a device, we want to continue
if (dev ^ (Items[game][0].Machine.MachineType.HasFlag(MachineType.Device))) if (dev ^ (Items[machine][0].Machine.MachineType.HasFlag(MachineType.Device)))
continue; continue;
// If the game has no devices, mark it // Get all device reference names from the current machine
bool devices = true; List<string> deviceReferences = Items[machine]
if (Items[game].Count(i => i.ItemType == ItemType.DeviceReference) == 0)
{
devices = false;
}
// If we're checking devices
if (devices)
{
// Determine if the game has any devices or not
List<string> deviceReferences = Items[game]
.Where(i => i.ItemType == ItemType.DeviceReference) .Where(i => i.ItemType == ItemType.DeviceReference)
.Select(i => (i as DeviceReference).Name) .Select(i => i as DeviceReference)
.Select(dr => dr.Name)
.Distinct()
.ToList(); .ToList();
List<string> newdevs = new List<string>();
// Get all slot option names from the current machine
List<string> slotOptions = Items[machine]
.Where(i => i.ItemType == ItemType.Slot)
.Select(i => i as Slot)
.Where(s => s.SlotOptions != null && s.SlotOptions.Count != 0)
.SelectMany(s => s.SlotOptions)
.Select(so => so.DeviceName)
.Distinct()
.ToList();
// If we're checking device references
if (deviceReferences.Any())
{
// Loop through all names and check the corresponding machines
List<string> newDeviceReferences = new List<string>();
foreach (string deviceReference in deviceReferences) foreach (string deviceReference in deviceReferences)
{ {
// If the device doesn't exist then we continue // If the machine doesn't exist then we continue
if (Items[deviceReference] == null || Items[deviceReference].Count == 0) if (Items[deviceReference] == null || Items[deviceReference].Count == 0)
continue; continue;
// Otherwise, copy the items from the device to the current game // Add to the list of new device reference names
DatItem copyFrom = Items[game][0];
List<DatItem> devItems = Items[deviceReference]; List<DatItem> devItems = Items[deviceReference];
newdevs.AddRange((Items[deviceReference] ?? new List<DatItem>()) newDeviceReferences.AddRange(devItems
.Where(i => i.ItemType == ItemType.DeviceReference) .Where(i => i.ItemType == ItemType.DeviceReference)
.Select(i => (i as DeviceReference).Name)); .Select(i => (i as DeviceReference).Name));
// Set new machine information and add to the current machine
DatItem copyFrom = Items[machine][0];
foreach (DatItem item in devItems) foreach (DatItem item in devItems)
{ {
// If the parent machine doesn't already contain this item, add it
if (!Items[machine].Any(i => i.ItemType == item.ItemType && i.GetName() == item.GetName()))
{
// Set that we found new items
foundnew = true;
// Clone the item and then add it
DatItem datItem = (DatItem)item.Clone(); DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom); datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.ItemType == ItemType.DeviceReference && (i as DeviceReference).Name == (datItem as DeviceReference).Name).Count() == 0) Items.Add(machine, datItem);
{
foundnew = true;
Items.Add(game, datItem);
} }
} }
} }
// Now that every device is accounted for, add the new list of devices, if they don't already exist // Now that every device reference is accounted for, add the new list of device references, if they don't already exist
foreach (string device in newdevs) foreach (string deviceReference in newDeviceReferences.Distinct())
{ {
if (!deviceReferences.Contains(device)) if (!deviceReferences.Contains(deviceReference))
Items[game].Add(new DeviceReference() { Name = device }); Items[machine].Add(new DeviceReference() { Name = deviceReference });
} }
} }
// If we're checking slotoptions // If we're checking slotoptions
if (slotoptions) if (useSlotOptions && slotOptions.Any())
{ {
// If the game has no slot options, we continue // Loop through all names and check the corresponding machines
if (Items[game]
.Where(i => i.ItemType == ItemType.Slot)
.SelectMany(s => (s as Slot).SlotOptions ?? new List<SlotOption>()).Count() == 0)
{
continue;
}
// Determine if the game has any slot options or not
List<string> slotOptions = Items[game]
.Where(i => i.ItemType == ItemType.Slot)
.Where(s => (s as Slot).SlotOptions != null && (s as Slot).SlotOptions.Count != 0)
.SelectMany(s => (s as Slot).SlotOptions)
.Select(o => o.DeviceName)
.Distinct()
.ToList();
List<string> newSlotOptions = new List<string>(); List<string> newSlotOptions = new List<string>();
foreach (string slotOption in slotOptions) foreach (string slotOption in slotOptions)
{ {
// If the slot option doesn't exist then we continue // If the machine doesn't exist then we continue
if (Items[slotOption] == null || Items[slotOption].Count == 0) if (Items[slotOption] == null || Items[slotOption].Count == 0)
continue; continue;
// Otherwise, copy the items from the slot option to the current game // Add to the list of new slot option names
DatItem copyFrom = Items[game][0]; List<DatItem> slotItems = Items[slotOption];
List<DatItem> slotOptionItems = Items[slotOption]; newSlotOptions.AddRange(slotItems
newSlotOptions.AddRange((Items[slotOption] ?? new List<DatItem>())
.Where(i => i.ItemType == ItemType.Slot) .Where(i => i.ItemType == ItemType.Slot)
.Where(s => (s as Slot).SlotOptions != null && (s as Slot).SlotOptions.Count != 0) .Where(s => (s as Slot).SlotOptions != null)
.Where(s => (s as Slot).SlotOptions.Count != 0)
.SelectMany(s => (s as Slot).SlotOptions) .SelectMany(s => (s as Slot).SlotOptions)
.Select(o => o.DeviceName)); .Select(o => o.DeviceName));
foreach (DatItem item in slotOptionItems) // Set new machine information and add to the current machine
DatItem copyFrom = Items[machine][0];
foreach (DatItem item in slotItems)
{ {
// If the parent machine doesn't already contain this item, add it
if (!Items[machine].Any(i => i.ItemType == item.ItemType && i.GetName() == item.GetName()))
{
// Set that we found new items
foundnew = true;
// Clone the item and then add it
DatItem datItem = (DatItem)item.Clone(); DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom); datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.ItemType == ItemType.Slot && (i as Slot).Name == (datItem as Slot).Name).Count() == 0) Items.Add(machine, datItem);
{
foundnew = true;
Items.Add(game, datItem);
} }
} }
} }
// Ensure we don't have unnecessary duplicates // Now that every device is accounted for, add the new list of slot options, if they don't already exist
newSlotOptions = newSlotOptions.Distinct().ToList(); foreach (string slotOption in newSlotOptions.Distinct())
// Now that every slot option is accounted for, add the new list of options, if they don't already exist
foreach (string slotOption in newSlotOptions)
{ {
if (!slotOptions.Contains(slotOption)) if (!slotOptions.Contains(slotOption))
Items[game].Add(new Slot() { SlotOptions = new List<SlotOption> { new SlotOption { DeviceName = slotOption } } }); Items[machine].Add(new Slot() { SlotOptions = new List<SlotOption> { new SlotOption { DeviceName = slotOption } } });
} }
} }
} }