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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/scummsys.h"
#include "common/archive.h"
#include "common/textconsole.h"
#include "mads/mads.h"
#include "mads/resources.h"
namespace MADS {
enum ResourceType {RESTYPE_ROOM, RESTYPE_SC, RESTYPE_TEXT, RESTYPE_QUO, RESTYPE_I,
RESTYPE_OB, RESTYPE_FONT, RESTYPE_SOUND, RESTYPE_SPEECH, RESTYPE_HAS_EXT, RESTYPE_NO_EXT};
/**
* HAG Archives implementation
*/
class HagArchive : public Common::Archive {
private:
/**
* Details of a single entry in a HAG file index
*/
struct HagEntry {
Common::String _resourceName;
uint32 _offset;
uint32 _size;
HagEntry(): _offset(0), _size(0) {}
HagEntry(Common::String resourceName, uint32 offset, uint32 size):
_resourceName(resourceName), _offset(offset), _size(size) {}
};
class HagIndex {
public:
Common::List<HagEntry> _entries;
Common::String _filename;
};
Common::Array<HagIndex> _index;
/**
* Load the index of all the game's HAG files
*/
void loadIndex(MADSEngine *vm);
/**
* Given a resource name, opens up the correct HAG file and returns whether
* an entry with the given name exists.
*/
bool getHeaderEntry(const Common::String &resourceName, HagIndex &hagIndex, HagEntry &hagEntry) const;
/**
* Returns the HAG resource filename that will contain a given resource
*/
Common::String getResourceFilename(const Common::String &resourceName) const;
/**
* Return a resource type given a resource name
*/
ResourceType getResourceType(const Common::String &resourceName) const;
public:
HagArchive(MADSEngine *vm);
virtual ~HagArchive();
// Archive implementation
virtual bool hasFile(const Common::String &name) const;
virtual int listMembers(Common::ArchiveMemberList &list) const;
virtual const Common::ArchiveMemberPtr getMember(const Common::String &name) const;
virtual Common::SeekableReadStream *createReadStreamForMember(const Common::String &name) const;
};
const char *const MADSCONCAT_STRING = "MADSCONCAT";
HagArchive::HagArchive(MADSEngine *vm) {
loadIndex(vm);
}
HagArchive::~HagArchive() {
}
// Archive implementation
bool HagArchive::hasFile(const Common::String &name) const {
HagIndex hagIndex;
HagEntry hagEntry;
return getHeaderEntry(name, hagIndex, hagEntry);
}
int HagArchive::listMembers(Common::ArchiveMemberList &list) const {
int members = 0;
for (uint hagCtr = 0; hagCtr < _index.size(); ++hagCtr) {
HagIndex hagIndex = _index[hagCtr];
Common::List<HagEntry>::iterator i;
for (i = hagIndex._entries.begin(); i != hagIndex._entries.end(); ++i) {
list.push_back(Common::ArchiveMemberList::value_type(
new Common::GenericArchiveMember((*i)._resourceName, this)));
++members;
}
}
return members;
}
const Common::ArchiveMemberPtr HagArchive::getMember(const Common::String &name) const {
if (!hasFile(name))
return Common::ArchiveMemberPtr();
return Common::ArchiveMemberPtr(new Common::GenericArchiveMember(name, this));
}
Common::SeekableReadStream *HagArchive::createReadStreamForMember(const Common::String &name) const {
HagIndex hagIndex;
HagEntry hagEntry;
if (getHeaderEntry(name, hagIndex, hagEntry)) {
// Entry found. If the correct file is not already open, open it
Common::File f;
if (!f.open(hagIndex._filename))
error("Could not open HAG file");
// Return a new stream for the specific resource
f.seek(hagEntry._offset);
return f.readStream(hagEntry._size);
}
return nullptr;
}
void HagArchive::loadIndex(MADSEngine *vm) {
Common::File hagFile;
for (int sectionIndex = -1; sectionIndex < 11; ++sectionIndex) {
if (sectionIndex == 0)
continue;
// Dragonsphere does not have some sections - skip them
if (vm->getGameID() == GType_Dragonsphere) {
if (sectionIndex == 7 || sectionIndex == 8)
continue;
}
// Phantom does not have some sections - skip them
if (vm->getGameID() == GType_Phantom) {
if (sectionIndex == 6 || sectionIndex == 7 || sectionIndex == 8)
continue;
}
Common::String filename = (sectionIndex == -1) ? "GLOBAL.HAG" :
Common::String::format("SECTION%d.HAG", sectionIndex);
if (sectionIndex == 10) {
// Speech
if (!Common::File::exists("SPEECH.HAG"))
break;
else
filename = "SPEECH.HAG";
}
if (!hagFile.open(filename))
error("Could not locate HAG file - %s", filename.c_str());
// Check for header
char headerBuffer[16];
if ((hagFile.read(headerBuffer, 16) != 16) ||
(strncmp(headerBuffer, MADSCONCAT_STRING, 10) != 0))
error("Invalid HAG file opened");
// Scan through the HAG index
int numEntries = hagFile.readUint16LE();
HagIndex hagIndex;
hagIndex._filename = filename;
for (int idx = 0; idx < numEntries; ++idx) {
// Read in the details of the next resource
char resourceBuffer[14];
uint32 offset = hagFile.readUint32LE();
uint32 size = hagFile.readUint32LE();
hagFile.read(resourceBuffer, 14);
hagIndex._entries.push_back(HagEntry(resourceBuffer, offset, size));
}
hagFile.close();
_index.push_back(hagIndex);
}
}
bool HagArchive::getHeaderEntry(const Common::String &resourceName,
HagIndex &hagIndex, HagEntry &hagEntry) const {
Common::String resName = resourceName;
resName.toUppercase();
if (resName[0] == '*')
resName.deleteChar(0);
Common::String hagFilename = getResourceFilename(resName);
// Find the index for the given file
for (uint hagCtr = 0; hagCtr < _index.size(); ++hagCtr) {
hagIndex = _index[hagCtr];
if (hagIndex._filename == hagFilename) {
Common::List<HagEntry>::iterator ei;
for (ei = hagIndex._entries.begin(); ei != hagIndex._entries.end(); ++ei) {
hagEntry = *ei;
if (hagEntry._resourceName.compareToIgnoreCase(resName) == 0)
return true;
}
}
}
return false;
}
Common::String HagArchive::getResourceFilename(const Common::String &resourceName) const {
ResourceType resType = getResourceType(resourceName);
Common::String outputFilename = "GLOBAL.HAG";
if ((resType == RESTYPE_ROOM) || (resType == RESTYPE_SC)) {
int value = atoi(resourceName.c_str() + 2);
int hagFileNum = (resType == RESTYPE_ROOM) ? value / 100 : value;
if (hagFileNum > 0)
outputFilename = Common::String::format("SECTION%d.HAG", hagFileNum);
}
if (resType == RESTYPE_SPEECH)
outputFilename = "SPEECH.HAG";
return outputFilename;
}
ResourceType HagArchive::getResourceType(const Common::String &resourceName) const {
if (resourceName.hasPrefix("RM")) {
// Room resource
return RESTYPE_ROOM;
} else if (resourceName.hasPrefix("SC")) {
// SC resource
return RESTYPE_SC;
} else if (resourceName.hasSuffix(".TXT")) {
// Text resource
return RESTYPE_TEXT;
} else if (resourceName.hasSuffix(".QUO")) {
// QUO resource
return RESTYPE_QUO;
} else if (resourceName.hasPrefix("I")) {
// I resource
return RESTYPE_I;
} else if (resourceName.hasPrefix("OB")) {
// OB resource
return RESTYPE_OB;
} else if (resourceName.hasPrefix("FONT")) {
// FONT resource
return RESTYPE_FONT;
} else if (resourceName.hasPrefix("SOUND")) {
// SOUND resource
return RESTYPE_SOUND;
} else if (resourceName.hasPrefix("SPCHC")) {
// SPEECH resource
return RESTYPE_SPEECH;
}
// Check for a known extension
const char *extPos = strchr(resourceName.c_str(), '.');
if (extPos) {
++extPos;
if (!strcmp(extPos, "FL") || !strcmp(extPos, "LBM") || !strcmp(extPos, "ANM") ||
!strcmp(extPos, "AA") || !strcmp(extPos, "SS")) {
return RESTYPE_HAS_EXT;
}
}
return RESTYPE_NO_EXT;
}
/*------------------------------------------------------------------------*/
void Resources::init(MADSEngine *vm) {
SearchMan.add("HAG", new HagArchive(vm));
}
Common::String Resources::formatName(RESPREFIX resType, int id, const Common::String &ext) {
Common::String result = "*";
if (resType == 3 && !id) {
id = id / 100;
}
if (!ext.empty()) {
switch (resType) {
case RESPREFIX_GL:
result += "GL000";
break;
case RESPREFIX_SC:
result += Common::String::format("SC%.3d", id);
break;
case RESPREFIX_RM:
result += Common::String::format("RM%.3d", id);
break;
default:
break;
}
result += ext;
}
return result;
}
Common::String Resources::formatName(int prefix, char asciiCh, int id, EXTTYPE extType,
const Common::String &suffix) {
Common::String result;
if (prefix <= 0) {
result = "*";
} else {
result = Common::String::format("%s%.3d",
(prefix < 100) ? "*SC" : "*RM", prefix);
}
result += Common::String::format("%c", asciiCh);
if (id >= 0)
result += Common::String::format("%d", id);
if (!suffix.empty())
result += suffix;
switch (extType) {
case EXT_SS:
result += ".SS";
break;
case EXT_AA:
result += ".AA";
break;
case EXT_DAT:
result += ".DAT";
break;
case EXT_HH:
result += ".HH";
break;
case EXT_ART:
result += ".ART";
break;
case EXT_INT:
result += ".INT";
break;
default:
break;
}
return result;
}
Common::String Resources::formatResource(const Common::String &resName,
const Common::String &hagFilename) {
// int v1 = 0, v2 = 0;
if (resName.hasPrefix("*")) {
// Resource file specified
error("TODO: formatResource");
} else {
// File outside of hag file
return resName;
}
}
Common::String Resources::formatAAName(int idx) {
return formatName(0, 'I', idx, EXT_AA, "");
}
/*------------------------------------------------------------------------*/
void File::openFile(const Common::String &filename) {
if (!Common::File::open(filename))
error("Could not open file - %s", filename.c_str());
}
/*------------------------------------------------------------------------*/
void SynchronizedList::synchronize(Common::Serializer &s) {
int v = 0;
int count = size();
s.syncAsUint16LE(count);
if (s.isSaving()) {
for (int idx = 0; idx < count; ++idx) {
v = (*this)[idx];
s.syncAsSint32LE(v);
}
} else {
clear();
reserve(count);
for (int idx = 0; idx < count; ++idx) {
s.syncAsSint32LE(v);
push_back(v);
}
}
}
} // End of namespace MADS
|