2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2006-05-23 23:43:52 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2006-05-23 23:43:52 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2006-05-23 23:43:52 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2006-05-23 23:43:52 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
|
|
|
|
2006-05-23 23:43:52 +00:00
|
|
|
#include "agi/agi.h"
|
2016-01-29 12:13:40 +00:00
|
|
|
#include "agi/words.h"
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
namespace Agi {
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
int AgiLoader_v2::detectGame() {
|
2006-05-23 23:43:52 +00:00
|
|
|
if (!Common::File::exists(LOGDIR) ||
|
|
|
|
!Common::File::exists(PICDIR) ||
|
|
|
|
!Common::File::exists(SNDDIR) ||
|
|
|
|
!Common::File::exists(VIEWDIR))
|
2007-01-16 12:40:51 +00:00
|
|
|
return errInvalidAGIFile;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-06-06 17:45:52 +00:00
|
|
|
return _vm->setupV2Game(_vm->getVersion());
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
int AgiLoader_v2::loadDir(AgiDir *agid, const char *fname) {
|
2006-05-23 23:43:52 +00:00
|
|
|
Common::File fp;
|
|
|
|
uint8 *mem;
|
|
|
|
uint32 flen;
|
2006-05-24 19:51:37 +00:00
|
|
|
uint i;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2010-10-26 22:33:49 +00:00
|
|
|
debug(0, "Loading directory: %s", fname);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2006-05-24 19:51:37 +00:00
|
|
|
if (!fp.open(fname)) {
|
2007-01-16 12:40:51 +00:00
|
|
|
return errBadFileOpen;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fp.seek(0, SEEK_END);
|
|
|
|
flen = fp.pos();
|
|
|
|
fp.seek(0, SEEK_SET);
|
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
if ((mem = (uint8 *)malloc(flen + 32)) == NULL) {
|
2006-05-23 23:43:52 +00:00
|
|
|
fp.close();
|
2007-01-16 12:40:51 +00:00
|
|
|
return errNotEnoughMemory;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fp.read(mem, flen);
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// set all directory resources to gone
|
2016-01-29 12:13:40 +00:00
|
|
|
for (i = 0; i < MAX_DIRECTORY_ENTRIES; i++) {
|
2006-05-23 23:43:52 +00:00
|
|
|
agid[i].volume = 0xff;
|
|
|
|
agid[i].offset = _EMPTY;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// build directory entries
|
2006-05-23 23:43:52 +00:00
|
|
|
for (i = 0; i < flen; 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);
|
|
|
|
fp.close();
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
return errOK;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
int AgiLoader_v2::init() {
|
2007-01-16 12:40:51 +00:00
|
|
|
int ec = errOK;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// load directory files
|
2007-01-16 12:40:51 +00:00
|
|
|
ec = loadDir(_vm->_game.dirLogic, LOGDIR);
|
|
|
|
if (ec == errOK)
|
|
|
|
ec = loadDir(_vm->_game.dirPic, PICDIR);
|
|
|
|
if (ec == errOK)
|
|
|
|
ec = loadDir(_vm->_game.dirView, VIEWDIR);
|
|
|
|
if (ec == errOK)
|
|
|
|
ec = loadDir(_vm->_game.dirSound, SNDDIR);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2006-12-06 19:27:02 +00:00
|
|
|
int AgiLoader_v2::deinit() {
|
2007-01-16 12:40:51 +00:00
|
|
|
int ec = errOK;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
#if 0
|
2009-06-06 17:39:13 +00:00
|
|
|
// unload words
|
2007-01-16 12:40:51 +00:00
|
|
|
agiV2UnloadWords();
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// unload objects
|
2007-01-16 12:40:51 +00:00
|
|
|
agiV2UnloadObjects();
|
2006-05-23 23:43:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
int AgiLoader_v2::unloadResource(int16 resourceType, int16 resourceNr) {
|
2006-05-23 23:43:52 +00:00
|
|
|
debugC(3, kDebugLevelResources, "unload resource");
|
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
switch (resourceType) {
|
|
|
|
case RESOURCETYPE_LOGIC:
|
|
|
|
_vm->unloadLogic(resourceNr);
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
2016-01-29 12:13:40 +00:00
|
|
|
case RESOURCETYPE_PICTURE:
|
|
|
|
_vm->_picture->unloadPicture(resourceNr);
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
2016-01-29 12:13:40 +00:00
|
|
|
case RESOURCETYPE_VIEW:
|
|
|
|
_vm->unloadView(resourceNr);
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
2016-01-29 12:13:40 +00:00
|
|
|
case RESOURCETYPE_SOUND:
|
|
|
|
_vm->_sound->unloadSound(resourceNr);
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
return errOK;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
/**
|
2016-01-29 12:13:40 +00:00
|
|
|
* This function loads a raw resource into memory,
|
2006-05-23 23:43:52 +00:00
|
|
|
* if further decoding is required, it must be done by another
|
|
|
|
* routine. NULL is returned if unsucsessfull.
|
|
|
|
*/
|
2007-01-16 12:40:51 +00:00
|
|
|
uint8 *AgiLoader_v2::loadVolRes(struct AgiDir *agid) {
|
2006-05-23 23:43:52 +00:00
|
|
|
uint8 *data = NULL;
|
2014-12-22 21:46:52 +00:00
|
|
|
char x[6];
|
2006-05-23 23:43:52 +00:00
|
|
|
Common::File fp;
|
|
|
|
unsigned int sig;
|
2014-12-22 21:39:05 +00:00
|
|
|
Common::String path;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2014-12-22 21:39:05 +00:00
|
|
|
path = Common::String::format("vol.%i", agid->volume);
|
|
|
|
debugC(3, kDebugLevelResources, "Vol res: path = %s", path.c_str());
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
if (agid->offset != _EMPTY && fp.open(path)) {
|
|
|
|
debugC(3, kDebugLevelResources, "loading resource at offset %d", agid->offset);
|
|
|
|
fp.seek(agid->offset, SEEK_SET);
|
|
|
|
fp.read(&x, 5);
|
|
|
|
if ((sig = READ_BE_UINT16((uint8 *) x)) == 0x1234) {
|
|
|
|
agid->len = READ_LE_UINT16((uint8 *) x + 3);
|
2011-05-23 17:20:56 +00:00
|
|
|
data = (uint8 *)calloc(1, agid->len + 32);
|
2006-05-23 23:43:52 +00:00
|
|
|
if (data != NULL) {
|
|
|
|
fp.read(data, agid->len);
|
|
|
|
} else {
|
2011-05-23 17:20:56 +00:00
|
|
|
error("AgiLoader_v2::loadVolRes out of memory");
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-26 22:33:49 +00:00
|
|
|
warning("AgiLoader_v2::loadVolRes: bad signature %04x", sig);
|
2006-05-23 23:43:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fp.close();
|
|
|
|
} else {
|
2009-06-06 17:39:13 +00:00
|
|
|
// we have a bad volume resource
|
|
|
|
// set that resource to NA
|
2006-05-23 23:43:52 +00:00
|
|
|
agid->offset = _EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
/**
|
2006-05-23 23:43:52 +00:00
|
|
|
* Loads a resource into memory, a raw resource is loaded in
|
|
|
|
* with above routine, then further decoded here.
|
|
|
|
*/
|
2016-01-29 12:13:40 +00:00
|
|
|
int AgiLoader_v2::loadResource(int16 resourceType, int16 resourceNr) {
|
2007-01-16 12:40:51 +00:00
|
|
|
int ec = errOK;
|
2006-05-23 23:43:52 +00:00
|
|
|
uint8 *data = NULL;
|
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
debugC(3, kDebugLevelResources, "(t = %d, n = %d)", resourceType, resourceNr);
|
|
|
|
if (resourceNr >= MAX_DIRECTORY_ENTRIES)
|
2007-01-16 12:40:51 +00:00
|
|
|
return errBadResource;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
switch (resourceType) {
|
|
|
|
case RESOURCETYPE_LOGIC:
|
|
|
|
if (~_vm->_game.dirLogic[resourceNr].flags & RES_LOADED) {
|
|
|
|
debugC(3, kDebugLevelResources, "loading logic resource %d", resourceNr);
|
|
|
|
unloadResource(RESOURCETYPE_LOGIC, resourceNr);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// load raw resource into data
|
2016-01-29 12:13:40 +00:00
|
|
|
data = loadVolRes(&_vm->_game.dirLogic[resourceNr]);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
_vm->_game.logics[resourceNr].data = data;
|
|
|
|
ec = data ? _vm->decodeLogic(resourceNr) : errBadResource;
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
_vm->_game.logics[resourceNr].sIP = 2;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// if logic was cached, we get here
|
|
|
|
// reset code pointers incase it was cached
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
_vm->_game.logics[resourceNr].cIP = _vm->_game.logics[resourceNr].sIP;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
2016-01-29 12:13:40 +00:00
|
|
|
case RESOURCETYPE_PICTURE:
|
2009-06-06 17:39:13 +00:00
|
|
|
// if picture is currently NOT loaded *OR* cacheing is off,
|
|
|
|
// unload the resource (caching == off) and reload it
|
2006-05-23 23:43:52 +00:00
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
debugC(3, kDebugLevelResources, "loading picture resource %d", resourceNr);
|
|
|
|
if (_vm->_game.dirPic[resourceNr].flags & RES_LOADED)
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
|
2009-06-06 17:39:13 +00:00
|
|
|
// if loaded but not cached, unload it
|
|
|
|
// if cached but not loaded, etc
|
2016-01-29 12:13:40 +00:00
|
|
|
unloadResource(RESOURCETYPE_PICTURE, resourceNr);
|
|
|
|
data = loadVolRes(&_vm->_game.dirPic[resourceNr]);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
if (data != NULL) {
|
2016-01-29 12:13:40 +00:00
|
|
|
_vm->_game.pictures[resourceNr].rdata = data;
|
|
|
|
_vm->_game.dirPic[resourceNr].flags |= RES_LOADED;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-01-16 12:40:51 +00:00
|
|
|
ec = errBadResource;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-01-29 12:13:40 +00:00
|
|
|
case RESOURCETYPE_SOUND:
|
|
|
|
debugC(3, kDebugLevelResources, "loading sound resource %d", resourceNr);
|
|
|
|
if (_vm->_game.dirSound[resourceNr].flags & RES_LOADED)
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
data = loadVolRes(&_vm->_game.dirSound[resourceNr]);
|
2006-05-23 23:43:52 +00:00
|
|
|
|
|
|
|
if (data != NULL) {
|
2007-08-15 22:00:31 +00:00
|
|
|
// Freeing of the raw resource from memory is delegated to the createFromRawResource-function
|
2016-01-29 12:13:40 +00:00
|
|
|
_vm->_game.sounds[resourceNr] = AgiSound::createFromRawResource(data, _vm->_game.dirSound[resourceNr].len, resourceNr, _vm->_soundemu);
|
|
|
|
_vm->_game.dirSound[resourceNr].flags |= RES_LOADED;
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-01-16 12:40:51 +00:00
|
|
|
ec = errBadResource;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-01-29 12:13:40 +00:00
|
|
|
case RESOURCETYPE_VIEW:
|
2009-06-06 17:39:13 +00:00
|
|
|
// 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?
|
2016-01-29 12:13:40 +00:00
|
|
|
if (_vm->_game.dirView[resourceNr].flags & RES_LOADED)
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-29 12:13:40 +00:00
|
|
|
debugC(3, kDebugLevelResources, "loading view resource %d", resourceNr);
|
|
|
|
unloadResource(RESOURCETYPE_VIEW, resourceNr);
|
|
|
|
data = loadVolRes(&_vm->_game.dirView[resourceNr]);
|
2006-05-23 23:43:52 +00:00
|
|
|
if (data) {
|
2016-01-29 12:13:40 +00:00
|
|
|
_vm->_game.dirView[resourceNr].flags |= RES_LOADED;
|
|
|
|
ec = _vm->decodeView(data, _vm->_game.dirView[resourceNr].len, resourceNr);
|
|
|
|
free(data);
|
2006-05-23 23:43:52 +00:00
|
|
|
} else {
|
2007-01-16 12:40:51 +00:00
|
|
|
ec = errBadResource;
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2007-01-16 12:40:51 +00:00
|
|
|
ec = errBadResource;
|
2006-05-23 23:43:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
int AgiLoader_v2::loadObjects(const char *fname) {
|
|
|
|
return _vm->loadObjects(fname);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
int AgiLoader_v2::loadWords(const char *fname) {
|
2016-01-29 12:13:40 +00:00
|
|
|
return _vm->_words->loadDictionary(fname);
|
2006-05-23 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:40:51 +00:00
|
|
|
} // End of namespace Agi
|