Skip to content

Commit

Permalink
Uni implementation is now more correct, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
henbagle committed Dec 28, 2023
1 parent a4bd277 commit e43ebbf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
17 changes: 17 additions & 0 deletions ME3Tweaks.Wwiser.Tests/FormatTests/UniTests.cs
Original file line number Diff line number Diff line change
@@ -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<Uni>(data, 113);
var reserialized = TestHelpers.Serialize(result, 113);
Assert.That(reserialized, Is.EquivalentTo(data));
}
}
60 changes: 39 additions & 21 deletions ME3Tweaks.Wwiser/Formats/Uni.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,63 @@ 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)
{
Span<byte> 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<byte> 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<byte> data)
{
Integer = i;
uint value = BitConverter.ToUInt32(data);
return value > 0x10000000;
}
}
22 changes: 9 additions & 13 deletions ME3Tweaks.Wwiser/Model/ParameterNode/InitialParamsV62.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InitialParamsV62>();
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;
}
}

Expand All @@ -68,12 +62,14 @@ public ParameterValue(Uni i)
{
var initialParams = serializationContext.FindAncestor<InitialParamsV62>();
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<byte> span = stackalloc byte[4];
var read = stream.Read(span);
if (read != 4) throw new Exception();
Integer = BitConverter.ToUInt32(span);
Float = 0f;
StoredAsFloat = false;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion ME3Tweaks.Wwiser/WwiseBankParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down

0 comments on commit e43ebbf

Please sign in to comment.