2016-08-29 16:55:55 -07:00
|
|
|
|
using System;
|
2016-06-13 23:42:27 -07:00
|
|
|
|
using System.Collections.Generic;
|
2016-09-07 17:26:59 -07:00
|
|
|
|
using System.IO;
|
2016-06-22 21:19:25 -07:00
|
|
|
|
using System.Linq;
|
2016-06-13 23:42:27 -07:00
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Helper
|
|
|
|
|
|
{
|
|
|
|
|
|
public class RomTools
|
|
|
|
|
|
{
|
2016-08-29 17:04:16 -07:00
|
|
|
|
#region Rom-based sorting and merging
|
|
|
|
|
|
|
2016-06-13 23:42:27 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Merge an arbitrary set of ROMs based on the supplied information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="inroms">List of RomData objects representing the roms to be merged</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
|
|
|
|
|
/// <returns>A List of RomData objects representing the merged roms</returns>
|
2016-08-29 13:57:46 -07:00
|
|
|
|
public static List<Rom> Merge(List<Rom> inroms, Logger logger)
|
2016-06-13 23:42:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Check for null or blank roms first
|
|
|
|
|
|
if (inroms == null || inroms.Count == 0)
|
|
|
|
|
|
{
|
2016-08-29 13:57:46 -07:00
|
|
|
|
return new List<Rom>();
|
2016-06-13 23:42:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create output list
|
2016-08-29 13:57:46 -07:00
|
|
|
|
List<Rom> outroms = new List<Rom>();
|
2016-06-13 23:42:27 -07:00
|
|
|
|
|
2016-06-16 17:41:23 -07:00
|
|
|
|
// Then deduplicate them by checking to see if data matches previous saved roms
|
2016-08-29 13:57:46 -07:00
|
|
|
|
foreach (Rom rom in inroms)
|
2016-06-13 23:42:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If it's a nodump, add and skip
|
|
|
|
|
|
if (rom.Nodump)
|
|
|
|
|
|
{
|
|
|
|
|
|
outroms.Add(rom);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If it's the first rom in the list, don't touch it
|
|
|
|
|
|
if (outroms.Count != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check if the rom is a duplicate
|
2016-06-16 10:34:37 -07:00
|
|
|
|
DupeType dupetype = DupeType.None;
|
2016-08-29 13:57:46 -07:00
|
|
|
|
Rom savedrom = new Rom();
|
2016-06-13 23:42:27 -07:00
|
|
|
|
int pos = -1;
|
|
|
|
|
|
for (int i = 0; i < outroms.Count; i++)
|
|
|
|
|
|
{
|
2016-08-29 13:57:46 -07:00
|
|
|
|
Rom lastrom = outroms[i];
|
2016-06-13 23:42:27 -07:00
|
|
|
|
|
|
|
|
|
|
// Get the duplicate status
|
2016-07-12 10:42:29 -07:00
|
|
|
|
dupetype = GetDuplicateStatus(rom, lastrom, logger);
|
2016-06-13 23:42:27 -07:00
|
|
|
|
|
|
|
|
|
|
// If it's a duplicate, skip adding it to the output but add any missing information
|
2016-06-16 10:34:37 -07:00
|
|
|
|
if (dupetype != DupeType.None)
|
2016-06-13 23:42:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
savedrom = lastrom;
|
|
|
|
|
|
pos = i;
|
|
|
|
|
|
|
2016-08-29 13:05:32 -07:00
|
|
|
|
savedrom.HashData.CRC = (String.IsNullOrEmpty(savedrom.HashData.CRC) && !String.IsNullOrEmpty(rom.HashData.CRC) ? rom.HashData.CRC : savedrom.HashData.CRC);
|
|
|
|
|
|
savedrom.HashData.MD5 = (String.IsNullOrEmpty(savedrom.HashData.MD5) && !String.IsNullOrEmpty(rom.HashData.MD5) ? rom.HashData.MD5 : savedrom.HashData.MD5);
|
|
|
|
|
|
savedrom.HashData.SHA1 = (String.IsNullOrEmpty(savedrom.HashData.SHA1) && !String.IsNullOrEmpty(rom.HashData.SHA1) ? rom.HashData.SHA1 : savedrom.HashData.SHA1);
|
2016-06-16 10:34:37 -07:00
|
|
|
|
savedrom.Dupe = dupetype;
|
2016-06-13 23:42:27 -07:00
|
|
|
|
|
|
|
|
|
|
// If the current system has a lower ID than the previous, set the system accordingly
|
2016-06-16 17:27:32 -07:00
|
|
|
|
if (rom.Metadata.SystemID < savedrom.Metadata.SystemID)
|
2016-06-13 23:42:27 -07:00
|
|
|
|
{
|
2016-06-16 17:27:32 -07:00
|
|
|
|
savedrom.Metadata.SystemID = rom.Metadata.SystemID;
|
|
|
|
|
|
savedrom.Metadata.System = rom.Metadata.System;
|
2016-08-29 13:42:27 -07:00
|
|
|
|
savedrom.Machine.Name = rom.Machine.Name;
|
2016-06-13 23:42:27 -07:00
|
|
|
|
savedrom.Name = rom.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the current source has a lower ID than the previous, set the source accordingly
|
2016-06-16 17:27:32 -07:00
|
|
|
|
if (rom.Metadata.SourceID < savedrom.Metadata.SourceID)
|
2016-06-13 23:42:27 -07:00
|
|
|
|
{
|
2016-06-16 17:27:32 -07:00
|
|
|
|
savedrom.Metadata.SourceID = rom.Metadata.SourceID;
|
|
|
|
|
|
savedrom.Metadata.Source = rom.Metadata.Source;
|
2016-08-29 13:42:27 -07:00
|
|
|
|
savedrom.Machine.Name = rom.Machine.Name;
|
2016-06-13 23:42:27 -07:00
|
|
|
|
savedrom.Name = rom.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If no duplicate is found, add it to the list
|
2016-06-16 10:34:37 -07:00
|
|
|
|
if (dupetype == DupeType.None)
|
2016-06-13 23:42:27 -07:00
|
|
|
|
{
|
|
|
|
|
|
outroms.Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, if a new rom information is found, add that
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
outroms.RemoveAt(pos);
|
|
|
|
|
|
outroms.Insert(pos, savedrom);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
outroms.Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Then return the result
|
|
|
|
|
|
return outroms;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-29 17:04:16 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// List all duplicates found in a DAT based on a rom
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="lastrom">Rom to use as a base</param>
|
|
|
|
|
|
/// <param name="datdata">DAT to match against</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
|
|
|
|
|
/// <param name="remove">True to remove matched roms from the input, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>List of matched RomData objects</returns>
|
|
|
|
|
|
public static List<Rom> GetDuplicates(Rom lastrom, Dat datdata, Logger logger, bool remove = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Rom> output = new List<Rom>();
|
|
|
|
|
|
|
|
|
|
|
|
// Check for an empty rom list first
|
|
|
|
|
|
if (datdata.Files == null || datdata.Files.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Try to find duplicates
|
|
|
|
|
|
List<string> keys = datdata.Files.Keys.ToList();
|
|
|
|
|
|
foreach (string key in keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Rom> roms = datdata.Files[key];
|
|
|
|
|
|
List<Rom> left = new List<Rom>();
|
2016-08-29 20:45:20 -07:00
|
|
|
|
|
2016-08-29 17:04:16 -07:00
|
|
|
|
foreach (Rom rom in roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsDuplicate(rom, lastrom, logger))
|
|
|
|
|
|
{
|
|
|
|
|
|
output.Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
left.Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we're in removal mode, replace the list with the new one
|
|
|
|
|
|
if (remove)
|
|
|
|
|
|
{
|
|
|
|
|
|
datdata.Files[key] = left;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determine if a file is a duplicate using partial matching logic
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rom">Rom to check for duplicate status</param>
|
|
|
|
|
|
/// <param name="lastrom">Rom to use as a baseline</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
|
|
|
|
|
/// <returns>True if the roms are duplicates, false otherwise</returns>
|
|
|
|
|
|
public static bool IsDuplicate(Rom rom, Rom lastrom, Logger logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool dupefound = rom.Equals(lastrom);
|
|
|
|
|
|
|
|
|
|
|
|
// More wonderful SHA-1 logging that has to be done
|
|
|
|
|
|
if (rom.HashData.SHA1 == lastrom.HashData.SHA1 && rom.HashData.Size != lastrom.HashData.Size)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.User("SHA-1 mismatch - Hash: " + rom.HashData.SHA1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return dupefound;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Return the duplicate status of two roms
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rom">Current rom to check</param>
|
|
|
|
|
|
/// <param name="lastrom">Last rom to check against</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
|
|
|
|
|
/// <returns>The DupeType corresponding to the relationship between the two</returns>
|
|
|
|
|
|
public static DupeType GetDuplicateStatus(Rom rom, Rom lastrom, Logger logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
DupeType output = DupeType.None;
|
|
|
|
|
|
|
|
|
|
|
|
// If we don't have a duplicate at all, return none
|
|
|
|
|
|
if (!IsDuplicate(rom, lastrom, logger))
|
|
|
|
|
|
{
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the duplicate is external already or should be, set it
|
|
|
|
|
|
if (lastrom.Dupe >= DupeType.ExternalHash || lastrom.Metadata.SystemID != rom.Metadata.SystemID || lastrom.Metadata.SourceID != rom.Metadata.SourceID)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (lastrom.Machine.Name == rom.Machine.Name && lastrom.Name == rom.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
output = DupeType.ExternalAll;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
output = DupeType.ExternalHash;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, it's considered an internal dupe
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (lastrom.Machine.Name == rom.Machine.Name && lastrom.Name == rom.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
output = DupeType.InternalAll;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
output = DupeType.InternalHash;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="roms">List of RomData 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>
|
|
|
|
|
|
public static bool Sort(List<Rom> roms, bool norename)
|
|
|
|
|
|
{
|
2016-09-11 17:40:46 -07:00
|
|
|
|
try
|
2016-08-29 17:04:16 -07:00
|
|
|
|
{
|
2016-09-11 17:40:46 -07:00
|
|
|
|
roms.Sort(delegate (Rom x, Rom y)
|
2016-08-29 17:04:16 -07:00
|
|
|
|
{
|
2016-09-11 17:40:46 -07:00
|
|
|
|
if (x.Metadata.SystemID == y.Metadata.SystemID)
|
2016-08-29 17:04:16 -07:00
|
|
|
|
{
|
2016-09-11 17:40:46 -07:00
|
|
|
|
if (x.Metadata.SourceID == y.Metadata.SourceID)
|
2016-08-29 17:04:16 -07:00
|
|
|
|
{
|
2016-09-11 17:40:46 -07:00
|
|
|
|
if (x.Machine.Name == y.Machine.Name)
|
2016-09-07 17:26:59 -07:00
|
|
|
|
{
|
2016-09-11 17:40:46 -07:00
|
|
|
|
if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Style.CompareNumeric(Path.GetFileName(x.Name), Path.GetFileName(y.Name));
|
|
|
|
|
|
}
|
|
|
|
|
|
return Style.CompareNumeric(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name));
|
2016-09-07 17:26:59 -07:00
|
|
|
|
}
|
2016-09-11 17:40:46 -07:00
|
|
|
|
return Style.CompareNumeric(x.Machine.Name, y.Machine.Name);
|
2016-08-29 17:04:16 -07:00
|
|
|
|
}
|
2016-09-11 17:40:46 -07:00
|
|
|
|
return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SourceID - y.Metadata.SourceID);
|
2016-08-29 17:04:16 -07:00
|
|
|
|
}
|
2016-09-11 17:40:46 -07:00
|
|
|
|
return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SystemID - y.Metadata.SystemID);
|
|
|
|
|
|
});
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-08-29 17:04:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2016-06-13 23:42:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|