diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index 88965783..7b93ef71 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -81,7 +81,7 @@ namespace SabreTools.DatFiles DatFormat.ArchiveDotOrg => new ArchiveDotOrg(baseDat), DatFormat.AttractMode => new AttractMode(baseDat), DatFormat.ClrMamePro => new ClrMamePro(baseDat, quotes), - DatFormat.CSV => new SeparatedValue(baseDat, ','), + DatFormat.CSV => new CommaSeparatedValue(baseDat), DatFormat.DOSCenter => new DosCenter(baseDat), DatFormat.EverdriveSMDB => new EverdriveSMDB(baseDat), DatFormat.Listrom => new Listrom(baseDat), @@ -91,19 +91,19 @@ namespace SabreTools.DatFiles DatFormat.MissFile => new Missfile(baseDat), DatFormat.OfflineList => new OfflineList(baseDat), DatFormat.OpenMSX => new OpenMSX(baseDat), - DatFormat.RedumpMD5 => new Hashfile(baseDat, HashType.MD5), - DatFormat.RedumpSFV => new Hashfile(baseDat, HashType.CRC32), - DatFormat.RedumpSHA1 => new Hashfile(baseDat, HashType.SHA1), - DatFormat.RedumpSHA256 => new Hashfile(baseDat, HashType.SHA256), - DatFormat.RedumpSHA384 => new Hashfile(baseDat, HashType.SHA384), - DatFormat.RedumpSHA512 => new Hashfile(baseDat, HashType.SHA512), - DatFormat.RedumpSpamSum => new Hashfile(baseDat, HashType.SpamSum), + DatFormat.RedumpMD5 => new Md5File(baseDat), + DatFormat.RedumpSFV => new SfvFile(baseDat), + DatFormat.RedumpSHA1 => new Sha1File(baseDat), + DatFormat.RedumpSHA256 => new Sha256File(baseDat), + DatFormat.RedumpSHA384 => new Sha384File(baseDat), + DatFormat.RedumpSHA512 => new Sha512File(baseDat), + DatFormat.RedumpSpamSum => new SpamSumFile(baseDat), DatFormat.RomCenter => new RomCenter(baseDat), DatFormat.SabreJSON => new SabreJSON(baseDat), DatFormat.SabreXML => new SabreXML(baseDat), DatFormat.SoftwareList => new Formats.SoftwareList(baseDat), - DatFormat.SSV => new SeparatedValue(baseDat, ';'), - DatFormat.TSV => new SeparatedValue(baseDat, '\t'), + DatFormat.SSV => new SemicolonSeparatedValue(baseDat), + DatFormat.TSV => new TabSeparatedValue(baseDat), // We use new-style Logiqx as a backup for generic DatFile _ => new Logiqx(baseDat, false), diff --git a/SabreTools.DatFiles/Formats/Hashfile.cs b/SabreTools.DatFiles/Formats/Hashfile.cs index 6505ea1d..dcf603db 100644 --- a/SabreTools.DatFiles/Formats/Hashfile.cs +++ b/SabreTools.DatFiles/Formats/Hashfile.cs @@ -3,45 +3,24 @@ using System.Collections.Generic; using SabreTools.Core; using SabreTools.DatItems; using SabreTools.DatItems.Formats; -using SabreTools.Hashing; namespace SabreTools.DatFiles.Formats { /// /// Represents a hashfile such as an SFV, MD5, or SHA-1 file /// - internal sealed class Hashfile : SerializableDatFile + internal abstract class Hashfile : SerializableDatFile { // Private instance variables specific to Hashfile DATs - private readonly Serialization.Hash _hash; + protected Serialization.Hash _hash; /// /// Constructor designed for casting a base DatFile /// /// Parent DatFile to copy from - /// Type of hash that is associated with this DAT - public Hashfile(DatFile? datFile, HashType hash) + public Hashfile(DatFile? datFile) : base(datFile) { - _hash = ConvertHash(hash); - } - - /// - /// Convert hash types between internal and Serialization - /// - private static Serialization.Hash ConvertHash(HashType hash) - { - return hash switch - { - HashType.CRC32 => Serialization.Hash.CRC, - HashType.MD5 => Serialization.Hash.MD5, - HashType.SHA1 => Serialization.Hash.SHA1, - HashType.SHA256 => Serialization.Hash.SHA256, - HashType.SHA384 => Serialization.Hash.SHA384, - HashType.SHA512 => Serialization.Hash.SHA512, - HashType.SpamSum => Serialization.Hash.SpamSum, - _ => throw new System.ArgumentOutOfRangeException(nameof(hash)), - }; } /// @@ -225,4 +204,116 @@ namespace SabreTools.DatFiles.Formats return true; } } + + /// + /// Represents an SFV (CRC-32) hashfile + /// + internal sealed class SfvFile : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public SfvFile(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.CRC; + } + } + + /// + /// Represents an MD5 hashfile + /// + internal sealed class Md5File : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public Md5File(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.MD5; + } + } + + /// + /// Represents an SHA-1 hashfile + /// + internal sealed class Sha1File : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public Sha1File(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.SHA1; + } + } + + /// + /// Represents an SHA-256 hashfile + /// + internal sealed class Sha256File : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public Sha256File(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.SHA256; + } + } + + /// + /// Represents an SHA-384 hashfile + /// + internal sealed class Sha384File : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public Sha384File(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.SHA384; + } + } + + /// + /// Represents an SHA-512 hashfile + /// + internal sealed class Sha512File : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public Sha512File(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.SHA512; + } + } + + /// + /// Represents an SpamSum hashfile + /// + internal sealed class SpamSumFile : Hashfile + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public SpamSumFile(DatFile? datFile) + : base(datFile) + { + _hash = Serialization.Hash.SpamSum; + } + } } diff --git a/SabreTools.DatFiles/Formats/SeparatedValue.cs b/SabreTools.DatFiles/Formats/SeparatedValue.cs index 9719d603..8bde497c 100644 --- a/SabreTools.DatFiles/Formats/SeparatedValue.cs +++ b/SabreTools.DatFiles/Formats/SeparatedValue.cs @@ -9,20 +9,18 @@ namespace SabreTools.DatFiles.Formats /// /// Represents a value-separated DAT /// - internal sealed class SeparatedValue : SerializableDatFile + internal abstract class SeparatedValue : SerializableDatFile { - // Private instance variables specific to Separated Value DATs - private readonly char _delim; + // Private instance variables specific to Hashfile DATs + protected char _delim; /// /// Constructor designed for casting a base DatFile /// /// Parent DatFile to copy from - /// Delimiter for parsing individual lines - public SeparatedValue(DatFile? datFile, char delim) + public SeparatedValue(DatFile? datFile) : base(datFile) { - _delim = delim; } /// @@ -119,4 +117,52 @@ namespace SabreTools.DatFiles.Formats return true; } } + + /// + /// Represents a comma-separated value file + /// + internal sealed class CommaSeparatedValue : SeparatedValue + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public CommaSeparatedValue(DatFile? datFile) + : base(datFile) + { + _delim = ','; + } + } + + /// + /// Represents a semicolon-separated value file + /// + internal sealed class SemicolonSeparatedValue : SeparatedValue + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public SemicolonSeparatedValue(DatFile? datFile) + : base(datFile) + { + _delim = ';'; + } + } + + /// + /// Represents a tab-separated value file + /// + internal sealed class TabSeparatedValue : SeparatedValue + { + /// + /// Constructor designed for casting a base DatFile + /// + /// Parent DatFile to copy from + public TabSeparatedValue(DatFile? datFile) + : base(datFile) + { + _delim = '\t'; + } + } }