2021-02-01 12:35:59 -08:00
|
|
|
using System.Collections.Generic;
|
2024-03-05 20:07:38 -05:00
|
|
|
using System.Linq;
|
2021-02-01 12:35:59 -08:00
|
|
|
using SabreTools.Core;
|
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;
|
2021-02-01 12:35:59 -08:00
|
|
|
|
2021-02-01 14:16:51 -08:00
|
|
|
namespace SabreTools.Filtering
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Replace fields in DatItems
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class Replacer
|
|
|
|
|
{
|
|
|
|
|
/// <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)
|
2023-04-20 16:30:00 -04:00
|
|
|
{
|
2024-03-05 20:07:38 -05:00
|
|
|
// Special case for description
|
|
|
|
|
if (machineFieldNames.Contains(Models.Metadata.Machine.DescriptionKey))
|
|
|
|
|
{
|
|
|
|
|
if (!onlySame || (onlySame && machine.Name == machine.Description))
|
|
|
|
|
machine.Description = repMachine.Description;
|
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
|
|
|
}
|
|
|
|
|
|
2023-08-14 13:17:51 -04: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)
|
2023-08-14 13:17:51 -04:00
|
|
|
{
|
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-05 20:07:38 -05:00
|
|
|
if (datItem.ItemType != repDatItem.ItemType)
|
|
|
|
|
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
|
|
|
|
|
string? itemType = datItem.ItemType.AsStringValue<ItemType>();
|
|
|
|
|
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
|
|
|
|
|
var fieldNames = new List<string>();
|
|
|
|
|
if (itemFieldNames.ContainsKey(itemType))
|
|
|
|
|
fieldNames.AddRange(itemFieldNames[itemType]);
|
|
|
|
|
if (itemFieldNames.ContainsKey("item"))
|
|
|
|
|
fieldNames.AddRange(itemFieldNames["item"]);
|
|
|
|
|
fieldNames = fieldNames.Distinct().ToList();
|
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
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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);
|
|
|
|
|
}
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
case (Disk disk, Disk repDisk): ReplaceFields(disk, repDisk, fieldNames); break;
|
|
|
|
|
case (Media media, Media repMedia): ReplaceFields(media, repMedia, fieldNames); break;
|
|
|
|
|
case (Rom rom, Rom repRom): ReplaceFields(rom, repRom, fieldNames); break;
|
|
|
|
|
}
|
2023-08-14 13:17:51 -04:00
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
#endregion
|
2023-08-14 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(disk.MD5) && !string.IsNullOrEmpty(newItem.MD5))
|
|
|
|
|
disk.MD5 = newItem.MD5;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Disk.SHA1Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(disk.SHA1) && !string.IsNullOrEmpty(newItem.SHA1))
|
|
|
|
|
disk.SHA1 = newItem.SHA1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(media.MD5) && !string.IsNullOrEmpty(newItem.MD5))
|
|
|
|
|
media.MD5 = newItem.MD5;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Media.SHA1Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(media.SHA1) && !string.IsNullOrEmpty(newItem.SHA1))
|
|
|
|
|
media.SHA1 = newItem.SHA1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Media.SHA256Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(media.SHA256) && !string.IsNullOrEmpty(newItem.SHA256))
|
|
|
|
|
media.SHA256 = newItem.SHA256;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Media.SpamSumKey))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(media.SpamSum) && !string.IsNullOrEmpty(newItem.SpamSum))
|
|
|
|
|
media.SpamSum = newItem.SpamSum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.CRC) && !string.IsNullOrEmpty(newItem.CRC))
|
|
|
|
|
rom.CRC = newItem.CRC;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Rom.MD5Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.MD5) && !string.IsNullOrEmpty(newItem.MD5))
|
|
|
|
|
rom.MD5 = newItem.MD5;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Rom.SHA1Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.SHA1) && !string.IsNullOrEmpty(newItem.SHA1))
|
|
|
|
|
rom.SHA1 = newItem.SHA1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Rom.SHA256Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.SHA256) && !string.IsNullOrEmpty(newItem.SHA256))
|
|
|
|
|
rom.SHA256 = newItem.SHA256;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Rom.SHA384Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.SHA384) && !string.IsNullOrEmpty(newItem.SHA384))
|
|
|
|
|
rom.SHA384 = newItem.SHA384;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Rom.SHA512Key))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.SHA512) && !string.IsNullOrEmpty(newItem.SHA512))
|
|
|
|
|
rom.SHA512 = newItem.SHA512;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:07:38 -05:00
|
|
|
if (datItemFields.Contains(Models.Metadata.Rom.SpamSumKey))
|
2021-02-01 12:35:59 -08:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(rom.SpamSum) && !string.IsNullOrEmpty(newItem.SpamSum))
|
|
|
|
|
rom.SpamSum = newItem.SpamSum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|