2024-03-19 16:10:00 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-03-12 23:27:23 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
#endif
|
2024-03-13 02:10:34 -04:00
|
|
|
|
using System.Collections.Generic;
|
2024-03-13 10:14:04 -04:00
|
|
|
|
using System.IO;
|
2024-03-13 02:10:34 -04:00
|
|
|
|
using System.Linq;
|
2024-03-19 16:22:19 -04:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-03-13 02:44:04 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#endif
|
2022-11-03 15:54:00 -07:00
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2024-03-19 14:08:46 -04:00
|
|
|
|
using SabreTools.Core.Filter;
|
2024-03-13 10:14:04 -04:00
|
|
|
|
using SabreTools.Core.Tools;
|
2022-11-03 15:54:00 -07:00
|
|
|
|
using SabreTools.DatItems;
|
2024-03-13 02:44:04 -04:00
|
|
|
|
using SabreTools.DatItems.Formats;
|
|
|
|
|
|
using SabreTools.Hashing;
|
2024-10-24 00:36:44 -04:00
|
|
|
|
using SabreTools.IO.Logging;
|
2024-10-19 11:43:11 -04:00
|
|
|
|
using SabreTools.Matching.Compare;
|
2022-11-03 15:54:00 -07:00
|
|
|
|
|
2024-03-19 11:02:36 -04:00
|
|
|
|
/*
|
|
|
|
|
|
* Planning Notes:
|
|
|
|
|
|
*
|
|
|
|
|
|
* In order for this in-memory "database" design to work, there need to be a few things:
|
|
|
|
|
|
* - Feature parity with all existing item dictionary operations
|
|
|
|
|
|
* - A way to transition between the two item dictionaries (a flag?)
|
|
|
|
|
|
* - Helper methods that target the "database" version instead of assuming the standard dictionary
|
|
|
|
|
|
*
|
|
|
|
|
|
* Notable changes include:
|
|
|
|
|
|
* - Separation of Machine from DatItem, leading to a mapping instead
|
|
|
|
|
|
* + Should DatItem include an index reference to the machine? Or should that be all external?
|
|
|
|
|
|
* - Adding machines to the dictionary distinctly from the items
|
|
|
|
|
|
* - Having a separate "bucketing" system that only reorders indicies and not full items; quicker?
|
|
|
|
|
|
* - Non-key-based add/remove of values; use explicit methods instead of dictionary-style accessors
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2022-11-03 15:54:00 -07:00
|
|
|
|
namespace SabreTools.DatFiles
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Item dictionary with statistics, bucketing, and sorting
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonObject("items"), XmlRoot("items")]
|
2024-03-13 01:44:15 -04:00
|
|
|
|
public class ItemDictionaryDB
|
2022-11-03 15:54:00 -07:00
|
|
|
|
{
|
|
|
|
|
|
#region Private instance variables
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-12 23:27:23 -04:00
|
|
|
|
/// Internal dictionary for all items
|
2022-11-03 15:54:00 -07:00
|
|
|
|
/// </summary>
|
2024-03-13 02:10:34 -04:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-12 23:27:23 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, DatItem> _items = [];
|
2024-03-12 23:27:23 -04:00
|
|
|
|
#else
|
2024-03-13 02:10:34 -04:00
|
|
|
|
private readonly Dictionary<long, DatItem> _items = [];
|
2024-03-12 23:27:23 -04:00
|
|
|
|
#endif
|
2022-11-03 15:54:00 -07:00
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Current highest available item index
|
|
|
|
|
|
/// </summary>
|
2024-03-13 02:44:04 -04:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-13 02:10:34 -04:00
|
|
|
|
private long _itemIndex = 0;
|
|
|
|
|
|
|
2022-11-03 15:54:00 -07:00
|
|
|
|
/// <summary>
|
2024-03-12 23:27:23 -04:00
|
|
|
|
/// Internal dictionary for all machines
|
2022-11-03 15:54:00 -07:00
|
|
|
|
/// </summary>
|
2024-03-13 02:10:34 -04:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-12 23:27:23 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, Machine> _machines = [];
|
2024-03-12 23:27:23 -04:00
|
|
|
|
#else
|
2024-03-13 02:10:34 -04:00
|
|
|
|
private readonly Dictionary<long, Machine> _machines = [];
|
2024-03-13 01:41:18 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Current highest available machine index
|
|
|
|
|
|
/// </summary>
|
2024-03-13 02:44:04 -04:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-13 02:10:34 -04:00
|
|
|
|
private long _machineIndex = 0;
|
|
|
|
|
|
|
2024-03-20 00:59:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal dictionary for all sources
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, Source> _sources = [];
|
2024-03-20 00:59:47 -04:00
|
|
|
|
#else
|
|
|
|
|
|
private readonly Dictionary<long, Source> _sources = [];
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Current highest available source index
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
private long _sourceIndex = 0;
|
|
|
|
|
|
|
2024-03-13 01:41:18 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal dictionary for item to machine mappings
|
|
|
|
|
|
/// </summary>
|
2024-03-13 02:10:34 -04:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-13 01:41:18 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, long> _itemToMachineMapping = [];
|
2024-02-28 22:54:56 -05:00
|
|
|
|
#else
|
2024-03-13 02:10:34 -04:00
|
|
|
|
private readonly Dictionary<long, long> _itemToMachineMapping = [];
|
2024-02-28 22:54:56 -05:00
|
|
|
|
#endif
|
2022-11-03 15:54:00 -07:00
|
|
|
|
|
2024-03-20 00:59:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal dictionary for item to source mappings
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, long> _itemToSourceMapping = [];
|
2024-03-20 00:59:47 -04:00
|
|
|
|
#else
|
|
|
|
|
|
private readonly Dictionary<long, long> _itemToSourceMapping = [];
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2024-03-13 02:44:04 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal dictionary representing the current buckets
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-10-30 10:59:04 -04:00
|
|
|
|
private readonly ConcurrentDictionary<string, List<long>> _buckets = [];
|
2024-03-13 02:44:04 -04:00
|
|
|
|
#else
|
2024-10-30 10:59:04 -04:00
|
|
|
|
private readonly Dictionary<string, List<long>> _buckets = [];
|
2024-03-13 02:44:04 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Current bucketed by value
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private ItemKey _bucketedBy = ItemKey.NULL;
|
2024-03-13 02:10:34 -04:00
|
|
|
|
|
2024-03-19 16:10:00 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Logging object
|
|
|
|
|
|
/// </summary>
|
2025-01-08 16:59:44 -05:00
|
|
|
|
private readonly Logger _logger;
|
2024-03-19 16:10:00 -04:00
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Fields
|
|
|
|
|
|
|
2024-03-19 15:21:01 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the keys in sorted order from the file dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>List of the keys in sorted order</returns>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
public string[] SortedKeys
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> keys = [.. _buckets.Keys];
|
|
|
|
|
|
keys.Sort(new NaturalComparer());
|
2024-10-19 21:41:08 -04:00
|
|
|
|
return [.. keys];
|
2024-03-19 15:21:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// DAT statistics
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
public DatStatistics DatStatistics { get; } = new DatStatistics();
|
|
|
|
|
|
|
2022-11-03 15:54:00 -07:00
|
|
|
|
#endregion
|
2024-03-13 02:10:34 -04:00
|
|
|
|
|
2024-03-19 16:10:00 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Generic constructor
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ItemDictionaryDB()
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger = new Logger(this);
|
2024-03-19 16:10:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
#region Accessors
|
|
|
|
|
|
|
2024-03-19 11:12:04 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a DatItem to the dictionary after validation
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item">Item data to validate</param>
|
|
|
|
|
|
/// <param name="machineIndex">Index of the machine related to the item</param>
|
2024-03-20 01:32:15 -04:00
|
|
|
|
/// <param name="sourceIndex">Index of the source related to the item</param>
|
|
|
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
2024-03-19 11:12:04 -04:00
|
|
|
|
/// <returns>The index for the added item, -1 on error</returns>
|
2024-03-20 01:32:15 -04:00
|
|
|
|
public long AddItem(DatItem item, long machineIndex, long sourceIndex, bool statsOnly)
|
2024-03-19 11:12:04 -04:00
|
|
|
|
{
|
|
|
|
|
|
// If we have a Disk, Media, or Rom, clean the hash data
|
|
|
|
|
|
if (item is Disk disk)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the file has aboslutely no hashes, skip and log
|
|
|
|
|
|
if (disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump
|
|
|
|
|
|
&& string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key))
|
|
|
|
|
|
&& string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key)))
|
|
|
|
|
|
{
|
|
|
|
|
|
disk.SetFieldValue<string?>(Models.Metadata.Disk.StatusKey, ItemStatus.Nodump.AsStringValue());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
item = disk;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (item is Media media)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the file has aboslutely no hashes, skip and log
|
|
|
|
|
|
if (string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.MD5Key))
|
|
|
|
|
|
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))
|
|
|
|
|
|
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA256Key))
|
|
|
|
|
|
&& string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SpamSumKey)))
|
|
|
|
|
|
{
|
|
|
|
|
|
// No-op as there is no status key for Media
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
item = media;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (item is Rom rom)
|
|
|
|
|
|
{
|
|
|
|
|
|
long? size = rom.GetInt64FieldValue(Models.Metadata.Rom.SizeKey);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have the case where there is SHA-1 and nothing else, we don't fill in any other part of the data
|
|
|
|
|
|
if (size == null && !rom.HasHashes())
|
|
|
|
|
|
{
|
|
|
|
|
|
// No-op, just catch it so it doesn't go further
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
|
|
|
|
|
else if ((size == 0 || size == null)
|
|
|
|
|
|
&& (string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey)) || rom.HasZeroHash()))
|
|
|
|
|
|
{
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SizeKey, Constants.SizeZero.ToString());
|
2024-11-13 03:55:33 -05:00
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, ZeroHash.CRC32Str);
|
2025-01-09 05:26:36 -05:00
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD2Key, null); // ZeroHash.GetString(HashType.MD2)
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD4Key, null); // ZeroHash.GetString(HashType.MD4)
|
2024-11-13 03:55:33 -05:00
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD5Key, ZeroHash.MD5Str);
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA1Key, ZeroHash.SHA1Str);
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA256Key, null); // ZeroHash.SHA256Str;
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA384Key, null); // ZeroHash.SHA384Str;
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA512Key, null); // ZeroHash.SHA512Str;
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.SpamSumKey, null); // ZeroHash.SpamSumStr;
|
2024-03-19 11:12:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the file has no size and it's not the above case, skip and log
|
|
|
|
|
|
else if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump && (size == 0 || size == null))
|
|
|
|
|
|
{
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.Nodump.AsStringValue());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the file has a size but aboslutely no hashes, skip and log
|
|
|
|
|
|
else if (rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() != ItemStatus.Nodump
|
|
|
|
|
|
&& size != null && size > 0
|
|
|
|
|
|
&& !rom.HasHashes())
|
|
|
|
|
|
{
|
|
|
|
|
|
rom.SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.Nodump.AsStringValue());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
item = rom;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the key and add the file
|
|
|
|
|
|
string key = item.GetKey(ItemKey.Machine);
|
|
|
|
|
|
|
|
|
|
|
|
// If only adding statistics, we add an empty key for games and then just item stats
|
|
|
|
|
|
if (statsOnly)
|
|
|
|
|
|
{
|
|
|
|
|
|
EnsureBucketingKey(key);
|
|
|
|
|
|
DatStatistics.AddItemStatistics(item);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-03-20 00:59:47 -04:00
|
|
|
|
return AddItem(item, machineIndex, sourceIndex);
|
2024-03-19 11:12:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a machine, returning the insert index
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public long AddMachine(Machine machine)
|
|
|
|
|
|
{
|
|
|
|
|
|
_machines[_machineIndex++] = machine;
|
|
|
|
|
|
return _machineIndex - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-20 00:59:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a source, returning the insert index
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public long AddSource(Source source)
|
|
|
|
|
|
{
|
|
|
|
|
|
_sources[_sourceIndex++] = source;
|
|
|
|
|
|
return _sourceIndex - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 15:31:23 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove any keys that have null or empty values
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ClearEmpty()
|
|
|
|
|
|
{
|
2024-11-12 21:12:06 -05:00
|
|
|
|
var keys = Array.FindAll(SortedKeys, k => k != null);
|
2024-03-19 15:31:23 -04:00
|
|
|
|
foreach (string key in keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the key doesn't exist, skip
|
|
|
|
|
|
if (!_buckets.ContainsKey(key))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the value is null, remove
|
|
|
|
|
|
else if (_buckets[key] == null)
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_buckets.TryRemove(key, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_buckets.Remove(key);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// If there are no non-blank items, remove
|
2024-12-06 13:57:48 -05:00
|
|
|
|
else if (!_buckets[key].Exists(i => GetItem(i) != null && GetItem(i) is not Blank))
|
2024-03-19 15:31:23 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_buckets.TryRemove(key, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_buckets.Remove(key);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 11:11:59 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove all items marked for removal
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ClearMarked()
|
|
|
|
|
|
{
|
|
|
|
|
|
var itemIndices = _items.Keys;
|
|
|
|
|
|
foreach (long itemIndex in itemIndices)
|
|
|
|
|
|
{
|
|
|
|
|
|
var datItem = _items[itemIndex];
|
|
|
|
|
|
if (datItem == null || datItem.GetBoolFieldValue(DatItem.RemoveKey) != true)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
RemoveItem(itemIndex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get an item based on the index
|
|
|
|
|
|
/// </summary>
|
2024-03-13 19:41:10 -04:00
|
|
|
|
public DatItem? GetItem(long index)
|
2024-03-13 02:10:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!_items.ContainsKey(index))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return _items[index];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 23:35:29 -04:00
|
|
|
|
/// <summary>
|
2024-03-20 00:59:47 -04:00
|
|
|
|
/// Get all item to machine mappings
|
2024-03-19 23:35:29 -04:00
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, long> GetItemMachineMappings() => _itemToMachineMapping;
|
2024-03-20 00:59:47 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get all item to source mappings
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, long> GetItemSourceMappings() => _itemToSourceMapping;
|
2024-03-19 23:38:56 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get all items and their indicies
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, DatItem> GetItems() => _items;
|
2024-03-19 23:35:29 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the indices and items associated with a bucket name
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public Dictionary<long, DatItem> GetItemsForBucket(string bucketName, bool filter = false)
|
2024-03-19 23:35:29 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!_buckets.ContainsKey(bucketName))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return [];
|
2024-03-19 23:35:29 -04:00
|
|
|
|
|
|
|
|
|
|
var itemIds = _buckets[bucketName];
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var datItems = new Dictionary<long, DatItem>();
|
2024-03-19 23:35:29 -04:00
|
|
|
|
foreach (long itemId in itemIds)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
// Ignore missing IDs
|
|
|
|
|
|
if (!_items.ContainsKey(itemId))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (!filter || _items[itemId].GetBoolFieldValue(DatItem.RemoveKey) != true)
|
|
|
|
|
|
datItems[itemId] = _items[itemId];
|
2024-03-19 23:35:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return datItems;
|
2024-03-19 23:35:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the indices and items associated with a machine index
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, DatItem>? GetItemsForMachine(long machineIndex, bool filter = false)
|
2024-03-19 23:35:29 -04:00
|
|
|
|
{
|
|
|
|
|
|
var itemIds = _itemToMachineMapping
|
|
|
|
|
|
.Where(mapping => mapping.Value == machineIndex)
|
|
|
|
|
|
.Select(mapping => mapping.Key);
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var datItems = new Dictionary<long, DatItem>();
|
2024-03-19 23:35:29 -04:00
|
|
|
|
foreach (long itemId in itemIds)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
// Ignore missing IDs
|
|
|
|
|
|
if (!_items.ContainsKey(itemId))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (!filter || _items[itemId].GetBoolFieldValue(DatItem.RemoveKey) != true)
|
|
|
|
|
|
datItems[itemId] = _items[itemId];
|
2024-03-19 23:35:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return datItems;
|
2024-03-19 23:35:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-20 00:59:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the indices and items associated with a source index
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, DatItem>? GetItemsForSource(long sourceIndex, bool filter = false)
|
2024-03-20 00:59:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
var itemIds = _itemToSourceMapping
|
|
|
|
|
|
.Where(mapping => mapping.Value == sourceIndex)
|
|
|
|
|
|
.Select(mapping => mapping.Key);
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var datItems = new Dictionary<long, DatItem>();
|
2024-03-20 00:59:47 -04:00
|
|
|
|
foreach (long itemId in itemIds)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
// Ignore missing IDs
|
|
|
|
|
|
if (!_items.ContainsKey(itemId))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (!filter || _items[itemId].GetBoolFieldValue(DatItem.RemoveKey) != true)
|
|
|
|
|
|
datItems[itemId] = _items[itemId];
|
2024-03-20 00:59:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return datItems;
|
2024-03-20 00:59:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get a machine based on the index
|
|
|
|
|
|
/// </summary>
|
2024-03-13 19:41:10 -04:00
|
|
|
|
public Machine? GetMachine(long index)
|
2024-03-13 02:10:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!_machines.ContainsKey(index))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return _machines[index];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 21:07:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get a machine based on the name
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>This assume that all machines have unique names</remarks>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public KeyValuePair<long, Machine?> GetMachine(string? name)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Machine?>(-1, null);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
|
|
|
|
|
|
var machine = _machines.FirstOrDefault(m => m.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey) == name);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Machine?>(machine.Key, machine.Value);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
2024-03-19 14:05:53 -04:00
|
|
|
|
/// Get the index and machine associated with an item index
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public KeyValuePair<long, Machine?> GetMachineForItem(long itemIndex)
|
2024-03-13 02:10:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!_itemToMachineMapping.ContainsKey(itemIndex))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Machine?>(-1, null);
|
2024-03-13 02:10:34 -04:00
|
|
|
|
|
|
|
|
|
|
long machineIndex = _itemToMachineMapping[itemIndex];
|
|
|
|
|
|
if (!_machines.ContainsKey(machineIndex))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Machine?>(-1, null);
|
2024-03-13 02:10:34 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Machine?>(machineIndex, _machines[machineIndex]);
|
2024-03-13 02:10:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 19:41:10 -04:00
|
|
|
|
/// <summary>
|
2024-03-19 23:38:56 -04:00
|
|
|
|
/// Get all machines and their indicies
|
2024-03-13 19:41:10 -04:00
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, Machine> GetMachines() => _machines;
|
2024-03-13 02:10:34 -04:00
|
|
|
|
|
2024-03-20 00:59:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get a source based on the index
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Source? GetSource(long index)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_sources.ContainsKey(index))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
return _sources[index];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the index and source associated with an item index
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public KeyValuePair<long, Source?> GetSourceForItem(long itemIndex)
|
2024-03-20 00:59:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!_itemToSourceMapping.ContainsKey(itemIndex))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Source?>(-1, null);
|
2024-03-20 00:59:47 -04:00
|
|
|
|
|
|
|
|
|
|
long sourceIndex = _itemToSourceMapping[itemIndex];
|
|
|
|
|
|
if (!_sources.ContainsKey(sourceIndex))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Source?>(-1, null);
|
2024-03-20 00:59:47 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return new KeyValuePair<long, Source?>(sourceIndex, _sources[sourceIndex]);
|
2024-03-20 00:59:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get all sources and their indicies
|
|
|
|
|
|
/// </summary>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public IDictionary<long, Source> GetSources() => _sources;
|
2024-03-20 00:59:47 -04:00
|
|
|
|
|
2024-03-13 02:10:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove an item, returning if it could be removed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool RemoveItem(long itemIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_items.ContainsKey(itemIndex))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_items.TryRemove(itemIndex, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_items.Remove(itemIndex);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (_itemToMachineMapping.ContainsKey(itemIndex))
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_itemToMachineMapping.TryRemove(itemIndex, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_itemToMachineMapping.Remove(itemIndex);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove a machine, returning if it could be removed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool RemoveMachine(long machineIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_machines.ContainsKey(machineIndex))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_machines.TryRemove(machineIndex, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_machines.Remove(machineIndex);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
var itemIds = _itemToMachineMapping
|
|
|
|
|
|
.Where(mapping => mapping.Value == machineIndex)
|
|
|
|
|
|
.Select(mapping => mapping.Key);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (long itemId in itemIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_itemToMachineMapping.TryRemove(itemId, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_itemToMachineMapping.Remove(itemId);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 22:10:59 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove a machine, returning if it could be removed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool RemoveMachine(string machineName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(machineName))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
var machine = _machines.FirstOrDefault(m => m.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey) == machineName);
|
|
|
|
|
|
return RemoveMachine(machine.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 12:53:38 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add an item, returning the insert index
|
|
|
|
|
|
/// </summary>
|
2024-03-20 00:59:47 -04:00
|
|
|
|
private long AddItem(DatItem item, long machineIndex, long sourceIndex)
|
2024-03-19 12:53:38 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Add the item with a new index
|
|
|
|
|
|
_items[_itemIndex++] = item;
|
|
|
|
|
|
|
|
|
|
|
|
// Add the machine mapping
|
|
|
|
|
|
_itemToMachineMapping[_itemIndex - 1] = machineIndex;
|
|
|
|
|
|
|
2024-03-20 00:59:47 -04:00
|
|
|
|
// Add the source mapping
|
|
|
|
|
|
_itemToSourceMapping[_itemIndex - 1] = sourceIndex;
|
|
|
|
|
|
|
2024-03-19 12:53:38 -04:00
|
|
|
|
// Add the item statistics
|
|
|
|
|
|
DatStatistics.AddItemStatistics(item);
|
|
|
|
|
|
|
|
|
|
|
|
// Add the item to the default bucket
|
|
|
|
|
|
PerformItemBucketing(_itemIndex - 1, _bucketedBy, lower: true, norename: true);
|
|
|
|
|
|
|
|
|
|
|
|
// Return the used index
|
|
|
|
|
|
return _itemIndex - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:44:04 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Bucketing
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update the bucketing dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bucketBy">ItemKey enum representing how to bucket the individual items</param>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// <param name="dedupeType">Dedupe type that should be used</param>
|
2024-03-13 02:44:04 -04:00
|
|
|
|
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
|
|
|
|
|
|
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
public void BucketBy(ItemKey bucketBy, DedupeType dedupeType, bool lower = true, bool norename = true)
|
2024-03-13 02:44:04 -04:00
|
|
|
|
{
|
2024-03-13 12:00:39 -04:00
|
|
|
|
// If the sorted type isn't the same, we want to sort the dictionary accordingly
|
|
|
|
|
|
if (_bucketedBy != bucketBy && bucketBy != ItemKey.NULL)
|
|
|
|
|
|
PerformBucketing(bucketBy, lower, norename);
|
2024-03-13 02:44:04 -04:00
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
// If the merge type isn't the same, we want to merge the dictionary accordingly
|
|
|
|
|
|
if (dedupeType != DedupeType.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
PerformDeduplication(bucketBy, dedupeType);
|
|
|
|
|
|
}
|
|
|
|
|
|
// If the merge type is the same, we want to sort the dictionary to be consistent
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
PerformSorting(norename);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-03-13 02:44:04 -04:00
|
|
|
|
|
2024-03-20 10:40:30 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// List all duplicates found in a DAT based on a DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datItem">Item to try to match</param>
|
|
|
|
|
|
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>List of matched DatItem objects</returns>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public Dictionary<long, DatItem> GetDuplicates(DatItem datItem, bool sorted = false)
|
2024-03-20 10:40:30 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
Dictionary<long, DatItem> output = [];
|
2024-03-20 10:40:30 -04:00
|
|
|
|
|
|
|
|
|
|
// Check for an empty rom list first
|
|
|
|
|
|
if (DatStatistics.TotalCount == 0)
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
|
|
// We want to get the proper key for the DatItem
|
|
|
|
|
|
string key = SortAndGetKey(datItem, sorted);
|
|
|
|
|
|
|
|
|
|
|
|
// If the key doesn't exist, return the empty list
|
|
|
|
|
|
var roms = GetItemsForBucket(key);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (roms == null || roms.Count == 0)
|
2024-03-20 10:40:30 -04:00
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
|
|
// Try to find duplicates
|
2024-12-06 23:16:09 -05:00
|
|
|
|
Dictionary<long, DatItem> left = [];
|
|
|
|
|
|
foreach (var rom in roms)
|
2024-03-20 10:40:30 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (rom.Value.GetBoolFieldValue(DatItem.RemoveKey) == true)
|
2024-03-20 10:40:30 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (datItem.Equals(rom.Value))
|
2024-03-20 10:40:30 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
rom.Value.SetFieldValue<bool?>(DatItem.RemoveKey, true);
|
|
|
|
|
|
output[rom.Key] = rom.Value;
|
2024-03-20 10:40:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
left[rom.Key] = rom.Value;
|
2024-03-20 10:40:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add back all roms with the proper flags
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[key] = [.. output.Keys, .. left.Keys];
|
2024-03-20 10:40:30 -04:00
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 23:15:58 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// List all duplicates found in a DAT based on a DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datItem">Item to try to match</param>
|
|
|
|
|
|
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>List of matched DatItem objects</returns>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public Dictionary<long, DatItem> GetDuplicates(KeyValuePair<long, DatItem> datItem, bool sorted = false)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
Dictionary<long, DatItem> output = [];
|
2024-03-19 23:15:58 -04:00
|
|
|
|
|
|
|
|
|
|
// Check for an empty rom list first
|
|
|
|
|
|
if (DatStatistics.TotalCount == 0)
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
|
|
// We want to get the proper key for the DatItem
|
|
|
|
|
|
string key = SortAndGetKey(datItem, sorted);
|
|
|
|
|
|
|
|
|
|
|
|
// If the key doesn't exist, return the empty list
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var roms = GetItemsForBucket(key);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (roms == null || roms.Count == 0)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
|
|
// Try to find duplicates
|
2024-12-06 23:16:09 -05:00
|
|
|
|
Dictionary<long, DatItem> left = [];
|
|
|
|
|
|
foreach (var rom in roms)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (rom.Value.GetBoolFieldValue(DatItem.RemoveKey) == true)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (datItem.Value.Equals(rom.Value))
|
2024-03-19 23:15:58 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
rom.Value.SetFieldValue<bool?>(DatItem.RemoveKey, true);
|
|
|
|
|
|
output[rom.Key] = rom.Value;
|
2024-03-19 23:15:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
left[rom.Key] = rom.Value;
|
2024-03-19 23:15:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add back all roms with the proper flags
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[key] = [.. output.Keys, .. left.Keys];
|
2024-03-19 23:15:58 -04:00
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Check if a DAT contains the given DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datItem">Item to try to match</param>
|
|
|
|
|
|
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>True if it contains the rom, false otherwise</returns>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
public bool HasDuplicates(KeyValuePair<long, DatItem> datItem, bool sorted = false)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Check for an empty rom list first
|
|
|
|
|
|
if (DatStatistics.TotalCount == 0)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// We want to get the proper key for the DatItem
|
|
|
|
|
|
string key = SortAndGetKey(datItem, sorted);
|
|
|
|
|
|
|
|
|
|
|
|
// If the key doesn't exist
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var roms = GetItemsForBucket(key);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (roms == null || roms.Count == 0)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Try to find duplicates
|
2024-12-06 23:16:09 -05:00
|
|
|
|
return roms.Values.Any(r => datItem.Equals(r));
|
2024-03-19 23:15:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Merge an arbitrary set of item pairs based on the supplied information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemMappings">List of pairs representing the items to be merged</param>
|
2025-01-07 14:55:56 -05:00
|
|
|
|
private List<KeyValuePair<long, DatItem>> Merge(List<KeyValuePair<long, DatItem>> itemMappings)
|
2024-03-13 12:00:39 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Check for null or blank roms first
|
2024-03-19 14:35:43 -04:00
|
|
|
|
if (itemMappings == null || itemMappings.Count == 0)
|
2024-03-13 12:00:39 -04:00
|
|
|
|
return [];
|
2024-03-13 02:44:04 -04:00
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
// Create output list
|
2024-12-06 23:16:09 -05:00
|
|
|
|
List<KeyValuePair<long, DatItem>> output = [];
|
2024-03-13 12:00:39 -04:00
|
|
|
|
|
|
|
|
|
|
// Then deduplicate them by checking to see if data matches previous saved roms
|
|
|
|
|
|
int nodumpCount = 0;
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var kvp in itemMappings)
|
2024-03-13 02:44:04 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
long itemIndex = kvp.Key;
|
|
|
|
|
|
DatItem datItem = kvp.Value;
|
2024-03-13 12:00:39 -04:00
|
|
|
|
|
|
|
|
|
|
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
|
|
|
|
|
|
if (datItem is not Disk && datItem is not DatItems.Formats.File && datItem is not Media && datItem is not Rom)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If it's a nodump, add and skip
|
|
|
|
|
|
if (datItem is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
output.Add(new KeyValuePair<long, DatItem>(itemIndex, datItem));
|
2024-03-13 12:00:39 -04:00
|
|
|
|
nodumpCount++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (datItem is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
output.Add(new KeyValuePair<long, DatItem>(itemIndex, datItem));
|
2024-03-13 12:00:39 -04:00
|
|
|
|
nodumpCount++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-01-07 14:55:56 -05:00
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
// If it's the first non-nodump rom in the list, don't touch it
|
2025-01-07 14:55:56 -05:00
|
|
|
|
if (output.Count == nodumpCount)
|
2024-03-13 12:00:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
output.Add(new KeyValuePair<long, DatItem>(itemIndex, datItem));
|
2024-03-13 12:00:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-07 14:55:56 -05:00
|
|
|
|
// Find the index of the first duplicate, if one exists
|
|
|
|
|
|
int pos = output.FindIndex(lastItem => datItem.GetDuplicateStatus(lastItem.Value) != 0x00);
|
|
|
|
|
|
if (pos < 0)
|
2024-03-13 12:00:39 -04:00
|
|
|
|
{
|
2025-01-07 14:55:56 -05:00
|
|
|
|
output.Add(new KeyValuePair<long, DatItem>(itemIndex, datItem));
|
|
|
|
|
|
continue;
|
2024-03-13 12:00:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-07 14:55:56 -05:00
|
|
|
|
// Get the duplicate item
|
|
|
|
|
|
long savedIndex = output[pos].Key;
|
|
|
|
|
|
DatItem savedItem = output[pos].Value;
|
|
|
|
|
|
DupeType dupetype = datItem.GetDuplicateStatus(savedItem);
|
|
|
|
|
|
|
|
|
|
|
|
// Disks, Media, and Roms have more information to fill
|
|
|
|
|
|
if (datItem is Disk diskItem && savedItem is Disk savedDisk)
|
|
|
|
|
|
savedDisk.FillMissingInformation(diskItem);
|
|
|
|
|
|
else if (datItem is DatItems.Formats.File fileItem && savedItem is DatItems.Formats.File savedFile)
|
|
|
|
|
|
savedFile.FillMissingInformation(fileItem);
|
|
|
|
|
|
else if (datItem is Media mediaItem && savedItem is Media savedMedia)
|
|
|
|
|
|
savedMedia.FillMissingInformation(mediaItem);
|
|
|
|
|
|
else if (datItem is Rom romItem && savedItem is Rom savedRom)
|
|
|
|
|
|
savedRom.FillMissingInformation(romItem);
|
|
|
|
|
|
|
|
|
|
|
|
savedItem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
|
|
|
|
|
|
|
|
|
|
|
// Get the sources associated with the items
|
|
|
|
|
|
var savedSource = _sources[_itemToSourceMapping[savedIndex]];
|
|
|
|
|
|
var itemSource = _sources[_itemToSourceMapping[itemIndex]];
|
|
|
|
|
|
|
|
|
|
|
|
// Get the machines associated with the items
|
|
|
|
|
|
var savedMachine = _machines[_itemToMachineMapping[savedIndex]];
|
|
|
|
|
|
var itemMachine = _machines[_itemToMachineMapping[itemIndex]];
|
|
|
|
|
|
|
2025-01-07 15:03:27 -05:00
|
|
|
|
// If the current source has a lower ID than the saved, use the saved source
|
2025-01-07 14:55:56 -05:00
|
|
|
|
if (itemSource?.Index < savedSource?.Index)
|
2024-03-13 12:00:39 -04:00
|
|
|
|
{
|
2025-01-07 14:55:56 -05:00
|
|
|
|
_itemToSourceMapping[itemIndex] = _itemToSourceMapping[savedIndex];
|
|
|
|
|
|
_machines[_itemToMachineMapping[savedIndex]] = (itemMachine.Clone() as Machine)!;
|
|
|
|
|
|
savedItem.SetName(datItem.GetName());
|
2024-03-13 12:00:39 -04:00
|
|
|
|
}
|
2025-01-07 14:55:56 -05:00
|
|
|
|
|
2025-01-07 15:03:27 -05:00
|
|
|
|
// If the saved machine is a child of the current machine, use the current machine instead
|
2025-01-07 14:55:56 -05:00
|
|
|
|
if (savedMachine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey) == itemMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey)
|
|
|
|
|
|
|| savedMachine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey) == itemMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey))
|
2024-03-13 12:00:39 -04:00
|
|
|
|
{
|
2025-01-07 14:55:56 -05:00
|
|
|
|
_machines[_itemToMachineMapping[savedIndex]] = (itemMachine.Clone() as Machine)!;
|
|
|
|
|
|
savedItem.SetName(datItem.GetName());
|
2024-03-13 12:00:39 -04:00
|
|
|
|
}
|
2025-01-07 14:55:56 -05:00
|
|
|
|
|
|
|
|
|
|
// Replace the original item in the list
|
|
|
|
|
|
output.RemoveAt(pos);
|
|
|
|
|
|
output.Insert(pos, new KeyValuePair<long, DatItem>(savedIndex, savedItem));
|
2024-03-13 02:44:04 -04:00
|
|
|
|
}
|
2024-03-13 10:14:04 -04:00
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
return output;
|
2024-03-13 02:44:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 23:15:58 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the highest-order Field value that represents the statistics
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private ItemKey GetBestAvailable()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the required counts
|
|
|
|
|
|
long diskCount = DatStatistics.GetItemCount(ItemType.Disk);
|
|
|
|
|
|
long mediaCount = DatStatistics.GetItemCount(ItemType.Media);
|
|
|
|
|
|
long romCount = DatStatistics.GetItemCount(ItemType.Rom);
|
|
|
|
|
|
long nodumpCount = DatStatistics.GetStatusCount(ItemStatus.Nodump);
|
|
|
|
|
|
|
|
|
|
|
|
// If all items are supposed to have a SHA-512, we bucket by that
|
|
|
|
|
|
if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.SHA512))
|
|
|
|
|
|
return ItemKey.SHA512;
|
|
|
|
|
|
|
|
|
|
|
|
// If all items are supposed to have a SHA-384, we bucket by that
|
|
|
|
|
|
else if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.SHA384))
|
|
|
|
|
|
return ItemKey.SHA384;
|
|
|
|
|
|
|
|
|
|
|
|
// If all items are supposed to have a SHA-256, we bucket by that
|
|
|
|
|
|
else if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.SHA256))
|
|
|
|
|
|
return ItemKey.SHA256;
|
|
|
|
|
|
|
|
|
|
|
|
// If all items are supposed to have a SHA-1, we bucket by that
|
|
|
|
|
|
else if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.SHA1))
|
|
|
|
|
|
return ItemKey.SHA1;
|
|
|
|
|
|
|
|
|
|
|
|
// If all items are supposed to have a MD5, we bucket by that
|
|
|
|
|
|
else if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.MD5))
|
|
|
|
|
|
return ItemKey.MD5;
|
|
|
|
|
|
|
2025-01-09 05:44:34 -05:00
|
|
|
|
// If all items are supposed to have a MD4, we bucket by that
|
|
|
|
|
|
else if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.MD4))
|
|
|
|
|
|
return ItemKey.MD4;
|
2025-01-09 05:26:36 -05:00
|
|
|
|
|
2025-01-09 05:44:34 -05:00
|
|
|
|
// If all items are supposed to have a MD2, we bucket by that
|
|
|
|
|
|
else if (diskCount + mediaCount + romCount - nodumpCount == DatStatistics.GetHashCount(HashType.MD2))
|
|
|
|
|
|
return ItemKey.MD2;
|
2025-01-09 05:26:36 -05:00
|
|
|
|
|
2024-03-19 23:15:58 -04:00
|
|
|
|
// Otherwise, we bucket by CRC
|
|
|
|
|
|
else
|
|
|
|
|
|
return ItemKey.CRC;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:44:04 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the bucketing key for a given item index
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// <param name="itemIndex">Index of the current item</param>
|
|
|
|
|
|
/// <param name="bucketBy">ItemKey value representing what key to get</param>
|
|
|
|
|
|
/// <param name="lower">True if the key should be lowercased, false otherwise</param>
|
|
|
|
|
|
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
2024-03-13 02:44:04 -04:00
|
|
|
|
/// </summary>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
private string GetBucketKey(long itemIndex, ItemKey bucketBy, bool lower, bool norename)
|
2024-03-13 02:44:04 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!_items.ContainsKey(itemIndex))
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
var datItem = _items[itemIndex];
|
|
|
|
|
|
if (datItem == null)
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
2024-03-20 01:50:08 -04:00
|
|
|
|
var machine = GetMachineForItem(itemIndex);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (machine.Value == null)
|
2024-03-13 02:44:04 -04:00
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
2024-03-20 01:50:08 -04:00
|
|
|
|
var source = GetSourceForItem(itemIndex);
|
2024-03-13 02:44:04 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string sourceKeyPadded = source.Value?.Index.ToString().PadLeft(10, '0') + '-';
|
|
|
|
|
|
string machineName = machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? "Default";
|
2024-03-13 12:00:39 -04:00
|
|
|
|
|
|
|
|
|
|
string bucketKey = bucketBy switch
|
2024-03-13 02:44:04 -04:00
|
|
|
|
{
|
2024-03-13 12:00:39 -04:00
|
|
|
|
ItemKey.Machine => (norename ? string.Empty : sourceKeyPadded) + machineName,
|
2024-03-13 02:44:04 -04:00
|
|
|
|
_ => GetBucketHashValue(datItem, bucketBy),
|
|
|
|
|
|
};
|
2024-03-13 12:00:39 -04:00
|
|
|
|
|
|
|
|
|
|
if (lower)
|
|
|
|
|
|
bucketKey = bucketKey.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
|
|
return bucketKey;
|
2024-03-13 02:44:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the hash value for a given item, if possible
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static string GetBucketHashValue(DatItem datItem, ItemKey bucketBy)
|
|
|
|
|
|
{
|
|
|
|
|
|
return datItem switch
|
|
|
|
|
|
{
|
|
|
|
|
|
Disk disk => bucketBy switch
|
|
|
|
|
|
{
|
2024-11-13 03:55:33 -05:00
|
|
|
|
ItemKey.CRC => ZeroHash.CRC32Str,
|
2025-01-09 05:44:34 -05:00
|
|
|
|
ItemKey.MD2 => ZeroHash.GetString(HashType.MD2),
|
|
|
|
|
|
ItemKey.MD4 => ZeroHash.GetString(HashType.MD4),
|
2024-03-13 02:44:04 -04:00
|
|
|
|
ItemKey.MD5 => disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA1 => disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key) ?? string.Empty,
|
2024-11-13 03:55:33 -05:00
|
|
|
|
ItemKey.SHA256 => ZeroHash.SHA256Str,
|
|
|
|
|
|
ItemKey.SHA384 => ZeroHash.SHA384Str,
|
|
|
|
|
|
ItemKey.SHA512 => ZeroHash.SHA512Str,
|
|
|
|
|
|
ItemKey.SpamSum => ZeroHash.SpamSumStr,
|
2024-03-13 02:44:04 -04:00
|
|
|
|
_ => string.Empty,
|
|
|
|
|
|
},
|
|
|
|
|
|
Media media => bucketBy switch
|
|
|
|
|
|
{
|
2024-11-13 03:55:33 -05:00
|
|
|
|
ItemKey.CRC => ZeroHash.CRC32Str,
|
2025-01-09 05:44:34 -05:00
|
|
|
|
ItemKey.MD2 => ZeroHash.GetString(HashType.MD2),
|
|
|
|
|
|
ItemKey.MD4 => ZeroHash.GetString(HashType.MD4),
|
2024-03-13 02:44:04 -04:00
|
|
|
|
ItemKey.MD5 => media.GetStringFieldValue(Models.Metadata.Media.MD5Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA1 => media.GetStringFieldValue(Models.Metadata.Media.SHA1Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA256 => media.GetStringFieldValue(Models.Metadata.Media.SHA256Key) ?? string.Empty,
|
2024-11-13 03:55:33 -05:00
|
|
|
|
ItemKey.SHA384 => ZeroHash.SHA384Str,
|
|
|
|
|
|
ItemKey.SHA512 => ZeroHash.SHA512Str,
|
2024-03-13 02:44:04 -04:00
|
|
|
|
ItemKey.SpamSum => media.GetStringFieldValue(Models.Metadata.Media.SpamSumKey) ?? string.Empty,
|
|
|
|
|
|
_ => string.Empty,
|
|
|
|
|
|
},
|
|
|
|
|
|
Rom rom => bucketBy switch
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemKey.CRC => rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey) ?? string.Empty,
|
2025-01-09 05:44:34 -05:00
|
|
|
|
ItemKey.MD2 => rom.GetStringFieldValue(Models.Metadata.Rom.MD2Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.MD4 => rom.GetStringFieldValue(Models.Metadata.Rom.MD4Key) ?? string.Empty,
|
2024-03-13 02:44:04 -04:00
|
|
|
|
ItemKey.MD5 => rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA1 => rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA256 => rom.GetStringFieldValue(Models.Metadata.Rom.SHA256Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA384 => rom.GetStringFieldValue(Models.Metadata.Rom.SHA384Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SHA512 => rom.GetStringFieldValue(Models.Metadata.Rom.SHA512Key) ?? string.Empty,
|
|
|
|
|
|
ItemKey.SpamSum => rom.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey) ?? string.Empty,
|
|
|
|
|
|
_ => string.Empty,
|
|
|
|
|
|
},
|
|
|
|
|
|
_ => bucketBy switch
|
|
|
|
|
|
{
|
2024-11-13 03:55:33 -05:00
|
|
|
|
ItemKey.CRC => ZeroHash.CRC32Str,
|
2025-01-09 05:44:34 -05:00
|
|
|
|
ItemKey.MD2 => ZeroHash.GetString(HashType.MD2),
|
|
|
|
|
|
ItemKey.MD4 => ZeroHash.GetString(HashType.MD4),
|
2024-11-13 03:55:33 -05:00
|
|
|
|
ItemKey.MD5 => ZeroHash.MD5Str,
|
|
|
|
|
|
ItemKey.SHA1 => ZeroHash.SHA1Str,
|
|
|
|
|
|
ItemKey.SHA256 => ZeroHash.SHA256Str,
|
|
|
|
|
|
ItemKey.SHA384 => ZeroHash.SHA384Str,
|
|
|
|
|
|
ItemKey.SHA512 => ZeroHash.SHA512Str,
|
|
|
|
|
|
ItemKey.SpamSum => ZeroHash.SpamSumStr,
|
2024-03-13 02:44:04 -04:00
|
|
|
|
_ => string.Empty,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensure the key exists in the items dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void EnsureBucketingKey(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the key is missing from the dictionary, add it
|
|
|
|
|
|
if (!_buckets.ContainsKey(key))
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_buckets.TryAdd(key, []);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_buckets[key] = [];
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Perform bucketing based on the item key provided
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bucketBy">ItemKey enum representing how to bucket the individual items</param>
|
|
|
|
|
|
/// <param name="lower">True if the key should be lowercased, false otherwise</param>
|
|
|
|
|
|
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
|
|
|
|
|
private void PerformBucketing(ItemKey bucketBy, bool lower, bool norename)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Reset the bucketing values
|
|
|
|
|
|
_bucketedBy = bucketBy;
|
|
|
|
|
|
_buckets.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
// Get the current list of item indicies
|
|
|
|
|
|
long[] itemIndicies = [.. _items.Keys];
|
|
|
|
|
|
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.For(0, itemIndicies.Length, Core.Globals.ParallelOptions, i =>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.For(0, itemIndicies.Length, i =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
for (int i = 0; i < itemIndicies.Length; i++)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
2024-03-19 12:53:38 -04:00
|
|
|
|
PerformItemBucketing(i, bucketBy, lower, norename);
|
2024-03-13 12:00:39 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 12:53:38 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Bucket a single DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemIndex">Index of the item to bucket</param>
|
|
|
|
|
|
/// <param name="bucketBy">ItemKey enum representing how to bucket the individual items</param>
|
|
|
|
|
|
/// <param name="lower">True if the key should be lowercased, false otherwise</param>
|
|
|
|
|
|
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
|
|
|
|
|
private void PerformItemBucketing(long itemIndex, ItemKey bucketBy, bool lower, bool norename)
|
|
|
|
|
|
{
|
|
|
|
|
|
string? bucketKey = GetBucketKey(itemIndex, bucketBy, lower, norename);
|
|
|
|
|
|
EnsureBucketingKey(bucketKey);
|
|
|
|
|
|
_buckets[bucketKey].Add(itemIndex);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Perform deduplication based on the deduplication type provided
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bucketBy">ItemKey enum representing how to bucket the individual items</param>
|
|
|
|
|
|
/// <param name="dedupeType">Dedupe type that should be used</param>
|
|
|
|
|
|
private void PerformDeduplication(ItemKey bucketBy, DedupeType dedupeType)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the current list of bucket keys
|
|
|
|
|
|
string[] bucketKeys = [.. _buckets.Keys];
|
|
|
|
|
|
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.For(0, bucketKeys.Length, Core.Globals.ParallelOptions, i =>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.For(0, bucketKeys.Length, i =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
for (int i = 0; i < bucketKeys.Length; i++)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
|
|
|
|
|
var itemIndices = _buckets[bucketKeys[i]];
|
2024-03-19 14:35:43 -04:00
|
|
|
|
if (itemIndices == null || itemIndices.Count == 0)
|
2024-03-13 12:00:39 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
continue;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
var datItems = itemIndices
|
2024-11-12 21:12:06 -05:00
|
|
|
|
.FindAll(i => _items.ContainsKey(i))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
.Select(i => new KeyValuePair<long, DatItem>(i, _items[i]))
|
|
|
|
|
|
.ToList();
|
2024-03-13 12:00:39 -04:00
|
|
|
|
|
|
|
|
|
|
Sort(ref datItems, false);
|
|
|
|
|
|
|
|
|
|
|
|
// If we're merging the roms, do so
|
|
|
|
|
|
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
|
2025-01-07 14:55:56 -05:00
|
|
|
|
datItems = Merge(datItems);
|
2024-03-13 12:00:39 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[bucketKeys[i]] = [.. datItems.Select(kvp => kvp.Key)];
|
2024-03-13 12:00:39 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 10:14:04 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sort existing buckets for consistency
|
|
|
|
|
|
/// </summary>
|
2024-03-13 11:02:17 -04:00
|
|
|
|
private void PerformSorting(bool norename)
|
2024-03-13 10:14:04 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Get the current list of bucket keys
|
|
|
|
|
|
string[] bucketKeys = [.. _buckets.Keys];
|
|
|
|
|
|
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.For(0, bucketKeys.Length, Core.Globals.ParallelOptions, i =>
|
2024-03-13 10:14:04 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.For(0, bucketKeys.Length, i =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
for (int i = 0; i < bucketKeys.Length; i++)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
|
|
|
|
|
var itemIndices = _buckets[bucketKeys[i]];
|
2024-03-19 14:35:43 -04:00
|
|
|
|
if (itemIndices == null || itemIndices.Count == 0)
|
2024-03-13 10:14:04 -04:00
|
|
|
|
{
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_buckets.TryRemove(bucketKeys[i], out _);
|
|
|
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
_buckets.Remove(bucketKeys[i]);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var datItems = itemIndices
|
2024-12-06 13:57:48 -05:00
|
|
|
|
.FindAll(i => _items.ContainsKey(i))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
.Select(i => new KeyValuePair<long, DatItem>(i, _items[i]))
|
|
|
|
|
|
.ToList();
|
2024-03-13 10:14:04 -04:00
|
|
|
|
|
|
|
|
|
|
Sort(ref datItems, norename);
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[bucketKeys[i]] = [.. datItems.Select(kvp => kvp.Key)];
|
2024-03-13 10:14:04 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// Sort a list of item pairs by SourceID, Game, and Name (in order)
|
2024-03-13 10:14:04 -04:00
|
|
|
|
/// </summary>
|
2024-03-13 12:00:39 -04:00
|
|
|
|
/// <param name="itemMappings">List of pairs representing the items to be sorted</param>
|
2024-03-13 10:14:04 -04:00
|
|
|
|
/// <param name="norename">True if files are not renamed, false otherwise</param>
|
|
|
|
|
|
/// <returns>True if it sorted correctly, false otherwise</returns>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
private bool Sort(ref List<KeyValuePair<long, DatItem>> itemMappings, bool norename)
|
2024-03-13 10:14:04 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
itemMappings.Sort(delegate (KeyValuePair<long, DatItem> x, KeyValuePair<long, DatItem> y)
|
2024-03-13 10:14:04 -04:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-03-13 10:27:22 -04:00
|
|
|
|
var nc = new NaturalComparer();
|
2024-03-13 10:14:04 -04:00
|
|
|
|
|
2024-03-13 10:45:08 -04:00
|
|
|
|
// If machine names don't match
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? xMachineName = _machines[_itemToMachineMapping[x.Key]].GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
|
|
|
|
|
string? yMachineName = _machines[_itemToMachineMapping[y.Key]].GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
2024-03-13 10:43:05 -04:00
|
|
|
|
if (xMachineName != yMachineName)
|
|
|
|
|
|
return nc.Compare(xMachineName, yMachineName);
|
|
|
|
|
|
|
|
|
|
|
|
// If types don't match
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? xType = x.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
|
|
|
|
|
|
string? yType = y.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
|
2024-03-13 10:43:05 -04:00
|
|
|
|
if (xType != yType)
|
2024-03-13 10:14:04 -04:00
|
|
|
|
return xType.AsEnumValue<ItemType>() - yType.AsEnumValue<ItemType>();
|
|
|
|
|
|
|
2024-03-13 10:43:05 -04:00
|
|
|
|
// If directory names don't match
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName()));
|
|
|
|
|
|
string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName()));
|
2024-03-13 10:43:05 -04:00
|
|
|
|
if (xDirectoryName != yDirectoryName)
|
|
|
|
|
|
return nc.Compare(xDirectoryName, yDirectoryName);
|
|
|
|
|
|
|
|
|
|
|
|
// If item names don't match
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName()));
|
|
|
|
|
|
string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName()));
|
2024-03-13 10:43:05 -04:00
|
|
|
|
if (xName != yName)
|
|
|
|
|
|
return nc.Compare(xName, yName);
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, compare on machine or source, depending on the flag
|
2024-12-06 23:16:09 -05:00
|
|
|
|
int? xSourceIndex = GetSourceForItem(x.Key).Value?.Index;
|
|
|
|
|
|
int? ySourceIndex = GetSourceForItem(y.Key).Value?.Index;
|
2024-03-13 10:45:08 -04:00
|
|
|
|
return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0);
|
2024-03-13 10:14:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// Absorb the error
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-20 10:40:30 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sort the input DAT and get the key to be used by the item
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datItem">Item to try to match</param>
|
|
|
|
|
|
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>Key to try to use</returns>
|
|
|
|
|
|
private string SortAndGetKey(DatItem datItem, bool sorted = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we're not already sorted, take care of it
|
|
|
|
|
|
if (!sorted)
|
|
|
|
|
|
BucketBy(GetBestAvailable(), DedupeType.None);
|
|
|
|
|
|
|
|
|
|
|
|
// Now that we have the sorted type, we get the proper key
|
2025-01-08 11:44:40 -05:00
|
|
|
|
return datItem.GetKeyDB(_bucketedBy, null);
|
2024-03-20 10:40:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 23:15:58 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sort the input DAT and get the key to be used by the item
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datItem">Item to try to match</param>
|
|
|
|
|
|
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>Key to try to use</returns>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
private string SortAndGetKey(KeyValuePair<long, DatItem> datItem, bool sorted = false)
|
2024-03-19 23:15:58 -04:00
|
|
|
|
{
|
|
|
|
|
|
// If we're not already sorted, take care of it
|
|
|
|
|
|
if (!sorted)
|
|
|
|
|
|
BucketBy(GetBestAvailable(), DedupeType.None);
|
|
|
|
|
|
|
|
|
|
|
|
// Now that we have the sorted type, we get the proper key
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var source = GetSourceForItem(datItem.Key);
|
2025-01-08 11:44:40 -05:00
|
|
|
|
return datItem.Value.GetKeyDB(_bucketedBy, source.Value);
|
2024-03-19 23:15:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 02:44:04 -04:00
|
|
|
|
#endregion
|
2024-03-13 11:05:34 -04:00
|
|
|
|
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#region Filtering
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Execute all filters in a filter runner on the items in the dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filterRunner">Preconfigured filter runner to use</param>
|
|
|
|
|
|
public void ExecuteFilters(FilterRunner filterRunner)
|
|
|
|
|
|
{
|
2024-03-19 15:21:01 -04:00
|
|
|
|
List<string> keys = [.. SortedKeys];
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.ForEach(keys, key =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
foreach (var key in keys)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
2024-03-19 14:10:51 -04:00
|
|
|
|
ExecuteFilterOnBucket(filterRunner, key);
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-03-19 14:10:51 -04:00
|
|
|
|
});
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#else
|
2024-03-19 14:10:51 -04:00
|
|
|
|
}
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#endif
|
2024-03-19 14:10:51 -04:00
|
|
|
|
}
|
2024-03-19 14:08:46 -04:00
|
|
|
|
|
2024-03-19 16:10:00 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use game descriptions as names, updating cloneof/romof/sampleof
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
|
|
|
|
|
public void MachineDescriptionToName(bool throwOnError = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// First we want to get a mapping for all games to description
|
2024-10-19 22:39:23 -04:00
|
|
|
|
var mapping = CreateMachineToDescriptionMapping();
|
2024-03-19 16:10:00 -04:00
|
|
|
|
|
|
|
|
|
|
// Now we loop through every item and update accordingly
|
2024-03-19 16:56:55 -04:00
|
|
|
|
UpdateMachineNamesFromDescriptions(mapping);
|
2024-03-19 16:10:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Warning(ex.ToString());
|
2024-03-19 16:10:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 22:10:59 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Filter a DAT using 1G1R logic given an ordered set of regions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="regionList">List of regions in order of priority</param>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// In the most technical sense, the way that the region list is being used does not
|
|
|
|
|
|
/// confine its values to be just regions. Since it's essentially acting like a
|
|
|
|
|
|
/// specialized version of the machine name filter, anything that is usually encapsulated
|
|
|
|
|
|
/// in parenthesis would be matched on, including disc numbers, languages, editions,
|
|
|
|
|
|
/// and anything else commonly used. Please note that, unlike other existing 1G1R
|
|
|
|
|
|
/// solutions, this does not have the ability to contain custom mappings of parent
|
|
|
|
|
|
/// to clone sets based on name, nor does it have the ability to match on the
|
|
|
|
|
|
/// Release DatItem type.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public void SetOneGamePerRegion(List<string> regionList)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we have null region list, make it empty
|
|
|
|
|
|
regionList ??= [];
|
|
|
|
|
|
|
|
|
|
|
|
// For sake of ease, the first thing we want to do is bucket by game
|
|
|
|
|
|
BucketBy(ItemKey.Machine, DedupeType.None, norename: true);
|
|
|
|
|
|
|
|
|
|
|
|
// Then we want to get a mapping of all machines to parents
|
|
|
|
|
|
Dictionary<string, List<string>> parents = [];
|
|
|
|
|
|
foreach (string key in SortedKeys)
|
|
|
|
|
|
{
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(key);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 22:10:59 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var item = items.First();
|
|
|
|
|
|
var machine = GetMachineForItem(item.Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 22:10:59 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-07-15 12:48:26 -04:00
|
|
|
|
// Get machine information
|
2024-12-06 23:16:09 -05:00
|
|
|
|
Machine? machineObj = machine.Value.GetFieldValue<Machine>(DatItem.MachineKey);
|
2024-07-15 12:48:26 -04:00
|
|
|
|
string? machineName = machineObj?.GetStringFieldValue(Models.Metadata.Machine.NameKey)?.ToLowerInvariant();
|
|
|
|
|
|
if (machineObj == null || machineName == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the string values
|
|
|
|
|
|
string? cloneOf = machineObj.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)?.ToLowerInvariant();
|
|
|
|
|
|
string? romOf = machineObj.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)?.ToLowerInvariant();
|
|
|
|
|
|
|
2024-03-19 22:10:59 -04:00
|
|
|
|
// Match on CloneOf first
|
2024-07-15 12:48:26 -04:00
|
|
|
|
if (!string.IsNullOrEmpty(cloneOf))
|
2024-03-19 22:10:59 -04:00
|
|
|
|
{
|
2024-07-15 12:48:26 -04:00
|
|
|
|
if (!parents.ContainsKey(cloneOf!))
|
|
|
|
|
|
parents.Add(cloneOf!, []);
|
2024-03-19 22:10:59 -04:00
|
|
|
|
|
2024-07-15 12:48:26 -04:00
|
|
|
|
parents[cloneOf!].Add(machineName);
|
2024-03-19 22:10:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Then by RomOf
|
2024-07-15 12:48:26 -04:00
|
|
|
|
else if (!string.IsNullOrEmpty(romOf))
|
2024-03-19 22:10:59 -04:00
|
|
|
|
{
|
2024-07-15 12:48:26 -04:00
|
|
|
|
if (!parents.ContainsKey(romOf!))
|
|
|
|
|
|
parents.Add(romOf!, []);
|
2024-03-19 22:10:59 -04:00
|
|
|
|
|
2024-07-15 12:48:26 -04:00
|
|
|
|
parents[romOf!].Add(machineName);
|
2024-03-19 22:10:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, treat it as a parent
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-07-15 12:48:26 -04:00
|
|
|
|
if (!parents.ContainsKey(machineName))
|
|
|
|
|
|
parents.Add(machineName, []);
|
2024-03-19 22:10:59 -04:00
|
|
|
|
|
2024-07-15 12:48:26 -04:00
|
|
|
|
parents[machineName].Add(machineName);
|
2024-03-19 22:10:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Once we have the full list of mappings, filter out games to keep
|
|
|
|
|
|
foreach (string key in parents.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Find the first machine that matches the regions in order, if possible
|
|
|
|
|
|
string? machine = default;
|
|
|
|
|
|
foreach (string region in regionList)
|
|
|
|
|
|
{
|
2024-11-12 21:12:06 -05:00
|
|
|
|
machine = parents[key].Find(m => Regex.IsMatch(m, @"\(.*" + region + @".*\)", RegexOptions.IgnoreCase));
|
2024-03-19 22:10:59 -04:00
|
|
|
|
if (machine != default)
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we didn't get a match, use the parent
|
|
|
|
|
|
if (machine == default)
|
|
|
|
|
|
machine = key;
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the key from the list
|
|
|
|
|
|
parents[key].Remove(machine);
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the rest of the items from this key
|
|
|
|
|
|
parents[key].ForEach(k => RemoveMachine(k));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Finally, strip out the parent tags
|
|
|
|
|
|
RemoveTagsFromChild();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 16:18:35 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensure that all roms are in their own game (or at least try to ensure)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void SetOneRomPerGame()
|
|
|
|
|
|
{
|
|
|
|
|
|
// For each rom, we want to update the game to be "<game name>/<rom name>"
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.ForEach(SortedKeys, Core.Globals.ParallelOptions, key =>
|
2024-03-19 16:18:35 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.ForEach(SortedKeys, key =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
foreach (var key in SortedKeys)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(key);
|
2024-03-19 16:18:35 -04:00
|
|
|
|
if (items == null)
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
continue;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in items)
|
2024-03-19 16:18:35 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
SetOneRomPerGame(item);
|
2024-03-19 16:18:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 16:22:19 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Strip the dates from the beginning of scene-style set names
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void StripSceneDatesFromItems()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Set the regex pattern to use
|
|
|
|
|
|
string pattern = @"([0-9]{2}\.[0-9]{2}\.[0-9]{2}-)(.*?-.*?)";
|
|
|
|
|
|
|
|
|
|
|
|
// Now process all of the roms
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.ForEach(SortedKeys, Core.Globals.ParallelOptions, key =>
|
2024-03-19 16:22:19 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.ForEach(SortedKeys, key =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
foreach (var key in SortedKeys)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(key);
|
2024-03-19 16:22:19 -04:00
|
|
|
|
if (items == null)
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
continue;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
List<long> itemIds = [];
|
|
|
|
|
|
foreach (var item in items)
|
2024-03-19 16:22:19 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(item.Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:14:07 -04:00
|
|
|
|
continue;
|
2024-03-19 16:22:19 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (Regex.IsMatch(machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)!, pattern))
|
|
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Regex.Replace(machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)!, pattern, "$2"));
|
2024-03-19 21:14:07 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (Regex.IsMatch(machine.Value.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!, pattern))
|
|
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Regex.Replace(machine.Value.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!, pattern, "$2"));
|
2024-03-19 16:22:19 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
itemIds.Add(item.Key);
|
2024-03-19 16:22:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[key] = itemIds;
|
2024-03-19 16:22:19 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 16:56:55 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create machine to description mapping dictionary
|
|
|
|
|
|
/// </summary>
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private IDictionary<string, string> CreateMachineToDescriptionMapping()
|
2024-03-19 16:56:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
ConcurrentDictionary<string, string> mapping = new();
|
|
|
|
|
|
#else
|
|
|
|
|
|
Dictionary<string, string> mapping = [];
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.ForEach(SortedKeys, Core.Globals.ParallelOptions, key =>
|
2024-03-19 16:56:55 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.ForEach(SortedKeys, key =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
foreach (var key in SortedKeys)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(key);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
if (items == null)
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
continue;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in items)
|
2024-03-19 16:56:55 -04:00
|
|
|
|
{
|
2024-03-19 21:14:07 -04:00
|
|
|
|
// Get the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(item.Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:14:07 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-03-19 16:56:55 -04:00
|
|
|
|
// If the key mapping doesn't exist, add it
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
2024-12-06 23:16:09 -05:00
|
|
|
|
mapping.TryAdd(machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)!,
|
|
|
|
|
|
machine.Value.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -"));
|
2024-03-19 16:56:55 -04:00
|
|
|
|
#else
|
2024-12-06 23:16:09 -05:00
|
|
|
|
mapping[machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]
|
|
|
|
|
|
= machine.Value.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)!.Replace('/', '_').Replace("\"", "''").Replace(":", " -");
|
2024-03-19 16:56:55 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
return mapping;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 14:10:51 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Execute all filters in a filter runner on a single bucket
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filterRunner">Preconfigured filter runner to use</param>
|
|
|
|
|
|
/// <param name="bucketName">Name of the bucket to filter on</param>
|
|
|
|
|
|
private void ExecuteFilterOnBucket(FilterRunner filterRunner, string bucketName)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var items = GetItemsForBucket(bucketName);
|
2024-03-19 14:10:51 -04:00
|
|
|
|
if (items == null)
|
|
|
|
|
|
return;
|
2024-03-19 14:08:46 -04:00
|
|
|
|
|
2024-03-19 14:10:51 -04:00
|
|
|
|
// Filter all items in the current key
|
2024-12-06 23:16:09 -05:00
|
|
|
|
List<long> newItems = [];
|
2024-03-19 14:10:51 -04:00
|
|
|
|
foreach (var item in items)
|
|
|
|
|
|
{
|
2025-01-08 12:57:46 -05:00
|
|
|
|
if (item.Value.PassesFilterDB(filterRunner))
|
2024-12-06 23:16:09 -05:00
|
|
|
|
newItems.Add(item.Key);
|
2024-03-19 14:08:46 -04:00
|
|
|
|
}
|
2024-03-19 14:10:51 -04:00
|
|
|
|
|
|
|
|
|
|
// Set the value in the key to the new set
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[bucketName] = newItems;
|
2024-03-19 14:08:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 16:18:35 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set internal names to match One Rom Per Game (ORPG) logic
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datItem">DatItem to run logic on</param>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
private void SetOneRomPerGame(KeyValuePair<long, DatItem> datItem)
|
2024-03-19 16:18:35 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (datItem.Key < 0 || datItem.Value.GetName() == null)
|
2024-03-19 16:18:35 -04:00
|
|
|
|
return;
|
|
|
|
|
|
|
2024-03-19 21:14:07 -04:00
|
|
|
|
// Get the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(datItem.Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:14:07 -04:00
|
|
|
|
return;
|
|
|
|
|
|
|
2024-07-30 09:10:03 -04:00
|
|
|
|
// Remove extensions from Rom items
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string machineName = datItem.Value.GetName()!;
|
|
|
|
|
|
if (datItem.Value is Rom)
|
2024-07-30 09:10:03 -04:00
|
|
|
|
{
|
|
|
|
|
|
string[] splitname = machineName.Split('.');
|
2024-12-06 23:16:09 -05:00
|
|
|
|
machineName = datItem.Value.GetFieldValue<Machine>(DatItem.MachineKey)!
|
2024-12-06 13:57:48 -05:00
|
|
|
|
.GetStringFieldValue(Models.Metadata.Machine.NameKey)
|
|
|
|
|
|
+ $"/{string.Join(".", splitname, 0, splitname.Length > 1 ? splitname.Length - 1 : 1)}";
|
2024-07-30 09:10:03 -04:00
|
|
|
|
}
|
2024-07-03 10:59:35 -04:00
|
|
|
|
|
|
|
|
|
|
// Strip off "Default" prefix only for ORPG
|
|
|
|
|
|
if (machineName.StartsWith("Default"))
|
|
|
|
|
|
machineName = machineName.Substring("Default".Length + 1);
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machineName);
|
|
|
|
|
|
datItem.Value.SetName(Path.GetFileName(datItem.Value.GetName()));
|
2024-03-19 16:18:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 16:56:55 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update machine names from descriptions according to mappings
|
|
|
|
|
|
/// </summary>
|
2024-10-19 22:39:23 -04:00
|
|
|
|
private void UpdateMachineNamesFromDescriptions(IDictionary<string, string> mapping)
|
2024-03-19 16:56:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
#if NET452_OR_GREATER || NETCOREAPP
|
2024-10-24 05:58:03 -04:00
|
|
|
|
Parallel.ForEach(SortedKeys, Core.Globals.ParallelOptions, key =>
|
2024-03-19 16:56:55 -04:00
|
|
|
|
#elif NET40_OR_GREATER
|
|
|
|
|
|
Parallel.ForEach(SortedKeys, key =>
|
|
|
|
|
|
#else
|
|
|
|
|
|
foreach (var key in SortedKeys)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
{
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(key);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
if (items == null)
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
return;
|
|
|
|
|
|
#else
|
|
|
|
|
|
continue;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
List<long> newItems = [];
|
|
|
|
|
|
foreach (var item in items)
|
2024-03-19 16:56:55 -04:00
|
|
|
|
{
|
2024-03-19 21:14:07 -04:00
|
|
|
|
// Get the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(item.Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:14:07 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-03-19 16:56:55 -04:00
|
|
|
|
// Update machine name
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!string.IsNullOrEmpty(machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)) && mapping.ContainsKey(machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)!))
|
|
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, mapping[machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey)!]);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
|
|
|
|
|
|
// Update cloneof
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!string.IsNullOrEmpty(machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)) && mapping.ContainsKey(machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!))
|
|
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, mapping[machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey)!]);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
|
|
|
|
|
|
// Update romof
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!string.IsNullOrEmpty(machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)) && mapping.ContainsKey(machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!))
|
|
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, mapping[machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey)!]);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
|
|
|
|
|
|
// Update sampleof
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!string.IsNullOrEmpty(machine.Value.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)) && mapping.ContainsKey(machine.Value.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!))
|
|
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.SampleOfKey, mapping[machine.Value.GetStringFieldValue(Models.Metadata.Machine.SampleOfKey)!]);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
|
|
|
|
|
|
// Add the new item to the output list
|
2024-12-06 23:16:09 -05:00
|
|
|
|
newItems.Add(item.Key);
|
2024-03-19 16:56:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Replace the old list of roms with the new one
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_buckets[key] = newItems;
|
2024-03-19 16:56:55 -04:00
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
});
|
|
|
|
|
|
#else
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 14:08:46 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-03-19 20:41:39 -04:00
|
|
|
|
#region Splitting
|
|
|
|
|
|
|
2024-03-19 21:48:40 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use romof tags to add roms to the children
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void AddRomsFromBios()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the items for this game
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 21:48:40 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-03-20 01:29:59 -04:00
|
|
|
|
// Get the source for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var source = GetSourceForItem(items.First().Key);
|
2024-03-20 01:29:59 -04:00
|
|
|
|
|
2024-03-19 21:48:40 -04:00
|
|
|
|
// Get the machine for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:48:40 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the bios parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? romOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
|
2024-10-19 22:39:23 -04:00
|
|
|
|
if (string.IsNullOrEmpty(romOf))
|
2024-03-19 21:48:40 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent doesn't have any items, we want to continue
|
2024-10-19 22:50:42 -04:00
|
|
|
|
var parentItems = GetItemsForBucket(romOf!);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (parentItems == null || parentItems.Count == 0)
|
2024-03-19 21:48:40 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent exists and has items, we copy the items from the parent to the current game
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in parentItems)
|
2024-03-19 21:48:40 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
DatItem datItem = (item.Value.Clone() as DatItem)!;
|
|
|
|
|
|
if (items.Any(i => i.Value.GetName() == datItem.GetName())
|
|
|
|
|
|
&& items.Any(i => i.Value == datItem))
|
2024-11-12 21:12:06 -05:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
AddItem(datItem, machine.Key, source.Key);
|
2024-11-12 21:12:06 -05:00
|
|
|
|
}
|
2024-03-19 21:48:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 21:41:09 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use device_ref and optionally slotoption tags to add roms to the children
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dev">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>
|
|
|
|
|
|
public bool AddRomsFromDevices(bool dev, bool useSlotOptions)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool foundnew = false;
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-03-20 01:29:59 -04:00
|
|
|
|
// Get the source for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var source = GetSourceForItem(items.First().Key);
|
2024-03-20 01:29:59 -04:00
|
|
|
|
|
2024-03-19 21:41:09 -04:00
|
|
|
|
// Get the machine for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the machine (is/is not) a device, we want to continue
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (dev ^ (machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Get all device reference names from the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
List<string?> deviceReferences = items.Values
|
|
|
|
|
|
.Where(i => i is DeviceRef)
|
|
|
|
|
|
.Select(i => i as DeviceRef)
|
|
|
|
|
|
.Select(dr => dr!.GetName())
|
2024-03-19 21:41:09 -04:00
|
|
|
|
.Distinct()
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// Get all slot option names from the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
List<string?> slotOptions = items.Values
|
|
|
|
|
|
.Where(i => i is Slot)
|
|
|
|
|
|
.Select(i => i as Slot)
|
|
|
|
|
|
.Where(s => s!.SlotOptionsSpecified)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
.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
|
2024-10-19 21:41:08 -04:00
|
|
|
|
if (deviceReferences.Count > 0)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Loop through all names and check the corresponding machines
|
2024-11-12 21:12:06 -05:00
|
|
|
|
var newDeviceReferences = new HashSet<string>();
|
2024-03-19 21:41:09 -04:00
|
|
|
|
foreach (string? deviceReference in deviceReferences)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the device reference is invalid
|
|
|
|
|
|
if (deviceReference == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the machine doesn't exist then we continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var devItems = GetItemsForBucket(deviceReference);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (devItems == null || devItems.Count == 0)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Add to the list of new device reference names
|
2024-12-06 23:16:09 -05:00
|
|
|
|
newDeviceReferences.UnionWith(devItems.Values
|
|
|
|
|
|
.Where(i => i is DeviceRef)
|
|
|
|
|
|
.Select(i => (i as DeviceRef)!.GetName()!));
|
2024-03-19 21:41:09 -04:00
|
|
|
|
|
|
|
|
|
|
// Set new machine information and add to the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var copyFrom = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (copyFrom.Value == null)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in devItems.Values)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
// If the parent machine doesn't already contain this item, add it
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!items.Values.Any(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey)
|
|
|
|
|
|
&& i.GetName() == item.GetName()))
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Set that we found new items
|
|
|
|
|
|
foundnew = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Clone the item and then add it
|
2024-12-06 23:16:09 -05:00
|
|
|
|
DatItem datItem = (item.Clone() as DatItem)!;
|
|
|
|
|
|
AddItem(datItem, machine.Key, source.Key);
|
2024-03-19 21:41:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now that every device reference is accounted for, add the new list of device references, if they don't already exist
|
2024-11-12 21:12:06 -05:00
|
|
|
|
foreach (string deviceReference in newDeviceReferences)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (!deviceReferences.Contains(deviceReference))
|
|
|
|
|
|
{
|
|
|
|
|
|
var deviceRef = new DeviceRef();
|
|
|
|
|
|
deviceRef.SetName(deviceReference);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
AddItem(deviceRef, machine.Key, source.Key);
|
2024-03-19 21:41:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we're checking slotoptions
|
2024-10-19 21:41:08 -04:00
|
|
|
|
if (useSlotOptions && slotOptions.Count > 0)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Loop through all names and check the corresponding machines
|
2024-11-12 21:12:06 -05:00
|
|
|
|
var newSlotOptions = new HashSet<string>();
|
2024-03-19 21:41:09 -04:00
|
|
|
|
foreach (string? slotOption in slotOptions)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the slot option is invalid
|
|
|
|
|
|
if (slotOption == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the machine doesn't exist then we continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var slotItems = GetItemsForBucket(slotOption);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (slotItems == null || slotItems.Count == 0)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Add to the list of new slot option names
|
2024-12-06 23:16:09 -05:00
|
|
|
|
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)!)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
.Select(o => o.GetStringFieldValue(Models.Metadata.SlotOption.DevNameKey)!));
|
|
|
|
|
|
|
|
|
|
|
|
// Set new machine information and add to the current machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var copyFrom = GetMachineForItem(GetItemsForBucket(game)!.First().Key);
|
|
|
|
|
|
if (copyFrom.Value == null)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in slotItems.Values)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
// If the parent machine doesn't already contain this item, add it
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!items.Values.Any(i => i.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) == item.GetStringFieldValue(Models.Metadata.DatItem.TypeKey)
|
|
|
|
|
|
&& i.GetName() == item.GetName()))
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Set that we found new items
|
|
|
|
|
|
foundnew = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Clone the item and then add it
|
2024-12-06 23:16:09 -05:00
|
|
|
|
DatItem datItem = (item.Clone() as DatItem)!;
|
|
|
|
|
|
AddItem(datItem, machine.Key, source.Key);
|
2024-03-19 21:41:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now that every device is accounted for, add the new list of slot options, if they don't already exist
|
2024-11-12 21:12:06 -05:00
|
|
|
|
foreach (string slotOption in newSlotOptions)
|
2024-03-19 21:41:09 -04:00
|
|
|
|
{
|
|
|
|
|
|
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]);
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
AddItem(slotItem, machine.Key, source.Key);
|
2024-03-19 21:41:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return foundnew;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 21:25:50 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use cloneof tags to add roms to the children, setting the new romof tag in the process
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void AddRomsFromParent()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 21:25:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-03-20 01:29:59 -04:00
|
|
|
|
// Get the source for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var source = GetSourceForItem(items.First().Key);
|
2024-03-20 01:29:59 -04:00
|
|
|
|
|
2024-03-19 21:25:50 -04:00
|
|
|
|
// Get the machine for the first item in the list
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:25:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the clone parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
|
2024-10-19 22:39:23 -04:00
|
|
|
|
if (string.IsNullOrEmpty(cloneOf))
|
2024-03-19 21:25:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent doesn't have any items, we want to continue
|
2024-10-19 22:50:42 -04:00
|
|
|
|
var parentItems = GetItemsForBucket(cloneOf!);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (parentItems == null || parentItems.Count == 0)
|
2024-03-19 21:25:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent exists and has items, we copy the items from the parent to the current game
|
|
|
|
|
|
foreach (var item in parentItems)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
DatItem datItem = (DatItem)item.Value.Clone();
|
|
|
|
|
|
if (items.Values.Any(i => i.GetName()?.ToLowerInvariant() == datItem.GetName()?.ToLowerInvariant())
|
|
|
|
|
|
&& items.Values.Any(i => i == datItem))
|
2024-03-19 21:25:50 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
AddItem(datItem, machine.Key, source.Key);
|
2024-03-19 21:25:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the parent machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var parentMachine = GetMachineForItem(GetItemsForBucket(cloneOf!)!.First().Key);
|
|
|
|
|
|
if (parentMachine.Value == null)
|
2024-03-19 21:25:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Now we want to get the parent romof tag and put it in each of the items
|
2024-03-19 23:35:29 -04:00
|
|
|
|
items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? romof = parentMachine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
|
|
|
|
|
|
foreach (var key in items.Keys)
|
2024-03-19 21:25:50 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var itemMachine = GetMachineForItem(key);
|
|
|
|
|
|
if (itemMachine.Value == null)
|
2024-03-19 21:25:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
itemMachine.Value.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
|
2024-03-19 21:25:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 21:07:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use cloneof tags to add roms 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>
|
|
|
|
|
|
public void AddRomsFromChildren(bool subfolder, bool skipDedup)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the machine for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the clone parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
|
2024-10-19 22:39:23 -04:00
|
|
|
|
if (string.IsNullOrEmpty(cloneOf))
|
|
|
|
|
|
continue;
|
2024-03-19 21:07:47 -04:00
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the clone parent machine
|
|
|
|
|
|
var cloneOfMachine = GetMachine(cloneOf);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (cloneOfMachine.Value == null)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-03-19 23:35:29 -04:00
|
|
|
|
items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in items)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Get the parent items and current machine name
|
2024-10-19 22:50:42 -04:00
|
|
|
|
var parentItems = GetItemsForBucket(cloneOf!);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (parentItems == null || parentItems.Count == 0)
|
2024-11-12 21:12:06 -05:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? machineName = GetMachineForItem(item.Key).Value?
|
|
|
|
|
|
.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
|
|
|
|
|
|
// Special disk handling
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (item.Value is Disk disk)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
string? mergeTag = disk.GetStringFieldValue(Models.Metadata.Disk.MergeKey);
|
|
|
|
|
|
|
|
|
|
|
|
// If the merge tag exists and the parent already contains it, skip
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (mergeTag != null && parentItems.Values
|
|
|
|
|
|
.Where(i => i is Disk)
|
|
|
|
|
|
.Select(i => (i as Disk)!.GetName())
|
2024-03-19 21:07:47 -04:00
|
|
|
|
.Contains(mergeTag))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the merge tag exists but the parent doesn't contain it, add to parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
else if (mergeTag != null && !parentItems.Values
|
|
|
|
|
|
.Where(i => i is Disk)
|
|
|
|
|
|
.Select(i => (i as Disk)!.GetName())
|
2024-03-19 21:07:47 -04:00
|
|
|
|
.Contains(mergeTag))
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_itemToMachineMapping[item.Key] = cloneOfMachine.Key;
|
|
|
|
|
|
_buckets[cloneOf!].Add(item.Key);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If there is no merge tag, add to parent
|
|
|
|
|
|
else if (mergeTag == null)
|
|
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_itemToMachineMapping[item.Key] = cloneOfMachine.Key;
|
|
|
|
|
|
_buckets[cloneOf!].Add(item.Key);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Special rom handling
|
2024-12-06 23:16:09 -05:00
|
|
|
|
else if (item.Value is Rom rom)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
// If the merge tag exists and the parent already contains it, skip
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && parentItems.Values
|
|
|
|
|
|
.Where(i => i is Rom)
|
|
|
|
|
|
.Select(i => (i as Rom)!.GetName())
|
2024-03-19 21:07:47 -04:00
|
|
|
|
.Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey)))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the merge tag exists but the parent doesn't contain it, add to subfolder of parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
else if (rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey) != null && !parentItems.Values
|
|
|
|
|
|
.Where(i => i is Rom)
|
|
|
|
|
|
.Select(i => (i as Rom)!.GetName())
|
2024-03-19 21:07:47 -04:00
|
|
|
|
.Contains(rom.GetStringFieldValue(Models.Metadata.Rom.MergeKey)))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (subfolder)
|
|
|
|
|
|
rom.SetName($"{machineName}\\{rom.GetName()}");
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_itemToMachineMapping[item.Key] = cloneOfMachine.Key;
|
|
|
|
|
|
_buckets[cloneOf!].Add(item.Key);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent doesn't already contain this item, add to subfolder of parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
else if (!parentItems.Contains(item) || skipDedup)
|
2024-03-19 21:07:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (subfolder)
|
|
|
|
|
|
rom.SetName($"{machineName}\\{rom.GetName()}");
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_itemToMachineMapping[item.Key] = cloneOfMachine.Key;
|
|
|
|
|
|
_buckets[cloneOf!].Add(item.Key);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// All other that would be missing to subfolder of parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
else if (!parentItems.Contains(item))
|
2024-03-19 21:07:47 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (subfolder)
|
2024-12-06 23:16:09 -05:00
|
|
|
|
item.Value.SetName($"{machineName}\\{item.Value.GetName()}");
|
2024-03-19 21:07:47 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
_itemToMachineMapping[item.Key] = cloneOfMachine.Key;
|
|
|
|
|
|
_buckets[cloneOf!].Add(item.Key);
|
2024-03-19 21:07:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Then, remove the old game so it's not picked up by the writer
|
|
|
|
|
|
#if NET40_OR_GREATER || NETCOREAPP
|
|
|
|
|
|
_buckets.TryRemove(game, out _);
|
|
|
|
|
|
#else
|
|
|
|
|
|
_buckets.Remove(game);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-19 20:41:39 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove all BIOS and device sets
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RemoveBiosAndDeviceSets()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the machine
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Remove flagged items
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if ((machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true)
|
|
|
|
|
|
|| (machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsDeviceKey) == true))
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var key in items.Keys)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
RemoveItem(key);
|
2024-03-19 20:41:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use romof tags to remove bios roms from children
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bios">True if only child Bios sets are touched, false for non-bios sets</param>
|
|
|
|
|
|
public void RemoveBiosRomsFromChild(bool bios)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Loop through the romof tags
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the machine for the item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the game (is/is not) a bios, we want to continue
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (bios ^ (machine.Value.GetBoolFieldValue(Models.Metadata.Machine.IsBiosKey) == true))
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the bios parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? romOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
|
2024-10-19 22:39:23 -04:00
|
|
|
|
if (string.IsNullOrEmpty(romOf))
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent doesn't have any items, we want to continue
|
2024-10-19 22:50:42 -04:00
|
|
|
|
var parentItems = GetItemsForBucket(romOf!);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (parentItems == null || parentItems.Count == 0)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent exists and has items, we remove the items that are in the parent from the current game
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in parentItems)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var matchedItems = items.Where(i => i.Value == item.Value);
|
|
|
|
|
|
foreach (var match in matchedItems)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
RemoveItem(match.Key);
|
2024-03-19 20:41:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Use cloneof tags to remove roms from the children
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RemoveRomsFromChild()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-10-19 22:39:23 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the machine for the first item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(items.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// Get the clone parent
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? cloneOf = machine.Value.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
|
2024-10-19 22:39:23 -04:00
|
|
|
|
if (string.IsNullOrEmpty(cloneOf))
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent doesn't have any items, we want to continue
|
2024-10-19 22:50:42 -04:00
|
|
|
|
var parentItems = GetItemsForBucket(cloneOf!);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (parentItems == null || parentItems.Count == 0)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// If the parent exists and has items, we remove the parent items from the current game
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var item in parentItems)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var matchedItems = items.Where(i => i.Value == item.Value);
|
|
|
|
|
|
foreach (var match in matchedItems)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
RemoveItem(match.Key);
|
2024-03-19 20:41:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now we want to get the parent romof tag and put it in each of the remaining items
|
2024-03-19 23:35:29 -04:00
|
|
|
|
items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
machine = GetMachineForItem(GetItemsForBucket(cloneOf!)!.First().Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
string? romof = machine.Value.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
|
|
|
|
|
|
foreach (var item in items!)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
machine = GetMachineForItem(item.Key);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, romof);
|
2024-03-19 20:41:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove all romof and cloneof tags from all games
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RemoveTagsFromChild()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> games = [.. SortedKeys];
|
|
|
|
|
|
foreach (string game in games)
|
|
|
|
|
|
{
|
2024-10-19 22:39:23 -04:00
|
|
|
|
// If the game has no items in it, we want to continue
|
2024-03-19 23:35:29 -04:00
|
|
|
|
var items = GetItemsForBucket(game);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (items == null || items.Count == 0)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (long id in items.Keys)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
{
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = GetMachineForItem(id);
|
|
|
|
|
|
if (machine.Value == null)
|
2024-03-19 20:41:39 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-12-06 23:16:09 -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);
|
2024-03-19 20:41:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-03-13 11:05:34 -04:00
|
|
|
|
#region Statistics
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Recalculate the statistics for the Dat
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RecalculateStats()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Wipe out any stats already there
|
|
|
|
|
|
DatStatistics.ResetStatistics();
|
|
|
|
|
|
|
|
|
|
|
|
// If there are no items
|
2024-03-19 14:35:43 -04:00
|
|
|
|
if (_items == null || _items.Count == 0)
|
2024-03-13 11:05:34 -04:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Loop through and add
|
|
|
|
|
|
foreach (var item in _items.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
DatStatistics.AddItemStatistics(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-11-03 15:54:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|