Create and use Hashfile serializer

This commit is contained in:
Matt Nadareski
2023-07-28 21:34:34 -04:00
parent 8cf93a4b3b
commit 5adcd5f18b
6 changed files with 1296 additions and 474 deletions

View File

@@ -0,0 +1,538 @@
using System;
using System.IO;
using System.Linq;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
/// <summary>
/// Represents parsing of a hashfile such as an SFV, MD5, or SHA-1 file
/// </summary>
internal partial class Hashfile : DatFile
{
/// <inheritdoc/>
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{
try
{
// Deserialize the input file
var hashfile = Serialization.Hashfile.Deserialize(filename, _hash);
// Convert items
switch (_hash)
{
case Hash.CRC:
ConvertSFV(hashfile?.SFV, filename, indexId, statsOnly);
break;
case Hash.MD5:
ConvertMD5(hashfile?.MD5, filename, indexId, statsOnly);
break;
case Hash.SHA1:
ConvertSHA1(hashfile?.SHA1, filename, indexId, statsOnly);
break;
case Hash.SHA256:
ConvertSHA256(hashfile?.SHA256, filename, indexId, statsOnly);
break;
case Hash.SHA384:
ConvertSHA384(hashfile?.SHA384, filename, indexId, statsOnly);
break;
case Hash.SHA512:
ConvertSHA512(hashfile?.SHA512, filename, indexId, statsOnly);
break;
case Hash.SpamSum:
ConvertSpamSum(hashfile?.SpamSum, filename, indexId, statsOnly);
break;
}
}
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - An error occurred during parsing";
logger.Error(ex, message);
}
}
#region Converters
/// <summary>
/// Create a machine from the filename
/// </summary>
/// <param name="filename">Filename to derive from</param>
/// <returns>Filled machine and new filename on success, null on error</returns>
private static (Machine?, string?) DeriveMachine(string filename)
{
// If the filename is missing, we can't do anything
if (string.IsNullOrWhiteSpace(filename))
return (null, null);
string machineName = Path.GetFileNameWithoutExtension(filename);
if (filename.Contains('/'))
{
string[] split = filename.Split('/');
machineName = split[0];
filename = filename[(machineName.Length + 1)..];
}
else if (filename.Contains('\\'))
{
string[] split = filename.Split('\\');
machineName = split[0];
filename = filename[(machineName.Length + 1)..];
}
var machine = new Machine { Name = machineName };
return (machine, filename);
}
/// <summary>
/// Derive the item type from the filename
/// </summary>
/// <param name="filename">Filename to derive from</param>
/// <returns>ItemType representing the item (Rom by default), ItemType.NULL on error</returns>
private static ItemType DeriveItemType(string filename)
{
// If the filename is missing, we can't do anything
if (string.IsNullOrWhiteSpace(filename))
return ItemType.NULL;
// If we end in the CHD extension
if (filename.EndsWith(".chd", StringComparison.OrdinalIgnoreCase))
return ItemType.Disk;
// If we end in an Aaruformat extension
if (filename.EndsWith(".aaru", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".aaruf", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".aaruformat", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".aif", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".dicf", StringComparison.OrdinalIgnoreCase))
{
return ItemType.Media;
}
// Everything else is assumed to be a generic item
return ItemType.Rom;
}
/// <summary>
/// Convert SFV information
/// </summary>
/// <param name="sfvs">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSFV(Models.Hashfile.SFV[]? sfvs, string filename, int indexId, bool statsOnly)
{
// If the sfv array is missing, we can't do anything
if (sfvs == null || !sfvs.Any())
return;
// Loop through and add the items
foreach (var sfv in sfvs)
{
// Get the item type
ItemType itemType = DeriveItemType(sfv.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(sfv.File);
switch (itemType)
{
case ItemType.Disk: // Should not happen with CRC32 hashes
case ItemType.Media: // Should not happen with CRC32 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
CRC = sfv.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert MD5 information
/// </summary>
/// <param name="md5s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertMD5(Models.Hashfile.MD5[]? md5s, string filename, int indexId, bool statsOnly)
{
// If the md5 array is missing, we can't do anything
if (md5s == null || !md5s.Any())
return;
// Loop through and add the items
foreach (var md5 in md5s)
{
// Get the item type
ItemType itemType = DeriveItemType(md5.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(md5.File);
switch (itemType)
{
case ItemType.Disk:
var disk = new Disk
{
Name = itemName,
MD5 = md5.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk, statsOnly);
break;
case ItemType.Media:
var media = new Media
{
Name = itemName,
MD5 = md5.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(media, statsOnly);
break;
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
MD5 = md5.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA1 information
/// </summary>
/// <param name="sha1s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA1(Models.Hashfile.SHA1[]? sha1s, string filename, int indexId, bool statsOnly)
{
// If the sha1 array is missing, we can't do anything
if (sha1s == null || !sha1s.Any())
return;
// Loop through and add the items
foreach (var sha1 in sha1s)
{
// Get the item type
ItemType itemType = DeriveItemType(sha1.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(sha1.File);
switch (itemType)
{
case ItemType.Disk:
var disk = new Disk
{
Name = itemName,
SHA1 = sha1.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk, statsOnly);
break;
case ItemType.Media:
var media = new Media
{
Name = itemName,
SHA1 = sha1.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(media, statsOnly);
break;
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA1 = sha1.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA256 information
/// </summary>
/// <param name="sha256s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA256(Models.Hashfile.SHA256[]? sha256s, string filename, int indexId, bool statsOnly)
{
// If the sha256 array is missing, we can't do anything
if (sha256s == null || !sha256s.Any())
return;
// Loop through and add the items
foreach (var sha256 in sha256s)
{
// Get the item type
ItemType itemType = DeriveItemType(sha256.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(sha256.File);
switch (itemType)
{
case ItemType.Media:
var media = new Media
{
Name = itemName,
SHA256 = sha256.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(media, statsOnly);
break;
case ItemType.Disk: // Should not happen with SHA-256 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA256 = sha256.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA384 information
/// </summary>
/// <param name="sha384s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA384(Models.Hashfile.SHA384[]? sha384s, string filename, int indexId, bool statsOnly)
{
// If the sha384 array is missing, we can't do anything
if (sha384s == null || !sha384s.Any())
return;
// Loop through and add the items
foreach (var sha384 in sha384s)
{
// Get the item type
ItemType itemType = DeriveItemType(sha384.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(sha384.File);
switch (itemType)
{
case ItemType.Disk: // Should not happen with SHA-384 hashes
case ItemType.Media: // Should not happen with SHA-384 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA384 = sha384.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA512 information
/// </summary>
/// <param name="sha512s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA512(Models.Hashfile.SHA512[]? sha512s, string filename, int indexId, bool statsOnly)
{
// If the sha512 array is missing, we can't do anything
if (sha512s == null || !sha512s.Any())
return;
// Loop through and add the items
foreach (var sha512 in sha512s)
{
// Get the item type
ItemType itemType = DeriveItemType(sha512.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(sha512.File);
switch (itemType)
{
case ItemType.Disk: // Should not happen with SHA-512 hashes
case ItemType.Media: // Should not happen with SHA-512 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA512 = sha512.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SpamSum information
/// </summary>
/// <param name="spamsums">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSpamSum(Models.Hashfile.SpamSum[]? spamsums, string filename, int indexId, bool statsOnly)
{
// If the spamsum array is missing, we can't do anything
if (spamsums == null || !spamsums.Any())
return;
// Loop through and add the items
foreach (var spamsum in spamsums)
{
// Get the item type
ItemType itemType = DeriveItemType(spamsum.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string itemName) = DeriveMachine(spamsum.File);
switch (itemType)
{
case ItemType.Media:
var media = new Media
{
Name = itemName,
SpamSum = spamsum.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(media, statsOnly);
break;
case ItemType.Disk: // Should not happen with SpamSum fuzzy hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SpamSum = spamsum.Hash,
Machine = machine,
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,505 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
/// <summary>
/// Represents writing a hashfile such as an SFV, MD5, or SHA-1 file
/// </summary>
internal partial class Hashfile : DatFile
{
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[]
{
ItemType.Disk,
ItemType.Media,
ItemType.Rom
};
}
/// <inheritdoc/>
protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem)
{
List<DatItemField> missingFields = new();
// Check item name
if (string.IsNullOrWhiteSpace(datItem.GetName()))
missingFields.Add(DatItemField.Name);
// Check hash linked to specific Hashfile type
switch (_hash)
{
case Hash.CRC:
switch (datItem.ItemType)
{
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.CRC))
missingFields.Add(DatItemField.CRC);
break;
default:
missingFields.Add(DatItemField.CRC);
break;
}
break;
case Hash.MD5:
switch (datItem.ItemType)
{
case ItemType.Disk:
if (!string.IsNullOrEmpty((datItem as Disk)?.MD5))
missingFields.Add(DatItemField.MD5);
break;
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.MD5))
missingFields.Add(DatItemField.MD5);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.MD5))
missingFields.Add(DatItemField.MD5);
break;
default:
missingFields.Add(DatItemField.MD5);
break;
}
break;
case Hash.SHA1:
switch (datItem.ItemType)
{
case ItemType.Disk:
if (!string.IsNullOrEmpty((datItem as Disk)?.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
default:
missingFields.Add(DatItemField.SHA1);
break;
}
break;
case Hash.SHA256:
switch (datItem.ItemType)
{
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.SHA256))
missingFields.Add(DatItemField.SHA256);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA256))
missingFields.Add(DatItemField.SHA256);
break;
default:
missingFields.Add(DatItemField.SHA256);
break;
}
break;
case Hash.SHA384:
switch (datItem.ItemType)
{
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA384))
missingFields.Add(DatItemField.SHA384);
break;
default:
missingFields.Add(DatItemField.SHA384);
break;
}
break;
case Hash.SHA512:
switch (datItem.ItemType)
{
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA512))
missingFields.Add(DatItemField.SHA512);
break;
default:
missingFields.Add(DatItemField.SHA512);
break;
}
break;
case Hash.SpamSum:
switch (datItem.ItemType)
{
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.SpamSum))
missingFields.Add(DatItemField.SpamSum);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SpamSum))
missingFields.Add(DatItemField.SpamSum);
break;
default:
missingFields.Add(DatItemField.SpamSum);
break;
}
break;
}
return missingFields;
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
{
try
{
logger.User($"Writing to '{outfile}'...");
var hashfile = CreateHashFile();
if (!Serialization.Hashfile.SerializeToFile(hashfile, outfile, _hash))
{
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
return false;
}
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
return true;
}
/// <summary>
/// Create a Hashfile from the current internal information
/// <summary>
private Models.Hashfile.Hashfile CreateHashFile()
{
var hashfile = new Models.Hashfile.Hashfile();
switch (_hash)
{
case Hash.CRC:
hashfile.SFV = CreateSFV();
break;
case Hash.MD5:
hashfile.MD5 = CreateMD5();
break;
case Hash.SHA1:
hashfile.SHA1 = CreateSHA1();
break;
case Hash.SHA256:
hashfile.SHA256 = CreateSHA256();
break;
case Hash.SHA384:
hashfile.SHA384 = CreateSHA384();
break;
case Hash.SHA512:
hashfile.SHA512 = CreateSHA512();
break;
case Hash.SpamSum:
hashfile.SpamSum = CreateSpamSum();
break;
}
return hashfile;
}
/// <summary>
/// Create an array of SFV
/// <summary>
private Models.Hashfile.SFV[]? CreateSFV()
{
// Create a list of hold the SFVs
var sfvs = new List<Models.Hashfile.SFV>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
if (Header.GameName)
name = $"{item.Machine.Name}{Path.DirectorySeparatorChar}";
switch (item)
{
case Rom rom:
sfvs.Add(new Models.Hashfile.SFV
{
File = name + rom.Name,
Hash = rom.CRC,
});
break;
}
}
}
return sfvs.ToArray();
}
/// <summary>
/// Create an array of MD5
/// <summary>
private Models.Hashfile.MD5[]? CreateMD5()
{
// Create a list of hold the MD5s
var md5s = new List<Models.Hashfile.MD5>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
if (Header.GameName)
name = $"{item.Machine.Name}{Path.DirectorySeparatorChar}";
switch (item)
{
case Disk disk:
md5s.Add(new Models.Hashfile.MD5
{
Hash = disk.MD5,
File = name + disk.Name,
});
break;
case Media media:
md5s.Add(new Models.Hashfile.MD5
{
Hash = media.MD5,
File = name + media.Name,
});
break;
case Rom rom:
md5s.Add(new Models.Hashfile.MD5
{
Hash = rom.MD5,
File = name + rom.Name,
});
break;
}
}
}
return md5s.ToArray();
}
/// <summary>
/// Create an array of SHA1
/// <summary>
private Models.Hashfile.SHA1[]? CreateSHA1()
{
// Create a list of hold the SHA1s
var sha1s = new List<Models.Hashfile.SHA1>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
if (Header.GameName)
name = $"{item.Machine.Name}{Path.DirectorySeparatorChar}";
switch (item)
{
case Disk disk:
sha1s.Add(new Models.Hashfile.SHA1
{
Hash = disk.SHA1,
File = name + disk.Name,
});
break;
case Media media:
sha1s.Add(new Models.Hashfile.SHA1
{
Hash = media.SHA1,
File = name + media.Name,
});
break;
case Rom rom:
sha1s.Add(new Models.Hashfile.SHA1
{
Hash = rom.SHA1,
File = name + rom.Name,
});
break;
}
}
}
return sha1s.ToArray();
}
/// <summary>
/// Create an array of SHA256
/// <summary>
private Models.Hashfile.SHA256[]? CreateSHA256()
{
// Create a list of hold the SHA256s
var sha256s = new List<Models.Hashfile.SHA256>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
switch (item)
{
case Media media:
sha256s.Add(new Models.Hashfile.SHA256
{
Hash = media.SHA256,
File = name + media.Name,
});
break;
case Rom rom:
sha256s.Add(new Models.Hashfile.SHA256
{
Hash = rom.SHA256,
File = name + rom.Name,
});
break;
}
}
}
return sha256s.ToArray();
}
/// <summary>
/// Create an array of SHA384
/// <summary>
private Models.Hashfile.SHA384[]? CreateSHA384()
{
// Create a list of hold the SHA384s
var sha384s = new List<Models.Hashfile.SHA384>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
switch (item)
{
case Rom rom:
sha384s.Add(new Models.Hashfile.SHA384
{
Hash = rom.SHA384,
File = name + rom.Name,
});
break;
}
}
}
return sha384s.ToArray();
}
/// <summary>
/// Create an array of SHA512
/// <summary>
private Models.Hashfile.SHA512[]? CreateSHA512()
{
// Create a list of hold the SHA512s
var sha512s = new List<Models.Hashfile.SHA512>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
switch (item)
{
case Rom rom:
sha512s.Add(new Models.Hashfile.SHA512
{
Hash = rom.SHA512,
File = name + rom.Name,
});
break;
}
}
}
return sha512s.ToArray();
}
/// <summary>
/// Create an array of SpamSum
/// <summary>
private Models.Hashfile.SpamSum[]? CreateSpamSum()
{
// Create a list of hold the SpamSums
var spamsums = new List<Models.Hashfile.SpamSum>();
// 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;
foreach (var item in items)
{
string name = string.Empty;
switch (item)
{
case Media media:
spamsums.Add(new Models.Hashfile.SpamSum
{
Hash = media.SpamSum,
File = name + media.Name,
});
break;
case Rom rom:
spamsums.Add(new Models.Hashfile.SpamSum
{
Hash = rom.SpamSum,
File = name + rom.Name,
});
break;
}
}
}
return spamsums.ToArray();
}
}
}

View File

@@ -1,19 +1,11 @@
using System; using SabreTools.Core;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
using SabreTools.IO;
using SabreTools.IO.Writers;
namespace SabreTools.DatFiles.Formats namespace SabreTools.DatFiles.Formats
{ {
/// <summary> /// <summary>
/// Represents parsing and writing of a hashfile such as an SFV, MD5, or SHA-1 file /// Represents a hashfile such as an SFV, MD5, or SHA-1 file
/// </summary> /// </summary>
internal class Hashfile : DatFile internal partial class Hashfile : DatFile
{ {
// Private instance variables specific to Hashfile DATs // Private instance variables specific to Hashfile DATs
private readonly Hash _hash; private readonly Hash _hash;
@@ -28,450 +20,5 @@ namespace SabreTools.DatFiles.Formats
{ {
_hash = hash; _hash = hash;
} }
/// <inheritdoc/>
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{
// Open a file reader
Encoding enc = filename.GetEncoding();
StreamReader sr = new(System.IO.File.OpenRead(filename), enc);
while (!sr.EndOfStream)
{
try
{
string line = sr.ReadLine();
// Split the line and get the name and hash
string[] split = line.Split(' ');
string name = string.Empty;
string hash = string.Empty;
// If we have CRC, then it's an SFV file and the name is first
if (_hash.HasFlag(Hash.CRC))
{
name = string.Join(" ", split[..^1]).Replace("*", String.Empty).Trim();
hash = split[^1];
}
// Otherwise, the name is second
else
{
name = string.Join(" ", split[1..]).Replace("*", String.Empty).Trim();
hash = split[0];
}
// If the name contains a path, use that path as the machine
string machine = Path.GetFileNameWithoutExtension(filename);
if (name.Contains('/'))
{
split = name.Split('/');
machine = split[0];
name = name[(machine.Length + 1)..];
}
else if (name.Contains('\\'))
{
split = name.Split('\\');
machine = split[0];
name = name[(machine.Length + 1)..];
}
Rom rom = new()
{
Name = name,
Size = null,
CRC = (_hash.HasFlag(Hash.CRC) ? hash : null),
MD5 = (_hash.HasFlag(Hash.MD5) ? hash : null),
SHA1 = (_hash.HasFlag(Hash.SHA1) ? hash : null),
SHA256 = (_hash.HasFlag(Hash.SHA256) ? hash : null),
SHA384 = (_hash.HasFlag(Hash.SHA384) ? hash : null),
SHA512 = (_hash.HasFlag(Hash.SHA512) ? hash : null),
SpamSum = (_hash.HasFlag(Hash.SpamSum) ? hash : null),
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = machine,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom, statsOnly);
}
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
logger.Error(ex, message);
}
}
sr.Dispose();
}
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[] { ItemType.Disk, ItemType.Media, ItemType.Rom };
}
/// <inheritdoc/>
protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem)
{
List<DatItemField> missingFields = new();
// Check item name
if (string.IsNullOrWhiteSpace(datItem.GetName()))
missingFields.Add(DatItemField.Name);
// Check hash linked to specific Hashfile type
switch (_hash)
{
case Hash.CRC:
switch (datItem.ItemType)
{
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.CRC))
missingFields.Add(DatItemField.CRC);
break;
default:
missingFields.Add(DatItemField.CRC);
break;
}
break;
case Hash.MD5:
switch (datItem.ItemType)
{
case ItemType.Disk:
if (!string.IsNullOrEmpty((datItem as Disk)?.MD5))
missingFields.Add(DatItemField.MD5);
break;
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.MD5))
missingFields.Add(DatItemField.MD5);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.MD5))
missingFields.Add(DatItemField.MD5);
break;
default:
missingFields.Add(DatItemField.MD5);
break;
}
break;
case Hash.SHA1:
switch (datItem.ItemType)
{
case ItemType.Disk:
if (!string.IsNullOrEmpty((datItem as Disk)?.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA1))
missingFields.Add(DatItemField.SHA1);
break;
default:
missingFields.Add(DatItemField.SHA1);
break;
}
break;
case Hash.SHA256:
switch (datItem.ItemType)
{
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.SHA256))
missingFields.Add(DatItemField.SHA256);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA256))
missingFields.Add(DatItemField.SHA256);
break;
default:
missingFields.Add(DatItemField.SHA256);
break;
}
break;
case Hash.SHA384:
switch (datItem.ItemType)
{
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA384))
missingFields.Add(DatItemField.SHA384);
break;
default:
missingFields.Add(DatItemField.SHA384);
break;
}
break;
case Hash.SHA512:
switch (datItem.ItemType)
{
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SHA512))
missingFields.Add(DatItemField.SHA512);
break;
default:
missingFields.Add(DatItemField.SHA512);
break;
}
break;
case Hash.SpamSum:
switch (datItem.ItemType)
{
case ItemType.Media:
if (!string.IsNullOrEmpty((datItem as Media)?.SpamSum))
missingFields.Add(DatItemField.SpamSum);
break;
case ItemType.Rom:
if (!string.IsNullOrEmpty((datItem as Rom)?.SpamSum))
missingFields.Add(DatItemField.SpamSum);
break;
default:
missingFields.Add(DatItemField.SpamSum);
break;
}
break;
}
return missingFields;
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
{
try
{
logger.User($"Writing to '{outfile}'...");
FileStream fs = System.IO.File.Create(outfile);
// If we get back null for some reason, just log and return
if (fs == null)
{
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
SeparatedValueWriter svw = new(fs, new UTF8Encoding(false))
{
Quotes = false,
Separator = ' ',
VerifyFieldCount = true
};
// Use a sorted list of games to output
foreach (string key in Items.SortedKeys)
{
ConcurrentList<DatItem> datItems = Items[key];
// If this machine doesn't contain any writable items, skip
if (!ContainsWritable(datItems))
continue;
// Resolve the names in the block
datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < datItems.Count; index++)
{
DatItem datItem = datItems[index];
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(svw, datItem);
}
}
logger.User($"'{outfile}' written!{Environment.NewLine}");
svw.Dispose();
fs.Dispose();
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
return true;
}
/// <summary>
/// Write out DatItem using the supplied SeparatedValueWriter
/// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
{
// Build the state
string[] fields = new string[2];
// Get the name field
string name = string.Empty;
switch (datItem.ItemType)
{
case ItemType.Disk:
var disk = datItem as Disk;
if (Header.GameName)
name = $"{disk.Machine.Name}{Path.DirectorySeparatorChar}";
name += disk.Name;
break;
case ItemType.Media:
var media = datItem as Media;
if (Header.GameName)
name = $"{media.Machine.Name}{Path.DirectorySeparatorChar}";
name += media.Name;
break;
case ItemType.Rom:
var rom = datItem as Rom;
if (Header.GameName)
name = $"{rom.Machine.Name}{Path.DirectorySeparatorChar}";
name += rom.Name;
break;
}
// Get the hash field and set final fields
switch (_hash)
{
case Hash.CRC:
switch (datItem.ItemType)
{
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = name;
fields[1] = rom.CRC;
break;
}
break;
case Hash.MD5:
switch (datItem.ItemType)
{
case ItemType.Disk:
var disk = datItem as Disk;
fields[0] = disk.MD5;
fields[1] = name;
break;
case ItemType.Media:
var media = datItem as Media;
fields[0] = media.MD5;
fields[1] = name;
break;
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = rom.MD5;
fields[1] = name;
break;
}
break;
case Hash.SHA1:
switch (datItem.ItemType)
{
case ItemType.Disk:
var disk = datItem as Disk;
fields[0] = disk.SHA1;
fields[1] = name;
break;
case ItemType.Media:
var media = datItem as Media;
fields[0] = media.SHA1;
fields[1] = name;
break;
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = rom.SHA1;
fields[1] = name;
break;
}
break;
case Hash.SHA256:
switch (datItem.ItemType)
{
case ItemType.Media:
var media = datItem as Media;
fields[0] = media.SHA256;
fields[1] = name;
break;
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = rom.SHA256;
fields[1] = name;
break;
}
break;
case Hash.SHA384:
switch (datItem.ItemType)
{
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = rom.SHA384;
fields[1] = name;
break;
}
break;
case Hash.SHA512:
switch (datItem.ItemType)
{
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = rom.SHA512;
fields[1] = name;
break;
}
break;
case Hash.SpamSum:
switch (datItem.ItemType)
{
case ItemType.Media:
var media = datItem as Media;
fields[0] = media.SpamSum;
fields[1] = name;
break;
case ItemType.Rom:
var rom = datItem as Rom;
fields[0] = rom.SpamSum;
fields[1] = name;
break;
}
break;
}
// If we had at least one field filled in
if (!string.IsNullOrEmpty(fields[0]) || !string.IsNullOrEmpty(fields[1]))
svw.WriteValues(fields);
svw.Flush();
}
} }
} }

View File

@@ -76,7 +76,7 @@ namespace SabreTools.Serialization
/// Write header information to the current writer /// Write header information to the current writer
/// </summary> /// </summary>
/// <param name="header">ClrMamePro representing the header information</param> /// <param name="header">ClrMamePro representing the header information</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteHeader(Models.ClrMamePro.ClrMamePro? header, ClrMameProWriter writer) private static void WriteHeader(Models.ClrMamePro.ClrMamePro? header, ClrMameProWriter writer)
{ {
// If the header information is missing, we can't do anything // If the header information is missing, we can't do anything
@@ -109,7 +109,7 @@ namespace SabreTools.Serialization
/// Write games information to the current writer /// Write games information to the current writer
/// </summary> /// </summary>
/// <param name="games">Array of GameBase objects representing the games information</param> /// <param name="games">Array of GameBase objects representing the games information</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteGames(GameBase[]? games, ClrMameProWriter writer) private static void WriteGames(GameBase[]? games, ClrMameProWriter writer)
{ {
// If the games information is missing, we can't do anything // If the games information is missing, we can't do anything
@@ -128,7 +128,7 @@ namespace SabreTools.Serialization
/// Write game information to the current writer /// Write game information to the current writer
/// </summary> /// </summary>
/// <param name="game">GameBase object representing the game information</param> /// <param name="game">GameBase object representing the game information</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteGame(GameBase game, ClrMameProWriter writer) private static void WriteGame(GameBase game, ClrMameProWriter writer)
{ {
// If the game information is missing, we can't do anything // If the game information is missing, we can't do anything
@@ -183,7 +183,7 @@ namespace SabreTools.Serialization
/// Write releases information to the current writer /// Write releases information to the current writer
/// </summary> /// </summary>
/// <param name="releases">Array of Release objects to write</param> /// <param name="releases">Array of Release objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteReleases(Release[]? releases, ClrMameProWriter writer) private static void WriteReleases(Release[]? releases, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -206,7 +206,7 @@ namespace SabreTools.Serialization
/// Write biossets information to the current writer /// Write biossets information to the current writer
/// </summary> /// </summary>
/// <param name="biossets">Array of BiosSet objects to write</param> /// <param name="biossets">Array of BiosSet objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteBiosSets(BiosSet[]? biossets, ClrMameProWriter writer) private static void WriteBiosSets(BiosSet[]? biossets, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -227,7 +227,7 @@ namespace SabreTools.Serialization
/// Write roms information to the current writer /// Write roms information to the current writer
/// </summary> /// </summary>
/// <param name="roms">Array of Rom objects to write</param> /// <param name="roms">Array of Rom objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteRoms(Rom[]? roms, ClrMameProWriter writer) private static void WriteRoms(Rom[]? roms, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -266,7 +266,7 @@ namespace SabreTools.Serialization
/// Write disks information to the current writer /// Write disks information to the current writer
/// </summary> /// </summary>
/// <param name="disks">Array of Disk objects to write</param> /// <param name="disks">Array of Disk objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteDisks(Disk[]? disks, ClrMameProWriter writer) private static void WriteDisks(Disk[]? disks, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -290,7 +290,7 @@ namespace SabreTools.Serialization
/// Write medias information to the current writer /// Write medias information to the current writer
/// </summary> /// </summary>
/// <param name="medias">Array of Media objects to write</param> /// <param name="medias">Array of Media objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteMedia(Media[]? medias, ClrMameProWriter writer) private static void WriteMedia(Media[]? medias, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -313,7 +313,7 @@ namespace SabreTools.Serialization
/// Write samples information to the current writer /// Write samples information to the current writer
/// </summary> /// </summary>
/// <param name="samples">Array of Sample objects to write</param> /// <param name="samples">Array of Sample objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteSamples(Sample[]? samples, ClrMameProWriter writer) private static void WriteSamples(Sample[]? samples, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -332,7 +332,7 @@ namespace SabreTools.Serialization
/// Write archives information to the current writer /// Write archives information to the current writer
/// </summary> /// </summary>
/// <param name="archives">Array of Archive objects to write</param> /// <param name="archives">Array of Archive objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteArchives(Archive[]? archives, ClrMameProWriter writer) private static void WriteArchives(Archive[]? archives, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -351,7 +351,7 @@ namespace SabreTools.Serialization
/// Write chips information to the current writer /// Write chips information to the current writer
/// </summary> /// </summary>
/// <param name="chips">Array of Chip objects to write</param> /// <param name="chips">Array of Chip objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteChips(Chip[]? chips, ClrMameProWriter writer) private static void WriteChips(Chip[]? chips, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -373,7 +373,7 @@ namespace SabreTools.Serialization
/// Write video information to the current writer /// Write video information to the current writer
/// </summary> /// </summary>
/// <param name="video">Video object to write</param> /// <param name="video">Video object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteVideo(Video? video, ClrMameProWriter writer) private static void WriteVideo(Video? video, ClrMameProWriter writer)
{ {
// If the item is missing, we can't do anything // If the item is missing, we can't do anything
@@ -395,7 +395,7 @@ namespace SabreTools.Serialization
/// Write sound information to the current writer /// Write sound information to the current writer
/// </summary> /// </summary>
/// <param name="sound">Sound object to write</param> /// <param name="sound">Sound object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteSound(Sound? sound, ClrMameProWriter writer) private static void WriteSound(Sound? sound, ClrMameProWriter writer)
{ {
// If the item is missing, we can't do anything // If the item is missing, we can't do anything
@@ -411,7 +411,7 @@ namespace SabreTools.Serialization
/// Write input information to the current writer /// Write input information to the current writer
/// </summary> /// </summary>
/// <param name="input">Input object to write</param> /// <param name="input">Input object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteInput(Input? input, ClrMameProWriter writer) private static void WriteInput(Input? input, ClrMameProWriter writer)
{ {
// If the item is missing, we can't do anything // If the item is missing, we can't do anything
@@ -432,7 +432,7 @@ namespace SabreTools.Serialization
/// Write dipswitches information to the current writer /// Write dipswitches information to the current writer
/// </summary> /// </summary>
/// <param name="dipswitches">Array of DipSwitch objects to write</param> /// <param name="dipswitches">Array of DipSwitch objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteDipSwitches(DipSwitch[]? dipswitches, ClrMameProWriter writer) private static void WriteDipSwitches(DipSwitch[]? dipswitches, ClrMameProWriter writer)
{ {
// If the array is missing, we can't do anything // If the array is missing, we can't do anything
@@ -456,7 +456,7 @@ namespace SabreTools.Serialization
/// Write driver information to the current writer /// Write driver information to the current writer
/// </summary> /// </summary>
/// <param name="driver">Driver object to write</param> /// <param name="driver">Driver object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param> /// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteDriver(Driver? driver, ClrMameProWriter writer) private static void WriteDriver(Driver? driver, ClrMameProWriter writer)
{ {
// If the item is missing, we can't do anything // If the item is missing, we can't do anything

View File

@@ -7,9 +7,9 @@ using SabreTools.Core;
namespace SabreTools.Serialization namespace SabreTools.Serialization
{ {
/// <summary> /// <summary>
/// Serializer for hashfile variants /// Deserializer for hashfile variants
/// </summary> /// </summary>
public class Hashfile public partial class Hashfile
{ {
/// <summary> /// <summary>
/// Deserializes a hashfile variant to the defined type /// Deserializes a hashfile variant to the defined type

View File

@@ -0,0 +1,232 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.Core;
using SabreTools.IO.Writers;
using SabreTools.Models.Hashfile;
namespace SabreTools.Serialization
{
/// <summary>
/// Serializer for hashfile variants
/// </summary>
public partial class Hashfile
{
/// <summary>
/// Serializes the defined type to a hashfile variant file
/// </summary>
/// <param name="hashfile">Data to serialize</param>
/// <param name="path">Path to the file to serialize to</param>
/// <param name="hash">Hash corresponding to the hashfile variant</param>
/// <returns>True on successful serialization, false otherwise</returns>
public static bool SerializeToFile(Models.Hashfile.Hashfile? hashfile, string path, Hash hash)
{
try
{
using var stream = SerializeToStream(hashfile, hash);
if (stream == null)
return false;
using var fs = File.OpenWrite(path);
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fs);
return true;
}
catch
{
// TODO: Handle logging the exception
return false;
}
}
/// <summary>
/// Serializes the defined type to a stream
/// </summary>
/// <param name="hashfile">Data to serialize</param>
/// <param name="hash">Hash corresponding to the hashfile variant</param>
/// <returns>Stream containing serialized data on success, null otherwise</returns>
public static Stream? SerializeToStream(Models.Hashfile.Hashfile? hashfile, Hash hash)
{
try
{
// If the metadata file is null
if (hashfile == null)
return null;
// Setup the writer and output
var stream = new MemoryStream();
var writer = new SeparatedValueWriter(stream, Encoding.UTF8) { Separator = ' ', Quotes = false };
// Write out the items, if they exist
switch (hash)
{
case Hash.CRC:
WriteSFV(hashfile.SFV, writer);
break;
case Hash.MD5:
WriteMD5(hashfile.MD5, writer);
break;
case Hash.SHA1:
WriteSHA1(hashfile.SHA1, writer);
break;
case Hash.SHA256:
WriteSHA256(hashfile.SHA256, writer);
break;
case Hash.SHA384:
WriteSHA384(hashfile.SHA384, writer);
break;
case Hash.SHA512:
WriteSHA512(hashfile.SHA512, writer);
break;
case Hash.SpamSum:
WriteSpamSum(hashfile.SpamSum, writer);
break;
default:
throw new ArgumentOutOfRangeException(nameof(hash));
}
// Return the stream
return stream;
}
catch
{
// TODO: Handle logging the exception
return null;
}
}
/// <summary>
/// Write SFV information to the current writer
/// </summary>
/// <param name="sfvs">Array of SFV objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSFV(SFV[]? sfvs, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sfvs == null || !sfvs.Any())
return;
// Loop through and write out the items
foreach (var sfv in sfvs)
{
writer.WriteValues(new string[] { sfv.File, sfv.Hash });
writer.Flush();
}
}
/// <summary>
/// Write MD5 information to the current writer
/// </summary>
/// <param name="md5s">Array of MD5 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteMD5(MD5[]? md5s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (md5s == null || !md5s.Any())
return;
// Loop through and write out the items
foreach (var md5 in md5s)
{
writer.WriteValues(new string[] { md5.Hash, md5.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA1 information to the current writer
/// </summary>
/// <param name="sha1s">Array of SHA1 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA1(SHA1[]? sha1s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha1s == null || !sha1s.Any())
return;
// Loop through and write out the items
foreach (var sha1 in sha1s)
{
writer.WriteValues(new string[] { sha1.Hash, sha1.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA256 information to the current writer
/// </summary>
/// <param name="sha256s">Array of SHA256 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA256(SHA256[]? sha256s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha256s == null || !sha256s.Any())
return;
// Loop through and write out the items
foreach (var sha256 in sha256s)
{
writer.WriteValues(new string[] { sha256.Hash, sha256.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA384 information to the current writer
/// </summary>
/// <param name="sha384s">Array of SHA384 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA384(SHA384[]? sha384s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha384s == null || !sha384s.Any())
return;
// Loop through and write out the items
foreach (var sha384 in sha384s)
{
writer.WriteValues(new string[] { sha384.Hash, sha384.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA512 information to the current writer
/// </summary>
/// <param name="sha512s">Array of SHA512 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA512(SHA512[]? sha512s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha512s == null || !sha512s.Any())
return;
// Loop through and write out the items
foreach (var sha512 in sha512s)
{
writer.WriteValues(new string[] { sha512.Hash, sha512.File });
writer.Flush();
}
}
/// <summary>
/// Write SpamSum information to the current writer
/// </summary>
/// <param name="spamsums">Array of SpamSum objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSpamSum(SpamSum[]? spamsums, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (spamsums == null || !spamsums.Any())
return;
// Loop through and write out the items
foreach (var spamsum in spamsums)
{
writer.WriteValues(new string[] { spamsum.Hash, spamsum.File });
writer.Flush();
}
}
}
}