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(); }