From ee33efd2c046afe5d39530fc274e24fd549d7796 Mon Sep 17 00:00:00 2001 From: henbagle Date: Thu, 28 Dec 2023 10:12:31 -0500 Subject: [PATCH] Add non-async serialization methods to WwiseBankParser --- .../WwiseBankParserTests.cs | 25 ++++++++++++++----- ME3Tweaks.Wwiser/WwiseBankParser.cs | 25 +++++++++++++++++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/ME3Tweaks.Wwiser.Tests/WwiseBankParserTests.cs b/ME3Tweaks.Wwiser.Tests/WwiseBankParserTests.cs index 7c0e456..38f513c 100644 --- a/ME3Tweaks.Wwiser.Tests/WwiseBankParserTests.cs +++ b/ME3Tweaks.Wwiser.Tests/WwiseBankParserTests.cs @@ -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(); diff --git a/ME3Tweaks.Wwiser/WwiseBankParser.cs b/ME3Tweaks.Wwiser/WwiseBankParser.cs index 9612626..fa5516b 100644 --- a/ME3Tweaks.Wwiser/WwiseBankParser.cs +++ b/ME3Tweaks.Wwiser/WwiseBankParser.cs @@ -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 }) { @@ -134,8 +134,20 @@ public async Task Deserialize() .DeserializeAsync(_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(_stream, CreateSerializationContext()); + WwiseBank = new WwiseBank(root); + } - public async Task Serialize(Stream stream) + public async Task SerializeToAsync(Stream stream) { if (WwiseBank is null) { @@ -143,6 +155,15 @@ public async Task Serialize(Stream stream) } 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() {