using System; using System.Collections.Generic; using System.IO; using System.Linq; using SabreTools.Models.Hashfile; namespace SabreTools.Serialization.Streams { public partial class Hashfile : IStreamSerializer { /// #if NET48 public Models.Hashfile.Hashfile Deserialize(Stream data) => Deserialize(data, Hash.CRC); #else public Models.Hashfile.Hashfile? Deserialize(Stream? data) => Deserialize(data, Hash.CRC); #endif /// #if NET48 public Models.Hashfile.Hashfile Deserialize(Stream data, Hash hash) #else public Models.Hashfile.Hashfile? Deserialize(Stream? data, Hash hash) #endif { // If the stream is null if (data == null) return default; // Setup the reader and output var reader = new StreamReader(data); var dat = new Models.Hashfile.Hashfile(); var additional = new List(); // Loop through the rows and parse out values var hashes = new List(); while (!reader.EndOfStream) { // Read and split the line #if NET48 string line = reader.ReadLine(); string[] lineParts = line?.Split(new char[] { ' ' } , StringSplitOptions.RemoveEmptyEntries); #else string? line = reader.ReadLine(); string[]? lineParts = line?.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); #endif if (lineParts == null) continue; // Parse the line into a hash switch (hash) { case Hash.CRC: var sfv = new SFV { #if NET48 File = string.Join(" ", lineParts.Take(lineParts.Length - 1)), Hash = lineParts[lineParts.Length - 1], #else File = string.Join(" ", lineParts[..^1]), Hash = lineParts[^1], #endif }; hashes.Add(sfv); break; case Hash.MD5: var md5 = new MD5 { Hash = lineParts[0], #if NET48 File = string.Join(" ", lineParts.Skip(1)), #else File = string.Join(" ", lineParts[1..]), #endif }; hashes.Add(md5); break; case Hash.SHA1: var sha1 = new SHA1 { Hash = lineParts[0], #if NET48 File = string.Join(" ", lineParts.Skip(1)), #else File = string.Join(" ", lineParts[1..]), #endif }; hashes.Add(sha1); break; case Hash.SHA256: var sha256 = new SHA256 { Hash = lineParts[0], #if NET48 File = string.Join(" ", lineParts.Skip(1)), #else File = string.Join(" ", lineParts[1..]), #endif }; hashes.Add(sha256); break; case Hash.SHA384: var sha384 = new SHA384 { Hash = lineParts[0], #if NET48 File = string.Join(" ", lineParts.Skip(1)), #else File = string.Join(" ", lineParts[1..]), #endif }; hashes.Add(sha384); break; case Hash.SHA512: var sha512 = new SHA512 { Hash = lineParts[0], #if NET48 File = string.Join(" ", lineParts.Skip(1)), #else File = string.Join(" ", lineParts[1..]), #endif }; hashes.Add(sha512); break; case Hash.SpamSum: var spamSum = new SpamSum { Hash = lineParts[0], #if NET48 File = string.Join(" ", lineParts.Skip(1)), #else File = string.Join(" ", lineParts[1..]), #endif }; hashes.Add(spamSum); break; } } // Assign the hashes to the hashfile and return switch (hash) { case Hash.CRC: dat.SFV = hashes.Cast().ToArray(); break; case Hash.MD5: dat.MD5 = hashes.Cast().ToArray(); break; case Hash.SHA1: dat.SHA1 = hashes.Cast().ToArray(); break; case Hash.SHA256: dat.SHA256 = hashes.Cast().ToArray(); break; case Hash.SHA384: dat.SHA384 = hashes.Cast().ToArray(); break; case Hash.SHA512: dat.SHA512 = hashes.Cast().ToArray(); break; case Hash.SpamSum: dat.SpamSum = hashes.Cast().ToArray(); break; } dat.ADDITIONAL_ELEMENTS = additional.ToArray(); return dat; } } }