using System; using SabreTools.Hashing; using SabreTools.Metadata.Filter; #pragma warning disable IDE0290 // Use primary constructor namespace SabreTools.Metadata.DatFiles.Formats { /// /// Represents a hashfile such as an SFV, MD5, or SHA-1 file /// public abstract class Hashfile : SerializableDatFile { #region Properties // Private instance variables specific to Hashfile DATs protected HashType _hash; #endregion /// /// Constructor designed for casting a base DatFile /// /// Parent DatFile to copy from public Hashfile(DatFile? datFile) : base(datFile) { _hash = HashType.SHA1; } /// 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); } } /// 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; } } }