Add EverdriveSMDB deserialization test, fix issues

This commit is contained in:
Matt Nadareski
2023-07-13 16:44:14 -04:00
parent fcd95a6e60
commit 025bea69a1
5 changed files with 6244 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
namespace SabreTools.Models.EverdriveSMDB
{
public class Dat
{
public Row[]? Row { get; set; }
}
}

View File

@@ -15,6 +15,13 @@ namespace SabreTools.Models.EverdriveSMDB
public string CRC32 { get; set; }
public long? Size { get; set; }
public string? Size { get; set; }
#region DO NOT USE IN PRODUCTION
/// <remarks>Should be empty</remarks>
public string[]? ADDITIONAL_ELEMENTS { get; set; }
#endregion
}
}

View File

@@ -0,0 +1,96 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.IO.Readers;
using SabreTools.Models.EverdriveSMDB;
namespace SabreTools.Serialization
{
/// <summary>
/// Separated value serializer for Everdrive SMDBs
/// </summary>
public class EverdriveSMDB
{
/// <summary>
/// Deserializes an Everdrive SMDB to the defined type
/// </summary>
/// <param name="path">Path to the file to deserialize</param>
/// <returns>Deserialized data on success, null on failure</returns>
public static Dat? Deserialize(string path)
{
try
{
using var stream = PathProcessor.OpenStream(path);
return Deserialize(stream);
}
catch
{
// TODO: Handle logging the exception
return default;
}
}
/// <summary>
/// Deserializes an Everdrive SMDB in a stream to the defined type
/// </summary>
/// <param name="stream">Stream to deserialize</param>
/// <returns>Deserialized data on success, null on failure</returns>
public static Dat? Deserialize(Stream? stream)
{
try
{
// If the stream is null
if (stream == null)
return default;
// Setup the reader and output
var reader = new SeparatedValueReader(stream, Encoding.UTF8)
{
Header = false,
Separator = '\t',
VerifyFieldCount = false,
};
var dat = new Dat();
// Loop through the rows and parse out values
var rows = new List<Row>();
while (!reader.EndOfStream)
{
// If we have no next line
if (!reader.ReadNextLine())
break;
// Parse the line into a row
var row = new Row
{
SHA256 = reader.Line[0],
Name = reader.Line[1],
SHA1 = reader.Line[2],
MD5 = reader.Line[3],
CRC32 = reader.Line[4],
};
// If we have the size field
if (reader.Line.Count > 5)
row.Size = reader.Line[5];
// If we have additional fields
if (reader.Line.Count > 6)
row.ADDITIONAL_ELEMENTS = reader.Line.Skip(5).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;
}
}
}
}

View File

@@ -75,6 +75,26 @@ namespace SabreTools.Test.Parser
}
}
[Fact]
public void EverdriveSMDBDeserializeTest()
{
// Open the file for reading
string filename = System.IO.Path.Combine(Environment.CurrentDirectory, "TestData", "test-smdb-files.txt");
// Deserialize the file
var dat = Serialization.EverdriveSMDB.Deserialize(filename);
// Validate the values
Assert.NotNull(dat?.Row);
Assert.Equal(6113, dat.Row.Length);
// Validate we're not missing any attributes or elements
foreach (var file in dat.Row)
{
Assert.Null(file.ADDITIONAL_ELEMENTS);
}
}
[Theory]
[InlineData("test-sfv.sfv", Hash.CRC)]
[InlineData("test-md5.md5", Hash.MD5)]

File diff suppressed because it is too large Load Diff