using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using SabreTools.IO.Readers; using SabreTools.Models.SeparatedValue; namespace SabreTools.Serialization { /// /// Serializer for separated-value variants /// public class SeparatedValue { private const int HeaderWithoutExtendedHashesCount = 14; private const int HeaderWithExtendedHashesCount = 17; /// /// Deserializes a separated-value variant to the defined type /// /// Path to the file to deserialize /// Character delimiter between values /// Deserialized data on success, null on failure public static MetadataFile? Deserialize(string path, char delim) { try { using var stream = PathProcessor.OpenStream(path); return Deserialize(stream, delim); } catch { // TODO: Handle logging the exception return default; } } /// /// Deserializes a separated-value variant in a stream to the defined type /// /// Stream to deserialize /// Character delimiter between values /// Deserialized data on success, null on failure public static MetadataFile? Deserialize(Stream? stream, char delim) { try { // If the stream is null if (stream == null) return default; // Setup the reader and output var reader = new SeparatedValueReader(stream, Encoding.UTF8) { Header = true, Separator = delim, VerifyFieldCount = false, }; var dat = new MetadataFile(); // Read the header values first if (!reader.ReadHeader()) return null; dat.Header = reader.HeaderValues.ToArray(); // Loop through the rows and parse out values var rows = new List(); while (!reader.EndOfStream) { // If we have no next line if (!reader.ReadNextLine()) break; // Parse the line into a row Row? row = null; if (reader.Line.Count < HeaderWithExtendedHashesCount) { row = new Row { FileName = reader.Line[0], InternalName = reader.Line[1], Description = reader.Line[2], GameName = reader.Line[3], GameDescription = reader.Line[4], Type = reader.Line[5], RomName = reader.Line[6], DiskName = reader.Line[7], Size = reader.Line[8], CRC = reader.Line[9], MD5 = reader.Line[10], SHA1 = reader.Line[11], SHA256 = reader.Line[12], Nodump = reader.Line[13], }; // If we have additional fields if (reader.Line.Count > HeaderWithoutExtendedHashesCount) row.ADDITIONAL_ELEMENTS = reader.Line.Skip(HeaderWithoutExtendedHashesCount).ToArray(); } else { row = new Row { FileName = reader.Line[0], InternalName = reader.Line[1], Description = reader.Line[2], GameName = reader.Line[3], GameDescription = reader.Line[4], Type = reader.Line[5], RomName = reader.Line[6], DiskName = reader.Line[7], Size = reader.Line[8], CRC = reader.Line[9], MD5 = reader.Line[10], SHA1 = reader.Line[11], SHA256 = reader.Line[12], SHA384 = reader.Line[13], SHA512 = reader.Line[14], SpamSum = reader.Line[15], Nodump = reader.Line[16], }; // If we have additional fields if (reader.Line.Count > HeaderWithExtendedHashesCount) row.ADDITIONAL_ELEMENTS = reader.Line.Skip(HeaderWithExtendedHashesCount).ToArray(); } rows.Add(row); } // Assign the rows to the Dat and return dat.Row = rows.ToArray(); return dat; } catch { // TODO: Handle logging the exception return default; } } } }