aboutsummaryrefslogtreecommitdiff
path: root/engines/agi/loader_v3.cpp
blob: 4ee35d73eb54ec34d6ec5085804a8a137b9352b7 (plain)
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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "agi/agi.h"
#include "agi/lzw.h"

#include "common/config-manager.h"
#include "common/fs.h"

namespace Agi {

int AgiLoader_v3::detectGame() {
	int ec = errUnk;
	bool found = false;

	Common::FSList fslist;
	Common::FSNode dir(ConfMan.get("path"));

	if (!dir.getChildren(fslist, Common::FSNode::kListFilesOnly)) {
		warning("AgiEngine: invalid game path '%s'", dir.getPath().c_str());
		return errInvalidAGIFile;
	}

	for (Common::FSList::const_iterator file = fslist.begin();
	    file != fslist.end() && !found; ++file) {
		Common::String f = file->getName();
		f.toLowercase();

		if (f.hasSuffix("vol.0")) {
			memset(_vm->_game.name, 0, 8);
			strncpy(_vm->_game.name, f.c_str(), MIN((uint)8, f.size() > 5 ? f.size() - 5 : f.size()));
			debugC(3, kDebugLevelMain, "game.name = %s", _vm->_game.name);

			ec = _vm->setupV3Game(_vm->getVersion());

			found = true;
		}
	}

	if (!found) {
		debugC(3, kDebugLevelMain, "not found");
		ec = errInvalidAGIFile;
	}

	return ec;
}

int AgiLoader_v3::loadDir(struct AgiDir *agid, Common::File *fp,
						   uint32 offs, uint32 len) {
	int ec = errOK;
	uint8 *mem;
	unsigned int i;

	fp->seek(offs, SEEK_SET);
	if ((mem = (uint8 *)malloc(len + 32)) != NULL) {
		fp->read(mem, len);

		// set all directory resources to gone
		for (i = 0; i < MAX_DIRS; i++) {
			agid[i].volume = 0xff;
			agid[i].offset = _EMPTY;
		}

		// build directory entries
		for (i = 0; i < len; i += 3) {
			agid[i / 3].volume = *(mem + i) >> 4;
			agid[i / 3].offset = READ_BE_UINT24(mem + i) & (uint32) _EMPTY;
		}

		free(mem);
	} else {
		ec = errNotEnoughMemory;
	}

	return ec;
}

struct agi3vol {
	uint32 sddr;
	uint32 len;
};

int AgiLoader_v3::init() {
	int ec = errOK;
	struct agi3vol agiVol3[4];
	int i;
	uint16 xd[4];
	Common::File fp;
	Common::String path;

	if (_vm->getPlatform() == Common::kPlatformAmiga) {
		path = Common::String("dirs");
		_vm->_game.name[0] = 0; // Empty prefix
	} else if (_vm->getFeatures() & GF_MACGOLDRUSH) {
		path = "grdir";
		_vm->_game.name[0] = 0; // Empty prefix
	} else {
		path = Common::String(_vm->_game.name) + DIR_;
	}

	if (!fp.open(path)) {
		printf("Failed to open \"%s\"\n", path.c_str());
		return errBadFileOpen;
	}
	// build offset table for v3 directory format
	fp.read(&xd, 8);
	fp.seek(0, SEEK_END);

	for (i = 0; i < 4; i++)
		agiVol3[i].sddr = READ_LE_UINT16((uint8 *) & xd[i]);

	agiVol3[0].len = agiVol3[1].sddr - agiVol3[0].sddr;
	agiVol3[1].len = agiVol3[2].sddr - agiVol3[1].sddr;
	agiVol3[2].len = agiVol3[3].sddr - agiVol3[2].sddr;
	agiVol3[3].len = fp.pos() - agiVol3[3].sddr;

	if (agiVol3[3].len > 256 * 3)
		agiVol3[3].len = 256 * 3;

	fp.seek(0, SEEK_SET);

	// read in directory files
	ec = loadDir(_vm->_game.dirLogic, &fp, agiVol3[0].sddr, agiVol3[0].len);

	if (ec == errOK) {
		ec = loadDir(_vm->_game.dirPic, &fp, agiVol3[1].sddr, agiVol3[1].len);
	}

	if (ec == errOK) {
		ec = loadDir(_vm->_game.dirView, &fp, agiVol3[2].sddr, agiVol3[2].len);
	}

	if (ec == errOK) {
		ec = loadDir(_vm->_game.dirSound, &fp, agiVol3[3].sddr, agiVol3[3].len);
	}

	return ec;
}

int AgiLoader_v3::deinit() {
	int ec = errOK;

#if 0
	// unload words
	agiV3UnloadWords();

	// unload objects
	agiV3UnloadObjects();
#endif

	return ec;
}

int AgiLoader_v3::unloadResource(int t, int n) {
	switch (t) {
	case rLOGIC:
		_vm->unloadLogic(n);
		break;
	case rPICTURE:
		_vm->_picture->unloadPicture(n);
		break;
	case rVIEW:
		_vm->unloadView(n);
		break;
	case rSOUND:
		_vm->_sound->unloadSound(n);
		break;
	}

	return errOK;
}

/**
 * This function does noting but load a raw resource into memory.
 * If further decoding is required, it must be done by another
 * routine.
 *
 * NULL is returned if unsucsessful.
 */
uint8 *AgiLoader_v3::loadVolRes(AgiDir *agid) {
	char x[MAXPATHLEN];
	uint8 *data = NULL, *compBuffer;
	Common::File fp;
	Common::String path;

	debugC(3, kDebugLevelResources, "(%p)", (void *)agid);
	sprintf(x, "vol.%i", agid->volume);
	path = Common::String(_vm->_game.name) + x;

	if (agid->offset != _EMPTY && fp.open(path)) {
		fp.seek(agid->offset, SEEK_SET);
		fp.read(&x, 7);

		if (READ_BE_UINT16((uint8 *) x) != 0x1234) {
			debugC(3, kDebugLevelResources, "path = %s", path.c_str());
			debugC(3, kDebugLevelResources, "offset = %d", agid->offset);
			debugC(3, kDebugLevelResources, "x = %x %x", x[0], x[1]);
			error("ACK! BAD RESOURCE");
			_vm->quitGame();	// for compilers that don't support NORETURN
		}

		agid->len = READ_LE_UINT16((uint8 *) x + 3);	// uncompressed size
		agid->clen = READ_LE_UINT16((uint8 *) x + 5);	// compressed len

		compBuffer = (uint8 *)calloc(1, agid->clen + 32);
		fp.read(compBuffer, agid->clen);

		if (x[2] & 0x80) { // compressed pic
			data = _vm->_picture->convertV3Pic(compBuffer, agid->clen);
			free(compBuffer);
		} else if (agid->len == agid->clen) {
			// do not decompress
			data = compBuffer;
		} else {
			// it is compressed
			data = (uint8 *)calloc(1, agid->len + 32);
			lzwExpand(compBuffer, data, agid->len);
			free(compBuffer);
			agid->flags |= RES_COMPRESSED;
		}

		fp.close();
	} else {
		// we have a bad volume resource
		// set that resource to NA
		agid->offset = _EMPTY;
	}

	return data;
}

/**
 * Loads a resource into memory, a raw resource is loaded in
 * with above routine, then further decoded here.
 */
int AgiLoader_v3::loadResource(int t, int n) {
	int ec = errOK;
	uint8 *data = NULL;

	if (n > MAX_DIRS)
		return errBadResource;

	switch (t) {
	case rLOGIC:
		// load resource into memory, decrypt messages at the end
		// and build the message list (if logic is in memory)
		if (~_vm->_game.dirLogic[n].flags & RES_LOADED) {
			// if logic is already in memory, unload it
			unloadResource(rLOGIC, n);

			// load raw resource into data
			data = loadVolRes(&_vm->_game.dirLogic[n]);
			_vm->_game.logics[n].data = data;

			// uncompressed logic files need to be decrypted
			if (data != NULL) {
				// resloaded flag gets set by decode logic
				// needed to build string table
				ec = _vm->decodeLogic(n);
				_vm->_game.logics[n].sIP = 2;
			} else {
				ec = errBadResource;
			}

			// logics[n].sIP=2; // saved IP = 2
			// logics[n].cIP=2; // current IP = 2

			_vm->_game.logics[n].cIP = _vm->_game.logics[n].sIP;
		}

		// if logic was cached, we get here
		// reset code pointers incase it was cached

		_vm->_game.logics[n].cIP = _vm->_game.logics[n].sIP;
		break;
	case rPICTURE:
		// if picture is currently NOT loaded *OR* cacheing is off,
		// unload the resource (caching==off) and reload it
		if (~_vm->_game.dirPic[n].flags & RES_LOADED) {
			unloadResource(rPICTURE, n);
			data = loadVolRes(&_vm->_game.dirPic[n]);
			if (data != NULL) {
				_vm->_game.pictures[n].rdata = data;
				_vm->_game.dirPic[n].flags |= RES_LOADED;
			} else {
				ec = errBadResource;
			}
		}
		break;
	case rSOUND:
		if (_vm->_game.dirSound[n].flags & RES_LOADED)
			break;

		data = loadVolRes(&_vm->_game.dirSound[n]);
		if (data != NULL) {
			// Freeing of the raw resource from memory is delegated to the createFromRawResource-function
			_vm->_game.sounds[n] = AgiSound::createFromRawResource(data, _vm->_game.dirSound[n].len, n, *_vm->_sound, _vm->_soundemu);
			_vm->_game.dirSound[n].flags |= RES_LOADED;
		} else {
			ec = errBadResource;
		}
		break;
	case rVIEW:
		// Load a VIEW resource into memory...
		// Since VIEWS alter the view table ALL the time can we
		// cache the view? or must we reload it all the time?
		//
		// load a raw view from a VOL file into data
		if (_vm->_game.dirView[n].flags & RES_LOADED)
			break;

		unloadResource(rVIEW, n);
		data = loadVolRes(&_vm->_game.dirView[n]);
		if (data != NULL) {
			_vm->_game.views[n].rdata = data;
			_vm->_game.dirView[n].flags |= RES_LOADED;
			ec = _vm->decodeView(n);
		} else {
			ec = errBadResource;
		}
		break;
	default:
		ec = errBadResource;
		break;
	}

	return ec;
}

int AgiLoader_v3::loadObjects(const char *fname) {
	return _vm->loadObjects(fname);
}

int AgiLoader_v3::loadWords(const char *fname) {
	return _vm->loadWords(fname);
}

} // End of namespace Agi