Skip to content

Commit

Permalink
fix(Sah): fix write
Browse files Browse the repository at this point in the history
  • Loading branch information
matigramirez committed Oct 13, 2023
1 parent 3d41735 commit bc35910
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Parsec/Shaiya/Data/Sah.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Parsec.Extensions;
using Parsec.Serialization;
using Parsec.Shaiya.Core;

Expand Down Expand Up @@ -37,10 +36,24 @@ public Sah(string path, SDirectory rootDirectory, int fileCount)
FileCount = fileCount;
}

private string _signature = "SAH";

/// <summary>
/// SAH signature, normally "SAH" but it can be changed. Read as char[3].
/// </summary>
public string Signature { get; set; } = "SAH";
public string Signature
{
get => _signature;
set
{
if (value.Length != 3)
{
return;
}

_signature = value;
}
}

/// <summary>
/// 4 bytes after signature. Meaning isn't truly known but it's suspected that's used for versioning.
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/Parsec.Tests/Shaiya/Data/SahTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sah>("Shaiya/Data/sample.sah");
sah.Write(outputPath);
var patch = ParsecReader.FromFile<Sah>("Shaiya/Data/patch.sah");
patch.Write(patchOutputPath);

var sah2 = ParsecReader.FromFile<Sah>(outputPath);
var patch2 = ParsecReader.FromFile<Sah>(patchOutputPath);
Assert.Equal(sah.GetBytes(), sah2.GetBytes());
Assert.Equal(patch.GetBytes(), patch2.GetBytes());
}
}

0 comments on commit bc35910

Please sign in to comment.