-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_nth.cpp
319 lines (296 loc) · 7.11 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
#include <sys/stat.h>
#include <zlib.h>
#include "pak.h"
#include "resource_nth.h"
#include "util.h"
struct Resource15th: ResourceNth {
Pak _pak;
char _dataPath[MAXPATHLEN];
Resource15th(const char *dataPath) {
snprintf(_dataPath, sizeof(_dataPath), "%s/Data", dataPath);
}
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];
snprintf(name, sizeof(name), "rmsnd/file%03d.wav", num);
const PakEntry *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;
}
virtual const char *getMusicName(int num) {
const char *path = 0;
switch (num) {
case 7:
path = "Music/AW/RmSnd/Intro2004.wav";
break;
case 138:
path = "Music/AW/RmSnd/End2004.wav";
break;
}
return path;
}
};
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) {
uint8_t 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];
Resource20th(const char *dataPath)
: _dataPath(dataPath), _textBuf(0) {
memset(_stringsTable, 0, sizeof(_stringsTable));
}
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;
}
}
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) {
snprintf(path, sizeof(path), "%s/game/BGZ/data1280x800/1280x800_e%04d.bgz", _dataPath, num);
} else {
snprintf(path, sizeof(path), "%s/game/BGZ/file%03d.bgz", _dataPath, num);
}
return inflateGzip(path);
}
virtual uint8_t *loadDat(int num, uint8_t *dst, uint32_t *size) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/game/DAT/FILE%03d.DAT", _dataPath, num);
File f;
if (f.open(path)) {
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;
return dst;
} else {
warning("Unable to open '%s'", path);
}
return 0;
}
virtual uint8_t *loadWav(int num, uint8_t *dst, uint32_t *size) {
char path[MAXPATHLEN];
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);
}
*size = 0;
return inflateGzip(path);
}
const char *getString(Language lang, int num) {
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 0;
}
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 = (char *)malloc(size + 1);
if (_textBuf) {
int count = f.read(_textBuf, size);
if (count != size) {
warning("Failed to read %d bytes (%d expected)", count, size);
free(_textBuf);
_textBuf = 0;
return 0;
}
_textBuf[count] = 0;
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;
}
}
}
}
if (num >= 0 && num < (int)ARRAYSIZE(_stringsTable)) {
return _stringsTable[num];
}
return 0;
}
virtual const char *getMusicName(int num) {
if (num >= 5000) {
snprintf(_musicName, sizeof(_musicName), "game/OGG/amb%d.ogg", num);
} else {
switch (num) {
case 7:
strcpy(_musicName, "game/OGG/Intro_20th.ogg");
break;
case 138:
strcpy(_musicName, "game/OGG/Ending_15th.ogg");
break;
default:
return 0;
}
}
return _musicName;
}
};
ResourceNth *ResourceNth::create(int edition, const char *dataPath) {
switch (edition) {
case 15:
return new Resource15th(dataPath);
case 20:
return new Resource20th(dataPath);
}
return 0;
}