forked from Gota7/SequenceConvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructures.cs
295 lines (263 loc) · 9.14 KB
/
Structures.cs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using GotaSoundIO.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SequenceConvert {
/// <summary>
/// NDS header.
/// </summary>
public class NHeader : FileHeader {
/// <summary>
/// Read the header.
/// </summary>
/// <param name="r">The reader.</param>
public override void Read(FileReader r) {
Magic = new string(r.ReadChars(4));
r.ByteOrder = ByteOrder.BigEndian;
r.ByteOrder = ByteOrder = r.ReadUInt16() == 0xFEFF ? ByteOrder.BigEndian : ByteOrder.LittleEndian;
r.ReadUInt16(); //Version is always constant.
FileSize = r.ReadUInt32();
HeaderSize = r.ReadUInt16();
r.ReadUInt16();
BlockOffsets = new long[] { 0x10 };
}
/// <summary>
/// Write the header.
/// </summary>
/// <param name="w">The writer.</param>
public override void Write(FileWriter w) {
HeaderSize = 0x10;
w.ByteOrder = ByteOrder.LittleEndian;
w.Write(Magic.ToCharArray());
w.Write((ushort)0xFEFF);
w.Write((ushort)0x0100);
w.Write((uint)FileSize);
w.Write((ushort)HeaderSize);
w.Write((ushort)1);
}
}
/// <summary>
/// A version.
/// </summary>
public class RVersion : GotaSoundIO.IO.Version {
/// <summary>
/// Create a version from a ushort.
/// </summary>
/// <param name="s">The ushort.</param>
public void FromUShort(ushort s) {
Major = (byte)((s & 0xFF00) >> 8);
Minor = (byte)(s & 0xFF);
}
/// <summary>
/// Convert the version to a ushort.
/// </summary>
/// <returns>The version as a ushort.</returns>
public ushort ToUShort() {
return (ushort)((Major << 8) | Minor);
}
/// <summary>
/// Read the version.
/// </summary>
/// <param name="r">The reader.</param>
public override void Read(FileReader r) {
FromUShort(r.ReadUInt16());
}
/// <summary>
/// Write the version.
/// </summary>
/// <param name="w">The writer.</param>
public override void Write(FileWriter w) {
w.Write(ToUShort());
}
}
/// <summary>
/// A revolution file header.
/// </summary>
public class RFileHeader : FileHeader {
/// <summary>
/// Read the header.
/// </summary>
/// <param name="r">The reader.</param>
public override void Read(FileReader r) {
Magic = new string(r.ReadChars(4));
ByteOrder = r.ByteOrder = ByteOrder.BigEndian;
if (r.ReadUInt16() == 0xFFFE) {
ByteOrder = ByteOrder.LittleEndian;
}
Version = r.Read<RVersion>();
FileSize = r.ReadUInt32();
HeaderSize = r.ReadUInt16();
ushort numBlocks = r.ReadUInt16();
BlockOffsets = new long[numBlocks];
BlockSizes = new long[numBlocks];
for (int i = 0; i < numBlocks; i++) {
BlockOffsets[i] = r.ReadUInt32();
BlockSizes[i] = r.ReadUInt32();
}
}
/// <summary>
/// Write the header.
/// </summary>
/// <param name="w">The writer.</param>
public override void Write(FileWriter w) {
w.Write(Magic.ToCharArray());
w.ByteOrder = ByteOrder;
w.Write((ushort)0xFEFF);
w.Write(Version);
w.Write((uint)FileSize);
HeaderSize = 0x10 + 8 * BlockOffsets.Length;
while (HeaderSize % 0x20 != 0) {
HeaderSize++;
}
w.Write((ushort)HeaderSize);
w.Write((ushort)BlockOffsets.Length);
for (int i = 0; i < BlockOffsets.Length; i++) {
w.Write((uint)BlockOffsets[i]);
w.Write((uint)BlockSizes[i]);
}
w.Align(0x20);
}
}
/// <summary>
/// 3ds, Wii U, and Switch.
/// </summary>
public class XVersion : GotaSoundIO.IO.Version {
/// <summary>
/// If the version is an F one and not a C one.
/// </summary>
public bool FType = true;
/// <summary>
/// Read the version.
/// </summary>
/// <param name="r">The reader.</param>
public override void Read(FileReader r) {
uint version = r.ReadUInt32();
if (FType) {
Major = (byte)((version & 0x00FF0000) >> 16);
Minor = (byte)((version & 0x0000FF00) >> 8);
Revision = (byte)(version & 0x000000FF);
} else {
Major = (byte)((version & 0xFF000000) >> 24);
Minor = (byte)((version & 0x00FF0000) >> 16);
Revision = (byte)((version & 0x0000FF00) >> 8);
}
}
/// <summary>
/// Write the version.
/// </summary>
/// <param name="w">The writer.</param>
public override void Write(FileWriter w) {
uint ver = 0;
if (FType) {
ver |= (uint)(Major << 16);
ver |= (uint)(Minor << 8);
ver |= (uint)(Revision << 0);
} else {
ver |= (uint)(Major << 24);
ver |= (uint)(Minor << 16);
ver |= (uint)(Revision << 8);
}
w.Write(ver);
}
}
/// <summary>
/// 3ds, Wii U, and Switch header.
/// </summary>
public class XFileHeader : FileHeader {
/// <summary>
/// F type.
/// </summary>
public bool FType => Magic.StartsWith("F");
/// <summary>
/// Read the header.
/// </summary>
/// <param name="r">The reader.</param>
public override void Read(FileReader r) {
Magic = new string(r.ReadChars(4));
r.ByteOrder = ByteOrder = ByteOrder.BigEndian;
if (r.ReadUInt16() == 0xFFFE) {
ByteOrder = r.ByteOrder = ByteOrder.LittleEndian;
}
HeaderSize = r.ReadUInt16();
Version = new XVersion();
(Version as XVersion).FType = FType;
Version.Read(r);
FileSize = r.ReadUInt32();
ushort numBlocks = r.ReadUInt16();
r.ReadUInt16();
BlockOffsets = new long[numBlocks];
BlockSizes = new long[numBlocks];
BlockTypes = new long[numBlocks];
for (int i = 0; i < numBlocks; i++) {
BlockTypes[i] = r.ReadUInt16();
r.ReadUInt16();
BlockOffsets[i] = r.ReadUInt32();
BlockSizes[i] = r.ReadUInt32();
}
}
/// <summary>
/// Write the header.
/// </summary>
/// <param name="w">The writer.</param>
public override void Write(FileWriter w) {
w.Write(Magic.ToCharArray());
w.ByteOrder = ByteOrder;
w.Write((ushort)0xFEFF);
w.Write((ushort)HeaderSize);
(Version as XVersion).FType = FType;
w.Write(Version);
w.Write((uint)FileSize);
w.Write((ushort)BlockOffsets.Length);
w.Write((ushort)0);
for (int i = 0; i < BlockOffsets.Length; i++) {
w.Write((ushort)BlockTypes[i]);
w.Write((ushort)0);
w.Write((uint)BlockOffsets[i]);
w.Write((uint)BlockSizes[i]);
}
w.Align(0x20);
}
}
/// <summary>
/// Reference for 3ds, Wii U, and Switch.
/// </summary>
/// <typeparam name="T">Reference type.</typeparam>
public class XReference<T> : Reference<T> {
/// <summary>
/// Null reference is 0.
/// </summary>
/// <returns>If the null reference is 0.</returns>
public override bool NullReferenceIs0() => false;
/// <summary>
/// If to set the current offset when jumping.
/// </summary>
/// <returns>Set current offset when jumping.</returns>
public override bool SetCurrentOffsetOnJump() => true;
/// <summary>
/// Read the reference.
/// </summary>
/// <param name="r">The reader.</param>
public override void ReadRef(FileReader r) {
Identifier = r.ReadUInt16();
r.ReadUInt16();
Offset = r.ReadInt32();
}
/// <summary>
/// Write the reference.
/// </summary>
/// <param name="w">The writer.</param>
/// <param name="ignoreNullData">If to ignore raw data.</param>
public override void WriteRef(FileWriter w, bool ignoreNullData = false) {
if (!ignoreNullData && ((NullReferenceIs0() && Offset == 0) || (!NullReferenceIs0() && Offset == -1))) {
w.Write((uint)0);
w.Write(-1);
} else {
w.Write((ushort)Identifier);
w.Write((ushort)0);
w.Write((int)Offset);
}
}
}
}