2020-12-10 14:47:38 -08:00
|
|
|
using System.Collections.Generic;
|
2025-01-07 15:40:05 -05:00
|
|
|
using SabreTools.Core.Tools;
|
2020-12-10 14:47:38 -08:00
|
|
|
using SabreTools.DatItems;
|
2025-01-07 15:40:05 -05:00
|
|
|
using SabreTools.DatItems.Formats;
|
2024-10-24 00:36:44 -04:00
|
|
|
using SabreTools.IO.Logging;
|
2020-12-10 10:39:39 -08:00
|
|
|
|
2025-01-07 15:31:28 -05:00
|
|
|
namespace SabreTools.DatFiles
|
2020-12-10 10:39:39 -08:00
|
|
|
{
|
2020-12-21 11:38:56 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Helper methods for updating and converting DatFiles
|
|
|
|
|
/// </summary>
|
2021-02-01 11:43:38 -08:00
|
|
|
public static class DatFileTool
|
2020-12-10 10:39:39 -08:00
|
|
|
{
|
|
|
|
|
#region Logging
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logging object
|
|
|
|
|
/// </summary>
|
2025-01-08 16:59:44 -05:00
|
|
|
private static readonly Logger _staticLogger = new();
|
2020-12-10 10:39:39 -08:00
|
|
|
|
|
|
|
|
#endregion
|
2020-12-10 14:47:38 -08:00
|
|
|
|
2025-01-07 15:40:05 -05:00
|
|
|
#region Sorting and Merging
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Merge an arbitrary set of DatItems based on the supplied information
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">List of DatItem objects representing the items to be merged</param>
|
|
|
|
|
/// <returns>A List of DatItem objects representing the merged items</returns>
|
|
|
|
|
public static List<DatItem> Merge(List<DatItem>? items)
|
|
|
|
|
{
|
|
|
|
|
// Check for null or blank inputs first
|
|
|
|
|
if (items == null || items.Count == 0)
|
|
|
|
|
return [];
|
|
|
|
|
|
|
|
|
|
// Create output list
|
|
|
|
|
List<DatItem> output = [];
|
|
|
|
|
|
|
|
|
|
// Then deduplicate them by checking to see if data matches previous saved roms
|
|
|
|
|
int nodumpCount = 0;
|
|
|
|
|
foreach (DatItem datItem in items)
|
|
|
|
|
{
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
output.Add(datItem);
|
|
|
|
|
nodumpCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (datItem is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
|
|
|
|
{
|
|
|
|
|
output.Add(datItem);
|
|
|
|
|
nodumpCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's the first non-nodump item in the list, don't touch it
|
|
|
|
|
if (output.Count == nodumpCount)
|
|
|
|
|
{
|
|
|
|
|
output.Add(datItem);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the index of the first duplicate, if one exists
|
|
|
|
|
int pos = output.FindIndex(lastItem => datItem.GetDuplicateStatus(lastItem) != 0x00);
|
|
|
|
|
if (pos < 0)
|
|
|
|
|
{
|
|
|
|
|
output.Add(datItem);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the duplicate item
|
|
|
|
|
DatItem savedItem = output[pos];
|
|
|
|
|
DupeType dupetype = datItem.GetDuplicateStatus(savedItem);
|
|
|
|
|
|
|
|
|
|
// Disks, File, 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);
|
|
|
|
|
|
|
|
|
|
// Set the duplicate type on the saved item
|
|
|
|
|
savedItem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
|
|
|
|
|
|
|
|
|
// Get the sources associated with the items
|
|
|
|
|
var savedSource = savedItem.GetFieldValue<Source?>(DatItem.SourceKey);
|
|
|
|
|
var itemSource = datItem.GetFieldValue<Source?>(DatItem.SourceKey);
|
|
|
|
|
|
|
|
|
|
// Get the machines associated with the items
|
|
|
|
|
var savedMachine = savedItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
|
|
|
|
var itemMachine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
|
|
|
|
|
|
|
|
|
// If the current source has a lower ID than the saved, use the saved source
|
|
|
|
|
if (itemSource?.Index < savedSource?.Index)
|
|
|
|
|
{
|
|
|
|
|
datItem.SetFieldValue<Source?>(DatItem.SourceKey, savedSource.Clone() as Source);
|
|
|
|
|
savedItem.CopyMachineInformation(datItem);
|
|
|
|
|
savedItem.SetName(datItem.GetName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the saved machine is a child of the current machine, use the current machine instead
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
savedItem.CopyMachineInformation(datItem);
|
|
|
|
|
savedItem.SetName(datItem.GetName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace the original item in the list
|
|
|
|
|
output.RemoveAt(pos);
|
|
|
|
|
output.Insert(pos, savedItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then return the result
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2020-12-10 10:39:39 -08:00
|
|
|
}
|
|
|
|
|
}
|