From 902070c542697bd85e23b475b5d7c6642ba80289 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 18:04:24 -0700 Subject: [PATCH 01/13] [ALL] Overhaul to internal system This massive change replaces the old "Rom" struct system with a new one that have different objects for each major item type. This required a lot of work and a lot of changes and has unfortunately been untested. But this is the first step in moving away from using structs. The next major step is converting Dat over to this as well. --- RombaSharp/Partials/RombaSharp_Inits.cs | 2 +- SabreTools.Helper/Data/Structs.cs | 114 +- SabreTools.Helper/Objects/DATFromDir.cs | 60 +- .../Objects/DatObjects/Archive.cs | 91 + .../Objects/DatObjects/BiosSet.cs | 121 ++ SabreTools.Helper/Objects/DatObjects/Disk.cs | 146 ++ .../Objects/DatObjects/Release.cs | 149 ++ SabreTools.Helper/Objects/DatObjects/Rom.cs | 175 ++ .../Objects/DatObjects/Sample.cs | 91 + SabreTools.Helper/Objects/Generate.cs | 61 +- SabreTools.Helper/Objects/GenerateTwo.cs | 26 +- SabreTools.Helper/Objects/Import.cs | 36 +- SabreTools.Helper/Objects/SimpleSort.cs | 66 +- SabreTools.Helper/Objects/Stats.cs | 6 +- SabreTools.Helper/SabreTools.Helper.csproj | 10 +- SabreTools.Helper/Skippers/Skippers.cs | 12 +- SabreTools.Helper/Tools/DatTools.cs | 1175 +++++++------ SabreTools.Helper/Tools/DatToolsHash.cs | 1534 ----------------- SabreTools.Helper/Tools/FileTools.cs | 23 +- SabreTools.Helper/Tools/RomTools.cs | 505 ------ SabreTools.Helper/Tools/RomToolsHash.cs | 225 --- SabreTools/Partials/SabreTools_Inits.cs | 4 +- 22 files changed, 1615 insertions(+), 3017 deletions(-) create mode 100644 SabreTools.Helper/Objects/DatObjects/Archive.cs create mode 100644 SabreTools.Helper/Objects/DatObjects/BiosSet.cs create mode 100644 SabreTools.Helper/Objects/DatObjects/Disk.cs create mode 100644 SabreTools.Helper/Objects/DatObjects/Release.cs create mode 100644 SabreTools.Helper/Objects/DatObjects/Rom.cs create mode 100644 SabreTools.Helper/Objects/DatObjects/Sample.cs delete mode 100644 SabreTools.Helper/Tools/DatToolsHash.cs delete mode 100644 SabreTools.Helper/Tools/RomTools.cs delete mode 100644 SabreTools.Helper/Tools/RomToolsHash.cs diff --git a/RombaSharp/Partials/RombaSharp_Inits.cs b/RombaSharp/Partials/RombaSharp_Inits.cs index 39a6f93c..5e929669 100644 --- a/RombaSharp/Partials/RombaSharp_Inits.cs +++ b/RombaSharp/Partials/RombaSharp_Inits.cs @@ -63,7 +63,7 @@ namespace SabreTools Name = Path.GetFileName(inputs[0]) + " Dir2Dat", Description = Path.GetFileName(inputs[0]) + " Dir2Dat", OutputFormat = OutputFormat.Xml, - Files = new Dictionary>(), + Files = new Dictionary>(), }; Logger logger = new Logger(false, ""); diff --git a/SabreTools.Helper/Data/Structs.cs b/SabreTools.Helper/Data/Structs.cs index 2f37f94c..931132cb 100644 --- a/SabreTools.Helper/Data/Structs.cs +++ b/SabreTools.Helper/Data/Structs.cs @@ -191,116 +191,6 @@ namespace SabreTools.Helper } } - /// - /// Intermediate struct for holding and processing rom data - /// - public struct Rom : IComparable, IEquatable - { - public Machine Machine; - public string Name; - public ItemType Type; - public Hash HashData; - public DupeType Dupe; - public bool Nodump; - public string Date; - public SourceMetadata Metadata; - - // Non rom or disk - public string Region; - public string Language; - public bool? Default; - public string Description; - - public int CompareTo(object obj) - { - int ret = 0; - - try - { - Rom comp = (Rom)obj; - - if (this.Machine.Name == comp.Machine.Name) - { - if (this.Name == comp.Name) - { - ret = (this.Equals(comp) ? 0 : 1); - } - ret = String.Compare(this.Name, comp.Name); - } - ret = String.Compare(this.Machine.Name, comp.Machine.Name); - } - catch - { - ret = 1; - } - - return ret; - } - - public bool Equals(Rom other) - { - bool dupefound = false; - - // If either is a nodump, it's never a match - if (this.Nodump || other.Nodump) - { - return dupefound; - } - - if (this.Type == ItemType.Rom && other.Type == ItemType.Rom) - { - dupefound = this.HashData.Equals(other.HashData, false); - } - else if (this.Type == ItemType.Disk && other.Type == ItemType.Disk) - { - dupefound = this.HashData.Equals(other.HashData, true); - } - - return dupefound; - } - } - - /// - /// Intermediate metadata kept with a RomData object representing source information - /// - public struct SourceMetadata - { - public int SystemID; - public string System; - public int SourceID; - public string Source; - } - - /// - /// Intermediate struct for holding and processing Rom/Machine data - /// - public struct Machine : IEquatable - { - public string Name; - public string Comment; - public string Description; - public string Year; - public string Manufacturer; - public string RomOf; - public string CloneOf; - public string SampleOf; - public string SourceFile; - public bool IsBios; - public string Board; - public string RebuildTo; - public bool TorrentZipped; - - public bool Equals(Machine other) - { - if (this.Name == other.Name) - { - return true; - } - - return false; - } - } - /// /// Intermediate struct for holding DAT information /// @@ -326,7 +216,7 @@ namespace SabreTools.Helper public ForcePacking ForcePacking; public OutputFormat OutputFormat; public bool MergeRoms; - public Dictionary> Files; + public Dictionary> Files; // Data specific to the Miss DAT type public bool UseGame; @@ -416,7 +306,7 @@ namespace SabreTools.Helper ForcePacking = this.ForcePacking, OutputFormat = this.OutputFormat, MergeRoms = this.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), UseGame = this.UseGame, Prefix = this.Prefix, Postfix = this.Postfix, diff --git a/SabreTools.Helper/Objects/DATFromDir.cs b/SabreTools.Helper/Objects/DATFromDir.cs index a4781e3f..fb338823 100644 --- a/SabreTools.Helper/Objects/DATFromDir.cs +++ b/SabreTools.Helper/Objects/DATFromDir.cs @@ -57,8 +57,8 @@ namespace SabreTools { _basePath = Path.GetFullPath(basePath); _datdata = datdata; - _datdata.Files = new Dictionary>(); - _datdata.Files.Add("null", new List()); + _datdata.Files = new Dictionary>(); + _datdata.Files.Add("null", new List()); _noMD5 = noMD5; _noSHA1 = noSHA1; _bare = bare; @@ -158,25 +158,7 @@ namespace SabreTools } _logger.Log("Adding blank empty folder: " + gamename); - - Rom blankrom = new Rom - { - Name = romname, - Machine = new Machine - { - Name = gamename, - Description = gamename, - }, - HashData = new Hash - { - Size = -1, - CRC = "null", - MD5 = "null", - SHA1 = "null", - }, - }; - - _datdata.Files["null"].Add(blankrom); + _datdata.Files["null"].Add(new Rom(romname, gamename)); } }); } @@ -221,7 +203,7 @@ namespace SabreTools { if (!_datdata.Files.ContainsKey(key)) { - _datdata.Files.Add(key, new List()); + _datdata.Files.Add(key, new List()); } _datdata.Files[key].Add(rom); @@ -321,18 +303,33 @@ namespace SabreTools /// Process a single file as a file (with found Rom data) /// /// File to be added - /// Rom data to be used to write to file + /// Rom data to be used to write to file /// Path the represents the parent directory /// Parent game to be used - private void ProcessFileHelper(string item, Rom rom, string basepath, string parent) + private void ProcessFileHelper(string item, DatItem datItem, string basepath, string parent) { + // If the datItem isn't a Rom or Disk, return + if (datItem.Type != ItemType.Rom && datItem.Type != ItemType.Disk) + { + return; + } + + string key = ""; + if (datItem.Type == ItemType.Rom) + { + key = ((Rom)datItem).HashData.Size + "-" + ((Rom)datItem).HashData.CRC; + } + else + { + key = ((Disk)datItem).HashData.Size + "-" + ((Disk)datItem).HashData.MD5; + } + // Add the list if it doesn't exist already - string key = rom.HashData.Size + "-" + rom.HashData.CRC; lock (_datdata.Files) { if (!_datdata.Files.ContainsKey(key)) { - _datdata.Files.Add(key, new List()); + _datdata.Files.Add(key, new List()); } } @@ -406,17 +403,14 @@ namespace SabreTools } // Update rom information - rom.Machine = new Machine - { - Name = gamename, - Description = gamename, - }; - rom.Name = romname; + datItem.Name = romname; + datItem.MachineName = gamename; + datItem.MachineDescription = gamename; // Add the file information to the DAT lock (_datdata.Files) { - _datdata.Files[key].Add(rom); + _datdata.Files[key].Add(datItem); } _logger.User("File added: " + romname + Environment.NewLine); diff --git a/SabreTools.Helper/Objects/DatObjects/Archive.cs b/SabreTools.Helper/Objects/DatObjects/Archive.cs new file mode 100644 index 00000000..1f0eaea2 --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/Archive.cs @@ -0,0 +1,91 @@ +namespace SabreTools.Helper +{ + public class Archive : DatItem + { + #region Constructors + + /// + /// Create a default, empty Archive object + /// + public Archive() + { + _name = ""; + _itemType = ItemType.Archive; + } + + /// + /// Create a new Archive object with the included information + /// + /// Name of the item, including extension + public Archive(string name) + { + _name = name; + _itemType = ItemType.Archive; + } + + /// + /// Create a new Archive object with the included information + /// + /// Name of the item, including extension + /// Name for the machine/game + /// Comment for the machine/game + /// Description for the machine/game + /// Year for the machine/game + /// Manufacturer name for the machine/game + /// Set that this machine/game is a rom of + /// Set that this machine/game is a clone of + /// Set that this machine/game is a sample of + /// Source file for the machine/game + /// True if this game is a BIOS, false otherwise + /// Name of the board for this machine/game + /// Name of the game to rebuild to + /// System ID to be associated with + /// System Name to be associated with + /// Source ID to be associated with + /// Source Name to be associated with + public Archive(string name, string machineName, string comment, string machineDescription, string year, + string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, bool isBios, string board, string rebuildTo, + int systemId, string systemName, int sourceId, string sourceName) + { + _name = name; + _itemType = ItemType.Archive; + _machineName = machineName; + _comment = comment; + _machineDescription = machineDescription; + _year = year; + _manufacturer = manufacturer; + _romOf = romOf; + _cloneOf = cloneOf; + _sampleOf = sampleOf; + _sourceFile = sourceFile; + _isBios = isBios; + _board = board; + _rebuildTo = rebuildTo; + _systemId = systemId; + _systemName = systemName; + _sourceId = sourceId; + _sourceName = sourceName; + } + + #endregion + + #region Comparision Methods + + public override bool Equals(DatItem other) + { + // If we don't have a rom, return false + if (_itemType == other.Type) + { + return false; + } + + // Otherwise, treat it as a rom + Archive newOther = (Archive)other; + + // If the archive information matches + return (_name == newOther.Name); + } + + #endregion + } +} diff --git a/SabreTools.Helper/Objects/DatObjects/BiosSet.cs b/SabreTools.Helper/Objects/DatObjects/BiosSet.cs new file mode 100644 index 00000000..ec412bcf --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/BiosSet.cs @@ -0,0 +1,121 @@ +namespace SabreTools.Helper +{ + public class BiosSet : DatItem + { + #region Private instance variables + + private string _description; + private bool? _default; + + #endregion + + #region Publicly facing variables + + public string Description + { + get { return _description; } + set { _description = value; } + } + public bool? Default + { + get { return _default; } + set { _default = value; } + } + + #endregion + + #region Constructors + + /// + /// Create a default, empty Sample object + /// + public BiosSet() + { + _name = ""; + _itemType = ItemType.BiosSet; + } + + /// + /// Create a new Sample object with the included information + /// + /// Name of the item, including extension + /// Description of the Bios set item + /// True if this is the default BIOS, false if it is not, null for undefined + public BiosSet(string name, string description, bool? @default) + { + _name = name; + _itemType = ItemType.BiosSet; + _description = description; + _default = @default; + } + + /// + /// Create a new Sample object with the included information + /// + /// Name of the item, including extension + /// Description of the Bios set item + /// True if this is the default BIOS, false if it is not, null for undefined + /// Name for the machine/game + /// Comment for the machine/game + /// Description for the machine/game + /// Year for the machine/game + /// Manufacturer name for the machine/game + /// Set that this machine/game is a rom of + /// Set that this machine/game is a clone of + /// Set that this machine/game is a sample of + /// Source file for the machine/game + /// True if this game is a BIOS, false otherwise + /// Name of the board for this machine/game + /// Name of the game to rebuild to + /// System ID to be associated with + /// System Name to be associated with + /// Source ID to be associated with + /// Source Name to be associated with + public BiosSet(string name, string description, bool? @default, string machineName, string comment, string machineDescription, string year, + string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, bool isBios, string board, string rebuildTo, + int systemId, string systemName, int sourceId, string sourceName) + { + _name = name; + _itemType = ItemType.BiosSet; + _description = description; + _default = @default; + _machineName = machineName; + _comment = comment; + _machineDescription = machineDescription; + _year = year; + _manufacturer = manufacturer; + _romOf = romOf; + _cloneOf = cloneOf; + _sampleOf = sampleOf; + _sourceFile = sourceFile; + _isBios = isBios; + _board = board; + _rebuildTo = rebuildTo; + _systemId = systemId; + _systemName = systemName; + _sourceId = sourceId; + _sourceName = sourceName; + } + + #endregion + + #region Comparision Methods + + public override bool Equals(DatItem other) + { + // If we don't have a rom, return false + if (_itemType == other.Type) + { + return false; + } + + // Otherwise, treat it as a rom + BiosSet newOther = (BiosSet)other; + + // If the archive information matches + return (_name == newOther.Name && _description == newOther.Description && _default == newOther.Default); + } + + #endregion + } +} diff --git a/SabreTools.Helper/Objects/DatObjects/Disk.cs b/SabreTools.Helper/Objects/DatObjects/Disk.cs new file mode 100644 index 00000000..8ad768ac --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/Disk.cs @@ -0,0 +1,146 @@ +namespace SabreTools.Helper +{ + public class Disk : DatItem + { + #region Private instance variables + + // Disk information + private Hash _hashData; + // private string _merge; + // private DiskStatus _romStatus; + private bool _nodump; + + #endregion + + #region Publicly facing variables + + // Disk information + public Hash HashData + { + get { return _hashData; } + set { _hashData = value; } + } + public bool Nodump + { + get { return _nodump; } + set { _nodump = value; } + } + + #endregion + + #region Constructors + + /// + /// Create a default, empty Disk object + /// + public Disk() + { + _name = ""; + _itemType = ItemType.Disk; + _dupeType = DupeType.None; + _nodump = false; + } + + /// + /// Create a new Disk object with the included information + /// + /// Name of the item, including extension + /// String representation of the MD5 + /// String representation of the SHA-1 + /// True if the file is a nodump, false otherwise + public Disk(string name, string md5, string sha1, bool nodump) + { + _name = name; + _itemType = ItemType.Disk; + _hashData = new Hash + { + MD5 = md5.ToLowerInvariant(), + SHA1 = sha1.ToLowerInvariant(), + }; + _nodump = nodump; + } + + /// + /// Create a new Disk object with the included information + /// + /// Name of the item, including extension + /// String representation of the MD5 + /// String representation of the SHA-1 + /// True if the file is a nodump, false otherwise + /// Name for the machine/game + /// Comment for the machine/game + /// Description for the machine/game + /// Year for the machine/game + /// Manufacturer name for the machine/game + /// Set that this machine/game is a rom of + /// Set that this machine/game is a clone of + /// Set that this machine/game is a sample of + /// Source file for the machine/game + /// True if this game is a BIOS, false otherwise + /// Name of the board for this machine/game + /// Name of the game to rebuild to + /// System ID to be associated with + /// System Name to be associated with + /// Source ID to be associated with + /// Source Name to be associated with + public Disk(string name, string md5, string sha1, bool nodump, string machineName, string comment, + string machineDescription, string year, string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, + bool isBios, string board, string rebuildTo, int systemId, string systemName, int sourceId, string sourceName) + { + _name = name; + _itemType = ItemType.Disk; + _hashData = new Hash + { + MD5 = md5.ToLowerInvariant(), + SHA1 = sha1.ToLowerInvariant(), + }; + _nodump = nodump; + _machineName = machineName; + _comment = comment; + _machineDescription = machineDescription; + _year = year; + _manufacturer = manufacturer; + _romOf = romOf; + _cloneOf = cloneOf; + _sampleOf = sampleOf; + _sourceFile = sourceFile; + _isBios = isBios; + _board = board; + _rebuildTo = rebuildTo; + _systemId = systemId; + _systemName = systemName; + _sourceId = sourceId; + _sourceName = sourceName; + } + + #endregion + + #region Comparision Methods + + public override bool Equals(DatItem other) + { + bool dupefound = false; + + // If we don't have a rom, return false + if (_itemType == other.Type) + { + return dupefound; + } + + // Otherwise, treat it as a rom + Disk newOther = (Disk)other; + + // If either is a nodump, it's never a match + if (_nodump || newOther.Nodump) + { + return dupefound; + } + + dupefound = _hashData.Equals(newOther.HashData, false); + + return dupefound; + } + + #endregion + } +} diff --git a/SabreTools.Helper/Objects/DatObjects/Release.cs b/SabreTools.Helper/Objects/DatObjects/Release.cs new file mode 100644 index 00000000..a82b6433 --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/Release.cs @@ -0,0 +1,149 @@ +namespace SabreTools.Helper +{ + public class Release : DatItem + { + #region Private instance variables + + private string _region; + private string _language; + private string _date; + private bool? _default; + + #endregion + + #region Publicly facing variables + + public string Region + { + get { return _region; } + set { _region = value; } + } + public string Language + { + get { return _language; } + set { _language = value; } + } + public string Date + { + get { return _date; } + set { _date = value; } + } + public bool? Default + { + get { return _default; } + set { _default = value; } + } + + #endregion + + #region Constructors + + /// + /// Create a default, empty Release object + /// + public Release() + { + _name = ""; + _itemType = ItemType.Release; + _region = ""; + _language = ""; + _date = ""; + _default = null; + } + + /// + /// Create a new Release object with the included information + /// + /// Name of the item, including extension + /// Region of the item + /// Language of the item + /// String representation of the Date + /// True if this is the default BIOS, false if it is not, null for undefined + public Release(string name, string region, string language, string date, bool? @default) + { + _name = name; + _itemType = ItemType.Release; + _region = region; + _language = language; + _date = date; + _default = @default; + } + + /// + /// Create a new Release object with the included information + /// + /// Name of the item, including extension + /// Region of the item + /// Language of the item + /// String representation of the Date + /// True if this is the default BIOS, false if it is not, null for undefined + /// Name for the machine/game + /// Comment for the machine/game + /// Description for the machine/game + /// Year for the machine/game + /// Manufacturer name for the machine/game + /// Set that this machine/game is a rom of + /// Set that this machine/game is a clone of + /// Set that this machine/game is a sample of + /// Source file for the machine/game + /// True if this game is a BIOS, false otherwise + /// Name of the board for this machine/game + /// Name of the game to rebuild to + /// System ID to be associated with + /// System Name to be associated with + /// Source ID to be associated with + /// Source Name to be associated with + public Release(string name, string region, string language, string date, bool? @default, string machineName, string comment, + string machineDescription, string year, string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, + bool isBios, string board, string rebuildTo, int systemId, string systemName, int sourceId, string sourceName) + { + _name = name; + _itemType = ItemType.Release; + _region = region; + _language = language; + _date = date; + _default = @default; + _machineName = machineName; + _comment = comment; + _machineDescription = machineDescription; + _year = year; + _manufacturer = manufacturer; + _romOf = romOf; + _cloneOf = cloneOf; + _sampleOf = sampleOf; + _sourceFile = sourceFile; + _isBios = isBios; + _board = board; + _rebuildTo = rebuildTo; + _systemId = systemId; + _systemName = systemName; + _sourceId = sourceId; + _sourceName = sourceName; + } + + #endregion + + #region Comparision Methods + + public override bool Equals(DatItem other) + { + // If we don't have a rom, return false + if (_itemType == other.Type) + { + return false; + } + + // Otherwise, treat it as a rom + Release newOther = (Release)other; + + // If the archive information matches + return (_name == newOther.Name + && _region == newOther.Region + && _language == newOther.Language + && _date == newOther.Date + && _default == newOther.Default); + } + + #endregion + } +} diff --git a/SabreTools.Helper/Objects/DatObjects/Rom.cs b/SabreTools.Helper/Objects/DatObjects/Rom.cs new file mode 100644 index 00000000..51d195ad --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/Rom.cs @@ -0,0 +1,175 @@ +namespace SabreTools.Helper +{ + public class Rom : DatItem + { + #region Private instance variables + + // Rom information + private Hash _hashData; + // private string _merge; + // private RomStatus _romStatus; + private bool _nodump; + private string _date; + + #endregion + + #region Publicly facing variables + + // Rom information + public Hash HashData + { + get { return _hashData; } + set { _hashData = value; } + } + public bool Nodump + { + get { return _nodump; } + set { _nodump = value; } + } + public string Date + { + get { return _date; } + set { _date = value; } + } + + #endregion + + #region Constructors + + /// + /// Create a default, empty Rom object + /// + public Rom() + { + _name = ""; + _itemType = ItemType.Rom; + _dupeType = DupeType.None; + _nodump = false; + _date = ""; + } + + /// + /// Create a "blank" Rom object + /// + /// + /// + public Rom(string name, string machineName) : + this(name, -1, "null", "null", "null", false, null, machineName, null, machineName, null, null, null, null, null, null, false, null, null, -1, null, -1, null) + { + } + + /// + /// Create a new Rom object with the included information + /// + /// Name of the item, including extension + /// Long size of the item + /// String representation of the CRC + /// String representation of the MD5 + /// String representation of the SHA-1 + /// True if the file is a nodump, false otherwise + /// String representation of the Date + public Rom(string name, long size, string crc, string md5, string sha1, bool nodump, string date) + { + _name = name; + _itemType = ItemType.Rom; + _hashData = new Hash + { + Size = size, + CRC = crc.ToLowerInvariant(), + MD5 = md5.ToLowerInvariant(), + SHA1 = sha1.ToLowerInvariant(), + }; + _nodump = nodump; + _date = date; + } + + /// + /// Create a new Rom object with the included information + /// + /// Name of the item, including extension + /// Long size of the item + /// String representation of the CRC + /// String representation of the MD5 + /// String representation of the SHA-1 + /// True if the file is a nodump, false otherwise + /// String representation of the Date + /// Name for the machine/game + /// Comment for the machine/game + /// Description for the machine/game + /// Year for the machine/game + /// Manufacturer name for the machine/game + /// Set that this machine/game is a rom of + /// Set that this machine/game is a clone of + /// Set that this machine/game is a sample of + /// Source file for the machine/game + /// True if this game is a BIOS, false otherwise + /// Name of the board for this machine/game + /// Name of the game to rebuild to + /// System ID to be associated with + /// System Name to be associated with + /// Source ID to be associated with + /// Source Name to be associated with + public Rom(string name, long size, string crc, string md5, string sha1, bool nodump, string date, string machineName, + string comment, string machineDescription, string year, string manufacturer, string romOf, string cloneOf, string sampleOf, + string sourceFile, bool isBios, string board, string rebuildTo, int systemId, string systemName, int sourceId, string sourceName) + { + _name = name; + _itemType = ItemType.Rom; + _hashData = new Hash + { + Size = size, + CRC = crc.ToLowerInvariant(), + MD5 = md5.ToLowerInvariant(), + SHA1 = sha1.ToLowerInvariant(), + }; + _nodump = nodump; + _date = date; + _machineName = machineName; + _comment = comment; + _machineDescription = machineDescription; + _year = year; + _manufacturer = manufacturer; + _romOf = romOf; + _cloneOf = cloneOf; + _sampleOf = sampleOf; + _sourceFile = sourceFile; + _isBios = isBios; + _board = board; + _rebuildTo = rebuildTo; + _systemId = systemId; + _systemName = systemName; + _sourceId = sourceId; + _sourceName = sourceName; + } + + #endregion + + #region Comparision Methods + + public override bool Equals(DatItem other) + { + bool dupefound = false; + + // If we don't have a rom, return false + if (_itemType == other.Type) + { + return dupefound; + } + + // Otherwise, treat it as a rom + Rom newOther = (Rom)other; + + // If either is a nodump, it's never a match + if (_nodump || newOther.Nodump) + { + return dupefound; + } + + dupefound = _hashData.Equals(newOther.HashData, false); + + return dupefound; + } + + #endregion + } +} diff --git a/SabreTools.Helper/Objects/DatObjects/Sample.cs b/SabreTools.Helper/Objects/DatObjects/Sample.cs new file mode 100644 index 00000000..01331f1c --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/Sample.cs @@ -0,0 +1,91 @@ +namespace SabreTools.Helper +{ + public class Sample : DatItem + { + #region Constructors + + /// + /// Create a default, empty Sample object + /// + public Sample() + { + _name = ""; + _itemType = ItemType.Sample; + } + + /// + /// Create a new Sample object with the included information + /// + /// Name of the item, including extension + public Sample(string name) + { + _name = name; + _itemType = ItemType.Sample; + } + + /// + /// Create a new Sample object with the included information + /// + /// Name of the item, including extension + /// Name for the machine/game + /// Comment for the machine/game + /// Description for the machine/game + /// Year for the machine/game + /// Manufacturer name for the machine/game + /// Set that this machine/game is a rom of + /// Set that this machine/game is a clone of + /// Set that this machine/game is a sample of + /// Source file for the machine/game + /// True if this game is a BIOS, false otherwise + /// Name of the board for this machine/game + /// Name of the game to rebuild to + /// System ID to be associated with + /// System Name to be associated with + /// Source ID to be associated with + /// Source Name to be associated with + public Sample(string name, string machineName, string comment, string machineDescription, string year, + string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, bool isBios, string board, string rebuildTo, + int systemId, string systemName, int sourceId, string sourceName) + { + _name = name; + _itemType = ItemType.Sample; + _machineName = machineName; + _comment = comment; + _machineDescription = machineDescription; + _year = year; + _manufacturer = manufacturer; + _romOf = romOf; + _cloneOf = cloneOf; + _sampleOf = sampleOf; + _sourceFile = sourceFile; + _isBios = isBios; + _board = board; + _rebuildTo = rebuildTo; + _systemId = systemId; + _systemName = systemName; + _sourceId = sourceId; + _sourceName = sourceName; + } + + #endregion + + #region Comparision Methods + + public override bool Equals(DatItem other) + { + // If we don't have a rom, return false + if (_itemType == other.Type) + { + return false; + } + + // Otherwise, treat it as a rom + Sample newOther = (Sample)other; + + // If the archive information matches + return (_name == newOther.Name); + } + + #endregion + } +} diff --git a/SabreTools.Helper/Objects/Generate.cs b/SabreTools.Helper/Objects/Generate.cs index bd43c90f..046695c1 100644 --- a/SabreTools.Helper/Objects/Generate.cs +++ b/SabreTools.Helper/Objects/Generate.cs @@ -9,7 +9,7 @@ namespace SabreTools /// /// Generate a DAT from the data in the database /// - class Generate : IGenerate + public class Generate : IGenerate { // Private instance variables private string _systems; @@ -153,7 +153,7 @@ namespace SabreTools } // Retrieve the list of processed roms - Dictionary> dict = ProcessRoms(); + Dictionary> dict = ProcessRoms(); // If the output is null, nothing was found so return false if (dict.Count == 0) @@ -186,9 +186,9 @@ namespace SabreTools /// Preprocess the rom data that is to be included in the outputted DAT /// /// A List of Rom objects containing all information about the files - public Dictionary> ProcessRoms() + public Dictionary> ProcessRoms() { - Dictionary> roms = new Dictionary>(); + Dictionary> roms = new Dictionary>(); // Check if we have listed sources or systems bool sysmerged = (_systems == "" || _systems.Split(',').Length > 1); @@ -236,47 +236,42 @@ JOIN checksums // Retrieve and process the roms for merging while (sldr.Read()) { - Rom temp = new Rom + DatItem temp; + string key = ""; + switch (sldr.GetString(8).ToLowerInvariant()) { - Name = sldr.GetString(7), - Type = (sldr.GetString(8) == "disk" ? ItemType.Disk : ItemType.Rom), - Machine = new Machine - { - Name = sldr.GetString(6), - Manufacturer = sldr.GetString(0), - }, - Metadata = new SourceMetadata - { - System = sldr.GetString(1), - SystemID = sldr.GetInt32(2), - Source = sldr.GetString(3), - SourceID = sldr.GetInt32(5), - }, - HashData = new Hash - { - Size = sldr.GetInt64(9), - CRC = sldr.GetString(10), - MD5 = sldr.GetString(11), - SHA1 = sldr.GetString(12), - }, - }; + case "disk": + temp = new Disk(sldr.GetString(7), sldr.GetString(11), sldr.GetString(12), false, sldr.GetString(13), sldr.GetString(6), + sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, null, null, sldr.GetInt32(2), + sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); + + key = ((Disk)temp).HashData.Size + "-" + ((Disk)temp).HashData.CRC; + break; + case "rom": + default: + temp = new Rom(sldr.GetString(7), sldr.GetInt64(9), sldr.GetString(10), sldr.GetString(11), sldr.GetString(12), false, + sldr.GetString(13), sldr.GetString(6), null, sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, + null, null, sldr.GetInt32(2), sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); + + key = ((Disk)temp).HashData.Size + "-" + ((Disk)temp).HashData.CRC; + break; + } // Rename the game associated if it's still valid and we allow renames if (merged && !_norename) { - temp.Machine.Name = temp.Machine.Name + - (sysmerged ? " [" + temp.Machine.Manufacturer + " - " + temp.Metadata.System + "]" : "") + - (srcmerged ? " [" + temp.Metadata.Source + "]" : ""); + temp.MachineName = temp.MachineName + + (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + + (srcmerged ? " [" + temp.Source + "]" : ""); } - string key = temp.HashData.Size + "-" + temp.HashData.CRC; if (roms.ContainsKey(key)) { roms[key].Add(temp); } else { - List templist = new List(); + List templist = new List(); templist.Add(temp); roms.Add(key, templist); } @@ -290,7 +285,7 @@ JOIN checksums { foreach (string key in roms.Keys) { - roms[key] = RomTools.Merge(roms[key], _logger); + roms[key] = DatItem.Merge(roms[key], _logger); } } // END COMMENT diff --git a/SabreTools.Helper/Objects/GenerateTwo.cs b/SabreTools.Helper/Objects/GenerateTwo.cs index 41d6c82d..381357ab 100644 --- a/SabreTools.Helper/Objects/GenerateTwo.cs +++ b/SabreTools.Helper/Objects/GenerateTwo.cs @@ -188,14 +188,14 @@ namespace SabreTools List keys = datdata.Files.Keys.ToList(); foreach (string key in keys) { - List temp = new List(); - List newroms = datdata.Files[key]; + List temp = new List(); + List newroms = datdata.Files[key]; for (int i = 0; i < newroms.Count; i++) { - Rom rom = newroms[i]; + Rom rom = (Rom)newroms[i]; // In the case that the RomData is incomplete, skip it - if (rom.Name == null || rom.Machine.Name == null) + if (rom.Name == null || rom.MachineName == null) { continue; } @@ -209,27 +209,27 @@ namespace SabreTools rom.Name = Regex.Replace(rom.Name, @"(.*) \.(.*)", "$1.$2"); // Run the name through the filters to make sure that it's correct - rom.Machine.Name = Style.NormalizeChars(rom.Machine.Name); - rom.Machine.Name = Style.RussianToLatin(rom.Machine.Name); - rom.Machine.Name = Style.SearchPattern(rom.Machine.Name); + rom.MachineName = Style.NormalizeChars(rom.MachineName); + rom.MachineName = Style.RussianToLatin(rom.MachineName); + rom.MachineName = Style.SearchPattern(rom.MachineName); // WoD gets rid of anything past the first "(" or "[" as the name, we will do the same string stripPattern = @"(([[(].*[\)\]] )?([^([]+))"; Regex stripRegex = new Regex(stripPattern); - Match stripMatch = stripRegex.Match(rom.Machine.Name); - rom.Machine.Name = stripMatch.Groups[1].Value; + Match stripMatch = stripRegex.Match(rom.MachineName); + rom.MachineName = stripMatch.Groups[1].Value; - rom.Machine.Name = rom.Machine.Name.TrimEnd().TrimStart(); + rom.MachineName = rom.MachineName.TrimEnd().TrimStart(); if (!_norename) { - rom.Machine.Name += " [" + sources[rom.Metadata.SourceID] + "]"; + rom.MachineName += " [" + sources[rom.SourceID] + "]"; } // If a game has source "0" it's Default. Make this Int32.MaxValue for sorting purposes - if (rom.Metadata.SourceID == 0) + if (rom.SourceID == 0) { - rom.Metadata.SourceID = Int32.MaxValue; + rom.SourceID = Int32.MaxValue; } temp.Add(rom); diff --git a/SabreTools.Helper/Objects/Import.cs b/SabreTools.Helper/Objects/Import.cs index 2834b292..16f802c7 100644 --- a/SabreTools.Helper/Objects/Import.cs +++ b/SabreTools.Helper/Objects/Import.cs @@ -370,27 +370,27 @@ namespace SabreTools DatTools.Parse(_filepath, sysid, srcid, ref datdata, _logger); // Sort inputted roms into games - SortedDictionary> sortable = new SortedDictionary>(); + SortedDictionary> sortable = new SortedDictionary>(); long count = 0; - foreach (List roms in datdata.Files.Values) + foreach (List roms in datdata.Files.Values) { - List newroms = roms; + List newroms = roms; if (datdata.MergeRoms) { - newroms = RomTools.Merge(newroms, _logger); + newroms = DatItem.Merge(newroms, _logger); } foreach (Rom rom in newroms) { count++; - string key = rom.Metadata.SystemID.ToString().PadLeft(10, '0') + "-" + rom.Metadata.SourceID.ToString().PadLeft(10, '0') + "-" + rom.Machine.Name.ToLowerInvariant(); + string key = rom.SystemID.ToString().PadLeft(10, '0') + "-" + rom.SourceID.ToString().PadLeft(10, '0') + "-" + rom.MachineName.ToLowerInvariant(); if (sortable.ContainsKey(key)) { sortable[key].Add(rom); } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); sortable.Add(key, temp); } @@ -400,8 +400,8 @@ namespace SabreTools // Loop over all roms, checking for adds foreach (string key in sortable.Keys) { - List roms = sortable[key]; - RomTools.Sort(ref roms, true); + List roms = sortable[key]; + DatItem.Sort(ref roms, true); long gameid = -1; using (SqliteConnection dbc = new SqliteConnection(_connectionString)) @@ -409,7 +409,7 @@ namespace SabreTools dbc.Open(); // For each game, check for a new ID - gameid = AddGame(sysid, roms[0].Machine.Name, srcid, dbc); + gameid = AddGame(sysid, roms[0].MachineName, srcid, dbc); foreach (Rom rom in roms) { @@ -573,14 +573,14 @@ COMMIT;"; // WoD gets rid of anything past the first "(" or "[" as the name, we will do the same string stripPattern = @"(([[(].*[\)\]] )?([^([]+))"; Regex stripRegex = new Regex(stripPattern); - Match stripMatch = stripRegex.Match(rom.Machine.Name); - rom.Machine.Name = stripMatch.Groups[1].Value; + Match stripMatch = stripRegex.Match(rom.MachineName); + rom.MachineName = stripMatch.Groups[1].Value; // Run the name through the filters to make sure that it's correct - rom.Machine.Name = Style.NormalizeChars(rom.Machine.Name); - rom.Machine.Name = Style.RussianToLatin(rom.Machine.Name); - rom.Machine.Name = Style.SearchPattern(rom.Machine.Name); - rom.Machine.Name = rom.Machine.Name.Trim(); + rom.MachineName = Style.NormalizeChars(rom.MachineName); + rom.MachineName = Style.RussianToLatin(rom.MachineName); + rom.MachineName = Style.SearchPattern(rom.MachineName); + rom.MachineName = rom.MachineName.Trim(); // Process the rom name @@ -629,11 +629,11 @@ COMMIT;"; query = @"BEGIN; INSERT OR IGNORE INTO hashdata (hashid, key, value) VALUES " + "(" + hashid + ", 'name', '" + rom.Name.Replace("'", "''") + "'), " + - "(" + hashid + ", 'game', '" + rom.Machine.Name.Replace("'", "''") + "'), " + + "(" + hashid + ", 'game', '" + rom.MachineName.Replace("'", "''") + "'), " + "(" + hashid + ", 'type', '" + rom.Type + "'), " + "(" + hashid + ", 'lastupdated', '" + date + @"'); -INSERT OR IGNORE INTO gamesystem (game, systemid) VALUES ('" + rom.Machine.Name.Replace("'", "''") + "', " + sysid + @"); -INSERT OR IGNORE INTO gamesource (game, sourceid) VALUES ('" + rom.Machine.Name.Replace("'", "''") + "', " + srcid + @"); +INSERT OR IGNORE INTO gamesystem (game, systemid) VALUES ('" + rom.MachineName.Replace("'", "''") + "', " + sysid + @"); +INSERT OR IGNORE INTO gamesource (game, sourceid) VALUES ('" + rom.MachineName.Replace("'", "''") + "', " + srcid + @"); COMMIT;"; using (SqliteCommand slc = new SqliteCommand(query, dbc)) diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index 5ae87992..5a978d1b 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -76,7 +76,7 @@ namespace SabreTools.Helper _cursorLeft = Console.CursorLeft; _matched = new Dat { - Files = new Dictionary>(), + Files = new Dictionary>(), }; } @@ -145,7 +145,7 @@ namespace SabreTools.Helper // Setup the fixdat _matched = (Dat)_datdata.CloneHeader(); - _matched.Files = new Dictionary>(); + _matched.Files = new Dictionary>(); _matched.FileName = "fixDat_" + _matched.FileName; _matched.Name = "fixDat_" + _matched.Name; _matched.Description = "fixDat_" + _matched.Description; @@ -153,12 +153,12 @@ namespace SabreTools.Helper // Now that all files are parsed, get only files found in directory bool found = false; - foreach (List roms in _datdata.Files.Values) + foreach (List roms in _datdata.Files.Values) { - List newroms = RomTools.Merge(roms, _logger); + List newroms = DatItem.Merge(roms, _logger); foreach (Rom rom in newroms) { - if (rom.Metadata.SourceID == 99) + if (rom.SourceID == 99) { found = true; string key = rom.HashData.Size + "-" + rom.HashData.CRC; @@ -168,7 +168,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); _matched.Files.Add(key, temp); } @@ -343,7 +343,7 @@ namespace SabreTools.Helper _logger.User("Getting source file information..."); Dat matchdat = new Dat { - Files = new Dictionary>(), + Files = new Dictionary>(), }; foreach (string file in files) { @@ -409,15 +409,15 @@ namespace SabreTools.Helper #region Find all files to rebuild and bucket by game // Create a dictionary of from/to Rom mappings - Dictionary toFromMap = new Dictionary(); + Dictionary toFromMap = new Dictionary(); // Now populate it foreach (string key in matchdat.Files.Keys) { - foreach (Rom rom in matchdat.Files[key]) + foreach (DatItem rom in matchdat.Files[key]) { - List matched = RomTools.GetDuplicates(rom, _datdata, _logger, true); - foreach (Rom match in matched) + List matched = DatItem.GetDuplicates(rom, _datdata, _logger, true); + foreach (DatItem match in matched) { try { @@ -429,7 +429,7 @@ namespace SabreTools.Helper } // Then bucket the keys by game for better output - SortedDictionary> keysByGame = DatTools.BucketByGame(toFromMap.Keys.ToList(), false, true, _logger); + SortedDictionary> keysByGame = DatTools.BucketByGame(toFromMap.Keys.ToList(), false, true, _logger); #endregion @@ -467,7 +467,7 @@ namespace SabreTools.Helper } // Otherwise, set the machine name as the full path to the file - rom.Machine.Name = Path.GetDirectoryName(Path.GetFullPath(file)); + rom.MachineName = Path.GetDirectoryName(Path.GetFullPath(file)); // Add the rom information to the Dat string key = rom.HashData.Size + "-" + rom.HashData.CRC; @@ -477,7 +477,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); matchdat.Files.Add(key, temp); } @@ -494,7 +494,7 @@ namespace SabreTools.Helper Skippers.TransformStream(input, output, rule, _logger, false, true); Rom romNH = FileTools.GetSingleStreamInfo(output); romNH.Name = "HEAD::" + rom.Name; - romNH.Machine.Name = rom.Machine.Name; + romNH.MachineName = rom.MachineName; // Add the rom information to the Dat key = romNH.HashData.Size + "-" + romNH.HashData.CRC; @@ -504,7 +504,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(romNH); matchdat.Files.Add(key, temp); } @@ -548,7 +548,7 @@ namespace SabreTools.Helper } // Try to find the matches to the file that was found - List foundroms = RomTools.GetDuplicates(rom, _datdata, _logger); + List foundroms = DatItem.GetDuplicates(rom, _datdata, _logger); _logger.Log("File '" + input + "' had " + foundroms.Count + " matches in the DAT!"); foreach (Rom found in foundroms) { @@ -562,7 +562,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(found); _matched.Files.Add(key, temp); } @@ -570,7 +570,7 @@ namespace SabreTools.Helper if (_toFolder) { // Copy file to output directory - string gamedir = Path.Combine(_outDir, found.Machine.Name); + string gamedir = Path.Combine(_outDir, found.MachineName); if (!Directory.Exists(gamedir)) { Directory.CreateDirectory(gamedir); @@ -618,7 +618,7 @@ namespace SabreTools.Helper } // Try to find the matches to the file that was found - List founddroms = RomTools.GetDuplicates(drom, _datdata, _logger); + List founddroms = DatItem.GetDuplicates(drom, _datdata, _logger); _logger.Log("File '" + newinput + "' had " + founddroms.Count + " matches in the DAT!"); foreach (Rom found in founddroms) { @@ -630,7 +630,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(found); _matched.Files.Add(key, temp); } @@ -641,7 +641,7 @@ namespace SabreTools.Helper if (_toFolder) { // Copy file to output directory - string gamedir = Path.Combine(_outDir, found.Machine.Name); + string gamedir = Path.Combine(_outDir, found.MachineName); if (!Directory.Exists(gamedir)) { Directory.CreateDirectory(gamedir); @@ -683,7 +683,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(newfound); _matched.Files.Add(key, temp); } @@ -691,7 +691,7 @@ namespace SabreTools.Helper if (_toFolder) { // Copy file to output directory - string gamedir = Path.Combine(_outDir, found.Machine.Name); + string gamedir = Path.Combine(_outDir, found.MachineName); if (!Directory.Exists(gamedir)) { Directory.CreateDirectory(gamedir); @@ -750,7 +750,7 @@ namespace SabreTools.Helper foreach (Rom rom in internalRomData) { // Try to find the matches to the file that was found - List foundroms = RomTools.GetDuplicates(rom, _datdata, _logger); + List foundroms = DatItem.GetDuplicates(rom, _datdata, _logger); _logger.Log("File '" + rom.Name + "' had " + foundroms.Count + " matches in the DAT!"); foreach (Rom found in foundroms) { @@ -762,7 +762,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(found); _matched.Files.Add(key, temp); } @@ -774,7 +774,7 @@ namespace SabreTools.Helper string outfile = FileTools.ExtractSingleItemFromArchive(input, rom.Name, _tempDir, _logger); if (File.Exists(outfile)) { - string gamedir = Path.Combine(_outDir, found.Machine.Name); + string gamedir = Path.Combine(_outDir, found.MachineName); if (!Directory.Exists(gamedir)) { Directory.CreateDirectory(gamedir); @@ -888,7 +888,7 @@ namespace SabreTools.Helper } // Now process the inputs (assumed that it's archived sets as of right now - Dictionary> scanned = new Dictionary>(); + Dictionary> scanned = new Dictionary>(); foreach (string archive in Directory.EnumerateFiles(_outDir, "*", SearchOption.AllDirectories)) { // If we are in quickscan, get the list of roms that way @@ -921,7 +921,7 @@ namespace SabreTools.Helper } else { - List templist = new List(); + List templist = new List(); templist.Add(rom); scanned.Add(key, templist); } @@ -935,7 +935,7 @@ namespace SabreTools.Helper } // Now that we have all of the from DAT and from folder roms, we try to match them, removing the perfect matches - Dictionary> remove = new Dictionary>(); + Dictionary> remove = new Dictionary>(); foreach (string key in scanned.Keys) { // If the key doesn't even exist in the DAT, then mark the entire key for removal @@ -953,8 +953,8 @@ namespace SabreTools.Helper // Otherwise check each of the values individually else { - List romsList = _datdata.Files[key]; - List scannedList = scanned[key]; + List romsList = _datdata.Files[key]; + List scannedList = scanned[key]; foreach (Rom rom in scannedList) { if (!romsList.Contains(rom)) @@ -965,7 +965,7 @@ namespace SabreTools.Helper } else { - List templist = new List(); + List templist = new List(); templist.Add(rom); remove.Add(key, templist); } diff --git a/SabreTools.Helper/Objects/Stats.cs b/SabreTools.Helper/Objects/Stats.cs index f50236d2..ce1887d2 100644 --- a/SabreTools.Helper/Objects/Stats.cs +++ b/SabreTools.Helper/Objects/Stats.cs @@ -49,7 +49,7 @@ namespace SabreTools.Helper List games = new List(); Dat datdata = new Dat(); DatTools.Parse(filename, 0, 0, ref datdata, _logger); - SortedDictionary> newroms = DatTools.BucketByGame(datdata.Files, false, true, _logger, false); + SortedDictionary> newroms = DatTools.BucketByGame(datdata.Files, false, true, _logger, false); // Output single DAT stats (if asked) if (_single) @@ -116,7 +116,7 @@ Please check the log folder if the stats scrolled offscreen"); datdata.NodumpCount = 0; // Loop through and add - foreach (List roms in datdata.Files.Values) + foreach (List roms in datdata.Files.Values) { foreach (Rom rom in roms) { @@ -131,7 +131,7 @@ Please check the log folder if the stats scrolled offscreen"); } } - SortedDictionary> newroms = DatTools.BucketByGame(datdata.Files, false, true, logger, false); + SortedDictionary> newroms = DatTools.BucketByGame(datdata.Files, false, true, logger, false); if (datdata.TotalSize < 0) { datdata.TotalSize = Int64.MaxValue + datdata.TotalSize; diff --git a/SabreTools.Helper/SabreTools.Helper.csproj b/SabreTools.Helper/SabreTools.Helper.csproj index 4d75e236..b4b0891c 100644 --- a/SabreTools.Helper/SabreTools.Helper.csproj +++ b/SabreTools.Helper/SabreTools.Helper.csproj @@ -101,11 +101,18 @@ + + + + + + + @@ -130,7 +137,6 @@ Resources.fr-FR.resx - @@ -142,8 +148,6 @@ - - diff --git a/SabreTools.Helper/Skippers/Skippers.cs b/SabreTools.Helper/Skippers/Skippers.cs index 7c482b78..e6e2250d 100644 --- a/SabreTools.Helper/Skippers/Skippers.cs +++ b/SabreTools.Helper/Skippers/Skippers.cs @@ -10,7 +10,7 @@ namespace SabreTools.Helper public class Skippers { // Local paths - public const string Path = "Skippers"; + public const string LocalPath = "Skippers"; // Header skippers represented by a list of skipper objects private static List _list; @@ -56,9 +56,9 @@ namespace SabreTools.Helper _list = new List(); } - foreach (string skipperFile in Directory.EnumerateFiles(Path, "*", SearchOption.AllDirectories)) + foreach (string skipperFile in Directory.EnumerateFiles(LocalPath, "*", SearchOption.AllDirectories)) { - _list.Add(PopulateSkippersHelper(System.IO.Path.GetFullPath(skipperFile))); + _list.Add(PopulateSkippersHelper(Path.GetFullPath(skipperFile))); } } @@ -527,9 +527,9 @@ namespace SabreTools.Helper } // Create the output directory if it doesn't already - if (!Directory.Exists(System.IO.Path.GetDirectoryName(output))) + if (!Directory.Exists(Path.GetDirectoryName(output))) { - Directory.CreateDirectory(System.IO.Path.GetDirectoryName(output)); + Directory.CreateDirectory(Path.GetDirectoryName(output)); } logger.User("Attempting to apply rule to '" + input + "'"); @@ -725,7 +725,7 @@ namespace SabreTools.Helper XmlDocument doc = new XmlDocument(); try { - doc.LoadXml(File.ReadAllText(System.IO.Path.Combine(Path, skipper + ".xml"))); + doc.LoadXml(File.ReadAllText(Path.Combine(LocalPath, skipper + ".xml"))); } catch (XmlException ex) { diff --git a/SabreTools.Helper/Tools/DatTools.cs b/SabreTools.Helper/Tools/DatTools.cs index 8c33d203..13d071c0 100644 --- a/SabreTools.Helper/Tools/DatTools.cs +++ b/SabreTools.Helper/Tools/DatTools.cs @@ -180,7 +180,7 @@ namespace SabreTools.Helper // Make sure there's a dictionary to read to if (datdata.Files == null) { - datdata.Files = new Dictionary>(); + datdata.Files = new Dictionary>(); } // Now parse the correct type of DAT @@ -307,27 +307,48 @@ namespace SabreTools.Helper temptype = ItemType.Sample; } - Rom rom = new Rom + // Create the proper DatItem based on the type + DatItem item; + switch (temptype) { - Machine = new Machine - { - Name = tempgamename, - Description = gamedesc, - CloneOf = cloneof, - RomOf = romof, - SampleOf = sampleof, - Manufacturer = manufacturer, - Year = year, - }, - Type = temptype, - Metadata = new SourceMetadata { SystemID = sysid, SourceID = srcid }, - }; + case ItemType.Archive: + item = new Archive(); + break; + case ItemType.BiosSet: + item = new BiosSet(); + break; + case ItemType.Disk: + item = new Disk(); + break; + case ItemType.Release: + item = new Release(); + break; + case ItemType.Sample: + item = new Sample(); + break; + case ItemType.Rom: + default: + item = new Rom(); + break; + } + + // Then populate it with information + item.MachineName = tempgamename; + item.MachineDescription = gamedesc; + item.CloneOf = cloneof; + item.RomOf = romof; + item.SampleOf = sampleof; + item.Manufacturer = manufacturer; + item.Year = year; + item.SystemID = sysid; + item.SourceID = srcid; + Hash tempHash = new Hash(); // If we have a sample, treat it special if (temptype == ItemType.Sample) { line = line.Trim().Remove(0, 6).Trim().Replace("\"", ""); // Remove "sample" from the input string - rom.Name = line; + item.Name = line; } // Otherwise, process the rest of the line @@ -348,7 +369,14 @@ namespace SabreTools.Helper // Special case for nodump... else if (gc[i] == "nodump" && attrib != "status" && attrib != "flags") { - rom.Nodump = true; + if (item.Type == ItemType.Rom) + { + ((Rom)item).Nodump = true; + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).Nodump = true; + } } // Even number of quotes, not in a quote, not in attribute else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && !quote && attrib == "") @@ -361,28 +389,38 @@ namespace SabreTools.Helper switch (attrib.ToLowerInvariant()) { case "name": - rom.Name = gc[i].Replace("\"", ""); + item.Name = gc[i].Replace("\"", ""); break; case "size": - Int64.TryParse(gc[i].Replace("\"", ""), out rom.HashData.Size); + Int64.TryParse(gc[i].Replace("\"", ""), out tempHash.Size); break; case "crc": - rom.HashData.CRC = gc[i].Replace("\"", "").ToLowerInvariant(); + tempHash.CRC = gc[i].Replace("\"", "").ToLowerInvariant(); break; case "md5": - rom.HashData.MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); + tempHash.MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); break; case "sha1": - rom.HashData.SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); + tempHash.SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); break; case "flags": if (gc[i].Replace("\"", "").ToLowerInvariant() == "nodump") { - rom.Nodump = true; + if (item.Type == ItemType.Rom) + { + ((Rom)item).Nodump = true; + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).Nodump = true; + } } break; case "date": - rom.Date = gc[i].Replace("\"", "") + " " + gc[i + 1].Replace("\"", ""); + if (item.Type == ItemType.Rom) + { + ((Rom)item).Date = gc[i].Replace("\"", "") + " " + gc[i + 1].Replace("\"", ""); + } i++; break; } @@ -422,28 +460,38 @@ namespace SabreTools.Helper switch (attrib.ToLowerInvariant()) { case "name": - rom.Name = val; + item.Name = val; break; case "size": - Int64.TryParse(val, out rom.HashData.Size); + Int64.TryParse(val, out tempHash.Size); break; case "crc": - rom.HashData.CRC = val.ToLowerInvariant(); + tempHash.CRC = val.ToLowerInvariant(); break; case "md5": - rom.HashData.MD5 = val.ToLowerInvariant(); + tempHash.MD5 = val.ToLowerInvariant(); break; case "sha1": - rom.HashData.SHA1 = val.ToLowerInvariant(); + tempHash.SHA1 = val.ToLowerInvariant(); break; case "flags": if (val.ToLowerInvariant() == "nodump") { - rom.Nodump = true; + if (item.Type == ItemType.Rom) + { + ((Rom)item).Nodump = true; + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).Nodump = true; + } } break; case "date": - rom.Date = gc[i].Replace("\"", "") + " " + gc[i + 1].Replace("\"", ""); + if (item.Type == ItemType.Rom) + { + ((Rom)item).Date = gc[i].Replace("\"", "") + " " + gc[i + 1].Replace("\"", ""); + } i++; break; } @@ -457,7 +505,15 @@ namespace SabreTools.Helper // Now process and add the rom string key = ""; - ParseAddHelper(rom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); + if (item.Type == ItemType.Rom) + { + ((Rom)item).HashData = tempHash; + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).HashData = tempHash; + } + ParseAddHelper(item, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); } // If the line is anything but a rom or disk and we're in a block else if (Regex.IsMatch(line, Constants.ItemPatternCMP) && block) @@ -741,23 +797,8 @@ namespace SabreTools.Helper */ string[] rominfo = line.Split('¬'); - Rom rom = new Rom - { - Machine = new Machine - { - Name = rominfo[3], - Description = rominfo[4], - CloneOf = rominfo[1], - RomOf = rominfo[8], - }, - Name = rominfo[5], - HashData = new Hash - { - CRC = rominfo[6].ToLowerInvariant(), - Size = Int64.Parse(rominfo[7]), - }, - Metadata = new SourceMetadata { SystemID = sysid, SourceID = srcid }, - }; + Rom rom = new Rom(rominfo[5], Int64.Parse(rominfo[7]), rominfo[6], null, null, false, null, rominfo[3], null, + rominfo[4], null, null, rominfo[8], rominfo[1], null, null, false, null, null, sysid, null, srcid, null); // Now process and add the rom string key = ""; @@ -844,24 +885,7 @@ namespace SabreTools.Helper if (empty) { string tempgame = String.Join("\\", parent); - - Rom rom = new Rom - { - Type = ItemType.Rom, - Name = "null", - Machine = new Machine - { - Name = tempgame, - Description = tempgame, - }, - HashData = new Hash - { - Size = -1, - CRC = "null", - MD5 = "null", - SHA1 = "null", - }, - }; + Rom rom = new Rom("null", tempgame); // Now process and add the rom ParseAddHelper(rom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1238,23 +1262,7 @@ namespace SabreTools.Helper } } - Rom relrom = new Rom - { - Machine = new Machine - { - Name = tempname, - Description = gamedesc, - RomOf = romof, - CloneOf = cloneof, - SampleOf = sampleof, - }, - Name = subreader.GetAttribute("name"), - Type = ItemType.Release, - Region = subreader.GetAttribute("region"), - Language = subreader.GetAttribute("language"), - Default = defaultrel, - Metadata = new SourceMetadata { SystemID = sysid, System = filename, SourceID = srcid }, - }; + DatItem relrom = new Release(subreader.GetAttribute("name"), subreader.GetAttribute("region"), subreader.GetAttribute("language"), date, defaultrel); // Now process and add the rom ParseAddHelper(relrom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1275,22 +1283,8 @@ namespace SabreTools.Helper } } - Rom biosrom = new Rom - { - Machine = new Machine - { - Name = tempname, - Description = gamedesc, - RomOf = romof, - CloneOf = cloneof, - SampleOf = sampleof, - }, - Name = subreader.GetAttribute("name"), - Type = ItemType.BiosSet, - Description = subreader.GetAttribute("description"), - Default = defaultbios, - Metadata = new SourceMetadata { SystemID = sysid, System = filename, SourceID = srcid }, - }; + DatItem biosrom = new BiosSet(subreader.GetAttribute("name"), subreader.GetAttribute("description"), defaultbios, + tempname, null, gamedesc, null, null, romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null); // Now process and add the rom ParseAddHelper(biosrom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1298,21 +1292,17 @@ namespace SabreTools.Helper subreader.Read(); break; case "archive": + DatItem archiverom = new Archive(subreader.GetAttribute("name"), tempname, null, gamedesc, null, null, + romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null); + + // Now process and add the rom + ParseAddHelper(archiverom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); + + subreader.Read(); + break; case "sample": - Rom samplerom = new Rom - { - Machine = new Machine - { - Name = tempname, - Description = gamedesc, - RomOf = romof, - CloneOf = cloneof, - SampleOf = sampleof, - }, - Name = subreader.GetAttribute("name"), - Type = (subreader.Name == "sample" ? ItemType.Sample : ItemType.Archive), - Metadata = new SourceMetadata { SystemID = sysid, System = filename, SourceID = srcid }, - }; + DatItem samplerom = new Sample(subreader.GetAttribute("name"), tempname, null, gamedesc, null, null, + romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null); // Now process and add the rom ParseAddHelper(samplerom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1362,8 +1352,13 @@ namespace SabreTools.Helper if (subreader.GetAttribute("loadflag") == "continue" || subreader.GetAttribute("loadflag") == "ignore") { int index = datdata.Files[key].Count() - 1; - Rom lastrom = datdata.Files[key][index]; - lastrom.HashData.Size += size; + DatItem lastrom = datdata.Files[key][index]; + if (lastrom.Type == ItemType.Rom) + { + Hash temphash = ((Rom)lastrom).HashData; + temphash.Size += size; + ((Rom)lastrom).HashData = temphash; + } datdata.Files[key].RemoveAt(index); datdata.Files[key].Add(lastrom); subreader.Read(); @@ -1376,29 +1371,22 @@ namespace SabreTools.Helper tempname = Style.CleanGameName(tempname.Split(Path.DirectorySeparatorChar)); } - Rom inrom = new Rom + DatItem inrom; + switch (subreader.Name.ToLowerInvariant()) { - Machine = new Machine - { - Name = tempname, - Description = gamedesc, - RomOf = romof, - CloneOf = cloneof, - SampleOf = sampleof, - }, - Name = subreader.GetAttribute("name"), - Type = (subreader.Name.ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom), - HashData = new Hash - { - Size = size, - CRC = subreader.GetAttribute("crc")?.ToLowerInvariant(), - MD5 = subreader.GetAttribute("md5")?.ToLowerInvariant(), - SHA1 = subreader.GetAttribute("sha1")?.ToLowerInvariant(), - }, - Nodump = isnodump, - Date = date, - Metadata = new SourceMetadata { SystemID = sysid, System = filename, SourceID = srcid }, - }; + case "disk": + inrom = new Disk(subreader.GetAttribute("name"), subreader.GetAttribute("md5")?.ToLowerInvariant(), + subreader.GetAttribute("sha1")?.ToLowerInvariant(), isnodump, tempname, null, gamedesc, null, null, + romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null); + break; + case "rom": + default: + inrom = new Rom(subreader.GetAttribute("name"), size, subreader.GetAttribute("crc")?.ToLowerInvariant(), + subreader.GetAttribute("md5")?.ToLowerInvariant(), subreader.GetAttribute("sha1")?.ToLowerInvariant(), isnodump, + date, tempname, null, gamedesc, null, null, romof, cloneof, sampleof, null, false, null, null, sysid, filename, + srcid, null); + break; + } // Now process and add the rom ParseAddHelper(inrom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1417,23 +1405,7 @@ namespace SabreTools.Helper { tempname = (parent.Count > 0 ? String.Join("\\", parent) + Path.DirectorySeparatorChar : "") + tempname; - Rom inrom = new Rom - { - Type = ItemType.Rom, - Name = "null", - Machine = new Machine - { - Name = tempname, - Description = tempname, - }, - HashData = new Hash - { - Size = -1, - CRC = "null", - MD5 = "null", - SHA1 = "null", - } - }; + Rom inrom = new Rom("null", tempname); // Now process and add the rom ParseAddHelper(inrom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1525,8 +1497,13 @@ namespace SabreTools.Helper if (xtr.GetAttribute("loadflag") == "continue" || xtr.GetAttribute("loadflag") == "ignore") { int index = datdata.Files[key].Count() - 1; - Rom lastrom = datdata.Files[key][index]; - lastrom.HashData.Size += size; + DatItem lastrom = datdata.Files[key][index]; + if (lastrom.Type == ItemType.Rom) + { + Hash temphash = ((Rom)lastrom).HashData; + temphash.Size += size; + ((Rom)lastrom).HashData = temphash; + } datdata.Files[key].RemoveAt(index); datdata.Files[key].Add(lastrom); continue; @@ -1545,25 +1522,22 @@ namespace SabreTools.Helper } } - Rom rom = new Rom + DatItem rom; + switch (xtr.GetAttribute("type").ToLowerInvariant()) { - Machine = new Machine - { - Name = tempname, - }, - Name = xtr.GetAttribute("name"), - Type = (xtr.GetAttribute("type").ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom), - HashData = new Hash - { - Size = size, - CRC = xtr.GetAttribute("crc")?.ToLowerInvariant(), - MD5 = xtr.GetAttribute("md5")?.ToLowerInvariant(), - SHA1 = xtr.GetAttribute("sha1")?.ToLowerInvariant(), - }, - Nodump = isnodump, - Date = date, - Metadata = new SourceMetadata { SystemID = sysid, System = filename, SourceID = srcid }, - }; + case "disk": + rom = new Disk(xtr.GetAttribute("name"), xtr.GetAttribute("md5")?.ToLowerInvariant(), + xtr.GetAttribute("sha1")?.ToLowerInvariant(), isnodump, tempname, null, tempname, null, null, + null, null, null, null, false, null, null, sysid, filename, srcid, null); + break; + case "rom": + default: + rom = new Rom(xtr.GetAttribute("name"), size, xtr.GetAttribute("crc")?.ToLowerInvariant(), + xtr.GetAttribute("md5")?.ToLowerInvariant(), xtr.GetAttribute("sha1")?.ToLowerInvariant(), isnodump, + date, tempname, null, tempname, null, null, null, null, null, null, false, null, null, sysid, filename, + srcid, null); + break; + } // Now process and add the rom ParseAddHelper(rom, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); @@ -1584,7 +1558,7 @@ namespace SabreTools.Helper /// /// Add a rom to the Dat after checking /// - /// Rom data to check against + /// Item data to check against /// Dat to add information to, if possible /// Name of the game to match (can use asterisk-partials) /// Name of the rom to match (can use asterisk-partials) @@ -1600,96 +1574,145 @@ namespace SabreTools.Helper /// True if all games should be replaced by '!', false otherwise /// String representing root directory to compare against for length calculation /// Logger object for console and/or file output - private static void ParseAddHelper(Rom rom, ref Dat datdata, string gamename, string romname, string romtype, long sgt, long slt, + private static void ParseAddHelper(DatItem item, ref Dat datdata, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, bool? nodump, bool trim, bool single, string root, bool clean, Logger logger, out string key) { key = ""; // If there's no name in the rom, we log and skip it - if (String.IsNullOrEmpty(rom.Name)) + if (String.IsNullOrEmpty(item.Name)) { logger.Warning("Rom with no name found! Skipping..."); return; } // If we're in cleaning mode, sanitize the game name - rom.Machine.Name = (clean ? Style.CleanGameName(rom.Machine.Name) : rom.Machine.Name); + item.MachineName = (clean ? Style.CleanGameName(item.MachineName) : item.MachineName); - // Sanitize the hashes from null, hex sizes, and "true blank" strings - rom.HashData.CRC = Style.CleanHashData(rom.HashData.CRC, Constants.CRCLength); - rom.HashData.MD5 = Style.CleanHashData(rom.HashData.MD5, Constants.MD5Length); - rom.HashData.SHA1 = Style.CleanHashData(rom.HashData.SHA1, Constants.SHA1Length); - - // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info - if (rom.Type == ItemType.Rom - && (rom.HashData.Size == 0 || rom.HashData.Size == -1) - && ((rom.HashData.CRC == Constants.CRCZero || rom.HashData.CRC == "") - || rom.HashData.MD5 == Constants.MD5Zero - || rom.HashData.SHA1 == Constants.SHA1Zero)) + // If we have a Rom or a Disk, clean the hash data + if (item.Type == ItemType.Rom) { - rom.HashData.Size = Constants.SizeZero; - rom.HashData.CRC = Constants.CRCZero; - rom.HashData.MD5 = Constants.MD5Zero; - rom.HashData.SHA1 = Constants.SHA1Zero; + Rom itemRom = (Rom)item; + + // Sanitize the hashes from null, hex sizes, and "true blank" strings + Hash tempItemHash = itemRom.HashData; + tempItemHash.CRC = Style.CleanHashData(itemRom.HashData.CRC, Constants.CRCLength); + tempItemHash.MD5 = Style.CleanHashData(itemRom.HashData.MD5, Constants.MD5Length); + tempItemHash.SHA1 = Style.CleanHashData(itemRom.HashData.SHA1, Constants.SHA1Length); + + // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info + if ((tempItemHash.Size == 0 || tempItemHash.Size == -1) + && ((tempItemHash.CRC == Constants.CRCZero || tempItemHash.CRC == "") + || tempItemHash.MD5 == Constants.MD5Zero + || tempItemHash.SHA1 == Constants.SHA1Zero)) + { + tempItemHash.Size = Constants.SizeZero; + tempItemHash.CRC = Constants.CRCZero; + tempItemHash.MD5 = Constants.MD5Zero; + tempItemHash.SHA1 = Constants.SHA1Zero; + } + // If the file has no size and it's not the above case, skip and log + else if (itemRom.Type == ItemType.Rom && (tempItemHash.Size == 0 || tempItemHash.Size == -1)) + { + logger.Warning("Incomplete entry for \"" + itemRom.Name + "\" will be output as nodump"); + itemRom.Nodump = true; + } + + itemRom.HashData = tempItemHash; + item = itemRom; } - // If the file has no size and it's not the above case, skip and log - else if (rom.Type == ItemType.Rom && (rom.HashData.Size == 0 || rom.HashData.Size == -1)) + else if (item.Type == ItemType.Disk) { - logger.Warning("Incomplete entry for \"" + rom.Name + "\" will be output as nodump"); - rom.Nodump = true; + Disk itemDisk = (Disk)item; + + // Sanitize the hashes from null, hex sizes, and "true blank" strings + Hash tempItemHash = itemDisk.HashData; + tempItemHash.MD5 = Style.CleanHashData(itemDisk.HashData.MD5, Constants.MD5Length); + tempItemHash.SHA1 = Style.CleanHashData(itemDisk.HashData.SHA1, Constants.SHA1Length); + + itemDisk.HashData = tempItemHash; + item = itemDisk; } // If the rom passes the filter, include it - if (RomTools.Filter(rom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger)) + if (DatItem.Filter(item, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger)) { // If we are in single game mode, rename all games if (single) { - rom.Machine.Name = "!"; + item.MachineName = "!"; } // If we are in NTFS trim mode, trim the game name if (trim) { // Windows max name length is 260 - int usableLength = 260 - rom.Machine.Name.Length - root.Length; - if (rom.Name.Length > usableLength) + int usableLength = 260 - item.MachineName.Length - root.Length; + if (item.Name.Length > usableLength) { - string ext = Path.GetExtension(rom.Name); - rom.Name = rom.Name.Substring(0, usableLength - ext.Length); - rom.Name += ext; + string ext = Path.GetExtension(item.Name); + item.Name = item.Name.Substring(0, usableLength - ext.Length); + item.Name += ext; } } // If we have a disk, make sure that the value for size is -1 - if (rom.Type == ItemType.Disk) + if (item.Type == ItemType.Disk) { - logger.Log("Disk found: \"" + rom.Name + "\""); - rom.HashData.Size = -1; + logger.Log("Disk found: \"" + item.Name + "\""); + Hash tempDiskHash = ((Disk)item).HashData; + tempDiskHash.Size = -1; + ((Disk)item).HashData = tempDiskHash; } lock (datdata.Files) { - key = rom.HashData.Size + "-" + rom.HashData.CRC; + // Get the key and add statistical data + switch (item.Type) + { + case ItemType.Archive: + case ItemType.BiosSet: + case ItemType.Release: + case ItemType.Sample: + key = item.Type.ToString(); + break; + case ItemType.Disk: + key = ((Disk)item).HashData.Size + "-" + ((Disk)item).HashData.CRC; + + // Add statistical data + datdata.DiskCount += 1; + datdata.TotalSize += (((Disk)item).Nodump ? 0 : ((Disk)item).HashData.Size); + datdata.MD5Count += (String.IsNullOrEmpty(((Disk)item).HashData.MD5) ? 0 : 1); + datdata.SHA1Count += (String.IsNullOrEmpty(((Disk)item).HashData.SHA1) ? 0 : 1); + datdata.NodumpCount += (((Disk)item).Nodump ? 1 : 0); + break; + case ItemType.Rom: + key = ((Rom)item).HashData.Size + "-" + ((Rom)item).HashData.CRC; + + // Add statistical data + datdata.RomCount += 1; + datdata.TotalSize += (((Rom)item).Nodump ? 0 : ((Rom)item).HashData.Size); + datdata.CRCCount += (String.IsNullOrEmpty(((Rom)item).HashData.CRC) ? 0 : 1); + datdata.MD5Count += (String.IsNullOrEmpty(((Rom)item).HashData.MD5) ? 0 : 1); + datdata.SHA1Count += (String.IsNullOrEmpty(((Rom)item).HashData.SHA1) ? 0 : 1); + datdata.NodumpCount += (((Rom)item).Nodump ? 1 : 0); + break; + default: + key = "default"; + break; + } + + // Add the item to the DAT if (datdata.Files.ContainsKey(key)) { - datdata.Files[key].Add(rom); + datdata.Files[key].Add(item); } else { - List newvalue = new List(); - newvalue.Add(rom); + List newvalue = new List(); + newvalue.Add(item); datdata.Files.Add(key, newvalue); } - - // Add statistical data - datdata.RomCount += (rom.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (rom.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (rom.Nodump ? 0 : rom.HashData.Size); - datdata.CRCCount += (String.IsNullOrEmpty(rom.HashData.CRC) ? 0 : 1); - datdata.MD5Count += (String.IsNullOrEmpty(rom.HashData.MD5) ? 0 : 1); - datdata.SHA1Count += (String.IsNullOrEmpty(rom.HashData.SHA1) ? 0 : 1); - datdata.NodumpCount += (rom.Nodump ? 1 : 0); } } } @@ -1707,9 +1730,9 @@ namespace SabreTools.Helper /// Logger object for file and console output /// True if the number of hashes counted is to be output (default), false otherwise /// SortedDictionary bucketed by game name - public static SortedDictionary> BucketByGame(List list, bool mergeroms, bool norename, Logger logger, bool output = true) + public static SortedDictionary> BucketByGame(List list, bool mergeroms, bool norename, Logger logger, bool output = true) { - Dictionary> dict = new Dictionary>(); + Dictionary> dict = new Dictionary>(); dict.Add("key", list); return BucketByGame(dict, mergeroms, norename, logger, output); } @@ -1723,11 +1746,11 @@ namespace SabreTools.Helper /// Logger object for file and console output /// True if the number of hashes counted is to be output (default), false otherwise /// SortedDictionary bucketed by game name - public static SortedDictionary> BucketByGame(IDictionary> dict, bool mergeroms, bool norename, Logger logger, bool output = true) + public static SortedDictionary> BucketByGame(IDictionary> dict, bool mergeroms, bool norename, Logger logger, bool output = true) { logger.User("Organizing " + (mergeroms ? "and merging " : "") + "roms for output"); - SortedDictionary> sortable = new SortedDictionary>(); + SortedDictionary> sortable = new SortedDictionary>(); long count = 0; // If we have a null dict or an empty one, output a new dictionary @@ -1739,29 +1762,29 @@ namespace SabreTools.Helper // Process each all of the roms foreach (string key in dict.Keys) { - List roms = dict[key]; + List roms = dict[key]; if (mergeroms) { - roms = RomTools.Merge(roms, logger); + roms = DatItem.Merge(roms, logger); } foreach (Rom rom in roms) { count++; string newkey = (norename ? "" - : rom.Metadata.SystemID.ToString().PadLeft(10, '0') + : rom.SystemID.ToString().PadLeft(10, '0') + "-" - + rom.Metadata.SourceID.ToString().PadLeft(10, '0') + "-") - + (String.IsNullOrEmpty(rom.Machine.Name) + + rom.SourceID.ToString().PadLeft(10, '0') + "-") + + (String.IsNullOrEmpty(rom.MachineName) ? "Default" - : rom.Machine.Name.ToLowerInvariant()); + : rom.MachineName.ToLowerInvariant()); if (sortable.ContainsKey(newkey)) { sortable[newkey].Add(rom); } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); sortable.Add(newkey, temp); } @@ -1772,8 +1795,8 @@ namespace SabreTools.Helper List keys = sortable.Keys.ToList(); foreach (string key in keys) { - List sortedlist = sortable[key]; - RomTools.Sort(ref sortedlist, norename); + List sortedlist = sortable[key]; + DatItem.Sort(ref sortedlist, norename); sortable[key] = sortedlist; } @@ -1786,86 +1809,6 @@ namespace SabreTools.Helper return sortable; } - /// - /// Take an arbitrarily ordered List and return a Dictionary sorted by size and hash - /// - /// Input unsorted list - /// True if roms should be deduped, false otherwise - /// True if games should only be compared on game and file name, false if system and source are counted - /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - /// SortedDictionary bucketed by size and hash - public static SortedDictionary> BucketByHashSize(List list, bool mergeroms, bool norename, Logger logger, bool output = true) - { - Dictionary> dict = new Dictionary>(); - dict.Add("key", list); - return BucketByHashSize(dict, mergeroms, norename, logger, output); - } - - /// - /// Take an arbitrarily bucketed Dictionary and return one sorted by size and hash - /// - /// Input unsorted dictionary - /// True if roms should be deduped, false otherwise - /// True if games should only be compared on game and file name, false if system and source are counted - /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - /// SortedDictionary bucketed by size and hash - public static SortedDictionary> BucketByHashSize(IDictionary> dict, bool mergeroms, bool norename, Logger logger, bool output = true) - { - SortedDictionary> sortable = new SortedDictionary>(); - long count = 0; - - // If we have a null dict or an empty one, output a new dictionary - if (dict == null || dict.Count == 0) - { - return sortable; - } - - // Process each all of the roms - foreach (List roms in dict.Values) - { - List newroms = roms; - if (mergeroms) - { - newroms = RomTools.Merge(newroms, logger); - } - - foreach (Rom rom in newroms) - { - count++; - string key = rom.HashData.Size + "-" + rom.HashData.CRC; - if (sortable.ContainsKey(key)) - { - sortable[key].Add(rom); - } - else - { - List temp = new List(); - temp.Add(rom); - sortable.Add(key, temp); - } - } - } - - // Now go through and sort all of the lists - List keys = sortable.Keys.ToList(); - foreach (string key in keys) - { - List sortedlist = sortable[key]; - RomTools.Sort(ref sortedlist, norename); - sortable[key] = sortedlist; - } - - // Output the count if told to - if (output) - { - logger.User("A total of " + count + " file hashes will be written out to file"); - } - - return sortable; - } - #endregion #region Converting and Updating @@ -2070,7 +2013,7 @@ namespace SabreTools.Helper datHeaders[i] = new Dat { OutputFormat = (inputDat.OutputFormat != 0 ? inputDat.OutputFormat: 0), - Files = new Dictionary>(), + Files = new Dictionary>(), MergeRoms = inputDat.MergeRoms, }; @@ -2082,7 +2025,7 @@ namespace SabreTools.Helper logger.User("Populating internal DAT"); userData = (Dat)inputDat.CloneHeader(); - userData.Files = new Dictionary>(); + userData.Files = new Dictionary>(); for (int i = 0; i < inputs.Count; i++) { List keys = datHeaders[i].Files.Keys.ToList(); @@ -2132,7 +2075,7 @@ namespace SabreTools.Helper outerDiffData.FileName += post; outerDiffData.Name += post; outerDiffData.Description += post; - outerDiffData.Files = new Dictionary>(); + outerDiffData.Files = new Dictionary>(); } // Have External dupes @@ -2143,7 +2086,7 @@ namespace SabreTools.Helper dupeData.FileName += post; dupeData.Name += post; dupeData.Description += post; - dupeData.Files = new Dictionary>(); + dupeData.Files = new Dictionary>(); } // Create a list of DatData objects representing individual output files @@ -2161,7 +2104,7 @@ namespace SabreTools.Helper diffData.FileName += innerpost; diffData.Name += innerpost; diffData.Description += innerpost; - diffData.Files = new Dictionary>(); + diffData.Files = new Dictionary>(); outDatsArray[j] = diffData; }); @@ -2175,7 +2118,7 @@ namespace SabreTools.Helper List keys = userData.Files.Keys.ToList(); foreach (string key in keys) { - List roms = RomTools.Merge(userData.Files[key], logger); + List roms = DatItem.Merge(userData.Files[key], logger); if (roms != null && roms.Count > 0) { @@ -2189,15 +2132,15 @@ namespace SabreTools.Helper // Individual DATs that are output if ((diff & DiffMode.Individuals) != 0) { - if (outDats[rom.Metadata.SystemID].Files.ContainsKey(key)) + if (outDats[rom.SystemID].Files.ContainsKey(key)) { - outDats[rom.Metadata.SystemID].Files[key].Add(rom); + outDats[rom.SystemID].Files[key].Add(rom); } else { - List tl = new List(); + List tl = new List(); tl.Add(rom); - outDats[rom.Metadata.SystemID].Files.Add(key, tl); + outDats[rom.SystemID].Files.Add(key, tl); } } @@ -2205,7 +2148,7 @@ namespace SabreTools.Helper if ((diff & DiffMode.NoDupes) != 0) { Rom newrom = rom; - newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.Metadata.SystemID].Split('¬')[0]) + ")"; + newrom.MachineName += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")"; if (outerDiffData.Files.ContainsKey(key)) { @@ -2213,7 +2156,7 @@ namespace SabreTools.Helper } else { - List tl = new List(); + List tl = new List(); tl.Add(rom); outerDiffData.Files.Add(key, tl); } @@ -2227,7 +2170,7 @@ namespace SabreTools.Helper if (rom.Dupe >= DupeType.ExternalHash) { Rom newrom = rom; - newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.Metadata.SystemID].Split('¬')[0]) + ")"; + newrom.MachineName += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")"; if (dupeData.Files.ContainsKey(key)) { @@ -2235,7 +2178,7 @@ namespace SabreTools.Helper } else { - List tl = new List(); + List tl = new List(); tl.Add(rom); dupeData.Files.Add(key, tl); } @@ -2320,7 +2263,7 @@ namespace SabreTools.Helper diffData.Name += post; diffData.Description += post; } - diffData.Files = new Dictionary>(); + diffData.Files = new Dictionary>(); outDatsArray[j] = diffData; }); @@ -2335,21 +2278,21 @@ namespace SabreTools.Helper foreach (string key in keys) { - List roms = RomTools.Merge(userData.Files[key], logger); + List< DatItem> roms = DatItem.Merge(userData.Files[key], logger); if (roms != null && roms.Count > 0) { foreach (Rom rom in roms) { - if (outDats[rom.Metadata.SystemID].Files.ContainsKey(key)) + if (outDats[rom.SystemID].Files.ContainsKey(key)) { - outDats[rom.Metadata.SystemID].Files[key].Add(rom); + outDats[rom.SystemID].Files[key].Add(rom); } else { - List tl = new List(); + List tl = new List(); tl.Add(rom); - outDats[rom.Metadata.SystemID].Files.Add(key, tl); + outDats[rom.SystemID].Files.Add(key, tl); } } } @@ -2397,18 +2340,18 @@ namespace SabreTools.Helper List keys = userData.Files.Keys.ToList(); foreach (string key in keys) { - List newroms = new List(); - foreach (Rom rom in userData.Files[key]) + List newroms = new List(); + foreach (DatItem rom in userData.Files[key]) { - Rom newrom = rom; - string filename = inputs[newrom.Metadata.SystemID].Split('¬')[0]; - string rootpath = inputs[newrom.Metadata.SystemID].Split('¬')[1]; + DatItem newrom = rom; + string filename = inputs[newrom.SystemID].Split('¬')[0]; + string rootpath = inputs[newrom.SystemID].Split('¬')[1]; rootpath += (rootpath == "" ? "" : Path.DirectorySeparatorChar.ToString()); filename = filename.Remove(0, rootpath.Length); - newrom.Machine.Name = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar + newrom.MachineName = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar - + newrom.Machine.Name; + + newrom.MachineName; newroms.Add(newrom); } userData.Files[key] = newroms; @@ -2508,7 +2451,7 @@ namespace SabreTools.Helper } // Bucket roms by game name and optionally dedupe - SortedDictionary> sortable = BucketByGame(datdata.Files, datdata.MergeRoms, norename, logger); + SortedDictionary> sortable = BucketByGame(datdata.Files, datdata.MergeRoms, norename, logger); // Get the outfile name Dictionary outfiles = Style.CreateOutfileNames(outDir, datdata, overwrite); @@ -2537,53 +2480,59 @@ namespace SabreTools.Helper foreach (string key in keys) { - List roms = sortable[key]; + List roms = sortable[key]; for (int index = 0; index < roms.Count; index++) { - Rom rom = roms[index]; + DatItem rom = roms[index]; // There are apparently times when a null rom can skip by, skip them - if (rom.Name == null || rom.Machine.Name == null) + if (rom.Name == null || rom.MachineName == null) { logger.Warning("Null rom found!"); continue; } - List newsplit = rom.Machine.Name.Split('\\').ToList(); + List newsplit = rom.MachineName.Split('\\').ToList(); // If we have a different game and we're not at the start of the list, output the end of last item - if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) + if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) { depth = WriteEndGame(sw, outputFormat, rom, splitpath, newsplit, lastgame, depth, out last, logger); } // If we have a new game, output the beginning of the new item - if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) + if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) { depth = WriteStartGame(sw, outputFormat, rom, newsplit, lastgame, depth, last, logger); } // If we have a "null" game (created by DATFromDir or something similar), log it to file - if (rom.HashData.Size == -1 && rom.HashData.CRC == "null" && rom.HashData.MD5 == "null" && rom.HashData.SHA1 == "null") + if (rom.Type == ItemType.Rom + && ((Rom)rom).HashData.Size == -1 + && ((Rom)rom).HashData.CRC == "null" + && ((Rom)rom).HashData.MD5 == "null" + && ((Rom)rom).HashData.SHA1 == "null") { - logger.Log("Empty folder found: " + rom.Machine.Name); + logger.Log("Empty folder found: " + rom.MachineName); // If we're in a mode that doesn't allow for actual empty folders, add the blank info if (outputFormat != OutputFormat.SabreDat && outputFormat != OutputFormat.MissFile) { rom.Name = (rom.Name == "null" ? "-" : rom.Name); - rom.HashData.Size = Constants.SizeZero; - rom.HashData.CRC = Constants.CRCZero; - rom.HashData.MD5 = Constants.MD5Zero; - rom.HashData.SHA1 = Constants.SHA1Zero; + Hash tempHash = ((Rom)rom).HashData; + tempHash.Size = Constants.SizeZero; + tempHash.CRC = Constants.CRCZero; + tempHash.MD5 = Constants.MD5Zero; + tempHash.SHA1 = Constants.SHA1Zero; + ((Rom)rom).HashData = tempHash; } // Otherwise, set the new path and such, write out, and continue else { splitpath = newsplit; - lastgame = rom.Machine.Name; + lastgame = rom.MachineName; continue; } } @@ -2593,7 +2542,7 @@ namespace SabreTools.Helper // Set the new data to compare against splitpath = newsplit; - lastgame = rom.Machine.Name; + lastgame = rom.MachineName; } } @@ -2758,26 +2707,26 @@ namespace SabreTools.Helper /// Last known depth to cycle back from (SabreDAT only) /// Logger object for file and console output /// The new depth of the tag - public static int WriteStartGame(StreamWriter sw, OutputFormat outputFormat, Rom rom, List newsplit, string lastgame, int depth, int last, Logger logger) + public static int WriteStartGame(StreamWriter sw, OutputFormat outputFormat, DatItem rom, List newsplit, string lastgame, int depth, int last, Logger logger) { try { // No game should start with a path separator - if (rom.Machine.Name.StartsWith(Path.DirectorySeparatorChar.ToString())) + if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString())) { - rom.Machine.Name = rom.Machine.Name.Substring(1); + rom.MachineName = rom.MachineName.Substring(1); } string state = ""; switch (outputFormat) { case OutputFormat.ClrMamePro: - state += "game (\n\tname \"" + rom.Machine.Name + "\"\n" + - (String.IsNullOrEmpty(rom.Machine.RomOf) ? "" : "\tromof \"" + rom.Machine.RomOf + "\"\n") + - (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : "\tcloneof \"" + rom.Machine.CloneOf + "\"\n") + - "\tdescription \"" + (String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description) + "\"\n" + - (String.IsNullOrEmpty(rom.Machine.Year) ? "" : "\tyear " + rom.Machine.Year + "\n") + - (String.IsNullOrEmpty(rom.Machine.Manufacturer) ? "" : "\tmanufacturer \"" + rom.Machine.Manufacturer + "\"\n"); + state += "game (\n\tname \"" + rom.MachineName + "\"\n" + + (String.IsNullOrEmpty(rom.RomOf) ? "" : "\tromof \"" + rom.RomOf + "\"\n") + + (String.IsNullOrEmpty(rom.CloneOf) ? "" : "\tcloneof \"" + rom.CloneOf + "\"\n") + + "\tdescription \"" + (String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription) + "\"\n" + + (String.IsNullOrEmpty(rom.Year) ? "" : "\tyear " + rom.Year + "\n") + + (String.IsNullOrEmpty(rom.Manufacturer) ? "" : "\tmanufacturer \"" + rom.Manufacturer + "\"\n"); break; case OutputFormat.SabreDat: @@ -2793,16 +2742,16 @@ namespace SabreTools.Helper depth = depth - (last == -1 ? 0 : last) + newsplit.Count; break; case OutputFormat.Xml: - state += "\t\n" + - (String.IsNullOrEmpty(rom.Machine.Comment) ? "" : "\t\t" + HttpUtility.HtmlEncode(rom.Machine.Comment) + "\n") + - "\t\t" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description)) + "\n" + - (String.IsNullOrEmpty(rom.Machine.Year) ? "" : "\t\t" + HttpUtility.HtmlEncode(rom.Machine.Year) + "\n") + - (String.IsNullOrEmpty(rom.Machine.Manufacturer) ? "" : "\t\t" + HttpUtility.HtmlEncode(rom.Machine.Manufacturer) + "\n"); + (String.IsNullOrEmpty(rom.Comment) ? "" : "\t\t" + HttpUtility.HtmlEncode(rom.Comment) + "\n") + + "\t\t" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) + "\n" + + (String.IsNullOrEmpty(rom.Year) ? "" : "\t\t" + HttpUtility.HtmlEncode(rom.Year) + "\n") + + (String.IsNullOrEmpty(rom.Manufacturer) ? "" : "\t\t" + HttpUtility.HtmlEncode(rom.Manufacturer) + "\n"); break; } @@ -2831,7 +2780,7 @@ namespace SabreTools.Helper /// Last known depth to cycle back from (SabreDAT only) /// Logger object for file and console output /// The new depth of the tag - public static int WriteEndGame(StreamWriter sw, OutputFormat outputFormat, Rom rom, List splitpath, List newsplit, string lastgame, int depth, out int last, Logger logger) + public static int WriteEndGame(StreamWriter sw, OutputFormat outputFormat, DatItem rom, List splitpath, List newsplit, string lastgame, int depth, out int last, Logger logger) { last = 0; @@ -2842,7 +2791,7 @@ namespace SabreTools.Helper switch (outputFormat) { case OutputFormat.ClrMamePro: - state += (String.IsNullOrEmpty(rom.Machine.SampleOf) ? "" : "\tsampleof \"" + rom.Machine.SampleOf + "\"\n") + ")\n"; + state += (String.IsNullOrEmpty(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n") + ")\n"; break; case OutputFormat.SabreDat: if (splitpath != null) @@ -2903,10 +2852,12 @@ namespace SabreTools.Helper /// Logger object for file and console output /// True if blank roms should be skipped on output, false otherwise (default) /// True if the data was written, false on error - public static bool WriteRomData(StreamWriter sw, OutputFormat outputFormat, Rom rom, string lastgame, Dat datdata, int depth, Logger logger, bool ignoreblanks = false) + public static bool WriteRomData(StreamWriter sw, OutputFormat outputFormat, DatItem rom, string lastgame, Dat datdata, int depth, Logger logger, bool ignoreblanks = false) { // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip - if (ignoreblanks && (rom.HashData.Size == 0 || rom.HashData.Size == -1)) + if (ignoreblanks + && (rom.Type == ItemType.Rom + && (((Rom)rom).HashData.Size == 0 || ((Rom)rom).HashData.Size == -1))) { return true; } @@ -2917,66 +2868,171 @@ namespace SabreTools.Helper switch (outputFormat) { case OutputFormat.ClrMamePro: - state += "\t" + rom.Type.ToString().ToLowerInvariant() + " ( name \"" + rom.Name + "\"" + - (rom.HashData.Size != -1 ? " size " + rom.HashData.Size : "") + - (!String.IsNullOrEmpty(rom.HashData.CRC) ? " crc " + rom.HashData.CRC.ToLowerInvariant() : "") + - (!String.IsNullOrEmpty(rom.HashData.MD5) ? " md5 " + rom.HashData.MD5.ToLowerInvariant() : "") + - (!String.IsNullOrEmpty(rom.HashData.SHA1) ? " sha1 " + rom.HashData.SHA1.ToLowerInvariant() : "") + - (!String.IsNullOrEmpty(rom.Date) ? " date \"" + rom.Date + "\"" : "") + - (rom.Nodump ? " flags nodump" : "") + - " )\n"; + switch (rom.Type) + { + case ItemType.Archive: + state += "\tarchive ( name\"" + rom.Name + "\"" + + " )\n"; + break; + case ItemType.BiosSet: + state += "\tbiosset ( name\"" + rom.Name + "\"" + + (!String.IsNullOrEmpty(((BiosSet)rom).Description) ? " description \"" + ((BiosSet)rom).Description + "\"" : "") + + (((BiosSet)rom).Default != null + ? "default " + ((BiosSet)rom).Default.ToString().ToLowerInvariant() + : "") + + " )\n"; + break; + case ItemType.Disk: + state += "\tdisk ( name \"" + rom.Name + "\"" + + (!String.IsNullOrEmpty(((Disk)rom).HashData.MD5) ? " md5 " + ((Disk)rom).HashData.MD5.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Disk)rom).HashData.SHA1) ? " sha1 " + ((Disk)rom).HashData.SHA1.ToLowerInvariant() : "") + + (((Disk)rom).Nodump ? " flags nodump" : "") + + " )\n"; + break; + case ItemType.Release: + state += "\trelease ( name\"" + rom.Name + "\"" + + (!String.IsNullOrEmpty(((Release)rom).Region) ? " region \"" + ((Release)rom).Region + "\"" : "") + + (!String.IsNullOrEmpty(((Release)rom).Language) ? " language \"" + ((Release)rom).Language + "\"" : "") + + (!String.IsNullOrEmpty(((Release)rom).Date) ? " date \"" + ((Release)rom).Date + "\"" : "") + + (((Release)rom).Default != null + ? "default " + ((Release)rom).Default.ToString().ToLowerInvariant() + : "") + + " )\n"; + break; + case ItemType.Rom: + state += "\trom ( name \"" + rom.Name + "\"" + + (((Rom)rom).HashData.Size != -1 ? " size " + ((Rom)rom).HashData.Size : "") + + (!String.IsNullOrEmpty(((Rom)rom).HashData.CRC) ? " crc " + ((Rom)rom).HashData.CRC.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Rom)rom).HashData.MD5) ? " md5 " + ((Rom)rom).HashData.MD5.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Rom)rom).HashData.SHA1) ? " sha1 " + ((Rom)rom).HashData.SHA1.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Rom)rom).Date) ? " date \"" + ((Rom)rom).Date + "\"" : "") + + (((Rom)rom).Nodump ? " flags nodump" : "") + + " )\n"; + break; + case ItemType.Sample: + state += "\tsample ( name\"" + rom.Name + "\"" + + " )\n"; + break; + } + break; case OutputFormat.MissFile: + // Missfile should only output Rom and Disk + if (rom.Type != ItemType.Disk && rom.Type != ItemType.Disk) + { + return true; + } + string pre = datdata.Prefix + (datdata.Quotes ? "\"" : ""); string post = (datdata.Quotes ? "\"" : "") + datdata.Postfix; - // Check for special strings in prefix and postfix - pre = pre - .Replace("%game%", rom.Machine.Name) - .Replace("%name%", rom.Name) - .Replace("%crc%", rom.HashData.CRC) - .Replace("%md5%", rom.HashData.MD5) - .Replace("%sha1%", rom.HashData.SHA1) - .Replace("%size%", rom.HashData.Size.ToString()); - post = post - .Replace("%game%", rom.Machine.Name) - .Replace("%name%", rom.Name) - .Replace("%crc%", rom.HashData.CRC) - .Replace("%md5%", rom.HashData.MD5) - .Replace("%sha1%", rom.HashData.SHA1) - .Replace("%size%", rom.HashData.Size.ToString()); + if (rom.Type == ItemType.Rom) + { + // Check for special strings in prefix and postfix + pre = pre + .Replace("%game%", rom.MachineName) + .Replace("%name%", rom.Name) + .Replace("%crc%", ((Rom)rom).HashData.CRC) + .Replace("%md5%", ((Rom)rom).HashData.MD5) + .Replace("%sha1%", ((Rom)rom).HashData.SHA1) + .Replace("%size%", ((Rom)rom).HashData.Size.ToString()); + post = post + .Replace("%game%", rom.MachineName) + .Replace("%name%", rom.Name) + .Replace("%crc%", ((Rom)rom).HashData.CRC) + .Replace("%md5%", ((Rom)rom).HashData.MD5) + .Replace("%sha1%", ((Rom)rom).HashData.SHA1) + .Replace("%size%", ((Rom)rom).HashData.Size.ToString()); + } + else if (rom.Type == ItemType.Disk) + { + // Check for special strings in prefix and postfix + pre = pre + .Replace("%game%", rom.MachineName) + .Replace("%name%", rom.Name) + .Replace("%md5%", ((Disk)rom).HashData.MD5) + .Replace("%sha1%", ((Disk)rom).HashData.SHA1); + post = post + .Replace("%game%", rom.MachineName) + .Replace("%name%", rom.Name) + .Replace("%md5%", ((Disk)rom).HashData.MD5) + .Replace("%sha1%", ((Disk)rom).HashData.SHA1); + } // If we're in Romba mode, the state is consistent if (datdata.Romba) { - // We can only write out if there's a SHA-1 - if (rom.HashData.SHA1 != "") + if (rom.Type == ItemType.Rom) { - string name = rom.HashData.SHA1.Substring(0, 2) + "/" + rom.HashData.SHA1.Substring(2, 2) + "/" + rom.HashData.SHA1.Substring(4, 2) + "/" + - rom.HashData.SHA1.Substring(6, 2) + "/" + rom.HashData.SHA1 + ".gz"; - state += pre + name + post + "\n"; + // We can only write out if there's a SHA-1 + if (((Rom)rom).HashData.SHA1 != "") + { + string name = ((Rom)rom).HashData.SHA1.Substring(0, 2) + + "/" + ((Rom)rom).HashData.SHA1.Substring(2, 2) + + "/" + ((Rom)rom).HashData.SHA1.Substring(4, 2) + + "/" + ((Rom)rom).HashData.SHA1.Substring(6, 2) + + "/" + ((Rom)rom).HashData.SHA1 + ".gz"; + state += pre + name + post + "\n"; + } + } + else if (rom.Type == ItemType.Disk) + { + // We can only write out if there's a SHA-1 + if (((Disk)rom).HashData.SHA1 != "") + { + string name = ((Disk)rom).HashData.SHA1.Substring(0, 2) + + "/" + ((Disk)rom).HashData.SHA1.Substring(2, 2) + + "/" + ((Disk)rom).HashData.SHA1.Substring(4, 2) + + "/" + ((Disk)rom).HashData.SHA1.Substring(6, 2) + + "/" + ((Disk)rom).HashData.SHA1 + ".gz"; + state += pre + name + post + "\n"; + } } } - // If we're in TSV mode, similarly the state is consistent - else if (datdata.XSV == true) + // If we're in TSV/CSV mode, similarly the state is consistent + else if (datdata.XSV != null) { - string inline = "\"" + datdata.FileName + "\"\t\"" + datdata.Name + "\"\t\"" + datdata.Description + "\"\t\"" + rom.Machine + "\"\t\"" + rom.Machine + "\"\t\"" + - rom.Type.ToString().ToLowerInvariant() + "\"\t\"" + (rom.Type == ItemType.Rom ? rom.Name : "") + "\"\t\"" + (rom.Type == ItemType.Disk ? rom.Name : "") + "\"\t\"" + rom.HashData.Size + "\"\t\"" + - rom.HashData.CRC + "\"\t\"" + rom.HashData.MD5 + "\"\t\"" + rom.HashData.SHA1 + "\"\t" + (rom.Nodump ? "\"Nodump\"" : "\"\""); - state += pre + inline + post + "\n"; - } - // If we're in CSV mode, similarly the state is consistent - else if (datdata.XSV == false) - { - string inline = "\"" + datdata.FileName + "\",\"" + datdata.Name + "\",\"" + datdata.Description + "\",\"" + rom.Machine + "\",\"" + rom.Machine + "\",\"" + - rom.Type.ToString().ToLowerInvariant() + "\",\"" + (rom.Type == ItemType.Rom ? rom.Name : "") + "\",\"" + (rom.Type == ItemType.Disk ? rom.Name : "") + "\",\"" + rom.HashData.Size + "\",\"" + - rom.HashData.CRC + "\",\"" + rom.HashData.MD5 + "\",\"" + rom.HashData.SHA1 + "\"," + (rom.Nodump ? "\"Nodump\"" : "\"\""); - state += pre + inline + post + "\n"; + string separator = (datdata.XSV == true ? "\t" : ","); + + if (rom.Type == ItemType.Rom) + { + string inline = "\"" + datdata.FileName + "\"" + + separator + "\"" + datdata.Name + "\"" + + separator + "\"" + datdata.Description+ "\"" + + separator + "\"" + rom.MachineName + "\"" + + separator + "\"" + rom.MachineDescription + "\"" + + separator + "\"rom\"" + + separator + "\"" + rom.Name + "\"" + + separator + "\"\"" + + separator + "\"" + ((Rom)rom).HashData.Size + "\"" + + separator + "\"" + ((Rom)rom).HashData.CRC + "\"" + + separator + "\"" + ((Rom)rom).HashData.MD5 + "\"" + + separator + "\"" + ((Rom)rom).HashData.SHA1 + "\"" + + separator + (((Rom)rom).Nodump ? "\"Nodump\"" : "\"\""); + state += pre + inline + post + "\n"; + } + else if (rom.Type == ItemType.Disk) + { + string inline = "\"" + datdata.FileName + "\"" + + separator + "\"" + datdata.Name + "\"" + + separator + "\"" + datdata.Description + "\"" + + separator + "\"" + rom.MachineName + "\"" + + separator + "\"" + rom.MachineDescription + "\"" + + separator + "\"disk\"" + + separator + "\"\"" + + separator + "\"" + rom.Name + "\"" + + separator + "\"\"" + + separator + "\"\"" + + separator + "\"" + ((Disk)rom).HashData.MD5 + "\"" + + separator + "\"" + ((Disk)rom).HashData.SHA1 + "\"" + + separator + (((Disk)rom).Nodump ? "\"Nodump\"" : "\"\""); + state += pre + inline + post + "\n"; + } } // Otherwise, use any flags else { - string name = (datdata.UseGame ? rom.Machine.Name : rom.Name); + string name = (datdata.UseGame ? rom.MachineName : rom.Name); if (datdata.RepExt != "" || datdata.RemExt) { if (datdata.RemExt) @@ -2994,13 +3050,13 @@ namespace SabreTools.Helper } if (!datdata.UseGame && datdata.GameName) { - name = Path.Combine(rom.Machine.Name, name); + name = Path.Combine(rom.MachineName, name); } - if (datdata.UseGame && rom.Machine.Name != lastgame) + if (datdata.UseGame && rom.MachineName != lastgame) { state += pre + name + post + "\n"; - lastgame = rom.Machine.Name; + lastgame = rom.MachineName; } else if (!datdata.UseGame) { @@ -3009,22 +3065,52 @@ namespace SabreTools.Helper } break; case OutputFormat.RedumpMD5: - state += rom.HashData.MD5 + " *" + rom.Name + "\n"; + if (rom.Type == ItemType.Rom) + { + state += ((Rom)rom).HashData.MD5 + " *" + rom.Name + "\n"; + } + else if (rom.Type == ItemType.Disk) + { + state += ((Disk)rom).HashData.MD5 + " *" + rom.Name + "\n"; + } break; case OutputFormat.RedumpSFV: - state += rom.Name + " " + rom.HashData.CRC + "\n"; + if (rom.Type == ItemType.Rom) + { + state += rom.Name + " " + ((Rom)rom).HashData.CRC + "\n"; + } break; case OutputFormat.RedumpSHA1: - state += rom.HashData.SHA1 + " *" + rom.Name + "\n"; + if (rom.Type == ItemType.Rom) + { + state += ((Rom)rom).HashData.SHA1 + " *" + rom.Name + "\n"; + } + else if (rom.Type == ItemType.Disk) + { + state += ((Disk)rom).HashData.SHA1 + " *" + rom.Name + "\n"; + } break; case OutputFormat.RomCenter: - state += "¬" + (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.Machine.CloneOf)) + - "¬" + (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.Machine.CloneOf)) + - "¬" + HttpUtility.HtmlEncode(rom.Machine.Name) + - "¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description)) + + if (rom.Type == ItemType.Rom) + { + state += "¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) + + "¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) + + "¬" + HttpUtility.HtmlEncode(rom.MachineName) + + "¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) + "¬" + HttpUtility.HtmlEncode(rom.Name) + - "¬" + rom.HashData.CRC.ToLowerInvariant() + - "¬" + (rom.HashData.Size != -1 ? rom.HashData.Size.ToString() : "") + "¬¬¬\n"; + "¬" + ((Rom)rom).HashData.CRC.ToLowerInvariant() + + "¬" + (((Rom)rom).HashData.Size != -1 ? ((Rom)rom).HashData.Size.ToString() : "") + "¬¬¬\n"; + } + else if (rom.Type == ItemType.Disk) + { + state += "¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) + + "¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) + + "¬" + HttpUtility.HtmlEncode(rom.MachineName) + + "¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) + + "¬" + HttpUtility.HtmlEncode(rom.Name) + + "¬¬¬¬¬\n"; + } + break; case OutputFormat.SabreDat: string prefix = ""; @@ -3032,29 +3118,106 @@ namespace SabreTools.Helper { prefix += "\t"; } - state += prefix; - state += "\n" + prefix + "\t\n" + - prefix + "\t\t\n" + - prefix + "\t\n" + - prefix + "\n" : - "/>\n"); + + switch (rom.Type) + { + case ItemType.Archive: + state += "\n"; + break; + case ItemType.BiosSet: + state += "\n"; + break; + case ItemType.Disk: + state += "\n" + prefix + "\t\n" + + prefix + "\t\t\n" + + prefix + "\t\n" + + prefix + "\n" : "/>\n"); + break; + case ItemType.Release: + state += "\n"; + break; + case ItemType.Rom: + state += "\n" + prefix + "\t\n" + + prefix + "\t\t\n" + + prefix + "\t\n" + + prefix + "\n" : "/>\n"); + break; + case ItemType.Sample: + state += "\n"; + break; + } break; case OutputFormat.Xml: - state += "\t\t<" + rom.Type.ToString().ToLowerInvariant() + " name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\"" + - (rom.Type == ItemType.Rom && rom.HashData.Size != -1 ? " size=\"" + rom.HashData.Size + "\"" : "") + - (!String.IsNullOrEmpty(rom.HashData.CRC) ? " crc=\"" + rom.HashData.CRC.ToLowerInvariant() + "\"" : "") + - (!String.IsNullOrEmpty(rom.HashData.MD5) ? " md5=\"" + rom.HashData.MD5.ToLowerInvariant() + "\"" : "") + - (!String.IsNullOrEmpty(rom.HashData.SHA1) ? " sha1=\"" + rom.HashData.SHA1.ToLowerInvariant() + "\"" : "") + - (!String.IsNullOrEmpty(rom.Date) ? " date=\"" + rom.Date + "\"" : "") + - (rom.Nodump ? " status=\"nodump\"" : "") + - "/>\n"; + switch (rom.Type) + { + case ItemType.Archive: + state += "\t\t\n"; + break; + case ItemType.BiosSet: + state += "\t\t\n"; + break; + case ItemType.Disk: + state += "\t\t\n"; + break; + case ItemType.Release: + state += "\t\t\n"; + break; + case ItemType.Rom: + state += "\t\t\n"; + break; + case ItemType.Sample: + state += "\t\t\n"; + break; + } break; } @@ -3191,7 +3354,7 @@ namespace SabreTools.Helper Homepage = datdata.Homepage, Url = datdata.Url, Comment = datdata.Comment, - Files = new Dictionary>(), + Files = new Dictionary>(), OutputFormat = outputFormat, }; Dat datdataB = new Dat @@ -3207,7 +3370,7 @@ namespace SabreTools.Helper Homepage = datdata.Homepage, Url = datdata.Url, Comment = datdata.Comment, - Files = new Dictionary>(), + Files = new Dictionary>(), OutputFormat = outputFormat, }; @@ -3220,7 +3383,7 @@ namespace SabreTools.Helper // Now separate the roms accordingly foreach (string key in datdata.Files.Keys) { - foreach (Rom rom in datdata.Files[key]) + foreach (DatItem rom in datdata.Files[key]) { if (newExtA.Contains(Path.GetExtension(rom.Name.ToUpperInvariant()))) { @@ -3230,7 +3393,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); datdataA.Files.Add(key, temp); } @@ -3243,7 +3406,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); datdataB.Files.Add(key, temp); } @@ -3256,7 +3419,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); datdataA.Files.Add(key, temp); } @@ -3266,7 +3429,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); datdataB.Files.Add(key, temp); } @@ -3337,7 +3500,7 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), }; Dat sha1 = new Dat { @@ -3359,7 +3522,7 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), }; Dat md5 = new Dat { @@ -3381,7 +3544,7 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), }; Dat crc = new Dat { @@ -3403,18 +3566,48 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), + }; + + Dat other = new Dat + { + FileName = datdata.FileName + " (Other)", + Name = datdata.Name + " (Other)", + Description = datdata.Description + " (Other)", + Category = datdata.Category, + Version = datdata.Version, + Date = datdata.Date, + Author = datdata.Author, + Email = datdata.Email, + Homepage = datdata.Homepage, + Url = datdata.Url, + Comment = datdata.Comment, + Header = datdata.Header, + Type = datdata.Type, + ForceMerging = datdata.ForceMerging, + ForceNodump = datdata.ForceNodump, + ForcePacking = datdata.ForcePacking, + OutputFormat = outputFormat, + MergeRoms = datdata.MergeRoms, + Files = new Dictionary>(), }; // Now populate each of the DAT objects in turn List keys = datdata.Files.Keys.ToList(); foreach (string key in keys) { - List roms = datdata.Files[key]; - foreach (Rom rom in roms) + List roms = datdata.Files[key]; + foreach (DatItem rom in roms) { + // If the file is not a Rom or Disk, continue + if (rom.Type != ItemType.Disk && rom.Type != ItemType.Rom) + { + continue; + } + // If the file is a nodump - if (rom.Nodump) + if ((rom.Type == ItemType.Rom && ((Rom)rom).Nodump) + || (rom.Type == ItemType.Disk && ((Disk)rom).Nodump)) { if (nodump.Files.ContainsKey(key)) { @@ -3422,13 +3615,14 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); nodump.Files.Add(key, temp); } } // If the file has a SHA-1 - else if (rom.HashData.SHA1 != null && rom.HashData.SHA1 != "") + else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).HashData.SHA1)) + || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).HashData.SHA1))) { if (sha1.Files.ContainsKey(key)) { @@ -3436,13 +3630,14 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); sha1.Files.Add(key, temp); } } // If the file has no SHA-1 but has an MD5 - else if (rom.HashData.MD5 != null && rom.HashData.MD5 != "") + else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).HashData.MD5)) + || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).HashData.MD5))) { if (md5.Files.ContainsKey(key)) { @@ -3450,13 +3645,14 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); md5.Files.Add(key, temp); } } - // All other cases - else + // If the file has no MD5 but a CRC + else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).HashData.SHA1)) + || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).HashData.SHA1))) { if (crc.Files.ContainsKey(key)) { @@ -3464,11 +3660,24 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); crc.Files.Add(key, temp); } } + else + { + if (other.Files.ContainsKey(key)) + { + other.Files[key].Add(rom); + } + else + { + List temp = new List(); + temp.Add(rom); + other.Files.Add(key, temp); + } + } } } @@ -3551,7 +3760,7 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), }; Dat diskdat = new Dat { @@ -3573,7 +3782,7 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), }; Dat sampledat = new Dat { @@ -3595,15 +3804,15 @@ namespace SabreTools.Helper ForcePacking = datdata.ForcePacking, OutputFormat = outputFormat, MergeRoms = datdata.MergeRoms, - Files = new Dictionary>(), + Files = new Dictionary>(), }; // Now populate each of the DAT objects in turn List keys = datdata.Files.Keys.ToList(); foreach (string key in keys) { - List roms = datdata.Files[key]; - foreach (Rom rom in roms) + List roms = datdata.Files[key]; + foreach (DatItem rom in roms) { // If the file is a Rom if (rom.Type == ItemType.Rom) @@ -3614,7 +3823,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); romdat.Files.Add(key, temp); } @@ -3628,7 +3837,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); diskdat.Files.Add(key, temp); } @@ -3643,7 +3852,7 @@ namespace SabreTools.Helper } else { - List temp = new List(); + List temp = new List(); temp.Add(rom); sampledat.Files.Add(key, temp); } diff --git a/SabreTools.Helper/Tools/DatToolsHash.cs b/SabreTools.Helper/Tools/DatToolsHash.cs deleted file mode 100644 index dee319bc..00000000 --- a/SabreTools.Helper/Tools/DatToolsHash.cs +++ /dev/null @@ -1,1534 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; -using System.Xml; - -namespace SabreTools.Helper -{ - /// - /// DAT manipulation tools that rely on HashData and related structs - /// - /// THIS IS NOT IN SYNC WITH THE MAIN DATTOOLS CLASS, WILL NEED AN UPDATE BEFORE THAT IS POSSIBLE - public class DatToolsHash - { - #region DAT Parsing - - /// - /// Parse a DAT and return all found games and roms within - /// - /// Name of the file to be parsed - /// System ID for the DAT - /// Source ID for the DAT - /// The DatData object representing found roms to this point - /// Logger object for console and/or file output - /// True if full pathnames are to be kept, false otherwise (default) - /// True if game names are sanitized, false otherwise (default) - /// DatData object representing the read-in data - public static DatData Parse(string filename, int sysid, int srcid, DatData datdata, Logger logger, bool keep = false, bool clean = false, bool softlist = false, bool keepext = false) - { - // Check the file extension first as a safeguard - string ext = Path.GetExtension(filename).ToLowerInvariant(); - if (ext != ".txt" && ext != ".dat" && ext != ".xml") - { - return datdata; - } - - // If the output filename isn't set already, get the internal filename - datdata.FileName = (String.IsNullOrEmpty(datdata.FileName) ? (keepext ? Path.GetFileName(filename) : Path.GetFileNameWithoutExtension(filename)) : datdata.FileName); - - // If the output type isn't set already, get the internal output type - datdata.OutputFormat = (datdata.OutputFormat == 0 ? DatTools.GetOutputFormat(filename, logger) : datdata.OutputFormat); - - // Make sure there's a dictionary to read to - if (datdata.Hashes == null) - { - datdata.Hashes = new List(); - } - - // Now parse the correct type of DAT - switch (DatTools.GetOutputFormat(filename, logger)) - { - case OutputFormat.ClrMamePro: - return ParseCMP(filename, sysid, srcid, datdata, logger, keep, clean); - case OutputFormat.RomCenter: - return ParseRC(filename, sysid, srcid, datdata, logger, clean); - case OutputFormat.SabreDat: - case OutputFormat.Xml: - return ParseXML(filename, sysid, srcid, datdata, logger, keep, clean, softlist); - default: - return datdata; - } - } - - /// - /// Parse a ClrMamePro DAT and return all found games and roms within - /// - /// Name of the file to be parsed - /// System ID for the DAT - /// Source ID for the DAT - /// The DatData object representing found roms to this point - /// Logger object for console and/or file output - /// True if full pathnames are to be kept, false otherwise (default) - /// True if game names are sanitized, false otherwise (default) - /// DatData object representing the read-in data - public static DatData ParseCMP(string filename, int sysid, int srcid, DatData datdata, Logger logger, bool keep, bool clean) - { - // Open a file reader - StreamReader sr = new StreamReader(File.OpenRead(filename)); - - bool block = false, superdat = false; - string blockname = "", gamename = "", gamedesc = ""; - while (!sr.EndOfStream) - { - string line = sr.ReadLine(); - - // Comments in CMP DATs start with a # - if (line.Trim().StartsWith("#")) - { - continue; - } - - // If the line is the header or a game - if (Regex.IsMatch(line, Constants.HeaderPatternCMP)) - { - GroupCollection gc = Regex.Match(line, Constants.HeaderPatternCMP).Groups; - - if (gc[1].Value == "clrmamepro" || gc[1].Value == "romvault") - { - blockname = "header"; - } - - block = true; - } - - // If the line is a rom or disk and we're in a block - else if ((line.Trim().StartsWith("rom (") || line.Trim().StartsWith("disk (")) && block) - { - // If we're in cleaning mode, sanitize the game name - gamename = (clean ? Style.CleanGameName(gamename) : gamename); - - RomData romData = new RomData - { - Type = (line.Trim().StartsWith("disk (") ? ItemType.Disk : ItemType.Rom), - Machine = new MachineData - { - Name = gamename, - Description = gamedesc, - SystemID = sysid, - SourceID = srcid, - }, - }; - HashData hashData = new HashData - { - Roms = new List(), - }; - - string[] gc = line.Trim().Split(' '); - - // Loop over all attributes and add them if possible - bool quote = false; - string attrib = "", val = ""; - for (int i = 2; i < gc.Length; i++) - { - //If the item is empty, we automatically skip it because it's a fluke - if (gc[i].Trim() == String.Empty) - { - continue; - } - // Special case for nodump... - else if (gc[i] == "nodump" && attrib != "status" && attrib != "flags") - { - romData.Nodump = true; - } - // Even number of quotes, not in a quote, not in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && !quote && attrib == "") - { - attrib = gc[i].Replace("\"", ""); - } - // Even number of quotes, not in a quote, in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && !quote && attrib != "") - { - switch (attrib.ToLowerInvariant()) - { - case "name": - romData.Name = gc[i].Replace("\"", ""); - break; - case "size": - Int64.TryParse(gc[i].Replace("\"", ""), out hashData.Size); - break; - case "crc": - hashData.CRC = Style.StringToByteArray(gc[i].Replace("\"", "")); - break; - case "md5": - hashData.MD5 = Style.StringToByteArray(gc[i].Replace("\"", "")); - break; - case "sha1": - hashData.SHA1 = Style.StringToByteArray(gc[i].Replace("\"", "")); - break; - case "flags": - if (gc[i].Replace("\"", "").ToLowerInvariant() == "nodump") - { - romData.Nodump = true; - } - break; - } - - attrib = ""; - } - // Even number of quotes, in a quote, not in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && quote && attrib == "") - { - // Attributes can't have quoted names - } - // Even number of quotes, in a quote, in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && quote && attrib != "") - { - val += " " + gc[i]; - } - // Odd number of quotes, not in a quote, not in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && !quote && attrib == "") - { - // Attributes can't have quoted names - } - // Odd number of quotes, not in a quote, in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && !quote && attrib != "") - { - val = gc[i].Replace("\"", ""); - quote = true; - } - // Odd number of quotes, in a quote, not in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && quote && attrib == "") - { - quote = false; - } - // Odd number of quotes, in a quote, in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && quote && attrib != "") - { - val += " " + gc[i].Replace("\"", ""); - switch (attrib.ToLowerInvariant()) - { - case "name": - romData.Name = val; - break; - case "size": - Int64.TryParse(val, out hashData.Size); - break; - case "crc": - hashData.CRC = Style.StringToByteArray(val); - break; - case "md5": - hashData.MD5 = Style.StringToByteArray(val); - break; - case "sha1": - hashData.SHA1 = Style.StringToByteArray(val); - break; - case "flags": - if (val.ToLowerInvariant() == "nodump") - { - romData.Nodump = true; - } - break; - } - - quote = false; - attrib = ""; - val = ""; - } - } - - // Sanitize the hashes from null, hex sizes, and "true blank" strings - hashData.CRC = Style.CleanHashData(hashData.CRC, Constants.CRCBytesLength); - hashData.MD5 = Style.CleanHashData(hashData.MD5, Constants.MD5BytesLength); - hashData.SHA1 = Style.CleanHashData(hashData.SHA1, Constants.SHA1BytesLength); - - // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info - if (romData.Type == ItemType.Rom - && (hashData.Size == 0 || hashData.Size == -1) - && ((hashData.CRC == Constants.CRCZeroBytes || hashData.CRC == null) - || hashData.MD5 == Constants.MD5ZeroBytes - || hashData.SHA1 == Constants.SHA1ZeroBytes)) - { - hashData.Size = Constants.SizeZero; - hashData.CRC = Constants.CRCZeroBytes; - hashData.MD5 = Constants.MD5ZeroBytes; - hashData.SHA1 = Constants.SHA1ZeroBytes; - } - // If the file has no size and it's not the above case, skip and log - else if (romData.Type == ItemType.Rom && (hashData.Size == 0 || hashData.Size == -1)) - { - logger.Warning("Incomplete entry for \"" + romData.Name + "\" will be output as nodump"); - romData.Nodump = true; - } - - // If we have a disk, make sure that the value for size is -1 - if (romData.Type == ItemType.Disk) - { - hashData.Size = -1; - } - - // Now add the hash to the Dat - if (datdata.Hashes.Contains(hashData)) - { - datdata.Hashes[datdata.Hashes.IndexOf(hashData)].Roms.Add(romData); - } - else - { - hashData.Roms.Add(romData); - datdata.Hashes.Add(hashData); - } - - // Add statistical data - datdata.RomCount += (romData.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (romData.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (romData.Nodump ? 0 : hashData.Size); - datdata.CRCCount += (hashData.CRC == null ? 0 : 1); - datdata.MD5Count += (hashData.MD5 == null ? 0 : 1); - datdata.SHA1Count += (hashData.SHA1 == null ? 0 : 1); - datdata.NodumpCount += (romData.Nodump ? 1 : 0); - } - // If the line is anything but a rom or disk and we're in a block - else if (Regex.IsMatch(line, Constants.ItemPatternCMP) && block) - { - GroupCollection gc = Regex.Match(line, Constants.ItemPatternCMP).Groups; - - if (gc[1].Value == "name" && blockname != "header") - { - gamename = gc[2].Value.Replace("\"", ""); - } - else if (gc[1].Value == "description" && blockname != "header") - { - gamedesc = gc[2].Value.Replace("\"", ""); - } - else - { - string itemval = gc[2].Value.Replace("\"", ""); - switch (gc[1].Value) - { - case "name": - datdata.Name = (String.IsNullOrEmpty(datdata.Name) ? itemval : datdata.Name); - superdat = superdat || itemval.Contains(" - SuperDAT"); - if (keep && superdat) - { - datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? "SuperDAT" : datdata.Type); - } - break; - case "description": - datdata.Description = (String.IsNullOrEmpty(datdata.Description) ? itemval : datdata.Description); - break; - case "rootdir": - datdata.RootDir = (String.IsNullOrEmpty(datdata.RootDir) ? itemval : datdata.RootDir); - break; - case "category": - datdata.Category = (String.IsNullOrEmpty(datdata.Category) ? itemval : datdata.Category); - break; - case "version": - datdata.Version = (String.IsNullOrEmpty(datdata.Version) ? itemval : datdata.Version); - break; - case "date": - datdata.Date = (String.IsNullOrEmpty(datdata.Date) ? itemval : datdata.Date); - break; - case "author": - datdata.Author = (String.IsNullOrEmpty(datdata.Author) ? itemval : datdata.Author); - break; - case "email": - datdata.Email = (String.IsNullOrEmpty(datdata.Email) ? itemval : datdata.Email); - break; - case "homepage": - datdata.Homepage = (String.IsNullOrEmpty(datdata.Homepage) ? itemval : datdata.Homepage); - break; - case "url": - datdata.Url = (String.IsNullOrEmpty(datdata.Url) ? itemval : datdata.Url); - break; - case "comment": - datdata.Comment = (String.IsNullOrEmpty(datdata.Comment) ? itemval : datdata.Comment); - break; - case "header": - datdata.Header = (String.IsNullOrEmpty(datdata.Header) ? itemval : datdata.Header); - break; - case "type": - datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? itemval : datdata.Type); - superdat = superdat || itemval.Contains("SuperDAT"); - break; - case "forcemerging": - switch (itemval) - { - case "none": - datdata.ForceMerging = ForceMerging.None; - break; - case "split": - datdata.ForceMerging = ForceMerging.Split; - break; - case "full": - datdata.ForceMerging = ForceMerging.Full; - break; - } - break; - case "forcezipping": - datdata.ForcePacking = (itemval == "yes" ? ForcePacking.Zip : ForcePacking.Unzip); - break; - } - } - } - - // If we find an end bracket that's not associated with anything else, the block is done - else if (Regex.IsMatch(line, Constants.EndPatternCMP) && block) - { - block = false; - blockname = ""; - gamename = ""; - } - } - - sr.Close(); - sr.Dispose(); - - return datdata; - } - - /// - /// Parse a RomCenter DAT and return all found games and roms within - /// - /// Name of the file to be parsed - /// System ID for the DAT - /// Source ID for the DAT - /// The DatData object representing found roms to this point - /// Logger object for console and/or file output - /// True if game names are sanitized, false otherwise (default) - /// DatData object representing the read-in data - public static DatData ParseRC(string filename, int sysid, int srcid, DatData datdata, Logger logger, bool clean) - { - // Read the input file, if possible - logger.Log("Attempting to read file: \"" + filename + "\""); - - // Check if file exists - if (!File.Exists(filename)) - { - logger.Warning("File '" + filename + "' could not read from!"); - return datdata; - } - - // If it does, open a file reader - StreamReader sr = new StreamReader(File.OpenRead(filename)); - - string blocktype = ""; - while (!sr.EndOfStream) - { - string line = sr.ReadLine(); - - // If the line is the start of the credits section - if (line.ToLowerInvariant().Contains("[credits]")) - { - blocktype = "credits"; - } - // If the line is the start of the dat section - else if (line.ToLowerInvariant().Contains("[dat]")) - { - blocktype = "dat"; - } - // If the line is the start of the emulator section - else if (line.ToLowerInvariant().Contains("[emulator]")) - { - blocktype = "emulator"; - } - // If the line is the start of the game section - else if (line.ToLowerInvariant().Contains("[games]")) - { - blocktype = "games"; - } - // Otherwise, it's not a section and it's data, so get out all data - else - { - // If we have an author - if (line.StartsWith("author=")) - { - datdata.Author = (String.IsNullOrEmpty(datdata.Author) ? line.Split('=')[1] : datdata.Author); - } - // If we have one of the three version tags - else if (line.StartsWith("version=")) - { - switch (blocktype) - { - case "credits": - datdata.Version = (String.IsNullOrEmpty(datdata.Version) ? line.Split('=')[1] : datdata.Version); - break; - case "emulator": - datdata.Description = (String.IsNullOrEmpty(datdata.Description) ? line.Split('=')[1] : datdata.Description); - break; - } - } - // If we have a comment - else if (line.StartsWith("comment=")) - { - datdata.Comment = (String.IsNullOrEmpty(datdata.Comment) ? line.Split('=')[1] : datdata.Comment); - } - // If we have the split flag - else if (line.StartsWith("split=")) - { - int split = 0; - if (Int32.TryParse(line.Split('=')[1], out split)) - { - if (split == 1) - { - datdata.ForceMerging = ForceMerging.Split; - } - } - } - // If we have the merge tag - else if (line.StartsWith("merge=")) - { - int merge = 0; - if (Int32.TryParse(line.Split('=')[1], out merge)) - { - if (merge == 1) - { - datdata.ForceMerging = ForceMerging.Full; - } - } - } - // If we have the refname tag - else if (line.StartsWith("refname=")) - { - datdata.Name = (String.IsNullOrEmpty(datdata.Name) ? line.Split('=')[1] : datdata.Name); - } - // If we have a rom - else if (line.StartsWith("¬")) - { - /* - The rominfo order is as follows: - 1 - parent name - 2 - parent description - 3 - game name - 4 - game description - 5 - rom name - 6 - rom crc - 7 - rom size - 8 - romof name - 9 - merge name - */ - string[] rominfo = line.Split('¬'); - - // If we're in cleaning mode, sanitize the game name - rominfo[3] = (clean ? Style.CleanGameName(rominfo[3]) : rominfo[3]); - - RomData romData = new RomData - { - Name = rominfo[5], - Machine = new MachineData - { - Name = rominfo[3], - Description = rominfo[4], - CloneOf = rominfo[1], - RomOf = rominfo[1], - SystemID = sysid, - SourceID = srcid, - }, - }; - HashData hashData = new HashData - { - Size = Int64.Parse(rominfo[7]), - CRC = Style.StringToByteArray(rominfo[6]), - }; - - // Sanitize the hashes from null, hex sizes, and "true blank" strings - hashData.CRC = Style.CleanHashData(hashData.CRC, Constants.CRCBytesLength); - hashData.MD5 = Style.CleanHashData(hashData.MD5, Constants.MD5BytesLength); - hashData.SHA1 = Style.CleanHashData(hashData.SHA1, Constants.SHA1BytesLength); - - // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info - if (romData.Type == ItemType.Rom - && (hashData.Size == 0 || hashData.Size == -1) - && ((hashData.CRC == Constants.CRCZeroBytes || hashData.CRC == null) - || hashData.MD5 == Constants.MD5ZeroBytes - || hashData.SHA1 == Constants.SHA1ZeroBytes)) - { - hashData.Size = Constants.SizeZero; - hashData.CRC = Constants.CRCZeroBytes; - hashData.MD5 = Constants.MD5ZeroBytes; - hashData.SHA1 = Constants.SHA1ZeroBytes; - } - // If the file has no size and it's not the above case, skip and log - else if (romData.Type == ItemType.Rom && (hashData.Size == 0 || hashData.Size == -1)) - { - logger.Warning("Incomplete entry for \"" + romData.Name + "\" will be output as nodump"); - romData.Nodump = true; - } - - // If we have a disk, make sure that the value for size is -1 - if (romData.Type == ItemType.Disk) - { - hashData.Size = -1; - } - - // Now add the hash to the Dat - if (datdata.Hashes.Contains(hashData)) - { - datdata.Hashes[datdata.Hashes.IndexOf(hashData)].Roms.Add(romData); - } - else - { - hashData.Roms.Add(romData); - datdata.Hashes.Add(hashData); - } - - // Add statistical data - datdata.RomCount += (romData.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (romData.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (romData.Nodump ? 0 : hashData.Size); - datdata.CRCCount += (hashData.CRC == null ? 0 : 1); - datdata.MD5Count += (hashData.MD5 == null ? 0 : 1); - datdata.SHA1Count += (hashData.SHA1 == null ? 0 : 1); - datdata.NodumpCount += (romData.Nodump ? 1 : 0); - } - } - } - - sr.Close(); - sr.Dispose(); - - return datdata; - } - - /// - /// Parse an XML DAT (Logiqx, SabreDAT, or SL) and return all found games and roms within - /// - /// Name of the file to be parsed - /// System ID for the DAT - /// Source ID for the DAT - /// The DatData object representing found roms to this point - /// Logger object for console and/or file output - /// True if full pathnames are to be kept, false otherwise (default) - /// True if game names are sanitized, false otherwise (default) - /// True if SL XML names should be kept, false otherwise (default) - /// DatData object representing the read-in data - public static DatData ParseXML(string filename, int sysid, int srcid, DatData datdata, Logger logger, bool keep, bool clean, bool softlist) - { - // Prepare all internal variables - XmlReader subreader, headreader, flagreader; - bool superdat = false, nodump = false, empty = true; - string crc = "", md5 = "", sha1 = "", date = ""; - long size = -1; - List parent = new List(); - - XmlTextReader xtr = DatTools.GetXmlTextReader(filename, logger); - if (xtr != null) - { - xtr.MoveToContent(); - while (!xtr.EOF) - { - // If we're ending a folder or game, take care of possibly empty games and removing from the parent - if (xtr.NodeType == XmlNodeType.EndElement && (xtr.Name == "directory" || xtr.Name == "dir")) - { - // If we didn't find any items in the folder, make sure to add the blank rom - if (empty) - { - string tempgame = String.Join("\\", parent); - - // If we're in cleaning mode, sanitize the game name - tempgame = (clean ? Style.CleanGameName(tempgame) : tempgame); - - RomData romData = new RomData - { - Type = ItemType.Rom, - Name = "null", - Machine = new MachineData - { - Name = tempgame, - Description = tempgame, - }, - }; - HashData hashData = new HashData - { - Size = -1, - CRC = null, - MD5 = null, - SHA1 = null, - Roms = new List(), - }; - - // Now add the hash to the Dat - if (datdata.Hashes.Contains(hashData)) - { - datdata.Hashes[datdata.Hashes.IndexOf(hashData)].Roms.Add(romData); - } - else - { - hashData.Roms.Add(romData); - datdata.Hashes.Add(hashData); - } - - // Add statistical data - datdata.RomCount += (romData.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (romData.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (romData.Nodump ? 0 : hashData.Size); - datdata.CRCCount += (hashData.CRC == null ? 0 : 1); - datdata.MD5Count += (hashData.MD5 == null ? 0 : 1); - datdata.SHA1Count += (hashData.SHA1 == null ? 0 : 1); - datdata.NodumpCount += (romData.Nodump ? 1 : 0); - } - - // Regardless, end the current folder - int parentcount = parent.Count; - if (parentcount == 0) - { - logger.Log("Empty parent: " + String.Join("\\", parent)); - empty = true; - } - - // If we have an end folder element, remove one item from the parent, if possible - if (parentcount > 0) - { - parent.RemoveAt(parent.Count - 1); - if (keep && parentcount > 1) - { - datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? "SuperDAT" : datdata.Type); - superdat = true; - } - } - } - - // We only want elements - if (xtr.NodeType != XmlNodeType.Element) - { - xtr.Read(); - continue; - } - - switch (xtr.Name) - { - // New software lists have this behavior - case "softwarelist": - if (xtr.GetAttribute("name") != null) - { - datdata.Name = (String.IsNullOrEmpty(datdata.Name) ? xtr.GetAttribute("name") : datdata.Name); - } - if (xtr.GetAttribute("description") != null) - { - datdata.Description = (String.IsNullOrEmpty(datdata.Description) ? xtr.GetAttribute("description") : datdata.Description); - } - xtr.Read(); - break; - // Handle M1 DATs since they're 99% the same as a SL DAT - case "m1": - datdata.Name = (String.IsNullOrEmpty(datdata.Name) ? "M1" : datdata.Name); - datdata.Description = (String.IsNullOrEmpty(datdata.Description) ? "M1" : datdata.Description); - if (xtr.GetAttribute("version") != null) - { - datdata.Version = (String.IsNullOrEmpty(datdata.Version) ? xtr.GetAttribute("version") : datdata.Version); - } - break; - case "header": - // We want to process the entire subtree of the header - headreader = xtr.ReadSubtree(); - - if (headreader != null) - { - while (!headreader.EOF) - { - // We only want elements - if (headreader.NodeType != XmlNodeType.Element || headreader.Name == "header") - { - headreader.Read(); - continue; - } - - // Get all header items (ONLY OVERWRITE IF THERE'S NO DATA) - string content = ""; - switch (headreader.Name) - { - case "name": - content = headreader.ReadElementContentAsString(); ; - datdata.Name = (String.IsNullOrEmpty(datdata.Name) ? content : datdata.Name); - superdat = superdat || content.Contains(" - SuperDAT"); - if (keep && superdat) - { - datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? "SuperDAT" : datdata.Type); - } - break; - case "description": - content = headreader.ReadElementContentAsString(); - datdata.Description = (String.IsNullOrEmpty(datdata.Description) ? content : datdata.Description); - break; - case "rootdir": - content = headreader.ReadElementContentAsString(); - datdata.RootDir = (String.IsNullOrEmpty(datdata.RootDir) ? content : datdata.RootDir); - break; - case "category": - content = headreader.ReadElementContentAsString(); - datdata.Category = (String.IsNullOrEmpty(datdata.Category) ? content : datdata.Category); - break; - case "version": - content = headreader.ReadElementContentAsString(); - datdata.Version = (String.IsNullOrEmpty(datdata.Version) ? content : datdata.Version); - break; - case "date": - content = headreader.ReadElementContentAsString(); - datdata.Date = (String.IsNullOrEmpty(datdata.Date) ? content : datdata.Date); - break; - case "author": - content = headreader.ReadElementContentAsString(); - datdata.Author = (String.IsNullOrEmpty(datdata.Author) ? content : datdata.Author); - - // Special cases for SabreDAT - datdata.Email = (String.IsNullOrEmpty(datdata.Email) && !String.IsNullOrEmpty(headreader.GetAttribute("email")) ? - headreader.GetAttribute("email") : datdata.Email); - datdata.Homepage = (String.IsNullOrEmpty(datdata.Homepage) && !String.IsNullOrEmpty(headreader.GetAttribute("homepage")) ? - headreader.GetAttribute("homepage") : datdata.Email); - datdata.Url = (String.IsNullOrEmpty(datdata.Url) && !String.IsNullOrEmpty(headreader.GetAttribute("url")) ? - headreader.GetAttribute("url") : datdata.Email); - break; - case "email": - content = headreader.ReadElementContentAsString(); - datdata.Email = (String.IsNullOrEmpty(datdata.Email) ? content : datdata.Email); - break; - case "homepage": - content = headreader.ReadElementContentAsString(); - datdata.Homepage = (String.IsNullOrEmpty(datdata.Homepage) ? content : datdata.Homepage); - break; - case "url": - content = headreader.ReadElementContentAsString(); - datdata.Url = (String.IsNullOrEmpty(datdata.Url) ? content : datdata.Url); - break; - case "comment": - content = headreader.ReadElementContentAsString(); - datdata.Comment = (String.IsNullOrEmpty(datdata.Comment) ? content : datdata.Comment); - break; - case "type": - content = headreader.ReadElementContentAsString(); - datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? content : datdata.Type); - superdat = superdat || content.Contains("SuperDAT"); - break; - case "clrmamepro": - if (headreader.GetAttribute("header") != null) - { - datdata.Header = (String.IsNullOrEmpty(datdata.Header) ? headreader.GetAttribute("header") : datdata.Header); - } - if (headreader.GetAttribute("forcemerging") != null) - { - switch (headreader.GetAttribute("forcemerging")) - { - case "split": - datdata.ForceMerging = ForceMerging.Split; - break; - case "none": - datdata.ForceMerging = ForceMerging.None; - break; - case "full": - datdata.ForceMerging = ForceMerging.Full; - break; - } - } - if (headreader.GetAttribute("forcenodump") != null) - { - switch (headreader.GetAttribute("forcenodump")) - { - case "obsolete": - datdata.ForceNodump = ForceNodump.Obsolete; - break; - case "required": - datdata.ForceNodump = ForceNodump.Required; - break; - case "ignore": - datdata.ForceNodump = ForceNodump.Ignore; - break; - } - } - if (headreader.GetAttribute("forcepacking") != null) - { - switch (headreader.GetAttribute("forcepacking")) - { - case "zip": - datdata.ForcePacking = ForcePacking.Zip; - break; - case "unzip": - datdata.ForcePacking = ForcePacking.Unzip; - break; - } - } - headreader.Read(); - break; - case "flags": - flagreader = xtr.ReadSubtree(); - if (flagreader != null) - { - while (!flagreader.EOF) - { - // We only want elements - if (flagreader.NodeType != XmlNodeType.Element || flagreader.Name == "flags") - { - flagreader.Read(); - continue; - } - - switch (flagreader.Name) - { - case "flag": - if (flagreader.GetAttribute("name") != null && flagreader.GetAttribute("value") != null) - { - content = flagreader.GetAttribute("value"); - switch (flagreader.GetAttribute("name")) - { - case "type": - datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? content : datdata.Type); - superdat = superdat || content.Contains("SuperDAT"); - break; - case "forcemerging": - switch (content) - { - case "split": - datdata.ForceMerging = ForceMerging.Split; - break; - case "none": - datdata.ForceMerging = ForceMerging.None; - break; - case "full": - datdata.ForceMerging = ForceMerging.Full; - break; - } - break; - case "forcenodump": - switch (content) - { - case "obsolete": - datdata.ForceNodump = ForceNodump.Obsolete; - break; - case "required": - datdata.ForceNodump = ForceNodump.Required; - break; - case "ignore": - datdata.ForceNodump = ForceNodump.Ignore; - break; - } - break; - case "forcepacking": - switch (content) - { - case "zip": - datdata.ForcePacking = ForcePacking.Zip; - break; - case "unzip": - datdata.ForcePacking = ForcePacking.Unzip; - break; - } - break; - } - } - flagreader.Read(); - break; - default: - flagreader.Read(); - break; - } - } - } - headreader.Skip(); - break; - default: - headreader.Read(); - break; - } - } - } - - // Skip the header node now that we've processed it - xtr.Skip(); - break; - case "machine": - case "game": - case "software": - string temptype = xtr.Name; - string tempname = "", gamedesc = ""; - - // We want to process the entire subtree of the game - subreader = xtr.ReadSubtree(); - - // Safeguard for interesting case of "software" without anything except roms - bool software = false; - - // If we have a subtree, add what is possible - if (subreader != null) - { - if (!softlist && temptype == "software" && subreader.ReadToFollowing("description")) - { - tempname = subreader.ReadElementContentAsString(); - tempname = tempname.Replace('/', '_').Replace("\"", "''"); - software = true; - } - else - { - // There are rare cases where a malformed XML will not have the required attributes. We can only skip them. - if (xtr.AttributeCount == 0) - { - logger.Error("No attributes were found"); - xtr.Skip(); - continue; - } - tempname = xtr.GetAttribute("name"); - } - - if (superdat && !keep) - { - string tempout = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value; - if (tempout != "") - { - tempname = tempout; - } - } - // Get the name of the game from the parent - else if (superdat && keep && parent.Count > 0) - { - tempname = String.Join("\\", parent) + "\\" + tempname; - } - - while (software || subreader.Read()) - { - software = false; - - // We only want elements - if (subreader.NodeType != XmlNodeType.Element) - { - continue; - } - - // Get the roms from the machine - switch (subreader.Name) - { - case "description": - gamedesc = subreader.ReadElementContentAsString(); - break; - case "rom": - case "disk": - empty = false; - - // If the rom is nodump, flag it - nodump = false; - if (subreader.GetAttribute("flags") == "nodump" || subreader.GetAttribute("status") == "nodump") - { - logger.Log("Nodump detected: " + - (subreader.GetAttribute("name") != null && subreader.GetAttribute("name") != "" ? "\"" + xtr.GetAttribute("name") + "\"" : "ROM NAME NOT FOUND")); - nodump = true; - } - - // If the rom has a Date attached, read it in and then sanitize it - date = ""; - if (subreader.GetAttribute("date") != null) - { - date = DateTime.Parse(subreader.GetAttribute("date")).ToString(); - } - - // Take care of hex-sized files - size = -1; - if (subreader.GetAttribute("size") != null && subreader.GetAttribute("size").Contains("0x")) - { - size = Convert.ToInt64(subreader.GetAttribute("size"), 16); - } - else if (subreader.GetAttribute("size") != null) - { - Int64.TryParse(subreader.GetAttribute("size"), out size); - } - - // If the rom is continue or ignore, add the size to the previous rom - if (subreader.GetAttribute("loadflag") == "continue" || subreader.GetAttribute("loadflag") == "ignore") - { - int index = datdata.Hashes.Count() - 1; - HashData lasthash = datdata.Hashes[index]; - lasthash.Size += size; - datdata.Hashes.RemoveAt(index); - datdata.Hashes.Add(lasthash); - continue; - } - - // Sanitize the hashes from null, hex sizes, and "true blank" strings - crc = Style.CleanHashData(subreader.GetAttribute("crc"), Constants.CRCLength); - md5 = Style.CleanHashData(subreader.GetAttribute("md5"), Constants.MD5Length); - sha1 = Style.CleanHashData(subreader.GetAttribute("sha1"), Constants.SHA1Length); - - // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info - if (subreader.Name == "rom" && (size == 0 || size == -1) && - ((crc == Constants.CRCZero || crc == "") || md5 == Constants.MD5Zero || sha1 == Constants.SHA1Zero)) - { - size = Constants.SizeZero; - crc = Constants.CRCZero; - md5 = Constants.MD5Zero; - sha1 = Constants.SHA1Zero; - } - // If the file has no size and it's not the above case, skip and log - else if (subreader.Name == "rom" && (size == 0 || size == -1)) - { - logger.Warning("Incomplete entry for \"" + subreader.GetAttribute("name") + "\" will be output as nodump"); - nodump = true; - } - - // If we're in clean mode, sanitize the game name - if (clean) - { - tempname = Style.CleanGameName(tempname.Split(Path.DirectorySeparatorChar)); - } - - // Only add the rom if there's useful information in it - if (!(crc == "" && md5 == "" && sha1 == "") || nodump) - { - // If we got to this point and it's a disk, log it because some tools don't like disks - if (subreader.Name == "disk") - { - logger.Log("Disk found: \"" + subreader.GetAttribute("name") + "\""); - } - - // Now add the hash to the Dat - RomData romData = new RomData - { - Name = subreader.GetAttribute("name"), - Type = (subreader.Name.ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom), - Nodump = nodump, - Date = date, - Machine = new MachineData - { - Name = tempname, - Description = gamedesc, - SystemID = sysid, - System = filename, - SourceID = srcid, - }, - }; - HashData hashData = new HashData - { - Size = size, - CRC = Style.CleanHashData(Style.StringToByteArray(crc), Constants.CRCBytesLength), - MD5 = Style.CleanHashData(Style.StringToByteArray(md5), Constants.MD5BytesLength), - SHA1 = Style.CleanHashData(Style.StringToByteArray(sha1), Constants.SHA1BytesLength), - Roms = new List(), - }; - - if (datdata.Hashes.Contains(hashData)) - { - datdata.Hashes[datdata.Hashes.IndexOf(hashData)].Roms.Add(romData); - } - else - { - hashData.Roms.Add(romData); - datdata.Hashes.Add(hashData); - } - - // Add statistical data - datdata.RomCount += (romData.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (romData.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (romData.Nodump ? 0 : hashData.Size); - datdata.CRCCount += (hashData.CRC == null ? 0 : 1); - datdata.MD5Count += (hashData.MD5 == null ? 0 : 1); - datdata.SHA1Count += (hashData.SHA1 == null ? 0 : 1); - datdata.NodumpCount += (romData.Nodump ? 1 : 0); - } - // Otherwise, log that it wasn't added - else - { - logger.Log("Rom was not added: '" + xtr.GetAttribute("name") + "'"); - } - break; - } - } - } - - // If we didn't find any items in the folder, make sure to add the blank rom - if (empty) - { - tempname = (parent.Count > 0 ? String.Join("\\", parent) + Path.DirectorySeparatorChar : "") + tempname; - - // If we're in cleaning mode, sanitize the game name - tempname = (clean ? Style.CleanGameName(tempname.Split(Path.DirectorySeparatorChar)) : tempname); - - // Now add the hash to the Dat - RomData romData = new RomData - { - Type = ItemType.Rom, - Name = "null", - Machine = new MachineData - { - Name = tempname, - Description = tempname, - }, - }; - HashData hashData = new HashData - { - Size = -1, - CRC = null, - MD5 = null, - SHA1 = null, - Roms = new List(), - }; - - if (datdata.Hashes.Contains(hashData)) - { - datdata.Hashes[datdata.Hashes.IndexOf(hashData)].Roms.Add(romData); - } - else - { - hashData.Roms.Add(romData); - datdata.Hashes.Add(hashData); - } - - // Add statistical data - datdata.RomCount += (romData.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (romData.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (romData.Nodump ? 0 : hashData.Size); - datdata.CRCCount += (hashData.CRC == null ? 0 : 1); - datdata.MD5Count += (hashData.MD5 == null ? 0 : 1); - datdata.SHA1Count += (hashData.SHA1 == null ? 0 : 1); - datdata.NodumpCount += (romData.Nodump ? 1 : 0); - } - - // Regardless, end the current folder - if (parent.Count == 0) - { - empty = true; - } - xtr.Skip(); - break; - case "dir": - case "directory": - // Set SuperDAT flag for all SabreDAT inputs, regardless of depth - superdat = true; - if (keep) - { - datdata.Type = (datdata.Type == "" ? "SuperDAT" : datdata.Type); - } - - string foldername = (xtr.GetAttribute("name") == null ? "" : xtr.GetAttribute("name")); - if (foldername != "") - { - parent.Add(foldername); - } - - xtr.Read(); - break; - case "file": - empty = false; - - // If the rom is nodump, flag it - nodump = false; - flagreader = xtr.ReadSubtree(); - if (flagreader != null) - { - while (!flagreader.EOF) - { - // We only want elements - if (flagreader.NodeType != XmlNodeType.Element || flagreader.Name == "flags") - { - flagreader.Read(); - continue; - } - - switch (flagreader.Name) - { - case "flag": - case "status": - if (flagreader.GetAttribute("name") != null && flagreader.GetAttribute("value") != null) - { - string content = flagreader.GetAttribute("value"); - switch (flagreader.GetAttribute("name")) - { - case "nodump": - logger.Log("Nodump detected: " + (xtr.GetAttribute("name") != null && xtr.GetAttribute("name") != "" ? - "\"" + xtr.GetAttribute("name") + "\"" : "ROM NAME NOT FOUND")); - nodump = true; - break; - } - } - break; - } - - flagreader.Read(); - } - } - - // If the rom has a Date attached, read it in and then sanitize it - date = ""; - if (xtr.GetAttribute("date") != null) - { - date = DateTime.Parse(xtr.GetAttribute("date")).ToString(); - } - - // Take care of hex-sized files - size = -1; - if (xtr.GetAttribute("size") != null && xtr.GetAttribute("size").Contains("0x")) - { - size = Convert.ToInt64(xtr.GetAttribute("size"), 16); - } - else if (xtr.GetAttribute("size") != null) - { - Int64.TryParse(xtr.GetAttribute("size"), out size); - } - - // If the rom is continue or ignore, add the size to the previous rom - if (xtr.GetAttribute("loadflag") == "continue" || xtr.GetAttribute("loadflag") == "ignore") - { - int index = datdata.Hashes.Count() - 1; - HashData lasthash = datdata.Hashes[index]; - lasthash.Size += size; - datdata.Hashes.RemoveAt(index); - datdata.Hashes.Add(lasthash); - continue; - } - - // Sanitize the hashes from null, hex sizes, and "true blank" strings - crc = Style.CleanHashData(xtr.GetAttribute("crc"), Constants.CRCLength); - md5 = Style.CleanHashData(xtr.GetAttribute("md5"), Constants.MD5Length); - sha1 = Style.CleanHashData(xtr.GetAttribute("sha1"), Constants.SHA1Length); - - // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info - if (xtr.GetAttribute("type") == "rom" && (size == 0 || size == -1) && ((crc == Constants.CRCZero || crc == "") || md5 == Constants.MD5Zero || sha1 == Constants.SHA1Zero)) - { - size = Constants.SizeZero; - crc = Constants.CRCZero; - md5 = Constants.MD5Zero; - sha1 = Constants.SHA1Zero; - } - // If the file has no size and it's not the above case, skip and log - else if (xtr.GetAttribute("type") == "rom" && (size == 0 || size == -1)) - { - logger.Warning("Incomplete entry for \"" + xtr.GetAttribute("name") + "\" will be output as nodump"); - nodump = true; - } - - // Get the name of the game from the parent - tempname = String.Join("\\", parent); - - // If we aren't keeping names, trim out the path - if (!keep || !superdat) - { - string tempout = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value; - if (tempout != "") - { - tempname = tempout; - } - } - - // If we're in cleaning mode, sanitize the game name - tempname = (clean ? Style.CleanGameName(tempname) : tempname); - - // Only add the rom if there's useful information in it - if (!(crc == "" && md5 == "" && sha1 == "") || nodump) - { - // If we got to this point and it's a disk, log it because some tools don't like disks - if (xtr.GetAttribute("type") == "disk") - { - logger.Log("Disk found: \"" + xtr.GetAttribute("name") + "\""); - } - - // Now add the hash to the Dat - RomData romData = new RomData - { - Name = xtr.GetAttribute("name"), - Type = (xtr.GetAttribute("type").ToLowerInvariant() == "disk" ? ItemType.Disk : ItemType.Rom), - Nodump = nodump, - Date = date, - Machine = new MachineData - { - Name = tempname, - SystemID = sysid, - System = filename, - SourceID = srcid, - }, - }; - HashData hashData = new HashData - { - Size = size, - CRC = Style.CleanHashData(Style.StringToByteArray(crc), Constants.CRCBytesLength), - MD5 = Style.CleanHashData(Style.StringToByteArray(md5), Constants.MD5BytesLength), - SHA1 = Style.CleanHashData(Style.StringToByteArray(sha1), Constants.SHA1BytesLength), - Roms = new List(), - }; - - if (datdata.Hashes.Contains(hashData)) - { - datdata.Hashes[datdata.Hashes.IndexOf(hashData)].Roms.Add(romData); - } - else - { - hashData.Roms.Add(romData); - datdata.Hashes.Add(hashData); - } - - // Add statistical data - datdata.RomCount += (romData.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (romData.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (romData.Nodump ? 0 : hashData.Size); - datdata.CRCCount += (hashData.CRC == null ? 0 : 1); - datdata.MD5Count += (hashData.MD5 == null ? 0 : 1); - datdata.SHA1Count += (hashData.SHA1 == null ? 0 : 1); - datdata.NodumpCount += (romData.Nodump ? 1 : 0); - } - xtr.Read(); - break; - default: - xtr.Read(); - break; - } - } - - xtr.Close(); - xtr.Dispose(); - } - - return datdata; - } - - #endregion - - #region Bucketing methods - - /// - /// Take an arbitrarily ordered List and return a Dictionary sorted by Game - /// - /// Input unsorted list - /// True if roms should be deduped, false otherwise - /// True if games should only be compared on game and file name, false if system and source are counted - /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - /// SortedDictionary bucketed by game name - public static SortedDictionary> BucketByGame(List list, bool mergeroms, bool norename, Logger logger, bool output = true) - { - Dictionary> dict = new Dictionary>(); - dict.Add("key", list); - return BucketByGame(dict, mergeroms, norename, logger, output); - } - - /// - /// Take an arbitrarily bucketed Dictionary and return one sorted by Game - /// - /// Input unsorted dictionary - /// True if roms should be deduped, false otherwise - /// True if games should only be compared on game and file name, false if system and source are counted - /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - /// SortedDictionary bucketed by game name> BucketByGame(IDictionary> dict, bool mergeroms, bool norename, Logger logger, bool output = true) - { - SortedDictionary> sortable = new SortedDictionary>(); - long count = 0; - - // If we have a null dict or an empty one, output a new dictionary - if (dict == null || dict.Count == 0) - { - return sortable; - } - - // Process each all of the roms - foreach (string key in dict.Keys) - { - List hashes = dict[key]; - if (mergeroms) - { - hashes = RomToolsHash.Merge(hashes, logger); - } - - foreach (HashData hash in hashes) - { - count++; - string newkey = (norename ? "" - : hash.Roms[0].Machine.SystemID.ToString().PadLeft(10, '0') - + "-" - + hash.Roms[0].Machine.SourceID.ToString().PadLeft(10, '0') + "-") - + (String.IsNullOrEmpty(hash.Roms[0].Machine.Name) - ? "" - : hash.Roms[0].Machine.Name.ToLowerInvariant()); - if (sortable.ContainsKey(newkey)) - { - sortable[newkey].Add(hash); - } - else - { - List temp = new List(); - temp.Add(hash); - sortable.Add(newkey, temp); - } - } - } - - // Output the count if told to - if (output) - { - logger.User("A total of " + count + " file hashes will be written out to file"); - } - - return sortable; - } - - /// - /// Take an arbitrarily ordered List and return a Dictionary sorted by size and hash - /// - /// Input unsorted list - /// True if roms should be deduped, false otherwise - /// True if games should only be compared on game and file name, false if system and source are counted - /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - /// SortedDictionary bucketed by size and hash - public static SortedDictionary> BucketByHashSize(List list, bool mergeroms, bool norename, Logger logger, bool output = true) - { - Dictionary> dict = new Dictionary>(); - dict.Add("key", list); - return BucketByHashSize(dict, mergeroms, norename, logger, output); - } - - /// - /// Take an arbitrarily bucketed Dictionary and return one sorted by size and hash - /// - /// Input unsorted dictionary - /// True if roms should be deduped, false otherwise - /// True if games should only be compared on game and file name, false if system and source are counted - /// Logger object for file and console output - /// True if the number of hashes counted is to be output (default), false otherwise - /// SortedDictionary bucketed by size and hash - public static SortedDictionary> BucketByHashSize(IDictionary> dict, bool mergeroms, bool norename, Logger logger, bool output = true) - { - SortedDictionary> sortable = new SortedDictionary>(); - long count = 0; - - // If we have a null dict or an empty one, output a new dictionary - if (dict == null || dict.Count == 0) - { - return sortable; - } - - // Process each all of the roms - foreach (List hashes in dict.Values) - { - List newhashes = hashes; - if (mergeroms) - { - newhashes = RomToolsHash.Merge(newhashes, logger); - } - - foreach (HashData hash in newhashes) - { - count++; - string key = hash.Size + "-" + BitConverter.ToString(hash.CRC).Replace("-", string.Empty); - if (sortable.ContainsKey(key)) - { - sortable[key].Add(hash); - } - else - { - List temp = new List(); - temp.Add(hash); - sortable.Add(key, temp); - } - } - } - - // Output the count if told to - if (output) - { - logger.User("A total of " + count + " file hashes will be written out to file"); - } - - return sortable; - } - - #endregion - - #region Converting and updating - - #endregion - } -} diff --git a/SabreTools.Helper/Tools/FileTools.cs b/SabreTools.Helper/Tools/FileTools.cs index c669aca5..ff4118d6 100644 --- a/SabreTools.Helper/Tools/FileTools.cs +++ b/SabreTools.Helper/Tools/FileTools.cs @@ -63,7 +63,7 @@ namespace SabreTools.Helper } // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, roms[0].Machine.Name + ".zip"); + string archiveFileName = Path.Combine(outDir, roms[0].MachineName + ".zip"); // First, open the archive ZipArchive outarchive = null; @@ -172,7 +172,7 @@ namespace SabreTools.Helper } // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, roms[0].Machine.Name + ".zip"); + string archiveFileName = Path.Combine(outDir, roms[0].MachineName + ".zip"); // Set internal variables Stream writeStream = null; @@ -760,18 +760,21 @@ namespace SabreTools.Helper } crc.Update(buffer, 0, 0); - rom.HashData.CRC = crc.Value.ToString("X8").ToLowerInvariant(); + Hash tempHash = new Hash(); + tempHash.CRC = crc.Value.ToString("X8").ToLowerInvariant(); if (!noMD5) { md5.TransformFinalBlock(buffer, 0, 0); - rom.HashData.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); + tempHash.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); } if (!noSHA1) { sha1.TransformFinalBlock(buffer, 0, 0); - rom.HashData.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); + tempHash.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); } + + rom.HashData = tempHash; } } catch (IOException) @@ -860,10 +863,7 @@ namespace SabreTools.Helper { Type = ItemType.Rom, Name = reader.Entry.Key, - Machine = new Machine - { - Name = gamename, - }, + MachineName = gamename, HashData = new Hash { Size = (size == 0 ? reader.Entry.Size : size), @@ -948,10 +948,7 @@ namespace SabreTools.Helper Rom rom = new Rom { Type = ItemType.Rom, - Machine = new Machine - { - Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), - }, + MachineName = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), HashData = new Hash { diff --git a/SabreTools.Helper/Tools/RomTools.cs b/SabreTools.Helper/Tools/RomTools.cs deleted file mode 100644 index 35592988..00000000 --- a/SabreTools.Helper/Tools/RomTools.cs +++ /dev/null @@ -1,505 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace SabreTools.Helper -{ - public class RomTools - { - #region Rom-based sorting and merging - - /// - /// Determine if a rom should be included based on filters - /// - /// User supplied Rom to check - /// Name of the game to match (can use asterisk-partials) - /// Name of the rom to match (can use asterisk-partials) - /// Type of the rom to match - /// Find roms greater than or equal to this size - /// Find roms less than or equal to this size - /// Find roms equal to this size - /// CRC of the rom to match (can use asterisk-partials) - /// MD5 of the rom to match (can use asterisk-partials) - /// SHA-1 of the rom to match (can use asterisk-partials) - /// Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump) - /// Logging object for console and file output - /// Returns true if it should be included, false otherwise - public static bool Filter(Rom romdata, string gamename, string romname, string romtype, long sgt, - long slt, long seq, string crc, string md5, string sha1, bool? nodump, Logger logger) - { - // Filter on nodump status - if (nodump == true && !romdata.Nodump) - { - return false; - } - if (nodump == false && romdata.Nodump) - { - return false; - } - - // Filter on game name - if (!String.IsNullOrEmpty(gamename)) - { - if (gamename.StartsWith("*") && gamename.EndsWith("*")) - { - if (!romdata.Machine.Name.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", ""))) - { - return false; - } - } - else if (gamename.StartsWith("*")) - { - if (!romdata.Machine.Name.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else if (gamename.EndsWith("*")) - { - if (!romdata.Machine.Name.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else - { - if (!String.Equals(romdata.Machine.Name, gamename, StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - } - - // Filter on rom name - if (!String.IsNullOrEmpty(romname)) - { - if (romname.StartsWith("*") && romname.EndsWith("*")) - { - if (!romdata.Name.ToLowerInvariant().Contains(romname.ToLowerInvariant().Replace("*", ""))) - { - return false; - } - } - else if (romname.StartsWith("*")) - { - if (!romdata.Name.EndsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else if (romname.EndsWith("*")) - { - if (!romdata.Name.StartsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else - { - if (!String.Equals(romdata.Name, romname, StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - } - - // Filter on rom type - if (String.IsNullOrEmpty(romtype) && romdata.Type != ItemType.Rom && romdata.Type != ItemType.Disk) - { - return false; - } - if (!String.IsNullOrEmpty(romtype) && !String.Equals(romdata.Type.ToString(), romtype, StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - - // Filter on rom size - if (seq != -1 && romdata.HashData.Size != seq) - { - return false; - } - else - { - if (sgt != -1 && romdata.HashData.Size < sgt) - { - return false; - } - if (slt != -1 && romdata.HashData.Size > slt) - { - return false; - } - } - - // Filter on crc - if (!String.IsNullOrEmpty(crc)) - { - if (crc.StartsWith("*") && crc.EndsWith("*")) - { - if (!romdata.HashData.CRC.ToLowerInvariant().Contains(crc.ToLowerInvariant().Replace("*", ""))) - { - return false; - } - } - else if (crc.StartsWith("*")) - { - if (!romdata.HashData.CRC.EndsWith(crc.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else if (crc.EndsWith("*")) - { - if (!romdata.HashData.CRC.StartsWith(crc.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else - { - if (!String.Equals(romdata.HashData.CRC, crc, StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - } - - // Filter on md5 - if (!String.IsNullOrEmpty(md5)) - { - if (md5.StartsWith("*") && md5.EndsWith("*")) - { - if (!romdata.HashData.MD5.ToLowerInvariant().Contains(md5.ToLowerInvariant().Replace("*", ""))) - { - return false; - } - } - else if (md5.StartsWith("*")) - { - if (!romdata.HashData.MD5.EndsWith(md5.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else if (md5.EndsWith("*")) - { - if (!romdata.HashData.MD5.StartsWith(md5.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else - { - if (!String.Equals(romdata.HashData.MD5, md5, StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - } - - // Filter on sha1 - if (!String.IsNullOrEmpty(sha1)) - { - if (sha1.StartsWith("*") && sha1.EndsWith("*")) - { - if (!romdata.HashData.SHA1.ToLowerInvariant().Contains(sha1.ToLowerInvariant().Replace("*", ""))) - { - return false; - } - } - else if (sha1.StartsWith("*")) - { - if (!romdata.HashData.SHA1.EndsWith(sha1.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else if (sha1.EndsWith("*")) - { - if (!romdata.HashData.SHA1.StartsWith(sha1.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - else - { - if (!String.Equals(romdata.HashData.SHA1, sha1, StringComparison.InvariantCultureIgnoreCase)) - { - return false; - } - } - } - - return true; - } - - /// - /// Merge an arbitrary set of ROMs based on the supplied information - /// - /// List of RomData objects representing the roms to be merged - /// Logger object for console and/or file output - /// A List of RomData objects representing the merged roms - public static List Merge(List inroms, Logger logger) - { - // Check for null or blank roms first - if (inroms == null || inroms.Count == 0) - { - return new List(); - } - - // Create output list - List outroms = new List(); - - // Then deduplicate them by checking to see if data matches previous saved roms - foreach (Rom rom in inroms) - { - // 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 - DupeType dupetype = DupeType.None; - Rom savedrom = new Rom(); - int pos = -1; - for (int i = 0; i < outroms.Count; i++) - { - Rom lastrom = outroms[i]; - - // Get the duplicate status - dupetype = GetDuplicateStatus(rom, lastrom, logger); - - // If it's a duplicate, skip adding it to the output but add any missing information - if (dupetype != DupeType.None) - { - savedrom = lastrom; - pos = i; - - 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); - savedrom.Dupe = dupetype; - - // If the current system has a lower ID than the previous, set the system accordingly - if (rom.Metadata.SystemID < savedrom.Metadata.SystemID) - { - savedrom.Metadata.SystemID = rom.Metadata.SystemID; - savedrom.Metadata.System = rom.Metadata.System; - savedrom.Machine.Name = rom.Machine.Name; - savedrom.Name = rom.Name; - } - - // If the current source has a lower ID than the previous, set the source accordingly - if (rom.Metadata.SourceID < savedrom.Metadata.SourceID) - { - savedrom.Metadata.SourceID = rom.Metadata.SourceID; - savedrom.Metadata.Source = rom.Metadata.Source; - savedrom.Machine.Name = rom.Machine.Name; - savedrom.Name = rom.Name; - } - - break; - } - } - - // If no duplicate is found, add it to the list - if (dupetype == DupeType.None) - { - 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; - } - - /// - /// List all duplicates found in a DAT based on a rom - /// - /// Rom to use as a base - /// DAT to match against - /// Logger object for console and/or file output - /// True to remove matched roms from the input, false otherwise (default) - /// List of matched RomData objects - public static List GetDuplicates(Rom lastrom, Dat datdata, Logger logger, bool remove = false) - { - List output = new List(); - - // Check for an empty rom list first - if (datdata.Files == null || datdata.Files.Count == 0) - { - return output; - } - - // Try to find duplicates - List keys = datdata.Files.Keys.ToList(); - foreach (string key in keys) - { - List roms = datdata.Files[key]; - List left = new List(); - - 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; - } - - /// - /// Determine if a file is a duplicate using partial matching logic - /// - /// Rom to check for duplicate status - /// Rom to use as a baseline - /// Logger object for console and/or file output - /// True if the roms are duplicates, false otherwise - 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; - } - - /// - /// Return the duplicate status of two roms - /// - /// Current rom to check - /// Last rom to check against - /// Logger object for console and/or file output - /// The DupeType corresponding to the relationship between the two - 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; - } - - /// - /// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order) - /// - /// List of RomData objects representing the roms to be sorted - /// True if files are not renamed, false otherwise - /// True if it sorted correctly, false otherwise - public static bool Sort(ref List roms, bool norename) - { - try - { - roms.Sort(delegate (Rom x, Rom y) - { - if (x.Metadata.SystemID == y.Metadata.SystemID) - { - if (x.Metadata.SourceID == y.Metadata.SourceID) - { - if (x.Machine.Name == y.Machine.Name) - { - if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type == ItemType.Rom || y.Type == ItemType.Disk)) - { - 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)); - } - else if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type != ItemType.Rom && y.Type != ItemType.Disk)) - { - return -1; - } - else if ((x.Type != ItemType.Rom && x.Type != ItemType.Disk) && (y.Type == ItemType.Rom || y.Type == ItemType.Disk)) - { - return 1; - } - else - { - 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)); - } - } - return Style.CompareNumeric(x.Machine.Name, y.Machine.Name); - } - return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.Metadata.SourceID - y.Metadata.SourceID); - } - return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.Metadata.SystemID - y.Metadata.SystemID); - }); - return true; - } - catch (Exception) - { - // Absorb the error - return false; - } - } - - #endregion - } -} diff --git a/SabreTools.Helper/Tools/RomToolsHash.cs b/SabreTools.Helper/Tools/RomToolsHash.cs deleted file mode 100644 index 632295c2..00000000 --- a/SabreTools.Helper/Tools/RomToolsHash.cs +++ /dev/null @@ -1,225 +0,0 @@ -using System.Collections.Generic; - -namespace SabreTools.Helper -{ - public class RomToolsHash - { - #region HashData-based sorting and merging - - /// - /// Merge an arbitrary set of ROMs based on the supplied information - /// - /// List of HashData objects representing the roms to be merged - /// Logger object for console and/or file output - /// A List of HashData objects representing the merged roms - public static List Merge(List inhashes, Logger logger) - { - // Create output list - List outroms = new List(); - - // Check for null or blank roms first - if (inhashes == null || inhashes.Count == 0) - { - return outroms; - } - - // Then deduplicate them by checking to see if data matches previous saved roms - foreach (HashData hash in inhashes) - { - // If it's a nodump, add and skip - if (hash.Roms[0].Nodump) - { - outroms.Add(hash); - 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 - DupeType dupetype = DupeType.None; - HashData savedHash = new HashData(); - int pos = -1; - for (int i = 0; i < outroms.Count; i++) - { - HashData lastrom = outroms[i]; - RomData savedRom = savedHash.Roms[0]; - MachineData savedMachine = savedRom.Machine; - - // Get the duplicate status - dupetype = GetDuplicateStatus(hash, lastrom, logger); - - // If it's a duplicate, skip adding it to the output but add any missing information - if (dupetype != DupeType.None) - { - savedHash = lastrom; - pos = i; - - savedHash.CRC = (savedHash.CRC == null && hash.CRC != null ? hash.CRC : savedHash.CRC); - savedHash.MD5 = (savedHash.MD5 == null && hash.MD5 != null ? hash.MD5 : savedHash.MD5); - savedHash.SHA1 = (savedHash.SHA1 == null && hash.SHA1 != null ? hash.SHA1 : savedHash.SHA1); - savedRom.DupeType = dupetype; - - // If the current system has a lower ID than the previous, set the system accordingly - if (hash.Roms[0].Machine.SystemID < savedMachine.SystemID) - { - savedMachine.SystemID = hash.Roms[0].Machine.SystemID; - savedMachine.System = hash.Roms[0].Machine.System; - savedMachine.Name = hash.Roms[0].Machine.Name; - savedRom.Name = hash.Roms[0].Name; - } - - // If the current source has a lower ID than the previous, set the source accordingly - if (hash.Roms[0].Machine.SourceID < savedMachine.SourceID) - { - savedMachine.SourceID = hash.Roms[0].Machine.SourceID; - savedMachine.Source = hash.Roms[0].Machine.Source; - savedMachine.Name = hash.Roms[0].Machine.Name; - savedRom.Name = hash.Roms[0].Name; - } - - break; - } - } - - // If no duplicate is found, add it to the list - if (dupetype == DupeType.None) - { - outroms.Add(hash); - } - // Otherwise, if a new rom information is found, add that - else - { - outroms.RemoveAt(pos); - outroms.Insert(pos, savedHash); - } - } - else - { - outroms.Add(hash); - } - } - - // Then return the result - return outroms; - } - - /* - /// - /// List all duplicates found in a DAT based on a rom - /// - /// Hash to use as a base - /// DAT to match against - /// Logger object for console and/or file output - /// True to remove matched roms from the input, false otherwise (default) - /// List of matched HashData objects - public static List GetDuplicates(HashData lastrom, DatData datdata, Logger logger, bool remove = false) - { - List output = new List(); - - // Check for an empty rom list first - if (datdata.Hashes == null || datdata.Hashes.Count == 0) - { - return output; - } - - // Try to find duplicates - for (int i = 0; i < datdata.Hashes.Count; i++) - { - - List roms = datdata.Files[key]; - List left = new List(); - 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; - } - */ - - /// - /// Determine if a file is a duplicate using partial matching logic - /// - /// Hash to check for duplicate status - /// Hash to use as a baseline - /// Logger object for console and/or file output - /// True if the hashes are duplicates, false otherwise - public static bool IsDuplicate(HashData hash, int hashIndex, HashData lastHash, int lastHashIndex, Logger logger) - { - bool dupefound = hash.Equals(lastHash); - - // More wonderful SHA-1 logging that has to be done - if (hash.SHA1 == lastHash.SHA1 && hash.Size != lastHash.Size) - { - logger.User("SHA-1 mismatch - Hash: " + hash.SHA1); - } - - return dupefound; - } - - /// - /// Return the duplicate status of two hashes - /// - /// Current hash to check - /// Last hash to check against - /// Logger object for console and/or file output - /// The DupeType corresponding to the relationship between the two - public static DupeType GetDuplicateStatus(HashData hash, HashData lasthash, Logger logger) - { - DupeType output = DupeType.None; - - /* - // If we don't have a duplicate at all, return none - if (!IsDuplicate(hash, lasthash, logger)) - { - return output; - } - */ - - // If the duplicate is external already or should be, set it - if (lasthash.Roms[0].DupeType >= DupeType.ExternalHash || lasthash.Roms[0].Machine.SystemID != hash.Roms[0].Machine.SystemID || lasthash.Roms[0].Machine.SourceID != hash.Roms[0].Machine.SourceID) - { - if (lasthash.Roms[0].Machine.Name == hash.Roms[0].Machine.Name && lasthash.Roms[0].Name == hash.Roms[0].Name) - { - output = DupeType.ExternalAll; - } - else - { - output = DupeType.ExternalHash; - } - } - - // Otherwise, it's considered an internal dupe - else - { - if (lasthash.Roms[0].Machine.Name == hash.Roms[0].Machine.Name && lasthash.Roms[0].Name == hash.Roms[0].Name) - { - output = DupeType.InternalAll; - } - else - { - output = DupeType.InternalHash; - } - } - - return output; - } - - #endregion - } -} diff --git a/SabreTools/Partials/SabreTools_Inits.cs b/SabreTools/Partials/SabreTools_Inits.cs index 62ca2fcb..231afd17 100644 --- a/SabreTools/Partials/SabreTools_Inits.cs +++ b/SabreTools/Partials/SabreTools_Inits.cs @@ -140,7 +140,7 @@ namespace SabreTools OutputFormat = (outputFormat == 0 ? OutputFormat.Xml : outputFormat), Romba = romba, Type = (superdat ? "SuperDAT" : ""), - Files = new Dictionary>(), + Files = new Dictionary>(), }; // For each input directory, create a DAT @@ -150,7 +150,7 @@ namespace SabreTools { // Clone the base Dat for information Dat datdata = (Dat)basedat.Clone(); - datdata.Files = new Dictionary>(); + datdata.Files = new Dictionary>(); string basePath = Path.GetFullPath(path); DATFromDir dfd = new DATFromDir(basePath, datdata, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, addBlanks, addDate, tempDir, maxDegreeOfParallelism, _logger); From b549085c345b69f8bbcd0009d013a68a087f8313 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 20:08:25 -0700 Subject: [PATCH 02/13] [ALL] Convert Dat from a struct to an object This effectively doesn't do much for the time being since there's only one "Dat type" that's being used. Realistically, this is probably the best bet since a given DAT should not be restricted to an output type as much as an ItemType is bound to its data. This also removes the experimental classes that won't be in use for forever. More work still might need to be done but it is unknown at this point. --- RombaSharp/Partials/RombaSharp_Helpers.cs | 6 +- RombaSharp/Partials/RombaSharp_Inits.cs | 4 +- SabreTools.Helper/Data/Structs.cs | 278 -------- SabreTools.Helper/Objects/DATFromDir.cs | 6 +- .../DatObjects/DatFile.cs} | 665 +++++++++++++++--- SabreTools.Helper/Objects/Generate.cs | 4 +- SabreTools.Helper/Objects/GenerateTwo.cs | 6 +- SabreTools.Helper/Objects/Import.cs | 4 +- SabreTools.Helper/Objects/SimpleSort.cs | 20 +- SabreTools.Helper/Objects/Stats.cs | 12 +- SabreTools.Helper/SabreTools.Helper.csproj | 3 +- SabreTools.Helper/Skippers/Skippers.cs | 2 +- SabreTools.Helper/Tools/FileToolsHash.cs | 305 -------- SabreTools.Helper/Tools/Style.cs | 4 +- SabreTools/Partials/SabreTools_Inits.cs | 28 +- SimpleSort/SimpleSortApp.cs | 6 +- 16 files changed, 611 insertions(+), 742 deletions(-) rename SabreTools.Helper/{Tools/DatTools.cs => Objects/DatObjects/DatFile.cs} (89%) delete mode 100644 SabreTools.Helper/Tools/FileToolsHash.cs diff --git a/RombaSharp/Partials/RombaSharp_Helpers.cs b/RombaSharp/Partials/RombaSharp_Helpers.cs index 23937a19..18b170de 100644 --- a/RombaSharp/Partials/RombaSharp_Helpers.cs +++ b/RombaSharp/Partials/RombaSharp_Helpers.cs @@ -33,7 +33,7 @@ namespace SabreTools Dictionary> depots = new Dictionary>(); // Get the XML text reader for the configuration file, if possible - XmlTextReader xtr = DatTools.GetXmlTextReader(_config, _logger); + XmlTextReader xtr = DatFile.GetXmlTextReader(_config, _logger); // Now parse the XML file for settings if (xtr != null) @@ -357,8 +357,8 @@ namespace SabreTools foreach (string key in toscan.Keys) { // Parse the Dat if possible - Dat tempdat = new Dat(); - DatTools.Parse(toscan[key], 0, 0, ref tempdat, _logger); + DatFile tempdat = new DatFile(); + DatFile.Parse(toscan[key], 0, 0, ref tempdat, _logger); // If the Dat wasn't empty, add the information if (tempdat.Files.Count != 0) diff --git a/RombaSharp/Partials/RombaSharp_Inits.cs b/RombaSharp/Partials/RombaSharp_Inits.cs index 5e929669..003c0025 100644 --- a/RombaSharp/Partials/RombaSharp_Inits.cs +++ b/RombaSharp/Partials/RombaSharp_Inits.cs @@ -57,7 +57,7 @@ namespace SabreTools private static void InitDir2Dat(List inputs) { // Create a simple Dat output - Dat datdata = new Dat() + DatFile datdata = new DatFile() { FileName = Path.GetFileName(inputs[0]) + " Dir2Dat", Name = Path.GetFileName(inputs[0]) + " Dir2Dat", @@ -73,7 +73,7 @@ namespace SabreTools DATFromDir dfd = new DATFromDir(input, datdata, false /* noMD5 */, false /* noSHA1 */, true /* bare */, false /* archivesAsFiles */, true /* enableGzip */, false /* addBlanks */, false /* addDate */, "__temp__" /* tempDir */, 4 /* maxDegreeOfParallelism */, _logger); dfd.Start(); - DatTools.WriteDatfile(dfd.DatData, "", logger); + DatFile.WriteDatfile(dfd.DatData, "", logger); } logger.Close(); } diff --git a/SabreTools.Helper/Data/Structs.cs b/SabreTools.Helper/Data/Structs.cs index 931132cb..87c3d828 100644 --- a/SabreTools.Helper/Data/Structs.cs +++ b/SabreTools.Helper/Data/Structs.cs @@ -3,154 +3,6 @@ using System.Collections.Generic; namespace SabreTools.Helper { - #region Hash-to-Dat structs [Currently Unused] - - /* Thought experiment - - So, here's a connundrum: Should the internal structure of how DATs work (down to the hash level) mirror - how people see it (Dat to multiple games, game to multiple roms, rom to single hash) or - should it more closely mirror real life (Hash to multiple roms, rom to multiple games, game to single DAT) - - If I use the "how people see it": - Things are pretty much how they are now with redundant data and the like - It makes sense to write things out to file, though. And life is easier when output is easier. - No code changes (big plus!) - - If I use the "how it is": - Less data is likely to be mirrored - Refs to DAT files are possible so that there aren't duplicates - A lot of code will have to change... - */ - - /// - /// Intermediate struct for holding and processing Hash data (NEW SYSTEM) - /// - public struct HashData : IEquatable - { - public long Size; - public byte[] CRC; - public byte[] MD5; - public byte[] SHA1; - public List Roms; - - public bool Equals(HashData other) - { - return this.Equals(other, false); - } - - public bool Equals(HashData other, bool IsDisk) - { - bool equals = false; - - if (!IsDisk && - ((this.MD5 == null || other.MD5 == null) || this.MD5 == other.MD5) && - ((this.SHA1 == null || other.SHA1 == null) || this.SHA1 == other.SHA1)) - { - equals = true; - } - else if (!IsDisk && - (this.Size == other.Size) && - ((this.CRC == null || other.CRC != null) || this.CRC == other.CRC) && - ((this.MD5 == null || other.MD5 == null) || this.MD5 == other.MD5) && - ((this.SHA1 == null || other.SHA1 == null) || this.SHA1 == other.SHA1)) - { - equals = true; - } - - return equals; - } - } - - /// - /// Intermediate struct for holding and processing Rom data (NEW SYSTEM) - /// - public struct RomData - { - public string Name; - public ItemType Type; - public bool Nodump; - public string Date; - public DupeType DupeType; - public MachineData Machine; - } - - /// - /// Intermediate struct for holding and processing Game/Machine data (NEW SYSTEM) - /// - public struct MachineData - { - // Data specific to Machine/Game - public string Name; - public string Comment; - public string Description; - public string Year; - public string Manufacturer; - public string RomOf; - public string CloneOf; - public string SampleOf; - public string SourceFile; - public bool IsBios; - public string Board; - public string RebuildTo; - public bool TorrentZipped; - - // Data specific to the source of the Machine/Game - public int SystemID; - public string System; - public int SourceID; - public string Source; - } - - /// - /// Intermediate struct for holding and processing DAT data (NEW SYSTEM) - /// - public struct DatData - { - // Data common to most DAT types - public string FileName; - public string Name; - public string Description; - public string RootDir; - public string Category; - public string Version; - public string Date; - public string Author; - public string Email; - public string Homepage; - public string Url; - public string Comment; - public string Header; - public string Type; // Generally only used for SuperDAT - public ForceMerging ForceMerging; - public ForceNodump ForceNodump; - public ForcePacking ForcePacking; - public OutputFormat OutputFormat; - public bool MergeRoms; - public List Hashes; - - // Data specific to the Miss DAT type - public bool UseGame; - public string Prefix; - public string Postfix; - public bool Quotes; - public string RepExt; - public string AddExt; - public bool GameName; - public bool Romba; - public bool? XSV; // true for tab-deliminated output, false for comma-deliminated output - - // Statistical data related to the DAT - public long RomCount; - public long DiskCount; - public long TotalSize; - public long CRCCount; - public long MD5Count; - public long SHA1Count; - public long NodumpCount; - } - - #endregion - #region Dat-to-Hash structs /// @@ -191,136 +43,6 @@ namespace SabreTools.Helper } } - /// - /// Intermediate struct for holding DAT information - /// - public struct Dat : ICloneable - { - // Data common to most DAT types - public string FileName; - public string Name; - public string Description; - public string RootDir; - public string Category; - public string Version; - public string Date; - public string Author; - public string Email; - public string Homepage; - public string Url; - public string Comment; - public string Header; - public string Type; // Generally only used for SuperDAT - public ForceMerging ForceMerging; - public ForceNodump ForceNodump; - public ForcePacking ForcePacking; - public OutputFormat OutputFormat; - public bool MergeRoms; - public Dictionary> Files; - - // Data specific to the Miss DAT type - public bool UseGame; - public string Prefix; - public string Postfix; - public bool Quotes; - public string RepExt; - public string AddExt; - public bool RemExt; - public bool GameName; - public bool Romba; - public bool? XSV; // true for tab-deliminated output, false for comma-deliminated output - - // Statistical data related to the DAT - public long RomCount; - public long DiskCount; - public long TotalSize; - public long CRCCount; - public long MD5Count; - public long SHA1Count; - public long NodumpCount; - - public object Clone() - { - return new Dat - { - FileName = this.FileName, - Name = this.Name, - Description = this.Description, - RootDir = this.RootDir, - Category = this.Category, - Version = this.Version, - Date = this.Date, - Author = this.Author, - Email = this.Email, - Homepage = this.Homepage, - Url = this.Url, - Comment = this.Comment, - Header = this.Header, - Type = this.Type, - ForceMerging = this.ForceMerging, - ForceNodump = this.ForceNodump, - ForcePacking = this.ForcePacking, - OutputFormat = this.OutputFormat, - MergeRoms = this.MergeRoms, - Files = this.Files, - UseGame = this.UseGame, - Prefix = this.Prefix, - Postfix = this.Postfix, - Quotes = this.Quotes, - RepExt = this.RepExt, - AddExt = this.AddExt, - RemExt = this.RemExt, - GameName = this.GameName, - Romba = this.Romba, - XSV = this.XSV, - RomCount = this.RomCount, - DiskCount = this.DiskCount, - TotalSize = this.TotalSize, - CRCCount = this.CRCCount, - MD5Count = this.MD5Count, - SHA1Count = this.SHA1Count, - NodumpCount = this.NodumpCount, - }; - } - - public object CloneHeader() - { - return new Dat - { - FileName = this.FileName, - Name = this.Name, - Description = this.Description, - RootDir = this.RootDir, - Category = this.Category, - Version = this.Version, - Date = this.Date, - Author = this.Author, - Email = this.Email, - Homepage = this.Homepage, - Url = this.Url, - Comment = this.Comment, - Header = this.Header, - Type = this.Type, - ForceMerging = this.ForceMerging, - ForceNodump = this.ForceNodump, - ForcePacking = this.ForcePacking, - OutputFormat = this.OutputFormat, - MergeRoms = this.MergeRoms, - Files = new Dictionary>(), - UseGame = this.UseGame, - Prefix = this.Prefix, - Postfix = this.Postfix, - Quotes = this.Quotes, - RepExt = this.RepExt, - AddExt = this.AddExt, - RemExt = this.RemExt, - GameName = this.GameName, - Romba = this.Romba, - XSV = this.XSV, - }; - } - } - #endregion #region Skipper structs diff --git a/SabreTools.Helper/Objects/DATFromDir.cs b/SabreTools.Helper/Objects/DATFromDir.cs index fb338823..04570e2e 100644 --- a/SabreTools.Helper/Objects/DATFromDir.cs +++ b/SabreTools.Helper/Objects/DATFromDir.cs @@ -18,7 +18,7 @@ namespace SabreTools private string _tempDir; // User specified inputs - private Dat _datdata; + private DatFile _datdata; private bool _noMD5; private bool _noSHA1; private bool _bare; @@ -32,7 +32,7 @@ namespace SabreTools private Logger _logger; // Public variables - public Dat DatData + public DatFile DatData { get { return _datdata; } } @@ -52,7 +52,7 @@ namespace SabreTools /// Name of the directory to create a temp folder in (blank is current directory) /// Integer representing the maximum amount of parallelization to be used /// Logger object for console and file output - public DATFromDir(string basePath, Dat datdata, bool noMD5, bool noSHA1, bool bare, bool archivesAsFiles, + public DATFromDir(string basePath, DatFile datdata, bool noMD5, bool noSHA1, bool bare, bool archivesAsFiles, bool enableGzip, bool addBlanks, bool addDate, string tempDir, int maxDegreeOfParallelism, Logger logger) { _basePath = Path.GetFullPath(basePath); diff --git a/SabreTools.Helper/Tools/DatTools.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs similarity index 89% rename from SabreTools.Helper/Tools/DatTools.cs rename to SabreTools.Helper/Objects/DatObjects/DatFile.cs index 13d071c0..92f86236 100644 --- a/SabreTools.Helper/Tools/DatTools.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -10,11 +10,464 @@ using System.Xml; namespace SabreTools.Helper { - /// - /// DAT manipulation tools that rely on Rom and related structs - /// - public class DatTools + public class DatFile : ICloneable { + #region Private instance variables + + // Data common to most DAT types + private string _fileName; + private string _name; + private string _description; + private string _rootDir; + private string _category; + private string _version; + private string _date; + private string _author; + private string _email; + private string _homepage; + private string _url; + private string _comment; + private string _header; + private string _type; // Generally only used for SuperDAT + private ForceMerging _forceMerging; + private ForceNodump _forceNodump; + private ForcePacking _forcePacking; + private OutputFormat _outputFormat; + private bool _mergeRoms; + private Dictionary> _files; + + // Data specific to the Miss DAT type + private bool _useGame; + private string _prefix; + private string _postfix; + private bool _quotes; + private string _repExt; + private string _addExt; + private bool _remExt; + private bool _gameName; + private bool _romba; + private bool? _xsv; // true for tab-deliminated output, false for comma-deliminated output + + // Statistical data related to the DAT + private long _romCount; + private long _diskCount; + private long _totalSize; + private long _crcCount; + private long _md5Count; + private long _sha1Count; + private long _nodumpCount; + + #endregion + + #region Publicly facing variables + + // Data common to most DAT types + public string FileName + { + get { return _fileName; } + set { _fileName = value; } + } + public string Name + { + get { return _name; } + set { _name = value; } + } + public string Description + { + get { return _description; } + set { _description = value; } + } + public string RootDir + { + get { return _rootDir; } + set { _rootDir = value; } + } + public string Category + { + get { return _category; } + set { _category = value; } + } + public string Version + { + get { return _version; } + set { _version = value; } + } + public string Date + { + get { return _date; } + set { _date = value; } + } + public string Author + { + get { return _author; } + set { _author = value; } + } + public string Email + { + get { return _email; } + set { _email = value; } + } + public string Homepage + { + get { return _homepage; } + set { _homepage = value; } + } + public string Url + { + get { return _url; } + set { _url = value; } + } + public string Comment + { + get { return _comment; } + set { _comment = value; } + } + public string Header + { + get { return _header; } + set { _header = value; } + } + public string Type // Generally only used for SuperDAT + { + get { return _type; } + set { _type = value; } + } + public ForceMerging ForceMerging + { + get { return _forceMerging; } + set { _forceMerging = value; } + } + public ForceNodump ForceNodump + { + get { return _forceNodump; } + set { _forceNodump = value; } + } + public ForcePacking ForcePacking + { + get { return _forcePacking; } + set { _forcePacking = value; } + } + public OutputFormat OutputFormat + { + get { return _outputFormat; } + set { _outputFormat = value; } + } + public bool MergeRoms + { + get { return _mergeRoms; } + set { _mergeRoms = value; } + } + public Dictionary> Files + { + get + { + if (_files == null) + { + _files = new Dictionary>(); + } + return _files; + } + set + { + _files = value; + } + } + + // Data specific to the Miss DAT type + public bool UseGame + { + get { return _useGame; } + set { _useGame = value; } + } + public string Prefix + { + get { return _prefix; } + set { _prefix = value; } + } + public string Postfix + { + get { return _postfix; } + set { _postfix = value; } + } + public bool Quotes + { + get { return _quotes; } + set { _quotes = value; } + } + public string RepExt + { + get { return _repExt; } + set { _repExt = value; } + } + public string AddExt + { + get { return _addExt; } + set { _addExt = value; } + } + public bool RemExt + { + get { return _remExt; } + set { _remExt = value; } + } + public bool GameName + { + get { return _gameName; } + set { _gameName = value; } + } + public bool Romba + { + get { return _romba; } + set { _romba = value; } + } + public bool? XSV // true for tab-deliminated output, false for comma-deliminated output + { + get { return _xsv; } + set { _xsv = value; } + } + + // Statistical data related to the DAT + public long RomCount + { + get { return _romCount; } + set { _romCount = value; } + } + public long DiskCount + { + get { return _diskCount; } + set { _diskCount = value; } + } + public long TotalSize + { + get { return _totalSize; } + set { _totalSize = value; } + } + public long CRCCount + { + get { return _crcCount; } + set { _crcCount = value; } + } + public long MD5Count + { + get { return _md5Count; } + set { _md5Count = value; } + } + public long SHA1Count + { + get { return _sha1Count; } + set { _sha1Count = value; } + } + public long NodumpCount + { + get { return _nodumpCount; } + set { _nodumpCount = value; } + } + + #endregion + + #region Constructors + + /// + /// Create a default, empty Dat object + /// + public DatFile() + { + // Nothing needs to be done + } + + /// + /// Create a new Dat object with the included information (standard Dats) + /// + /// New filename + /// New name + /// New description + /// New rootdir + /// New category + /// New version + /// New date + /// New author + /// New email + /// New homepage + /// New URL + /// New comment + /// New header + /// True to set SuperDAT type, false otherwise + /// None, Split, Full + /// None, Obsolete, Required, Ignore + /// None, Zip, Unzip + /// Non-zero flag for output format, zero otherwise for default + /// True to dedupe the roms in the DAT, false otherwise (default) + /// Dictionary of lists of DatItem objects + public DatFile(string fileName, string name, string description, string rootDir, string category, string version, string date, + string author, string email, string homepage, string url, string comment, string header, string type, ForceMerging forceMerging, + ForceNodump forceNodump, ForcePacking forcePacking, OutputFormat outputFormat, bool mergeRoms, Dictionary> files) + { + _fileName = fileName; + _name = name; + _description = description; + _rootDir = rootDir; + _category = category; + _version = version; + _date = date; + _author = author; + _email = email; + _homepage = homepage; + _url = url; + _comment = comment; + _header = header; + _type = type; + _forceMerging = forceMerging; + _forceNodump = forceNodump; + _forcePacking = forcePacking; + _outputFormat = outputFormat; + _mergeRoms = mergeRoms; + _files = files; + + _romCount = 0; + _diskCount = 0; + _totalSize = 0; + _crcCount = 0; + _md5Count = 0; + _sha1Count = 0; + _nodumpCount = 0; + } + + /// + /// Create a new Dat object with the included information (missfile) + /// + /// New filename + /// New name + /// New description + /// Non-zero flag for output format, zero otherwise for default + /// True to dedupe the roms in the DAT, false otherwise (default) + /// Dictionary of lists of DatItem objects + /// True if games are to be used in output, false if roms are + /// Generic prefix to be added to each line + /// Generic postfix to be added to each line + /// Add quotes to each item + /// Replace all extensions with another + /// Add an extension to all items + /// Remove all extensions + /// Add the dat name as a directory prefix + /// Output files in romba format + /// True to output files in TSV format, false to output files in CSV format, null otherwise + public DatFile(string fileName, string name, string description, OutputFormat outputFormat, bool mergeRoms, + Dictionary> files, bool useGame, string prefix, string postfix, bool quotes, + string repExt, string addExt, bool remExt, bool gameName, bool romba, bool? xsv) + { + _fileName = fileName; + _name = name; + _description = description; + _outputFormat = outputFormat; + _mergeRoms = mergeRoms; + _files = files; + + _useGame = useGame; + _prefix = prefix; + _postfix = postfix; + _quotes = quotes; + _repExt = repExt; + _addExt = addExt; + _remExt = remExt; + _gameName = gameName; + _romba = romba; + _xsv = xsv; + + _romCount = 0; + _diskCount = 0; + _totalSize = 0; + _crcCount = 0; + _md5Count = 0; + _sha1Count = 0; + _nodumpCount = 0; + } + + #endregion + + #region Cloning Methods + + public object Clone() + { + return new DatFile + { + FileName = this.FileName, + Name = this.Name, + Description = this.Description, + RootDir = this.RootDir, + Category = this.Category, + Version = this.Version, + Date = this.Date, + Author = this.Author, + Email = this.Email, + Homepage = this.Homepage, + Url = this.Url, + Comment = this.Comment, + Header = this.Header, + Type = this.Type, + ForceMerging = this.ForceMerging, + ForceNodump = this.ForceNodump, + ForcePacking = this.ForcePacking, + OutputFormat = this.OutputFormat, + MergeRoms = this.MergeRoms, + Files = this.Files, + UseGame = this.UseGame, + Prefix = this.Prefix, + Postfix = this.Postfix, + Quotes = this.Quotes, + RepExt = this.RepExt, + AddExt = this.AddExt, + RemExt = this.RemExt, + GameName = this.GameName, + Romba = this.Romba, + XSV = this.XSV, + RomCount = this.RomCount, + DiskCount = this.DiskCount, + TotalSize = this.TotalSize, + CRCCount = this.CRCCount, + MD5Count = this.MD5Count, + SHA1Count = this.SHA1Count, + NodumpCount = this.NodumpCount, + }; + } + + public object CloneHeader() + { + return new DatFile + { + FileName = this.FileName, + Name = this.Name, + Description = this.Description, + RootDir = this.RootDir, + Category = this.Category, + Version = this.Version, + Date = this.Date, + Author = this.Author, + Email = this.Email, + Homepage = this.Homepage, + Url = this.Url, + Comment = this.Comment, + Header = this.Header, + Type = this.Type, + ForceMerging = this.ForceMerging, + ForceNodump = this.ForceNodump, + ForcePacking = this.ForcePacking, + OutputFormat = this.OutputFormat, + MergeRoms = this.MergeRoms, + Files = new Dictionary>(), + UseGame = this.UseGame, + Prefix = this.Prefix, + Postfix = this.Postfix, + Quotes = this.Quotes, + RepExt = this.RepExt, + AddExt = this.AddExt, + RemExt = this.RemExt, + GameName = this.GameName, + Romba = this.Romba, + XSV = this.XSV, + }; + } + + #endregion + #region DAT Parsing /// @@ -103,7 +556,7 @@ namespace SabreTools.Helper /// True if game names are sanitized, false otherwise (default) /// True if SL XML names should be kept, false otherwise (default) /// True if original extension should be kept, false otherwise (default) - public static void Parse(string filename, int sysid, int srcid, ref Dat datdata, Logger logger, bool keep = false, bool clean = false, bool softlist = false, bool keepext = false) + public static void Parse(string filename, int sysid, int srcid, ref DatFile datdata, Logger logger, bool keep = false, bool clean = false, bool softlist = false, bool keepext = false) { Parse(filename, sysid, srcid, ref datdata, null, null, null, -1, -1, -1, null, null, null, null, false, false, "", logger, keep, clean, softlist, keepext); } @@ -138,7 +591,7 @@ namespace SabreTools.Helper string filename, int sysid, int srcid, - ref Dat datdata, + ref DatFile datdata, // Rom filtering string gamename, @@ -229,7 +682,7 @@ namespace SabreTools.Helper string filename, int sysid, int srcid, - ref Dat datdata, + ref DatFile datdata, // Rom filtering string gamename, @@ -674,7 +1127,7 @@ namespace SabreTools.Helper string filename, int sysid, int srcid, - ref Dat datdata, + ref DatFile datdata, // Rom filtering string gamename, @@ -840,7 +1293,7 @@ namespace SabreTools.Helper string filename, int sysid, int srcid, - ref Dat datdata, + ref DatFile datdata, // Rom filtering string gamename, @@ -1574,7 +2027,7 @@ namespace SabreTools.Helper /// True if all games should be replaced by '!', false otherwise /// String representing root directory to compare against for length calculation /// Logger object for console and/or file output - private static void ParseAddHelper(DatItem item, ref Dat datdata, string gamename, string romname, string romtype, long sgt, long slt, + private static void ParseAddHelper(DatItem item, ref DatFile datdata, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, bool? nodump, bool trim, bool single, string root, bool clean, Logger logger, out string key) { key = ""; @@ -1805,7 +2258,7 @@ namespace SabreTools.Helper { logger.User("A total of " + count + " file hashes will be written out to file"); } - + return sortable; } @@ -1843,7 +2296,7 @@ namespace SabreTools.Helper /// String representing root directory to compare against for length calculation /// Integer representing the maximum amount of parallelization to be used /// Logging object for console and file output - public static void Update(List inputFileNames, Dat datdata, OutputFormat outputFormat, string outDir, bool merge, + public static void Update(List inputFileNames, DatFile datdata, OutputFormat outputFormat, string outDir, bool merge, DiffMode diff, bool? cascade, bool inplace, bool skip, bool bare, bool clean, bool softlist, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, bool? nodump, bool trim, bool single, string root, int maxDegreeOfParallelism, Logger logger) @@ -1897,8 +2350,8 @@ namespace SabreTools.Helper } // Create a dictionary of all ROMs from the input DATs - Dat userData; - List datHeaders = PopulateUserData(newInputFileNames, inplace, clean, softlist, + DatFile userData; + List datHeaders = PopulateUserData(newInputFileNames, inplace, clean, softlist, outDir, datdata, out userData, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, maxDegreeOfParallelism, logger); @@ -1924,53 +2377,53 @@ namespace SabreTools.Helper Parallel.ForEach(inputFileNames, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, inputFileName => - { - // Clean the input string - if (inputFileName != "") { - inputFileName = Path.GetFullPath(inputFileName); - } - - if (File.Exists(inputFileName)) - { - Dat innerDatdata = (Dat)datdata.CloneHeader(); - logger.User("Processing \"" + Path.GetFileName(inputFileName) + "\""); - Parse(inputFileName, 0, 0, ref innerDatdata, gamename, romname, - romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, - root, logger, true, clean, softlist, keepext: (innerDatdata.XSV != null)); - - // If we have roms, output them - if (innerDatdata.Files.Count != 0) + // Clean the input string + if (inputFileName != "") { - WriteDatfile(innerDatdata, (outDir == "" ? Path.GetDirectoryName(inputFileName) : outDir), logger, overwrite: (outDir != "")); + inputFileName = Path.GetFullPath(inputFileName); } - } - else if (Directory.Exists(inputFileName)) - { - inputFileName = Path.GetFullPath(inputFileName) + Path.DirectorySeparatorChar; - Parallel.ForEach(Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories), - new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, - file => + if (File.Exists(inputFileName)) { - logger.User("Processing \"" + Path.GetFullPath(file).Remove(0, inputFileName.Length) + "\""); - Dat innerDatdata = (Dat)datdata.Clone(); - innerDatdata.Files = null; - Parse(file, 0, 0, ref innerDatdata, gamename, romname, romtype, sgt, - slt, seq, crc, md5, sha1, nodump, trim, single, root, logger, true, clean, keepext: (datdata.XSV != null)); + DatFile innerDatdata = (DatFile)datdata.CloneHeader(); + logger.User("Processing \"" + Path.GetFileName(inputFileName) + "\""); + Parse(inputFileName, 0, 0, ref innerDatdata, gamename, romname, + romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, + root, logger, true, clean, softlist, keepext: (innerDatdata.XSV != null)); // If we have roms, output them - if (innerDatdata.Files != null && innerDatdata.Files.Count != 0) + if (innerDatdata.Files.Count != 0) { - WriteDatfile(innerDatdata, (outDir == "" ? Path.GetDirectoryName(file) : outDir + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), logger, overwrite: (outDir != "")); + WriteDatfile(innerDatdata, (outDir == "" ? Path.GetDirectoryName(inputFileName) : outDir), logger, overwrite: (outDir != "")); } - }); - } - else - { - logger.Error("I'm sorry but " + inputFileName + " doesn't exist!"); - } - }); + } + else if (Directory.Exists(inputFileName)) + { + inputFileName = Path.GetFullPath(inputFileName) + Path.DirectorySeparatorChar; + + Parallel.ForEach(Directory.EnumerateFiles(inputFileName, "*", SearchOption.AllDirectories), + new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, + file => + { + logger.User("Processing \"" + Path.GetFullPath(file).Remove(0, inputFileName.Length) + "\""); + DatFile innerDatdata = (DatFile)datdata.Clone(); + innerDatdata.Files = null; + Parse(file, 0, 0, ref innerDatdata, gamename, romname, romtype, sgt, + slt, seq, crc, md5, sha1, nodump, trim, single, root, logger, true, clean, keepext: (datdata.XSV != null)); + + // If we have roms, output them + if (innerDatdata.Files != null && innerDatdata.Files.Count != 0) + { + WriteDatfile(innerDatdata, (outDir == "" ? Path.GetDirectoryName(file) : outDir + Path.GetDirectoryName(file).Remove(0, inputFileName.Length - 1)), logger, overwrite: (outDir != "")); + } + }); + } + else + { + logger.Error("I'm sorry but " + inputFileName + " doesn't exist!"); + } + }); } return; } @@ -1995,11 +2448,11 @@ namespace SabreTools.Helper /// Integer representing the maximum amount of parallelization to be used /// Logging object for console and file output /// List of DatData objects representing headers - private static List PopulateUserData(List inputs, bool inplace, bool clean, bool softlist, string outDir, - Dat inputDat, out Dat userData, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, + private static List PopulateUserData(List inputs, bool inplace, bool clean, bool softlist, string outDir, + DatFile inputDat, out DatFile userData, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, bool? nodump, bool trim, bool single, string root, int maxDegreeOfParallelism, Logger logger) { - Dat[] datHeaders = new Dat[inputs.Count]; + DatFile[] datHeaders = new DatFile[inputs.Count]; DateTime start = DateTime.Now; logger.User("Processing individual DATs"); @@ -2007,24 +2460,24 @@ namespace SabreTools.Helper inputs.Count, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, i => - { - string input = inputs[i]; - logger.User("Adding DAT: " + input.Split('¬')[0]); - datHeaders[i] = new Dat { - OutputFormat = (inputDat.OutputFormat != 0 ? inputDat.OutputFormat: 0), - Files = new Dictionary>(), - MergeRoms = inputDat.MergeRoms, - }; + string input = inputs[i]; + logger.User("Adding DAT: " + input.Split('¬')[0]); + datHeaders[i] = new DatFile + { + OutputFormat = (inputDat.OutputFormat != 0 ? inputDat.OutputFormat : 0), + Files = new Dictionary>(), + MergeRoms = inputDat.MergeRoms, + }; - Parse(input.Split('¬')[0], i, 0, ref datHeaders[i], gamename, romname, romtype, sgt, slt, seq, - crc, md5, sha1, nodump, trim, single, root, logger, true, clean, softlist); - }); + Parse(input.Split('¬')[0], i, 0, ref datHeaders[i], gamename, romname, romtype, sgt, slt, seq, + crc, md5, sha1, nodump, trim, single, root, logger, true, clean, softlist); + }); logger.User("Processing complete in " + DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.fffff")); logger.User("Populating internal DAT"); - userData = (Dat)inputDat.CloneHeader(); + userData = (DatFile)inputDat.CloneHeader(); userData.Files = new Dictionary>(); for (int i = 0; i < inputs.Count; i++) { @@ -2057,21 +2510,21 @@ namespace SabreTools.Helper /// Main DatData to draw information from /// List of inputs to write out from /// Logging object for console and file output - public static void DiffNoCascade(DiffMode diff, string outDir, Dat userData, List inputs, Logger logger) + public static void DiffNoCascade(DiffMode diff, string outDir, DatFile userData, List inputs, Logger logger) { DateTime start = DateTime.Now; logger.User("Initializing all output DATs"); // Default vars for use string post = ""; - Dat outerDiffData = new Dat(); - Dat dupeData = new Dat(); + DatFile outerDiffData = new DatFile(); + DatFile dupeData = new DatFile(); // Don't have External dupes if ((diff & DiffMode.NoDupes) != 0) { post = " (No Duplicates)"; - outerDiffData = (Dat)userData.CloneHeader(); + outerDiffData = (DatFile)userData.CloneHeader(); outerDiffData.FileName += post; outerDiffData.Name += post; outerDiffData.Description += post; @@ -2082,7 +2535,7 @@ namespace SabreTools.Helper if ((diff & DiffMode.Dupes) != 0) { post = " (Duplicates)"; - dupeData = (Dat)userData.CloneHeader(); + dupeData = (DatFile)userData.CloneHeader(); dupeData.FileName += post; dupeData.Name += post; dupeData.Description += post; @@ -2090,17 +2543,17 @@ namespace SabreTools.Helper } // Create a list of DatData objects representing individual output files - List outDats = new List(); + List outDats = new List(); // Loop through each of the inputs and get or create a new DatData object if ((diff & DiffMode.Individuals) != 0) { - Dat[] outDatsArray = new Dat[inputs.Count]; + DatFile[] outDatsArray = new DatFile[inputs.Count]; Parallel.For(0, inputs.Count, j => { string innerpost = " (" + Path.GetFileNameWithoutExtension(inputs[j].Split('¬')[0]) + " Only)"; - Dat diffData = (Dat)userData.CloneHeader(); + DatFile diffData = (DatFile)userData.CloneHeader(); diffData.FileName += innerpost; diffData.Name += innerpost; diffData.Description += innerpost; @@ -2233,23 +2686,23 @@ namespace SabreTools.Helper /// Dat headers used optionally /// True if the first cascaded diff file should be skipped on output, false otherwise /// Logging object for console and file output - public static void DiffCascade(string outDir, bool inplace, Dat userData, List inputs, List datHeaders, bool skip, Logger logger) + public static void DiffCascade(string outDir, bool inplace, DatFile userData, List inputs, List datHeaders, bool skip, Logger logger) { string post = ""; // Create a list of DatData objects representing output files - List outDats = new List(); + List outDats = new List(); // Loop through each of the inputs and get or create a new DatData object DateTime start = DateTime.Now; logger.User("Initializing all output DATs"); - Dat[] outDatsArray = new Dat[inputs.Count]; + DatFile[] outDatsArray = new DatFile[inputs.Count]; Parallel.For(0, inputs.Count, j => { string innerpost = " (" + Path.GetFileNameWithoutExtension(inputs[j].Split('¬')[0]) + " Only)"; - Dat diffData; + DatFile diffData; // If we're in inplace mode, take the appropriate DatData object already stored if (inplace || !String.IsNullOrEmpty(outDir)) @@ -2258,7 +2711,7 @@ namespace SabreTools.Helper } else { - diffData = (Dat)userData.CloneHeader(); + diffData = (DatFile)userData.CloneHeader(); diffData.FileName += post; diffData.Name += post; diffData.Description += post; @@ -2278,7 +2731,7 @@ namespace SabreTools.Helper foreach (string key in keys) { - List< DatItem> roms = DatItem.Merge(userData.Files[key], logger); + List roms = DatItem.Merge(userData.Files[key], logger); if (roms != null && roms.Count > 0) { @@ -2332,7 +2785,7 @@ namespace SabreTools.Helper /// Main DatData to draw information from /// Dat headers used optionally /// Logging object for console and file output - public static void MergeNoDiff(string outDir, Dat userData, List inputs, List datHeaders, Logger logger) + public static void MergeNoDiff(string outDir, DatFile userData, List inputs, List datHeaders, Logger logger) { // If we're in SuperDAT mode, prefix all games with their respective DATs if (userData.Type == "SuperDAT") @@ -2384,7 +2837,7 @@ namespace SabreTools.Helper /// The following features have been requested for file output: /// - Have the ability to strip special (non-ASCII) characters from rom information /// - public static bool WriteDatfile(Dat datdata, string outDir, Logger logger, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true) + public static bool WriteDatfile(DatFile datdata, string outDir, Logger logger, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true) { // If there's nothing there, abort if (datdata.Files == null || datdata.Files.Count == 0) @@ -2571,7 +3024,7 @@ namespace SabreTools.Helper /// DatData object representing DAT information /// Logger object for file and console output /// True if the data was written, false on error - public static bool WriteHeader(StreamWriter sw, OutputFormat outputFormat, Dat datdata, Logger logger) + public static bool WriteHeader(StreamWriter sw, OutputFormat outputFormat, DatFile datdata, Logger logger) { try { @@ -2668,7 +3121,7 @@ namespace SabreTools.Helper (!String.IsNullOrEmpty(datdata.Comment) ? "\t\t" + HttpUtility.HtmlEncode(datdata.Comment) + "\n" : "") + (!String.IsNullOrEmpty(datdata.Type) ? "\t\t" + HttpUtility.HtmlEncode(datdata.Type) + "\n" : "") + (datdata.ForcePacking != ForcePacking.None || datdata.ForceMerging != ForceMerging.None || datdata.ForceNodump != ForceNodump.None ? - "\t\tLogger object for file and console output /// True if blank roms should be skipped on output, false otherwise (default) /// True if the data was written, false on error - public static bool WriteRomData(StreamWriter sw, OutputFormat outputFormat, DatItem rom, string lastgame, Dat datdata, int depth, Logger logger, bool ignoreblanks = false) + public static bool WriteRomData(StreamWriter sw, OutputFormat outputFormat, DatItem rom, string lastgame, DatFile datdata, int depth, Logger logger, bool ignoreblanks = false) { // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip if (ignoreblanks @@ -2914,7 +3367,7 @@ namespace SabreTools.Helper + " )\n"; break; } - + break; case OutputFormat.MissFile: // Missfile should only output Rom and Disk @@ -2998,7 +3451,7 @@ namespace SabreTools.Helper { string inline = "\"" + datdata.FileName + "\"" + separator + "\"" + datdata.Name + "\"" - + separator + "\"" + datdata.Description+ "\"" + + separator + "\"" + datdata.Description + "\"" + separator + "\"" + rom.MachineName + "\"" + separator + "\"" + rom.MachineDescription + "\"" + separator + "\"rom\"" @@ -3110,7 +3563,7 @@ namespace SabreTools.Helper "¬" + HttpUtility.HtmlEncode(rom.Name) + "¬¬¬¬¬\n"; } - + break; case OutputFormat.SabreDat: string prefix = ""; @@ -3241,7 +3694,7 @@ namespace SabreTools.Helper /// Current depth to output file at (SabreDAT only) /// Logger object for file and console output /// True if the data was written, false on error - public static bool WriteFooter(StreamWriter sw, OutputFormat outputFormat, Dat datdata, int depth, Logger logger) + public static bool WriteFooter(StreamWriter sw, OutputFormat outputFormat, DatFile datdata, int depth, Logger logger) { try { @@ -3337,11 +3790,11 @@ namespace SabreTools.Helper } // Get the file data to be split - Dat datdata = new Dat(); - Parse(filename, 0, 0, ref datdata, logger, softlist:true); + DatFile datdata = new DatFile(); + Parse(filename, 0, 0, ref datdata, logger, softlist: true); // Set all of the appropriate outputs for each of the subsets - Dat datdataA = new Dat + DatFile datdataA = new DatFile { FileName = datdata.FileName + " (" + newExtAString + ")", Name = datdata.Name + " (" + newExtAString + ")", @@ -3357,7 +3810,7 @@ namespace SabreTools.Helper Files = new Dictionary>(), OutputFormat = outputFormat, }; - Dat datdataB = new Dat + DatFile datdataB = new DatFile { FileName = datdata.FileName + " (" + newExtBString + ")", Name = datdata.Name + " (" + newExtBString + ")", @@ -3475,12 +3928,12 @@ namespace SabreTools.Helper } // Get the file data to be split - Dat datdata = new Dat(); - Parse(filename, 0, 0, ref datdata, logger, true, softlist:true); + DatFile datdata = new DatFile(); + Parse(filename, 0, 0, ref datdata, logger, true, softlist: true); // Create each of the respective output DATs logger.User("Creating and populating new DATs"); - Dat nodump = new Dat + DatFile nodump = new DatFile { FileName = datdata.FileName + " (Nodump)", Name = datdata.Name + " (Nodump)", @@ -3502,7 +3955,7 @@ namespace SabreTools.Helper MergeRoms = datdata.MergeRoms, Files = new Dictionary>(), }; - Dat sha1 = new Dat + DatFile sha1 = new DatFile { FileName = datdata.FileName + " (SHA-1)", Name = datdata.Name + " (SHA-1)", @@ -3524,7 +3977,7 @@ namespace SabreTools.Helper MergeRoms = datdata.MergeRoms, Files = new Dictionary>(), }; - Dat md5 = new Dat + DatFile md5 = new DatFile { FileName = datdata.FileName + " (MD5)", Name = datdata.Name + " (MD5)", @@ -3546,7 +3999,7 @@ namespace SabreTools.Helper MergeRoms = datdata.MergeRoms, Files = new Dictionary>(), }; - Dat crc = new Dat + DatFile crc = new DatFile { FileName = datdata.FileName + " (CRC)", Name = datdata.Name + " (CRC)", @@ -3569,7 +4022,7 @@ namespace SabreTools.Helper Files = new Dictionary>(), }; - Dat other = new Dat + DatFile other = new DatFile { FileName = datdata.FileName + " (Other)", Name = datdata.Name + " (Other)", @@ -3735,12 +4188,12 @@ namespace SabreTools.Helper } // Get the file data to be split - Dat datdata = new Dat(); - Parse(filename, 0, 0, ref datdata, logger, true, softlist:true); + DatFile datdata = new DatFile(); + Parse(filename, 0, 0, ref datdata, logger, true, softlist: true); // Create each of the respective output DATs logger.User("Creating and populating new DATs"); - Dat romdat = new Dat + DatFile romdat = new DatFile { FileName = datdata.FileName + " (ROM)", Name = datdata.Name + " (ROM)", @@ -3762,7 +4215,7 @@ namespace SabreTools.Helper MergeRoms = datdata.MergeRoms, Files = new Dictionary>(), }; - Dat diskdat = new Dat + DatFile diskdat = new DatFile { FileName = datdata.FileName + " (Disk)", Name = datdata.Name + " (Disk)", @@ -3784,7 +4237,7 @@ namespace SabreTools.Helper MergeRoms = datdata.MergeRoms, Files = new Dictionary>(), }; - Dat sampledat = new Dat + DatFile sampledat = new DatFile { FileName = datdata.FileName + " (Sample)", Name = datdata.Name + " (Sample)", diff --git a/SabreTools.Helper/Objects/Generate.cs b/SabreTools.Helper/Objects/Generate.cs index 046695c1..72f9d41e 100644 --- a/SabreTools.Helper/Objects/Generate.cs +++ b/SabreTools.Helper/Objects/Generate.cs @@ -166,7 +166,7 @@ namespace SabreTools string intname = systemname + " (" + sourcename + ")"; string datname = systemname + " (" + sourcename + " " + version + ")"; - Dat datdata = new Dat + DatFile datdata = new DatFile { Name = intname, Description = datname, @@ -179,7 +179,7 @@ namespace SabreTools Files = dict, }; - return DatTools.WriteDatfile(datdata, _outDir, _logger); + return DatFile.WriteDatfile(datdata, _outDir, _logger); } /// diff --git a/SabreTools.Helper/Objects/GenerateTwo.cs b/SabreTools.Helper/Objects/GenerateTwo.cs index 381357ab..9e21e6f2 100644 --- a/SabreTools.Helper/Objects/GenerateTwo.cs +++ b/SabreTools.Helper/Objects/GenerateTwo.cs @@ -144,7 +144,7 @@ namespace SabreTools } // Create the output DatData object - Dat datdata = new Dat + DatFile datdata = new DatFile { FileName = description, Name = name, @@ -173,7 +173,7 @@ namespace SabreTools { Int32.TryParse(sourcemap[hash], out tempSrcId); } - DatTools.Parse(file, 0, tempSrcId, ref datdata, _logger); + DatFile.Parse(file, 0, tempSrcId, ref datdata, _logger); } // If the dictionary is empty for any reason, tell the user and exit @@ -238,7 +238,7 @@ namespace SabreTools } // Then write out the file - DatTools.WriteDatfile(datdata, _outroot, _logger, _norename); + DatFile.WriteDatfile(datdata, _outroot, _logger, _norename); return true; } diff --git a/SabreTools.Helper/Objects/Import.cs b/SabreTools.Helper/Objects/Import.cs index 16f802c7..36c6d3e0 100644 --- a/SabreTools.Helper/Objects/Import.cs +++ b/SabreTools.Helper/Objects/Import.cs @@ -366,8 +366,8 @@ namespace SabreTools } // Get all roms that are found in the DAT to see what needs to be added - Dat datdata = new Dat(); - DatTools.Parse(_filepath, sysid, srcid, ref datdata, _logger); + DatFile datdata = new DatFile(); + DatFile.Parse(_filepath, sysid, srcid, ref datdata, _logger); // Sort inputted roms into games SortedDictionary> sortable = new SortedDictionary>(); diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index 5a978d1b..a241a718 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -9,7 +9,7 @@ namespace SabreTools.Helper public class SimpleSort { // Private instance variables - private Dat _datdata; + private DatFile _datdata; private List _inputs; private string _outDir; private string _tempDir; @@ -30,7 +30,7 @@ namespace SabreTools.Helper // Other private variables private int _cursorTop; private int _cursorLeft; - private Dat _matched; + private DatFile _matched; /// /// Create a new SimpleSort object @@ -51,7 +51,7 @@ namespace SabreTools.Helper /// Integer representing the archive handling level for Zip /// True if the updated DAT should be output, false otherwise /// Logger object for file and console output - public SimpleSort(Dat datdata, List inputs, string outDir, string tempDir, + public SimpleSort(DatFile datdata, List inputs, string outDir, string tempDir, bool quickScan, bool toFolder, bool verify, bool delete, bool? torrentX, bool romba, int sevenzip, int gz, int rar, int zip, bool updateDat, Logger logger) { @@ -74,7 +74,7 @@ namespace SabreTools.Helper _cursorTop = Console.CursorTop; _cursorLeft = Console.CursorLeft; - _matched = new Dat + _matched = new DatFile { Files = new Dictionary>(), }; @@ -144,7 +144,7 @@ namespace SabreTools.Helper } // Setup the fixdat - _matched = (Dat)_datdata.CloneHeader(); + _matched = (DatFile)_datdata.CloneHeader(); _matched.Files = new Dictionary>(); _matched.FileName = "fixDat_" + _matched.FileName; _matched.Name = "fixDat_" + _matched.Name; @@ -179,7 +179,7 @@ namespace SabreTools.Helper // Now output the fixdat to the main folder if (found) { - DatTools.WriteDatfile(_matched, "", _logger, stats: true); + DatFile.WriteDatfile(_matched, "", _logger, stats: true); } else { @@ -273,7 +273,7 @@ namespace SabreTools.Helper _datdata.Name = "fixDat_" + _datdata.Name; _datdata.Description = "fixDat_" + _datdata.Description; _datdata.OutputFormat = OutputFormat.Xml; - DatTools.WriteDatfile(_datdata, "", _logger); + DatFile.WriteDatfile(_datdata, "", _logger); } return success; @@ -341,7 +341,7 @@ namespace SabreTools.Helper // Now loop through all of the files and check them, DFD style _logger.User("Getting source file information..."); - Dat matchdat = new Dat + DatFile matchdat = new DatFile { Files = new Dictionary>(), }; @@ -429,7 +429,7 @@ namespace SabreTools.Helper } // Then bucket the keys by game for better output - SortedDictionary> keysByGame = DatTools.BucketByGame(toFromMap.Keys.ToList(), false, true, _logger); + SortedDictionary> keysByGame = DatFile.BucketByGame(toFromMap.Keys.ToList(), false, true, _logger); #endregion @@ -456,7 +456,7 @@ namespace SabreTools.Helper /// Name of the file to attempt to add /// Reference to the Dat to add to /// True if the file could be added, false otherwise - public bool RebuildToOutputAlternateParseRomHelper(string file, ref Dat matchdat) + public bool RebuildToOutputAlternateParseRomHelper(string file, ref DatFile matchdat) { Rom rom = FileTools.GetSingleFileInfo(file); diff --git a/SabreTools.Helper/Objects/Stats.cs b/SabreTools.Helper/Objects/Stats.cs index ce1887d2..b51ce636 100644 --- a/SabreTools.Helper/Objects/Stats.cs +++ b/SabreTools.Helper/Objects/Stats.cs @@ -47,9 +47,9 @@ namespace SabreTools.Helper { _logger.Log("Beginning stat collection for '" + filename + "'"); List games = new List(); - Dat datdata = new Dat(); - DatTools.Parse(filename, 0, 0, ref datdata, _logger); - SortedDictionary> newroms = DatTools.BucketByGame(datdata.Files, false, true, _logger, false); + DatFile datdata = new DatFile(); + DatFile.Parse(filename, 0, 0, ref datdata, _logger); + SortedDictionary> newroms = DatFile.BucketByGame(datdata.Files, false, true, _logger, false); // Output single DAT stats (if asked) if (_single) @@ -76,7 +76,7 @@ namespace SabreTools.Helper // Output total DAT stats if (!_single) { _logger.User(""); } - Dat totaldata = new Dat + DatFile totaldata = new DatFile { TotalSize = totalSize, RomCount = totalRom, @@ -102,7 +102,7 @@ Please check the log folder if the stats scrolled offscreen"); /// Logger object for file and console writing /// True if numbers should be recalculated for the DAT, false otherwise (default) /// Number of games to use, -1 means recalculate games (default) - public static void OutputStats(Dat datdata, Logger logger, bool recalculate = false, long game = -1) + public static void OutputStats(DatFile datdata, Logger logger, bool recalculate = false, long game = -1) { if (recalculate) { @@ -131,7 +131,7 @@ Please check the log folder if the stats scrolled offscreen"); } } - SortedDictionary> newroms = DatTools.BucketByGame(datdata.Files, false, true, logger, false); + SortedDictionary> newroms = DatFile.BucketByGame(datdata.Files, false, true, logger, false); if (datdata.TotalSize < 0) { datdata.TotalSize = Int64.MaxValue + datdata.TotalSize; diff --git a/SabreTools.Helper/SabreTools.Helper.csproj b/SabreTools.Helper/SabreTools.Helper.csproj index b4b0891c..24aa4802 100644 --- a/SabreTools.Helper/SabreTools.Helper.csproj +++ b/SabreTools.Helper/SabreTools.Helper.csproj @@ -103,6 +103,7 @@ + @@ -145,9 +146,7 @@ - - diff --git a/SabreTools.Helper/Skippers/Skippers.cs b/SabreTools.Helper/Skippers/Skippers.cs index e6e2250d..5355d2e1 100644 --- a/SabreTools.Helper/Skippers/Skippers.cs +++ b/SabreTools.Helper/Skippers/Skippers.cs @@ -81,7 +81,7 @@ namespace SabreTools.Helper Logger logger = new Logger(false, ""); logger.Start(); - XmlTextReader xtr = DatTools.GetXmlTextReader(filename, logger); + XmlTextReader xtr = DatFile.GetXmlTextReader(filename, logger); if (xtr == null) { diff --git a/SabreTools.Helper/Tools/FileToolsHash.cs b/SabreTools.Helper/Tools/FileToolsHash.cs deleted file mode 100644 index afc594fa..00000000 --- a/SabreTools.Helper/Tools/FileToolsHash.cs +++ /dev/null @@ -1,305 +0,0 @@ -using SharpCompress.Archive; -using SharpCompress.Common; -using SharpCompress.Reader; -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Text.RegularExpressions; - -namespace SabreTools.Helper -{ - public class FileToolsHash - { - #region Archive Writing - - // All things in this region are direct ports and do not take advantage of the multiple rom per hash that comes with the new system - - /// - /// Copy a file to an output archive - /// - /// Input filename to be moved - /// Output directory to build to - /// RomData representing the new information - /// This uses the new system that is not implemented anywhere yet - public static void WriteToArchive(string input, string output, RomData rom) - { - string archiveFileName = Path.Combine(output, rom.Machine + ".zip"); - - ZipArchive outarchive = null; - try - { - if (!File.Exists(archiveFileName)) - { - outarchive = System.IO.Compression.ZipFile.Open(archiveFileName, ZipArchiveMode.Create); - } - else - { - outarchive = System.IO.Compression.ZipFile.Open(archiveFileName, ZipArchiveMode.Update); - } - - if (File.Exists(input)) - { - if (outarchive.Mode == ZipArchiveMode.Create || outarchive.GetEntry(rom.Name) == null) - { - outarchive.CreateEntryFromFile(input, rom.Name, CompressionLevel.Optimal); - } - } - else if (Directory.Exists(input)) - { - foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) - { - if (outarchive.Mode == ZipArchiveMode.Create || outarchive.GetEntry(file) == null) - { - outarchive.CreateEntryFromFile(file, file, CompressionLevel.Optimal); - } - } - } - } - catch (Exception ex) - { - Console.WriteLine(ex); - } - finally - { - outarchive?.Dispose(); - } - } - - /// - /// Copy a file to an output archive using SharpCompress - /// - /// Input filename to be moved - /// Output directory to build to - /// RomData representing the new information - /// This uses the new system that is not implemented anywhere yet - public static void WriteToManagedArchive(string input, string output, RomData rom) - { - string archiveFileName = Path.Combine(output, rom.Machine + ".zip"); - - // Delete an empty file first - if (File.Exists(archiveFileName) && new FileInfo(archiveFileName).Length == 0) - { - File.Delete(archiveFileName); - } - - // Get if the file should be written out - bool newfile = File.Exists(archiveFileName) && new FileInfo(archiveFileName).Length != 0; - - using (SharpCompress.Archive.Zip.ZipArchive archive = (newfile - ? ArchiveFactory.Open(archiveFileName, Options.LookForHeader) as SharpCompress.Archive.Zip.ZipArchive - : ArchiveFactory.Create(ArchiveType.Zip) as SharpCompress.Archive.Zip.ZipArchive)) - { - try - { - if (File.Exists(input)) - { - archive.AddEntry(rom.Name, input); - } - else if (Directory.Exists(input)) - { - archive.AddAllFromDirectory(input, "*", SearchOption.AllDirectories); - } - - archive.SaveTo(archiveFileName + ".tmp", CompressionType.Deflate); - } - catch (Exception) - { - // Don't log archive write errors - } - } - - if (File.Exists(archiveFileName + ".tmp")) - { - File.Delete(archiveFileName); - File.Move(archiveFileName + ".tmp", archiveFileName); - } - } - - #endregion - - #region File Information - - /// - /// Generate a list of HashData objects from the header values in an archive - /// - /// Input file to get data from - /// Logger object for file and console output - /// List of HashData objects representing the found data - /// This uses the new system that is not implemented anywhere yet - public static List GetArchiveFileHashes(string input, Logger logger) - { - List hashes = new List(); - string gamename = Path.GetFileNameWithoutExtension(input); - - // First get the archive type - ArchiveType? at = FileTools.GetCurrentArchiveType(input, logger); - - // If we got back null, then it's not an archive, so we we return - if (at == null) - { - return hashes; - } - - // If we got back GZip, try to get TGZ info first - else if (at == ArchiveType.GZip) - { - HashData possibleTgz = GetTorrentGZFileHash(input, logger); - - // If it was, then add it to the outputs and continue - if (possibleTgz.Size != -1) - { - hashes.Add(possibleTgz); - return hashes; - } - } - - IReader reader = null; - try - { - logger.Log("Found archive of type: " + at); - long size = 0; - byte[] crc = null; - - // If we have a gzip file, get the crc directly - if (at == ArchiveType.GZip) - { - // Get the CRC and size from the file - using (BinaryReader br = new BinaryReader(File.OpenRead(input))) - { - br.BaseStream.Seek(-8, SeekOrigin.End); - crc = br.ReadBytes(4).Reverse().ToArray(); - byte[] headersize = br.ReadBytes(4); - size = BitConverter.ToInt32(headersize.Reverse().ToArray(), 0); - } - } - - reader = ReaderFactory.Open(File.OpenRead(input)); - - if (at != ArchiveType.Tar) - { - while (reader.MoveToNextEntry()) - { - if (reader.Entry != null && !reader.Entry.IsDirectory) - { - logger.Log("Entry found: '" + reader.Entry.Key + "': " - + (size == 0 ? reader.Entry.Size : size) + ", " - + (crc == null ? BitConverter.GetBytes(reader.Entry.Crc) : crc)); - - RomData temprom = new RomData - { - Type = ItemType.Rom, - Name = reader.Entry.Key, - Machine = new MachineData - { - Name = gamename, - Description = gamename, - }, - }; - HashData temphash = new HashData - { - Size = (size == 0 ? reader.Entry.Size : size), - CRC = (crc == null ? BitConverter.GetBytes(reader.Entry.Crc) : crc), - MD5 = null, - SHA1 = null, - Roms = new List(), - }; - temphash.Roms.Add(temprom); - hashes.Add(temphash); - } - } - } - } - catch (Exception ex) - { - logger.Error(ex.ToString()); - } - finally - { - reader?.Dispose(); - } - - return hashes; - } - - /// - /// Retrieve file information for a single torrent GZ file - /// - /// Filename to get information from - /// Logger object for file and console output - /// Populated HashData object if success, empty one on error - /// This uses the new system that is not implemented anywhere yet - public static HashData GetTorrentGZFileHash(string input, Logger logger) - { - string datum = Path.GetFileName(input).ToLowerInvariant(); - string sha1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(); - long filesize = new FileInfo(input).Length; - - // Check if the name is the right length - if (!Regex.IsMatch(datum, @"^[0-9a-f]{40}\.gz")) - { - logger.Warning("Non SHA-1 filename found, skipping: '" + datum + "'"); - return new HashData(); - } - - // Check if the file is at least the minimum length - if (filesize < 40 /* bytes */) - { - logger.Warning("Possibly corrupt file '" + input + "' with size " + Style.GetBytesReadable(filesize)); - return new HashData(); - } - - // Get the Romba-specific header data - byte[] header; // Get preamble header for checking - byte[] headermd5; // MD5 - byte[] headercrc; // CRC - byte[] headersz; // Int64 size - using (BinaryReader br = new BinaryReader(File.OpenRead(input))) - { - header = br.ReadBytes(12); - headermd5 = br.ReadBytes(16); - headercrc = br.ReadBytes(4); - headersz = br.ReadBytes(8); - } - - // If the header is not correct, return a blank rom - bool correct = true; - for (int i = 0; i < header.Length; i++) - { - correct &= (header[i] == Constants.TorrentGZHeader[i]); - } - if (!correct) - { - return new HashData(); - } - - // Now convert the size and get the right position - long extractedsize = (long)BitConverter.ToUInt64(headersz.Reverse().ToArray(), 0); - - RomData temprom = new RomData - { - Type = ItemType.Rom, - Name = sha1, - Machine = new MachineData - { - Name = sha1, - Description = sha1, - }, - }; - HashData temphash = new HashData - { - Size = extractedsize, - CRC = headercrc, - MD5 = headermd5, - SHA1 = Style.StringToByteArray(sha1), - Roms = new List(), - }; - temphash.Roms.Add(temprom); - - return temphash; - } - - #endregion - } -} diff --git a/SabreTools.Helper/Tools/Style.cs b/SabreTools.Helper/Tools/Style.cs index a08f4041..89e39f90 100644 --- a/SabreTools.Helper/Tools/Style.cs +++ b/SabreTools.Helper/Tools/Style.cs @@ -154,7 +154,7 @@ namespace SabreTools.Helper /// DAT information /// True if we ignore existing files (default), false otherwise /// Dictionary of output formats mapped to file names - public static Dictionary CreateOutfileNames(string outDir, Dat datdata, bool overwrite = true) + public static Dictionary CreateOutfileNames(string outDir, DatFile datdata, bool overwrite = true) { // Create the output dictionary Dictionary outfileNames = new Dictionary(); @@ -214,7 +214,7 @@ namespace SabreTools.Helper /// DAT information /// True if we ignore existing files, false otherwise /// String containing the new filename - private static string CreateOutfileNamesHelper(string outDir, string extension, Dat datdata, bool overwrite) + private static string CreateOutfileNamesHelper(string outDir, string extension, DatFile datdata, bool overwrite) { string filename = (String.IsNullOrEmpty(datdata.FileName) ? datdata.Description : datdata.FileName); string outfile = outDir + filename + extension; diff --git a/SabreTools/Partials/SabreTools_Inits.cs b/SabreTools/Partials/SabreTools_Inits.cs index 231afd17..986b3820 100644 --- a/SabreTools/Partials/SabreTools_Inits.cs +++ b/SabreTools/Partials/SabreTools_Inits.cs @@ -78,7 +78,7 @@ namespace SabreTools } } - SimpleSort ss = new SimpleSort(new Dat(), newinputs, outDir, tempDir, false, false, false, delete, false, romba, sevenzip, gz, rar, zip, false, logger); + SimpleSort ss = new SimpleSort(new DatFile(), newinputs, outDir, tempDir, false, false, false, delete, false, romba, sevenzip, gz, rar, zip, false, logger); return ss.Convert(); } @@ -127,7 +127,7 @@ namespace SabreTools int maxDegreeOfParallelism) { // Create a new DATFromDir object and process the inputs - Dat basedat = new Dat + DatFile basedat = new DatFile { FileName = filename, Name = name, @@ -149,7 +149,7 @@ namespace SabreTools if (Directory.Exists(path)) { // Clone the base Dat for information - Dat datdata = (Dat)basedat.Clone(); + DatFile datdata = (DatFile)basedat.Clone(); datdata.Files = new Dictionary>(); string basePath = Path.GetFullPath(path); @@ -159,7 +159,7 @@ namespace SabreTools // If it was a success, write the DAT out if (success) { - DatTools.WriteDatfile(dfd.DatData, "", _logger); + DatFile.WriteDatfile(dfd.DatData, "", _logger); } // Otherwise, show the help @@ -190,13 +190,13 @@ namespace SabreTools { if (File.Exists(input)) { - DatTools.SplitByExt(Path.GetFullPath(input), outDir, Path.GetDirectoryName(input), extaList, extbList, _logger); + DatFile.SplitByExt(Path.GetFullPath(input), outDir, Path.GetDirectoryName(input), extaList, extbList, _logger); } else if (Directory.Exists(input)) { foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) { - DatTools.SplitByExt(file, outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), extaList, extbList, _logger); + DatFile.SplitByExt(file, outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), extaList, extbList, _logger); } } else @@ -271,13 +271,13 @@ namespace SabreTools { if (File.Exists(input)) { - DatTools.SplitByHash(Path.GetFullPath(input), outDir, Path.GetDirectoryName(input), _logger); + DatFile.SplitByHash(Path.GetFullPath(input), outDir, Path.GetDirectoryName(input), _logger); } else if (Directory.Exists(input)) { foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) { - DatTools.SplitByHash(file, outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), _logger); + DatFile.SplitByHash(file, outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), _logger); } } else @@ -384,10 +384,10 @@ namespace SabreTools bool toFolder, bool verify, bool delete, bool? torrentX, bool romba, int sevenzip, int gz, int rar, int zip, bool updateDat, Logger logger) { // Add all of the input DATs into one huge internal DAT - Dat datdata = new Dat(); + DatFile datdata = new DatFile(); foreach (string datfile in datfiles) { - DatTools.Parse(datfile, 99, 99, ref datdata, logger); + DatFile.Parse(datfile, 99, 99, ref datdata, logger); } SimpleSort ss = new SimpleSort(datdata, inputs, outDir, tempDir, quickScan, toFolder, verify, @@ -438,13 +438,13 @@ namespace SabreTools { if (File.Exists(input)) { - DatTools.SplitByType(Path.GetFullPath(input), outDir, Path.GetFullPath(Path.GetDirectoryName(input)), _logger); + DatFile.SplitByType(Path.GetFullPath(input), outDir, Path.GetFullPath(Path.GetDirectoryName(input)), _logger); } else if (Directory.Exists(input)) { foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) { - DatTools.SplitByType(file, outDir, Path.GetFullPath((input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar)), _logger); + DatFile.SplitByType(file, outDir, Path.GetFullPath((input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar)), _logger); } } else @@ -671,7 +671,7 @@ namespace SabreTools } // Populate the DatData object - Dat userInputDat = new Dat + DatFile userInputDat = new DatFile { FileName = filename, Name = name, @@ -705,7 +705,7 @@ namespace SabreTools XSV = tsv, }; - DatTools.Update(inputs, userInputDat, outputFormat, outDir, merge, diffMode, cascade, inplace, skip, bare, clean, softlist, + DatFile.Update(inputs, userInputDat, outputFormat, outDir, merge, diffMode, cascade, inplace, skip, bare, clean, softlist, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, maxDegreeOfParallelism, _logger); } diff --git a/SimpleSort/SimpleSortApp.cs b/SimpleSort/SimpleSortApp.cs index a57a6e4c..f6fe3998 100644 --- a/SimpleSort/SimpleSortApp.cs +++ b/SimpleSort/SimpleSortApp.cs @@ -251,10 +251,10 @@ namespace SabreTools bool toFolder, bool verify, bool delete, bool? torrentX, bool romba, int sevenzip, int gz, int rar, int zip, bool updateDat, Logger logger) { // Add all of the input DATs into one huge internal DAT - Dat datdata = new Dat(); + DatFile datdata = new DatFile(); foreach (string datfile in datfiles) { - DatTools.Parse(datfile, 99, 99, ref datdata, logger, keep: true, softlist: true); + DatFile.Parse(datfile, 99, 99, ref datdata, logger, keep: true, softlist: true); } SimpleSort ss = new SimpleSort(datdata, inputs, outDir, tempDir, quickScan, toFolder, verify, @@ -295,7 +295,7 @@ namespace SabreTools } } - SimpleSort ss = new SimpleSort(new Dat(), newinputs, outDir, tempDir, false, false, false, + SimpleSort ss = new SimpleSort(new DatFile(), newinputs, outDir, tempDir, false, false, false, delete, false, romba, sevenzip, gz, rar, zip, false, logger); return ss.Convert(); } From 1db04406c3f26832a5c2c7796996e20f1c0eccb1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 20:36:12 -0700 Subject: [PATCH 03/13] [ALL] Remove Hash struct This is a bit controversial, even for me, but for the time being, we need to tie very specific information to each type of item. That means that a Rom and a Disk, though they both have hashes, they have different hashes. I'm going to see how this plays out for the time being. --- RombaSharp/Partials/RombaSharp_Helpers.cs | 26 +- SabreTools.Helper/Data/Structs.cs | 42 --- SabreTools.Helper/Objects/DATFromDir.cs | 6 +- .../Objects/DatObjects/DatFile.cs | 283 ++++++++++-------- SabreTools.Helper/Objects/DatObjects/Disk.cs | 40 +-- SabreTools.Helper/Objects/DatObjects/Rom.cs | 54 ++-- SabreTools.Helper/Objects/Generate.cs | 4 +- SabreTools.Helper/Objects/Headerer.cs | 4 +- SabreTools.Helper/Objects/Import.cs | 14 +- SabreTools.Helper/Objects/SimpleSort.cs | 26 +- SabreTools.Helper/Objects/Stats.cs | 8 +- SabreTools.Helper/Tools/FileTools.cs | 52 ++-- 12 files changed, 266 insertions(+), 293 deletions(-) diff --git a/RombaSharp/Partials/RombaSharp_Helpers.cs b/RombaSharp/Partials/RombaSharp_Helpers.cs index 18b170de..a364c30d 100644 --- a/RombaSharp/Partials/RombaSharp_Helpers.cs +++ b/RombaSharp/Partials/RombaSharp_Helpers.cs @@ -341,15 +341,15 @@ namespace SabreTools Rom dat = FileTools.GetSingleFileInfo(file); // If the Dat isn't in the database and isn't already accounted for in the DatRoot, add it - if (!databaseDats.Contains(dat.HashData.SHA1) && !toscan.ContainsKey(dat.HashData.SHA1)) + if (!databaseDats.Contains(dat.SHA1) && !toscan.ContainsKey(dat.SHA1)) { - toscan.Add(dat.HashData.SHA1, Path.GetFullPath(file)); + toscan.Add(dat.SHA1, Path.GetFullPath(file)); } // If the Dat is in the database already, remove it to find stragglers - else if (databaseDats.Contains(dat.HashData.SHA1)) + else if (databaseDats.Contains(dat.SHA1)) { - databaseDats.Remove(dat.HashData.SHA1); + databaseDats.Remove(dat.SHA1); } } @@ -368,10 +368,10 @@ namespace SabreTools { foreach (Rom rom in tempdat.Files[romkey]) { - query = "SELECT id FROM data WHERE key=\"size\" AND value=\"" + rom.HashData.Size + "\" AND (" - + "(key=\"crc\" AND (value=\"" + rom.HashData.CRC + "\" OR value=\"null\"))" - + "AND (key=\"md5\" AND value=\"" + rom.HashData.MD5 + "\" OR value=\"null\"))" - + "AND (key=\"sha1\" AND value=\"" + rom.HashData.SHA1 + "\" OR value=\"null\")))"; + query = "SELECT id FROM data WHERE key=\"size\" AND value=\"" + rom.Size + "\" AND (" + + "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))" + + "AND (key=\"md5\" AND value=\"" + rom.MD5 + "\" OR value=\"null\"))" + + "AND (key=\"sha1\" AND value=\"" + rom.SHA1 + "\" OR value=\"null\")))"; using (SqliteCommand slc = new SqliteCommand(query, dbc)) { using (SqliteDataReader sldr = slc.ExecuteReader()) @@ -392,7 +392,7 @@ namespace SabreTools // If it doesn't exist, add the hash and the dat hash for a new id else { - string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.HashData.Size + "\")"; + string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")"; using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) { sslc.ExecuteNonQuery(); @@ -406,9 +406,9 @@ namespace SabreTools id = (long)sslc.ExecuteScalar(); } - squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.HashData.CRC + "\")," - + " (\"" + id + "\", \"md5\", \"" + rom.HashData.MD5 + "\")," - + " (\"" + id + "\", \"sha1\", \"" + rom.HashData.SHA1 + "\")," + squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.CRC + "\")," + + " (\"" + id + "\", \"md5\", \"" + rom.MD5 + "\")," + + " (\"" + id + "\", \"sha1\", \"" + rom.SHA1 + "\")," + " (\"" + id + "\", \"dat\", \"" + key + "\")," + " (\"" + id + "\", \"exists\", \"false\")"; using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) @@ -451,7 +451,7 @@ namespace SabreTools if (datRootDats.Contains(input.ToLowerInvariant())) { string fullpath = Path.GetFullPath(datRootDats[datRootDats.IndexOf(input.ToLowerInvariant())]); - string sha1 = FileTools.GetSingleFileInfo(fullpath).HashData.SHA1; + string sha1 = FileTools.GetSingleFileInfo(fullpath).SHA1; foundDats.Add(sha1, fullpath); } else diff --git a/SabreTools.Helper/Data/Structs.cs b/SabreTools.Helper/Data/Structs.cs index 87c3d828..60eb1c3c 100644 --- a/SabreTools.Helper/Data/Structs.cs +++ b/SabreTools.Helper/Data/Structs.cs @@ -3,48 +3,6 @@ using System.Collections.Generic; namespace SabreTools.Helper { - #region Dat-to-Hash structs - - /// - /// Intermediate struct for holding and processing hash data - /// - public struct Hash : IEquatable - { - public long Size; - public string CRC; - public string MD5; - public string SHA1; - - public bool Equals(Hash other) - { - return this.Equals(other, false); - } - - public bool Equals(Hash other, bool IsDisk) - { - bool equals = false; - - if (IsDisk && - ((String.IsNullOrEmpty(this.MD5) || String.IsNullOrEmpty(other.MD5)) || this.MD5 == other.MD5) && - ((String.IsNullOrEmpty(this.SHA1) || String.IsNullOrEmpty(other.SHA1)) || this.SHA1 == other.SHA1)) - { - equals = true; - } - else if (!IsDisk && - (this.Size == other.Size) && - ((String.IsNullOrEmpty(this.CRC) || String.IsNullOrEmpty(other.CRC)) || this.CRC == other.CRC) && - ((String.IsNullOrEmpty(this.MD5) || String.IsNullOrEmpty(other.MD5)) || this.MD5 == other.MD5) && - ((String.IsNullOrEmpty(this.SHA1) || String.IsNullOrEmpty(other.SHA1)) || this.SHA1 == other.SHA1)) - { - equals = true; - } - - return equals; - } - } - - #endregion - #region Skipper structs /// diff --git a/SabreTools.Helper/Objects/DATFromDir.cs b/SabreTools.Helper/Objects/DATFromDir.cs index 04570e2e..1cf558b1 100644 --- a/SabreTools.Helper/Objects/DATFromDir.cs +++ b/SabreTools.Helper/Objects/DATFromDir.cs @@ -197,7 +197,7 @@ namespace SabreTools if (rom.Name != null) { // Add the list if it doesn't exist already - string key = rom.HashData.Size + "-" + rom.HashData.CRC; + string key = rom.Size + "-" + rom.CRC; lock (_datdata.Files) { @@ -317,11 +317,11 @@ namespace SabreTools string key = ""; if (datItem.Type == ItemType.Rom) { - key = ((Rom)datItem).HashData.Size + "-" + ((Rom)datItem).HashData.CRC; + key = ((Rom)datItem).Size + "-" + ((Rom)datItem).CRC; } else { - key = ((Disk)datItem).HashData.Size + "-" + ((Disk)datItem).HashData.MD5; + key = ((Disk)datItem).MD5; } // Add the list if it doesn't exist already diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs index 92f86236..13d4e648 100644 --- a/SabreTools.Helper/Objects/DatObjects/DatFile.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -795,7 +795,6 @@ namespace SabreTools.Helper item.Year = year; item.SystemID = sysid; item.SourceID = srcid; - Hash tempHash = new Hash(); // If we have a sample, treat it special if (temptype == ItemType.Sample) @@ -845,16 +844,41 @@ namespace SabreTools.Helper item.Name = gc[i].Replace("\"", ""); break; case "size": - Int64.TryParse(gc[i].Replace("\"", ""), out tempHash.Size); + if (item.Type == ItemType.Rom) + { + long size = -1; + if (Int64.TryParse(gc[i].Replace("\"", ""), out size)) + { + ((Rom)item).Size = size; + } + } + break; case "crc": - tempHash.CRC = gc[i].Replace("\"", "").ToLowerInvariant(); + if (item.Type == ItemType.Rom) + { + ((Rom)item).CRC = gc[i].Replace("\"", "").ToLowerInvariant(); + } break; case "md5": - tempHash.MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); + if (item.Type == ItemType.Rom) + { + ((Rom)item).MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); + } break; case "sha1": - tempHash.SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); + if (item.Type == ItemType.Rom) + { + ((Rom)item).SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); + } break; case "flags": if (gc[i].Replace("\"", "").ToLowerInvariant() == "nodump") @@ -916,16 +940,40 @@ namespace SabreTools.Helper item.Name = val; break; case "size": - Int64.TryParse(val, out tempHash.Size); + if (item.Type == ItemType.Rom) + { + long size = -1; + if (Int64.TryParse(gc[i].Replace("\"", ""), out size)) + { + ((Rom)item).Size = size; + } + } break; case "crc": - tempHash.CRC = val.ToLowerInvariant(); + if (item.Type == ItemType.Rom) + { + ((Rom)item).CRC = gc[i].Replace("\"", "").ToLowerInvariant(); + } break; case "md5": - tempHash.MD5 = val.ToLowerInvariant(); + if (item.Type == ItemType.Rom) + { + ((Rom)item).MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).MD5 = gc[i].Replace("\"", "").ToLowerInvariant(); + } break; case "sha1": - tempHash.SHA1 = val.ToLowerInvariant(); + if (item.Type == ItemType.Rom) + { + ((Rom)item).SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); + } + else if (item.Type == ItemType.Disk) + { + ((Disk)item).SHA1 = gc[i].Replace("\"", "").ToLowerInvariant(); + } break; case "flags": if (val.ToLowerInvariant() == "nodump") @@ -958,14 +1006,6 @@ namespace SabreTools.Helper // Now process and add the rom string key = ""; - if (item.Type == ItemType.Rom) - { - ((Rom)item).HashData = tempHash; - } - else if (item.Type == ItemType.Disk) - { - ((Disk)item).HashData = tempHash; - } ParseAddHelper(item, ref datdata, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, trim, single, root, clean, logger, out key); } // If the line is anything but a rom or disk and we're in a block @@ -1808,9 +1848,7 @@ namespace SabreTools.Helper DatItem lastrom = datdata.Files[key][index]; if (lastrom.Type == ItemType.Rom) { - Hash temphash = ((Rom)lastrom).HashData; - temphash.Size += size; - ((Rom)lastrom).HashData = temphash; + ((Rom)lastrom).Size += size; } datdata.Files[key].RemoveAt(index); datdata.Files[key].Add(lastrom); @@ -1953,9 +1991,7 @@ namespace SabreTools.Helper DatItem lastrom = datdata.Files[key][index]; if (lastrom.Type == ItemType.Rom) { - Hash temphash = ((Rom)lastrom).HashData; - temphash.Size += size; - ((Rom)lastrom).HashData = temphash; + ((Rom)lastrom).Size += size; } datdata.Files[key].RemoveAt(index); datdata.Files[key].Add(lastrom); @@ -2048,30 +2084,28 @@ namespace SabreTools.Helper Rom itemRom = (Rom)item; // Sanitize the hashes from null, hex sizes, and "true blank" strings - Hash tempItemHash = itemRom.HashData; - tempItemHash.CRC = Style.CleanHashData(itemRom.HashData.CRC, Constants.CRCLength); - tempItemHash.MD5 = Style.CleanHashData(itemRom.HashData.MD5, Constants.MD5Length); - tempItemHash.SHA1 = Style.CleanHashData(itemRom.HashData.SHA1, Constants.SHA1Length); + itemRom.CRC = Style.CleanHashData(itemRom.CRC, Constants.CRCLength); + itemRom.MD5 = Style.CleanHashData(itemRom.MD5, Constants.MD5Length); + itemRom.SHA1 = Style.CleanHashData(itemRom.SHA1, Constants.SHA1Length); // If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info - if ((tempItemHash.Size == 0 || tempItemHash.Size == -1) - && ((tempItemHash.CRC == Constants.CRCZero || tempItemHash.CRC == "") - || tempItemHash.MD5 == Constants.MD5Zero - || tempItemHash.SHA1 == Constants.SHA1Zero)) + if ((itemRom.Size == 0 || itemRom.Size == -1) + && ((itemRom.CRC == Constants.CRCZero || itemRom.CRC == "") + || itemRom.MD5 == Constants.MD5Zero + || itemRom.SHA1 == Constants.SHA1Zero)) { - tempItemHash.Size = Constants.SizeZero; - tempItemHash.CRC = Constants.CRCZero; - tempItemHash.MD5 = Constants.MD5Zero; - tempItemHash.SHA1 = Constants.SHA1Zero; + itemRom.Size = Constants.SizeZero; + itemRom.CRC = Constants.CRCZero; + itemRom.MD5 = Constants.MD5Zero; + itemRom.SHA1 = Constants.SHA1Zero; } // If the file has no size and it's not the above case, skip and log - else if (itemRom.Type == ItemType.Rom && (tempItemHash.Size == 0 || tempItemHash.Size == -1)) + else if (itemRom.Type == ItemType.Rom && (itemRom.Size == 0 || itemRom.Size == -1)) { logger.Warning("Incomplete entry for \"" + itemRom.Name + "\" will be output as nodump"); itemRom.Nodump = true; } - itemRom.HashData = tempItemHash; item = itemRom; } else if (item.Type == ItemType.Disk) @@ -2079,11 +2113,9 @@ namespace SabreTools.Helper Disk itemDisk = (Disk)item; // Sanitize the hashes from null, hex sizes, and "true blank" strings - Hash tempItemHash = itemDisk.HashData; - tempItemHash.MD5 = Style.CleanHashData(itemDisk.HashData.MD5, Constants.MD5Length); - tempItemHash.SHA1 = Style.CleanHashData(itemDisk.HashData.SHA1, Constants.SHA1Length); + itemDisk.MD5 = Style.CleanHashData(itemDisk.MD5, Constants.MD5Length); + itemDisk.SHA1 = Style.CleanHashData(itemDisk.SHA1, Constants.SHA1Length); - itemDisk.HashData = tempItemHash; item = itemDisk; } @@ -2109,15 +2141,6 @@ namespace SabreTools.Helper } } - // If we have a disk, make sure that the value for size is -1 - if (item.Type == ItemType.Disk) - { - logger.Log("Disk found: \"" + item.Name + "\""); - Hash tempDiskHash = ((Disk)item).HashData; - tempDiskHash.Size = -1; - ((Disk)item).HashData = tempDiskHash; - } - lock (datdata.Files) { // Get the key and add statistical data @@ -2130,24 +2153,24 @@ namespace SabreTools.Helper key = item.Type.ToString(); break; case ItemType.Disk: - key = ((Disk)item).HashData.Size + "-" + ((Disk)item).HashData.CRC; + key = ((Disk)item).MD5; // Add statistical data datdata.DiskCount += 1; - datdata.TotalSize += (((Disk)item).Nodump ? 0 : ((Disk)item).HashData.Size); - datdata.MD5Count += (String.IsNullOrEmpty(((Disk)item).HashData.MD5) ? 0 : 1); - datdata.SHA1Count += (String.IsNullOrEmpty(((Disk)item).HashData.SHA1) ? 0 : 1); + datdata.TotalSize += 0; + datdata.MD5Count += (String.IsNullOrEmpty(((Disk)item).MD5) ? 0 : 1); + datdata.SHA1Count += (String.IsNullOrEmpty(((Disk)item).SHA1) ? 0 : 1); datdata.NodumpCount += (((Disk)item).Nodump ? 1 : 0); break; case ItemType.Rom: - key = ((Rom)item).HashData.Size + "-" + ((Rom)item).HashData.CRC; + key = ((Rom)item).Size + "-" + ((Rom)item).CRC; // Add statistical data datdata.RomCount += 1; - datdata.TotalSize += (((Rom)item).Nodump ? 0 : ((Rom)item).HashData.Size); - datdata.CRCCount += (String.IsNullOrEmpty(((Rom)item).HashData.CRC) ? 0 : 1); - datdata.MD5Count += (String.IsNullOrEmpty(((Rom)item).HashData.MD5) ? 0 : 1); - datdata.SHA1Count += (String.IsNullOrEmpty(((Rom)item).HashData.SHA1) ? 0 : 1); + datdata.TotalSize += (((Rom)item).Nodump ? 0 : ((Rom)item).Size); + datdata.CRCCount += (String.IsNullOrEmpty(((Rom)item).CRC) ? 0 : 1); + datdata.MD5Count += (String.IsNullOrEmpty(((Rom)item).MD5) ? 0 : 1); + datdata.SHA1Count += (String.IsNullOrEmpty(((Rom)item).SHA1) ? 0 : 1); datdata.NodumpCount += (((Rom)item).Nodump ? 1 : 0); break; default: @@ -2962,10 +2985,10 @@ namespace SabreTools.Helper // If we have a "null" game (created by DATFromDir or something similar), log it to file if (rom.Type == ItemType.Rom - && ((Rom)rom).HashData.Size == -1 - && ((Rom)rom).HashData.CRC == "null" - && ((Rom)rom).HashData.MD5 == "null" - && ((Rom)rom).HashData.SHA1 == "null") + && ((Rom)rom).Size == -1 + && ((Rom)rom).CRC == "null" + && ((Rom)rom).MD5 == "null" + && ((Rom)rom).SHA1 == "null") { logger.Log("Empty folder found: " + rom.MachineName); @@ -2973,12 +2996,10 @@ namespace SabreTools.Helper if (outputFormat != OutputFormat.SabreDat && outputFormat != OutputFormat.MissFile) { rom.Name = (rom.Name == "null" ? "-" : rom.Name); - Hash tempHash = ((Rom)rom).HashData; - tempHash.Size = Constants.SizeZero; - tempHash.CRC = Constants.CRCZero; - tempHash.MD5 = Constants.MD5Zero; - tempHash.SHA1 = Constants.SHA1Zero; - ((Rom)rom).HashData = tempHash; + ((Rom)rom).Size = Constants.SizeZero; + ((Rom)rom).CRC = Constants.CRCZero; + ((Rom)rom).MD5 = Constants.MD5Zero; + ((Rom)rom).SHA1 = Constants.SHA1Zero; } // Otherwise, set the new path and such, write out, and continue @@ -3310,7 +3331,7 @@ namespace SabreTools.Helper // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip if (ignoreblanks && (rom.Type == ItemType.Rom - && (((Rom)rom).HashData.Size == 0 || ((Rom)rom).HashData.Size == -1))) + && (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1))) { return true; } @@ -3337,8 +3358,8 @@ namespace SabreTools.Helper break; case ItemType.Disk: state += "\tdisk ( name \"" + rom.Name + "\"" - + (!String.IsNullOrEmpty(((Disk)rom).HashData.MD5) ? " md5 " + ((Disk)rom).HashData.MD5.ToLowerInvariant() : "") - + (!String.IsNullOrEmpty(((Disk)rom).HashData.SHA1) ? " sha1 " + ((Disk)rom).HashData.SHA1.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Disk)rom).MD5) ? " md5 " + ((Disk)rom).MD5.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Disk)rom).SHA1) ? " sha1 " + ((Disk)rom).SHA1.ToLowerInvariant() : "") + (((Disk)rom).Nodump ? " flags nodump" : "") + " )\n"; break; @@ -3354,10 +3375,10 @@ namespace SabreTools.Helper break; case ItemType.Rom: state += "\trom ( name \"" + rom.Name + "\"" - + (((Rom)rom).HashData.Size != -1 ? " size " + ((Rom)rom).HashData.Size : "") - + (!String.IsNullOrEmpty(((Rom)rom).HashData.CRC) ? " crc " + ((Rom)rom).HashData.CRC.ToLowerInvariant() : "") - + (!String.IsNullOrEmpty(((Rom)rom).HashData.MD5) ? " md5 " + ((Rom)rom).HashData.MD5.ToLowerInvariant() : "") - + (!String.IsNullOrEmpty(((Rom)rom).HashData.SHA1) ? " sha1 " + ((Rom)rom).HashData.SHA1.ToLowerInvariant() : "") + + (((Rom)rom).Size != -1 ? " size " + ((Rom)rom).Size : "") + + (!String.IsNullOrEmpty(((Rom)rom).CRC) ? " crc " + ((Rom)rom).CRC.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Rom)rom).MD5) ? " md5 " + ((Rom)rom).MD5.ToLowerInvariant() : "") + + (!String.IsNullOrEmpty(((Rom)rom).SHA1) ? " sha1 " + ((Rom)rom).SHA1.ToLowerInvariant() : "") + (!String.IsNullOrEmpty(((Rom)rom).Date) ? " date \"" + ((Rom)rom).Date + "\"" : "") + (((Rom)rom).Nodump ? " flags nodump" : "") + " )\n"; @@ -3385,17 +3406,17 @@ namespace SabreTools.Helper pre = pre .Replace("%game%", rom.MachineName) .Replace("%name%", rom.Name) - .Replace("%crc%", ((Rom)rom).HashData.CRC) - .Replace("%md5%", ((Rom)rom).HashData.MD5) - .Replace("%sha1%", ((Rom)rom).HashData.SHA1) - .Replace("%size%", ((Rom)rom).HashData.Size.ToString()); + .Replace("%crc%", ((Rom)rom).CRC) + .Replace("%md5%", ((Rom)rom).MD5) + .Replace("%sha1%", ((Rom)rom).SHA1) + .Replace("%size%", ((Rom)rom).Size.ToString()); post = post .Replace("%game%", rom.MachineName) .Replace("%name%", rom.Name) - .Replace("%crc%", ((Rom)rom).HashData.CRC) - .Replace("%md5%", ((Rom)rom).HashData.MD5) - .Replace("%sha1%", ((Rom)rom).HashData.SHA1) - .Replace("%size%", ((Rom)rom).HashData.Size.ToString()); + .Replace("%crc%", ((Rom)rom).CRC) + .Replace("%md5%", ((Rom)rom).MD5) + .Replace("%sha1%", ((Rom)rom).SHA1) + .Replace("%size%", ((Rom)rom).Size.ToString()); } else if (rom.Type == ItemType.Disk) { @@ -3403,13 +3424,13 @@ namespace SabreTools.Helper pre = pre .Replace("%game%", rom.MachineName) .Replace("%name%", rom.Name) - .Replace("%md5%", ((Disk)rom).HashData.MD5) - .Replace("%sha1%", ((Disk)rom).HashData.SHA1); + .Replace("%md5%", ((Disk)rom).MD5) + .Replace("%sha1%", ((Disk)rom).SHA1); post = post .Replace("%game%", rom.MachineName) .Replace("%name%", rom.Name) - .Replace("%md5%", ((Disk)rom).HashData.MD5) - .Replace("%sha1%", ((Disk)rom).HashData.SHA1); + .Replace("%md5%", ((Disk)rom).MD5) + .Replace("%sha1%", ((Disk)rom).SHA1); } // If we're in Romba mode, the state is consistent @@ -3418,26 +3439,26 @@ namespace SabreTools.Helper if (rom.Type == ItemType.Rom) { // We can only write out if there's a SHA-1 - if (((Rom)rom).HashData.SHA1 != "") + if (((Rom)rom).SHA1 != "") { - string name = ((Rom)rom).HashData.SHA1.Substring(0, 2) - + "/" + ((Rom)rom).HashData.SHA1.Substring(2, 2) - + "/" + ((Rom)rom).HashData.SHA1.Substring(4, 2) - + "/" + ((Rom)rom).HashData.SHA1.Substring(6, 2) - + "/" + ((Rom)rom).HashData.SHA1 + ".gz"; + string name = ((Rom)rom).SHA1.Substring(0, 2) + + "/" + ((Rom)rom).SHA1.Substring(2, 2) + + "/" + ((Rom)rom).SHA1.Substring(4, 2) + + "/" + ((Rom)rom).SHA1.Substring(6, 2) + + "/" + ((Rom)rom).SHA1 + ".gz"; state += pre + name + post + "\n"; } } else if (rom.Type == ItemType.Disk) { // We can only write out if there's a SHA-1 - if (((Disk)rom).HashData.SHA1 != "") + if (((Disk)rom).SHA1 != "") { - string name = ((Disk)rom).HashData.SHA1.Substring(0, 2) - + "/" + ((Disk)rom).HashData.SHA1.Substring(2, 2) - + "/" + ((Disk)rom).HashData.SHA1.Substring(4, 2) - + "/" + ((Disk)rom).HashData.SHA1.Substring(6, 2) - + "/" + ((Disk)rom).HashData.SHA1 + ".gz"; + string name = ((Disk)rom).SHA1.Substring(0, 2) + + "/" + ((Disk)rom).SHA1.Substring(2, 2) + + "/" + ((Disk)rom).SHA1.Substring(4, 2) + + "/" + ((Disk)rom).SHA1.Substring(6, 2) + + "/" + ((Disk)rom).SHA1 + ".gz"; state += pre + name + post + "\n"; } } @@ -3457,10 +3478,10 @@ namespace SabreTools.Helper + separator + "\"rom\"" + separator + "\"" + rom.Name + "\"" + separator + "\"\"" - + separator + "\"" + ((Rom)rom).HashData.Size + "\"" - + separator + "\"" + ((Rom)rom).HashData.CRC + "\"" - + separator + "\"" + ((Rom)rom).HashData.MD5 + "\"" - + separator + "\"" + ((Rom)rom).HashData.SHA1 + "\"" + + separator + "\"" + ((Rom)rom).Size + "\"" + + separator + "\"" + ((Rom)rom).CRC + "\"" + + separator + "\"" + ((Rom)rom).MD5 + "\"" + + separator + "\"" + ((Rom)rom).SHA1 + "\"" + separator + (((Rom)rom).Nodump ? "\"Nodump\"" : "\"\""); state += pre + inline + post + "\n"; } @@ -3476,8 +3497,8 @@ namespace SabreTools.Helper + separator + "\"" + rom.Name + "\"" + separator + "\"\"" + separator + "\"\"" - + separator + "\"" + ((Disk)rom).HashData.MD5 + "\"" - + separator + "\"" + ((Disk)rom).HashData.SHA1 + "\"" + + separator + "\"" + ((Disk)rom).MD5 + "\"" + + separator + "\"" + ((Disk)rom).SHA1 + "\"" + separator + (((Disk)rom).Nodump ? "\"Nodump\"" : "\"\""); state += pre + inline + post + "\n"; } @@ -3520,27 +3541,27 @@ namespace SabreTools.Helper case OutputFormat.RedumpMD5: if (rom.Type == ItemType.Rom) { - state += ((Rom)rom).HashData.MD5 + " *" + rom.Name + "\n"; + state += ((Rom)rom).MD5 + " *" + rom.Name + "\n"; } else if (rom.Type == ItemType.Disk) { - state += ((Disk)rom).HashData.MD5 + " *" + rom.Name + "\n"; + state += ((Disk)rom).MD5 + " *" + rom.Name + "\n"; } break; case OutputFormat.RedumpSFV: if (rom.Type == ItemType.Rom) { - state += rom.Name + " " + ((Rom)rom).HashData.CRC + "\n"; + state += rom.Name + " " + ((Rom)rom).CRC + "\n"; } break; case OutputFormat.RedumpSHA1: if (rom.Type == ItemType.Rom) { - state += ((Rom)rom).HashData.SHA1 + " *" + rom.Name + "\n"; + state += ((Rom)rom).SHA1 + " *" + rom.Name + "\n"; } else if (rom.Type == ItemType.Disk) { - state += ((Disk)rom).HashData.SHA1 + " *" + rom.Name + "\n"; + state += ((Disk)rom).SHA1 + " *" + rom.Name + "\n"; } break; case OutputFormat.RomCenter: @@ -3551,8 +3572,8 @@ namespace SabreTools.Helper "¬" + HttpUtility.HtmlEncode(rom.MachineName) + "¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) + "¬" + HttpUtility.HtmlEncode(rom.Name) + - "¬" + ((Rom)rom).HashData.CRC.ToLowerInvariant() + - "¬" + (((Rom)rom).HashData.Size != -1 ? ((Rom)rom).HashData.Size.ToString() : "") + "¬¬¬\n"; + "¬" + ((Rom)rom).CRC.ToLowerInvariant() + + "¬" + (((Rom)rom).Size != -1 ? ((Rom)rom).Size.ToString() : "") + "¬¬¬\n"; } else if (rom.Type == ItemType.Disk) { @@ -3589,8 +3610,8 @@ namespace SabreTools.Helper break; case ItemType.Disk: state += "\n" + prefix + "\t\n" + prefix + "\t\t\n" + prefix + "\t\n" + @@ -3608,10 +3629,10 @@ namespace SabreTools.Helper break; case ItemType.Rom: state += "\n" + prefix + "\t\n" + prefix + "\t\t\n" + @@ -3641,8 +3662,8 @@ namespace SabreTools.Helper break; case ItemType.Disk: state += "\t\t\n"; break; @@ -3658,10 +3679,10 @@ namespace SabreTools.Helper break; case ItemType.Rom: state += "\t\t\n"; @@ -4074,8 +4095,8 @@ namespace SabreTools.Helper } } // If the file has a SHA-1 - else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).HashData.SHA1)) - || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).HashData.SHA1))) + else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).SHA1)) + || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).SHA1))) { if (sha1.Files.ContainsKey(key)) { @@ -4089,8 +4110,8 @@ namespace SabreTools.Helper } } // If the file has no SHA-1 but has an MD5 - else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).HashData.MD5)) - || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).HashData.MD5))) + else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).MD5)) + || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).MD5))) { if (md5.Files.ContainsKey(key)) { @@ -4104,8 +4125,8 @@ namespace SabreTools.Helper } } // If the file has no MD5 but a CRC - else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).HashData.SHA1)) - || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).HashData.SHA1))) + else if ((rom.Type == ItemType.Rom && !String.IsNullOrEmpty(((Rom)rom).SHA1)) + || (rom.Type == ItemType.Disk && !String.IsNullOrEmpty(((Disk)rom).SHA1))) { if (crc.Files.ContainsKey(key)) { diff --git a/SabreTools.Helper/Objects/DatObjects/Disk.cs b/SabreTools.Helper/Objects/DatObjects/Disk.cs index 8ad768ac..64993b10 100644 --- a/SabreTools.Helper/Objects/DatObjects/Disk.cs +++ b/SabreTools.Helper/Objects/DatObjects/Disk.cs @@ -1,24 +1,32 @@ -namespace SabreTools.Helper +using System; + +namespace SabreTools.Helper { public class Disk : DatItem { #region Private instance variables // Disk information - private Hash _hashData; + protected string _md5; + protected string _sha1; // private string _merge; // private DiskStatus _romStatus; - private bool _nodump; + protected bool _nodump; #endregion #region Publicly facing variables // Disk information - public Hash HashData + public string MD5 { - get { return _hashData; } - set { _hashData = value; } + get { return _md5; } + set { _md5 = value; } + } + public string SHA1 + { + get { return _sha1; } + set { _sha1 = value; } } public bool Nodump { @@ -52,11 +60,8 @@ { _name = name; _itemType = ItemType.Disk; - _hashData = new Hash - { - MD5 = md5.ToLowerInvariant(), - SHA1 = sha1.ToLowerInvariant(), - }; + _md5 = md5.ToLowerInvariant(); + _sha1 = sha1.ToLowerInvariant(); _nodump = nodump; } @@ -89,11 +94,8 @@ { _name = name; _itemType = ItemType.Disk; - _hashData = new Hash - { - MD5 = md5.ToLowerInvariant(), - SHA1 = sha1.ToLowerInvariant(), - }; + _md5 = md5.ToLowerInvariant(); + _sha1 = sha1.ToLowerInvariant(); _nodump = nodump; _machineName = machineName; _comment = comment; @@ -136,7 +138,11 @@ return dupefound; } - dupefound = _hashData.Equals(newOther.HashData, false); + if (((String.IsNullOrEmpty(_md5) || String.IsNullOrEmpty(newOther.MD5)) || this.MD5 == newOther.MD5) && + ((String.IsNullOrEmpty(this.SHA1) || String.IsNullOrEmpty(newOther.SHA1)) || this.SHA1 == newOther.SHA1)) + { + dupefound = true; + } return dupefound; } diff --git a/SabreTools.Helper/Objects/DatObjects/Rom.cs b/SabreTools.Helper/Objects/DatObjects/Rom.cs index 51d195ad..5f2f98d0 100644 --- a/SabreTools.Helper/Objects/DatObjects/Rom.cs +++ b/SabreTools.Helper/Objects/DatObjects/Rom.cs @@ -1,14 +1,14 @@ -namespace SabreTools.Helper +using System; + +namespace SabreTools.Helper { - public class Rom : DatItem + public class Rom : Disk { #region Private instance variables // Rom information - private Hash _hashData; - // private string _merge; - // private RomStatus _romStatus; - private bool _nodump; + protected long _size; + protected string _crc; private string _date; #endregion @@ -16,15 +16,15 @@ #region Publicly facing variables // Rom information - public Hash HashData + public long Size { - get { return _hashData; } - set { _hashData = value; } + get { return _size; } + set { _size = value; } } - public bool Nodump + public string CRC { - get { return _nodump; } - set { _nodump = value; } + get { return _crc; } + set { _crc = value; } } public string Date { @@ -72,13 +72,10 @@ { _name = name; _itemType = ItemType.Rom; - _hashData = new Hash - { - Size = size, - CRC = crc.ToLowerInvariant(), - MD5 = md5.ToLowerInvariant(), - SHA1 = sha1.ToLowerInvariant(), - }; + _size = size; + _crc = crc.ToLowerInvariant(); + _md5 = md5.ToLowerInvariant(); + _sha1 = sha1.ToLowerInvariant(); _nodump = nodump; _date = date; } @@ -115,13 +112,10 @@ { _name = name; _itemType = ItemType.Rom; - _hashData = new Hash - { - Size = size, - CRC = crc.ToLowerInvariant(), - MD5 = md5.ToLowerInvariant(), - SHA1 = sha1.ToLowerInvariant(), - }; + _size = size; + _crc = crc.ToLowerInvariant(); + _md5 = md5.ToLowerInvariant(); + _sha1 = sha1.ToLowerInvariant(); _nodump = nodump; _date = date; _machineName = machineName; @@ -165,7 +159,13 @@ return dupefound; } - dupefound = _hashData.Equals(newOther.HashData, false); + if ((this.Size == newOther.Size) && + ((String.IsNullOrEmpty(this.CRC) || String.IsNullOrEmpty(newOther.CRC)) || this.CRC == newOther.CRC) && + ((String.IsNullOrEmpty(this.MD5) || String.IsNullOrEmpty(newOther.MD5)) || this.MD5 == newOther.MD5) && + ((String.IsNullOrEmpty(this.SHA1) || String.IsNullOrEmpty(newOther.SHA1)) || this.SHA1 == newOther.SHA1)) + { + dupefound = true; + } return dupefound; } diff --git a/SabreTools.Helper/Objects/Generate.cs b/SabreTools.Helper/Objects/Generate.cs index 72f9d41e..7abfc15a 100644 --- a/SabreTools.Helper/Objects/Generate.cs +++ b/SabreTools.Helper/Objects/Generate.cs @@ -245,7 +245,7 @@ JOIN checksums sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, null, null, sldr.GetInt32(2), sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); - key = ((Disk)temp).HashData.Size + "-" + ((Disk)temp).HashData.CRC; + key = ((Disk)temp).MD5; break; case "rom": default: @@ -253,7 +253,7 @@ JOIN checksums sldr.GetString(13), sldr.GetString(6), null, sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, null, null, sldr.GetInt32(2), sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); - key = ((Disk)temp).HashData.Size + "-" + ((Disk)temp).HashData.CRC; + key = ((Rom)temp).Size + "-" + ((Rom)temp).CRC; break; } diff --git a/SabreTools.Helper/Objects/Headerer.cs b/SabreTools.Helper/Objects/Headerer.cs index e0304be4..9b779ed5 100644 --- a/SabreTools.Helper/Objects/Headerer.cs +++ b/SabreTools.Helper/Objects/Headerer.cs @@ -132,7 +132,7 @@ namespace SabreTools // Now add the information to the database if it's not already there Rom rom = FileTools.GetSingleFileInfo(newfile); - AddHeaderToDatabase(hstr, rom.HashData.SHA1, type); + AddHeaderToDatabase(hstr, rom.SHA1, type); } return true; @@ -191,7 +191,7 @@ namespace SabreTools // Then try to pull the corresponding headers from the database string header = ""; - string query = @"SELECT header, type FROM data WHERE sha1='" + rom.HashData.SHA1 + "'"; + string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'"; using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) { dbc.Open(); diff --git a/SabreTools.Helper/Objects/Import.cs b/SabreTools.Helper/Objects/Import.cs index 36c6d3e0..1287c4de 100644 --- a/SabreTools.Helper/Objects/Import.cs +++ b/SabreTools.Helper/Objects/Import.cs @@ -520,10 +520,10 @@ SELECT files.id FROM files WHERE files.name='" + rom.Name.Replace("'", "''") + @"' AND files.type='" + rom.Type + @"' AND files.setid=" + gameid + - " AND checksums.size=" + rom.HashData.Size + - " AND checksums.crc='" + rom.HashData.CRC + "'" + - " AND checksums.md5='" + rom.HashData.MD5 + "'" + - " AND checksums.sha1='" + rom.HashData.SHA1 + "'"; + " AND checksums.size=" + rom.Size + + " AND checksums.crc='" + rom.CRC + "'" + + " AND checksums.md5='" + rom.MD5 + "'" + + " AND checksums.sha1='" + rom.SHA1 + "'"; using (SqliteCommand slc = new SqliteCommand(query, dbc)) { @@ -536,7 +536,7 @@ SELECT files.id FROM files INSERT INTO files (setid, name, type, lastupdated) VALUES (" + gameid + ", '" + rom.Name.Replace("'", "''") + "', '" + rom.Type + "', '" + date + @"'); INSERT INTO checksums (file, size, crc, md5, sha1) -VALUES ((SELECT last_insertConstants.Rowid()), " + rom.HashData.Size + ", '" + rom.HashData.CRC + "'" + ", '" + rom.HashData.MD5 + "'" + ", '" + rom.HashData.SHA1 + @"'); +VALUES ((SELECT last_insertConstants.Rowid()), " + rom.Size + ", '" + rom.CRC + "'" + ", '" + rom.MD5 + "'" + ", '" + rom.SHA1 + @"'); COMMIT;"; using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) { @@ -594,7 +594,7 @@ COMMIT;"; // Retrieve or insert the hash long hashid = -1; - string query = "SELECT id FROM hash WHERE size=" + rom.HashData.Size + " AND crc='" + rom.HashData.CRC + "' AND md5='" + rom.HashData.MD5 + "' AND sha1='" + rom.HashData.SHA1 + "'"; + string query = "SELECT id FROM hash WHERE size=" + rom.Size + " AND crc='" + rom.CRC + "' AND md5='" + rom.MD5 + "' AND sha1='" + rom.SHA1 + "'"; using (SqliteCommand slc = new SqliteCommand(query, dbc)) { using (SqliteDataReader sldr = slc.ExecuteReader()) @@ -603,7 +603,7 @@ COMMIT;"; if (!sldr.HasRows) { query = "INSERT INTO hash (size, crc, md5, sha1)" + - " VALUES (" + rom.HashData.Size + ", '" + rom.HashData.CRC + "', '" + rom.HashData.MD5 + "', '" + rom.HashData.SHA1 + "')"; + " VALUES (" + rom.Size + ", '" + rom.CRC + "', '" + rom.MD5 + "', '" + rom.SHA1 + "')"; using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) { diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index a241a718..0d6048b6 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -161,7 +161,7 @@ namespace SabreTools.Helper if (rom.SourceID == 99) { found = true; - string key = rom.HashData.Size + "-" + rom.HashData.CRC; + string key = rom.Size + "-" + rom.CRC; if (_matched.Files.ContainsKey(key)) { _matched.Files[key].Add(rom); @@ -470,7 +470,7 @@ namespace SabreTools.Helper rom.MachineName = Path.GetDirectoryName(Path.GetFullPath(file)); // Add the rom information to the Dat - string key = rom.HashData.Size + "-" + rom.HashData.CRC; + string key = rom.Size + "-" + rom.CRC; if (matchdat.Files.ContainsKey(key)) { matchdat.Files[key].Add(rom); @@ -497,7 +497,7 @@ namespace SabreTools.Helper romNH.MachineName = rom.MachineName; // Add the rom information to the Dat - key = romNH.HashData.Size + "-" + romNH.HashData.CRC; + key = romNH.Size + "-" + romNH.CRC; if (matchdat.Files.ContainsKey(key)) { matchdat.Files[key].Add(romNH); @@ -555,7 +555,7 @@ namespace SabreTools.Helper _logger.Log("Matched name: " + found.Name); // Add rom to the matched list - string key = found.HashData.Size + "-" + found.HashData.CRC; + string key = found.Size + "-" + found.CRC; if (_matched.Files.ContainsKey(key)) { _matched.Files[key].Add(found); @@ -576,7 +576,7 @@ namespace SabreTools.Helper Directory.CreateDirectory(gamedir); } - _logger.Log("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + (_torrentX == false ? found.HashData.SHA1 : found.Name) + "'"); + _logger.Log("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + (_torrentX == false ? found.SHA1 : found.Name) + "'"); try { File.Copy(input, Path.Combine(gamedir, Path.GetFileName(found.Name))); @@ -623,7 +623,7 @@ namespace SabreTools.Helper foreach (Rom found in founddroms) { // Add rom to the matched list - string key = found.HashData.Size + "-" + found.HashData.CRC; + string key = found.Size + "-" + found.CRC; if (_matched.Files.ContainsKey(key)) { _matched.Files[key].Add(found); @@ -647,7 +647,7 @@ namespace SabreTools.Helper Directory.CreateDirectory(gamedir); } - _logger.Log("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + (_torrentX == false ? found.HashData.SHA1 : found.Name) + "'"); + _logger.Log("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + (_torrentX == false ? found.SHA1 : found.Name) + "'"); try { File.Copy(newinput, Path.Combine(gamedir, Path.GetFileName(found.Name))); @@ -672,11 +672,11 @@ namespace SabreTools.Helper // Then output the headered rom (renamed) Rom newfound = found; - newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.HashData.CRC + ")" + Path.GetExtension(newfound.Name); - newfound.HashData = rom.HashData; + newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.CRC + ")" + Path.GetExtension(newfound.Name); + newfound = rom; // Add rom to the matched list - key = newfound.HashData.Size + "-" + newfound.HashData.CRC; + key = newfound.Size + "-" + newfound.CRC; if (_matched.Files.ContainsKey(key)) { _matched.Files[key].Add(newfound); @@ -755,7 +755,7 @@ namespace SabreTools.Helper foreach (Rom found in foundroms) { // Add rom to the matched list - string key = found.HashData.Size + "-" + found.HashData.CRC; + string key = found.Size + "-" + found.CRC; if (_matched.Files.ContainsKey(key)) { _matched.Files[key].Add(found); @@ -790,7 +790,7 @@ namespace SabreTools.Helper else { // Copy file between archives - _logger.Log("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + (_torrentX == false ? found.HashData.SHA1 : found.Name) + "'"); + _logger.Log("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + (_torrentX == false ? found.SHA1 : found.Name) + "'"); if (Build.MonoEnvironment || _torrentX == false) { @@ -914,7 +914,7 @@ namespace SabreTools.Helper // Then add each of the found files to the new dictionary foreach (Rom rom in roms) { - string key = rom.HashData.Size + "-" + rom.HashData.CRC; + string key = rom.Size + "-" + rom.CRC; if (scanned.ContainsKey(key)) { scanned[key].Add(rom); diff --git a/SabreTools.Helper/Objects/Stats.cs b/SabreTools.Helper/Objects/Stats.cs index b51ce636..c213fd9e 100644 --- a/SabreTools.Helper/Objects/Stats.cs +++ b/SabreTools.Helper/Objects/Stats.cs @@ -122,10 +122,10 @@ Please check the log folder if the stats scrolled offscreen"); { datdata.RomCount += (rom.Type == ItemType.Rom ? 1 : 0); datdata.DiskCount += (rom.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (rom.Nodump ? 0 : rom.HashData.Size); - datdata.CRCCount += (String.IsNullOrEmpty(rom.HashData.CRC) ? 0 : 1); - datdata.MD5Count += (String.IsNullOrEmpty(rom.HashData.MD5) ? 0 : 1); - datdata.SHA1Count += (String.IsNullOrEmpty(rom.HashData.SHA1) ? 0 : 1); + datdata.TotalSize += (rom.Nodump ? 0 : rom.Size); + datdata.CRCCount += (String.IsNullOrEmpty(rom.CRC) ? 0 : 1); + datdata.MD5Count += (String.IsNullOrEmpty(rom.MD5) ? 0 : 1); + datdata.SHA1Count += (String.IsNullOrEmpty(rom.SHA1) ? 0 : 1); datdata.NodumpCount += (rom.Nodump ? 1 : 0); } } diff --git a/SabreTools.Helper/Tools/FileTools.cs b/SabreTools.Helper/Tools/FileTools.cs index ff4118d6..1e9dd060 100644 --- a/SabreTools.Helper/Tools/FileTools.cs +++ b/SabreTools.Helper/Tools/FileTools.cs @@ -224,7 +224,7 @@ namespace SabreTools.Helper writeStream.Write(buffer, 0, len); } writeStream.Flush(); - zipFile.CloseWriteStream(Convert.ToUInt32(rom.HashData.CRC, 16)); + zipFile.CloseWriteStream(Convert.ToUInt32(rom.CRC, 16)); } } } @@ -291,7 +291,7 @@ namespace SabreTools.Helper } freadStream.Close(); freadStream.Dispose(); - zipFile.CloseWriteStream(Convert.ToUInt32(roms[-index - 1].HashData.CRC, 16)); + zipFile.CloseWriteStream(Convert.ToUInt32(roms[-index - 1].CRC, 16)); } // Otherwise, copy the file from the old archive @@ -372,7 +372,7 @@ namespace SabreTools.Helper Rom rom = FileTools.GetSingleFileInfo(input); // If it doesn't exist, create the output file and then write - string outfile = Path.Combine(outDir, rom.HashData.SHA1 + ".gz"); + string outfile = Path.Combine(outDir, rom.SHA1 + ".gz"); using (FileStream inputstream = new FileStream(input, FileMode.Open)) using (GZipStream output = new GZipStream(File.Open(outfile, FileMode.Create, FileAccess.Write), CompressionMode.Compress)) { @@ -386,9 +386,9 @@ namespace SabreTools.Helper { // Write standard header and TGZ info byte[] data = Constants.TorrentGZHeader - .Concat(Style.StringToByteArray(rom.HashData.MD5)) // MD5 - .Concat(Style.StringToByteArray(rom.HashData.CRC)) // CRC - .Concat(BitConverter.GetBytes(rom.HashData.Size).Reverse().ToArray()) // Long size (Mirrored) + .Concat(Style.StringToByteArray(rom.MD5)) // MD5 + .Concat(Style.StringToByteArray(rom.CRC)) // CRC + .Concat(BitConverter.GetBytes(rom.Size).Reverse().ToArray()) // Long size (Mirrored) .ToArray(); sw.Write(data); @@ -414,7 +414,7 @@ namespace SabreTools.Helper // If we're in romba mode, create the subfolder and move the file if (romba) { - string subfolder = Path.Combine(rom.HashData.SHA1.Substring(0, 2), rom.HashData.SHA1.Substring(2, 2), rom.HashData.SHA1.Substring(4, 2), rom.HashData.SHA1.Substring(6, 2)); + string subfolder = Path.Combine(rom.SHA1.Substring(0, 2), rom.SHA1.Substring(2, 2), rom.SHA1.Substring(4, 2), rom.SHA1.Substring(6, 2)); outDir = Path.Combine(outDir, subfolder); if (!Directory.Exists(outDir)) { @@ -723,13 +723,10 @@ namespace SabreTools.Helper Rom rom = new Rom { Type = ItemType.Rom, - HashData = new Hash - { - Size = input.Length - Math.Abs(offset), - CRC = string.Empty, - MD5 = string.Empty, - SHA1 = string.Empty, - }, + Size = input.Length - Math.Abs(offset), + CRC = string.Empty, + MD5 = string.Empty, + SHA1 = string.Empty, }; try @@ -760,21 +757,18 @@ namespace SabreTools.Helper } crc.Update(buffer, 0, 0); - Hash tempHash = new Hash(); - tempHash.CRC = crc.Value.ToString("X8").ToLowerInvariant(); + rom.CRC = crc.Value.ToString("X8").ToLowerInvariant(); if (!noMD5) { md5.TransformFinalBlock(buffer, 0, 0); - tempHash.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); + rom.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); } if (!noSHA1) { sha1.TransformFinalBlock(buffer, 0, 0); - tempHash.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); + rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); } - - rom.HashData = tempHash; } } catch (IOException) @@ -864,11 +858,8 @@ namespace SabreTools.Helper Type = ItemType.Rom, Name = reader.Entry.Key, MachineName = gamename, - HashData = new Hash - { - Size = (size == 0 ? reader.Entry.Size : size), - CRC = (crc == "" ? reader.Entry.Crc.ToString("X").ToLowerInvariant() : crc), - }, + Size = (size == 0 ? reader.Entry.Size : size), + CRC = (crc == "" ? reader.Entry.Crc.ToString("X").ToLowerInvariant() : crc), }); } } @@ -950,13 +941,10 @@ namespace SabreTools.Helper Type = ItemType.Rom, MachineName = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), - HashData = new Hash - { - Size = extractedsize, - CRC = gzcrc.ToLowerInvariant(), - MD5 = gzmd5.ToLowerInvariant(), - SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), - }, + Size = extractedsize, + CRC = gzcrc.ToLowerInvariant(), + MD5 = gzmd5.ToLowerInvariant(), + SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), }; return rom; From 10b74ec550543638e443dce6158325655f513b9f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 20:39:51 -0700 Subject: [PATCH 04/13] [DatFile] Fix XML output --- SabreTools.Helper/Objects/DatObjects/DatFile.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs index 13d4e648..bf0c96bb 100644 --- a/SabreTools.Helper/Objects/DatObjects/DatFile.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -3630,9 +3630,9 @@ namespace SabreTools.Helper case ItemType.Rom: state += "\n" + prefix + "\t\n" + prefix + "\t\t\n" + @@ -3680,9 +3680,9 @@ namespace SabreTools.Helper case ItemType.Rom: state += "\t\t\n"; From 44b00f9f5a715cdd84a52aa8d8417891b07a1252 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 20:51:18 -0700 Subject: [PATCH 05/13] [DatFile, Disk, Rom] Make sure nulls are accounted for --- SabreTools.Helper/Objects/DatObjects/DatFile.cs | 13 ++++++------- SabreTools.Helper/Objects/DatObjects/Disk.cs | 8 ++++---- SabreTools.Helper/Objects/DatObjects/Rom.cs | 12 ++++++------ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs index bf0c96bb..e45613bc 100644 --- a/SabreTools.Helper/Objects/DatObjects/DatFile.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -1866,16 +1866,15 @@ namespace SabreTools.Helper switch (subreader.Name.ToLowerInvariant()) { case "disk": - inrom = new Disk(subreader.GetAttribute("name"), subreader.GetAttribute("md5")?.ToLowerInvariant(), - subreader.GetAttribute("sha1")?.ToLowerInvariant(), isnodump, tempname, null, gamedesc, null, null, - romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null); + inrom = new Disk(subreader.GetAttribute("name"), subreader.GetAttribute("md5"), subreader.GetAttribute("sha1"), + isnodump, tempname, null, gamedesc, null, null, romof, cloneof, sampleof, null, false, null, null, sysid, + filename, srcid, null); break; case "rom": default: - inrom = new Rom(subreader.GetAttribute("name"), size, subreader.GetAttribute("crc")?.ToLowerInvariant(), - subreader.GetAttribute("md5")?.ToLowerInvariant(), subreader.GetAttribute("sha1")?.ToLowerInvariant(), isnodump, - date, tempname, null, gamedesc, null, null, romof, cloneof, sampleof, null, false, null, null, sysid, filename, - srcid, null); + inrom = new Rom(subreader.GetAttribute("name"), size, subreader.GetAttribute("crc"), subreader.GetAttribute("md5"), + subreader.GetAttribute("sha1"), isnodump, date, tempname, null, gamedesc, null, null, romof, cloneof, sampleof, + null, false, null, null, sysid, filename, srcid, null); break; } diff --git a/SabreTools.Helper/Objects/DatObjects/Disk.cs b/SabreTools.Helper/Objects/DatObjects/Disk.cs index 64993b10..9a2db1f6 100644 --- a/SabreTools.Helper/Objects/DatObjects/Disk.cs +++ b/SabreTools.Helper/Objects/DatObjects/Disk.cs @@ -60,8 +60,8 @@ namespace SabreTools.Helper { _name = name; _itemType = ItemType.Disk; - _md5 = md5.ToLowerInvariant(); - _sha1 = sha1.ToLowerInvariant(); + _md5 = md5?.ToLowerInvariant(); + _sha1 = sha1?.ToLowerInvariant(); _nodump = nodump; } @@ -94,8 +94,8 @@ namespace SabreTools.Helper { _name = name; _itemType = ItemType.Disk; - _md5 = md5.ToLowerInvariant(); - _sha1 = sha1.ToLowerInvariant(); + _md5 = md5?.ToLowerInvariant(); + _sha1 = sha1?.ToLowerInvariant(); _nodump = nodump; _machineName = machineName; _comment = comment; diff --git a/SabreTools.Helper/Objects/DatObjects/Rom.cs b/SabreTools.Helper/Objects/DatObjects/Rom.cs index 5f2f98d0..5bb18a8b 100644 --- a/SabreTools.Helper/Objects/DatObjects/Rom.cs +++ b/SabreTools.Helper/Objects/DatObjects/Rom.cs @@ -73,9 +73,9 @@ namespace SabreTools.Helper _name = name; _itemType = ItemType.Rom; _size = size; - _crc = crc.ToLowerInvariant(); - _md5 = md5.ToLowerInvariant(); - _sha1 = sha1.ToLowerInvariant(); + _crc = crc?.ToLowerInvariant(); + _md5 = md5?.ToLowerInvariant(); + _sha1 = sha1?.ToLowerInvariant(); _nodump = nodump; _date = date; } @@ -113,9 +113,9 @@ namespace SabreTools.Helper _name = name; _itemType = ItemType.Rom; _size = size; - _crc = crc.ToLowerInvariant(); - _md5 = md5.ToLowerInvariant(); - _sha1 = sha1.ToLowerInvariant(); + _crc = crc?.ToLowerInvariant(); + _md5 = md5?.ToLowerInvariant(); + _sha1 = sha1?.ToLowerInvariant(); _nodump = nodump; _date = date; _machineName = machineName; From 770f9d7275788602f7b2b24e89fc8fe2b906f5f7 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 20:52:55 -0700 Subject: [PATCH 06/13] [DatItem] Add and fix --- .../Objects/DatObjects/DatItem.cs | 784 ++++++++++++++++++ 1 file changed, 784 insertions(+) create mode 100644 SabreTools.Helper/Objects/DatObjects/DatItem.cs diff --git a/SabreTools.Helper/Objects/DatObjects/DatItem.cs b/SabreTools.Helper/Objects/DatObjects/DatItem.cs new file mode 100644 index 00000000..a967dbbf --- /dev/null +++ b/SabreTools.Helper/Objects/DatObjects/DatItem.cs @@ -0,0 +1,784 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace SabreTools.Helper +{ + public abstract class DatItem : IEquatable, IComparable + { + #region Protected instance variables + + // Standard item information + protected string _name; + protected ItemType _itemType; + protected DupeType _dupeType; + + // Machine information + protected string _machineName; + protected string _comment; + protected string _machineDescription; + protected string _year; + protected string _manufacturer; + protected string _romOf; + protected string _cloneOf; + protected string _sampleOf; + protected string _sourceFile; + protected bool _isBios; + protected string _board; + protected string _rebuildTo; + + // Source metadata information + protected int _systemId; + protected string _systemName; + protected int _sourceId; + protected string _sourceName; + + #endregion + + #region Publicly facing variables + + // Standard item information + public string Name + { + get { return _name; } + set { _name = value; } + } + public ItemType Type + { + get { return _itemType; } + set { _itemType = value; } + } + public DupeType Dupe + { + get { return _dupeType; } + set { _dupeType = value; } + } + + // Machine information + public string MachineName + { + get { return _machineName; } + set { _machineName = value; } + } + public string Comment + { + get { return _comment; } + set { _comment = value; } + } + public string MachineDescription + { + get { return _machineDescription; } + set { _machineDescription = value; } + } + public string Year + { + get { return _year; } + set { _year = value; } + } + public string Manufacturer + { + get { return _manufacturer; } + set { _manufacturer = value; } + } + public string RomOf + { + get { return _romOf; } + set { _romOf = value; } + } + public string CloneOf + { + get { return _cloneOf; } + set { _cloneOf = value; } + } + public string SampleOf + { + get { return _sampleOf; } + set { _sampleOf = value; } + } + public string SourceFile + { + get { return _sourceFile; } + set { _sourceFile = value; } + } + public bool IsBios + { + get { return _isBios; } + set { _isBios = value; } + } + public string Board + { + get { return _board; } + set { _board = value; } + } + public string RebuildTo + { + get { return _rebuildTo; } + set { _rebuildTo = value; } + } + + // Source metadata information + public int SystemID + { + get { return _systemId; } + set { _systemId = value; } + } + public string System + { + get { return _systemName; } + set { _systemName = value; } + } + public int SourceID + { + get { return _sourceId; } + set { _sourceId = value; } + } + public string Source + { + get { return _sourceName; } + set { _sourceName = value; } + } + + #endregion + + #region Comparision Methods + + public int CompareTo(DatItem other) + { + int ret = 0; + + try + { + if (_name == other.Name) + { + ret = (this.Equals(other) ? 0 : 1); + } + ret = String.Compare(_name, other.Name); + } + catch + { + ret = 1; + } + + return ret; + } + + public abstract bool Equals(DatItem other); + + /// + /// Determine if an item is a duplicate using partial matching logic + /// + /// DatItem to use as a baseline + /// Logger object for console and/or file output + /// True if the roms are duplicates, false otherwise + public bool IsDuplicate(DatItem lastItem, Logger logger) + { + bool dupefound = this.Equals(lastItem); + + // More wonderful SHA-1 logging that has to be done + if (_itemType == ItemType.Rom) + { + if (((Rom)this).SHA1 == ((Rom)lastItem).SHA1 && ((Rom)this).Size != ((Rom)lastItem).Size) + { + logger.User("SHA-1 mismatch - Hash: " + ((Rom)this).SHA1); + } + } + + return dupefound; + } + + /// + /// Return the duplicate status of two items + /// + /// DatItem to check against + /// Logger object for console and/or file output + /// The DupeType corresponding to the relationship between the two + public DupeType GetDuplicateStatus(DatItem lastItem, Logger logger) + { + DupeType output = DupeType.None; + + // If we don't have a duplicate at all, return none + if (!this.IsDuplicate(lastItem, logger)) + { + return output; + } + + // If the duplicate is external already or should be, set it + if (lastItem.Dupe >= DupeType.ExternalHash || lastItem.SystemID != this.SystemID || lastItem.SourceID != this.SourceID) + { + if (lastItem.MachineName == this.MachineName && lastItem.Name == this.Name) + { + output = DupeType.ExternalAll; + } + else + { + output = DupeType.ExternalHash; + } + } + + // Otherwise, it's considered an internal dupe + else + { + if (lastItem.MachineName == this.MachineName && lastItem.Name == this.Name) + { + output = DupeType.InternalAll; + } + else + { + output = DupeType.InternalHash; + } + } + + return output; + } + + #endregion + + #region Sorting and merging + + /// + /// Determine if a rom should be included based on filters + /// + /// User supplied item to check + /// Name of the game to match (can use asterisk-partials) + /// Name of the rom to match (can use asterisk-partials) + /// Type of the rom to match + /// Find roms greater than or equal to this size + /// Find roms less than or equal to this size + /// Find roms equal to this size + /// CRC of the rom to match (can use asterisk-partials) + /// MD5 of the rom to match (can use asterisk-partials) + /// SHA-1 of the rom to match (can use asterisk-partials) + /// Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump) + /// Logging object for console and file output + /// Returns true if it should be included, false otherwise + public static bool Filter(DatItem itemdata, string gamename, string romname, string romtype, long sgt, + long slt, long seq, string crc, string md5, string sha1, bool? nodump, Logger logger) + { + // Take care of Rom and Disk specific differences + if (itemdata.Type == ItemType.Rom) + { + Rom rom = (Rom)itemdata; + + // Filter on nodump status + if (nodump == true && !rom.Nodump) + { + return false; + } + if (nodump == false && rom.Nodump) + { + return false; + } + + // Filter on rom size + if (seq != -1 && rom.Size != seq) + { + return false; + } + else + { + if (sgt != -1 && rom.Size < sgt) + { + return false; + } + if (slt != -1 && rom.Size > slt) + { + return false; + } + } + + // Filter on crc + if (!String.IsNullOrEmpty(crc)) + { + if (crc.StartsWith("*") && crc.EndsWith("*")) + { + if (!rom.CRC.ToLowerInvariant().Contains(crc.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (crc.StartsWith("*")) + { + if (!rom.CRC.EndsWith(crc.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (crc.EndsWith("*")) + { + if (!rom.CRC.StartsWith(crc.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(rom.CRC, crc, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + + // Filter on md5 + if (!String.IsNullOrEmpty(md5)) + { + if (md5.StartsWith("*") && md5.EndsWith("*")) + { + if (!rom.MD5.ToLowerInvariant().Contains(md5.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (md5.StartsWith("*")) + { + if (!rom.MD5.EndsWith(md5.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (md5.EndsWith("*")) + { + if (!rom.MD5.StartsWith(md5.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(rom.MD5, md5, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + + // Filter on sha1 + if (!String.IsNullOrEmpty(sha1)) + { + if (sha1.StartsWith("*") && sha1.EndsWith("*")) + { + if (!rom.SHA1.ToLowerInvariant().Contains(sha1.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (sha1.StartsWith("*")) + { + if (!rom.SHA1.EndsWith(sha1.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (sha1.EndsWith("*")) + { + if (!rom.SHA1.StartsWith(sha1.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(rom.SHA1, sha1, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + } + else if (itemdata.Type == ItemType.Disk) + { + Disk rom = (Disk)itemdata; + + // Filter on nodump status + if (nodump == true && !rom.Nodump) + { + return false; + } + if (nodump == false && rom.Nodump) + { + return false; + } + + // Filter on md5 + if (!String.IsNullOrEmpty(md5)) + { + if (md5.StartsWith("*") && md5.EndsWith("*")) + { + if (!rom.MD5.ToLowerInvariant().Contains(md5.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (md5.StartsWith("*")) + { + if (!rom.MD5.EndsWith(md5.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (md5.EndsWith("*")) + { + if (!rom.MD5.StartsWith(md5.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(rom.MD5, md5, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + + // Filter on sha1 + if (!String.IsNullOrEmpty(sha1)) + { + if (sha1.StartsWith("*") && sha1.EndsWith("*")) + { + if (!rom.SHA1.ToLowerInvariant().Contains(sha1.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (sha1.StartsWith("*")) + { + if (!rom.SHA1.EndsWith(sha1.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (sha1.EndsWith("*")) + { + if (!rom.SHA1.StartsWith(sha1.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(rom.SHA1, sha1, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + } + + // Filter on game name + if (!String.IsNullOrEmpty(gamename)) + { + if (gamename.StartsWith("*") && gamename.EndsWith("*")) + { + if (!itemdata.MachineName.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (gamename.StartsWith("*")) + { + if (!itemdata.MachineName.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (gamename.EndsWith("*")) + { + if (!itemdata.MachineName.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(itemdata.MachineName, gamename, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + + // Filter on rom name + if (!String.IsNullOrEmpty(romname)) + { + if (romname.StartsWith("*") && romname.EndsWith("*")) + { + if (!itemdata.Name.ToLowerInvariant().Contains(romname.ToLowerInvariant().Replace("*", ""))) + { + return false; + } + } + else if (romname.StartsWith("*")) + { + if (!itemdata.Name.EndsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else if (romname.EndsWith("*")) + { + if (!itemdata.Name.StartsWith(romname.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + else + { + if (!String.Equals(itemdata.Name, romname, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + } + } + + // Filter on rom type + if (String.IsNullOrEmpty(romtype) && itemdata.Type != ItemType.Rom && itemdata.Type != ItemType.Disk) + { + return false; + } + if (!String.IsNullOrEmpty(romtype) && !String.Equals(itemdata.Type.ToString(), romtype, StringComparison.InvariantCultureIgnoreCase)) + { + return false; + } + + return true; + } + + /// + /// Merge an arbitrary set of ROMs based on the supplied information + /// + /// List of File objects representing the roms to be merged + /// Logger object for console and/or file output + /// A List of RomData objects representing the merged roms + public static List Merge(List infiles, Logger logger) + { + // Check for null or blank roms first + if (infiles == null || infiles.Count == 0) + { + return new List(); + } + + // Create output list + List outfiles = new List(); + + // Then deduplicate them by checking to see if data matches previous saved roms + foreach (DatItem file in infiles) + { + // If it's a nodump, add and skip + if (file.Type == ItemType.Rom && ((Rom)file).Nodump) + { + outfiles.Add(file); + continue; + } + else if (file.Type == ItemType.Disk && ((Disk)file).Nodump) + { + outfiles.Add(file); + continue; + } + + // If it's the first rom in the list, don't touch it + if (outfiles.Count != 0) + { + // Check if the rom is a duplicate + DupeType dupetype = DupeType.None; + DatItem saveditem = new Rom(); + int pos = -1; + for (int i = 0; i < outfiles.Count; i++) + { + DatItem lastrom = outfiles[i]; + + // Get the duplicate status + dupetype = file.GetDuplicateStatus(lastrom, logger); + + // If it's a duplicate, skip adding it to the output but add any missing information + if (dupetype != DupeType.None) + { + // If we don't have a rom or disk, then just skip adding + if (file.Type != ItemType.Rom && file.Type != ItemType.Disk) + { + continue; + } + + saveditem = lastrom; + pos = i; + + // Roms have more infomration to save + if (file.Type == ItemType.Rom) + { + ((Rom)saveditem).Size = ((Rom)saveditem).Size; + ((Rom)saveditem).CRC = (String.IsNullOrEmpty(((Rom)saveditem).CRC) && !String.IsNullOrEmpty(((Rom)file).CRC) + ? ((Rom)file).CRC + : ((Rom)saveditem).CRC); + ((Rom)saveditem).MD5 = (String.IsNullOrEmpty(((Rom)saveditem).MD5) && !String.IsNullOrEmpty(((Rom)file).MD5) + ? ((Rom)file).MD5 + : ((Rom)saveditem).MD5); + ((Rom)saveditem).SHA1 = (String.IsNullOrEmpty(((Rom)saveditem).SHA1) && !String.IsNullOrEmpty(((Rom)file).SHA1) + ? ((Rom)file).SHA1 + : ((Rom)saveditem).SHA1); + } + else + { + ((Disk)saveditem).MD5 = (String.IsNullOrEmpty(((Disk)saveditem).MD5) && !String.IsNullOrEmpty(((Disk)file).MD5) + ? ((Disk)file).MD5 + : ((Disk)saveditem).MD5); + ((Disk)saveditem).SHA1 = (String.IsNullOrEmpty(((Disk)saveditem).SHA1) && !String.IsNullOrEmpty(((Disk)file).SHA1) + ? ((Disk)file).SHA1 + : ((Disk)saveditem).SHA1); + } + + saveditem.Dupe = dupetype; + + // If the current system has a lower ID than the previous, set the system accordingly + if (file.SystemID < saveditem.SystemID) + { + saveditem.SystemID = file.SystemID; + saveditem.System = file.System; + saveditem.MachineName = file.MachineName; + saveditem.Name = file.Name; + } + + // If the current source has a lower ID than the previous, set the source accordingly + if (file.SourceID < saveditem.SourceID) + { + saveditem.SourceID = file.SourceID; + saveditem.Source = file.Source; + saveditem.MachineName = file.MachineName; + saveditem.Name = file.Name; + } + + break; + } + } + + // If no duplicate is found, add it to the list + if (dupetype == DupeType.None) + { + outfiles.Add(file); + } + // Otherwise, if a new rom information is found, add that + else + { + outfiles.RemoveAt(pos); + outfiles.Insert(pos, saveditem); + } + } + else + { + outfiles.Add(file); + } + } + + // Then return the result + return outfiles; + } + + /// + /// List all duplicates found in a DAT based on a rom + /// + /// Item to use as a base + /// Dat to match against + /// Logger object for console and/or file output + /// True to remove matched roms from the input, false otherwise (default) + /// List of matched DatItem objects + public static List GetDuplicates(DatItem lastitem, DatFile datdata, Logger logger, bool remove = false) + { + List output = new List(); + + // Check for an empty rom list first + if (datdata.Files == null || datdata.Files.Count == 0) + { + return output; + } + + // Try to find duplicates + List keys = datdata.Files.Keys.ToList(); + foreach (string key in keys) + { + List roms = datdata.Files[key]; + List left = new List(); + + foreach (DatItem rom in roms) + { + if (rom.IsDuplicate(lastitem, 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; + } + + /// + /// Sort a list of File objects by SystemID, SourceID, Game, and Name (in order) + /// + /// List of File objects representing the roms to be sorted + /// True if files are not renamed, false otherwise + /// True if it sorted correctly, false otherwise + public static bool Sort(ref List roms, bool norename) + { + try + { + roms.Sort(delegate (DatItem x, DatItem y) + { + if (x.SystemID == y.SystemID) + { + if (x.SourceID == y.SourceID) + { + if (x.MachineName == y.MachineName) + { + if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type == ItemType.Rom || y.Type == ItemType.Disk)) + { + 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)); + } + else if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type != ItemType.Rom && y.Type != ItemType.Disk)) + { + return -1; + } + else if ((x.Type != ItemType.Rom && x.Type != ItemType.Disk) && (y.Type == ItemType.Rom || y.Type == ItemType.Disk)) + { + return 1; + } + else + { + 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)); + } + } + return Style.CompareNumeric(x.MachineName, y.MachineName); + } + return (norename ? Style.CompareNumeric(x.MachineName, y.MachineName) : x.SourceID - y.SourceID); + } + return (norename ? Style.CompareNumeric(x.MachineName, y.MachineName) : x.SystemID - y.SystemID); + }); + return true; + } + catch (Exception) + { + // Absorb the error + return false; + } + } + + #endregion + } +} From 81f217b786aac53e514ee7a14b45f91c4ca00466 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 21:04:45 -0700 Subject: [PATCH 07/13] [DatFile] Make sure that we use DatItem objects unless specifics are needed --- SabreTools.Helper/Objects/DatObjects/DatFile.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs index e45613bc..f7cdbcbc 100644 --- a/SabreTools.Helper/Objects/DatObjects/DatFile.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -2243,16 +2243,16 @@ namespace SabreTools.Helper roms = DatItem.Merge(roms, logger); } - foreach (Rom rom in roms) + foreach (DatItem rom in roms) { count++; string newkey = (norename ? "" - : rom.SystemID.ToString().PadLeft(10, '0') - + "-" - + rom.SourceID.ToString().PadLeft(10, '0') + "-") - + (String.IsNullOrEmpty(rom.MachineName) - ? "Default" - : rom.MachineName.ToLowerInvariant()); + : rom.SystemID.ToString().PadLeft(10, '0') + + "-" + + rom.SourceID.ToString().PadLeft(10, '0') + "-") + + (String.IsNullOrEmpty(rom.MachineName) + ? "Default" + : rom.MachineName.ToLowerInvariant()); if (sortable.ContainsKey(newkey)) { sortable[newkey].Add(rom); From bc87d2a935fb6e2b00ccce041143fc7cd6fb7c60 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 21:45:32 -0700 Subject: [PATCH 08/13] [DatTools] A couple more rom locations --- SabreTools.Helper/Objects/DatObjects/DatFile.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs index f7cdbcbc..87586bd5 100644 --- a/SabreTools.Helper/Objects/DatObjects/DatFile.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -2597,7 +2597,7 @@ namespace SabreTools.Helper if (roms != null && roms.Count > 0) { - foreach (Rom rom in roms) + foreach (DatItem rom in roms) { // No duplicates if ((diff & DiffMode.NoDupes) != 0 || (diff & DiffMode.Individuals) != 0) @@ -2622,7 +2622,7 @@ namespace SabreTools.Helper // Merged no-duplicates DAT if ((diff & DiffMode.NoDupes) != 0) { - Rom newrom = rom; + DatItem newrom = rom; newrom.MachineName += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")"; if (outerDiffData.Files.ContainsKey(key)) @@ -2644,7 +2644,7 @@ namespace SabreTools.Helper { if (rom.Dupe >= DupeType.ExternalHash) { - Rom newrom = rom; + DatItem newrom = rom; newrom.MachineName += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")"; if (dupeData.Files.ContainsKey(key)) @@ -2757,7 +2757,7 @@ namespace SabreTools.Helper if (roms != null && roms.Count > 0) { - foreach (Rom rom in roms) + foreach (DatItem rom in roms) { if (outDats[rom.SystemID].Files.ContainsKey(key)) { From cea31f6c7b2f6aaaec534edcf0aa769646530bb8 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 21:45:51 -0700 Subject: [PATCH 09/13] [Generate/Import] Remove original implementations --- SabreTools.Helper/Objects/Generate.cs | 467 --------------- SabreTools.Helper/Objects/Import.cs | 652 --------------------- SabreTools.Helper/SabreTools.Helper.csproj | 2 - 3 files changed, 1121 deletions(-) delete mode 100644 SabreTools.Helper/Objects/Generate.cs delete mode 100644 SabreTools.Helper/Objects/Import.cs diff --git a/SabreTools.Helper/Objects/Generate.cs b/SabreTools.Helper/Objects/Generate.cs deleted file mode 100644 index 7abfc15a..00000000 --- a/SabreTools.Helper/Objects/Generate.cs +++ /dev/null @@ -1,467 +0,0 @@ -using Mono.Data.Sqlite; -using SabreTools.Helper; -using System; -using System.Collections.Generic; -using System.IO; - -namespace SabreTools -{ - /// - /// Generate a DAT from the data in the database - /// - public class Generate : IGenerate - { - // Private instance variables - private string _systems; - private string _sources; - private string _outDir; - private string _connectionString; - private bool _norename; - private bool _old; - - // Private required variables - private Logger _logger; - - /// - /// Initialize a Generate object with the given information - /// - /// Comma-separated list of systems to be included in the DAT (blank means all) - /// Comma-separated list of sources to be included in the DAT (blank means all) - /// The output folder where the generated DAT will be put; blank means the current directory - /// Connection string for SQLite - /// Logger object for file or console output - /// True if files should not be renamed with system and/or source in merged mode (default false) - /// True if the output file should be in ClrMamePro format (default false) - public Generate(string systems, string sources, string outDir, string connectionString, Logger logger, bool norename = false, bool old = false) - { - _systems = systems; - _sources = sources; - _connectionString = connectionString; - _norename = norename; - _old = old; - _logger = logger; - - // Take care of special outfolder cases - _outDir = (outDir == "" ? Environment.CurrentDirectory + Path.DirectorySeparatorChar : - (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? outDir + Path.DirectorySeparatorChar : outDir) - ); - if (_outDir != "" && !Directory.Exists(_outDir)) - { - Directory.CreateDirectory(_outDir); - } - } - - /// - /// Generate a DAT file that is represented by the data in the Generate object. - /// - /// True if the file could be created, false otherwise - public bool Export() - { - // Check to see if the source is an import-only. If so, tell the user and exit - int id = 0; - if (_sources != "" && Int32.TryParse(_sources, out id) && id <= 14) - { - _logger.Warning("This source (" + id + ") is import-only so a DAT cannot be created. We apologize for the inconvenience."); - return false; - } - - // Get the system name, if applicable - string systemname = ""; - if (_systems != "") - { - string query = "SELECT manufacturer, system FROM systems WHERE id in (" + _systems + ")"; - //string query = "SELECT manufacturer, name FROM system WHERE id in (" + _systems + ")"; - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No system could be found with id in \"" + _systems + "\". Please check and try again."); - return false; - } - - // Retrieve and build the system name from all retrieved - int tempsize = 0; - while (sldr.Read() && tempsize < 3) - { - systemname += (tempsize == 0 ? - sldr.GetString(0) + " - " + sldr.GetString(1) : - "; " + sldr.GetString(0) + " - " + sldr.GetString(1)); - tempsize++; - } - - // If there are more than 3 systems, just put "etc." on the end - if (sldr.Read()) - { - systemname += "; etc."; - } - } - } - } - } - else - { - systemname = "ALL"; - } - - string sourcename = ""; - if (_sources != "") - { - string query = "SELECT name FROM sources WHERE id in (" + _sources + ")"; - //string query = "SELECT name FROM source WHERE id in (" + _sources + ")"; - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No source could be found with id in \"" + _sources + "\". Please check and try again."); - return false; - } - - // Retrieve and build the source name from all retrieved - int tempsize = 0; - while (sldr.Read() && tempsize < 3) - { - sourcename += (tempsize == 0 ? sldr.GetString(0) : "; " + sldr.GetString(0)); - tempsize++; - } - - // If there are more than 3 systems, just put "etc." on the end - if (sldr.Read()) - { - sourcename += "; etc."; - } - } - } - } - } - else - { - sourcename = "Merged"; - } - - // Retrieve the list of processed roms - Dictionary> dict = ProcessRoms(); - - // If the output is null, nothing was found so return false - if (dict.Count == 0) - { - return false; - } - - // Create a name for the file based on the retrieved information - string version = DateTime.Now.ToString("yyyyMMddHHmmss"); - string intname = systemname + " (" + sourcename + ")"; - string datname = systemname + " (" + sourcename + " " + version + ")"; - - DatFile datdata = new DatFile - { - Name = intname, - Description = datname, - Version = version, - Date = version, - Category = "The Wizard of DATz", - Author = "The Wizard of DATz", - ForcePacking = ForcePacking.None, - OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), - Files = dict, - }; - - return DatFile.WriteDatfile(datdata, _outDir, _logger); - } - - /// - /// Preprocess the rom data that is to be included in the outputted DAT - /// - /// A List of Rom objects containing all information about the files - public Dictionary> ProcessRoms() - { - Dictionary> roms = new Dictionary>(); - - // Check if we have listed sources or systems - bool sysmerged = (_systems == "" || _systems.Split(',').Length > 1); - bool srcmerged = (_sources == "" || _sources.Split(',').Length > 1); - bool merged = sysmerged || srcmerged; - - // BEGIN COMMENT - string query = @" -SELECT DISTINCT systems.manufacturer AS manufacturer, systems.system AS system, systems.id AS systemid, - sources.name AS source, sources.url AS url, sources.id AS sourceid, - games.name AS game, files.name AS name, files.type AS type, - checksums.size AS size, checksums.crc AS crc, checksums.md5 AS md5, checksums.sha1 AS sha1, - files.lastupdated AS lastupdated -FROM systems -JOIN games - ON systems.id=games.system -JOIN sources - ON games.source=sources.id -JOIN files - ON games.id=files.setid -JOIN checksums - ON files.id=checksums.file" + - (_systems != "" || _sources != "" ? "\nWHERE" : "") + - (_sources != "" ? " sources.id in (" + _sources + ")" : "") + - (_systems != "" && _sources != "" ? " AND" : "") + - (_systems != "" ? " systems.id in (" + _systems + ")" : "") + -"\nORDER BY " + - (merged ? "checksums.size, checksums.crc, systems.id, sources.id, files.lastupdated DESC, checksums.md5, checksums.sha1" - : "systems.id, sources.id, games.name, files.name"); - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No games could be found with those inputs. Please check and try again."); - return null; - } - - // Retrieve and process the roms for merging - while (sldr.Read()) - { - DatItem temp; - string key = ""; - switch (sldr.GetString(8).ToLowerInvariant()) - { - case "disk": - temp = new Disk(sldr.GetString(7), sldr.GetString(11), sldr.GetString(12), false, sldr.GetString(13), sldr.GetString(6), - sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, null, null, sldr.GetInt32(2), - sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); - - key = ((Disk)temp).MD5; - break; - case "rom": - default: - temp = new Rom(sldr.GetString(7), sldr.GetInt64(9), sldr.GetString(10), sldr.GetString(11), sldr.GetString(12), false, - sldr.GetString(13), sldr.GetString(6), null, sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, - null, null, sldr.GetInt32(2), sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); - - key = ((Rom)temp).Size + "-" + ((Rom)temp).CRC; - break; - } - - // Rename the game associated if it's still valid and we allow renames - if (merged && !_norename) - { - temp.MachineName = temp.MachineName + - (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + - (srcmerged ? " [" + temp.Source + "]" : ""); - } - - if (roms.ContainsKey(key)) - { - roms[key].Add(temp); - } - else - { - List templist = new List(); - templist.Add(temp); - roms.Add(key, templist); - } - } - } - } - } - - // If we're in a merged mode, merge and then resort by the correct parameters - if (merged) - { - foreach (string key in roms.Keys) - { - roms[key] = DatItem.Merge(roms[key], _logger); - } - } - // END COMMENT - - /* - // This block would replace the whole block above between BEGIN COMMENT and END COMMENT - string query = @" -SELECT hash.id AS id, hash.size AS size, hash.crc AS crc, hash.md5 AS md5, hash.sha1 AS sha1, - a.key AS key, a.value AS value, - source.id, source.name, source.url, - system.id, system.manufacturer, system.name -FROM hash -JOIN hashdata a - ON hash.id=a.hashid -JOIN hashdata b - ON a.hashid=b.hashid -JOIN gamesystem - ON b.value=gamesystem.game -JOIN gamesource - ON b.value=gamesource.game -JOIN system - ON gamesystem.systemid=system.id -JOIN source - ON gamesource.sourceid=source.id" + -(_systems != "" || _sources != "" ? "\nWHERE" : "") + - (_sources != "" ? " source.id in (" + _sources + ")" : "") + - (_systems != "" && _sources != "" ? " AND" : "") + - (_systems != "" ? " system.id in (" + _systems + ")" : "") + -"\nORDER BY hash.id"; - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No games could be found with those inputs. Please check and try again."); - return null; - } - - // Retrieve and process the roms for merging - int systemid = -1, sourceid = -1; - long lastid = -1, size = -1; - string name = "", game = "", type = "", manufacturer = "", system = "", source = "", url = "", crc = "", md5 = "", sha1 = ""; - while (sldr.Read()) - { - // If the hash is different than the last - if (lastid != -1 && sldr.GetInt64(0) != lastid) - { - Rom temp = new Rom - { - Manufacturer = manufacturer, - System = system, - SystemID = systemid, - Source = source, - URL = url, - SourceID = sourceid, - Game = game, - Name = name, - Type = type, - Size = size, - CRC = crc, - MD5 = md5, - SHA1 = sha1, - }; - - // Rename the game associated if it's still valid and we allow renames - if (merged && !_norename) - { - temp.Machine = temp.Machine + - (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + - (srcmerged ? " [" + temp.Source + "]" : ""); - } - - string key = temp.Size + "-" + temp.CRC; - if (roms.ContainsKey(key)) - { - roms[key].Add(temp); - } - else - { - List templist = new List(); - templist.Add(temp); - roms.Add(key, templist); - } - - // Reset the variables - game = ""; - name = ""; - type = ""; - } - - // Get all of the current ROM information - manufacturer = sldr.GetString(11); - system = sldr.GetString(12); - systemid = sldr.GetInt32(10); - source = sldr.GetString(8); - url = sldr.GetString(9); - sourceid = sldr.GetInt32(7); - size = sldr.GetInt64(1); - crc = sldr.GetString(2); - md5 = sldr.GetString(3); - sha1 = sldr.GetString(4); - - switch (sldr.GetString(5)) - { - case "game": - game = sldr.GetString(6); - break; - case "name": - name = sldr.GetString(6); - break; - case "type": - type = sldr.GetString(6); - break; - } - - lastid = sldr.GetInt64(0); - } - } - } - } - - // If we're in a merged mode, merge - if (merged) - { - foreach (string key in roms.Keys) - { - roms[key] = RomManipulation.Merge(roms[key]); - } - } - */ - - /* - // THIS CODE SHOULD BE PUT IN WriteToDatFromDict - - // Now check rename within games - string lastname = "", lastgame = ""; - for (int i = 0; i < roms.Count; i++) - { - Rom rom = roms[i]; - - // Now relable any roms that have the same name inside of the same game - bool samename = false, samegame = false; - if (rom.Name != "") - { - samename = (lastname == rom.Name); - } - if (rom.Machine != "") - { - samegame = (lastgame == rom.Machine); - } - - lastname = rom.Name; - lastgame = rom.Machine; - - // If the name and set are the same, rename it with whatever is different - if (samename && samegame) - { - rom.Name = Regex.Replace(rom.Name, @"^(.*)(\..*)", "$1(" + - (rom.CRC != "" ? rom.CRC : - (rom.MD5 != "" ? rom.MD5 : - (rom.SHA1 != "" ? rom.SHA1 : "Alt"))) + - ")$2"); - } - - // Assign back just in case - roms[i] = rom; - } - */ - - return roms; - } - } -} diff --git a/SabreTools.Helper/Objects/Import.cs b/SabreTools.Helper/Objects/Import.cs deleted file mode 100644 index 1287c4de..00000000 --- a/SabreTools.Helper/Objects/Import.cs +++ /dev/null @@ -1,652 +0,0 @@ -using Mono.Data.Sqlite; -using SabreTools.Helper; -using System.Collections.Generic; -using System.IO; -using System.Text.RegularExpressions; - -namespace SabreTools -{ - /// - /// Import data into the database from existing DATs - /// - public class Import : IImport - { - // Private instance variables - private string _filepath; - private string _connectionString; - private Logger _logger; - - /// - /// Initialize an Import object with the given information - /// - /// Path to the file that is going to be imported - /// Connection string for SQLite - /// Logger object for file or console output - public Import(string filepath, string connectionString, Logger logger) - { - _filepath = filepath; - _connectionString = connectionString; - _logger = logger; - } - - /// - /// Import the data from file into the database - /// - /// True if the data was imported, false otherwise - public bool UpdateDatabase() - { - // If file doesn't exist, error and return - if (!File.Exists(_filepath)) - { - _logger.Error("File '" + _filepath + "' doesn't exist"); - return false; - } - - // Determine which dattype we have - string filename = Path.GetFileName(_filepath); - GroupCollection fileinfo; - DatType type = DatType.none; - - if (Regex.IsMatch(filename, Constants.NonGoodPattern)) - { - fileinfo = Regex.Match(filename, Constants.NonGoodPattern).Groups; - type = DatType.NonGood; - } - else if (Regex.IsMatch(filename, Constants.NonGoodSpecialPattern)) - { - fileinfo = Regex.Match(filename, Constants.NonGoodSpecialPattern).Groups; - type = DatType.NonGood; - } - else if (Regex.IsMatch(filename, Constants.GoodPattern)) - { - fileinfo = Regex.Match(filename, Constants.GoodPattern).Groups; - type = DatType.Good; - } - else if (Regex.IsMatch(filename, Constants.GoodXmlPattern)) - { - fileinfo = Regex.Match(filename, Constants.GoodXmlPattern).Groups; - type = DatType.Good; - } - else if (Regex.IsMatch(filename, Constants.MaybeIntroPattern)) - { - fileinfo = Regex.Match(filename, Constants.MaybeIntroPattern).Groups; - type = DatType.MaybeIntro; - } - else if (Regex.IsMatch(filename, Constants.NoIntroPattern)) - { - fileinfo = Regex.Match(filename, Constants.NoIntroPattern).Groups; - type = DatType.NoIntro; - } - // For numbered DATs only - else if (Regex.IsMatch(filename, Constants.NoIntroNumberedPattern)) - { - fileinfo = Regex.Match(filename, Constants.NoIntroNumberedPattern).Groups; - type = DatType.NoIntro; - } - // For N-Gage and Gizmondo only - else if (Regex.IsMatch(filename, Constants.NoIntroSpecialPattern)) - { - fileinfo = Regex.Match(filename, Constants.NoIntroSpecialPattern).Groups; - type = DatType.NoIntro; - } - else if (Regex.IsMatch(filename, Constants.RedumpPattern)) - { - fileinfo = Regex.Match(filename, Constants.RedumpPattern).Groups; - type = DatType.Redump; - } - // For special BIOSes only - else if (Regex.IsMatch(filename, Constants.RedumpBiosPattern)) - { - fileinfo = Regex.Match(filename, Constants.RedumpBiosPattern).Groups; - type = DatType.Redump; - } - else if (Regex.IsMatch(filename, Constants.TosecPattern)) - { - fileinfo = Regex.Match(filename, Constants.TosecPattern).Groups; - type = DatType.TOSEC; - } - else if (Regex.IsMatch(filename, Constants.TruripPattern)) - { - fileinfo = Regex.Match(filename, Constants.TruripPattern).Groups; - type = DatType.TruRip; - } - else if (Regex.IsMatch(filename, Constants.ZandroPattern)) - { - filename = "Nintendo - Super Nintendo Entertainment System (Zandro " + File.GetLastWriteTime(_filepath).ToString("yyyyMMddHHmmss") + ").dat"; - fileinfo = Regex.Match(filename, Constants.DefaultPattern).Groups; - type = DatType.Custom; - } - else if (Regex.IsMatch(filename, Constants.DefaultPattern)) - { - fileinfo = Regex.Match(filename, Constants.DefaultPattern).Groups; - type = DatType.Custom; - } - else if (Regex.IsMatch(filename, Constants.DefaultSpecialPattern)) - { - fileinfo = Regex.Match(filename, Constants.DefaultSpecialPattern).Groups; - type = DatType.Custom; - } - else if (Regex.IsMatch(filename, Constants.MamePattern)) - { - fileinfo = Regex.Match(filename, Constants.MamePattern).Groups; - type = DatType.MAME; - } - // If the type is still unmatched, the data can't be imported yet - else - { - _logger.Warning("File " + filename + " cannot be imported at this time because it is not a known pattern.\nPlease try again with an unrenamed version."); - return false; - } - - _logger.Log("Type detected: " + type.ToString()); - - // Check for and extract import information from the file name based on type - string manufacturer = ""; - string system = ""; - string source = ""; - string datestring = ""; - string date = ""; - - switch (type) - { - case DatType.Good: - if (!Mappings.DatMaps["Good"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection goodInfo = Regex.Match(Mappings.DatMaps["Good"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = goodInfo[1].Value; - system = goodInfo[2].Value; - source = "Good"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.MAME: - if (!Mappings.DatMaps["MAME"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection mameInfo = Regex.Match(Mappings.DatMaps["MAME"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = mameInfo[1].Value; - system = mameInfo[2].Value; - source = "MAME"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.MaybeIntro: - if (!Mappings.DatMaps["MaybeIntro"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection maybeIntroInfo = Regex.Match(Mappings.DatMaps["MaybeIntro"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = maybeIntroInfo[1].Value; - system = maybeIntroInfo[2].Value; - source = "Maybe-Intro"; - datestring = fileinfo[2].Value; - GroupCollection miDateInfo = Regex.Match(datestring, Constants.NoIntroSpecialDatePattern).Groups; - date = miDateInfo[1].Value + "-" + miDateInfo[2].Value + "-" + miDateInfo[3].Value + " 00:00:00"; - break; - case DatType.NoIntro: - if (!Mappings.DatMaps["NoIntro"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection nointroInfo = Regex.Match(Mappings.DatMaps["NoIntro"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = nointroInfo[1].Value; - system = nointroInfo[2].Value; - source = "no-Intro"; - if (fileinfo.Count < 2) - { - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - } - else if (Regex.IsMatch(fileinfo[2].Value, Constants.NoIntroDatePattern)) - { - datestring = fileinfo[2].Value; - GroupCollection niDateInfo = Regex.Match(datestring, Constants.NoIntroDatePattern).Groups; - date = niDateInfo[1].Value + "-" + niDateInfo[2].Value + "-" + niDateInfo[3].Value + " " + - niDateInfo[4].Value + ":" + niDateInfo[5].Value + ":" + niDateInfo[6].Value; - } - else - { - datestring = fileinfo[2].Value; - GroupCollection niDateInfo = Regex.Match(datestring, Constants.NoIntroSpecialDatePattern).Groups; - date = niDateInfo[1].Value + "-" + niDateInfo[2].Value + "-" + niDateInfo[3].Value + " 00:00:00"; - } - break; - case DatType.NonGood: - if (!Mappings.DatMaps["NonGood"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection nonGoodInfo = Regex.Match(Mappings.DatMaps["NonGood"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = nonGoodInfo[1].Value; - system = nonGoodInfo[2].Value; - source = "NonGood"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.Redump: - if (!Mappings.DatMaps["Redump"].ContainsKey(fileinfo[1].Value)) - { - // Handle special case mappings found only in Redump - fileinfo = Regex.Match(filename, Constants.RedumpBiosPattern).Groups; - - if (!Mappings.DatMaps["Redump"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - } - GroupCollection redumpInfo = Regex.Match(Mappings.DatMaps["Redump"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = redumpInfo[1].Value; - system = redumpInfo[2].Value; - source = "Redump"; - datestring = fileinfo[2].Value; - if (Regex.IsMatch(datestring, Constants.RedumpDatePattern)) - { - GroupCollection rdDateInfo = Regex.Match(datestring, Constants.RedumpDatePattern).Groups; - date = rdDateInfo[1].Value + "-" + rdDateInfo[2].Value + "-" + rdDateInfo[3].Value + " " + - rdDateInfo[4].Value + ":" + rdDateInfo[5].Value + ":" + rdDateInfo[6].Value; - } - else - { - GroupCollection rdDateInfo = Regex.Match(datestring, Constants.TosecDatePattern).Groups; - date = rdDateInfo[1].Value + "-" + rdDateInfo[2].Value + "-" + rdDateInfo[3].Value + " 00:00:00"; - } - - break; - case DatType.TOSEC: - if (!Mappings.DatMaps["TOSEC"].ContainsKey(fileinfo[1].Value)) - { - // Handle special case mappings found only in TOSEC - fileinfo = Regex.Match(filename, Constants.TosecSpecialPatternA).Groups; - - if (!Mappings.DatMaps["TOSEC"].ContainsKey(fileinfo[1].Value)) - { - fileinfo = Regex.Match(filename, Constants.TosecSpecialPatternB).Groups; - - if (!Mappings.DatMaps["TOSEC"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - } - } - GroupCollection tosecInfo = Regex.Match(Mappings.DatMaps["TOSEC"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = tosecInfo[1].Value; - system = tosecInfo[2].Value; - source = "TOSEC"; - datestring = fileinfo[2].Value; - GroupCollection toDateInfo = Regex.Match(datestring, Constants.TosecDatePattern).Groups; - date = toDateInfo[1].Value + "-" + toDateInfo[2].Value + "-" + toDateInfo[3].Value + " 00:00:00"; - break; - case DatType.TruRip: - if (!Mappings.DatMaps["TruRip"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection truripInfo = Regex.Match(Mappings.DatMaps["TruRip"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = truripInfo[1].Value; - system = truripInfo[2].Value; - source = "trurip"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.Custom: - default: - manufacturer = fileinfo[1].Value; - system = fileinfo[2].Value; - source = fileinfo[3].Value; - datestring = fileinfo[4].Value; - - GroupCollection cDateInfo = Regex.Match(datestring, Constants.DefaultDatePattern).Groups; - date = cDateInfo[1].Value + "-" + cDateInfo[2].Value + "-" + cDateInfo[3].Value + " " + - cDateInfo[4].Value + ":" + cDateInfo[5].Value + ":" + cDateInfo[6].Value; - break; - } - - // Check to make sure that the manufacturer and system are valid according to the database - int sysid = -1; - string query = "SELECT id FROM systems WHERE manufacturer='" + manufacturer + "' AND system='" + system +"'"; - //string query = "SELECT id FROM system WHERE manufacturer='" + manufacturer + "' AND name='" + system + "'"; - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, tell the user and exit - if (!sldr.HasRows) - { - _logger.Error("No suitable system for '" + filename + "' (" + manufacturer + " " + system + ") found! Please add the system and then try again."); - return false; - } - - // Set the system ID from the first found value - sldr.Read(); - sysid = sldr.GetInt32(0); - } - } - } - - // Check to make sure that the source is valid according to the database - int srcid = -1; - query = "SELECT id FROM sources WHERE name='" + source + "'"; - //query = "SELECT id FROM source WHERE name='" + source + "'"; - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, tell the user and exit - if (!sldr.HasRows) - { - _logger.Error("No suitable source for '" + filename + "' found! Please add the source and then try again."); - return false; - } - - // Set the source ID from the first found value - sldr.Read(); - srcid = sldr.GetInt32(0); - } - } - } - - // Get all roms that are found in the DAT to see what needs to be added - DatFile datdata = new DatFile(); - DatFile.Parse(_filepath, sysid, srcid, ref datdata, _logger); - - // Sort inputted roms into games - SortedDictionary> sortable = new SortedDictionary>(); - long count = 0; - foreach (List roms in datdata.Files.Values) - { - List newroms = roms; - if (datdata.MergeRoms) - { - newroms = DatItem.Merge(newroms, _logger); - } - - foreach (Rom rom in newroms) - { - count++; - string key = rom.SystemID.ToString().PadLeft(10, '0') + "-" + rom.SourceID.ToString().PadLeft(10, '0') + "-" + rom.MachineName.ToLowerInvariant(); - if (sortable.ContainsKey(key)) - { - sortable[key].Add(rom); - } - else - { - List temp = new List(); - temp.Add(rom); - sortable.Add(key, temp); - } - } - } - - // Loop over all roms, checking for adds - foreach (string key in sortable.Keys) - { - List roms = sortable[key]; - DatItem.Sort(ref roms, true); - - long gameid = -1; - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - - // For each game, check for a new ID - gameid = AddGame(sysid, roms[0].MachineName, srcid, dbc); - - foreach (Rom rom in roms) - { - // BEGIN COMMENT - // Try to add the rom with the game information - AddRom(rom, gameid, date, dbc); - // END COMMENT - - /* - // Try to add the romdata - AddHash(rom, sysid, srcid, date, dbc); - */ - } - } - } - - return true; - } - - /// - /// Add a game to the database if it doesn't already exist - /// - /// System ID for the game to be added with - /// Name of the game to be added - /// Source ID for the game to be added with - /// SQLite database connection to use - /// Game ID of the inserted (or found) game, -1 on error - private long AddGame(int sysid, string machinename, int srcid, SqliteConnection dbc) - { - // WoD gets rid of anything past the first "(" or "[" as the name, we will do the same - string stripPattern = @"(([[(].*[\)\]] )?([^([]+))"; - Regex stripRegex = new Regex(stripPattern); - Match stripMatch = stripRegex.Match(machinename); - machinename = stripMatch.Groups[1].Value; - - // Run the name through the filters to make sure that it's correct - machinename = Style.NormalizeChars(machinename); - machinename = Style.RussianToLatin(machinename); - machinename = Style.SearchPattern(machinename); - machinename = machinename.Trim(); - - long gameid = -1; - string query = "SELECT id FROM games WHERE system=" + sysid + - " AND name='" + machinename.Replace("'", "''") + "'" + - " AND source=" + srcid; - - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, add the game and get the insert ID - if (!sldr.HasRows) - { - query = "INSERT INTO games (system, name, source)" + - " VALUES (" + sysid + ", '" + machinename.Replace("'", "''") + "', " + srcid + ")"; - - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - slc2.ExecuteNonQuery(); - } - - query = "SELECT last_insertConstants.Rowid()"; - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - gameid = (long)slc2.ExecuteScalar(); - } - } - // Otherwise, retrieve the ID - else - { - sldr.Read(); - gameid = sldr.GetInt64(0); - } - } - } - - return gameid; - } - - /// - /// Add a file to the database if it doesn't already exist - /// - /// Rom object representing the rom - /// ID of the parent game to be mapped to - /// Last updated date - /// SQLite database connection to use - /// True if the file exists or could be added, false on error - private bool AddRom(Rom rom, long gameid, string date, SqliteConnection dbc) - { - // WOD origninally stripped out any subdirs from the imported files, we do the same - rom.Name = Path.GetFileName(rom.Name); - - // Run the name through the filters to make sure that it's correct - rom.Name = Style.NormalizeChars(rom.Name); - rom.Name = Style.RussianToLatin(rom.Name); - rom.Name = Regex.Replace(rom.Name, @"(.*) \.(.*)", "$1.$2"); - - if (rom.Type != ItemType.Rom && rom.Type != ItemType.Disk) - { - rom.Type = ItemType.Rom; - } - - // Check to see if this exact file is in the database already - string query = @" -SELECT files.id FROM files - JOIN checksums - ON files.id=checksums.file - WHERE files.name='" + rom.Name.Replace("'", "''") + @"' - AND files.type='" + rom.Type + @"' - AND files.setid=" + gameid + - " AND checksums.size=" + rom.Size + - " AND checksums.crc='" + rom.CRC + "'" + - " AND checksums.md5='" + rom.MD5 + "'" + - " AND checksums.sha1='" + rom.SHA1 + "'"; - - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If the file doesn't exist, add it with its checksums - if (!sldr.HasRows) - { - query = @"BEGIN; -INSERT INTO files (setid, name, type, lastupdated) -VALUES (" + gameid + ", '" + rom.Name.Replace("'", "''") + "', '" + rom.Type + "', '" + date + @"'); -INSERT INTO checksums (file, size, crc, md5, sha1) -VALUES ((SELECT last_insertConstants.Rowid()), " + rom.Size + ", '" + rom.CRC + "'" + ", '" + rom.MD5 + "'" + ", '" + rom.SHA1 + @"'); -COMMIT;"; - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - int affected = slc2.ExecuteNonQuery(); - - // If the insert was unsuccessful, something bad happened - if (affected < 1) - { - _logger.Error("There was an error adding " + rom.Name + " to the database!"); - return false; - } - } - } - } - } - - return true; - } - - /// - /// Add a hash to the database if it doesn't exist already - /// - /// Rom object representing the rom - /// System ID for the game to be added with - /// Source ID for the game to be added with - /// Last updated date - /// SQLite database connection to use - /// True if the hash exists or could be added, false on error - /// This is currently unused. It is a test method for the new SabreTools DB schema - private bool AddHash(Rom rom, int sysid, int srcid, string date, SqliteConnection dbc) - { - // Process the game name - - // WoD gets rid of anything past the first "(" or "[" as the name, we will do the same - string stripPattern = @"(([[(].*[\)\]] )?([^([]+))"; - Regex stripRegex = new Regex(stripPattern); - Match stripMatch = stripRegex.Match(rom.MachineName); - rom.MachineName = stripMatch.Groups[1].Value; - - // Run the name through the filters to make sure that it's correct - rom.MachineName = Style.NormalizeChars(rom.MachineName); - rom.MachineName = Style.RussianToLatin(rom.MachineName); - rom.MachineName = Style.SearchPattern(rom.MachineName); - rom.MachineName = rom.MachineName.Trim(); - - // Process the rom name - - // WOD origninally stripped out any subdirs from the imported files, we do the same - rom.Name = Path.GetFileName(rom.Name); - - // Run the name through the filters to make sure that it's correct - rom.Name = Style.NormalizeChars(rom.Name); - rom.Name = Style.RussianToLatin(rom.Name); - rom.Name = Regex.Replace(rom.Name, @"(.*) \.(.*)", "$1.$2"); - - // Retrieve or insert the hash - long hashid = -1; - string query = "SELECT id FROM hash WHERE size=" + rom.Size + " AND crc='" + rom.CRC + "' AND md5='" + rom.MD5 + "' AND sha1='" + rom.SHA1 + "'"; - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, add the hash and get the insert ID - if (!sldr.HasRows) - { - query = "INSERT INTO hash (size, crc, md5, sha1)" + - " VALUES (" + rom.Size + ", '" + rom.CRC + "', '" + rom.MD5 + "', '" + rom.SHA1 + "')"; - - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - slc2.ExecuteNonQuery(); - } - - query = "SELECT last_insertConstants.Rowid()"; - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - hashid = (long)slc2.ExecuteScalar(); - } - } - // Otherwise, retrieve the ID - else - { - sldr.Read(); - hashid = sldr.GetInt64(0); - } - } - } - - // Ignore or insert the file and game - query = @"BEGIN; -INSERT OR IGNORE INTO hashdata (hashid, key, value) VALUES " + - "(" + hashid + ", 'name', '" + rom.Name.Replace("'", "''") + "'), " + - "(" + hashid + ", 'game', '" + rom.MachineName.Replace("'", "''") + "'), " + - "(" + hashid + ", 'type', '" + rom.Type + "'), " + - "(" + hashid + ", 'lastupdated', '" + date + @"'); -INSERT OR IGNORE INTO gamesystem (game, systemid) VALUES ('" + rom.MachineName.Replace("'", "''") + "', " + sysid + @"); -INSERT OR IGNORE INTO gamesource (game, sourceid) VALUES ('" + rom.MachineName.Replace("'", "''") + "', " + srcid + @"); -COMMIT;"; - - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - int ret = slc.ExecuteNonQuery(); - if ((SQLiteErrorCode)ret == SQLiteErrorCode.Error) - { - _logger.Error("A SQLite error has occurred: " + ((SQLiteErrorCode)ret).ToString()); - return false; - } - } - - return true; - } - } -} diff --git a/SabreTools.Helper/SabreTools.Helper.csproj b/SabreTools.Helper/SabreTools.Helper.csproj index 24aa4802..8f9c4a3c 100644 --- a/SabreTools.Helper/SabreTools.Helper.csproj +++ b/SabreTools.Helper/SabreTools.Helper.csproj @@ -108,10 +108,8 @@ - - From 9a7be539fb59a79f92f39dc72fbed11a15423f5c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 22:01:16 -0700 Subject: [PATCH 10/13] [DatFile] Add temporary fix with logging statement --- SabreTools.Helper/Objects/DatObjects/DatFile.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/DatObjects/DatFile.cs index 87586bd5..f3a83809 100644 --- a/SabreTools.Helper/Objects/DatObjects/DatFile.cs +++ b/SabreTools.Helper/Objects/DatObjects/DatFile.cs @@ -2759,6 +2759,13 @@ namespace SabreTools.Helper { foreach (DatItem rom in roms) { + // There's odd cases where there are items with System ID < 0. Skip them for now + if (rom.SystemID < 0) + { + logger.Warning("Item found with a <0 SystemID: " + rom.Name); + continue; + } + if (outDats[rom.SystemID].Files.ContainsKey(key)) { outDats[rom.SystemID].Files[key].Add(rom); From de5e63a6953b42cf0b475359dc238e72abdaf0bc Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 22:28:17 -0700 Subject: [PATCH 11/13] [DatItem] Fix child classes equatiblity --- SabreTools.Helper/Objects/DatObjects/Archive.cs | 6 +++--- SabreTools.Helper/Objects/DatObjects/BiosSet.cs | 6 +++--- SabreTools.Helper/Objects/DatObjects/Disk.cs | 2 +- SabreTools.Helper/Objects/DatObjects/Release.cs | 6 +++--- SabreTools.Helper/Objects/DatObjects/Rom.cs | 2 +- SabreTools.Helper/Objects/DatObjects/Sample.cs | 6 +++--- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/SabreTools.Helper/Objects/DatObjects/Archive.cs b/SabreTools.Helper/Objects/DatObjects/Archive.cs index 1f0eaea2..ca53d114 100644 --- a/SabreTools.Helper/Objects/DatObjects/Archive.cs +++ b/SabreTools.Helper/Objects/DatObjects/Archive.cs @@ -73,13 +73,13 @@ public override bool Equals(DatItem other) { - // If we don't have a rom, return false - if (_itemType == other.Type) + // If we don't have an archive, return false + if (_itemType != other.Type) { return false; } - // Otherwise, treat it as a rom + // Otherwise, treat it as an archive Archive newOther = (Archive)other; // If the archive information matches diff --git a/SabreTools.Helper/Objects/DatObjects/BiosSet.cs b/SabreTools.Helper/Objects/DatObjects/BiosSet.cs index ec412bcf..6600be81 100644 --- a/SabreTools.Helper/Objects/DatObjects/BiosSet.cs +++ b/SabreTools.Helper/Objects/DatObjects/BiosSet.cs @@ -103,13 +103,13 @@ public override bool Equals(DatItem other) { - // If we don't have a rom, return false - if (_itemType == other.Type) + // If we don't have a biosset, return false + if (_itemType != other.Type) { return false; } - // Otherwise, treat it as a rom + // Otherwise, treat it as a biosset BiosSet newOther = (BiosSet)other; // If the archive information matches diff --git a/SabreTools.Helper/Objects/DatObjects/Disk.cs b/SabreTools.Helper/Objects/DatObjects/Disk.cs index 9a2db1f6..5210ace4 100644 --- a/SabreTools.Helper/Objects/DatObjects/Disk.cs +++ b/SabreTools.Helper/Objects/DatObjects/Disk.cs @@ -124,7 +124,7 @@ namespace SabreTools.Helper bool dupefound = false; // If we don't have a rom, return false - if (_itemType == other.Type) + if (_itemType != other.Type) { return dupefound; } diff --git a/SabreTools.Helper/Objects/DatObjects/Release.cs b/SabreTools.Helper/Objects/DatObjects/Release.cs index a82b6433..02300921 100644 --- a/SabreTools.Helper/Objects/DatObjects/Release.cs +++ b/SabreTools.Helper/Objects/DatObjects/Release.cs @@ -127,13 +127,13 @@ public override bool Equals(DatItem other) { - // If we don't have a rom, return false - if (_itemType == other.Type) + // If we don't have a release return false + if (_itemType != other.Type) { return false; } - // Otherwise, treat it as a rom + // Otherwise, treat it as a reease Release newOther = (Release)other; // If the archive information matches diff --git a/SabreTools.Helper/Objects/DatObjects/Rom.cs b/SabreTools.Helper/Objects/DatObjects/Rom.cs index 5bb18a8b..04d92a91 100644 --- a/SabreTools.Helper/Objects/DatObjects/Rom.cs +++ b/SabreTools.Helper/Objects/DatObjects/Rom.cs @@ -145,7 +145,7 @@ namespace SabreTools.Helper bool dupefound = false; // If we don't have a rom, return false - if (_itemType == other.Type) + if (_itemType != other.Type) { return dupefound; } diff --git a/SabreTools.Helper/Objects/DatObjects/Sample.cs b/SabreTools.Helper/Objects/DatObjects/Sample.cs index 01331f1c..9791c0cb 100644 --- a/SabreTools.Helper/Objects/DatObjects/Sample.cs +++ b/SabreTools.Helper/Objects/DatObjects/Sample.cs @@ -73,13 +73,13 @@ public override bool Equals(DatItem other) { - // If we don't have a rom, return false - if (_itemType == other.Type) + // If we don't have a sample, return false + if (_itemType != other.Type) { return false; } - // Otherwise, treat it as a rom + // Otherwise, treat it as a sample Sample newOther = (Sample)other; // If the archive information matches From df2a06c2e2bfe3004949d8f1bdd77de3426e2657 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 Sep 2016 22:53:33 -0700 Subject: [PATCH 12/13] [SimpleSort] Fix hash copying --- SabreTools.Helper/Objects/SimpleSort.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index 0d6048b6..ddb74f0e 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -673,7 +673,10 @@ namespace SabreTools.Helper // Then output the headered rom (renamed) Rom newfound = found; newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.CRC + ")" + Path.GetExtension(newfound.Name); - newfound = rom; + newfound.Size = rom.Size; + newfound.CRC = rom.CRC; + newfound.MD5 = rom.MD5; + newfound.SHA1 = rom.SHA1; // Add rom to the matched list key = newfound.Size + "-" + newfound.CRC; From d625404dea5067bdfbe9b5a5c8cb71126715b97e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 20 Sep 2016 10:19:38 -0700 Subject: [PATCH 13/13] [SabreTools.Helper] Move some things around --- .../{ZipFIle.cs => Archive/ZipFile.cs} | 0 .../Objects/{ => Archive}/ZipFileEntry.cs | 0 .../Objects/{DatObjects => Dat}/Archive.cs | 0 .../Objects/{DatObjects => Dat}/BiosSet.cs | 0 .../Objects/{DatObjects => Dat}/DatFile.cs | 0 .../Objects/{DatObjects => Dat}/DatItem.cs | 0 .../Objects/{DatObjects => Dat}/Disk.cs | 0 .../Objects/{DatObjects => Dat}/Release.cs | 0 .../Objects/{DatObjects => Dat}/Rom.cs | 0 .../Objects/{DatObjects => Dat}/Sample.cs | 0 SabreTools.Helper/SabreTools.Helper.csproj | 20 +++++++++---------- 11 files changed, 10 insertions(+), 10 deletions(-) rename SabreTools.Helper/Objects/{ZipFIle.cs => Archive/ZipFile.cs} (100%) rename SabreTools.Helper/Objects/{ => Archive}/ZipFileEntry.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/Archive.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/BiosSet.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/DatFile.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/DatItem.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/Disk.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/Release.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/Rom.cs (100%) rename SabreTools.Helper/Objects/{DatObjects => Dat}/Sample.cs (100%) diff --git a/SabreTools.Helper/Objects/ZipFIle.cs b/SabreTools.Helper/Objects/Archive/ZipFile.cs similarity index 100% rename from SabreTools.Helper/Objects/ZipFIle.cs rename to SabreTools.Helper/Objects/Archive/ZipFile.cs diff --git a/SabreTools.Helper/Objects/ZipFileEntry.cs b/SabreTools.Helper/Objects/Archive/ZipFileEntry.cs similarity index 100% rename from SabreTools.Helper/Objects/ZipFileEntry.cs rename to SabreTools.Helper/Objects/Archive/ZipFileEntry.cs diff --git a/SabreTools.Helper/Objects/DatObjects/Archive.cs b/SabreTools.Helper/Objects/Dat/Archive.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/Archive.cs rename to SabreTools.Helper/Objects/Dat/Archive.cs diff --git a/SabreTools.Helper/Objects/DatObjects/BiosSet.cs b/SabreTools.Helper/Objects/Dat/BiosSet.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/BiosSet.cs rename to SabreTools.Helper/Objects/Dat/BiosSet.cs diff --git a/SabreTools.Helper/Objects/DatObjects/DatFile.cs b/SabreTools.Helper/Objects/Dat/DatFile.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/DatFile.cs rename to SabreTools.Helper/Objects/Dat/DatFile.cs diff --git a/SabreTools.Helper/Objects/DatObjects/DatItem.cs b/SabreTools.Helper/Objects/Dat/DatItem.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/DatItem.cs rename to SabreTools.Helper/Objects/Dat/DatItem.cs diff --git a/SabreTools.Helper/Objects/DatObjects/Disk.cs b/SabreTools.Helper/Objects/Dat/Disk.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/Disk.cs rename to SabreTools.Helper/Objects/Dat/Disk.cs diff --git a/SabreTools.Helper/Objects/DatObjects/Release.cs b/SabreTools.Helper/Objects/Dat/Release.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/Release.cs rename to SabreTools.Helper/Objects/Dat/Release.cs diff --git a/SabreTools.Helper/Objects/DatObjects/Rom.cs b/SabreTools.Helper/Objects/Dat/Rom.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/Rom.cs rename to SabreTools.Helper/Objects/Dat/Rom.cs diff --git a/SabreTools.Helper/Objects/DatObjects/Sample.cs b/SabreTools.Helper/Objects/Dat/Sample.cs similarity index 100% rename from SabreTools.Helper/Objects/DatObjects/Sample.cs rename to SabreTools.Helper/Objects/Dat/Sample.cs diff --git a/SabreTools.Helper/SabreTools.Helper.csproj b/SabreTools.Helper/SabreTools.Helper.csproj index 8f9c4a3c..da7d6e96 100644 --- a/SabreTools.Helper/SabreTools.Helper.csproj +++ b/SabreTools.Helper/SabreTools.Helper.csproj @@ -101,20 +101,20 @@ - - - - - - - + + + + + + + - + - - + + True True