diff --git a/SabreTools.DatFiles/Formats/Hashfile.Writer.cs b/SabreTools.DatFiles/Formats/Hashfile.Writer.cs index c3461478..537fbf86 100644 --- a/SabreTools.DatFiles/Formats/Hashfile.Writer.cs +++ b/SabreTools.DatFiles/Formats/Hashfile.Writer.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; -using System.Linq; using SabreTools.Core; using SabreTools.DatItems; using SabreTools.DatItems.Formats; @@ -156,7 +154,9 @@ namespace SabreTools.DatFiles.Formats { logger.User($"Writing to '{outfile}'..."); - var hashfile = CreateHashFile(ignoreblanks); + // Serialize the input file + var metadata = ConvertMetadata(ignoreblanks); + var hashfile = new Serialization.CrossModel.Hashfile().Deserialize(metadata, _hash); if (!(new Serialization.Files.Hashfile().Serialize(hashfile, outfile, _hash))) { logger.Warning($"File '{outfile}' could not be written! See the log for more details."); @@ -172,450 +172,5 @@ namespace SabreTools.DatFiles.Formats logger.User($"'{outfile}' written!{Environment.NewLine}"); return true; } - - #region Converters - - /// - /// Create a Hashfile from the current internal information - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.Hashfile CreateHashFile(bool ignoreblanks) - { - var hashfile = new Models.Hashfile.Hashfile(); - - switch (_hash) - { - case Serialization.Hash.CRC: - hashfile.SFV = CreateSFV(ignoreblanks); - break; - case Serialization.Hash.MD5: - hashfile.MD5 = CreateMD5(ignoreblanks); - break; - case Serialization.Hash.SHA1: - hashfile.SHA1 = CreateSHA1(ignoreblanks); - break; - case Serialization.Hash.SHA256: - hashfile.SHA256 = CreateSHA256(ignoreblanks); - break; - case Serialization.Hash.SHA384: - hashfile.SHA384 = CreateSHA384(ignoreblanks); - break; - case Serialization.Hash.SHA512: - hashfile.SHA512 = CreateSHA512(ignoreblanks); - break; - case Serialization.Hash.SpamSum: - hashfile.SpamSum = CreateSpamSum(ignoreblanks); - break; - } - - return hashfile; - } - - /// - /// Create an array of SFV - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.SFV[]? CreateSFV(bool ignoreblanks) - { - // Create a list of hold the SFVs - var sfvs = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Rom rom: - sfvs.Add(new Models.Hashfile.SFV - { - File = name + rom.GetName(), - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.CRCKey), - }); - break; - } - } - } - - return [.. sfvs]; - } - - /// - /// Create an array of MD5 - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.MD5[]? CreateMD5(bool ignoreblanks) - { - // Create a list of hold the MD5s - var md5s = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Disk disk: - md5s.Add(new Models.Hashfile.MD5 - { - Hash = disk.GetStringFieldValue(Models.Metadata.Disk.MD5Key), - File = name + disk.GetName(), - }); - break; - - case Media media: - md5s.Add(new Models.Hashfile.MD5 - { - Hash = media.GetStringFieldValue(Models.Metadata.Media.MD5Key), - File = name + media.GetName(), - }); - break; - - case Rom rom: - md5s.Add(new Models.Hashfile.MD5 - { - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.MD5Key), - File = name + rom.GetName(), - }); - break; - } - } - } - - return [.. md5s]; - } - - /// - /// Create an array of SHA1 - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.SHA1[]? CreateSHA1(bool ignoreblanks) - { - // Create a list of hold the SHA1s - var sha1s = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Disk disk: - sha1s.Add(new Models.Hashfile.SHA1 - { - Hash = disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key), - File = name + disk.GetName(), - }); - break; - - case Media media: - sha1s.Add(new Models.Hashfile.SHA1 - { - Hash = media.GetStringFieldValue(Models.Metadata.Media.SHA1Key), - File = name + media.GetName(), - }); - break; - - case Rom rom: - sha1s.Add(new Models.Hashfile.SHA1 - { - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key), - File = name + rom.GetName(), - }); - break; - } - } - } - - return [.. sha1s]; - } - - /// - /// Create an array of SHA256 - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.SHA256[]? CreateSHA256(bool ignoreblanks) - { - // Create a list of hold the SHA256s - var sha256s = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Media media: - sha256s.Add(new Models.Hashfile.SHA256 - { - Hash = media.GetStringFieldValue(Models.Metadata.Media.SHA256Key), - File = name + media.GetName(), - }); - break; - - case Rom rom: - sha256s.Add(new Models.Hashfile.SHA256 - { - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.SHA256Key), - File = name + rom.GetName(), - }); - break; - } - } - } - - return [.. sha256s]; - } - - /// - /// Create an array of SHA384 - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.SHA384[]? CreateSHA384(bool ignoreblanks) - { - // Create a list of hold the SHA384s - var sha384s = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Rom rom: - sha384s.Add(new Models.Hashfile.SHA384 - { - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.SHA384Key), - File = name + rom.GetName(), - }); - break; - } - } - } - - return [.. sha384s]; - } - - /// - /// Create an array of SHA512 - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.SHA512[]? CreateSHA512(bool ignoreblanks) - { - // Create a list of hold the SHA512s - var sha512s = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Rom rom: - sha512s.Add(new Models.Hashfile.SHA512 - { - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.SHA512Key), - File = name + rom.GetName(), - }); - break; - } - } - } - - return [.. sha512s]; - } - - /// - /// Create an array of SpamSum - /// - /// True if blank roms should be skipped on output, false otherwise - private Models.Hashfile.SpamSum[]? CreateSpamSum(bool ignoreblanks) - { - // Create a list of hold the SpamSums - var spamsums = new List(); - - // Loop through the sorted items and create items for them - foreach (string key in Items.SortedKeys) - { - var items = Items.FilteredItems(key); - if (items == null || !items.Any()) - continue; - - // Loop through and convert the items to respective lists - for (int index = 0; index < items.Count; index++) - { - // Get the item - var item = items[index]; - if (item == null) - continue; - - // Check for a "null" item - item = ProcessNullifiedItem(item); - - // Skip if we're ignoring the item - if (ShouldIgnore(item, ignoreblanks)) - continue; - - string name = string.Empty; - if (Header.GetBoolFieldValue(DatHeader.GameNameKey) == true && item.GetFieldValue(DatItem.MachineKey) != null) - name = $"{item.GetFieldValue(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)}{Path.DirectorySeparatorChar}"; - - switch (item) - { - case Media media: - spamsums.Add(new Models.Hashfile.SpamSum - { - Hash = media.GetStringFieldValue(Models.Metadata.Media.SpamSumKey), - File = name + media.GetName(), - }); - break; - - case Rom rom: - spamsums.Add(new Models.Hashfile.SpamSum - { - Hash = rom.GetStringFieldValue(Models.Metadata.Rom.SpamSumKey), - File = name + rom.GetName(), - }); - break; - } - } - } - - return [.. spamsums]; - } - - #endregion } }