Skip to content

Commit

Permalink
Write tests for SpatializationMode stuff. Make them pass-ish
Browse files Browse the repository at this point in the history
  • Loading branch information
henbagle committed Dec 16, 2023
1 parent c464c22 commit 357bdf5
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using ME3Tweaks.Wwiser.Model.ParameterNode.Positioning;

namespace ME3Tweaks.Wwiser.Tests.ParameterNodeTests;

public class SpatializationModeTests
{
private static readonly byte[] Data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 32, 64, 128 };

[TestCase(122)]
[TestCase(126)]
[TestCase(129)]
public void GetHasAutomationFromSpatializationMode_BehaviorMatchesWwiser(int version)
{
var uVersion = (uint)version;
Assert.Multiple(() =>
{
foreach (var d in Data)
{
var originalResult = OriginalGetHasAutomation(d, false, uVersion);
var mode = SpatializationHelpers.GetModeFromByte(d, uVersion);
var newResult = SpatializationHelpers.GetHasAutomationFromMode(mode, false, uVersion);
Assert.That(newResult, Is.EqualTo(originalResult), $"{d} returned {newResult}");
}
});
}

[TestCase(122)]
[TestCase(126)]
[TestCase(129)]
public void GetSpatializationModeWithAutomation_IsProperlyReadByWwiser(int version)
{
var uVersion = (uint)version;
Assert.Multiple(() =>
{
foreach (var d in Data)
{
var s = (SpatializationMode)d;
var hasAutomation = SpatializationHelpers.GetModeFromHasAutomation(true, s, uVersion);
var noAutomation = SpatializationHelpers.GetModeFromHasAutomation(false, s, uVersion);

var hasAutoByte = SpatializationHelpers.GetByteFromMode(hasAutomation, uVersion);
var noAutoByte = SpatializationHelpers.GetByteFromMode(noAutomation, uVersion);

Assert.That(OriginalGetHasAutomation(hasAutoByte, false, uVersion), Is.True, $"{d} True");
Assert.That(OriginalGetHasAutomation(noAutoByte, false, uVersion), Is.False, $"{d} False");
}
});
}


/// <summary>
/// Original implementation from Wwiser python code
/// </summary>
/// <param name="uBits3D"></param>
/// <param name="initialAutomation"></param>
/// <param name="version"></param>
/// <returns></returns>
private static bool OriginalGetHasAutomation(byte uBits3D, bool initialAutomation, uint version)
{
if (version <= 122)
{
return ((uBits3D >> 0) & 3) != 1;
}
if (version <= 126)
{
return ((uBits3D >> 4) & 1) != 1;
}
if (version <= 129)
{
return ((uBits3D >> 6) & 1) != 1;
}
return initialAutomation;
}
}
35 changes: 35 additions & 0 deletions ME3Tweaks.Wwiser/Formats/V36Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,39 @@ public void Deserialize(Stream stream, Endianness endianness, BinarySerializatio
}
else Value = (byte)stream.ReadByte();
}
}

public class V36ShortCount : IBinarySerializable
{
[Ignore]
public ushort Value { get; set; }

public void Serialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext)
{
var version = serializationContext.FindAncestor<BankSerializationContext>().Version;
if (version <= 36)
{
stream.Write(BitConverter.GetBytes((uint)Value));
}
else stream.Write(BitConverter.GetBytes(Value));
}

public void Deserialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext)
{
var version = serializationContext.FindAncestor<BankSerializationContext>().Version;
if (version <= 36)
{
Span<byte> span = stackalloc byte[4];
var read = stream.Read(span);
if (read != 4) throw new Exception();
Value = (byte)BitConverter.ToUInt32(span);
}
else
{
Span<byte> span = stackalloc byte[2];
var read = stream.Read(span);
if (read != 2) throw new Exception();
Value = BitConverter.ToUInt16(span);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ private void Write3DParams(Stream stream, uint version)
}
else
{
var mode = SpatializationHelpers.GetModeFromHasAutomation(HasAutomation, Mode, version);
stream.WriteByte(SpatializationHelpers.GetByteFromMode(mode, version));
Mode = SpatializationHelpers.GetModeFromHasAutomation(HasAutomation, Mode, version);
stream.WriteByte(SpatializationHelpers.GetByteFromMode(Mode, version));
}

if (version <= 129) stream.Write(BitConverter.GetBytes(AttenuationId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ public static SpatializationMode GetModeFromByte(byte value, uint version)
var mode = (SpatializationMode)value;
if (version <= 132)
{

// Wipe out any extraneous flags?
if(version <= 126) mode &= (SpatializationMode)0b0001_1111;

// HoldListener and HoldEmitter are one flag lower on version 132 and lower
if (mode.HasFlag(SpatializationMode.HoldEmitterPosAndOrient))
{
mode &= SpatializationMode.HoldListenerOrient;
mode |= SpatializationMode.HoldListenerOrient;
mode &= ~SpatializationMode.HoldEmitterPosAndOrient;
}

if (mode.HasFlag(SpatializationMode.EnableAttenuation))
{
mode &= SpatializationMode.HoldEmitterPosAndOrient;
mode |= SpatializationMode.HoldEmitterPosAndOrient;
mode &= ~SpatializationMode.EnableAttenuation;
}

}

return mode;
Expand Down Expand Up @@ -79,31 +84,26 @@ public static PositioningType GetTypeFromBools(bool hasAutomation, bool hasDynam
/// </summary>
public static SpatializationMode GetModeFromHasAutomation(bool hasAutomation, SpatializationMode modeIn, uint version)
{
// This code is super weird and may not be correct for all cases?
if (hasAutomation)
{
modeIn = version switch
return version switch
{
<= 122 => modeIn.HasFlag(SpatializationMode.PositionOnly)
? modeIn : modeIn & ~SpatializationMode.PositionAndOrientation,
<= 122 => modeIn & ~SpatializationMode.PositionOnly,
<= 126 => modeIn & ~SpatializationMode.HoldListenerOrient,
<= 129 => modeIn & ~SpatializationMode.EnableDiffraction,
_ => modeIn
};
}
else
{
modeIn = version switch
return version switch
{
<= 122 => modeIn.HasFlag(SpatializationMode.PositionAndOrientation)
? modeIn : modeIn | SpatializationMode.PositionOnly,
<= 122 => (modeIn | SpatializationMode.PositionOnly) & ~SpatializationMode.PositionAndOrientation,
<= 126 => modeIn | SpatializationMode.HoldListenerOrient,
<= 129 => modeIn | SpatializationMode.EnableDiffraction,
_ => modeIn
};
}

return modeIn;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ME3Tweaks.Wwiser.Model.ParameterNode;
public class RtpcParameterNodeBase
{
[FieldOrder(0)]
public V36Count RTPCCount { get; set; } = new();
public V36ShortCount RTPCCount { get; set; } = new();

[FieldOrder(1)]
[FieldCount($"{nameof(RTPCCount)}.{nameof(RTPCCount.Value)}")]
Expand Down

0 comments on commit 357bdf5

Please sign in to comment.