File: loader_v3.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (196 lines) | stat: -rw-r--r-- 5,772 bytes parent folder | download
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

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

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

namespace Agi {

void AgiLoader_v3::init() {
	// Find the game's name by locating a file that ends in "dir".
	// Amiga games don't use the game's name as a prefix.
	_name.clear();
	Common::FSList fslist;
	Common::FSNode dir(ConfMan.getPath("path"));

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

	for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
		Common::String fileName = file->getName();
		if (fileName.size() > 3 && fileName.hasSuffixIgnoreCase("dir")) {
			_name = fileName.substr(0, fileName.size() - 3);
			_name.toLowercase();
			debugC(3, kDebugLevelResources, "game name: %s", _name.c_str());
			break;
		}
	}
}

int AgiLoader_v3::loadDir(AgiDir *agid, Common::File *fp, uint32 offs, uint32 len) {
	fp->seek(offs, SEEK_SET);
	uint8 *mem = (uint8 *)malloc(len);
	if (mem == nullptr) {
		return errNotEnoughMemory;
	}

	fp->read(mem, len);

	// read directory entries
	for (uint32 i = 0; i + 2 < len; i += 3) {
		agid[i / 3].volume = *(mem + i) >> 4;
		agid[i / 3].offset = READ_BE_UINT24(mem + i) & (uint32) _EMPTY;
		debugC(3, kDebugLevelResources, "%d: volume %d, offset 0x%05x", i / 3, agid[i / 3].volume, agid[i / 3].offset);
	}	

	free(mem);
	return errOK;
}

int AgiLoader_v3::loadDirs() {
	int ec = errOK;
	uint8 fileHeader[8];
	Common::File fp;
	Common::Path path;

	if (_vm->getPlatform() == Common::kPlatformAmiga) {
		// Amiga directory file is always named "dirs"
		path = Common::Path("dirs");
	} else {
		if (_name.empty()) {
			warning("AgiLoader_v3: directory file not found");
			return errBadResource;
		}
		path = Common::Path(_name + DIR_);
	}

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

	uint16 dirOffsets[4];
	for (int i = 0; i < 4; i++)
		dirOffsets[i] = READ_LE_UINT16(&fileHeader[i * 2]);

	uint32 dirLengths[4];
	dirLengths[0] = dirOffsets[1] - dirOffsets[0];
	dirLengths[1] = dirOffsets[2] - dirOffsets[1];
	dirLengths[2] = dirOffsets[3] - dirOffsets[2];
	dirLengths[3] = fp.pos() - dirOffsets[3];

	if (dirLengths[3] > 256 * 3)
		dirLengths[3] = 256 * 3;

	fp.seek(0, SEEK_SET);

	// read in directory files
	ec = loadDir(_vm->_game.dirLogic, &fp, dirOffsets[0], dirLengths[0]);
	if (ec == errOK)
		ec = loadDir(_vm->_game.dirPic, &fp, dirOffsets[1], dirLengths[1]);
	if (ec == errOK)
		ec = loadDir(_vm->_game.dirView, &fp, dirOffsets[2], dirLengths[2]);
	if (ec == errOK)
		ec = loadDir(_vm->_game.dirSound, &fp, dirOffsets[3], dirLengths[3]);

	return ec;
}

/**
 * This function loads a raw resource into memory.
 * If further decoding is required, it must be done by another
 * routine.
 *
 * NULL is returned if unsuccessful.
 */
uint8 *AgiLoader_v3::loadVolumeResource(AgiDir *agid) {
	uint8 volumeHeader[7];
	uint8 *data = nullptr;
	Common::File fp;
	Common::Path path;

	debugC(3, kDebugLevelResources, "(%p)", (void *)agid);
	if (_vm->getPlatform() == Common::kPlatformMacintosh) {
		path = Common::String::format("vol.%i", agid->volume);
	} else {
		path = Common::String::format("%svol.%i", _name.c_str(), agid->volume);
	}

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

		uint16 signature = READ_BE_UINT16(volumeHeader);
		if (signature != 0x1234) {
			warning("AgiLoader_v3::loadVolRes: bad signature %04x", signature);
			return nullptr;
		}

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

		uint8 *compBuffer = (uint8 *)calloc(1, agid->clen + 32); // why the extra 32 bytes?
		fp.read(compBuffer, agid->clen);

		if (volumeHeader[2] & 0x80) { // compressed pic
			// effectively uncompressed, but having only 4-bit parameters for F0 / F2 commands
			// Manhunter 2 uses such pictures
			data = compBuffer;
			agid->flags |= RES_PICTURE_V3_NIBBLE_PARM;
		} else if (agid->len == agid->clen) {
			// do not decompress
			data = compBuffer;
		} else {
			// it is compressed
			data = (uint8 *)calloc(1, agid->len + 32); // why the extra 32 bytes?
			lzwExpand(compBuffer, data, agid->len);
			free(compBuffer);
			agid->flags |= RES_COMPRESSED;
		}
	} else {
		// we have a bad volume resource
		// set that resource to NA
		agid->offset = _EMPTY;
	}

	return data;
}

int AgiLoader_v3::loadObjects() {
	return _vm->loadObjects(OBJECTS);
}

int AgiLoader_v3::loadWords() {
	return _vm->_words->loadDictionary(WORDS);
}

} // End of namespace Agi