From e43ebbf012bc8e102d2cb5ac55980d70d54ac049 Mon Sep 17 00:00:00 2001 From: henbagle Date: Wed, 27 Dec 2023 20:43:49 -0500 Subject: [PATCH] Uni implementation is now more correct, add tests --- .../FormatTests/UniTests.cs | 17 ++++++ ME3Tweaks.Wwiser/Formats/Uni.cs | 60 ++++++++++++------- .../Model/ParameterNode/InitialParamsV62.cs | 22 +++---- ME3Tweaks.Wwiser/WwiseBankParser.cs | 2 +- 4 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 ME3Tweaks.Wwiser.Tests/FormatTests/UniTests.cs diff --git a/ME3Tweaks.Wwiser.Tests/FormatTests/UniTests.cs b/ME3Tweaks.Wwiser.Tests/FormatTests/UniTests.cs new file mode 100644 index 0000000..d47d8aa --- /dev/null +++ b/ME3Tweaks.Wwiser.Tests/FormatTests/UniTests.cs @@ -0,0 +1,17 @@ +using ME3Tweaks.Wwiser.Formats; + +namespace ME3Tweaks.Wwiser.Tests.FormatTests; + +public class UniTests +{ + [TestCase(new byte[] {0x0, 0x0, 0x0, 0x0 })] + [TestCase(new byte[] {0x0, 0x0, 0xC0, 0xC0 })] + [TestCase(new byte[] {0x0, 0x0, 0x40, 0xC1 })] + [TestCase(new byte[] {0xBF, 0x65, 0xD7, 0x17 })] + public void UniReserializes(byte[] data) + { + var (_, result) = TestHelpers.Deserialize(data, 113); + var reserialized = TestHelpers.Serialize(result, 113); + Assert.That(reserialized, Is.EquivalentTo(data)); + } +} \ No newline at end of file diff --git a/ME3Tweaks.Wwiser/Formats/Uni.cs b/ME3Tweaks.Wwiser/Formats/Uni.cs index 50710e3..59e5a17 100644 --- a/ME3Tweaks.Wwiser/Formats/Uni.cs +++ b/ME3Tweaks.Wwiser/Formats/Uni.cs @@ -12,20 +12,28 @@ public class Uni : IBinarySerializable [Ignore] public uint Integer { get; set; } + + [Ignore] + public bool StoredAsFloat { get; set; } [Ignore] - public float Value => Float == 0.0f ? Integer : Float; + public float Value => StoredAsFloat ? Float : Integer; + + public Uni() { } + + public Uni(float f) + { + SetValue(BitConverter.GetBytes(f)); + } + + public Uni(uint i) + { + SetValue(BitConverter.GetBytes(i)); + } public void Serialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext) { - if (Float == 0.0f) - { - stream.Write(BitConverter.GetBytes(Integer)); - } - else - { - stream.Write(BitConverter.GetBytes(Float)); - } + stream.Write(StoredAsFloat ? BitConverter.GetBytes(Float) : BitConverter.GetBytes(Integer)); } public void Deserialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext) @@ -33,24 +41,34 @@ public void Deserialize(Stream stream, Endianness endianness, BinarySerializatio Span span = stackalloc byte[4]; var read = stream.Read(span); if (read != 4) throw new Exception(); + + SetValue(span); + } - uint value = BitConverter.ToUInt32(span); - if (value > 0x10000000) - { - Float = BitConverter.ToSingle(span); - } - else Integer = value; + private void SetValue(byte[] data) + { + if (data.Length != 4) throw new ArgumentException(); + SetValue(data.AsSpan()); } - public Uni() { } - - public Uni(float f) + private void SetValue(Span span) { - Float = f; + StoredAsFloat = StoreAsFloat(span); + if (StoredAsFloat) + { + Float = BitConverter.ToSingle(span); + Integer = 0; + } + else + { + Float = 0f; + Integer = BitConverter.ToUInt32(span); + } } - public Uni(uint i) + private static bool StoreAsFloat(Span data) { - Integer = i; + uint value = BitConverter.ToUInt32(data); + return value > 0x10000000; } } \ No newline at end of file diff --git a/ME3Tweaks.Wwiser/Model/ParameterNode/InitialParamsV62.cs b/ME3Tweaks.Wwiser/Model/ParameterNode/InitialParamsV62.cs index 785be1e..fd3a2d8 100644 --- a/ME3Tweaks.Wwiser/Model/ParameterNode/InitialParamsV62.cs +++ b/ME3Tweaks.Wwiser/Model/ParameterNode/InitialParamsV62.cs @@ -45,21 +45,15 @@ public ParameterValue() : base() { } public ParameterValue(Uni i) { - Float = i.Float; - Integer = i.Integer; - } - - public new void Serialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext) - { - var initialParams = serializationContext.FindAncestor(); - var id = initialParams.ParameterIds[initialParams.ParameterValues.Count + 1]; - if (id.PropValue is PropId.AttachedPluginFXID /*or PropId.AttenuationID*/) + if (i.Float != 0f) { - stream.Write(BitConverter.GetBytes(Integer)); + Float = i.Float; + StoredAsFloat = true; } - else + else if (Integer != 0) { - base.Serialize(stream, endianness, serializationContext); + Integer = i.Integer; + StoredAsFloat = false; } } @@ -68,12 +62,14 @@ public ParameterValue(Uni i) { var initialParams = serializationContext.FindAncestor(); var id = initialParams.ParameterIds[initialParams.ParameterValues.Count + 1]; - if (id.PropValue is PropId.AttachedPluginFXID /*or PropId.AttenuationID*/) + if (id.PropValue is PropId.AttachedPluginFXID or PropId.AttenuationID) { Span span = stackalloc byte[4]; var read = stream.Read(span); if (read != 4) throw new Exception(); Integer = BitConverter.ToUInt32(span); + Float = 0f; + StoredAsFloat = false; } else { diff --git a/ME3Tweaks.Wwiser/WwiseBankParser.cs b/ME3Tweaks.Wwiser/WwiseBankParser.cs index 2572481..9612626 100644 --- a/ME3Tweaks.Wwiser/WwiseBankParser.cs +++ b/ME3Tweaks.Wwiser/WwiseBankParser.cs @@ -37,7 +37,7 @@ public WwiseBankParser(Stream stream) _stream.Position = 0; } - public void ConvertVersion(uint version) + public void ConvertToVersion(uint version) { ConvertWithHeader(CreateSerializationContext() with { Version = version }); }