forked from czajowaty/ad_resources_dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdMemoryHandler.cpp
455 lines (427 loc) · 16.1 KB
/
AdMemoryHandler.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "AdMemoryHandler.hpp"
#include "AdResourcesIterator.hpp"
#include "AdResourceUnpacker.hpp"
#include <QPainter>
const QPoint AdMemoryHandler::PORTRAIT_POSITION(0x54, 0x8d);
constexpr SpeakerInfo AdMemoryHandler::INVALID_SPEAKER_INFO;
AdMemoryHandler::AdMemoryHandler()
: ram_{std::make_unique<VirtualPsxRam>()},
vram_{std::make_unique<VirtualPsxVRam>()}
{}
void AdMemoryHandler::loadCdImage(QString const& cdImagePath)
{
adCdImageReader_ = BinCdImageReader::create(cdImagePath);
ram_->clear();
vram_->clear();
loadSlusTextSection();
loadTownResources();
}
void AdMemoryHandler::loadSlusTextSection()
{
static constexpr uint32_t SLUS_TEXT_SECTION_START_SECTOR = 25;
static constexpr uint32_t SLUS_TEXT_SECTION_SIZE = 0x54800;
static constexpr PsxRamAddress::Raw SLUS_TEXT_SECTION_LOAD_ADDRESS =
0x8002d000;
ram_->load(
adCdImageReader_->readSectors(
SLUS_TEXT_SECTION_START_SECTOR,
BinCdImageReader::calculateSectorsNumber(
SLUS_TEXT_SECTION_SIZE)),
SLUS_TEXT_SECTION_LOAD_ADDRESS);
}
void AdMemoryHandler::loadTownResources()
{
loadGameModeResources(GameMode::InTown);
loadTownVRamResources();
}
void AdMemoryHandler::loadGameModeResources(GameMode gameMode)
{
auto gameModeData = readGameModeData(gameMode);
if (gameModeData.memoryLoadInfoAddress.isNull())
{ return; }
auto sectorsNumber = readGameModeDataSectorsNumber(gameMode);
MemoryLoadInfo memoryLoadInfo;
ram_->readRegion(
{ gameModeData.memoryLoadInfoAddress, sizeof(MemoryLoadInfo)},
reinterpret_cast<uint8_t*>(&memoryLoadInfo));
PsxRamAddress memoryLoadInfoAddress = memoryLoadInfo.address;
if (memoryLoadInfoAddress.isNull())
{ throw QString("Address where to load resources is null."); }
memoryLoadInfoAddress |= PsxRamConst::KSEG0_ADDRESS;
sectorsNumber &= ~0x1ff;
sectorsNumber |= memoryLoadInfo.sectorsNumber;
ram_->load(
adCdImageReader_->readSectors(
memoryLoadInfo.sector,
sectorsNumber),
memoryLoadInfoAddress);
}
GameModeData AdMemoryHandler::readGameModeData(GameMode gameMode) const
{
PsxRamAddress gameModeDataAddress(0x8006ce44);
gameModeDataAddress += gameModeToIndex(gameMode) * sizeof(GameModeData);
GameModeData gameModeData;
ram_->readRegion(
{ gameModeDataAddress, sizeof(GameModeData) },
reinterpret_cast<uint8_t*>(&gameModeData));
return gameModeData;
}
uint32_t AdMemoryHandler::readGameModeDataSectorsNumber(GameMode gameMode)
{
PsxRamAddress gameModeDataSectorsNumberAddress = 0x8006ce6c;
gameModeDataSectorsNumberAddress +=
gameModeToIndex(gameMode) * sizeof(uint32_t);
return ram_->readDWord(gameModeDataSectorsNumberAddress);
}
void AdMemoryHandler::loadTownVRamResources()
{
MemoryLoadInfo townResourcesMemoryLoadInfo;
ram_->readRegion(
{0x80080ea0, sizeof(townResourcesMemoryLoadInfo)},
reinterpret_cast<uint8_t*>(&townResourcesMemoryLoadInfo));
auto townResourcesData = adCdImageReader_->readSectors(
townResourcesMemoryLoadInfo.sector,
townResourcesMemoryLoadInfo.sectorsNumber);
AdResourcesIterator resourcesIterator(townResourcesData);
while (resourcesIterator.hasNext())
{
auto nextResourceDescriptor = resourcesIterator.next();
auto const* resourceHeader = nextResourceDescriptor.header;
if (resourceHeader->type == ResourceType::VRamLoadablePackedImage)
{
loadVRamLoadablePackedImage(
reinterpret_cast<ResourceType1Header const*>(
resourceHeader),
nextResourceDescriptor.data,
nextResourceDescriptor.size);
}
else if (resourceHeader->type == ResourceType::Palette)
{
loadVRamPalette(
reinterpret_cast<ResourceType2Header const*>(
resourceHeader),
nextResourceDescriptor.data);
}
else
{
throw QString("Unexpected town resource type %1.")
.arg(static_cast<uint16_t>(resourceHeader->type));
}
}
}
void AdMemoryHandler::loadVRamLoadablePackedImage(
ResourceType1Header const* resourceHeader,
uint8_t const* resourceData,
uint32_t maxSize)
{
auto resourceTexture = unpackResourceTexture(resourceData, maxSize);
vram_->load(resourceTexture, resourceHeader->rect.toQRect());
}
QByteArray AdMemoryHandler::unpackResourceTexture(
uint8_t const* resourceData,
uint32_t maxSize)
{
AdResourceUnpacker adResourceUnpacker(resourceData, maxSize);
uint32_t outBufferSize =
(maxSize / BinCdImageReader::DATA_IN_SECTOR_SIZE) *
BinCdImageReader::DATA_IN_SECTOR_SIZE;
if (maxSize % BinCdImageReader::DATA_IN_SECTOR_SIZE != 0)
{ outBufferSize += BinCdImageReader::DATA_IN_SECTOR_SIZE; }
outBufferSize *= 2;
adResourceUnpacker.setOutBufferSize(outBufferSize);
return adResourceUnpacker.unpack();
}
void AdMemoryHandler::loadVRamPalette(
ResourceType2Header const* resourceHeader,
uint8_t const* resourceData)
{
if (!resourceHeader->flags.shouldCopyToVRam())
{ return; }
using Palette = VirtualPsxVRam::Palette4Bpp;
uint16_t palette4BppRawDataSize = Palette::rawDataSize();
uint16_t palette4BppSize = Palette::size();
QRect paletteRect(
resourceHeader->position.x * palette4BppRawDataSize,
resourceHeader->position.yOffsetFromY448 + 0x1c0,
resourceHeader->numberOf4BppPalettes * palette4BppSize,
1);
vram_->load(
resourceData,
resourceHeader->numberOf4BppPalettes * palette4BppRawDataSize,
paletteRect);
}
CharacterPortraitsData AdMemoryHandler::readCharacterPortraitsData(
AdSpeakerId speakerId)
{
auto const& speakerInfo = findSpeakerInfo(speakerId);
if (!speakerInfo.isValid())
{ return {}; }
auto speakerPortraitIndex = readSpeakerPortraitIndex(speakerId);
if (speakerPortraitIndex == INVALID_SPEAKER_PORTRAIT_INDEX)
{ return {}; }
PsxRamAddress portraitDataTableAddressAddress =
0x8006b1a8 + speakerPortraitIndex * sizeof(PsxRamAddress);
auto portraitDataTableAddress =
ram_->readAddress(portraitDataTableAddressAddress);
if (portraitDataTableAddress.isNull())
{ return {}; }
CharacterPortraitsData characterPortraitsData;
PsxRamAddress portraitDataAddress = portraitDataTableAddress;
for (
uint32_t variantIndex = 0;
variantIndex < speakerInfo.variantsNumber;
++variantIndex)
{
CharacterPortraitData characterPortraitData;
auto& portraitData = characterPortraitData.portraitData;
ram().readRegion(
{portraitDataAddress, sizeof(portraitData)},
reinterpret_cast<uint8_t*>(&portraitData));
if (portraitData.portraitMemoryLoadInfoAddress.isNull())
{ break; }
characterPortraitData.portraitDataAddress = portraitDataAddress;
characterPortraitsData.append(characterPortraitData);
portraitDataAddress += sizeof(PortraitData);
}
return characterPortraitsData;
}
uint8_t AdMemoryHandler::readSpeakerPortraitIndex(AdSpeakerId speakerId)
{
static constexpr uint8_t SPEAKERS_WITH_PORTRAIT_NUMBER = 18;
PsxRamAddress speakersWithPortraitsAddress = 0x8006b1ec;
for (uint8_t index = 0; index < SPEAKERS_WITH_PORTRAIT_NUMBER; ++index)
{
auto portraitSpeakerId =
ram().readByte(speakersWithPortraitsAddress.raw() + index);
if (speakerId == static_cast<AdSpeakerId>(portraitSpeakerId))
{ return index; }
}
return INVALID_SPEAKER_PORTRAIT_INDEX;
}
SpeakerInfo const& AdMemoryHandler::findSpeakerInfo(AdSpeakerId speakerId) const
{
for (auto const& speakerInfo : SPEAKERS_INFO)
{
if (speakerInfo.speakerId == speakerId)
{ return speakerInfo; }
}
return INVALID_SPEAKER_INFO;
}
CharacterPortraitResource AdMemoryHandler::loadCharacterPortrait(
PortraitData portraitData)
{
CharacterPortraitResource characterPortraitResource;
MemoryLoadInfo& memoryLoadInfo =
characterPortraitResource.resourcesMemoryLoadInfo;
ram().readRegion(
{
portraitData.portraitMemoryLoadInfoAddress,
sizeof(memoryLoadInfo)
},
reinterpret_cast<uint8_t*>(&memoryLoadInfo));
loadPortraitResourceIntoVRam(memoryLoadInfo);
characterPortraitResource.animationFrames =
readAnimation(portraitData.animationAddress);
auto animationAddress = portraitData.animationAddress;
characterPortraitResource.graphicsOffsetHalved =
doesAnimationHaveHalvedGraphicsOffsets(animationAddress);
return characterPortraitResource;
}
void AdMemoryHandler::loadPortraitResourceIntoVRam(
MemoryLoadInfo const& memoryLoadInfo)
{
auto portraitTextureResource = adCdImageReader_->readSectors(
memoryLoadInfo.sector,
memoryLoadInfo.sectorsNumber);
AdResourcesIterator resourcesIterator(portraitTextureResource);
int portraitImageIndex = 0;
while (resourcesIterator.hasNext())
{
if (portraitImageIndex > 2)
{
throw QString(
"Max number of resources (2) per portrait resources "
"excedeed.");
}
Rect portraitTextureRect;
ram_->readRegion(
{
0x8006b220 +
portraitImageIndex * sizeof(portraitTextureRect),
sizeof (portraitTextureRect)
},
reinterpret_cast<uint8_t*>(&portraitTextureRect));
auto nextResourceDescriptor = resourcesIterator.next();
auto const* resourceHeader = nextResourceDescriptor.header;
auto const* resourceData = nextResourceDescriptor.data;
auto resourceSize = nextResourceDescriptor.size;
if (resourceHeader->type == ResourceType::PackedImage)
{
auto resourceTexture =
unpackResourceTexture(resourceData, resourceSize);
vram().load(
resourceTexture,
portraitTextureRect.toQRect());
}
else
{
throw QString("Unexpected portrait resource type %1.")
.arg(static_cast<uint16_t>(resourceHeader->type));
}
++portraitImageIndex;
}
}
AnimationFrames AdMemoryHandler::readAnimation(PsxRamAddress animationAddress)
{
AnimationFrames animationFrames;
AnimationFrame animationFrame;
auto& animation = animationFrame.first;
while (true)
{
ram().readRegion(
{ animationAddress, sizeof(animation) },
reinterpret_cast<uint8_t*>(&animation));
if (animation.frameType != AnimationFrameType::NotLastFrame)
{ break; }
animationFrame.second = readGraphicsSeries(animation.graphicAddress);
animationFrames.append(animationFrame);
animationAddress += sizeof(Animation);
}
return animationFrames;
}
GraphicsSeries AdMemoryHandler::readGraphicsSeries(PsxRamAddress graphicAddress)
{
GraphicsSeries graphicsSeries;
Graphic graphic;
do
{
ram_->readRegion(
{graphicAddress, sizeof(graphic)},
reinterpret_cast<uint8_t*>(&graphic));
auto graphicImage = readGraphic(graphic);
graphicsSeries.append({graphic, graphicImage});
graphicAddress += sizeof(Graphic);
}
while (!graphic.hasFlags(GraphicFlags::SeriesEnd));
return graphicsSeries;
}
QImage AdMemoryHandler::readGraphic(Graphic const& graphic)
{
auto image = readPlainGraphic(graphic);
bool flipHorizontally = graphic.hasFlags(GraphicFlags::FlipHorizontally);
bool flipVertically = graphic.hasFlags(GraphicFlags::FlipVertically);
if (flipHorizontally || flipVertically)
{ return image.mirrored(flipHorizontally, flipVertically); }
else
{ return image;}
}
QImage AdMemoryHandler::readPlainGraphic(Graphic const& graphic)
{
switch (graphic.texpage.texpageBpp())
{
case TexpageBpp::BPP_4:
{
VirtualPsxVRam::Palette4Bpp palette;
vram_->read4BppPalette(graphic.clut, palette);
return vram_->read4BppTexture(graphic, &palette);
}
case TexpageBpp::BPP_8:
{
VirtualPsxVRam::Palette8Bpp palette;
vram_->read8BppPalette(graphic.clut, palette);
return vram_->read8BppTexture(graphic, &palette);
}
case TexpageBpp::BPP_15:
return vram_->read16BppTexture(graphic);
default:
throw QString("Unknown graphic Bpp (%1).").arg(graphic.texpage.bpp);
}
}
bool AdMemoryHandler::doesAnimationHaveHalvedGraphicsOffsets(
PsxRamAddress animationAddress)
{
return animationAddress == PsxRamAddress(0x80077784) ||
animationAddress == PsxRamAddress(0x800776fc);
}
QImage AdMemoryHandler::combinePortraitGraphicSeries(
GraphicsSeries const& graphicsSeries,
PortraitData const& portraitData)
{
auto animationAddress = portraitData.animationAddress;
return combineGraphicSeries(
graphicsSeries,
doesAnimationHaveHalvedGraphicsOffsets(animationAddress));
}
QImage AdMemoryHandler::combineGraphicSeries(
GraphicsSeries const& graphicSeries,
bool graphicOffsetHalved)
{
auto graphicSeriesBounds =
calculateGraphicsSeriesBounds(graphicSeries, graphicOffsetHalved);
auto anchorPoint = -graphicSeriesBounds.topLeft();
return combineGraphicSeries(
graphicSeries,
graphicOffsetHalved,
anchorPoint,
graphicSeriesBounds.size());
}
QRect AdMemoryHandler::calculateGraphicsSeriesBounds(
GraphicsSeries const& graphicsSeries,
bool graphicOffsetHalved)
{
if (graphicsSeries.isEmpty())
{ return {}; }
auto calculateGraphicBounds = [=](Graphic const& graphic) {
auto position = calculateGraphicPosition(graphic, graphicOffsetHalved);
QSize size(graphic.width, graphic.height);
return QRect(position, size);
};
auto it = graphicsSeries.begin();
auto bounds = calculateGraphicBounds((*it).first);
++it;
for (; it != graphicsSeries.end(); ++it)
{ bounds = bounds.united(calculateGraphicBounds((*it).first)); }
return bounds;
}
QImage AdMemoryHandler::combineGraphicSeries(
GraphicsSeries const& graphicsSeries,
bool graphicOffsetHalved,
QPoint const& anchorPoint,
QSize const& size)
{
if (graphicsSeries.isEmpty())
{ return QImage(); }
QImage combinedImage(size, QImage::Format_ARGB32);
combinedImage.fill(Qt::transparent);
QPainter combinedImagePainter(&combinedImage);
for (auto it = graphicsSeries.rbegin(); it != graphicsSeries.rend(); ++it)
{
auto const& graphic = (*it).first;
auto const& image = (*it).second;
auto imagePosition =
calculateGraphicInSeriesPosition(
graphic,
graphicOffsetHalved,
anchorPoint);
combinedImagePainter.drawImage(imagePosition, image);
}
return combinedImage;
}
QPoint AdMemoryHandler::calculateGraphicPosition(
Graphic const& graphic,
bool graphicOffsetHalved)
{
QPoint graphicPosition(graphic.xOffset, graphic.yOffset);
if (graphic.hasFlags(GraphicFlags::FlipHorizontally))
{ graphicPosition.rx() = -graphicPosition.rx() - graphic.width; }
if (graphic.hasFlags(GraphicFlags::FlipVertically))
{ graphicPosition.ry() = -graphicPosition.ry() - graphic.height; }
if (graphicOffsetHalved)
{ graphicPosition *= 2; }
return graphicPosition;
}
QPoint AdMemoryHandler::calculateGraphicInSeriesPosition(
Graphic const& graphic,
bool graphicOffsetHalved,
QPoint const& anchorPoint)
{ return calculateGraphicPosition(graphic, graphicOffsetHalved) + anchorPoint; }