From 5fce4c84b589f8b2556657dd098a24f0e123401c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 7 Apr 2023 16:13:15 -0400 Subject: [PATCH] Wire up skeleton formats to DatItem --- SabreTools.DatItems/DatItem.cs | 17 +++++++-- SabreTools.DatItems/Formats/File.cs | 56 +++++++++++++++++++++++++++++ SabreTools.DatTools/Rebuilder.cs | 30 +++++++++------- SabreTools.DatTools/Verification.cs | 2 +- 4 files changed, 88 insertions(+), 17 deletions(-) diff --git a/SabreTools.DatItems/DatItem.cs b/SabreTools.DatItems/DatItem.cs index d3adb4ed..dcc278ed 100644 --- a/SabreTools.DatItems/DatItem.cs +++ b/SabreTools.DatItems/DatItem.cs @@ -169,6 +169,7 @@ namespace SabreTools.DatItems ItemType.Chip => new Chip(), ItemType.Condition => new Condition(), ItemType.Configuration => new Configuration(), + ItemType.Details => new Details(), ItemType.Device => new Device(), ItemType.DeviceReference => new DeviceReference(), ItemType.DipSwitch => new DipSwitch(), @@ -177,6 +178,7 @@ namespace SabreTools.DatItems ItemType.Driver => new Driver(), ItemType.Extension => new Extension(), ItemType.Feature => new Feature(), + ItemType.File => new Formats.File(), ItemType.Info => new Info(), ItemType.Instance => new Instance(), ItemType.Location => new Location(), @@ -187,6 +189,7 @@ namespace SabreTools.DatItems ItemType.Release => new Release(), ItemType.Rom => new Rom(), ItemType.Sample => new Sample(), + ItemType.Serials => new Serials(), ItemType.SharedFeature => new SharedFeature(), ItemType.Slot => new Slot(), ItemType.SlotOption => new SlotOption(), @@ -548,9 +551,12 @@ namespace SabreTools.DatItems if (file == null) continue; - // If we don't have a Disk, Media, or Rom, we skip checking for duplicates - if (file.ItemType != ItemType.Disk && file.ItemType != ItemType.Media && file.ItemType != ItemType.Rom) + // If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates + if (file.ItemType != ItemType.Disk && file.ItemType != ItemType.File + && file.ItemType != ItemType.Media && file.ItemType != ItemType.Rom) + { continue; + } // If it's a nodump, add and skip if (file.ItemType == ItemType.Rom && (file as Rom).ItemStatus == ItemStatus.Nodump) @@ -592,6 +598,8 @@ namespace SabreTools.DatItems // Disks, Media, and Roms have more information to fill if (file.ItemType == ItemType.Disk) (saveditem as Disk).FillMissingInformation(file as Disk); + else if (file.ItemType == ItemType.File) + (saveditem as Formats.File).FillMissingInformation(file as Formats.File); else if (file.ItemType == ItemType.Media) (saveditem as Media).FillMissingInformation(file as Media); else if (file.ItemType == ItemType.Rom) @@ -682,7 +690,8 @@ namespace SabreTools.DatItems { staticLogger.Verbose($"Name duplicate found for '{datItemName}'"); - if (datItem.ItemType == ItemType.Disk || datItem.ItemType == ItemType.Media || datItem.ItemType == ItemType.Rom) + if (datItem.ItemType == ItemType.Disk || datItem.ItemType == ItemType.File + || datItem.ItemType == ItemType.Media || datItem.ItemType == ItemType.Rom) { datItemName += GetDuplicateSuffix(datItem); lastrenamed ??= datItemName; @@ -731,6 +740,8 @@ namespace SabreTools.DatItems { if (datItem.ItemType == ItemType.Disk) return (datItem as Disk).GetDuplicateSuffix(); + else if (datItem.ItemType == ItemType.File) + return (datItem as Formats.File).GetDuplicateSuffix(); else if (datItem.ItemType == ItemType.Media) return (datItem as Media).GetDuplicateSuffix(); else if (datItem.ItemType == ItemType.Rom) diff --git a/SabreTools.DatItems/Formats/File.cs b/SabreTools.DatItems/Formats/File.cs index 2eb25c20..58d87825 100644 --- a/SabreTools.DatItems/Formats/File.cs +++ b/SabreTools.DatItems/Formats/File.cs @@ -4,6 +4,7 @@ using System.Xml.Serialization; using Newtonsoft.Json; using SabreTools.Core; using SabreTools.Core.Tools; +using SabreTools.FileTypes; // TODO: Add item mappings for all fields namespace SabreTools.DatItems.Formats @@ -104,6 +105,21 @@ namespace SabreTools.DatItems.Formats ItemType = ItemType.File; } + /// + /// Create a File object from a BaseFile + /// + /// + public File(BaseFile baseFile) + { + _crc = baseFile.CRC; + _md5 = baseFile.MD5; + _sha1 = baseFile.SHA1; + _sha256 = baseFile.SHA256; + + ItemType = ItemType.File; + DupeType = 0x00; + } + #endregion #region Cloning Methods @@ -131,6 +147,46 @@ namespace SabreTools.DatItems.Formats }; } + /// + /// Convert Disk object to a BaseFile + /// + public BaseFile ConvertToBaseFile() + { + return new BaseFile() + { + Parent = this.Machine?.Name, + CRC = this._crc, + MD5 = this._md5, + SHA1 = this._sha1, + SHA256 = this._sha256, + }; + } + + /// + /// Convert a disk to the closest Rom approximation + /// + /// + public Rom ConvertToRom() + { + var rom = new Rom() + { + Name = $"{this.Id}.{this.Extension}", + ItemType = ItemType.Rom, + DupeType = this.DupeType, + + Machine = this.Machine.Clone() as Machine, + Source = this.Source.Clone() as Source, + Remove = this.Remove, + + CRC = this.CRC, + MD5 = this.MD5, + SHA1 = this.SHA1, + SHA256 = this.SHA256, + }; + + return rom; + } + #endregion #region Comparision Methods diff --git a/SabreTools.DatTools/Rebuilder.cs b/SabreTools.DatTools/Rebuilder.cs index baa06efa..680f9dc5 100644 --- a/SabreTools.DatTools/Rebuilder.cs +++ b/SabreTools.DatTools/Rebuilder.cs @@ -114,7 +114,7 @@ namespace SabreTools.DatTools string foundpath = null; foreach (string directory in directories) { - if (File.Exists(Path.Combine(directory, subpath))) + if (System.IO.File.Exists(Path.Combine(directory, subpath))) { foundpath = Path.Combine(directory, subpath); break; @@ -143,15 +143,17 @@ namespace SabreTools.DatTools // Otherwise, we rebuild that file to all locations that we need to bool usedInternally; if (datFile.Items[hash][0].ItemType == ItemType.Disk) - usedInternally = RebuildIndividualFile(datFile, new Disk(fileinfo), foundpath, outDir, date, inverse, outputFormat, false /* isZip */); + usedInternally = RebuildIndividualFile(datFile, new Disk(fileinfo), foundpath, outDir, date, inverse, outputFormat, isZip: false); + else if (datFile.Items[hash][0].ItemType == ItemType.File) + usedInternally = RebuildIndividualFile(datFile, new DatItems.Formats.File(fileinfo), foundpath, outDir, date, inverse, outputFormat, isZip: false); else if (datFile.Items[hash][0].ItemType == ItemType.Media) - usedInternally = RebuildIndividualFile(datFile, new Media(fileinfo), foundpath, outDir, date, inverse, outputFormat, false /* isZip */); + usedInternally = RebuildIndividualFile(datFile, new Media(fileinfo), foundpath, outDir, date, inverse, outputFormat, isZip: false); else - usedInternally = RebuildIndividualFile(datFile, new Rom(fileinfo), foundpath, outDir, date, inverse, outputFormat, false /* isZip */); + usedInternally = RebuildIndividualFile(datFile, new Rom(fileinfo), foundpath, outDir, date, inverse, outputFormat, isZip: false); // If we are supposed to delete the depot file, do so if (delete && usedInternally) - File.Delete(foundpath); + System.IO.File.Delete(foundpath); } watch.Stop(); @@ -219,14 +221,14 @@ namespace SabreTools.DatTools foreach (string input in inputs) { // If the input is a file - if (File.Exists(input)) + if (System.IO.File.Exists(input)) { logger.User($"Checking file: {input}"); bool rebuilt = RebuildGenericHelper(datFile, input, outDir, quickScan, date, inverse, outputFormat, asFiles); // If we are supposed to delete the file, do so if (delete && rebuilt) - File.Delete(input); + System.IO.File.Delete(input); } // If the input is a directory @@ -240,7 +242,7 @@ namespace SabreTools.DatTools // If we are supposed to delete the file, do so if (delete && rebuilt) - File.Delete(input); + System.IO.File.Delete(input); } } } @@ -300,7 +302,7 @@ namespace SabreTools.DatTools } // If the entries list is null, we encountered an error or have a file and should scan externally - if (entries == null && File.Exists(file)) + if (entries == null && System.IO.File.Exists(file)) { BaseFile internalFileInfo = BaseFile.GetInfo(file, asFiles: asFiles); @@ -361,9 +363,11 @@ namespace SabreTools.DatTools outputFormat = OutputFormat.Folder; } - // If we have a Disk or Media, change it into a Rom for later use + // If we have a Disk, File, or Media, change it into a Rom for later use if (datItem.ItemType == ItemType.Disk) datItem = (datItem as Disk).ConvertToRom(); + else if (datItem.ItemType == ItemType.File) + datItem = (datItem as DatItems.Formats.File).ConvertToRom(); else if (datItem.ItemType == ItemType.Media) datItem = (datItem as Media).ConvertToRom(); @@ -558,7 +562,7 @@ namespace SabreTools.DatTools // Now copy the file over try { - File.Copy(file, outDir); + System.IO.File.Copy(file, outDir); return true; } catch @@ -602,7 +606,7 @@ namespace SabreTools.DatTools // Now copy the file over try { - File.Copy(file, outDir); + System.IO.File.Copy(file, outDir); return true; } catch @@ -637,7 +641,7 @@ namespace SabreTools.DatTools // Otherwise, just open the filestream else { - stream = File.OpenRead(file); + stream = System.IO.File.OpenRead(file); } // If the stream is null, then continue diff --git a/SabreTools.DatTools/Verification.cs b/SabreTools.DatTools/Verification.cs index 53fd9d04..8df91990 100644 --- a/SabreTools.DatTools/Verification.cs +++ b/SabreTools.DatTools/Verification.cs @@ -75,7 +75,7 @@ namespace SabreTools.DatTools string foundpath = null; foreach (string directory in directories) { - if (File.Exists(Path.Combine(directory, subpath))) + if (System.IO.File.Exists(Path.Combine(directory, subpath))) { foundpath = Path.Combine(directory, subpath); break;