Skip to content

Commit

Permalink
Finish ActionType parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
henbagle committed Dec 9, 2023
1 parent 14237fa commit c134b30
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
39 changes: 21 additions & 18 deletions ME3Tweaks.Wwiser.Tests/ActionTests/ActionTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ namespace ME3Tweaks.Wwiser.Tests.ActionTests;

public class ActionTypeTests
{
[TestCase(0x01, ActionTypeValue.Stop)]
[TestCase(0x09, ActionTypeValue.SetPitch2)]
[TestCase(0x0A, ActionTypeValue.SetBusVolume1)]
[TestCase(0x0C, ActionTypeValue.SetLFE1)]
[TestCase(0x0E, ActionTypeValue.SetLPF1)]
[TestCase(0x01011, ActionTypeValue.Stop)]
[TestCase(0x09011, ActionTypeValue.SetPitch2)]
[TestCase(0x0A011, ActionTypeValue.SetBusVolume1)]
[TestCase(0x0C011, ActionTypeValue.SetLFE1)]
[TestCase(0x0E011, ActionTypeValue.SetLPF1)]
public void ActionTypeParsesAndReserializes_V56(int hex, ActionTypeValue expected)
{
var (_, result) = TestHelpers.Deserialize<ActionType>((byte)hex, 56);
var bytes = BitConverter.GetBytes((uint)hex);
var (_, result) = TestHelpers.Deserialize<ActionType>(bytes, 56);
Assert.That(result.Value, Is.EqualTo(expected));

var reserialized = TestHelpers.Serialize(result, 56);
Assert.That(reserialized[0], Is.EqualTo(hex));
Assert.That(reserialized, Is.EquivalentTo(bytes));
}

[TestCase(0x01, ActionTypeValue.Stop)]
[TestCase(0x09, ActionTypeValue.SetPitch2)]
[TestCase(0x0A, ActionTypeValue.SetNone1)]
[TestCase(0x0B, ActionTypeValue.SetNone2)]
[TestCase(0x0C, ActionTypeValue.SetBusVolume1)]
[TestCase(0x0E, ActionTypeValue.SetLPF1)]
[TestCase(0x19, ActionTypeValue.SetSwitch)]
[TestCase(0x1A, ActionTypeValue.BypassFX1)]
public void ActionTypeParsesAndReserializes_V72(byte hex, ActionTypeValue expected)
[TestCase(0x0101, ActionTypeValue.Stop)]
[TestCase(0x0901, ActionTypeValue.SetPitch2)]
[TestCase(0x0A01, ActionTypeValue.SetNone1)]
[TestCase(0x0B01, ActionTypeValue.SetNone2)]
[TestCase(0x0C01, ActionTypeValue.SetBusVolume1)]
[TestCase(0x0E01, ActionTypeValue.SetLPF1)]
[TestCase(0x1901, ActionTypeValue.SetSwitch)]
[TestCase(0x1A01, ActionTypeValue.BypassFX1)]
public void ActionTypeParsesAndReserializes_V72(int hex, ActionTypeValue expected)
{
var (_, result) = TestHelpers.Deserialize<ActionType>(hex, 72);
var bytes = BitConverter.GetBytes((ushort)hex);

var (_, result) = TestHelpers.Deserialize<ActionType>(bytes, 72);
Assert.That(result.Value, Is.EqualTo(expected));

var reserialized = TestHelpers.Serialize(result, 72);
Assert.That(reserialized[0], Is.EqualTo(hex));
Assert.That(reserialized, Is.EquivalentTo(bytes));
}
}
47 changes: 36 additions & 11 deletions ME3Tweaks.Wwiser/Model/Action/ActionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ public class ActionType : IBinarySerializable
{
[Ignore]
public ActionTypeValue Value { get; set; }

[Ignore]
public uint Data { get; set; }

public void Serialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext)
{
var version = serializationContext.FindAncestor<BankSerializationContext>().Version;
byte output;
if (version <= 65)
if (version <= 56)
{
output = Value switch
{
Expand All @@ -29,6 +32,9 @@ public void Serialize(Stream stream, Endianness endianness, BinarySerializationC
>= ActionTypeValue.SetBusVolume1 => (byte)(Value - 2),
_ => (byte)Value
};
var shifted = (uint)(output << 12);
var value = shifted | Data;
stream.Write(BitConverter.GetBytes(value));
}
else
{
Expand All @@ -38,17 +44,28 @@ public void Serialize(Stream stream, Endianness endianness, BinarySerializationC
>= ActionTypeValue.SetLPF1 => (byte)(Value - 2),
_ => (byte)Value
};
var value = (ushort)((ushort)(output << 8) | Data);
stream.Write(BitConverter.GetBytes(value));
}
stream.WriteByte(output);

}

public void Deserialize(Stream stream, Endianness endianness, BinarySerializationContext serializationContext)
{
var version = serializationContext.FindAncestor<BankSerializationContext>().Version;
var value = (byte)stream.ReadByte();
if (version <= 65)

//var value = (byte)stream.ReadByte();
if (version <= 56)
{
Value = value switch
Span<byte> span = stackalloc byte[4];
var read = stream.Read(span);
if (read != 4) throw new Exception();
uint value = BitConverter.ToUInt32(span);

var enumValue = value >> 12;
Data = value & 0xFFF;

Value = enumValue switch
{
0x20 => ActionTypeValue.Event1,
0x30 => ActionTypeValue.Event2,
Expand All @@ -61,17 +78,25 @@ public void Deserialize(Stream stream, Endianness endianness, BinarySerializatio
0x90 => ActionTypeValue.Break,
0xA0 => ActionTypeValue.Trigger,
0xB0 => ActionTypeValue.Seek,
>= 0x0A => (ActionTypeValue)(value + 2),
_ => (ActionTypeValue)value
>= 0x0A => (ActionTypeValue)(enumValue + 2),
_ => (ActionTypeValue)enumValue
};
}
else
{
Value = value switch
Span<byte> span = stackalloc byte[2];
var read = stream.Read(span);
if (read != 2) throw new Exception();
ushort value = BitConverter.ToUInt16(span);

var enumValue = value >> 8;
Data = (uint)value & 0xFF;

Value = enumValue switch
{
>= 0x1A => (ActionTypeValue)(value + 3),
>= 0x0E => (ActionTypeValue)(value + 2),
_ => (ActionTypeValue)value
>= 0x1A => (ActionTypeValue)(enumValue + 3),
>= 0x0E => (ActionTypeValue)(enumValue + 2),
_ => (ActionTypeValue)enumValue
};
}
}
Expand Down

0 comments on commit c134b30

Please sign in to comment.