Skip to content

Commit

Permalink
Add non-async serialization methods to WwiseBankParser
Browse files Browse the repository at this point in the history
  • Loading branch information
henbagle committed Dec 28, 2023
1 parent e43ebbf commit ee33efd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
25 changes: 19 additions & 6 deletions ME3Tweaks.Wwiser.Tests/WwiseBankParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,42 @@ public void OnInstantiation_WithFullBank_ParsesVersionCorrectly(string bankFileN
Assert.That(parser.Version, Is.EqualTo(correctVersion));
}

[Test]
public void FullBank_Reserializes_Synchronous()
{
var parser = new WwiseBankParser(TestData.GetTestDataFilePath("WholeBanks", "ME3_v56_1.bnk"));
parser.Deserialize();

var stream = new MemoryStream();
parser.SerializeTo(stream);
//TestHelpers.WriteStreamToFile(stream, TestData.GetTestDataFilePath("WholeBanks", "Out1.bnk"));
var data = TestData.GetTestDataBytes("WholeBanks", "ME3_v56_1.bnk");
Assert.That(stream.ToArray(), Is.EquivalentTo(data));
}

[TestCase("ME3_v56_1.bnk")]
[TestCase("ME3_v56_2.bnk")]
[TestCase("ME3_v56_3.bnk")]
public async Task FullBank_V56_Reserializes(string filename)
public async Task FullBank_V56_Reserializes_Async(string filename)
{
var parser = new WwiseBankParser(TestData.GetTestDataFilePath("WholeBanks", filename));
await parser.Deserialize();
await parser.DeserializeAsync();

var stream = new MemoryStream();
await parser.Serialize(stream);
await parser.SerializeToAsync(stream);
//TestHelpers.WriteStreamToFile(stream, TestData.GetTestDataFilePath("WholeBanks", "Out1.bnk"));
var data = TestData.GetTestDataBytes("WholeBanks", filename);
Assert.That(stream.ToArray(), Is.EquivalentTo(data));
}

[TestCase("LE3_v134_1.bnk")] // TODO: What is at the end of this file?
public async Task FullBank_V134_Reserializes(string filename)
public async Task FullBank_V134_Reserializes_Async(string filename)
{
var parser = new WwiseBankParser(TestData.GetTestDataFilePath("WholeBanks", filename));
await parser.Deserialize();
await parser.DeserializeAsync();

var stream = new MemoryStream();
await parser.Serialize(stream);
await parser.SerializeToAsync(stream);
//TestHelpers.WriteStreamToFile(stream, TestData.GetTestDataFilePath("WholeBanks", "Out1.bnk"));
var data = TestData.GetTestDataBytes("WholeBanks", filename);
var outData = stream.ToArray();
Expand Down
25 changes: 23 additions & 2 deletions ME3Tweaks.Wwiser/WwiseBankParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void ConvertWithHeader(BankSerializationContext context)
return (version, feedback);
}

public async Task Deserialize()
public async Task DeserializeAsync()
{
if (_stream is null or { Length : 0 })
{
Expand All @@ -134,15 +134,36 @@ public async Task Deserialize()
.DeserializeAsync<WwiseBankRoot>(_stream, CreateSerializationContext());
WwiseBank = new WwiseBank(root);
}

public void Deserialize()
{
if (_stream is null or { Length : 0 })
{
throw new Exception("Inner stream is null or empty, cannot deserialize.");
}

_stream.Position = 0;
var root = _serializer.Deserialize<WwiseBankRoot>(_stream, CreateSerializationContext());
WwiseBank = new WwiseBank(root);
}

public async Task Serialize(Stream stream)
public async Task SerializeToAsync(Stream stream)
{
if (WwiseBank is null)
{
throw new InvalidOperationException("Cannot serialize a null WwiseBank");
}
await _serializer.SerializeAsync(stream,WwiseBank.ToModel(), CreateSerializationContext());
}

public void SerializeTo(Stream stream)
{
if (WwiseBank is null)
{
throw new InvalidOperationException("Cannot serialize a null WwiseBank");
}
_serializer.Serialize(stream,WwiseBank.ToModel(), CreateSerializationContext());
}

private BankSerializationContext CreateSerializationContext()
{
Expand Down

0 comments on commit ee33efd

Please sign in to comment.