@@ -30,7 +30,7 @@ public ModulatorRtpcParameterId? ModParamId
30
30
}
31
31
}
32
32
33
- public void Serialize ( Stream stream , Endianness endianness , BinarySerializationContext serializationContext )
33
+ public virtual void Serialize ( Stream stream , Endianness endianness , BinarySerializationContext serializationContext )
34
34
{
35
35
var context = serializationContext . FindAncestor < BankSerializationContext > ( ) ;
36
36
var value = GetSerializableValue ( context . Version , context . UseModulator ) ;
@@ -58,7 +58,7 @@ public void Serialize(Stream stream, Endianness endianness, BinarySerializationC
58
58
}
59
59
}
60
60
61
- public void Deserialize ( Stream stream , Endianness endianness , BinarySerializationContext serializationContext )
61
+ public virtual void Deserialize ( Stream stream , Endianness endianness , BinarySerializationContext serializationContext )
62
62
{
63
63
var context = serializationContext . FindAncestor < BankSerializationContext > ( ) ;
64
64
var res = DeserializeStatic ( stream , context . Version , context . UseModulator ) ;
@@ -78,16 +78,35 @@ public bool IsValidOnVersion(uint version)
78
78
}
79
79
}
80
80
81
- public static ( RtpcParameterId ? , ModulatorRtpcParameterId ? ) DeserializeStatic ( Stream stream , uint version , bool useModulator )
81
+ /// <summary>
82
+ /// Deserializes a parameter id from a stream based on the version and whether the bank uses modulator parameters
83
+ /// </summary>
84
+ /// <param name="stream">Stream to read from, length read may be different per version</param>
85
+ /// <param name="version">Version of WwiseBank</param>
86
+ /// <param name="useModulator">Whether the bank uses modulator parameters</param>
87
+ /// <param name="isState">True if parameter being read is from HIRC State object, which uses different serialization</param>
88
+ /// <returns>Rtpc or Modulator parameter ID, one will always be null</returns>
89
+ /// <exception cref="Exception">Unable to read data length</exception>
90
+ /// <exception cref="ArgumentException">No parameter ID mapping is known for given Wwise version</exception>
91
+ public static ( RtpcParameterId ? , ModulatorRtpcParameterId ? ) DeserializeStatic ( Stream stream , uint version , bool useModulator , bool isState = false )
82
92
{
83
93
byte value ;
84
94
switch ( version )
85
95
{
96
+ case >= 72 and <= 126 when isState :
97
+ value = ( byte ) stream . ReadByte ( ) ;
98
+ break ;
99
+ case > 126 when isState :
100
+ Span < byte > spanUshort = stackalloc byte [ 2 ] ;
101
+ var readUshort = stream . Read ( spanUshort ) ;
102
+ if ( readUshort != 2 ) throw new Exception ( ) ;
103
+ value = ( byte ) BitConverter . ToUInt16 ( spanUshort ) ;
104
+ break ;
86
105
case <= 89 :
87
- Span < byte > span = stackalloc byte [ 4 ] ;
88
- var read = stream . Read ( span ) ;
89
- if ( read != 4 ) throw new Exception ( ) ;
90
- value = ( byte ) BitConverter . ToUInt32 ( span ) ;
106
+ Span < byte > spanUint = stackalloc byte [ 4 ] ;
107
+ var readUint = stream . Read ( spanUint ) ;
108
+ if ( readUint != 4 ) throw new Exception ( ) ;
109
+ value = ( byte ) BitConverter . ToUInt32 ( spanUint ) ;
91
110
break ;
92
111
case <= 113 :
93
112
value = ( byte ) stream . ReadByte ( ) ;
@@ -111,8 +130,8 @@ public static (RtpcParameterId?, ModulatorRtpcParameterId?) DeserializeStatic(St
111
130
_ => throw new ArgumentException ( $ "No known parameter id mapping for version { version } ", nameof ( version ) )
112
131
} ;
113
132
}
114
-
115
- private byte GetSerializableValue ( uint version , bool useModulator )
133
+
134
+ protected byte GetSerializableValue ( uint version , bool useModulator )
116
135
{
117
136
var paramId = ParamId ?? 0 ;
118
137
return version switch
@@ -326,4 +345,33 @@ public enum ModulatorRtpcParameterId : byte
326
345
ModulatorTimeInitialDelay
327
346
}
328
347
// ReSharper restore InconsistentNaming
348
+ }
349
+
350
+ /// <summary>
351
+ /// Slightly different version of ParameterId used for State HIRC objects
352
+ /// </summary>
353
+ public class StateParameterId : ParameterId
354
+ {
355
+ public override void Serialize ( Stream stream , Endianness endianness , BinarySerializationContext serializationContext )
356
+ {
357
+ var context = serializationContext . FindAncestor < BankSerializationContext > ( ) ;
358
+ var value = GetSerializableValue ( context . Version , false ) ; // TODO: This variant should never use modulator, right?
359
+
360
+ switch ( context . Version )
361
+ {
362
+ case <= 126 :
363
+ stream . WriteByte ( value ) ;
364
+ break ;
365
+ default :
366
+ stream . Write ( BitConverter . GetBytes ( ( ushort ) value ) ) ;
367
+ break ;
368
+ }
369
+ }
370
+
371
+ public override void Deserialize ( Stream stream , Endianness endianness , BinarySerializationContext serializationContext )
372
+ {
373
+ var context = serializationContext . FindAncestor < BankSerializationContext > ( ) ;
374
+ var res = DeserializeStatic ( stream , context . Version , false , true ) ;
375
+ ( ParamId , ModParamId ) = res ;
376
+ }
329
377
}
0 commit comments