From c134b309b856a9f5f8bce541a154f437085b8a3c Mon Sep 17 00:00:00 2001 From: henbagle Date: Sat, 9 Dec 2023 15:19:39 -0500 Subject: [PATCH] Finish ActionType parsing --- .../ActionTests/ActionTypeTests.cs | 39 ++++++++------- ME3Tweaks.Wwiser/Model/Action/ActionType.cs | 47 ++++++++++++++----- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/ME3Tweaks.Wwiser.Tests/ActionTests/ActionTypeTests.cs b/ME3Tweaks.Wwiser.Tests/ActionTests/ActionTypeTests.cs index bb29d81..0c49b99 100644 --- a/ME3Tweaks.Wwiser.Tests/ActionTests/ActionTypeTests.cs +++ b/ME3Tweaks.Wwiser.Tests/ActionTests/ActionTypeTests.cs @@ -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((byte)hex, 56); + var bytes = BitConverter.GetBytes((uint)hex); + var (_, result) = TestHelpers.Deserialize(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(hex, 72); + var bytes = BitConverter.GetBytes((ushort)hex); + + var (_, result) = TestHelpers.Deserialize(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)); } } \ No newline at end of file diff --git a/ME3Tweaks.Wwiser/Model/Action/ActionType.cs b/ME3Tweaks.Wwiser/Model/Action/ActionType.cs index 8a055fc..c20f4f4 100644 --- a/ME3Tweaks.Wwiser/Model/Action/ActionType.cs +++ b/ME3Tweaks.Wwiser/Model/Action/ActionType.cs @@ -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().Version; byte output; - if (version <= 65) + if (version <= 56) { output = Value switch { @@ -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 { @@ -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().Version; - var value = (byte)stream.ReadByte(); - if (version <= 65) + + //var value = (byte)stream.ReadByte(); + if (version <= 56) { - Value = value switch + Span 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, @@ -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 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 }; } }