Files
SabreTools/SabreTools.DatFiles/Replacer.cs

472 lines
21 KiB
C#
Raw Normal View History

2021-02-01 12:35:59 -08:00
using System.Collections.Generic;
2025-02-12 14:22:15 -05:00
using System.Linq;
#if NET40_OR_GREATER || NETCOREAPP
using System.Threading.Tasks;
#endif
2024-03-05 20:07:38 -05:00
using SabreTools.Core.Tools;
2021-02-01 12:35:59 -08:00
using SabreTools.DatItems;
2021-02-02 10:23:43 -08:00
using SabreTools.DatItems.Formats;
2025-02-12 14:22:15 -05:00
using SabreTools.IO.Logging;
2021-02-01 12:35:59 -08:00
namespace SabreTools.DatFiles
2021-02-01 12:35:59 -08:00
{
/// <summary>
/// Replace fields in DatItems
/// </summary>
2025-02-12 14:22:30 -05:00
/// TODO: Add tests for BaseReplace methods
2021-02-01 12:35:59 -08:00
public static class Replacer
{
2025-02-12 14:22:15 -05:00
/// <summary>
/// Replace item values from the base set represented by the current DAT
/// </summary>
/// <param name="datFile">Current DatFile object to use for updating</param>
/// <param name="intDat">DatFile to replace the values in</param>
/// <param name="machineFieldNames">List of machine field names representing what should be updated</param>
/// <param name="itemFieldNames">List of item field names representing what should be updated</param>
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
public static void BaseReplace(
DatFile datFile,
DatFile intDat,
List<string> machineFieldNames,
Dictionary<string, List<string>> itemFieldNames,
bool onlySame)
{
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.GetStringFieldValue(DatHeader.FileNameKey)}' from the base DAT");
// If we are matching based on DatItem fields of any sort
if (itemFieldNames.Count > 0)
{
// For comparison's sake, we want to use CRC as the base bucketing
datFile.BucketBy(ItemKey.CRC);
datFile.Deduplicate();
intDat.BucketBy(ItemKey.CRC);
// Then we do a hashwise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(intDat.Items.SortedKeys, key =>
#else
foreach (var key in intDat.Items.SortedKeys)
#endif
{
List<DatItem>? datItems = intDat.GetItemsForBucket(key);
if (datItems == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
List<DatItem> newDatItems = [];
foreach (DatItem datItem in datItems)
{
List<DatItem> dupes = datFile.GetDuplicates(datItem, sorted: true);
if (datItem.Clone() is not DatItem newDatItem)
continue;
// Replace fields from the first duplicate, if we have one
if (dupes.Count > 0)
Replacer.ReplaceFields(newDatItem, dupes[0], itemFieldNames);
newDatItems.Add(newDatItem);
}
// Now add the new list to the key
intDat.RemoveBucket(key);
newDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
// If we are matching based on Machine fields of any sort
if (machineFieldNames.Count > 0)
{
// For comparison's sake, we want to use Machine Name as the base bucketing
datFile.BucketBy(ItemKey.Machine);
datFile.Deduplicate();
intDat.BucketBy(ItemKey.Machine);
// Then we do a namewise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(intDat.Items.SortedKeys, key =>
#else
foreach (var key in intDat.Items.SortedKeys)
#endif
{
List<DatItem>? datItems = intDat.GetItemsForBucket(key);
if (datItems == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
List<DatItem> newDatItems = [];
foreach (DatItem datItem in datItems)
{
if (datItem.Clone() is not DatItem newDatItem)
continue;
var list = datFile.GetItemsForBucket(key);
if (list.Count > 0)
Replacer.ReplaceFields(newDatItem.GetFieldValue<Machine>(DatItem.MachineKey)!, list[index: 0].GetFieldValue<Machine>(DatItem.MachineKey)!, machineFieldNames, onlySame);
newDatItems.Add(newDatItem);
}
// Now add the new list to the key
intDat.RemoveBucket(key);
newDatItems.ForEach(item => intDat.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
watch.Stop();
}
/// <summary>
/// Replace item values from the base set represented by the current DAT
/// </summary>
/// <param name="datFile">Current DatFile object to use for updating</param>
/// <param name="intDat">DatFile to replace the values in</param>
/// <param name="machineFieldNames">List of machine field names representing what should be updated</param>
/// <param name="itemFieldNames">List of item field names representing what should be updated</param>
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
public static void BaseReplaceDB(
DatFile datFile,
DatFile intDat,
List<string> machineFieldNames,
Dictionary<string, List<string>> itemFieldNames,
bool onlySame)
{
InternalStopwatch watch = new($"Replacing items in '{intDat.Header.GetStringFieldValue(DatHeader.FileNameKey)}' from the base DAT");
// If we are matching based on DatItem fields of any sort
if (itemFieldNames.Count > 0)
{
// For comparison's sake, we want to use CRC as the base bucketing
datFile.BucketBy(ItemKey.CRC);
datFile.Deduplicate();
intDat.BucketBy(ItemKey.CRC);
// Then we do a hashwise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.ItemsDB.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(intDat.ItemsDB.SortedKeys, key =>
#else
foreach (var key in intDat.ItemsDB.SortedKeys)
#endif
{
var datItems = intDat.GetItemsForBucketDB(key);
if (datItems == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
foreach (var datItem in datItems)
{
var dupes = datFile.GetDuplicatesDB(datItem, sorted: true);
if (datItem.Value.Clone() is not DatItem newDatItem)
continue;
// Replace fields from the first duplicate, if we have one
if (dupes.Count > 0)
Replacer.ReplaceFields(datItem.Value, dupes.First().Value, itemFieldNames);
}
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
// If we are matching based on Machine fields of any sort
if (machineFieldNames.Count > 0)
{
// For comparison's sake, we want to use Machine Name as the base bucketing
datFile.BucketBy(ItemKey.Machine);
datFile.Deduplicate();
intDat.BucketBy(ItemKey.Machine);
// Then we do a namewise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.ItemsDB.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(intDat.ItemsDB.SortedKeys, key =>
#else
foreach (var key in intDat.ItemsDB.SortedKeys)
#endif
{
var datItems = intDat.GetItemsForBucketDB(key);
if (datItems == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
foreach (var datItem in datItems)
{
var datMachine = datFile.ItemsDB.GetMachineForItem(datFile.GetItemsForBucketDB(key)!.First().Key);
var intMachine = intDat.ItemsDB.GetMachineForItem(datItem.Key);
if (datMachine.Value != null && intMachine.Value != null)
Replacer.ReplaceFields(intMachine.Value, datMachine.Value, machineFieldNames, onlySame);
}
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
watch.Stop();
}
2021-02-01 12:35:59 -08:00
/// <summary>
/// Replace fields with given values
/// </summary>
/// <param name="machine">Machine to replace fields in</param>
/// <param name="repMachine">Machine to pull new information from</param>
2024-03-05 20:07:38 -05:00
/// <param name="machineFieldNames">List of fields representing what should be updated</param>
2021-02-01 12:35:59 -08:00
/// <param name="onlySame">True if descriptions should only be replaced if the game name is the same, false otherwise</param>
2024-03-05 20:07:38 -05:00
public static void ReplaceFields(Machine machine, Machine repMachine, List<string> machineFieldNames, bool onlySame)
2021-02-01 12:35:59 -08:00
{
2024-03-05 20:07:38 -05:00
// If we have an invalid input, return
if (machine == null || repMachine == null || machineFieldNames == null)
return;
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
// Loop through and replace fields
foreach (string fieldName in machineFieldNames)
{
2024-03-05 20:07:38 -05:00
// Special case for description
if (machineFieldNames.Contains(Models.Metadata.Machine.DescriptionKey))
{
if (!onlySame || (onlySame && machine.GetStringFieldValue(Models.Metadata.Machine.NameKey) == machine.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey)))
machine.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, repMachine.GetStringFieldValue(Models.Metadata.Machine.DescriptionKey));
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
continue;
}
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
machine.ReplaceField(repMachine, fieldName);
}
2021-02-01 12:35:59 -08:00
}
/// <summary>
/// Replace fields with given values
/// </summary>
2024-03-05 20:07:38 -05:00
/// <param name="datItem">DatItem to replace fields in</param>
/// <param name="repDatItem">DatItem to pull new information from</param>
/// <param name="itemFieldNames">List of fields representing what should be updated</param>
public static void ReplaceFields(DatItem datItem, DatItem repDatItem, Dictionary<string, List<string>> itemFieldNames)
{
2024-03-05 20:07:38 -05:00
// If we have an invalid input, return
if (datItem == null || repDatItem == null || itemFieldNames == null)
return;
2024-02-28 19:19:50 -05:00
2024-03-05 20:07:38 -05:00
#region Common
2021-02-01 12:35:59 -08:00
2024-03-11 16:26:28 -04:00
if (datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey) != repDatItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey))
2024-03-05 20:07:38 -05:00
return;
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
// If there are no field names for this type or generic, return
2024-03-11 16:26:28 -04:00
string? itemType = datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue();
2024-03-05 20:07:38 -05:00
if (itemType == null || (!itemFieldNames.ContainsKey(itemType) && !itemFieldNames.ContainsKey("item")))
return;
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
// Get the combined list of fields to remove
2024-11-12 19:57:58 -05:00
var fieldNames = new HashSet<string>();
2024-03-05 20:07:38 -05:00
if (itemFieldNames.ContainsKey(itemType))
2024-12-06 12:46:58 -05:00
fieldNames.UnionWith(itemFieldNames[itemType]);
2024-03-05 20:07:38 -05:00
if (itemFieldNames.ContainsKey("item"))
2024-12-06 12:46:58 -05:00
fieldNames.UnionWith(itemFieldNames["item"]);
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
// If the field specifically contains Name, set it separately
if (fieldNames.Contains(Models.Metadata.Rom.NameKey))
datItem.SetName(repDatItem.GetName());
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
#endregion
2021-02-01 12:35:59 -08:00
2024-03-05 20:07:38 -05:00
#region Item-Specific
2024-03-06 00:33:45 -05:00
// Handle normal sets first
2024-03-05 20:07:38 -05:00
foreach (var fieldName in fieldNames)
{
datItem.ReplaceField(repDatItem, fieldName);
}
2024-03-06 00:33:45 -05:00
// TODO: Filter out hashes before here so these checks actually work
// Handle special cases
2024-03-05 20:07:38 -05:00
switch (datItem, repDatItem)
{
2024-11-12 19:57:58 -05:00
case (Disk disk, Disk repDisk): ReplaceFields(disk, repDisk, [.. fieldNames]); break;
2025-02-12 12:36:42 -05:00
case (DatItems.Formats.File file, DatItems.Formats.File repFile): ReplaceFields(file, repFile, [.. fieldNames]); break;
2024-11-12 19:57:58 -05:00
case (Media media, Media repMedia): ReplaceFields(media, repMedia, [.. fieldNames]); break;
case (Rom rom, Rom repRom): ReplaceFields(rom, repRom, [.. fieldNames]); break;
2024-03-05 20:07:38 -05:00
}
2024-03-05 20:07:38 -05:00
#endregion
}
2021-02-01 12:35:59 -08:00
/// <summary>
/// Replace fields with given values
/// </summary>
/// <param name="disk">Disk to remove replace fields in</param>
/// <param name="newItem">Disk to pull new information from</param>
/// <param name="datItemFields">List of fields representing what should be updated</param>
2024-03-05 20:07:38 -05:00
private static void ReplaceFields(Disk disk, Disk newItem, List<string> datItemFields)
2021-02-01 12:35:59 -08:00
{
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Disk.MD5Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Disk.MD5Key)))
disk.SetFieldValue<string?>(Models.Metadata.Disk.MD5Key, newItem.GetStringFieldValue(Models.Metadata.Disk.MD5Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Disk.SHA1Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Disk.SHA1Key)))
disk.SetFieldValue<string?>(Models.Metadata.Disk.SHA1Key, newItem.GetStringFieldValue(Models.Metadata.Disk.SHA1Key));
2021-02-01 12:35:59 -08:00
}
}
2025-02-12 12:36:42 -05:00
/// <summary>
/// Replace fields with given values
/// </summary>
/// <param name="file">File to remove replace fields in</param>
/// <param name="newItem">File to pull new information from</param>
/// <param name="datItemFields">List of fields representing what should be updated</param>
private static void ReplaceFields(DatItems.Formats.File file, DatItems.Formats.File newItem, List<string> datItemFields)
{
if (datItemFields.Contains(Models.Metadata.Rom.CRCKey))
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.CRC))
2025-02-12 12:36:42 -05:00
file.CRC = newItem.CRC;
}
if (datItemFields.Contains(Models.Metadata.Rom.MD5Key))
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.MD5))
2025-02-12 12:36:42 -05:00
file.MD5 = newItem.MD5;
}
if (datItemFields.Contains(Models.Metadata.Rom.SHA1Key))
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.SHA1))
2025-02-12 12:36:42 -05:00
file.SHA1 = newItem.SHA1;
}
if (datItemFields.Contains(Models.Metadata.Rom.SHA256Key))
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.SHA256))
2025-02-12 12:36:42 -05:00
file.SHA256 = newItem.SHA256;
}
}
2021-02-01 12:35:59 -08:00
/// <summary>
/// Replace fields with given values
/// </summary>
/// <param name="media">Media to remove replace fields in</param>
/// <param name="newItem">Media to pull new information from</param>
/// <param name="datItemFields">List of fields representing what should be updated</param>
2024-03-05 20:07:38 -05:00
private static void ReplaceFields(Media media, Media newItem, List<string> datItemFields)
2021-02-01 12:35:59 -08:00
{
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Media.MD5Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Media.MD5Key)))
media.SetFieldValue<string?>(Models.Metadata.Media.MD5Key, newItem.GetStringFieldValue(Models.Metadata.Media.MD5Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Media.SHA1Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Media.SHA1Key)))
media.SetFieldValue<string?>(Models.Metadata.Media.SHA1Key, newItem.GetStringFieldValue(Models.Metadata.Media.SHA1Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Media.SHA256Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Media.SHA256Key)))
media.SetFieldValue<string?>(Models.Metadata.Media.SHA256Key, newItem.GetStringFieldValue(Models.Metadata.Media.SHA256Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Media.SpamSumKey))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Media.SpamSumKey)))
media.SetFieldValue<string?>(Models.Metadata.Media.SpamSumKey, newItem.GetStringFieldValue(Models.Metadata.Media.SpamSumKey));
2021-02-01 12:35:59 -08:00
}
}
/// <summary>
/// Replace fields with given values
/// </summary>
/// <param name="rom">Rom to remove replace fields in</param>
/// <param name="newItem">Rom to pull new information from</param>
/// <param name="datItemFields">List of fields representing what should be updated</param>
2024-03-05 20:07:38 -05:00
private static void ReplaceFields(Rom rom, Rom newItem, List<string> datItemFields)
2021-02-01 12:35:59 -08:00
{
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.CRCKey))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.CRCKey)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, newItem.GetStringFieldValue(Models.Metadata.Rom.CRCKey));
2021-02-01 12:35:59 -08:00
}
2025-01-09 05:26:36 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.MD2Key))
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.MD2Key)))
2025-01-09 05:26:36 -05:00
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD2Key, newItem.GetStringFieldValue(Models.Metadata.Rom.MD2Key));
}
if (datItemFields.Contains(Models.Metadata.Rom.MD4Key))
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.MD4Key)))
2025-01-09 05:26:36 -05:00
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD4Key, newItem.GetStringFieldValue(Models.Metadata.Rom.MD4Key));
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.MD5Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.MD5Key)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD5Key, newItem.GetStringFieldValue(Models.Metadata.Rom.MD5Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.SHA1Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.SHA1Key)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA1Key, newItem.GetStringFieldValue(Models.Metadata.Rom.SHA1Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.SHA256Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.SHA256Key)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA256Key, newItem.GetStringFieldValue(Models.Metadata.Rom.SHA256Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.SHA384Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.SHA384Key)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA384Key, newItem.GetStringFieldValue(Models.Metadata.Rom.SHA384Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.SHA512Key))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.SHA512Key)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA512Key, newItem.GetStringFieldValue(Models.Metadata.Rom.SHA512Key));
2021-02-01 12:35:59 -08:00
}
2024-03-05 20:07:38 -05:00
if (datItemFields.Contains(Models.Metadata.Rom.SpamSumKey))
2021-02-01 12:35:59 -08:00
{
2025-02-12 13:30:37 -05:00
if (!string.IsNullOrEmpty(newItem.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey)))
rom.SetFieldValue<string?>(Models.Metadata.Rom.SpamSumKey, newItem.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey));
2021-02-01 12:35:59 -08:00
}
}
}
}