-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSoundObjectLoader.cs
More file actions
209 lines (171 loc) · 6.35 KB
/
SoundObjectLoader.cs
File metadata and controls
209 lines (171 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Dat.Data;
using Dat.FileParsing;
using Dat.Types;
using Dat.Types.Audio;
using Definitions.ObjectModels;
using Definitions.ObjectModels.Objects.Sound;
using Definitions.ObjectModels.Types;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
namespace Dat.Loaders;
public abstract class SoundObjectLoader : IDatObjectLoader
{
public static class Constants
{
public const int NumUnkStructs = 16;
}
public static class StructSizes
{
public const int Dat = 0x0C;
public const int SoundObjectData = 0x1E;
}
public static ObjectType ObjectType => ObjectType.Sound;
public static DatObjectType DatObjectType => DatObjectType.Sound;
public static LocoObject Load(Stream stream)
{
var initialStreamPosition = stream.Position;
using (var br = new LocoBinaryReader(stream))
{
var model = new SoundObject();
// fixed
br.SkipStringId(); // Name offset, not part of object definition
br.SkipPointer(); // SoundObjectDataPtr, not part of object definition
model.ShouldLoop = br.ReadByte();
br.SkipByte(); // pad_07, not part of object definition
model.Volume = br.ReadUInt32();
// sanity check
ArgumentOutOfRangeException.ThrowIfNotEqual(stream.Position, initialStreamPosition + ObjectAttributes.StructSize(DatObjectType), nameof(stream.Position));
// string table
var stringTable = SawyerStreamReader.ReadStringTableStream(stream, ObjectAttributes.StringTable(DatObjectType), null);
// variable
LoadVariable(br, model);
// image table
// N/A
return new LocoObject(ObjectType, model, stringTable);
}
}
private static void LoadVariable(LocoBinaryReader br, SoundObject model)
{
model.NumUnkStructs = br.ReadUInt32();
_ = br.ReadUInt32(); // unused
model.UnkData = br.ReadBytes((int)model.NumUnkStructs * Constants.NumUnkStructs);
model.SoundObjectData = new SoundObjectData
{
var_00 = br.ReadInt32(),
Offset = br.ReadInt32(),
Length = br.ReadUInt32(),
PcmHeader = br.ReadSoundEffect(),
};
model.PcmData = br.ReadToEnd();
}
public static void Save(Stream stream, LocoObject obj)
{
var initialStreamPosition = stream.Position;
var model = (SoundObject)obj.Object;
using (var bw = new LocoBinaryWriter(stream))
{
bw.WriteEmptyStringId(); // Name offset, not part of object definition
bw.WriteEmptyPointer();
bw.Write(model.ShouldLoop);
bw.WriteEmptyBytes(1); // padding
bw.Write(model.Volume);
// sanity check
ArgumentOutOfRangeException.ThrowIfNotEqual(stream.Position, initialStreamPosition + ObjectAttributes.StructSize(DatObjectType), nameof(stream.Position));
// string table
SawyerStreamWriter.WriteStringTable(stream, obj.StringTable);
// variable
SaveVariable(model, bw);
// image table
// N/A
}
}
private static void SaveVariable(SoundObject model, LocoBinaryWriter bw)
{
bw.Write(model.NumUnkStructs);
bw.Write((uint32_t)0); // unused pcm data length
bw.Write(model.UnkData);
var m = model.SoundObjectData;
bw.Write(m.var_00);
bw.Write(m.Offset);
bw.Write(m.Length);
bw.Write(m.PcmHeader.WaveFormatTag);
bw.Write(m.PcmHeader.Channels);
bw.Write(m.PcmHeader.SampleRate);
bw.Write(m.PcmHeader.AverageBytesPerSecond);
bw.Write(m.PcmHeader.BlockAlign);
bw.Write(m.PcmHeader.BitsPerSample);
bw.Write(m.PcmHeader.ExtraSize);
bw.Write(model.PcmData);
}
}
[LocoStructSize(0x1E)]
internal record DatSoundObjectData(
[property: LocoStructOffset(0x00)] int32_t var_00,
[property: LocoStructOffset(0x04)] int32_t Offset,
[property: LocoStructOffset(0x08)] uint32_t Length,
[property: LocoStructOffset(0x0C)] DatSoundEffectWaveFormat PcmHeader
) : ILocoStruct
{
public DatSoundObjectData() : this(0, 0, 0, new DatSoundEffectWaveFormat())
{ }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Length > 0 && Offset < 0)
{
yield return new ValidationResult("If Length is greater than 0, Offset must be non-negative.", [nameof(Offset), nameof(Length)]);
}
}
}
[LocoStructSize(0x0C)]
[LocoStructType(DatObjectType.Sound)]
internal record DatSoundObject(
[property: LocoStructOffset(0x00), LocoString, Browsable(false)] string_id Name,
[property: LocoStructOffset(0x02), Browsable(false)] uint32_t SoundObjectDataPtr,
[property: LocoStructOffset(0x06)] uint8_t ShouldLoop, // 0 means no loop, any other number means loop (usually 1)
[property: LocoStructOffset(0x07), Browsable(false)] uint8_t pad_07,
[property: LocoStructOffset(0x08)] uint32_t Volume
) : ILocoStruct, ILocoStructVariableData
{
[Editable(false)] public DatSoundObjectData SoundObjectData { get; set; }
[Browsable(false)] public byte[] PcmData { get; set; } = [];
uint numUnkStructs;
uint pcmDataLength;
byte[] unkData;
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "LoadVariable calls ReadLocoStruct which uses reflection. The required types (DatSoundObjectData) are always statically referenced and preserved.")]
public ReadOnlySpan<byte> LoadVariable(ReadOnlySpan<byte> remainingData)
{
// unknown structs
numUnkStructs = BitConverter.ToUInt32(remainingData[0..4]);
remainingData = remainingData[4..];
// pcm data length
pcmDataLength = BitConverter.ToUInt32(remainingData[0..4]); // unused
remainingData = remainingData[4..];
// unk
unkData = remainingData[..(int)(numUnkStructs * 16)].ToArray();
remainingData = remainingData[(int)(numUnkStructs * 16)..];
// pcm data
SoundObjectData = ByteReader.ReadLocoStruct<DatSoundObjectData>(remainingData[..ObjectAttributes.StructSize<DatSoundObjectData>()]);
remainingData = remainingData[ObjectAttributes.StructSize<DatSoundObjectData>()..];
PcmData = remainingData.ToArray();
return remainingData[remainingData.Length..];
}
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "SaveVariable calls WriteLocoStruct which uses reflection. The required types (DatSoundObjectData) are always statically referenced and preserved.")]
public ReadOnlySpan<byte> SaveVariable()
{
using (var ms = new MemoryStream())
using (var br = new BinaryWriter(ms))
{
br.Write(numUnkStructs);
br.Write(pcmDataLength);
br.Write(unkData);
br.Write(ByteWriter.WriteLocoStruct(SoundObjectData));
br.Write(PcmData);
br.Flush();
ms.Flush();
return ms.ToArray();
}
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
=> [];
}