2016-09-19 20:52:55 -07:00
|
|
|
|
using System;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
2020-09-07 14:47:27 -07:00
|
|
|
|
using System.Xml.Serialization;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-12-08 13:23:59 -08:00
|
|
|
|
using SabreTools.Core;
|
2020-12-08 16:37:08 -08:00
|
|
|
|
using SabreTools.Core.Tools;
|
2021-02-02 10:23:43 -08:00
|
|
|
|
using SabreTools.DatItems.Formats;
|
2020-12-08 14:53:49 -08:00
|
|
|
|
using SabreTools.FileTypes;
|
2020-12-07 14:29:45 -08:00
|
|
|
|
using SabreTools.Logging;
|
2016-10-24 12:58:57 -07:00
|
|
|
|
using NaturalSort;
|
2020-06-15 21:00:09 -07:00
|
|
|
|
using Newtonsoft.Json;
|
2020-08-23 22:23:55 -07:00
|
|
|
|
using Newtonsoft.Json.Converters;
|
2016-09-19 20:52:55 -07:00
|
|
|
|
|
2020-12-08 15:15:41 -08:00
|
|
|
|
namespace SabreTools.DatItems
|
2016-09-19 20:52:55 -07:00
|
|
|
|
{
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Base class for all items included in a set
|
|
|
|
|
|
/// </summary>
|
2022-11-03 12:23:10 -07:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// TODO: Can this be made into a `record` type for easier comparison?
|
|
|
|
|
|
/// </remarks>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonObject("datitem"), XmlRoot("datitem")]
|
2020-09-07 14:47:27 -07:00
|
|
|
|
[XmlInclude(typeof(Adjuster))]
|
|
|
|
|
|
[XmlInclude(typeof(Analog))]
|
|
|
|
|
|
[XmlInclude(typeof(Archive))]
|
|
|
|
|
|
[XmlInclude(typeof(BiosSet))]
|
|
|
|
|
|
[XmlInclude(typeof(Blank))]
|
|
|
|
|
|
[XmlInclude(typeof(Chip))]
|
|
|
|
|
|
[XmlInclude(typeof(Condition))]
|
|
|
|
|
|
[XmlInclude(typeof(Configuration))]
|
|
|
|
|
|
[XmlInclude(typeof(Control))]
|
|
|
|
|
|
[XmlInclude(typeof(DataArea))]
|
|
|
|
|
|
[XmlInclude(typeof(Device))]
|
|
|
|
|
|
[XmlInclude(typeof(DeviceReference))]
|
|
|
|
|
|
[XmlInclude(typeof(DipSwitch))]
|
|
|
|
|
|
[XmlInclude(typeof(Disk))]
|
|
|
|
|
|
[XmlInclude(typeof(DiskArea))]
|
|
|
|
|
|
[XmlInclude(typeof(Display))]
|
|
|
|
|
|
[XmlInclude(typeof(Driver))]
|
|
|
|
|
|
[XmlInclude(typeof(Extension))]
|
|
|
|
|
|
[XmlInclude(typeof(Feature))]
|
|
|
|
|
|
[XmlInclude(typeof(Info))]
|
|
|
|
|
|
[XmlInclude(typeof(Input))]
|
|
|
|
|
|
[XmlInclude(typeof(Instance))]
|
|
|
|
|
|
[XmlInclude(typeof(Location))]
|
|
|
|
|
|
[XmlInclude(typeof(Media))]
|
|
|
|
|
|
[XmlInclude(typeof(Part))]
|
|
|
|
|
|
[XmlInclude(typeof(PartFeature))]
|
|
|
|
|
|
[XmlInclude(typeof(Port))]
|
|
|
|
|
|
[XmlInclude(typeof(RamOption))]
|
|
|
|
|
|
[XmlInclude(typeof(Release))]
|
|
|
|
|
|
[XmlInclude(typeof(Rom))]
|
|
|
|
|
|
[XmlInclude(typeof(Sample))]
|
|
|
|
|
|
[XmlInclude(typeof(Setting))]
|
|
|
|
|
|
[XmlInclude(typeof(SharedFeature))]
|
|
|
|
|
|
[XmlInclude(typeof(Slot))]
|
|
|
|
|
|
[XmlInclude(typeof(SlotOption))]
|
|
|
|
|
|
[XmlInclude(typeof(SoftwareList))]
|
|
|
|
|
|
[XmlInclude(typeof(Sound))]
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public abstract class DatItem : IEquatable<DatItem>, IComparable<DatItem>, ICloneable
|
|
|
|
|
|
{
|
2020-08-20 13:17:14 -07:00
|
|
|
|
#region Fields
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-08-20 21:15:37 -07:00
|
|
|
|
#region Common Fields
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Item type for outputting
|
|
|
|
|
|
/// </summary>
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[JsonProperty("itemtype")]
|
2020-08-23 22:23:55 -07:00
|
|
|
|
[JsonConverter(typeof(StringEnumConverter))]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("itemtype")]
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public ItemType ItemType { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Duplicate type when compared to another item
|
|
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public DupeType DupeType { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-08-20 21:15:37 -07:00
|
|
|
|
#region Machine Fields
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-20 13:17:14 -07:00
|
|
|
|
/// Machine values
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2020-08-20 13:17:14 -07:00
|
|
|
|
public Machine Machine { get; set; } = new Machine();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-08-20 13:17:14 -07:00
|
|
|
|
#region Metadata information
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-20 13:17:14 -07:00
|
|
|
|
/// Source information
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2020-08-20 13:17:14 -07:00
|
|
|
|
public Source Source { get; set; } = new Source();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Flag if item should be removed
|
|
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public bool Remove { get; set; }
|
|
|
|
|
|
|
2020-12-13 13:22:06 -08:00
|
|
|
|
#endregion // Metadata information
|
2020-08-19 13:12:42 -07:00
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Logging
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Logging object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2020-10-07 16:37:10 -07:00
|
|
|
|
protected Logger logger;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Static logger for static methods
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
|
|
|
|
|
protected static Logger staticLogger = new Logger();
|
2020-10-07 15:42:30 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Instance Methods
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
#region Accessors
|
|
|
|
|
|
|
2020-09-02 12:19:12 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the name to use for a DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>Name if available, null otherwise</returns>
|
2020-12-14 10:15:28 -08:00
|
|
|
|
public virtual string GetName() => null;
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2020-12-13 14:01:16 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the name to use for a DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name to set for the item</param>
|
2020-12-14 10:15:28 -08:00
|
|
|
|
public virtual void SetName(string name) { }
|
2020-12-13 14:01:16 -08:00
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
2020-10-07 16:37:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public DatItem()
|
|
|
|
|
|
{
|
|
|
|
|
|
logger = new Logger(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a specific type of DatItem to be used based on an ItemType
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemType">Type of the DatItem to be created</param>
|
|
|
|
|
|
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
|
2020-08-25 11:20:50 -07:00
|
|
|
|
public static DatItem Create(ItemType? itemType)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
return itemType switch
|
|
|
|
|
|
{
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.Adjuster => new Adjuster(),
|
2020-09-02 16:31:23 -07:00
|
|
|
|
ItemType.Analog => new Analog(),
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ItemType.Archive => new Archive(),
|
|
|
|
|
|
ItemType.BiosSet => new BiosSet(),
|
|
|
|
|
|
ItemType.Blank => new Blank(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.Chip => new Chip(),
|
2020-09-02 16:31:23 -07:00
|
|
|
|
ItemType.Condition => new Condition(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.Configuration => new Configuration(),
|
2020-09-02 17:09:19 -07:00
|
|
|
|
ItemType.Device => new Device(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.DeviceReference => new DeviceReference(),
|
|
|
|
|
|
ItemType.DipSwitch => new DipSwitch(),
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ItemType.Disk => new Disk(),
|
2020-09-02 21:36:14 -07:00
|
|
|
|
ItemType.Display => new Display(),
|
2020-09-02 15:38:10 -07:00
|
|
|
|
ItemType.Driver => new Driver(),
|
2020-09-02 16:37:01 -07:00
|
|
|
|
ItemType.Extension => new Extension(),
|
2020-09-02 13:31:50 -07:00
|
|
|
|
ItemType.Feature => new Feature(),
|
2023-04-07 16:13:15 -04:00
|
|
|
|
ItemType.File => new Formats.File(),
|
2020-09-02 23:31:35 -07:00
|
|
|
|
ItemType.Info => new Info(),
|
2020-09-02 16:46:17 -07:00
|
|
|
|
ItemType.Instance => new Instance(),
|
2020-09-02 22:38:00 -07:00
|
|
|
|
ItemType.Location => new Location(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.Media => new Media(),
|
2020-09-03 13:20:56 -07:00
|
|
|
|
ItemType.PartFeature => new PartFeature(),
|
2020-09-02 17:22:31 -07:00
|
|
|
|
ItemType.Port => new Port(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.RamOption => new RamOption(),
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ItemType.Release => new Release(),
|
2023-04-19 12:26:54 -04:00
|
|
|
|
ItemType.ReleaseDetails => new ReleaseDetails(),
|
2020-07-15 09:41:59 -07:00
|
|
|
|
ItemType.Rom => new Rom(),
|
|
|
|
|
|
ItemType.Sample => new Sample(),
|
2023-04-07 16:13:15 -04:00
|
|
|
|
ItemType.Serials => new Serials(),
|
2020-09-03 00:48:07 -07:00
|
|
|
|
ItemType.SharedFeature => new SharedFeature(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.Slot => new Slot(),
|
2020-09-02 22:44:54 -07:00
|
|
|
|
ItemType.SlotOption => new SlotOption(),
|
2020-09-02 12:19:12 -07:00
|
|
|
|
ItemType.SoftwareList => new SoftwareList(),
|
2020-09-02 12:51:21 -07:00
|
|
|
|
ItemType.Sound => new Sound(),
|
2023-04-19 12:26:54 -04:00
|
|
|
|
ItemType.SourceDetails => new SourceDetails(),
|
2020-07-15 09:41:59 -07:00
|
|
|
|
_ => new Rom(),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a specific type of DatItem to be used based on a BaseFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="baseFile">BaseFile containing information to be created</param>
|
|
|
|
|
|
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
|
|
|
|
|
|
public static DatItem Create(BaseFile baseFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (baseFile.Type)
|
|
|
|
|
|
{
|
2020-08-27 16:57:22 -07:00
|
|
|
|
case FileType.AaruFormat:
|
|
|
|
|
|
return new Media(baseFile);
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
case FileType.CHD:
|
|
|
|
|
|
return new Disk(baseFile);
|
|
|
|
|
|
|
|
|
|
|
|
case FileType.GZipArchive:
|
|
|
|
|
|
case FileType.LRZipArchive:
|
|
|
|
|
|
case FileType.LZ4Archive:
|
|
|
|
|
|
case FileType.None:
|
|
|
|
|
|
case FileType.RarArchive:
|
|
|
|
|
|
case FileType.SevenZipArchive:
|
|
|
|
|
|
case FileType.TapeArchive:
|
|
|
|
|
|
case FileType.XZArchive:
|
|
|
|
|
|
case FileType.ZipArchive:
|
|
|
|
|
|
case FileType.ZPAQArchive:
|
|
|
|
|
|
case FileType.ZstdArchive:
|
|
|
|
|
|
return new Rom(baseFile);
|
|
|
|
|
|
|
|
|
|
|
|
case FileType.Folder:
|
|
|
|
|
|
default:
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#region Cloning Methods
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clone the DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>Clone of the DatItem</returns>
|
|
|
|
|
|
public abstract object Clone();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copy all machine information over in one shot
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item">Existing item to copy information from</param>
|
|
|
|
|
|
public void CopyMachineInformation(DatItem item)
|
|
|
|
|
|
{
|
2020-08-20 13:17:14 -07:00
|
|
|
|
Machine = (Machine)item.Machine.Clone();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copy all machine information over in one shot
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="machine">Existing machine to copy information from</param>
|
|
|
|
|
|
public void CopyMachineInformation(Machine machine)
|
|
|
|
|
|
{
|
2020-08-20 13:17:14 -07:00
|
|
|
|
Machine = (Machine)machine.Clone();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Comparision Methods
|
|
|
|
|
|
|
2022-11-03 12:23:10 -07:00
|
|
|
|
/// <inheritdoc/>
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public int CompareTo(DatItem other)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-09-02 15:07:25 -07:00
|
|
|
|
if (GetName() == other.GetName())
|
|
|
|
|
|
return Equals(other) ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
|
|
return string.Compare(GetName(), other.GetName());
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return 1;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determine if an item is a duplicate using partial matching logic
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="other">DatItem to use as a baseline</param>
|
2022-11-03 12:23:10 -07:00
|
|
|
|
/// <returns>True if the items are duplicates, false otherwise</returns>
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public abstract bool Equals(DatItem other);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Return the duplicate status of two items
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="lastItem">DatItem to check against</param>
|
|
|
|
|
|
/// <returns>The DupeType corresponding to the relationship between the two</returns>
|
|
|
|
|
|
public DupeType GetDuplicateStatus(DatItem lastItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
DupeType output = 0x00;
|
|
|
|
|
|
|
|
|
|
|
|
// If we don't have a duplicate at all, return none
|
2020-08-17 17:28:32 -07:00
|
|
|
|
if (!Equals(lastItem))
|
2019-01-08 12:11:55 -08:00
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
|
|
// If the duplicate is external already or should be, set it
|
2020-08-20 13:17:14 -07:00
|
|
|
|
if (lastItem.DupeType.HasFlag(DupeType.External) || lastItem.Source.Index != Source.Index)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-09-02 15:07:25 -07:00
|
|
|
|
if (lastItem.Machine.Name == Machine.Name && lastItem.GetName() == GetName())
|
2019-01-08 12:11:55 -08:00
|
|
|
|
output = DupeType.External | DupeType.All;
|
|
|
|
|
|
else
|
|
|
|
|
|
output = DupeType.External | DupeType.Hash;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, it's considered an internal dupe
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-09-02 15:07:25 -07:00
|
|
|
|
if (lastItem.Machine.Name == Machine.Name && lastItem.GetName() == GetName())
|
2019-01-08 12:11:55 -08:00
|
|
|
|
output = DupeType.Internal | DupeType.All;
|
|
|
|
|
|
else
|
|
|
|
|
|
output = DupeType.Internal | DupeType.Hash;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-08-17 23:09:35 -07:00
|
|
|
|
#region Filtering
|
|
|
|
|
|
|
2020-12-09 22:27:41 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a CRC32 string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
protected string CleanCRC32(string hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CleanHashData(hash, Constants.CRCLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a MD5 string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
protected string CleanMD5(string hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CleanHashData(hash, Constants.MD5Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a SHA1 string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
protected string CleanSHA1(string hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CleanHashData(hash, Constants.SHA1Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a SHA256 string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
protected string CleanSHA256(string hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CleanHashData(hash, Constants.SHA256Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a SHA384 string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
protected string CleanSHA384(string hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CleanHashData(hash, Constants.SHA384Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a SHA512 string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
protected string CleanSHA512(string hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CleanHashData(hash, Constants.SHA512Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clean a hash string and pad to the correct size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hash">Hash string to sanitize</param>
|
|
|
|
|
|
/// <param name="padding">Amount of characters to pad to</param>
|
|
|
|
|
|
/// <returns>Cleaned string</returns>
|
|
|
|
|
|
private string CleanHashData(string hash, int padding)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we have a known blank hash, return blank
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(hash) || hash == "-" || hash == "_")
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
// Check to see if it's a "hex" hash
|
|
|
|
|
|
hash = hash.Trim().Replace("0x", string.Empty);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a blank hash now, return blank
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(hash))
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
// If the hash shorter than the required length, pad it
|
|
|
|
|
|
if (hash.Length < padding)
|
|
|
|
|
|
hash = hash.PadLeft(padding, '0');
|
|
|
|
|
|
|
|
|
|
|
|
// If the hash is longer than the required length, it's invalid
|
|
|
|
|
|
else if (hash.Length > padding)
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
// Now normalize the hash
|
|
|
|
|
|
hash = hash.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, make sure that every character is a proper match
|
|
|
|
|
|
for (int i = 0; i < hash.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((hash[i] < '0' || hash[i] > '9') && (hash[i] < 'a' || hash[i] > 'f'))
|
|
|
|
|
|
{
|
|
|
|
|
|
hash = string.Empty;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-17 23:09:35 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#region Sorting and Merging
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 10:47:13 -07:00
|
|
|
|
/// Get the dictionary key that should be used for a given item and bucketing type
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2020-12-14 15:31:28 -08:00
|
|
|
|
/// <param name="bucketedBy">ItemKey value representing what key to get</param>
|
2020-07-15 09:41:59 -07: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>String representing the key to be used for the DatItem</returns>
|
2020-12-14 15:31:28 -08:00
|
|
|
|
public virtual string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Set the output key as the default blank string
|
|
|
|
|
|
string key = string.Empty;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 10:47:13 -07:00
|
|
|
|
// Now determine what the key should be based on the bucketedBy value
|
|
|
|
|
|
switch (bucketedBy)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.CRC:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.CRCZero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.Machine:
|
2020-07-15 09:41:59 -07:00
|
|
|
|
key = (norename ? string.Empty
|
2020-08-20 13:17:14 -07:00
|
|
|
|
: Source.Index.ToString().PadLeft(10, '0')
|
2020-07-15 09:41:59 -07:00
|
|
|
|
+ "-")
|
2020-08-20 13:17:14 -07:00
|
|
|
|
+ (string.IsNullOrWhiteSpace(Machine.Name)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
? "Default"
|
2020-08-20 13:17:14 -07:00
|
|
|
|
: Machine.Name);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (lower)
|
|
|
|
|
|
key = key.ToLowerInvariant();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
key = "null";
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.MD5:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.MD5Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA1:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA1Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA256:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA256Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA384:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA384Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA512:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA512Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2020-09-04 15:02:15 -07:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SpamSum:
|
2020-09-17 23:37:42 -07:00
|
|
|
|
key = Constants.SpamSumZero;
|
2020-09-04 15:02:15 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Double and triple check the key for corner cases
|
|
|
|
|
|
if (key == null)
|
|
|
|
|
|
key = string.Empty;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return key;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#endregion // Instance Methods
|
|
|
|
|
|
|
|
|
|
|
|
#region Static Methods
|
|
|
|
|
|
|
|
|
|
|
|
#region Sorting and Merging
|
|
|
|
|
|
|
2020-08-17 14:57:54 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determine if two hashes are equal for the purposes of merging
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="firstHash">First hash to compare</param>
|
|
|
|
|
|
/// <param name="secondHash">Second hash to compare</param>
|
|
|
|
|
|
/// <returns>True if either is empty OR the hashes exactly match, false otherwise</returns>
|
|
|
|
|
|
public static bool ConditionalHashEquals(byte[] firstHash, byte[] secondHash)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If either hash is empty, we say they're equal for merging
|
|
|
|
|
|
if (firstHash.IsNullOrEmpty() || secondHash.IsNullOrEmpty())
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
// If they're different sizes, they can't match
|
|
|
|
|
|
if (firstHash.Length != secondHash.Length)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, they need to match exactly
|
|
|
|
|
|
return Enumerable.SequenceEqual(firstHash, secondHash);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Merge an arbitrary set of ROMs based on the supplied information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
|
|
|
|
|
/// <returns>A List of DatItem objects representing the merged roms</returns>
|
2021-07-18 21:00:01 -07:00
|
|
|
|
public static ConcurrentList<DatItem> Merge(ConcurrentList<DatItem> infiles)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Check for null or blank roms first
|
|
|
|
|
|
if (infiles == null || infiles.Count == 0)
|
2021-07-18 21:00:01 -07:00
|
|
|
|
return new ConcurrentList<DatItem>();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// Create output list
|
2021-07-18 21:00:01 -07:00
|
|
|
|
ConcurrentList<DatItem> outfiles = new ConcurrentList<DatItem>();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// Then deduplicate them by checking to see if data matches previous saved roms
|
|
|
|
|
|
int nodumpCount = 0;
|
2020-07-19 13:42:07 -07:00
|
|
|
|
for (int f = 0; f < infiles.Count; f++)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-07-19 13:42:07 -07:00
|
|
|
|
DatItem file = infiles[f];
|
|
|
|
|
|
|
2020-10-12 11:11:40 -07:00
|
|
|
|
// If we somehow have a null item, skip
|
|
|
|
|
|
if (file == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2023-04-07 16:13:15 -04:00
|
|
|
|
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
|
|
|
|
|
|
if (file.ItemType != ItemType.Disk && file.ItemType != ItemType.File
|
|
|
|
|
|
&& file.ItemType != ItemType.Media && file.ItemType != ItemType.Rom)
|
|
|
|
|
|
{
|
2019-01-08 12:11:55 -08:00
|
|
|
|
continue;
|
2023-04-07 16:13:15 -04:00
|
|
|
|
}
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// If it's a nodump, add and skip
|
2020-08-17 17:28:32 -07:00
|
|
|
|
if (file.ItemType == ItemType.Rom && (file as Rom).ItemStatus == ItemStatus.Nodump)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
outfiles.Add(file);
|
|
|
|
|
|
nodumpCount++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2020-08-17 17:28:32 -07:00
|
|
|
|
else if (file.ItemType == ItemType.Disk && (file as Disk).ItemStatus == ItemStatus.Nodump)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
outfiles.Add(file);
|
|
|
|
|
|
nodumpCount++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
// If it's the first non-nodump rom in the list, don't touch it
|
|
|
|
|
|
else if (outfiles.Count == 0 || outfiles.Count == nodumpCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
outfiles.Add(file);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the rom is a duplicate
|
|
|
|
|
|
DupeType dupetype = 0x00;
|
2020-08-17 17:28:32 -07:00
|
|
|
|
DatItem saveditem = new Blank();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
int pos = -1;
|
|
|
|
|
|
for (int i = 0; i < outfiles.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
DatItem lastrom = outfiles[i];
|
|
|
|
|
|
|
|
|
|
|
|
// Get the duplicate status
|
|
|
|
|
|
dupetype = file.GetDuplicateStatus(lastrom);
|
|
|
|
|
|
|
|
|
|
|
|
// If it's a duplicate, skip adding it to the output but add any missing information
|
|
|
|
|
|
if (dupetype != 0x00)
|
|
|
|
|
|
{
|
|
|
|
|
|
saveditem = lastrom;
|
|
|
|
|
|
pos = i;
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
// Disks, Media, and Roms have more information to fill
|
2020-08-17 17:28:32 -07:00
|
|
|
|
if (file.ItemType == ItemType.Disk)
|
|
|
|
|
|
(saveditem as Disk).FillMissingInformation(file as Disk);
|
2023-04-07 16:13:15 -04:00
|
|
|
|
else if (file.ItemType == ItemType.File)
|
|
|
|
|
|
(saveditem as Formats.File).FillMissingInformation(file as Formats.File);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
else if (file.ItemType == ItemType.Media)
|
|
|
|
|
|
(saveditem as Media).FillMissingInformation(file as Media);
|
2020-08-17 17:28:32 -07:00
|
|
|
|
else if (file.ItemType == ItemType.Rom)
|
|
|
|
|
|
(saveditem as Rom).FillMissingInformation(file as Rom);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
saveditem.DupeType = dupetype;
|
|
|
|
|
|
|
|
|
|
|
|
// If the current system has a lower ID than the previous, set the system accordingly
|
2020-08-20 13:17:14 -07:00
|
|
|
|
if (file.Source.Index < saveditem.Source.Index)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-08-20 13:17:14 -07:00
|
|
|
|
saveditem.Source = file.Source.Clone() as Source;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
saveditem.CopyMachineInformation(file);
|
2020-12-14 10:11:20 -08:00
|
|
|
|
saveditem.SetName(file.GetName());
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the current machine is a child of the new machine, use the new machine instead
|
2020-08-20 13:17:14 -07:00
|
|
|
|
if (saveditem.Machine.CloneOf == file.Machine.Name || saveditem.Machine.RomOf == file.Machine.Name)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
saveditem.CopyMachineInformation(file);
|
2020-12-14 10:11:20 -08:00
|
|
|
|
saveditem.SetName(file.GetName());
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If no duplicate is found, add it to the list
|
|
|
|
|
|
if (dupetype == 0x00)
|
|
|
|
|
|
{
|
|
|
|
|
|
outfiles.Add(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, if a new rom information is found, add that
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
outfiles.RemoveAt(pos);
|
|
|
|
|
|
outfiles.Insert(pos, saveditem);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Then return the result
|
|
|
|
|
|
return outfiles;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Resolve name duplicates in an arbitrary set of ROMs based on the supplied information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
|
|
|
|
|
/// <returns>A List of DatItem objects representing the renamed roms</returns>
|
2021-07-18 21:00:01 -07:00
|
|
|
|
public static ConcurrentList<DatItem> ResolveNames(ConcurrentList<DatItem> infiles)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Create the output list
|
2021-07-18 21:00:01 -07:00
|
|
|
|
ConcurrentList<DatItem> output = new ConcurrentList<DatItem>();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// First we want to make sure the list is in alphabetical order
|
|
|
|
|
|
Sort(ref infiles, true);
|
|
|
|
|
|
|
|
|
|
|
|
// Now we want to loop through and check names
|
|
|
|
|
|
DatItem lastItem = null;
|
|
|
|
|
|
string lastrenamed = null;
|
|
|
|
|
|
int lastid = 0;
|
|
|
|
|
|
for (int i = 0; i < infiles.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
DatItem datItem = infiles[i];
|
|
|
|
|
|
|
|
|
|
|
|
// If we have the first item, we automatically add it
|
|
|
|
|
|
if (lastItem == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
output.Add(datItem);
|
|
|
|
|
|
lastItem = datItem;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-02 12:19:12 -07:00
|
|
|
|
// Get the last item name, if applicable
|
|
|
|
|
|
string lastItemName = lastItem.GetName() ?? lastItem.ItemType.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
// Get the current item name, if applicable
|
|
|
|
|
|
string datItemName = datItem.GetName() ?? datItem.ItemType.ToString();
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
// If the current item exactly matches the last item, then we don't add it
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (datItem.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-10-07 16:37:10 -07:00
|
|
|
|
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
|
2019-01-08 12:11:55 -08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the current name matches the previous name, rename the current item
|
2020-09-02 12:19:12 -07:00
|
|
|
|
else if (datItemName == lastItemName)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-10-07 16:37:10 -07:00
|
|
|
|
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2023-04-07 16:13:15 -04:00
|
|
|
|
if (datItem.ItemType == ItemType.Disk || datItem.ItemType == ItemType.File
|
|
|
|
|
|
|| datItem.ItemType == ItemType.Media || datItem.ItemType == ItemType.Rom)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-09-02 12:19:12 -07:00
|
|
|
|
datItemName += GetDuplicateSuffix(datItem);
|
|
|
|
|
|
lastrenamed ??= datItemName;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a conflict with the last renamed item, do the right thing
|
2020-09-02 12:19:12 -07:00
|
|
|
|
if (datItemName == lastrenamed)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-09-02 12:19:12 -07:00
|
|
|
|
lastrenamed = datItemName;
|
|
|
|
|
|
datItemName += (lastid == 0 ? string.Empty : "_" + lastid);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
lastid++;
|
|
|
|
|
|
}
|
|
|
|
|
|
// If we have no conflict, then we want to reset the lastrenamed and id
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
lastrenamed = null;
|
|
|
|
|
|
lastid = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-02 12:19:12 -07:00
|
|
|
|
// Set the item name back to the datItem
|
2020-12-14 10:11:20 -08:00
|
|
|
|
datItem.SetName(datItemName);
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
output.Add(datItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, we say that we have a valid named file
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
output.Add(datItem);
|
|
|
|
|
|
lastItem = datItem;
|
|
|
|
|
|
lastrenamed = null;
|
|
|
|
|
|
lastid = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// One last sort to make sure this is ordered
|
|
|
|
|
|
Sort(ref output, true);
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get duplicate suffix based on the item type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static string GetDuplicateSuffix(DatItem datItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (datItem.ItemType == ItemType.Disk)
|
2020-08-17 17:28:32 -07:00
|
|
|
|
return (datItem as Disk).GetDuplicateSuffix();
|
2023-04-07 16:13:15 -04:00
|
|
|
|
else if (datItem.ItemType == ItemType.File)
|
|
|
|
|
|
return (datItem as Formats.File).GetDuplicateSuffix();
|
2020-08-27 16:57:22 -07:00
|
|
|
|
else if (datItem.ItemType == ItemType.Media)
|
|
|
|
|
|
return (datItem as Media).GetDuplicateSuffix();
|
2020-06-10 22:37:19 -07:00
|
|
|
|
else if (datItem.ItemType == ItemType.Rom)
|
2020-08-17 17:28:32 -07:00
|
|
|
|
return (datItem as Rom).GetDuplicateSuffix();
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
|
|
|
|
|
return "_1";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <summary>
|
2020-08-28 01:13:55 -07:00
|
|
|
|
/// Sort a list of File objects by SourceID, Game, and Name (in order)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="roms">List of File objects representing the roms to be sorted</param>
|
|
|
|
|
|
/// <param name="norename">True if files are not renamed, false otherwise</param>
|
|
|
|
|
|
/// <returns>True if it sorted correctly, false otherwise</returns>
|
2021-07-18 21:00:01 -07:00
|
|
|
|
public static bool Sort(ref ConcurrentList<DatItem> roms, bool norename)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
roms.Sort(delegate (DatItem x, DatItem y)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
NaturalComparer nc = new NaturalComparer();
|
2021-12-21 14:20:27 -08:00
|
|
|
|
|
|
|
|
|
|
// If machine names match, more refinement is needed
|
|
|
|
|
|
if (x.Machine.Name == y.Machine.Name)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// If item types match, more refinement is needed
|
|
|
|
|
|
if (x.ItemType == y.ItemType)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2021-12-21 14:20:27 -08:00
|
|
|
|
string xDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
|
|
|
|
|
|
string yDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
|
|
|
|
|
|
|
|
|
|
|
|
// If item directory names match, more refinement is needed
|
|
|
|
|
|
if (xDirectoryName == yDirectoryName)
|
2020-08-31 16:46:04 -07:00
|
|
|
|
{
|
2021-12-21 14:20:27 -08:00
|
|
|
|
string xName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
|
|
|
|
|
|
string yName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
|
|
|
|
|
|
|
|
|
|
|
|
// If item names match, then compare on machine or source, depending on the flag
|
|
|
|
|
|
if (xName == yName)
|
|
|
|
|
|
return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.Source.Index - y.Source.Index);
|
2020-08-31 15:33:05 -07:00
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on item names
|
|
|
|
|
|
return nc.Compare(xName, yName);
|
2020-08-31 16:46:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on directory name
|
|
|
|
|
|
return nc.Compare(xDirectoryName, yDirectoryName);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on item type
|
|
|
|
|
|
return x.ItemType - y.ItemType;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on machine name
|
|
|
|
|
|
return nc.Compare(x.Machine.Name, y.Machine.Name);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2020-12-08 13:23:59 -08:00
|
|
|
|
catch
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Absorb the error
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2019-09-20 10:30:16 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#endregion // Static Methods
|
|
|
|
|
|
}
|
2016-09-19 20:52:55 -07:00
|
|
|
|
}
|