From bc359106e9e5852f824dcc94837dfd06c1f0eed0 Mon Sep 17 00:00:00 2001 From: matias Date: Thu, 12 Oct 2023 22:33:15 -0300 Subject: [PATCH] fix(Sah): fix write --- src/Parsec/Shaiya/Data/Sah.cs | 19 ++++++++++++++++--- tests/Parsec.Tests/Shaiya/Data/SahTests.cs | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Parsec/Shaiya/Data/Sah.cs b/src/Parsec/Shaiya/Data/Sah.cs index b3d562ab..d090bb72 100644 --- a/src/Parsec/Shaiya/Data/Sah.cs +++ b/src/Parsec/Shaiya/Data/Sah.cs @@ -1,6 +1,5 @@ using System.Runtime.Serialization; using Newtonsoft.Json; -using Parsec.Extensions; using Parsec.Serialization; using Parsec.Shaiya.Core; @@ -37,10 +36,24 @@ public Sah(string path, SDirectory rootDirectory, int fileCount) FileCount = fileCount; } + private string _signature = "SAH"; + /// /// SAH signature, normally "SAH" but it can be changed. Read as char[3]. /// - public string Signature { get; set; } = "SAH"; + public string Signature + { + get => _signature; + set + { + if (value.Length != 3) + { + return; + } + + _signature = value; + } + } /// /// 4 bytes after signature. Meaning isn't truly known but it's suspected that's used for versioning. @@ -140,7 +153,7 @@ public SDirectory EnsureFolderExists(string path) protected override void Write(SBinaryWriter binaryWriter) { - binaryWriter.Write(Signature.Take(3).ToString()); + binaryWriter.Write(Signature, isLengthPrefixed: false, includeStringTerminator: false); binaryWriter.Write(Version); binaryWriter.Write(FileCount); binaryWriter.Write(new byte[40]); // Padding diff --git a/tests/Parsec.Tests/Shaiya/Data/SahTests.cs b/tests/Parsec.Tests/Shaiya/Data/SahTests.cs index 87c7819c..c57d46d6 100644 --- a/tests/Parsec.Tests/Shaiya/Data/SahTests.cs +++ b/tests/Parsec.Tests/Shaiya/Data/SahTests.cs @@ -61,4 +61,21 @@ public void SahJsonCreationTest() Assert.NotNull(folder); Assert.NotNull(file); } + + [Fact] + public void SahReadWriteTest() + { + var outputPath = "Shaiya/Data/sample_out.sah"; + var patchOutputPath = "Shaiya/Data/sample_out_patch.sah"; + + var sah = ParsecReader.FromFile("Shaiya/Data/sample.sah"); + sah.Write(outputPath); + var patch = ParsecReader.FromFile("Shaiya/Data/patch.sah"); + patch.Write(patchOutputPath); + + var sah2 = ParsecReader.FromFile(outputPath); + var patch2 = ParsecReader.FromFile(patchOutputPath); + Assert.Equal(sah.GetBytes(), sah2.GetBytes()); + Assert.Equal(patch.GetBytes(), patch2.GetBytes()); + } }