-
Notifications
You must be signed in to change notification settings - Fork 3
/
rw.h
363 lines (300 loc) · 8.32 KB
/
rw.h
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#pragma once
#include <vector>
#include <array>
#include <memory>
#include <span>
#include <variant>
#include "vecmat.h"
#include "File.h"
#include "DynArray.h"
struct RwsHeader {
uint32_t type, length, rwVersion;
RwsHeader() {}
RwsHeader(uint32_t type, uint32_t length, uint32_t rwVersion) : type(type), length(length), rwVersion(rwVersion) {}
};
struct HeaderWriter {
uint32_t headpos;
static uint32_t rwver;
void begin(File *file, int type) {
headpos = file->tell();
file->writeUint32(type);
file->writeUint32(0);
file->writeUint32(rwver);
}
void end(File *file) {
uint32_t endpos = file->tell();
file->seek(headpos+4, SEEK_SET);
file->writeUint32(endpos - headpos - 12);
file->seek(endpos, SEEK_SET);
}
};
RwsHeader rwReadHeader(File *file);
RwsHeader rwFindHeader(File *file, uint32_t type);
uint32_t rwCheckHeader(File *file, uint32_t type);
void rwCheckAndSkipHeader(File *file, uint32_t type);
void rwWriteString(File *file, const std::string &str);
struct RwExtension;
struct RwsExtHolder {
std::vector<std::unique_ptr<RwExtension>> exts;
void read(File *file, void *parent, uint32_t parentType = 0);
void write(File *file);
RwExtension *find(uint32_t type);
const RwExtension *find(uint32_t type) const;
~RwsExtHolder();
RwsExtHolder();
RwsExtHolder(const RwsExtHolder &orig);
RwsExtHolder(RwsExtHolder&& old) noexcept;
RwsExtHolder& operator=(const RwsExtHolder &orig);
RwsExtHolder& operator=(RwsExtHolder&& old) noexcept;
};
struct RwFrame {
float matrix[4][3];
uint32_t index, flags;
void deserialize(File *file);
void serialize(File *file);
};
struct RwFrameList {
std::vector<RwFrame> frames;
std::vector<RwsExtHolder> extensions;
void deserialize(File *file);
void serialize(File *file);
};
struct RwTexture {
uint8_t filtering, uAddr, vAddr;
bool usesMips;
std::string name, alphaName;
RwsExtHolder extensions;
void deserialize(File *file);
void serialize(File *file);
};
struct RwMaterial {
uint32_t flags;
uint32_t color;
uint32_t unused, isTextured;
float ambient, specular, diffuse;
RwTexture texture;
RwsExtHolder extensions;
void deserialize(File *file);
void serialize(File *file);
};
struct RwMaterialList {
std::vector<uint32_t> slots;
std::vector<RwMaterial> materials;
void deserialize(File *file);
void serialize(File *file);
};
struct RwGeometry {
struct Triangle {
std::array<uint16_t, 3> indices;
uint16_t materialId;
};
enum {
RWGEOFLAG_TRISTRIP = 0x01,
RWGEOFLAG_POSITIONS = 0x02,
RWGEOFLAG_TEXTURED = 0x04,
RWGEOFLAG_PRELIT = 0x08,
RWGEOFLAG_NORMALS = 0x10,
RWGEOFLAG_LIGHT = 0x20,
RWGEOFLAG_MODULATECOLOR = 0x40,
RWGEOFLAG_TEXTURED2 = 0x80,
RWGEOFLAG_NATIVE = 0x01000000,
};
uint32_t flags;
uint32_t numTris, numVerts, numMorphs;
std::vector<uint32_t> colors;
std::vector<std::vector<std::array<float, 2>>> texSets;
std::vector<Triangle> tris;
Vector3 spherePos; float sphereRadius;
uint32_t hasVertices, hasNormals;
std::vector<Vector3> verts;
std::vector<Vector3> norms;
//uint32_t numMaterials;
//std::vector<uint32_t> matIndices;
RwMaterialList materialList;
RwsExtHolder extensions;
// used on consoles for 1:1 back serialization
std::shared_ptr<RwGeometry> nativeVersion;
void deserialize(File *file);
void serialize(File *file);
void merge(const RwGeometry &other);
std::vector<std::unique_ptr<RwGeometry>> splitByMaterial();
RwGeometry convertToPI();
RwGeometry convertToPI_GCN();
RwGeometry convertToPI_X360();
};
struct RwAtomic {
uint32_t frameIndex, geoIndex, flags, unused;
std::shared_ptr<RwGeometry> geometry;
RwsExtHolder extensions;
void deserialize(File *file, bool hasGeo = true);
void serialize(File *file);
};
struct RwMiniClump {
RwFrameList frameList;
RwAtomic atomic;
void deserialize(File *file);
void serialize(File *file);
};
struct RwGeometryList {
std::vector<std::shared_ptr<RwGeometry>> geometries;
void deserialize(File *file);
void serialize(File *file);
};
struct RwClump {
RwFrameList frameList;
RwGeometryList geoList;
std::vector<RwAtomic> atomics;
RwsExtHolder extensions;
void deserialize(File *file);
void serialize(File *file);
};
template<int N> struct FixedBuffer {
uint8_t buf[N];
void deserialize(File *file) { file->read(buf, N); }
void serialize(File *file) { file->write(buf, N); }
};
struct RwTeamDictionary {
struct Bing {
uint32_t _ding = 1;
uint32_t _someNum;
RwMiniClump _clump;
};
uint32_t _unk1;
std::vector<Bing> _bings;
void deserialize(File *file);
void serialize(File *file);
};
struct RwTeam {
struct Dong {
FixedBuffer<16> head3;
FixedBuffer<12> head4;
std::vector<uint32_t> bongs;
RwClump clump;
};
uint32_t numBongs;
FixedBuffer<8> head2;
std::vector<Dong> dongs;
FixedBuffer<20> end;
void deserialize(File *file);
void serialize(File *file);
};
struct RwImage {
uint32_t width, height, bpp, pitch;
DynArray<uint8_t> pixels;
DynArray<uint32_t> palette;
enum Format {
// bpp <= 8 -> 8-bit palettized with 2^bpp palette colors
ImageFormat_P8 = 8,
ImageFormat_RGBA8888 = 32,
ImageFormat_DXT1 = 0x101,
ImageFormat_DXT2 = 0x102,
ImageFormat_DXT4 = 0x103,
};
void deserialize(File *file);
void serialize(File *file);
RwImage convertToRGBA32() const;
static RwImage loadFromFile(const char* filename);
static RwImage loadFromFile(const wchar_t* filename);
static RwImage loadFromFile(FILE* file);
static RwImage loadFromMemory(void *ptr, size_t len);
static std::vector<RwImage> loadDDS(const void* ddsData);
static int computeDDSSize(const void* ddsData);
};
struct RwRaster;
struct RwPITexDict {
uint16_t flags = 1;
struct PITexture {
//uint32_t numMipmaps = 1;
std::vector<RwImage> images;
RwTexture texture;
std::shared_ptr<const RwRaster> nativeVersion; // used for console 1:1 back serialization
};
std::vector<PITexture> textures;
uint16_t nativeVersionPlatform = 0; // platform of native versions
void deserialize(File *file);
void serialize(File *file);
size_t findTexture(const std::string& name) const;
};
struct RwFont2D {
struct Glyph {
std::array<float, 4> coords;
float glUnk1;
uint8_t texIndex;
};
uint32_t flags;
uint32_t fntUnk1;
float glyphHeight;
float fntUnk3;
uint32_t fntUnk4;
uint32_t fntUnk5;
uint32_t firstWideChar;
//uint32_t numGlyphs;
//uint32_t numWideChars;
std::vector<uint16_t> wideGlyphTable;
std::array<uint16_t, 128> charGlyphTable;
std::vector<Glyph> glyphs;
std::vector<std::string> texNames;
void deserialize(File *file);
void serialize(File *file);
uint16_t* createGlyphSlot(uint16_t charId);
};
struct RwBrush2D {
uint32_t rbUnk1 = 1;
std::array<float, 24> rbFloats = { 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 255, 0, 1, 0, 0, 0, 0, 1, 0 };
uint32_t rbUnk2 = 2;
float rbUnk3 = 0.5f;
uint32_t rbUnk4 = 0;
void deserialize(File *file);
void serialize(File *file);
};
struct RwAnimAnimation {
struct HAnimKeyFrame {
float time;
float quaternion[4];
Vector3 translation;
int32_t prevFrame;
};
struct CompressedKeyFrame {
float time;
int16_t quaternion[4];
int16_t translation[3];
int32_t prevFrame;
};
int32_t version = 0x100;
int32_t schemeId = 1;
//int32_t numFrames = 0;
uint32_t flags = 0;
uint32_t extra = 0;
float duration = 0.0f;
std::variant<std::vector<HAnimKeyFrame>, std::vector<CompressedKeyFrame>> frames;
Vector3 compressedTranslationOffset, compressedTranslationScale;
void deserialize(File* file);
void serialize(File* file);
size_t numFrames() const;
HAnimKeyFrame decompressFrame(int frameIndex) const;
float frameTime(int frameIndex) const;
std::span<Matrix> interpolateNodeTransforms(int numNodes, float time) const;
int guessNumNodes() const;
static constexpr float decompressFloat(uint16_t compressedValue);
};
struct RwRaster {
std::vector<uint8_t> data;
std::vector<uint8_t> furtherSegs; // PS2 rasters contain more than one "type 1" chunk
//RwsExtHolder extensions;
void deserialize(File* file, uint32_t len);
void serialize(File* file);
RwPITexDict::PITexture convertToPI() const;
RwPITexDict::PITexture convertToPI_GCN() const;
RwPITexDict::PITexture convertToPI_X360() const;
static RwRaster createFromPI(const RwPITexDict::PITexture& pit);
};
struct RwNTTexDict {
//uint16_t numTextures;
uint16_t platform;
std::vector<RwRaster> textures;
RwsExtHolder extensions;
void deserialize(File* file);
void serialize(File* file);
RwPITexDict convertToPI();
static RwNTTexDict createFromPI(const RwPITexDict& pi);
};