Files
SabreTools/SabreTools.DatFiles/DatFile.Splitting.cs

1417 lines
60 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
#if NET40_OR_GREATER || NETCOREAPP
using System.Threading.Tasks;
using SabreTools.Core;
#endif
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles
{
public partial class DatFile
{
#region Splitting
/// <summary>
/// Use cdevice_ref tags to get full non-merged sets and remove parenting tags
/// </summary>
/// <remarks>This is a destructive process and items will be removed</remarks>
public void ApplyDeviceNonMerged()
{
_logger.User("Creating device non-merged sets from the DAT");
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, norename: true);
// Now we want to loop through all of the games and set the correct information
while (AddItemsFromDevices(false, false)) ;
while (AddItemsFromDevices(true, false)) ;
// Then, remove the romof and cloneof tags so it's not picked up by the manager
RemoveMachineRelationshipTags();
}
/// <summary>
/// Use cloneof tags to create merged sets and remove the tags plus deduplicating if tags don't catch everything
/// </summary>
/// <remarks>This is a destructive process and items will be removed</remarks>
public void ApplyFullyMerged()
{
_logger.User("Creating fully merged sets from the DAT");
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, norename: true);
// Now we want to loop through all of the games and set the correct information
AddItemsFromChildren(true, false);
// Now that we have looped through the cloneof tags, we loop through the romof tags
RemoveItemsFromRomOfChild();
// Finally, remove the romof and cloneof tags so it's not picked up by the manager
RemoveMachineRelationshipTags();
}
/// <summary>
/// Use cloneof tags to create non-merged sets and remove the tags plus using the device_ref tags to get full sets
/// </summary>
/// <remarks>This is a destructive process and items will be removed</remarks>
public void ApplyFullyNonMerged()
{
_logger.User("Creating fully non-merged sets from the DAT");
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, norename: true);
// Now we want to loop through all of the games and set the correct information
while (AddItemsFromDevices(true, true)) ;
AddItemsFromDevices(false, true);
AddItemsFromCloneOfParent();
// Now that we have looped through the cloneof tags, we loop through the romof tags
AddItemsFromRomOfParent();
// Then, remove the romof and cloneof tags so it's not picked up by the manager
RemoveMachineRelationshipTags();
}
/// <summary>
/// Use cloneof tags to create merged sets and remove the tags
/// </summary>
/// <remarks>This is a destructive process and items will be removed</remarks>
public void ApplyMerged()
{
_logger.User("Creating merged sets from the DAT");
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, norename: true);
// Now we want to loop through all of the games and set the correct information
AddItemsFromChildren(true, true);
// Now that we have looped through the cloneof tags, we loop through the romof tags
RemoveItemsFromRomOfChild();
// Finally, remove the romof and cloneof tags so it's not picked up by the manager
RemoveMachineRelationshipTags();
}
/// <summary>
/// Use cloneof tags to create non-merged sets and remove the tags
/// </summary>
/// <remarks>This is a destructive process and items will be removed</remarks>
public void ApplyNonMerged()
{
_logger.User("Creating non-merged sets from the DAT");
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, norename: true);
// Now we want to loop through all of the games and set the correct information
AddItemsFromCloneOfParent();
// Now that we have looped through the cloneof tags, we loop through the romof tags
RemoveItemsFromRomOfChild();
// Finally, remove the romof and cloneof tags so it's not picked up by the manager
RemoveMachineRelationshipTags();
}
/// <summary>
/// Use cloneof and romof tags to create split sets and remove the tags
/// </summary>
/// <remarks>This is a destructive process and items will be removed</remarks>
public void ApplySplit()
{
_logger.User("Creating split sets from the DAT");
// For sake of ease, the first thing we want to do is bucket by game
BucketBy(ItemKey.Machine, norename: true);
// Now we want to loop through all of the games and set the correct information
RemoveItemsFromCloneOfChild();
// Now that we have looped through the cloneof tags, we loop through the romof tags
RemoveItemsFromRomOfChild();
// Finally, remove the romof and cloneof tags so it's not picked up by the manager
RemoveMachineRelationshipTags();
}
#endregion
#region Splitting Steps
/// <summary>
2025-01-13 10:45:42 -05:00
/// Use cloneof tags to add items to the parents, removing the child sets in the process
/// </summary>
/// <param name="subfolder">True to add DatItems to subfolder of parent (not including Disk), false otherwise</param>
/// <param name="skipDedup">True to skip checking for duplicate ROMs in parent, false otherwise</param>
2025-01-13 13:05:53 -05:00
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void AddItemsFromChildren(bool subfolder, bool skipDedup)
{
2025-01-13 10:45:42 -05:00
AddItemsFromChildrenImpl(subfolder, skipDedup);
AddItemsFromChildrenImplDB(subfolder, skipDedup);
}
2025-01-13 13:49:45 -05:00
/// <summary>
/// Use cloneof tags to add items to the children, setting the new romof tag in the process
/// </summary>
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void AddItemsFromCloneOfParent()
2025-01-13 13:49:45 -05:00
{
AddItemsFromCloneOfParentImpl();
AddItemsFromCloneOfParentImplDB();
}
/// <summary>
2025-01-13 10:45:42 -05:00
/// Use device_ref and optionally slotoption tags to add items to the children
/// </summary>
/// <param name="deviceOnly">True if only child device sets are touched, false for non-device sets</param>
/// <param name="useSlotOptions">True if slotoptions tags are used as well, false otherwise</param>
2025-01-13 13:05:53 -05:00
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal bool AddItemsFromDevices(bool deviceOnly, bool useSlotOptions)
{
bool foundnew = AddItemsFromDevicesImpl(deviceOnly, useSlotOptions);
foundnew |= AddItemsFromDevicesImplDB(deviceOnly, useSlotOptions);
return foundnew;
}
2025-01-13 13:43:38 -05:00
/// <summary>
/// Use romof tags to add items to the children
/// </summary>
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void AddItemsFromRomOfParent()
2025-01-13 13:43:38 -05:00
{
AddItemsFromRomOfParentImpl();
AddItemsFromRomOfParentImplDB();
}
/// <summary>
/// Remove all BIOS and device sets
/// </summary>
2025-01-13 13:05:53 -05:00
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void RemoveBiosAndDeviceSets()
{
RemoveBiosAndDeviceSetsImpl();
RemoveBiosAndDeviceSetsImplDB();
}
/// <summary>
2025-01-13 13:02:17 -05:00
/// Use cloneof tags to remove items from the children
/// </summary>
2025-01-13 13:05:53 -05:00
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void RemoveItemsFromCloneOfChild()
{
2025-01-13 13:02:17 -05:00
RemoveItemsFromCloneOfChildImpl();
RemoveItemsFromCloneOfChildImplDB();
}
/// <summary>
2025-01-13 13:02:17 -05:00
/// Use romof tags to remove bios items from children
/// </summary>
2025-01-13 13:05:53 -05:00
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void RemoveItemsFromRomOfChild()
{
2025-01-13 15:46:06 -05:00
RemoveItemsFromRomOfChildImpl();
RemoveItemsFromRomOfChildImplDB();
}
/// <summary>
2025-01-13 10:45:42 -05:00
/// Remove all romof and cloneof tags from all machines
/// </summary>
2025-01-13 13:05:53 -05:00
/// <remarks>Assumes items are bucketed by <see cref="ItemKey.Machine"/></remarks>
internal void RemoveMachineRelationshipTags()
{
RemoveMachineRelationshipTagsImpl();
RemoveMachineRelationshipTagsImplDB();
}
#endregion
#region Splitting Implementations
/// <summary>
2025-01-13 10:45:42 -05:00
/// Use cloneof tags to add items to the parents, removing the child sets in the process
/// </summary>
/// <param name="subfolder">True to add DatItems to subfolder of parent (not including Disk), false otherwise</param>
/// <param name="skipDedup">True to skip checking for duplicate ROMs in parent, false otherwise</param>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
2025-01-13 10:45:42 -05:00
private void AddItemsFromChildrenImpl(bool subfolder, bool skipDedup)
{
string[] buckets = [.. Items.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket);
if (items.Count == 0)
continue;
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = items[0].GetMachine();
if (machine == null)
continue;
// Get the cloneof parent items
string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
List<DatItem> parentItems = GetItemsForBucket(cloneOf);
if (cloneOf == null)
continue;
// Otherwise, move the items from the current game to a subfolder of the parent game
DatItem copyFrom;
if (parentItems.Count == 0)
{
copyFrom = new Rom();
2025-05-02 16:46:20 -04:00
copyFrom.GetMachine()!.SetName(cloneOf);
copyFrom.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, cloneOf);
}
else
{
copyFrom = parentItems[0];
}
2025-05-14 12:48:14 -04:00
items = GetItemsForBucket(bucket);
2025-01-13 10:52:27 -05:00
foreach (DatItem item in items)
{
// Special disk handling
if (item is Disk disk)
{
string? mergeTag = disk.GetStringFieldValue(Models.Metadata.Disk.MergeKey);
// If the merge tag exists and the parent already contains it, skip
if (mergeTag != null && GetItemsForBucket(cloneOf)
.FindAll(i => i is Disk)
2025-01-13 13:54:14 -05:00
.ConvertAll(i => (i as Disk)!.GetName())
.Contains(mergeTag))
{
continue;
}
// If the merge tag exists but the parent doesn't contain it, add to parent
else if (mergeTag != null && !GetItemsForBucket(cloneOf)
.FindAll(i => i is Disk)
2025-01-13 13:54:14 -05:00
.ConvertAll(i => (i as Disk)!.GetName())
.Contains(mergeTag))
{
disk.CopyMachineInformation(copyFrom);
2025-01-14 15:32:14 -05:00
AddItem(disk, statsOnly: false);
}
// If there is no merge tag, add to parent
2025-05-19 11:05:15 -04:00
else if (mergeTag == null && !GetItemsForBucket(cloneOf)
.FindAll(i => i is Disk)
.ConvertAll(i => (i as Disk)!.GetName())
.Contains(disk.GetName()))
{
disk.CopyMachineInformation(copyFrom);
2025-01-14 15:32:14 -05:00
AddItem(disk, statsOnly: false);
}
}
// Special rom handling
else if (item is Rom rom)
{
2025-05-14 08:52:24 -04:00
string? mergeTag = rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey);
2025-05-12 08:54:45 -04:00
// If the merge tag exists and the parent already contains it, skip
2025-05-12 08:54:45 -04:00
if (mergeTag != null && GetItemsForBucket(cloneOf)
.FindAll(i => i is Rom)
.ConvertAll(i => (i as Rom)!.GetName())
2025-05-12 08:54:45 -04:00
.Contains(mergeTag))
{
continue;
}
// If the merge tag exists but the parent doesn't contain it, add to subfolder of parent
2025-05-12 08:54:45 -04:00
else if (mergeTag != null && !GetItemsForBucket(cloneOf)
.FindAll(i => i is Rom)
.ConvertAll(i => (i as Rom)!.GetName())
2025-05-12 08:54:45 -04:00
.Contains(mergeTag))
{
if (subfolder)
2025-05-02 16:46:20 -04:00
rom.SetName($"{rom.GetMachine()!.GetName()}\\{rom.GetName()}");
rom.CopyMachineInformation(copyFrom);
2025-01-14 15:32:14 -05:00
AddItem(rom, statsOnly: false);
}
// If the parent doesn't already contain this item, add to subfolder of parent
else if (!GetItemsForBucket(cloneOf).Contains(item) || skipDedup)
{
if (subfolder)
2025-05-02 16:46:20 -04:00
rom.SetName($"{item.GetMachine()!.GetName()}\\{rom.GetName()}");
rom.CopyMachineInformation(copyFrom);
2025-01-14 15:32:14 -05:00
AddItem(rom, statsOnly: false);
}
}
// All other that would be missing to subfolder of parent
else if (!GetItemsForBucket(cloneOf).Contains(item))
{
if (subfolder)
2025-05-02 16:46:20 -04:00
item.SetName($"{item.GetMachine()!.GetName()}\\{item.GetName()}");
item.CopyMachineInformation(copyFrom);
2025-01-14 15:32:14 -05:00
AddItem(item, statsOnly: false);
}
}
// Then, remove the old game so it's not picked up by the writer
2025-01-14 15:32:14 -05:00
RemoveBucket(bucket);
}
}
/// <summary>
2025-01-13 10:45:42 -05:00
/// Use cloneof tags to add items to the parents, removing the child sets in the process
/// </summary>
/// <param name="subfolder">True to add DatItems to subfolder of parent (not including Disk), false otherwise</param>
/// <param name="skipDedup">True to skip checking for duplicate ROMs in parent, false otherwise</param>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
2025-01-13 10:45:42 -05:00
private void AddItemsFromChildrenImplDB(bool subfolder, bool skipDedup)
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
continue;
// Get the machine for the first item
var machine = GetMachineForItemDB(items.First().Key);
if (machine.Value == null)
continue;
// Get the clone parent
string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
if (string.IsNullOrEmpty(cloneOf))
continue;
// Get the clone parent machine
var cloneOfMachine = ItemsDB.GetMachine(cloneOf);
if (cloneOfMachine.Value == null)
continue;
2025-05-14 12:48:14 -04:00
items = GetItemsForBucketDB(bucket);
foreach (var item in items)
{
2025-01-14 21:02:37 -05:00
// Get the source for the current item
var source = GetSourceForItemDB(item.Key);
2025-01-14 21:02:37 -05:00
// Get the parent items and current machine name
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(cloneOf);
if (parentItems.Count == 0)
continue;
string? machineName = GetMachineForItemDB(item.Key).Value?
.GetName();
// Special disk handling
if (item.Value is Disk disk)
{
string? mergeTag = disk.GetStringFieldValue(Models.Metadata.Disk.MergeKey);
// If the merge tag exists and the parent already contains it, skip
if (mergeTag != null && GetItemsForBucketDB(cloneOf).Values
.Where(i => i is Disk)
.Select(i => (i as Disk)!.GetName())
.Contains(mergeTag))
{
continue;
}
// If the merge tag exists but the parent doesn't contain it, add to parent
else if (mergeTag != null && !GetItemsForBucketDB(cloneOf).Values
.Where(i => i is Disk)
.Select(i => (i as Disk)!.GetName())
.Contains(mergeTag))
{
2025-01-14 21:02:37 -05:00
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
// If there is no merge tag, add to parent
2025-05-19 11:05:15 -04:00
else if (mergeTag == null && !GetItemsForBucketDB(cloneOf).Values
.Where(i => i is Disk)
.Select(i => (i as Disk)!.GetName())
.Contains(disk.GetName()))
{
2025-01-14 21:02:37 -05:00
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
}
// Special rom handling
else if (item.Value is Rom rom)
{
2025-05-12 08:54:45 -04:00
string? mergeTag = rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey);
// If the merge tag exists and the parent already contains it, skip
2025-05-12 08:54:45 -04:00
if (mergeTag != null && GetItemsForBucketDB(cloneOf).Values
.Where(i => i is Rom)
.Select(i => (i as Rom)!.GetName())
2025-05-12 08:54:45 -04:00
.Contains(mergeTag))
{
continue;
}
// If the merge tag exists but the parent doesn't contain it, add to subfolder of parent
2025-05-12 08:54:45 -04:00
else if (mergeTag != null && !GetItemsForBucketDB(cloneOf).Values
.Where(i => i is Rom)
.Select(i => (i as Rom)!.GetName())
2025-05-12 08:54:45 -04:00
.Contains(mergeTag))
{
if (subfolder)
rom.SetName($"{machineName}\\{rom.GetName()}");
2025-01-14 21:02:37 -05:00
ItemsDB.RemapDatItemToMachine(item.Key, machineIndex: cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
// If the parent doesn't already contain this item, add to subfolder of parent
else if (!GetItemsForBucketDB(cloneOf).Values.Contains(item.Value) || skipDedup)
{
if (subfolder)
rom.SetName($"{machineName}\\{rom.GetName()}");
2025-01-14 21:02:37 -05:00
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
}
// All other that would be missing to subfolder of parent
else if (!GetItemsForBucketDB(cloneOf).Values.Contains(item.Value))
{
if (subfolder)
item.Value.SetName($"{machineName}\\{item.Value.GetName()}");
2025-01-14 21:02:37 -05:00
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
2025-01-14 21:02:37 -05:00
// Remove the current item
2025-01-14 22:34:58 -05:00
RemoveItemDB(item.Key);
2025-01-14 21:02:37 -05:00
}
}
}
2025-01-13 13:49:45 -05:00
/// <summary>
/// Use cloneof tags to add items to the children, setting the new romof tag in the process
/// </summary>
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private void AddItemsFromCloneOfParentImpl()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
foreach (string bucket in buckets)
2025-01-13 13:49:45 -05:00
{
// If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket);
if (items.Count == 0)
continue;
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = items[0].GetMachine();
2025-01-13 13:49:45 -05:00
if (machine == null)
continue;
// Get the cloneof parent items
string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
List<DatItem> parentItems = GetItemsForBucket(cloneOf);
if (parentItems.Count == 0)
continue;
// If the parent exists and has items, we copy the items from the parent to the current game
DatItem copyFrom = items[0];
foreach (DatItem item in parentItems)
{
DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom);
2025-01-13 13:54:14 -05:00
if (!items.Exists(i => string.Equals(i.GetName(), datItem.GetName(), StringComparison.OrdinalIgnoreCase))
2025-01-13 13:49:45 -05:00
&& !items.Contains(datItem))
{
2025-01-14 15:32:14 -05:00
AddItem(datItem, statsOnly: false);
2025-01-13 13:49:45 -05:00
}
}
// Now we want to get the parent romof tag and put it in each of the items
items = GetItemsForBucket(bucket);
2025-05-02 16:46:20 -04:00
string? romof = GetItemsForBucket(cloneOf)[0].GetMachine()!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
2025-01-13 13:49:45 -05:00
foreach (DatItem item in items)
{
2025-05-02 16:46:20 -04:00
item.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
2025-01-13 13:49:45 -05:00
}
}
}
/// <summary>
/// Use cloneof tags to add items to the children, setting the new romof tag in the process
/// </summary>
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private void AddItemsFromCloneOfParentImplDB()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
2025-01-13 13:49:45 -05:00
foreach (string bucket in buckets)
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
continue;
// Get the source for the first item
var source = GetSourceForItemDB(items.First().Key);
2025-01-13 13:49:45 -05:00
// Get the machine for the first item in the list
var machine = GetMachineForItemDB(items.First().Key);
2025-01-13 13:49:45 -05:00
if (machine.Value == null)
continue;
// Get the clone parent
string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
if (string.IsNullOrEmpty(cloneOf))
continue;
// If the parent doesn't have any items, we want to continue
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(cloneOf);
if (parentItems.Count == 0)
continue;
// If the parent exists and has items, we copy the items from the parent to the current game
foreach (var item in parentItems)
{
DatItem datItem = (DatItem)item.Value.Clone();
if (items.Values.Any(i => i.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant())
&& items.Values.Any(i => i == datItem))
{
ItemsDB.AddItem(datItem, machine.Key, source.Key);
}
}
// Get the parent machine
var parentMachine = GetMachineForItemDB(GetItemsForBucketDB(cloneOf).First().Key);
2025-01-13 13:49:45 -05:00
if (parentMachine.Value == null)
continue;
// Now we want to get the parent romof tag and put it in each of the items
items = GetItemsForBucketDB(bucket);
string? romof = parentMachine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
foreach (var key in items.Keys)
{
var itemMachine = GetMachineForItemDB(key);
2025-01-13 13:49:45 -05:00
if (itemMachine.Value == null)
continue;
// TODO: Remove merge tags here
2025-01-13 13:49:45 -05:00
itemMachine.Value.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
}
}
}
/// <summary>
2025-01-13 10:45:42 -05:00
/// Use device_ref and optionally slotoption tags to add items to the children
/// </summary>
/// <param name="deviceOnly">True if only child device sets are touched, false for non-device sets</param>
/// <param name="useSlotOptions">True if slotoptions tags are used as well, false otherwise</param>
/// <returns>True if any items were processed, false otherwise</returns>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private bool AddItemsFromDevicesImpl(bool deviceOnly, bool useSlotOptions)
{
bool foundnew = false;
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket doesn't have items
List<DatItem> datItems = GetItemsForBucket(bucket);
if (datItems.Count == 0)
continue;
// If the machine (is/is not) a device, we want to continue
2025-05-02 16:46:20 -04:00
if (deviceOnly ^ (datItems[0].GetMachine()!.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
continue;
2025-05-14 12:48:14 -04:00
// Get the first item from the bucket
2025-05-13 09:39:10 -04:00
DatItem copyFrom = datItems[0];
// Get all device reference names from the current machine
2025-05-14 12:48:14 -04:00
HashSet<string?> deviceReferences = [];
deviceReferences.UnionWith(datItems
.FindAll(i => i is DeviceRef)
.ConvertAll(i => i as DeviceRef)
2025-05-14 12:48:14 -04:00
.ConvertAll(dr => dr!.GetName()));
// Get all slot option names from the current machine
2025-05-14 12:48:14 -04:00
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)
{
// Loop through all names and check the corresponding machines
var newDeviceReferences = new HashSet<string>();
foreach (string? deviceReference in deviceReferences)
{
// Add to the list of new device reference names
List<DatItem> devItems = GetItemsForBucket(deviceReference);
if (devItems.Count == 0)
continue;
newDeviceReferences.UnionWith(devItems
.FindAll(i => i is DeviceRef)
.ConvertAll(i => (i as DeviceRef)!.GetName()!));
// Set new machine information and add to the current machine
foreach (DatItem item in devItems)
{
// If the parent machine doesn't already contain this item, add it
if (!datItems.Exists(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) && 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.CopyMachineInformation(copyFrom);
2025-05-14 12:48:14 -04:00
datItems.Add(datItem);
2025-01-14 15:32:14 -05:00
AddItem(datItem, statsOnly: false);
}
}
}
// Now that every device reference is accounted for, add the new list of device references, if they don't already exist
foreach (string deviceReference in newDeviceReferences)
{
if (!deviceReferences.Contains(deviceReference))
{
2025-05-14 12:48:14 -04:00
deviceReferences.Add(deviceReference);
var deviceRef = new DeviceRef();
deviceRef.SetName(deviceReference);
2025-05-13 09:39:10 -04:00
deviceRef.CopyMachineInformation(copyFrom);
Items.AddItem(deviceRef, statsOnly: false);
}
}
}
// If we're checking slotoptions
if (useSlotOptions && slotOptions.Count > 0)
{
// Loop through all names and check the corresponding machines
var newSlotOptions = new HashSet<string>();
foreach (string? slotOption in slotOptions)
{
// Add to the list of new slot option names
List<DatItem> slotItems = GetItemsForBucket(slotOption);
if (slotItems.Count == 0)
continue;
newSlotOptions.UnionWith(slotItems
.FindAll(i => i is Slot)
.FindAll(s => (s as Slot)!.SlotOptionsSpecified)
.SelectMany(s => (s as Slot)!.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
.Select(o => o.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey)!));
// Set new machine information and add to the current machine
foreach (DatItem item in slotItems)
{
// If the parent machine doesn't already contain this item, add it
if (!datItems.Exists(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) && 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.CopyMachineInformation(copyFrom);
2025-05-14 12:48:14 -04:00
datItems.Add(datItem);
2025-01-14 15:32:14 -05:00
AddItem(datItem, statsOnly: false);
}
}
}
// Now that every device is accounted for, add the new list of slot options, if they don't already exist
foreach (string slotOption in newSlotOptions)
{
if (!slotOptions.Contains(slotOption))
{
2025-05-14 12:48:14 -04:00
slotOptions.Add(slotOption);
var slotOptionItem = new SlotOption();
slotOptionItem.SetFieldValue<string?>(Models.Metadata.SlotOption.DevNameKey, slotOption);
2025-05-14 12:48:14 -04:00
slotOptionItem.CopyMachineInformation(copyFrom);
var slotItem = new Slot();
slotItem.SetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey, [slotOptionItem]);
2025-05-13 09:39:10 -04:00
slotItem.CopyMachineInformation(copyFrom);
2025-05-13 09:39:10 -04:00
Items.AddItem(slotItem, statsOnly: false);
}
}
}
}
return foundnew;
}
/// <summary>
2025-01-13 10:45:42 -05:00
/// Use device_ref and optionally slotoption tags to add items to the children
/// </summary>
/// <param name="deviceOnly">True if only child device sets are touched, false for non-device sets</param>
/// <param name="useSlotOptions">True if slotoptions tags are used as well, false otherwise</param>
/// <returns>True if any items were processed, false otherwise</returns>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private bool AddItemsFromDevicesImplDB(bool deviceOnly, bool useSlotOptions)
{
bool foundnew = false;
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
continue;
// Get the source for the first item
var source = GetSourceForItemDB(items.First().Key);
// Get the machine for the first item
var machine = GetMachineForItemDB(items.First().Key);
if (machine.Value == null)
continue;
// If the machine (is/is not) a device, we want to continue
if (deviceOnly ^ (machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
continue;
// Get all device reference names from the current machine
List<string?> deviceReferences = items.Values
.Where(i => i is DeviceRef)
.Select(i => i as DeviceRef)
.Select(dr => dr!.GetName())
.Distinct()
.ToList();
// Get all slot option names from the current machine
List<string?> slotOptions = items.Values
.Where(i => i is Slot)
.Select(i => i as Slot)
.Where(s => s!.SlotOptionsSpecified)
.SelectMany(s => s!.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
.Select(so => so.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey))
.Distinct()
.ToList();
// If we're checking device references
if (deviceReferences.Count > 0)
{
// Loop through all names and check the corresponding machines
var newDeviceReferences = new HashSet<string>();
foreach (string? deviceReference in deviceReferences)
{
// If the device reference is invalid
if (deviceReference == null)
continue;
// If the machine doesn't exist then we continue
Dictionary<long, DatItem> devItems = GetItemsForBucketDB(deviceReference);
if (devItems.Count == 0)
continue;
// Add to the list of new device reference names
newDeviceReferences.UnionWith(devItems.Values
.Where(i => i is DeviceRef)
.Select(i => (i as DeviceRef)!.GetName()!));
// Set new machine information and add to the current machine
var copyFrom = GetMachineForItemDB(items.First().Key);
if (copyFrom.Value == null)
continue;
foreach (var item in devItems.Values)
{
// If the parent machine doesn't already contain this item, add it
if (!items.Values.Any(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey)
&& i.GetName() == item.GetName()))
{
// Set that we found new items
foundnew = true;
// Clone the item and then add it
2025-01-13 13:31:14 -05:00
DatItem datItem = (DatItem)item.Clone();
ItemsDB.AddItem(datItem, machine.Key, source.Key);
}
}
}
// Now that every device reference is accounted for, add the new list of device references, if they don't already exist
foreach (string deviceReference in newDeviceReferences)
{
if (!deviceReferences.Contains(deviceReference))
{
var deviceRef = new DeviceRef();
deviceRef.SetName(deviceReference);
ItemsDB.AddItem(deviceRef, machine.Key, source.Key);
}
}
}
// If we're checking slotoptions
if (useSlotOptions && slotOptions.Count > 0)
{
// Loop through all names and check the corresponding machines
var newSlotOptions = new HashSet<string>();
foreach (string? slotOption in slotOptions)
{
// If the slot option is invalid
if (slotOption == null)
continue;
// If the machine doesn't exist then we continue
Dictionary<long, DatItem> slotItems = GetItemsForBucketDB(slotOption);
if (slotItems.Count == 0)
continue;
// Add to the list of new slot option names
newSlotOptions.UnionWith(slotItems.Values
.Where(i => i is Slot)
.Where(s => (s as Slot)!.SlotOptionsSpecified)
.SelectMany(s => (s as Slot)!.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
.Select(o => o.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey)!));
// Set new machine information and add to the current machine
var copyFrom = GetMachineForItemDB(GetItemsForBucketDB(bucket).First().Key);
if (copyFrom.Value == null)
continue;
foreach (var item in slotItems.Values)
{
// If the parent machine doesn't already contain this item, add it
if (!items.Values.Any(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey)
&& i.GetName() == item.GetName()))
{
// Set that we found new items
foundnew = true;
// Clone the item and then add it
2025-01-13 13:31:14 -05:00
DatItem datItem = (DatItem)item.Clone();
ItemsDB.AddItem(datItem, machine.Key, source.Key);
}
}
}
// Now that every device is accounted for, add the new list of slot options, if they don't already exist
foreach (string slotOption in newSlotOptions)
{
if (!slotOptions.Contains(slotOption))
{
var slotOptionItem = new SlotOption();
slotOptionItem.SetFieldValue<string?>(Models.Metadata.SlotOption.DevNameKey, slotOption);
var slotItem = new Slot();
slotItem.SetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey, [slotOptionItem]);
ItemsDB.AddItem(slotItem, machine.Key, source.Key);
}
}
}
}
return foundnew;
}
2025-01-13 13:43:38 -05:00
/// <summary>
/// Use romof tags to add items to the children
/// </summary>
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private void AddItemsFromRomOfParentImpl()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
foreach (string bucket in buckets)
2025-01-13 13:43:38 -05:00
{
// If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket);
if (items.Count == 0)
continue;
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = items[0].GetMachine();
2025-01-13 13:43:38 -05:00
if (machine == null)
continue;
// Get the romof parent items
string? romOf = machine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
List<DatItem> parentItems = GetItemsForBucket(romOf);
if (parentItems.Count == 0)
continue;
// If the parent exists and has items, we copy the items from the parent to the current game
DatItem copyFrom = items[0];
foreach (DatItem item in parentItems)
{
DatItem datItem = (DatItem)item.Clone();
datItem.CopyMachineInformation(copyFrom);
2025-01-13 13:54:14 -05:00
if (!items.Exists(i => i.GetName() == datItem.GetName()) && !items.Contains(datItem))
2025-01-14 15:32:14 -05:00
AddItem(datItem, statsOnly: false);
2025-01-13 13:43:38 -05:00
}
}
}
/// <summary>
/// Use romof tags to add items to the children
/// </summary>
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private void AddItemsFromRomOfParentImplDB()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
2025-01-13 13:43:38 -05:00
foreach (string bucket in buckets)
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
continue;
// Get the source for the first item
var source = GetSourceForItemDB(items.First().Key);
2025-01-13 13:43:38 -05:00
// Get the machine for the first item
var machine = GetMachineForItemDB(items.First().Key);
2025-01-13 13:43:38 -05:00
if (machine.Value == null)
continue;
// Get the romof parent items
string? romOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(romOf);
if (parentItems.Count == 0)
continue;
// If the parent exists and has items, we copy the items from the parent to the current game
foreach (var item in parentItems)
{
DatItem datItem = (DatItem)item.Value.Clone();
if (items.Any(i => i.Value.GetName() == datItem.GetName())
&& items.Any(i => i.Value == datItem))
{
ItemsDB.AddItem(datItem, machine.Key, source.Key);
}
}
}
}
/// <summary>
/// Remove all BIOS and device sets
/// </summary>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private void RemoveBiosAndDeviceSetsImpl()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
2025-05-11 21:29:34 -04:00
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(buckets, Globals.ParallelOptions, bucket =>
#elif NET40_OR_GREATER
Parallel.ForEach(buckets, bucket =>
#else
foreach (string bucket in buckets)
2025-05-11 21:29:34 -04:00
#endif
{
// If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket);
if (items.Count == 0)
2025-05-11 21:29:34 -04:00
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
2025-05-11 21:29:34 -04:00
#endif
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = items[0].GetMachine();
if (machine == null)
2025-05-11 21:29:34 -04:00
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
2025-05-11 21:29:34 -04:00
#endif
// Remove flagged items
if ((machine.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)
|| (machine.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
{
2025-01-14 15:32:14 -05:00
RemoveBucket(bucket);
}
2025-05-11 21:29:34 -04:00
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
2025-05-11 21:29:34 -04:00
#endif
}
/// <summary>
/// Remove all BIOS and device sets
/// </summary>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
private void RemoveBiosAndDeviceSetsImplDB()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
2025-05-11 21:29:34 -04:00
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(buckets, Globals.ParallelOptions, bucket =>
#elif NET40_OR_GREATER
Parallel.ForEach(buckets, bucket =>
#else
foreach (string bucket in buckets)
2025-05-11 21:29:34 -04:00
#endif
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
2025-05-11 21:29:34 -04:00
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
2025-05-11 21:29:34 -04:00
#endif
// Get the machine
var machine = GetMachineForItemDB(items.First().Key);
if (machine.Value == null)
2025-05-11 21:29:34 -04:00
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
2025-05-11 21:29:34 -04:00
#endif
// Remove flagged items
if ((machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)
|| (machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
{
foreach (var key in items.Keys)
{
2025-01-14 22:34:58 -05:00
RemoveItemDB(key);
}
}
2025-01-13 14:14:28 -05:00
// Remove the machine
2025-01-14 22:34:58 -05:00
RemoveMachineDB(machine.Key);
2025-05-11 21:29:34 -04:00
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
2025-05-11 21:29:34 -04:00
#endif
}
/// <summary>
2025-01-13 13:01:36 -05:00
/// Use cloneof tags to remove items from the children
/// </summary>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
2025-01-13 13:01:36 -05:00
private void RemoveItemsFromCloneOfChildImpl()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket);
if (items.Count == 0)
continue;
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = items[0].GetMachine();
if (machine == null)
continue;
// Get the cloneof parent items
2025-01-13 13:01:36 -05:00
string? cloneOf = machine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
List<DatItem> parentItems = GetItemsForBucket(cloneOf);
if (parentItems.Count == 0)
continue;
2025-01-13 13:01:36 -05:00
// If the parent exists and has items, we remove the parent items from the current game
foreach (DatItem item in parentItems)
{
2025-05-03 23:20:23 -04:00
while (true)
{
2025-05-03 23:20:23 -04:00
// Find the next index that matches the item
int index = items.FindIndex(i => i.Equals(item));
if (index < 0)
break;
// Remove the item from the local and internal lists
2025-05-03 23:20:23 -04:00
RemoveItem(bucket, items[index], index);
2025-05-11 22:00:09 -04:00
items.RemoveAt(index);
}
}
2025-01-13 13:01:36 -05:00
// Now we want to get the parent romof tag and put it in each of the remaining items
items = GetItemsForBucket(bucket);
2025-05-02 16:46:20 -04:00
string? romof = GetItemsForBucket(cloneOf)[0].GetMachine()!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
2025-01-13 13:01:36 -05:00
foreach (DatItem item in items)
{
2025-05-02 16:46:20 -04:00
item.GetMachine()!.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
2025-01-13 13:01:36 -05:00
}
}
}
/// <summary>
2025-01-13 13:01:36 -05:00
/// Use cloneof tags to remove items from the children
/// </summary>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
2025-01-13 13:01:36 -05:00
private void RemoveItemsFromCloneOfChildImplDB()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
continue;
2025-01-13 13:01:36 -05:00
// Get the machine for the first item
var machine = GetMachineForItemDB(items.First().Key);
if (machine.Value == null)
continue;
// Get the cloneof parent items
2025-01-13 13:01:36 -05:00
string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
2025-01-13 13:31:14 -05:00
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(cloneOf);
if (parentItems == null || parentItems.Count == 0)
continue;
2025-01-13 13:01:36 -05:00
// If the parent exists and has items, we remove the parent items from the current game
foreach (var item in parentItems)
{
var matchedItems = items.Where(i => i.Value.Equals(item.Value));
foreach (var match in matchedItems)
{
2025-01-14 22:34:58 -05:00
RemoveItemDB(match.Key);
}
}
2025-01-13 13:01:36 -05:00
// Now we want to get the parent romof tag and put it in each of the remaining items
items = GetItemsForBucketDB(bucket);
machine = GetMachineForItemDB(GetItemsForBucketDB(cloneOf).First().Key);
2025-01-13 13:01:36 -05:00
if (machine.Value == null)
continue;
string? romof = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
foreach (var item in items)
{
machine = GetMachineForItemDB(item.Key);
2025-01-13 13:01:36 -05:00
if (machine.Value == null)
continue;
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
}
}
}
/// <summary>
2025-01-13 15:46:06 -05:00
/// Use romof tags to remove items from children
/// </summary>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="Items"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
2025-01-13 15:46:06 -05:00
private void RemoveItemsFromRomOfChildImpl()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
List<DatItem> items = GetItemsForBucket(bucket);
if (items.Count == 0)
continue;
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = items[0].GetMachine();
if (machine == null)
continue;
// Get the romof parent items
2025-01-13 13:01:36 -05:00
string? romOf = machine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
2025-01-13 13:31:14 -05:00
List<DatItem> parentItems = GetItemsForBucket(romOf);
if (parentItems.Count == 0)
continue;
2025-05-03 23:20:23 -04:00
// If the parent exists and has items, we remove the parent items from the current game
2025-01-13 10:52:27 -05:00
foreach (DatItem item in parentItems)
{
2025-05-03 23:20:23 -04:00
while (true)
{
2025-05-03 23:20:23 -04:00
// Find the next index that matches the item
int index = items.FindIndex(i => i.Equals(item));
if (index < 0)
break;
// Remove the item from the local and internal lists
2025-05-03 23:20:23 -04:00
RemoveItem(bucket, items[index], index);
2025-05-11 22:00:09 -04:00
items.RemoveAt(index);
}
}
}
}
/// <summary>
2025-01-13 13:01:36 -05:00
/// Use romof tags to remove bios items from children
/// </summary>
2025-01-13 13:01:36 -05:00
/// <param name="bios">True if only child Bios sets are touched, false for non-bios sets</param>
2025-01-13 09:27:01 -05:00
/// <remarks>
/// Applies to <see cref="ItemsDB"/>.
/// Assumes items are bucketed by <see cref="ItemKey.Machine"/>.
/// </remarks>
2025-01-13 15:46:06 -05:00
private void RemoveItemsFromRomOfChildImplDB()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. ItemsDB.SortedKeys];
foreach (string bucket in buckets)
{
// If the bucket has no items in it
Dictionary<long, DatItem> items = GetItemsForBucketDB(bucket);
if (items.Count == 0)
continue;
2025-01-13 13:01:36 -05:00
// Get the machine for the item
var machine = GetMachineForItemDB(items.First().Key);
if (machine.Value == null)
continue;
// Get the romof parent items
2025-01-13 13:01:36 -05:00
string? romOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
2025-01-13 13:31:14 -05:00
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(romOf);
if (parentItems.Count == 0)
continue;
2025-01-13 13:01:36 -05:00
// If the parent exists and has items, we remove the items that are in the parent from the current game
foreach (var item in parentItems)
{
var matchedItems = items.Where(i => i.Value.Equals(item.Value));
foreach (var match in matchedItems)
{
2025-01-14 22:34:58 -05:00
RemoveItemDB(match.Key);
}
}
}
}
2025-01-13 00:29:39 -05:00
/// <summary>
2025-01-13 10:45:42 -05:00
/// Remove all romof and cloneof tags from all machines
/// </summary>
/// <remarks>Applies to <see cref="Items"/></remarks>
private void RemoveMachineRelationshipTagsImpl()
{
2025-05-03 23:57:57 -04:00
string[] buckets = [.. Items.SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(buckets, Globals.ParallelOptions, bucket =>
#elif NET40_OR_GREATER
Parallel.ForEach(buckets, bucket =>
#else
foreach (string bucket in buckets)
#endif
{
// If the bucket has no items in it
var items = GetItemsForBucket(bucket);
if (items == null || items.Count == 0)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
foreach (DatItem item in items)
{
2025-05-14 08:52:24 -04:00
// Remove the merge tag
item.RemoveField(Models.Metadata.Rom.MergeKey);
2025-01-13 00:31:38 -05:00
// Get the machine
2025-05-02 16:46:20 -04:00
var machine = item.GetMachine();
2025-01-13 00:31:38 -05:00
if (machine == null)
continue;
2025-01-13 00:31:38 -05:00
machine.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, null);
machine.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, null);
machine.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, null);
}
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
/// <summary>
2025-01-13 10:45:42 -05:00
/// Remove all romof and cloneof tags from all machines
/// </summary>
/// <remarks>Applies to <see cref="ItemsDB"/></remarks>
private void RemoveMachineRelationshipTagsImplDB()
{
var machines = GetMachinesDB();
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(machines, Globals.ParallelOptions, machine =>
#elif NET40_OR_GREATER
Parallel.ForEach(machines, machine =>
#else
2025-01-13 00:29:39 -05:00
foreach (var machine in machines)
#endif
{
2025-05-14 08:52:24 -04:00
// TODO: Remove merge tags here
2025-01-13 00:31:38 -05:00
// Get the machine
2025-01-13 00:29:39 -05:00
if (machine.Value == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
2025-01-13 00:29:39 -05:00
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, null);
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, null);
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, null);
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
#endregion
}
}