Skip to content

Commit 57771c1

Browse files
committed
Serialize all unparsed HIRC items as byte[]
1 parent 7f410c1 commit 57771c1

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using ME3Tweaks.Wwiser.Model.Hierarchy;
2+
3+
namespace ME3Tweaks.Wwiser.Tests.HierarchyTests;
4+
5+
public class UnknownItemTests
6+
{
7+
[Test]
8+
public void UnparsedHIRCItem_Reserializes()
9+
{
10+
var data = TestData.GetTestDataBytes(@"Hierarchy", @"Bus", "Bus_v56.bin");
11+
var (_, result) = TestHelpers.Deserialize<HircItemContainer>(data, 56);
12+
13+
Assert.That(result.Item, Is.TypeOf<EmptyHircItem>());
14+
15+
var reserialized = TestHelpers.Serialize(result, 56);
16+
Assert.That(reserialized, Is.EquivalentTo(data));
17+
}
18+
}
73 Bytes
Binary file not shown.

ME3Tweaks.Wwiser/Model/Hierarchy/EmptyHircItem.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ namespace ME3Tweaks.Wwiser.Model.Hierarchy;
66
public class EmptyHircItem : HircItem
77
{
88
[FieldOrder(0)]
9-
[FieldLength(nameof(HircItemContainer.Size), AncestorType = typeof(HircItemContainer),
10-
RelativeSourceMode = RelativeSourceMode.FindAncestor)]
11-
public byte[] Data { get; set; } = Array.Empty<byte>();
9+
public byte[] Data { get; set; } = [];
1210

13-
[Ignore] public override HircType HircType => HircType.Action;
11+
[Ignore] public override HircType HircType => HircType.None;
1412
}

ME3Tweaks.Wwiser/Model/Hierarchy/HircItemContainer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public class HircItemContainer
1414
[FieldOrder(3)]
1515
[FieldLength(nameof(Size))]
1616
[SubtypeFactory($"{nameof(Type)}.{nameof(Type.Value)}", typeof(HircItemSubtypeFactory))]
17+
[SubtypeDefault(typeof(EmptyHircItem))]
1718
public required HircItem Item { get; set; }
1819
}

ME3Tweaks.Wwiser/Model/Hierarchy/HircItemSubtypeFactory.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@ public class HircItemSubtypeFactory : ISubtypeFactory
2121
{ typeof(FxCustom), HircType.FxCustom },
2222
};
2323

24-
public bool TryGetKey(Type valueType, [UnscopedRef] out object key)
24+
public bool TryGetKey(Type valueType, out object? key)
2525
{
2626
if (TypeToEnum.TryGetValue(valueType, out var value))
2727
{
2828
key = value;
2929
return true;
3030
}
3131

32-
// fallback
33-
throw new ArgumentException($"Cannot parse Hirc type ${valueType}");
34-
//return false;
32+
key = null;
33+
return false;
3534
}
3635

37-
public bool TryGetType(object key, [UnscopedRef] out Type type)
36+
public bool TryGetType(object key, [UnscopedRef] out Type? type)
3837
{
3938
if (key is byte b)
4039
{
@@ -67,8 +66,8 @@ public bool TryGetType(object key, [UnscopedRef] out Type type)
6766
//HircType.Envelope =>
6867
//HircType.AudioDevice =>
6968
//HircType.TimeMod =>
70-
_ => throw new ArgumentException($"Cannot parse Hirc type ${(HircType)key} yet")
69+
_ => null
7170
};
72-
return true;
71+
return type != null;
7372
}
7473
}

0 commit comments

Comments
 (0)