forked from cyxx/rawgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_nth.cpp
555 lines (526 loc) · 12.4 KB
/
resource_nth.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include <time.h>
#include <sys/stat.h>
#include <zlib.h>
#include "pak.h"
#include "resource_nth.h"
#include "util.h"
#include "script.h"
static char *loadTextFile(File &f, const int size) {
char *buf = (char *)malloc(size + 1);
if (buf) {
const int count = f.read(buf, size);
if (count != size) {
warning("Failed to read %d bytes (%d expected)", count, size);
free(buf);
return 0;
}
buf[count] = 0;
}
return buf;
}
struct Resource15th: ResourceNth {
Pak _pak;
char _dataPath[MAXPATHLEN];
char _menuPath[MAXPATHLEN];
char *_textBuf;
const char *_stringsTable[157];
bool _hasRemasteredMusic;
Resource15th(const char *dataPath) {
snprintf(_dataPath, sizeof(_dataPath), "%s/Music/AW/RmSnd", dataPath);
struct stat s;
_hasRemasteredMusic = (stat(_dataPath, &s) == 0 && S_ISDIR(s.st_mode));
snprintf(_dataPath, sizeof(_dataPath), "%s/Data", dataPath);
snprintf(_menuPath, sizeof(_menuPath), "%s/Menu", dataPath);
_textBuf = 0;
memset(_stringsTable, 0, sizeof(_stringsTable));
}
virtual ~Resource15th() {
free(_textBuf);
}
virtual bool init() {
_pak.open(_dataPath);
_pak.readEntries();
return _pak._entriesCount != 0;
}
virtual uint8_t *load(const char *name) {
uint8_t *buf = 0;
const PakEntry *e = _pak.find(name);
if (e) {
buf = (uint8_t *)malloc(e->size);
if (buf) {
uint32_t size;
_pak.loadData(e, buf, &size);
}
} else {
warning("Unable to load '%s'", name);
}
return buf;
}
virtual uint8_t *loadBmp(int num) {
char name[16];
if (num >= 3000) {
snprintf(name, sizeof(name), "e%04d.bmp", num);
} else {
snprintf(name, sizeof(name), "file%03d.bmp", num);
}
return load(name);
}
virtual uint8_t *loadDat(int num, uint8_t *dst, uint32_t *size) {
char name[16];
snprintf(name, sizeof(name), "file%03d.dat", num);
const PakEntry *e = _pak.find(name);
if (e) {
_pak.loadData(e, dst, size);
return dst;
} else {
warning("Unable to load '%s'", name);
}
return 0;
}
virtual uint8_t *loadWav(int num, uint8_t *dst, uint32_t *size) {
char name[32];
const PakEntry *e = 0;
if (Script::_useRemasteredAudio) {
snprintf(name, sizeof(name), "rmsnd/file%03d.wav", num);
e = _pak.find(name);
}
if (!e) {
snprintf(name, sizeof(name), "file%03db.wav", num);
e = _pak.find(name);
if (!e) {
snprintf(name, sizeof(name), "file%03d.wav", num);
e = _pak.find(name);
}
}
if (e) {
uint8_t *p = (uint8_t *)malloc(e->size);
if (p) {
_pak.loadData(e, p, size);
*size = 0;
return p;
}
warning("Failed to allocate %d bytes", e->size);
} else {
warning("Unable to load '%s'", name);
}
return 0;
}
void loadStrings(Language lang) {
if (!_textBuf) {
const char *name = 0;
switch (lang) {
case LANG_FR:
name = "Francais.Txt";
break;
case LANG_US:
name = "English.Txt";
break;
case LANG_DE:
name = "German.Txt";
break;
case LANG_ES:
name = "Espanol.txt";
break;
case LANG_IT:
name = "Italian.Txt";
break;
default:
return;
}
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/lang_%s", _menuPath, name);
File f;
if (f.open(path)) {
const int size = f.size();
_textBuf = loadTextFile(f, size);
if (_textBuf) {
char *p = _textBuf;
while (true) {
char *end = strchr(p, '\r');
if (!end) {
break;
}
*end++ = 0;
if (*end == '\n') {
*end++ = 0;
}
const int len = end - p;
int strNum = -1;
if (len > 3 && sscanf(p, "%03d", &strNum) == 1) {
p += 3;
while (*p == ' ' || *p == '\t') {
++p;
}
if ((uint32_t)strNum < ARRAYSIZE(_stringsTable)) {
_stringsTable[strNum] = p;
}
}
p = end;
}
}
}
}
}
virtual const char *getString(Language lang, int num) {
loadStrings(lang);
if ((uint32_t)num < ARRAYSIZE(_stringsTable)) {
return _stringsTable[num];
}
return 0;
}
virtual const char *getMusicName(int num) {
const char *path = 0;
switch (num) {
case 7:
if (_hasRemasteredMusic && Script::_useRemasteredAudio) {
path = "Music/AW/RmSnd/Intro2004.wav";
} else {
path = "Music/AW/Intro2004.wav";
}
break;
case 138:
if (_hasRemasteredMusic && Script::_useRemasteredAudio) {
path = "Music/AW/RmSnd/End2004.wav";
} else {
path = "Music/AW/End2004.wav";
}
break;
}
return path;
}
virtual void getBitmapSize(int *w, int *h) {
*w = 1280;
*h = 800;
}
};
static uint8_t *inflateGzip(const char *filepath) {
File f;
if (!f.open(filepath)) {
warning("Unable to open '%s'", filepath);
return 0;
}
const uint16_t sig = f.readUint16LE();
if (sig != 0x8B1F) {
warning("Unexpected file signature 0x%x for '%s'", sig, filepath);
return 0;
}
f.seek(-4, SEEK_END);
const uint32_t dataSize = f.readUint32LE();
uint8_t *out = (uint8_t *)malloc(dataSize);
if (!out) {
warning("Failed to allocate %d bytes", dataSize);
return 0;
}
f.seek(0);
z_stream str;
memset(&str, 0, sizeof(str));
int err = inflateInit2(&str, MAX_WBITS + 16);
if (err == Z_OK) {
Bytef buf[1 << MAX_WBITS];
str.next_in = buf;
str.avail_in = 0;
str.next_out = out;
str.avail_out = dataSize;
while (err == Z_OK && str.avail_out != 0) {
if (str.avail_in == 0 && !f.ioErr()) {
str.next_in = buf;
str.avail_in = f.read(buf, sizeof(buf));
}
err = inflate(&str, Z_NO_FLUSH);
}
inflateEnd(&str);
if (err == Z_STREAM_END) {
return out;
}
}
free(out);
return 0;
}
struct Resource20th: ResourceNth {
const char *_dataPath;
char *_textBuf;
const char *_stringsTable[192];
char _musicName[64];
uint8_t _musicType;
char _datName[32];
const char *_bitmapSize;
Resource20th(const char *dataPath)
: _dataPath(dataPath), _textBuf(0) {
memset(_stringsTable, 0, sizeof(_stringsTable));
_musicType = 0;
_datName[0] = 0;
srand(time(NULL));
}
virtual ~Resource20th() {
free(_textBuf);
}
virtual bool init() {
static const char *dirs[] = { "BGZ", "DAT", "WGZ", 0 };
for (int i = 0; dirs[i]; ++i) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/game/%s", _dataPath, dirs[i]);
struct stat s;
if (stat(path, &s) != 0 || !S_ISDIR(s.st_mode)) {
warning("'%s' is not a directory", path);
return false;
}
}
static const char *bmps[] = {
"1728x1080",
"1280x800",
"1152x720",
"960x600",
"864x540",
"768x480",
"480x300",
"320x200",
0
};
_bitmapSize = 0;
for (int i = 0; bmps[i]; ++i) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/game/BGZ/data%s", _dataPath, bmps[i]);
struct stat s;
if (stat(path, &s) == 0 && S_ISDIR(s.st_mode)) {
_bitmapSize = bmps[i];
break;
}
}
return true;
}
virtual uint8_t *load(const char *name) {
if (strcmp(name, "font.bmp") == 0) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/game/BGZ/Font.bgz", _dataPath);
return inflateGzip(path);
} else if (strcmp(name, "heads.bmp") == 0) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/game/BGZ/Heads.bgz", _dataPath);
return inflateGzip(path);
}
return 0;
}
virtual uint8_t *loadBmp(int num) {
char path[MAXPATHLEN];
if (num >= 3000 && _bitmapSize) {
snprintf(path, sizeof(path), "%s/game/BGZ/data%s/%s_e%04d.bgz", _dataPath, _bitmapSize, _bitmapSize, num);
} else {
snprintf(path, sizeof(path), "%s/game/BGZ/file%03d.bgz", _dataPath, num);
}
return inflateGzip(path);
}
void preloadDat(int part, int type, int num) {
static const char *names[] = {
"INTRO", "EAU", "PRI", "CITE", "arene", "LUXE", "FINAL", 0
};
static const char *exts[] = {
"pal", "mac", "mat", 0
};
if (part > 0 && part < 8) {
if (type == 3) {
assert(num == 0x11);
strcpy(_datName, "BANK2.MAT");
} else {
snprintf(_datName, sizeof(_datName), "%s2011.%s", names[part - 1], exts[type]);
}
debug(DBG_RESOURCE, "Loading '%s'", _datName);
} else {
_datName[0] = 0;
}
}
virtual uint8_t *loadDat(int num, uint8_t *dst, uint32_t *size) {
bool datOpen = false;
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/game/DAT", _dataPath);
File f;
if (_datName[0]) {
datOpen = f.open(_datName, path);
}
if (!datOpen) {
snprintf(_datName, sizeof(_datName), "FILE%03d.DAT", num);
datOpen = f.open(_datName, path);
}
if (datOpen) {
const uint32_t dataSize = f.size();
const uint32_t count = f.read(dst, dataSize);
if (count != dataSize) {
warning("Failed to read %d bytes (expected %d)", dataSize, count);
}
*size = dataSize;
} else {
warning("Unable to open '%s/%s'", path, _datName);
dst = 0;
}
_datName[0] = 0;
return dst;
}
virtual uint8_t *loadWav(int num, uint8_t *dst, uint32_t *size) {
char path[MAXPATHLEN];
if (!Script::_useRemasteredAudio) {
snprintf(path, sizeof(path), "%s/game/WGZ/original/file%03d.wgz", _dataPath, num);
struct stat s;
if (stat(path, &s) != 0) {
snprintf(path, sizeof(path), "%s/game/WGZ/original/file%03dB.wgz", _dataPath, num);
}
*size = 0;
return inflateGzip(path);
}
switch (num) {
case 81: {
const int r = 1 + rand() % 3;
snprintf(path, sizeof(path), "%s/game/WGZ/file081-EX-%d.wgz", _dataPath, r);
}
break;
case 85: {
const int r = 1 + rand() % 2;
const char *snd = "IN";
if (_musicType == 1) {
snd = "EX";
}
snprintf(path, sizeof(path), "%s/game/WGZ/file085-%s-%d.wgz", _dataPath, snd, r);
}
break;
case 96: {
const int r = 1 + rand() % 3;
const char *snd = "GR";
if (_musicType == 1) {
snd = "EX";
} else if (_musicType == 2) {
snd = "IN";
}
snprintf(path, sizeof(path), "%s/game/WGZ/file096-%s-%d.wgz", _dataPath, snd, r);
}
break;
case 163: {
const char *snd = "GR";
if (_musicType == 1) {
snd = "EX";
} else if (_musicType == 2) {
snd = "IN";
}
snprintf(path, sizeof(path), "%s/game/WGZ/file163-%s-1.wgz", _dataPath, snd);
}
break;
default: {
snprintf(path, sizeof(path), "%s/game/WGZ/file%03d.wgz", _dataPath, num);
struct stat s;
if (stat(path, &s) != 0) {
snprintf(path, sizeof(path), "%s/game/WGZ/file%03dB.wgz", _dataPath, num);
}
}
break;
}
*size = 0;
return inflateGzip(path);
}
void loadStrings(Language lang) {
if (!_textBuf) {
const char *name = 0;
switch (lang) {
case LANG_FR:
name = "FR";
break;
case LANG_US:
name = "EN";
break;
case LANG_DE:
name = "DE";
break;
case LANG_ES:
name = "ES";
break;
case LANG_IT:
name = "IT";
break;
default:
return;
}
char path[MAXPATHLEN];
static const char *fmt[] = {
"%s/game/TXT/%s.txt",
"%s/game/TXT/Linux/%s.txt",
0
};
bool isOpen = false;
File f;
for (int i = 0; fmt[i] && !isOpen; ++i) {
snprintf(path, sizeof(path), fmt[i], _dataPath, name);
isOpen = f.open(path);
}
if (isOpen) {
const int size = f.size();
_textBuf = loadTextFile(f, size);
if (_textBuf) {
int count = 0;
for (char *p = _textBuf; count < (int)ARRAYSIZE(_stringsTable); ) {
_stringsTable[count++] = p;
char *end = strchr(p, '\n');
if (!end) {
break;
}
*end++ = 0;
p = end;
}
}
}
}
}
virtual const char *getString(Language lang, int num) {
loadStrings(lang);
if ((uint32_t)num < ARRAYSIZE(_stringsTable)) {
return _stringsTable[num];
}
return 0;
}
virtual const char *getMusicName(int num) {
if (num >= 5000 && Script::_useRemasteredAudio) {
snprintf(_musicName, sizeof(_musicName), "game/OGG/amb%d.ogg", num);
switch (num) {
case 5005:
_musicType = 1;
break;
case 5006:
_musicType = 3;
break;
default:
_musicType = 2;
break;
}
} else {
switch (num) {
case 7:
if (Script::_useRemasteredAudio) {
strcpy(_musicName, "game/OGG/Intro_20th.ogg");
} else {
strcpy(_musicName, "game/OGG/original/intro.ogg");
}
break;
case 138:
if (!Script::_useRemasteredAudio) {
strcpy(_musicName, "game/OGG/original/ending.ogg");
break;
}
/* fall-through */
default:
return 0;
}
}
return _musicName;
}
virtual void getBitmapSize(int *w, int *h) {
if (_bitmapSize && sscanf(_bitmapSize, "%dx%d", w, h) == 2) {
return;
}
*w = 0;
*h = 0;
}
};
ResourceNth *ResourceNth::create(int edition, const char *dataPath) {
switch (edition) {
case 15:
return new Resource15th(dataPath);
case 20:
return new Resource20th(dataPath);
}
return 0;
}