Skip to content

Commit

Permalink
More small parsing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
henbagle committed Dec 12, 2023
1 parent 22998b8 commit d6edad3
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 11 deletions.
3 changes: 2 additions & 1 deletion ME3Tweaks.Wwiser.Tests/HierarchyTests/BankSourceDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ public class BankSourceDataTests
[TestCase("BSDV134.bin", 134)]
[TestCase("BSDV56.bin", 56)]
[TestCase("BSDV56_2.bin", 56)]
[TestCase("BSDV56_3.bin", 56)]
[TestCase("BSDV44.bin", 44)]
public void BankSourceData_Reserializes(string filename, int version)
{
var data = TestData.GetTestDataBytes(@"Hierarchy",@"BankSourceData", filename);
var (_, result) = TestHelpers.Deserialize<BankSourceData>(data, version);

var reserialized = TestHelpers.Serialize(result, version);
Assert.That(reserialized, Is.EqualTo(data.Take(reserialized.Length).ToArray()));
Assert.That(reserialized, Is.EqualTo(data));
}
}
11 changes: 11 additions & 0 deletions ME3Tweaks.Wwiser.Tests/HierarchyTests/HierarchyChunkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ public void SimpleChunk_v134_Parses()
});
}
}

[Test]
public void BigChunkTest()
{
var data = TestData.GetTestDataBytes(@"Hierarchy", @"LargeFullChunks", @"HIRC_V56.bin");
var (serializer, result) = TestHelpers.Deserialize<ChunkContainer>(data, 56);

var reserialized = TestHelpers.Serialize(result, 56);
TestHelpers.WriteStreamToFile(new MemoryStream(reserialized), TestData.GetTestDataFilePath(@"Hierarchy", @"LargeFullChunks", @"Out56.bin"));
Assert.That(reserialized, Is.EqualTo(data));
}
}
18 changes: 18 additions & 0 deletions ME3Tweaks.Wwiser.Tests/HierarchyTests/RandSeqContainerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ME3Tweaks.Wwiser.Model.Hierarchy;
using ME3Tweaks.Wwiser.Model.Hierarchy.Enums;

namespace ME3Tweaks.Wwiser.Tests.HierarchyTests;

public class RandSeqContainerTests
{

[TestCase("RandSeqContainer_V56.bin", 56)]
public void RandSeqContainer_Reserializes(string filename, int version)
{
var data = TestData.GetTestDataBytes(@"Hierarchy",@"RandSeqContainer", filename);
var (_, result) = TestHelpers.Deserialize<RandSeqContainer>(data, version);

var reserialized = TestHelpers.Serialize(result, version);
Assert.That(reserialized, Is.EqualTo(data));
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion ME3Tweaks.Wwiser.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static byte[] Serialize(object data, int version, bool useModulator = fa
return Serialize(data, (uint)version, useModulator);
}

private static void WriteStreamToFile(Stream stream, string filepath)
public static void WriteStreamToFile(Stream stream, string filepath)
{
stream.Seek(0, SeekOrigin.Begin);

Expand Down
11 changes: 7 additions & 4 deletions ME3Tweaks.Wwiser.Tests/WwiseBankParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ public void OnInstantiation_WithFullBank_ParsesVersionCorrectly(string bankFileN
Assert.That(parser.Version, Is.EqualTo(correctVersion));
}

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

var stream = new MemoryStream();
await parser.Serialize(stream);
var data = TestData.GetTestDataBytes("WholeBanks", "ME3_v56_2.bnk");
//TestHelpers.WriteStreamToFile(stream, TestData.GetTestDataFilePath("WholeBanks", "Out1.bnk"));
var data = TestData.GetTestDataBytes("WholeBanks", filename);
Assert.That(stream.ToArray(), Is.EquivalentTo(data));
}
}
4 changes: 2 additions & 2 deletions ME3Tweaks.Wwiser/Model/Hierarchy/BankSourceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class BankSourceData
[FieldOrder(3)]
public MediaInformation MediaInformation { get; set; } = new();

/*[FieldOrder(4)]
[FieldOrder(4)]
[SerializeWhen(nameof(Plugin), true,
ConverterType = typeof(HasPluginParamConverter))]
public PluginParameters PluginParameters { get; set; } = new();*/
public PluginParameters PluginParameters { get; set; } = new();
}

public class AudioFormat
Expand Down
11 changes: 11 additions & 0 deletions ME3Tweaks.Wwiser/Model/Hierarchy/EmptyHircItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BinarySerialization;

namespace ME3Tweaks.Wwiser.Model.Hierarchy;

public class EmptyHircItem : HircItem
{
[FieldOrder(0)]
[FieldLength(nameof(HircItemContainer.Size), AncestorType = typeof(HircItemContainer),
RelativeSourceMode = RelativeSourceMode.FindAncestor)]
public byte[] Data { get; set; } = Array.Empty<byte>();
}
3 changes: 2 additions & 1 deletion ME3Tweaks.Wwiser/Model/Hierarchy/HircTypeFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using BinarySerialization;
using ME3Tweaks.Wwiser.Model.Action;
using ME3Tweaks.Wwiser.Model.Hierarchy.Enums;

namespace ME3Tweaks.Wwiser.Model.Hierarchy;
Expand Down Expand Up @@ -65,7 +66,7 @@ public bool TryGetType(object key, [UnscopedRef] out Type type)
//HircType.Envelope =>
//HircType.AudioDevice =>
//HircType.TimeMod =>
_ => typeof(HircItem)
_ => typeof(EmptyHircItem)
};
return true;
}
Expand Down
11 changes: 9 additions & 2 deletions ME3Tweaks.Wwiser/Model/Hierarchy/RandSeqContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class RandSeqContainer : HircItem

[FieldOrder(11)]
public Children Children { get; set; } = new();

[FieldOrder(12)]
public Playlist Playlist { get; set; } = new();
}

public class RanSeqFlags : IBinarySerializable
Expand Down Expand Up @@ -157,14 +160,18 @@ public void Serialize(Stream stream, Endianness endianness, BinarySerializationC
public void Deserialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext)
{
var version = serializationContext.FindAncestor<BankSerializationContext>().Version;
Span<byte> span = stackalloc byte[4];
var read = stream.Read(span);
if (read != 4) throw new Exception();
Id = BitConverter.ToUInt32(span);

if (version <= 56)
{
Weight = stream.ReadByte();
}
else
{
Span<byte> span = stackalloc byte[4];
var read = stream.Read(span);
read = stream.Read(span);
if (read != 4) throw new Exception();
Weight = BitConverter.ToInt32(span);
}
Expand Down

0 comments on commit d6edad3

Please sign in to comment.