Skip to content

Commit

Permalink
Create shortcut attributes for serializing against version. Reduces a…
Browse files Browse the repository at this point in the history
… lot of noise in the codebase!
  • Loading branch information
henbagle committed Dec 4, 2023
1 parent 9c0c155 commit 00be898
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 107 deletions.
12 changes: 12 additions & 0 deletions ME3Tweaks.Wwiser/Attributes/SerializeWhenVersionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BinarySerialization;

namespace ME3Tweaks.Wwiser.Attributes;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public class SerializeWhenVersionAttribute : SerializeWhenAttribute
{
public SerializeWhenVersionAttribute(object value, ComparisonOperator @operator = ComparisonOperator.Equal) : base(nameof(BankSerializationContext.Version), value, @operator)
{
RelativeSourceMode = RelativeSourceMode.SerializationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BinarySerialization;
using ME3Tweaks.Wwiser.Converters;

namespace ME3Tweaks.Wwiser.Attributes;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public class SerializeWhenVersionBetweenAttribute : SerializeWhenVersionAttribute
{
public SerializeWhenVersionBetweenAttribute(uint low, uint high, ComparisonOperator @operator = ComparisonOperator.Equal) : base(true, @operator)
{
RelativeSourceMode = RelativeSourceMode.SerializationContext;
ConverterType = typeof(BetweenConverter);
ConverterParameter = new[] { low, high };
}
}
20 changes: 9 additions & 11 deletions ME3Tweaks.Wwiser/Model/BankHeaderChunk.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BinarySerialization;
using ME3Tweaks.Wwiser.Converters;
using ME3Tweaks.Wwiser.Attributes;

namespace ME3Tweaks.Wwiser.Model
{
Expand Down Expand Up @@ -76,23 +76,23 @@ public class BankHeaderChunk : Chunk
/// (<= v122)
/// </summary>
[FieldOrder(2)]
[SerializeWhen(nameof(BankGeneratorVersion), 122, ComparisonOperator.LessThanOrEqual)]
[SerializeWhenVersion(122, ComparisonOperator.LessThanOrEqual)]
public LanguageID LanguageId { get; set; }

/// <summary>
/// String hash of language ID
/// (&gt; v122)
/// </summary>
[FieldOrder(3)]
[SerializeWhen(nameof(BankGeneratorVersion), 122, ComparisonOperator.GreaterThan)]
[SerializeWhenVersion(122, ComparisonOperator.GreaterThan)]
public uint LanguageIdStringHash { get; set; }

/// <summary>
/// Unknown - potentially timestamp?
/// (&lt;= v26)
/// </summary>
[FieldOrder(4)]
[SerializeWhen(nameof(BankGeneratorVersion), 26, ComparisonOperator.LessThanOrEqual)]
[SerializeWhenVersion(26, ComparisonOperator.LessThanOrEqual)]
public ulong Timestamp { get; set; }

/// <summary>
Expand All @@ -101,16 +101,14 @@ public class BankHeaderChunk : Chunk
/// </summary>
[FieldOrder(5)]
[SerializeAs(SerializedType.UInt4)]
[SerializeWhen(nameof(BankGeneratorVersion), true,
ConverterType = typeof(BetweenConverter),
ConverterParameter = new[] {27, 126})]
[SerializeWhenVersionBetween(27, 126)]
public bool FeedbackInBank { get; set; }

/// <summary>
/// Bitpacked bools encoded in AltValues depending on version
/// </summary>
[FieldOrder(6)]
[SerializeWhen(nameof(BankGeneratorVersion), 126, ComparisonOperator.GreaterThan)]
[SerializeWhenVersion(126, ComparisonOperator.GreaterThan)]
public uint AltValues { get; set; }

//TODO: Implement bit packed bools in AltValues
Expand All @@ -128,15 +126,15 @@ public class BankHeaderChunk : Chunk
/// (&gt; v76)
/// </summary>
[FieldOrder(7)]
[SerializeWhen(nameof(BankGeneratorVersion), 76, ComparisonOperator.GreaterThan)]
[SerializeWhenVersion(76, ComparisonOperator.GreaterThan)]
public uint ProjectId { get; set; } = 0;

/// <summary>
/// Type of bank
/// (&gt; v141)
/// </summary>
[FieldOrder(8)]
[SerializeWhen(nameof(BankGeneratorVersion), 141, ComparisonOperator.GreaterThan)]
[SerializeWhenVersion(141, ComparisonOperator.GreaterThan)]
public AkBankType SoundBankType { get; set; }

/// <summary>
Expand All @@ -145,7 +143,7 @@ public class BankHeaderChunk : Chunk
/// </summary>
[FieldOrder(9)]
[FieldCount(16)]
[SerializeWhen(nameof(BankGeneratorVersion), 141, ComparisonOperator.GreaterThan)]
[SerializeWhenVersion(141, ComparisonOperator.GreaterThan)]
public sbyte[] BankHash { get; set; }

// TODO: Copy any padding over into a byte[]? Will allow proper reserialization.
Expand Down
13 changes: 4 additions & 9 deletions ME3Tweaks.Wwiser/Model/Hierarchy/Attenuation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using BinarySerialization;
using BinarySerialization;
using ME3Tweaks.Wwiser.Attributes;
using ME3Tweaks.Wwiser.Model.RTPC;

namespace ME3Tweaks.Wwiser.Model.Hierarchy;
Expand All @@ -11,8 +8,7 @@ public class Attenuation : HircItem
{
[FieldOrder(0)]
[SerializeAs(SerializedType.UInt1)]
[SerializeWhen(nameof(BankSerializationContext.Version), 136,
ComparisonOperator.GreaterThan, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(136, ComparisonOperator.GreaterThan)]
public bool IsHeightSpreadEnabled { get; set; }

[FieldOrder(1)]
Expand Down Expand Up @@ -87,7 +83,6 @@ public class ConeParams
public float LowPass { get; set; }

[FieldOrder(5)]
[SerializeWhen(nameof(BankSerializationContext.Version), 89,
ComparisonOperator.GreaterThan, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(89, ComparisonOperator.GreaterThan)]
public float HighPass { get; set; }
}
7 changes: 3 additions & 4 deletions ME3Tweaks.Wwiser/Model/Hierarchy/BankSourceData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BinarySerialization;
using ME3Tweaks.Wwiser.Attributes;
using ME3Tweaks.Wwiser.Converters;
using ME3Tweaks.Wwiser.Model.Hierarchy.Enums;
using ME3Tweaks.Wwiser.Model.Plugins;
Expand All @@ -14,8 +15,7 @@ public class BankSourceData
public StreamType StreamType { get; set; }

[FieldOrder(2)]
[SerializeWhen(nameof(BankSerializationContext.Version), 46,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(46, ComparisonOperator.LessThanOrEqual)]
public AudioFormat AudioFormat { get; set; }

[FieldOrder(3)]
Expand All @@ -30,8 +30,7 @@ public class BankSourceData
public class AudioFormat
{
[FieldOrder(0)]
[SerializeWhen(nameof(BankSerializationContext.Version), 26,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(26, ComparisonOperator.LessThanOrEqual)]
public uint DataIndex { get; set; }

[FieldOrder(1)]
Expand Down
22 changes: 6 additions & 16 deletions ME3Tweaks.Wwiser/Model/Hierarchy/FxBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using BinarySerialization;
using ME3Tweaks.Wwiser.Attributes;
using ME3Tweaks.Wwiser.Converters;
using ME3Tweaks.Wwiser.Formats;
using ME3Tweaks.Wwiser.Model.Plugins;
Expand Down Expand Up @@ -32,29 +33,20 @@ public class FxBase : HircItem
public RtpcCurves RtpcCurves { get; set; }

[FieldOrder(6)]
[SerializeWhen(nameof(BankSerializationContext.Version), true,
RelativeSourceMode = RelativeSourceMode.SerializationContext,
ConverterType = typeof(BetweenConverter),
ConverterParameter = new[] {123, 126})]
[SerializeWhenVersionBetween(123, 126)]
public ushort Unk1 { get; set; }

[FieldOrder(7)]
[SerializeWhen(nameof(BankSerializationContext.Version), 126,
ComparisonOperator.GreaterThan,
RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(126, ComparisonOperator.GreaterThan)]
public StateChunk_Aware StateChunk { get; set; }

[FieldOrder(8)]
[SerializeWhen(nameof(BankSerializationContext.Version), 90,
ComparisonOperator.GreaterThan,
RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(90, ComparisonOperator.GreaterThan)]
public ushort RtpcInitCount { get; set; }

[FieldOrder(9)]
[FieldCount(nameof(RtpcInitCount))]
[SerializeWhen(nameof(BankSerializationContext.Version), 90,
ComparisonOperator.GreaterThan,
RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(90, ComparisonOperator.GreaterThan)]
public List<RtpcInitValue> RtpcInitValues { get; set; }


Expand All @@ -64,9 +56,7 @@ public class RtpcInitValue
public VarCount ParameterId { get; set; }

[FieldOrder(1)]
[SerializeWhen(nameof(BankSerializationContext.Version), 126,
ComparisonOperator.GreaterThan,
RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(126, ComparisonOperator.GreaterThan)]
public byte RtpcAccum { get; set; }

[FieldOrder(2)]
Expand Down
10 changes: 4 additions & 6 deletions ME3Tweaks.Wwiser/Model/Hierarchy/Sound.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BinarySerialization;
using ME3Tweaks.Wwiser.Attributes;
using ME3Tweaks.Wwiser.Model.ParameterNode;

namespace ME3Tweaks.Wwiser.Model.Hierarchy;
Expand All @@ -12,17 +13,14 @@ public class Sound : HircItem
public NodeBaseParameters NodeBaseParameters { get; set; }

[FieldOrder(2)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.LessThanOrEqual)]
public short Loop { get; set; }

[FieldOrder(3)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.LessThanOrEqual)]
public short LoopModMin { get; set; }

[FieldOrder(4)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.LessThanOrEqual)]
public short LoopModMax { get; set; }
}
27 changes: 9 additions & 18 deletions ME3Tweaks.Wwiser/Model/ParameterNode/FxChunk.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BinarySerialization;
using ME3Tweaks.Wwiser.Attributes;
using ME3Tweaks.Wwiser.Converters;
using ME3Tweaks.Wwiser.Model.Plugins;

Expand All @@ -7,51 +8,41 @@ namespace ME3Tweaks.Wwiser.Model.ParameterNode;
public class FxChunk : IAkIdentifiable
{
[FieldOrder(0)]
[SerializeWhen(nameof(BankSerializationContext.Version), 26,
ComparisonOperator.GreaterThan, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(26, ComparisonOperator.GreaterThan)]
public byte FxIndex { get; set; }

[FieldOrder(1)]
public uint Id { get; set; }

[FieldOrder(2)]
[SerializeWhen(nameof(BankSerializationContext.Version), 26,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(26, ComparisonOperator.LessThanOrEqual)]
public byte BitsFxBypass { get; set; }

[FieldOrder(3)]
[SerializeAs(SerializedType.UInt1)]
[SerializeWhen(nameof(BankSerializationContext.Version), true,
RelativeSourceMode = RelativeSourceMode.SerializationContext,
ConverterType = typeof(BetweenConverter),
ConverterParameter = new[] {49, 145})]
[SerializeWhenVersionBetween(49, 145)]
public bool IsShareSet { get; set; }

[FieldOrder(4)]
[SerializeAs(SerializedType.UInt1)]
[SerializeWhen(nameof(BankSerializationContext.Version), 145,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(145, ComparisonOperator.LessThanOrEqual)]
public bool IsRendered { get; set; }

[FieldOrder(5)]
[SerializeWhen(nameof(BankSerializationContext.Version), 48,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(48, ComparisonOperator.LessThanOrEqual)]
public PluginParameters PluginParameters { get; set; }

//TODO: Custom serialized class - merge with IsShareSet, IsRendered, BitsFXBypass
[FieldOrder(6)]
[SerializeWhen(nameof(BankSerializationContext.Version), 145,
ComparisonOperator.GreaterThan, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(145, ComparisonOperator.GreaterThan)]
public byte BitVector { get; set; }

[FieldOrder(7)]
[SerializeWhen(nameof(BankSerializationContext.Version), 48,
RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(48)]
public uint NumBankData { get; set; }

[FieldOrder(8)]
[SerializeWhen(nameof(BankSerializationContext.Version), 48,
RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(48)]
public List<BankData> BankDatas { get; set; }

public class BankData
Expand Down
25 changes: 8 additions & 17 deletions ME3Tweaks.Wwiser/Model/ParameterNode/NodeBaseParameters.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BinarySerialization;
using ME3Tweaks.Wwiser.Converters;
using ME3Tweaks.Wwiser.Attributes;
using ME3Tweaks.Wwiser.Model.Hierarchy.Enums;

namespace ME3Tweaks.Wwiser.Model.ParameterNode;
Expand All @@ -11,10 +11,7 @@ public class NodeBaseParameters

[FieldOrder(1)]
[SerializeAs(SerializedType.UInt1)]
[SerializeWhen(nameof(BankSerializationContext.Version), true,
RelativeSourceMode = RelativeSourceMode.SerializationContext,
ConverterType = typeof(BetweenConverter),
ConverterParameter = new[] {90, 145})]
[SerializeWhenVersionBetween(90, 145)]
public bool OverrideAttachmentParams { get; set; }

[FieldOrder(2)]
Expand All @@ -24,40 +21,34 @@ public class NodeBaseParameters
public uint DirectParentId { get; set; }

[FieldOrder(4)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.LessThanOrEqual)]
public byte Priority { get; set; }

[FieldOrder(5)]
public PriorityOverrideFlags PriorityOverrideFlags { get; set; }

[FieldOrder(6)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.LessThanOrEqual)]
public sbyte DistOffset { get; set; }

// TODO: Convert between these two variants when changing version
[FieldOrder(7)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.LessThanOrEqual)]
public InitialParamsV56 InitialParams56 { get; set; }

[FieldOrder(8)]
[SerializeWhen(nameof(BankSerializationContext.Version), 56,
ComparisonOperator.GreaterThan, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(56, ComparisonOperator.GreaterThan)]
public InitialParamsV62 InitialParams62 { get; set; }

[FieldOrder(9)]
[SerializeWhen(nameof(BankSerializationContext.Version), 52,
ComparisonOperator.LessThanOrEqual, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(52, ComparisonOperator.LessThanOrEqual)]
public uint StateGroupId { get; set; }

[FieldOrder(10)]
public Positioning.PositioningChunk PositioningChunk { get; set; }

[FieldOrder(11)]
[SerializeWhen(nameof(BankSerializationContext.Version), 65,
ComparisonOperator.GreaterThan, RelativeSourceMode = RelativeSourceMode.SerializationContext)]
[SerializeWhenVersion(65, ComparisonOperator.GreaterThan)]
public AuxParams AuxParams { get; set; }

}
Loading

0 comments on commit 00be898

Please sign in to comment.