-
Notifications
You must be signed in to change notification settings - Fork 3
/
CKDictionary.cpp
287 lines (268 loc) · 8.61 KB
/
CKDictionary.cpp
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
#include "CKDictionary.h"
#include "rw.h"
#include "KEnvironment.h"
#include <array>
void CAnimationDictionary::deserialize(KEnvironment * kenv, File * file, size_t length)
{
numAnims= file->readUint32();
numSets = 1;
if (kenv->version == KEnvironment::KVERSION_XXL1 && kenv->isRemaster)
numSets = 2;
else if (kenv->version >= KEnvironment::KVERSION_ARTHUR)
numSets = file->readUint32();
// TODO: Careful! Romaster has 0_Retro 0_Romaster 1_Retro 1_Romaster 2_Retro 2_Romaster ...
// whereas Ar+ has 0_Set0 1_Set0 2_Set0 0_Set1 1_Set1 2_Set1 ...
// might need to do some reordering
animIndices.resize(numAnims * numSets);
file->read(animIndices.data(), animIndices.size() * 4);
if (kenv->version >= KEnvironment::KVERSION_ARTHUR) {
arSector = file->readInt32();
arUnk = file->readInt8();
}
}
void CAnimationDictionary::serialize(KEnvironment * kenv, File * file)
{
assert(animIndices.size() == numAnims * numSets);
file->writeUint32(numAnims);
if (kenv->version >= KEnvironment::KVERSION_ARTHUR)
file->writeUint32(numSets);
file->write(animIndices.data(), animIndices.size() * 4);
if (kenv->version >= KEnvironment::KVERSION_ARTHUR) {
file->writeInt32(arSector);
file->writeInt8(arUnk);
}
}
void CTextureDictionary::deserialize(KEnvironment * kenv, File * file, size_t length)
{
// Console (except PS3)
if (kenv->platform != KEnvironment::PLATFORM_PC && kenv->platform != KEnvironment::PLATFORM_PS3) {
if (kenv->version >= KEnvironment::KVERSION_ARTHUR) {
uint8_t isRwDict = file->readUint8();
assert(isRwDict == 1);
}
RwNTTexDict nativeDict;
rwCheckHeader(file, 0x16);
nativeDict.deserialize(file);
piDict = nativeDict.convertToPI();
return;
}
// XXL Remaster
if (kenv->version <= kenv->KVERSION_XXL1 && kenv->isRemaster) {
RwPITexDict pitd;
rwCheckHeader(file, 0x23);
piDict.deserialize(file);
return;
}
// PC & PS3
if (kenv->version >= KEnvironment::KVERSION_ARTHUR) {
uint8_t isRwDict = file->readUint8();
assert(isRwDict == 0);
}
uint32_t numTex = file->readUint32();
piDict.textures.resize(numTex);
for (auto &pit : piDict.textures) {
pit.texture.name = file->readString(32).c_str();
pit.texture.filtering = (uint8_t)file->readUint32();
pit.texture.uAddr = (uint8_t)file->readUint32();
pit.texture.vAddr = (uint8_t)file->readUint32();
pit.texture.usesMips = true;
if (kenv->platform == KEnvironment::PLATFORM_PC) {
pit.images.emplace_back();
rwCheckHeader(file, 0x18);
pit.images[0].deserialize(file);
}
else if (kenv->platform == KEnvironment::PLATFORM_PS3) {
uint8_t header[128];
file->read(header, sizeof(header));
int ddsSize = RwImage::computeDDSSize(header);
std::shared_ptr<RwRaster> raster = std::make_shared<RwRaster>();
raster->data.resize(ddsSize);
memcpy(raster->data.data(), header, sizeof(header));
file->read(raster->data.data() + sizeof(header), ddsSize - sizeof(header));
pit.images = RwImage::loadDDS(raster->data.data());
pit.nativeVersion = std::move(raster);
}
}
}
void CTextureDictionary::serialize(KEnvironment * kenv, File * file)
{
// Console (except PS3)
if (kenv->platform != KEnvironment::PLATFORM_PC && kenv->platform != KEnvironment::PLATFORM_PS3) {
if (kenv->version >= KEnvironment::KVERSION_ARTHUR) {
file->writeUint8(1);
}
assert(piDict.nativeVersionPlatform != 0);
RwNTTexDict nativeDict = RwNTTexDict::createFromPI(piDict);
nativeDict.serialize(file);
return;
}
// XXL1 Remaster
if (kenv->version <= kenv->KVERSION_XXL1 && kenv->platform == kenv->PLATFORM_PC && kenv->isRemaster) { // for Romaster
piDict.serialize(file);
return;
}
// PC & PS3
if (kenv->version >= KEnvironment::KVERSION_ARTHUR) {
file->writeUint8(0);
}
file->writeUint32(piDict.textures.size());
for (auto &pit : piDict.textures) {
char buf[32] = { 0 };
std::copy_n(pit.texture.name.data(), std::min((size_t)32, pit.texture.name.size()), std::begin(buf));
file->write(buf, 32);
file->writeUint32(pit.texture.filtering);
file->writeUint32(pit.texture.uAddr);
file->writeUint32(pit.texture.vAddr);
if (kenv->platform == KEnvironment::PLATFORM_PC)
pit.images[0].serialize(file);
else if (kenv->platform == KEnvironment::PLATFORM_PS3)
file->write(pit.nativeVersion->data.data(), pit.nativeVersion->data.size());
}
}
void CKSoundDictionaryID::deserialize(KEnvironment * kenv, File * file, size_t length)
{
if (kenv->version == KEnvironment::KVERSION_XXL1) {
soundEntries.resize(file->readUint32());
for (SoundEntry& ent : soundEntries) {
ent.active = file->readUint8() == 1;
if (ent.active) {
ent.id = file->readUint32();
ent.flags = file->readUint32();
if (ent.flags & 0x10) {
ent.obj = kenv->readObjRef<CKObject>(file);
}
else {
for (float& u : ent.refalt)
u = file->readFloat();
}
ent.volume = file->readFloat();
ent.speed = file->readFloat();
ent.replayAfterMin = file->readFloat();
ent.replayAfterMax = file->readFloat();
for (float& f : ent.boxHigh)
f = file->readFloat();
for (float& f : ent.boxLow)
f = file->readFloat();
ent.playInLoop = file->readUint8();
}
}
}
else if (kenv->version >= KEnvironment::KVERSION_XXL2) {
x2Sounds.resize(file->readUint32());
for (auto& ref : x2Sounds)
ref.read(file);
x2Sector = file->readUint32();
}
}
void CKSoundDictionaryID::serialize(KEnvironment * kenv, File * file)
{
if (kenv->version == KEnvironment::KVERSION_XXL1) {
file->writeUint32((uint32_t)soundEntries.size());
for (SoundEntry& ent : soundEntries) {
file->writeUint8(ent.active ? 1 : 0);
if (ent.active) {
file->writeUint32(ent.id);
file->writeUint32(ent.flags);
if (ent.flags & 0x10) {
kenv->writeObjRef(file, ent.obj);
}
else {
for (float& u : ent.refalt)
file->writeFloat(u);
}
file->writeFloat(ent.volume);
file->writeFloat(ent.speed);
file->writeFloat(ent.replayAfterMin);
file->writeFloat(ent.replayAfterMax);
for (float& f : ent.boxHigh)
file->writeFloat(f);
for (float& f : ent.boxLow)
file->writeFloat(f);
file->writeUint8(ent.playInLoop);
}
}
}
else if (kenv->version >= KEnvironment::KVERSION_XXL2) {
file->writeUint32((uint32_t)x2Sounds.size());
for (auto& ref : x2Sounds)
ref.write(kenv, file);
file->writeUint32(x2Sector);
}
}
void CKSoundDictionaryID::onLevelLoaded(KEnvironment* kenv)
{
if (kenv->version >= KEnvironment::KVERSION_XXL2) {
for (auto& ref : x2Sounds)
ref.bind(kenv, (int)x2Sector - 1);
}
}
void CKSoundDictionary::deserialize(KEnvironment * kenv, File * file, size_t length)
{
inactive = file->readUint8();
uint32_t numSounds = file->readUint32();
sounds.resize(numSounds);
if (numSounds > 0) {
for (Sound &snd : sounds) {
if (kenv->version <= kenv->KVERSION_XXL1) {
snd.id1 = file->readUint32();
snd.unk2 = file->readFloat();
snd.unk3 = file->readFloat();
snd.unk4 = file->readUint8();
if (kenv->isRemaster)
snd.hdPath = file->readSizedString<uint16_t>();
snd.unk5 = file->readFloat();
snd.sampleRate = file->readUint16();
snd.unk7 = file->readUint32();
snd.unk8 = file->readUint8();
snd.unk9 = file->readUint8();
snd.unkA = file->readUint8();
snd.id2 = file->readUint32();
}
else if (kenv->version == kenv->KVERSION_XXL2) {
snd.unk5 = file->readFloat(); // could have been unk2 or unk3 ?
snd.sampleRate = file->readUint16();
if (kenv->isRemaster)
snd.hdPath = file->readSizedString<uint16_t>();
}
else if (kenv->version >= kenv->KVERSION_ARTHUR) {
snd.waveObj = kenv->readObjRef<CKObject>(file);
}
}
rwCheckHeader(file, RwSoundDictionary::tagID);
rwSoundDict.deserialize(file);
}
}
void CKSoundDictionary::serialize(KEnvironment * kenv, File * file)
{
file->writeUint8(inactive);
file->writeUint32(sounds.size());
if (sounds.size() > 0) {
for (Sound &snd : sounds) {
if (kenv->version <= kenv->KVERSION_XXL1) {
file->writeUint32(snd.id1);
file->writeFloat(snd.unk2);
file->writeFloat(snd.unk3);
file->writeUint8(snd.unk4);
if (kenv->isRemaster)
file->writeSizedString<uint16_t>(snd.hdPath);
file->writeFloat(snd.unk5);
file->writeUint16(snd.sampleRate);
file->writeUint32(snd.unk7);
file->writeUint8(snd.unk8);
file->writeUint8(snd.unk9);
file->writeUint8(snd.unkA);
file->writeUint32(snd.id2);
}
else if (kenv->version == kenv->KVERSION_XXL2) {
file->writeFloat(snd.unk5); // could have been unk2 or unk3 ?
file->writeUint16(snd.sampleRate);
if (kenv->isRemaster)
file->writeSizedString<uint16_t>(snd.hdPath);
}
else if (kenv->version >= kenv->KVERSION_ARTHUR) {
kenv->writeObjRef(file, snd.waveObj);
}
}
rwSoundDict.serialize(file);
}
}