mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-05 22:01:33 +00:00
80 lines
2.8 KiB
C#
80 lines
2.8 KiB
C#
using System;
|
|
using SabreTools.Hashing;
|
|
using SabreTools.Metadata.Filter;
|
|
|
|
#pragma warning disable IDE0290 // Use primary constructor
|
|
namespace SabreTools.Metadata.DatFiles.Formats
|
|
{
|
|
/// <summary>
|
|
/// Represents a hashfile such as an SFV, MD5, or SHA-1 file
|
|
/// </summary>
|
|
public abstract class Hashfile : SerializableDatFile<Data.Models.Hashfile.Hashfile, Serialization.Readers.Hashfile, Serialization.Writers.Hashfile, Serialization.CrossModel.Hashfile>
|
|
{
|
|
#region Fields
|
|
|
|
// Private instance variables specific to Hashfile DATs
|
|
protected HashType _hash;
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Constructor designed for casting a base DatFile
|
|
/// </summary>
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
public Hashfile(DatFile? datFile) : base(datFile)
|
|
{
|
|
_hash = HashType.SHA1;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void ParseFile(string filename,
|
|
int indexId,
|
|
bool keep,
|
|
bool statsOnly = false,
|
|
FilterRunner? filterRunner = null,
|
|
bool throwOnError = false)
|
|
{
|
|
try
|
|
{
|
|
// Deserialize the input file
|
|
var hashfile = new Serialization.Readers.Hashfile().Deserialize(filename, _hash);
|
|
var metadata = new Serialization.CrossModel.Hashfile().Serialize(hashfile);
|
|
|
|
// Convert to the internal format
|
|
ConvertFromMetadata(metadata, filename, indexId, keep, statsOnly, filterRunner);
|
|
}
|
|
catch (Exception ex) when (!throwOnError)
|
|
{
|
|
string message = $"'{filename}' - An error occurred during parsing";
|
|
_logger.Error(ex, message);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
|
{
|
|
try
|
|
{
|
|
_logger.User($"Writing to '{outfile}'...");
|
|
|
|
// Serialize the input file
|
|
var metadata = ConvertToMetadata(ignoreblanks);
|
|
var hashfile = new Serialization.CrossModel.Hashfile().Deserialize(metadata, _hash);
|
|
if (!new Serialization.Writers.Hashfile().SerializeFile(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;
|
|
}
|
|
|
|
_logger.User($"'{outfile}' written!{Environment.NewLine}");
|
|
return true;
|
|
}
|
|
}
|
|
}
|