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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2014 Franklin Wei, Benjamin Brown
* Copyright (C) 2004 Gregory Montoir
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "plugin.h"
#include "resource.h"
#include "bank.h"
#include "file.h"
#include "serializer.h"
#include "video.h"
#include "util.h"
#include "parts.h"
#include "vm.h"
#include "sys.h"
void res_create(struct Resource* res, struct Video* vid, struct System* sys, const char* dataDir)
{
res->video = vid;
res->sys = sys;
res->_dataDir = dataDir;
res->currentPartId = 0;
res->requestedNextPart = 0;
}
void res_readBank(struct Resource* res, const MemEntry *me, uint8_t *dstBuf) {
uint16_t n = me - res->_memList;
debug(DBG_BANK, "res_readBank(%d)", n);
struct Bank bk;
bank_create(&bk, res->_dataDir);
if (!bank_read(&bk, me, dstBuf)) {
error("res_readBank() unable to unpack entry %d\n", n);
}
}
#ifdef XWORLD_DEBUG
static const char *resTypeToString(struct Resource* res, unsigned int type)
{
(void) res;
static const char* resTypes[] =
{
"RT_SOUND",
"RT_MUSIC",
"RT_POLY_ANIM",
"RT_PALETTE",
"RT_BYTECODE",
"RT_POLY_CINEMATIC"
};
if (type >= (sizeof(resTypes) / sizeof(const char *)))
return "RT_UNKNOWN";
return resTypes[type];
}
#endif
#define RES_SIZE 0
#define RES_COMPRESSED 1
int resourceSizeStats[7][2];
#define STATS_TOTAL_SIZE 6
int resourceUnitStats[7][2];
/*
Read all entries from memlist.bin. Do not load anything in memory,
this is just a fast way to access the data later based on their id.
*/
void res_readEntries(struct Resource* res) {
File f;
file_create(&f, false);
int resourceCounter = 0;
if (!file_open(&f, "memlist.bin", res->_dataDir, "rb")) {
error("Could not open 'MEMLIST.BIN', data files missing");
/* error() will exit() no need to return or do anything else. */
}
/* Prepare stats array */
rb->memset(resourceSizeStats, 0, sizeof(resourceSizeStats));
rb->memset(resourceUnitStats, 0, sizeof(resourceUnitStats));
res->_numMemList = 0;
struct MemEntry *memEntry = res->_memList;
while (1) {
assert(res->_numMemList < ARRAYLEN(res->_memList));
memEntry->state = file_readByte(&f);
memEntry->type = file_readByte(&f);
memEntry->bufPtr = 0;
file_readUint16BE(&f);
memEntry->unk4 = file_readUint16BE(&f);
memEntry->rankNum = file_readByte(&f);
memEntry->bankId = file_readByte(&f);
memEntry->bankOffset = file_readUint32BE(&f);
memEntry->unkC = file_readUint16BE(&f);
memEntry->packedSize = file_readUint16BE(&f);
memEntry->unk10 = file_readUint16BE(&f);
memEntry->size = file_readUint16BE(&f);
debug(DBG_RES, "mementry state is %d", memEntry->state);
if (memEntry->state == MEMENTRY_STATE_END_OF_MEMLIST) {
break;
}
/* Memory tracking */
if (memEntry->packedSize == memEntry->size)
{
resourceUnitStats[memEntry->type][RES_SIZE] ++;
resourceUnitStats[STATS_TOTAL_SIZE][RES_SIZE] ++;
}
else
{
resourceUnitStats[memEntry->type][RES_COMPRESSED] ++;
resourceUnitStats[STATS_TOTAL_SIZE][RES_COMPRESSED] ++;
}
resourceSizeStats[memEntry->type][RES_SIZE] += memEntry->size;
resourceSizeStats[STATS_TOTAL_SIZE][RES_SIZE] += memEntry->size;
resourceSizeStats[memEntry->type][RES_COMPRESSED] += memEntry->packedSize;
resourceSizeStats[STATS_TOTAL_SIZE][RES_COMPRESSED] += memEntry->packedSize;
debug(DBG_RES, "R:0x%02X, %-17s size=%5d (compacted gain=%2.0f%%)",
resourceCounter,
resTypeToString(res, memEntry->type),
memEntry->size,
memEntry->size ? (memEntry->size - memEntry->packedSize) / (float)memEntry->size * 100.0f : 0.0f);
resourceCounter++;
res->_numMemList++;
memEntry++;
}
debug(DBG_RES, "\n");
debug(DBG_RES, "Total # resources: %d", resourceCounter);
debug(DBG_RES, "Compressed : %d", resourceUnitStats[STATS_TOTAL_SIZE][RES_COMPRESSED]);
debug(DBG_RES, "Uncompressed : %d", resourceUnitStats[STATS_TOTAL_SIZE][RES_SIZE]);
debug(DBG_RES, "Note: %2.0f%% of resources are compressed.", 100 * resourceUnitStats[STATS_TOTAL_SIZE][RES_COMPRESSED] / (float)resourceCounter);
debug(DBG_RES, "\n");
debug(DBG_RES, "Total size (uncompressed) : %7d bytes.", resourceSizeStats[STATS_TOTAL_SIZE][RES_SIZE]);
debug(DBG_RES, "Total size (compressed) : %7d bytes.", resourceSizeStats[STATS_TOTAL_SIZE][RES_COMPRESSED]);
debug(DBG_RES, "Note: Overall compression gain is : %2.0f%%.",
(resourceSizeStats[STATS_TOTAL_SIZE][RES_SIZE] - resourceSizeStats[STATS_TOTAL_SIZE][RES_COMPRESSED]) / (float)resourceSizeStats[STATS_TOTAL_SIZE][RES_SIZE] * 100);
debug(DBG_RES, "\n");
for(int i = 0 ; i < 6 ; i++)
debug(DBG_RES, "Total %-17s unpacked size: %7d (%2.0f%% of total unpacked size) packedSize %7d (%2.0f%% of floppy space) gain:(%2.0f%%)",
resTypeToString(res, i),
resourceSizeStats[i][RES_SIZE],
resourceSizeStats[i][RES_SIZE] / (float)resourceSizeStats[STATS_TOTAL_SIZE][RES_SIZE] * 100.0f,
resourceSizeStats[i][RES_COMPRESSED],
resourceSizeStats[i][RES_COMPRESSED] / (float)resourceSizeStats[STATS_TOTAL_SIZE][RES_COMPRESSED] * 100.0f,
(resourceSizeStats[i][RES_SIZE] - resourceSizeStats[i][RES_COMPRESSED]) / (float)resourceSizeStats[i][RES_SIZE] * 100.0f);
debug(DBG_RES, "Note: Damn you sound compression rate!");
debug(DBG_RES, "\nTotal bank files: %d", resourceUnitStats[STATS_TOTAL_SIZE][RES_SIZE] + resourceUnitStats[STATS_TOTAL_SIZE][RES_COMPRESSED]);
for(int i = 0 ; i < 6 ; i++)
debug(DBG_RES, "Total %-17s files: %3d", resTypeToString(res, i), resourceUnitStats[i][RES_SIZE] + resourceUnitStats[i][RES_COMPRESSED]);
file_close(&f);
}
/*
Go over every resource and check if they are marked at "MEMENTRY_STATE_LOAD_ME".
Load them in memory and mark them are MEMENTRY_STATE_LOADED
*/
void res_loadMarkedAsNeeded(struct Resource* res) {
while (1) {
struct MemEntry *me = NULL;
/* get resource with max rankNum */
uint8_t maxNum = 0;
uint16_t i = res->_numMemList;
struct MemEntry *it = res->_memList;
while (i--) {
if (it->state == MEMENTRY_STATE_LOAD_ME && maxNum <= it->rankNum) {
maxNum = it->rankNum;
me = it;
}
it++;
}
if (me == NULL) {
break; // no entry found
}
/* At this point the resource descriptor should be pointed to "me" */
uint8_t *loadDestination = NULL;
if (me->type == RT_POLY_ANIM) {
loadDestination = res->_vidCurPtr;
} else {
loadDestination = res->_scriptCurPtr;
if (me->size > res->_vidBakPtr - res->_scriptCurPtr) {
warning("res_load() not enough memory");
me->state = MEMENTRY_STATE_NOT_NEEDED;
continue;
}
}
if (me->bankId == 0) {
warning("res_load() ec=0x%X (me->bankId == 0)", 0xF00);
me->state = MEMENTRY_STATE_NOT_NEEDED;
} else {
debug(DBG_BANK, "res_load() bufPos=%X size=%X type=%X pos=%X bankId=%X", loadDestination - res->_memPtrStart, me->packedSize, me->type, me->bankOffset, me->bankId);
res_readBank(res, me, loadDestination);
if(me->type == RT_POLY_ANIM) {
video_copyPagePtr(res->video, res->_vidCurPtr);
me->state = MEMENTRY_STATE_NOT_NEEDED;
} else {
me->bufPtr = loadDestination;
me->state = MEMENTRY_STATE_LOADED;
res->_scriptCurPtr += me->size;
}
}
}
}
void res_invalidateRes(struct Resource* res) {
struct MemEntry *me = res->_memList;
uint16_t i = res->_numMemList;
while (i--) {
if (me->type <= RT_POLY_ANIM || me->type > 6) { /* 6 WTF ?!?! ResType goes up to 5 !! */
me->state = MEMENTRY_STATE_NOT_NEEDED;
}
++me;
}
res->_scriptCurPtr = res->_scriptBakPtr;
}
void res_invalidateAll(struct Resource* res) {
struct MemEntry *me = res->_memList;
uint16_t i = res->_numMemList;
while (i--) {
me->state = MEMENTRY_STATE_NOT_NEEDED;
++me;
}
res->_scriptCurPtr = res->_memPtrStart;
}
/* This method serves two purpose:
- Load parts in memory segments (palette,code,video1,video2)
or
- Load a resource in memory
This is decided based on the resourceId. If it does not match a mementry id it is supposed to
be a part id. */
void res_loadPartsOrMemoryEntry(struct Resource* res, uint16_t resourceId) {
if (resourceId > res->_numMemList) {
res->requestedNextPart = resourceId;
} else {
struct MemEntry *me = &res->_memList[resourceId];
if (me->state == MEMENTRY_STATE_NOT_NEEDED) {
me->state = MEMENTRY_STATE_LOAD_ME;
res_loadMarkedAsNeeded(res);
}
}
}
/* Protection screen and cinematic don't need the player and enemies polygon data
so _memList[video2Index] is never loaded for those parts of the game. When
needed (for action phrases) _memList[video2Index] is always loaded with 0x11
(as seen in memListParts). */
void res_setupPart(struct Resource* res, uint16_t partId) {
if (partId == res->currentPartId)
return;
if (partId < GAME_PART_FIRST || partId > GAME_PART_LAST)
error("res_setupPart() ec=0x%X invalid partId", partId);
uint16_t memListPartIndex = partId - GAME_PART_FIRST;
uint8_t paletteIndex = memListParts[memListPartIndex][MEMLIST_PART_PALETTE];
uint8_t codeIndex = memListParts[memListPartIndex][MEMLIST_PART_CODE];
uint8_t videoCinematicIndex = memListParts[memListPartIndex][MEMLIST_PART_POLY_CINEMATIC];
uint8_t video2Index = memListParts[memListPartIndex][MEMLIST_PART_VIDEO2];
/* Mark all resources as located on harddrive. */
res_invalidateAll(res);
res->_memList[paletteIndex].state = MEMENTRY_STATE_LOAD_ME;
res->_memList[codeIndex].state = MEMENTRY_STATE_LOAD_ME;
res->_memList[videoCinematicIndex].state = MEMENTRY_STATE_LOAD_ME;
/* This is probably a cinematic or a non interactive part of the game. */
/* Player and enemy polygons are not needed. */
if (video2Index != MEMLIST_PART_NONE)
res->_memList[video2Index].state = MEMENTRY_STATE_LOAD_ME;
res_loadMarkedAsNeeded(res);
res->segPalettes = res->_memList[paletteIndex].bufPtr;
debug(DBG_RES, "paletteIndex is 0x%08x", res->segPalettes);
res->segBytecode = res->_memList[codeIndex].bufPtr;
res->segCinematic = res->_memList[videoCinematicIndex].bufPtr;
/* This is probably a cinematic or a non interactive part of the game. */
/* Player and enemy polygons are not needed. */
if (video2Index != MEMLIST_PART_NONE)
res->_segVideo2 = res->_memList[video2Index].bufPtr;
debug(DBG_RES, "");
debug(DBG_RES, "setupPart(%d)", partId - GAME_PART_FIRST);
debug(DBG_RES, "Loaded resource %d (%s) in segPalettes.", paletteIndex, resTypeToString(res, res->_memList[paletteIndex].type));
debug(DBG_RES, "Loaded resource %d (%s) in segBytecode.", codeIndex, resTypeToString(res, res->_memList[codeIndex].type));
debug(DBG_RES, "Loaded resource %d (%s) in segCinematic.", videoCinematicIndex, resTypeToString(res, res->_memList[videoCinematicIndex].type));
/* prevent warnings: */
#ifdef XWORLD_DEBUG
if (video2Index != MEMLIST_PART_NONE)
debug(DBG_RES, "Loaded resource %d (%s) in _segVideo2.", video2Index, resTypeToString(res, res->_memList[video2Index].type));
#endif
res->currentPartId = partId;
/* _scriptCurPtr is changed in res->load(); */
res->_scriptBakPtr = res->_scriptCurPtr;
}
void res_allocMemBlock(struct Resource* res) {
if(rb->audio_status())
rb->audio_stop();
/* steal the audio buffer */
size_t sz;
/* memory usage is as follows:
[VM memory - 600K]
[Framebuffers - 128K]
[Temporary framebuffer - 192K]
[String table buffer]
*/
res->_memPtrStart = rb->plugin_get_audio_buffer(&sz);
if(sz < MEM_BLOCK_SIZE + (4 * VID_PAGE_SIZE) + 320 * 200 * sizeof(fb_data))
{
warning("res_allocMemBlock: can't allocate enough memory!");
}
res->sys->membuf = res->_memPtrStart + ( MEM_BLOCK_SIZE + (4 * VID_PAGE_SIZE) + 320 * 200 * sizeof(fb_data));
res->sys->bytes_left = sz - (MEM_BLOCK_SIZE + (4 * VID_PAGE_SIZE) + 320 * 200 * sizeof(fb_data));
debug(DBG_RES, "audiobuf is %d bytes in size", sz);
res->_scriptBakPtr = res->_scriptCurPtr = res->_memPtrStart;
res->_vidBakPtr = res->_vidCurPtr = res->_memPtrStart + MEM_BLOCK_SIZE - 0x800 * 16; //0x800 = 2048, so we have 32KB free for vidBack and vidCur
res->_useSegVideo2 = false;
}
void res_freeMemBlock(struct Resource* res) {
(void) res;
/* there's no need to do anything to free the audio buffer */
return;
}
void res_saveOrLoad(struct Resource* res, struct Serializer *ser) {
uint8_t loadedList[64];
if (ser->_mode == SM_SAVE) {
rb->memset(loadedList, 0, sizeof(loadedList));
uint8_t *p = loadedList;
uint8_t *q = res->_memPtrStart;
while (1) {
struct MemEntry *it = res->_memList;
struct MemEntry *me = 0;
uint16_t num = res->_numMemList;
while (num--) {
if (it->state == MEMENTRY_STATE_LOADED && it->bufPtr == q) {
me = it;
}
++it;
}
if (me == 0) {
break;
} else {
assert(p < loadedList + 64);
*p++ = me - res->_memList;
q += me->size;
}
}
}
struct Entry entries[] = {
SE_ARRAY(loadedList, 64, SES_INT8, VER(1)),
SE_INT(&res->currentPartId, SES_INT16, VER(1)),
SE_PTR(&res->_scriptBakPtr, VER(1)),
SE_PTR(&res->_scriptCurPtr, VER(1)),
SE_PTR(&res->_vidBakPtr, VER(1)),
SE_PTR(&res->_vidCurPtr, VER(1)),
SE_INT(&res->_useSegVideo2, SES_BOOL, VER(1)),
SE_PTR(&res->segPalettes, VER(1)),
SE_PTR(&res->segBytecode, VER(1)),
SE_PTR(&res->segCinematic, VER(1)),
SE_PTR(&res->_segVideo2, VER(1)),
SE_END()
};
ser_saveOrLoadEntries(ser, entries);
if (ser->_mode == SM_LOAD) {
uint8_t *p = loadedList;
uint8_t *q = res->_memPtrStart;
while (*p) {
struct MemEntry *me = &res->_memList[*p++];
res_readBank(res, me, q);
me->bufPtr = q;
me->state = MEMENTRY_STATE_LOADED;
q += me->size;
}
}
}
const char* res_getDataDir(struct Resource* res)
{
return res->_dataDir;
}
|