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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* 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 "gui/error.h"
#include "engines/grim/resource.h"
#include "engines/grim/colormap.h"
#include "engines/grim/costume.h"
#include "engines/grim/keyframe.h"
#include "engines/grim/material.h"
#include "engines/grim/grim.h"
#include "engines/grim/lipsync.h"
#include "engines/grim/savegame.h"
#include "engines/grim/lab.h"
#include "engines/grim/bitmap.h"
#include "engines/grim/font.h"
#include "engines/grim/model.h"
#include "engines/grim/sprite.h"
#include "engines/grim/inputdialog.h"
#include "engines/grim/debug.h"
#include "engines/grim/emi/animationemi.h"
#include "engines/grim/emi/costumeemi.h"
#include "engines/grim/emi/modelemi.h"
#include "engines/grim/emi/skeleton.h"
#include "engines/grim/patchr.h"
#include "engines/grim/md5check.h"
#include "engines/grim/update/update.h"
#include "common/algorithm.h"
#include "common/zlib.h"
#include "common/memstream.h"
#include "common/file.h"
#include "common/config-manager.h"
#include "common/translation.h"
namespace Grim {
ResourceLoader *g_resourceloader = nullptr;
class LabListComperator {
const Common::String _labName;
public:
LabListComperator() {}
LabListComperator(const Common::String &ln) : _labName(ln) {}
bool operator()(const Common::ArchiveMemberPtr &l) {
return _labName.compareToIgnoreCase(l->getName()) == 0;
}
bool operator()(const Common::ArchiveMemberPtr &l, const Common::ArchiveMemberPtr &r) {
return (l->getName().compareToIgnoreCase(r->getName()) > 0);
}
};
ResourceLoader::ResourceLoader() {
_cacheDirty = false;
_cacheMemorySize = 0;
Lab *l;
Common::ArchiveMemberList files, updFiles;
//Load the update from the executable, if needed
const char *updateFilename = g_grim->getUpdateFilename();
if (updateFilename) {
Common::File *updStream = new Common::File();
if (updStream && updStream->open(updateFilename)) {
Common::Archive *update = loadUpdateArchive(updStream);
if (update)
SearchMan.add("update", update, 1);
} else
delete updStream;
// Check if the update has been correctly loaded
if (!SearchMan.hasArchive("update")) {
const char *errorMessage = nullptr;
if (g_grim->getGameType() == GType_GRIM) {
errorMessage = _("The original patch of Grim Fandango\n"
"is missing. Please download it from\n"
"http://www.residualvm.org/downloads/\n"
"and put it in the game data files directory");
} else if (g_grim->getGameType() == GType_MONKEY4) {
errorMessage = _("The original patch of Escape from Monkey Island is missing. \n"
"Please download it from http://www.residualvm.org/downloads/\n"
"and put it in the game data files directory.\n"
"Pay attention to download the correct version according to the game's language");
}
GUI::displayErrorDialog(errorMessage);
error("%s not found", updateFilename);
}
}
if (g_grim->getGameType() == GType_GRIM) {
if (g_grim->getGameFlags() & ADGF_DEMO) {
SearchMan.listMatchingMembers(files, "gfdemo01.lab");
SearchMan.listMatchingMembers(files, "gdemo001.lab"); // For the english demo with video.
SearchMan.listMatchingMembers(files, "grimdemo.mus");
SearchMan.listMatchingMembers(files, "sound001.lab");
SearchMan.listMatchingMembers(files, "voice001.lab");
} else {
if (!SearchMan.hasFile("residualvm-grim-patch.lab"))
error("%s", _("residualvm-grim-patch.lab not found"));
SearchMan.listMatchingMembers(files, "residualvm-grim-patch.lab");
SearchMan.listMatchingMembers(files, "data005.lab");
SearchMan.listMatchingMembers(files, "data004.lab");
SearchMan.listMatchingMembers(files, "data003.lab");
SearchMan.listMatchingMembers(files, "data002.lab");
SearchMan.listMatchingMembers(files, "data001.lab");
SearchMan.listMatchingMembers(files, "data000.lab");
SearchMan.listMatchingMembers(files, "movie??.lab");
SearchMan.listMatchingMembers(files, "vox????.lab");
SearchMan.listMatchingMembers(files, "year?mus.lab");
SearchMan.listMatchingMembers(files, "local.lab");
SearchMan.listMatchingMembers(files, "credits.lab");
//Sort the archives in order to ensure that they are loaded with the correct order
Common::sort(files.begin(), files.end(), LabListComperator());
//Check the presence of datausr.lab and if the user wants to load it.
//In this case put it in the top of the list
const char *datausr_name = "datausr.lab";
if (SearchMan.hasFile(datausr_name) && ConfMan.getBool("datausr_load")) {
warning("%s", _("Loading datausr.lab. Please note that the ResidualVM-team doesn't provide support for using such patches"));
files.push_front(SearchMan.getMember(datausr_name));
}
}
} else if (g_grim->getGameType() == GType_MONKEY4) {
const char *emi_patches_filename = "residualvm-emi-patch.m4b";
if (!SearchMan.hasFile(emi_patches_filename))
error(_("%s not found"), emi_patches_filename);
SearchMan.listMatchingMembers(files, emi_patches_filename);
if (g_grim->getGameFlags() & ADGF_DEMO) {
SearchMan.listMatchingMembers(files, "i9n.lab");
SearchMan.listMatchingMembers(files, "lip.lab");
SearchMan.listMatchingMembers(files, "MagDemo.lab");
SearchMan.listMatchingMembers(files, "tile.lab");
SearchMan.listMatchingMembers(files, "voice.lab");
} else {
//Keep i9n.m4b before patch.m4b for a better efficiency
//in decompressing from Monkey Update.exe
SearchMan.listMatchingMembers(files, "i9n.m4b");
SearchMan.listMatchingMembers(files, "patch.m4b");
SearchMan.listMatchingMembers(files, "art???.m4b");
SearchMan.listMatchingMembers(files, "lip.m4b");
SearchMan.listMatchingMembers(files, "local.m4b");
SearchMan.listMatchingMembers(files, "sfx.m4b");
SearchMan.listMatchingMembers(files, "voice???.m4b");
SearchMan.listMatchingMembers(files, "music?.m4b");
if (g_grim->getGamePlatform() == Common::kPlatformPS2) {
SearchMan.listMatchingMembers(files, "???.m4b");
}
//Check the presence of datausr.m4b and if the user wants to load it.
//In this case put it in the top of the list
const char *datausr_name = "datausr.m4b";
if (SearchMan.hasFile(datausr_name) && ConfMan.getBool("datausr_load")) {
warning("%s", _("Loading datausr.m4b. Please note that the ResidualVM-team doesn't provide support for using such patches"));
files.push_front(SearchMan.getMember(datausr_name));
}
}
}
if (files.empty())
error("%s", _("Cannot find game data - check configuration file"));
//load labs
int priority = files.size();
for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
Common::String filename = (*x)->getName();
filename.toLowercase();
//Avoid duplicates
if (SearchMan.hasArchive(filename))
continue;
l = new Lab();
// Caching "local.m4b" to speed up the launch of the mac version,
// we _COULD_ protect this with a platform check, but the file isn't
// really big anyhow...
bool useCache = (filename == "local.m4b");
if (l->open(filename, useCache))
SearchMan.add(filename, l, priority--, true);
else
delete l;
}
files.clear();
}
template<typename T>
void clearList(Common::List<T> &list) {
while (!list.empty()) {
T p = list.front();
list.erase(list.begin());
delete p;
}
}
ResourceLoader::~ResourceLoader() {
for (Common::Array<ResourceCache>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
ResourceCache &r = *i;
delete[] r.fname;
delete[] r.resPtr;
}
clearList(_models);
clearList(_colormaps);
clearList(_keyframeAnims);
clearList(_lipsyncs);
MD5Check::clear();
}
static int sortCallback(const void *entry1, const void *entry2) {
return scumm_stricmp(((const ResourceLoader::ResourceCache *)entry1)->fname, ((const ResourceLoader::ResourceCache *)entry2)->fname);
}
Common::SeekableReadStream *ResourceLoader::getFileFromCache(const Common::String &filename) const {
ResourceLoader::ResourceCache *entry = getEntryFromCache(filename);
if (!entry)
return nullptr;
return new Common::MemoryReadStream(entry->resPtr, entry->len);
}
ResourceLoader::ResourceCache *ResourceLoader::getEntryFromCache(const Common::String &filename) const {
if (_cache.empty())
return nullptr;
if (_cacheDirty) {
qsort(_cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
_cacheDirty = false;
}
ResourceCache key;
key.fname = const_cast<char *>(filename.c_str());
return (ResourceLoader::ResourceCache *)bsearch(&key, _cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
}
Common::SeekableReadStream *ResourceLoader::loadFile(const Common::String &filename) const {
Common::SeekableReadStream *rs = nullptr;
if (SearchMan.hasFile(filename))
rs = SearchMan.createReadStreamForMember(filename);
else
return nullptr;
rs = wrapPatchedFile(rs, filename);
return rs;
}
Common::SeekableReadStream *ResourceLoader::openNewStreamFile(Common::String fname, bool cache) const {
Common::SeekableReadStream *s;
fname.toLowercase();
if (cache) {
s = getFileFromCache(fname);
if (!s) {
s = loadFile(fname);
if (!s)
return nullptr;
uint32 size = s->size();
byte *buf = new byte[size];
s->read(buf, size);
putIntoCache(fname, buf, size);
delete s;
s = new Common::MemoryReadStream(buf, size);
}
} else {
s = loadFile(fname);
}
// This will only have an effect if the stream is actually compressed.
return Common::wrapCompressedReadStream(s);
}
void ResourceLoader::putIntoCache(const Common::String &fname, byte *res, uint32 len) const {
ResourceCache entry;
entry.resPtr = res;
entry.len = len;
entry.fname = new char[fname.size() + 1];
strcpy(entry.fname, fname.c_str());
_cacheMemorySize += len;
_cache.push_back(entry);
_cacheDirty = true;
}
CMap *ResourceLoader::loadColormap(const Common::String &filename) {
Common::SeekableReadStream *stream = openNewStreamFile(filename.c_str());
if (!stream) {
error("Could not find colormap %s", filename.c_str());
}
CMap *result = new CMap(filename, stream);
_colormaps.push_back(result);
delete stream;
return result;
}
Common::String ResourceLoader::fixFilename(const Common::String &filename, bool append) {
Common::String fname(filename);
if (g_grim->getGameType() == GType_MONKEY4) {
int len = fname.size();
for (int i = 0; i < len; i++) {
if (fname[i] == '\\') {
fname.setChar('/', i);
}
}
// Append b to end of filename for EMI
if (append)
fname += "b";
}
return fname;
}
Costume *ResourceLoader::loadCostume(const Common::String &filename, Actor *owner, Costume *prevCost) {
Common::String fname = fixFilename(filename);
fname.toLowercase();
Common::SeekableReadStream *stream = openNewStreamFile(fname.c_str(), true);
if (!stream) {
error("Could not find costume \"%s\"", filename.c_str());
}
Costume *result;
if (g_grim->getGameType() == GType_MONKEY4) {
result = new EMICostume(filename, owner, prevCost);
} else {
result = new Costume(filename, owner, prevCost);
}
result->load(stream);
delete stream;
return result;
}
Font *ResourceLoader::loadFont(const Common::String &filename) {
Common::SeekableReadStream *stream;
stream = openNewStreamFile(filename.c_str(), true);
if (!stream)
error("Could not find font file %s", filename.c_str());
Font *result = new Font();
result->load(filename, stream);
delete stream;
return result;
}
KeyframeAnim *ResourceLoader::loadKeyframe(const Common::String &filename) {
Common::SeekableReadStream *stream;
stream = openNewStreamFile(filename.c_str());
if (!stream)
error("Could not find keyframe file %s", filename.c_str());
KeyframeAnim *result = new KeyframeAnim(filename, stream);
_keyframeAnims.push_back(result);
delete stream;
return result;
}
LipSync *ResourceLoader::loadLipSync(const Common::String &filename) {
LipSync *result;
Common::SeekableReadStream *stream;
stream = openNewStreamFile(filename.c_str());
if (!stream)
return nullptr;
result = new LipSync(filename, stream);
// Some lipsync files have no data
if (result->isValid())
_lipsyncs.push_back(result);
else {
delete result;
result = nullptr;
}
delete stream;
return result;
}
Material *ResourceLoader::loadMaterial(const Common::String &filename, CMap *c, bool clamp) {
Common::String fname = fixFilename(filename, false);
fname.toLowercase();
Common::SeekableReadStream *stream;
stream = openNewStreamFile(fname.c_str(), true);
if (!stream && !filename.hasPrefix("specialty")) {
// FIXME: EMI demo references files that aren't included. Return a known material.
// This should be fixed in the data files instead.
if (g_grim->getGameType() == GType_MONKEY4 && g_grim->getGameFlags() & ADGF_DEMO) {
const Common::String replacement("fx/candle.sprb");
warning("Could not find material %s, using %s instead", filename.c_str(), replacement.c_str());
return loadMaterial(replacement, nullptr, clamp);
} else {
error("Could not find material %s", filename.c_str());
}
}
Material *result = new Material(fname, stream, c, clamp);
delete stream;
return result;
}
Model *ResourceLoader::loadModel(const Common::String &filename, CMap *c, Model *parent) {
Common::String fname = fixFilename(filename);
Common::SeekableReadStream *stream;
stream = openNewStreamFile(fname.c_str());
if (!stream)
error("Could not find model %s", filename.c_str());
Model *result = new Model(filename, stream, c, parent);
_models.push_back(result);
delete stream;
return result;
}
EMIModel *ResourceLoader::loadModelEMI(const Common::String &filename, EMICostume *costume) {
Common::String fname = fixFilename(filename);
Common::SeekableReadStream *stream;
stream = openNewStreamFile(fname.c_str());
if (!stream) {
warning("Could not find model %s", filename.c_str());
return nullptr;
}
EMIModel *result = new EMIModel(filename, stream, costume);
_emiModels.push_back(result);
delete stream;
return result;
}
Skeleton *ResourceLoader::loadSkeleton(const Common::String &filename) {
Common::String fname = fixFilename(filename);
Common::SeekableReadStream *stream;
stream = openNewStreamFile(fname.c_str(), true);
if (!stream) {
warning("Could not find skeleton %s", filename.c_str());
return nullptr;
}
Skeleton *result = new Skeleton(filename, stream);
delete stream;
return result;
}
Sprite *ResourceLoader::loadSprite(const Common::String &filename, EMICostume *costume) {
assert(g_grim->getGameType() == GType_MONKEY4);
Common::SeekableReadStream *stream;
const Common::String fname = fixFilename(filename, true);
stream = openNewStreamFile(fname.c_str(), true);
if (!stream) {
warning("Could not find sprite %s", fname.c_str());
return nullptr;
}
Sprite *result = new Sprite();
result->loadBinary(stream, costume);
delete stream;
return result;
}
AnimationEmi *ResourceLoader::loadAnimationEmi(const Common::String &filename) {
Common::String fname = fixFilename(filename);
Common::SeekableReadStream *stream;
stream = openNewStreamFile(fname.c_str(), true);
if (!stream) {
warning("Could not find animation %s", filename.c_str());
return nullptr;
}
AnimationEmi *result = new AnimationEmi(filename, stream);
_emiAnims.push_back(result);
delete stream;
return result;
}
void ResourceLoader::uncache(const char *filename) const {
Common::String fname = filename;
fname.toLowercase();
if (_cacheDirty) {
qsort(_cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
_cacheDirty = false;
}
for (unsigned int i = 0; i < _cache.size(); i++) {
if (fname.compareTo(_cache[i].fname) == 0) {
delete[] _cache[i].fname;
_cacheMemorySize -= _cache[i].len;
delete[] _cache[i].resPtr;
_cache.remove_at(i);
_cacheDirty = true;
}
}
}
void ResourceLoader::uncacheModel(Model *m) {
_models.remove(m);
}
void ResourceLoader::uncacheColormap(CMap *c) {
_colormaps.remove(c);
}
void ResourceLoader::uncacheKeyframe(KeyframeAnim *k) {
_keyframeAnims.remove(k);
}
void ResourceLoader::uncacheLipSync(LipSync *s) {
_lipsyncs.remove(s);
}
void ResourceLoader::uncacheAnimationEmi(AnimationEmi *a) {
_emiAnims.remove(a);
}
ModelPtr ResourceLoader::getModel(const Common::String &fname, CMap *c) {
Common::String filename = fname;
filename.toLowercase();
for (Common::List<Model *>::const_iterator i = _models.begin(); i != _models.end(); ++i) {
Model *m = *i;
if (filename == m->getFilename() && *m->getCMap() == *c) {
return m;
}
}
return loadModel(fname, c);
}
CMapPtr ResourceLoader::getColormap(const Common::String &fname) {
Common::String filename = fname;
filename.toLowercase();
for (Common::List<CMap *>::const_iterator i = _colormaps.begin(); i != _colormaps.end(); ++i) {
CMap *c = *i;
if (filename.equals(c->_fname)) {
return c;
}
}
return loadColormap(fname);
}
KeyframeAnimPtr ResourceLoader::getKeyframe(const Common::String &fname) {
Common::String filename = fname;
filename.toLowercase();
for (Common::List<KeyframeAnim *>::const_iterator i = _keyframeAnims.begin(); i != _keyframeAnims.end(); ++i) {
KeyframeAnim *k = *i;
if (filename == k->getFilename()) {
return k;
}
}
return loadKeyframe(fname);
}
LipSyncPtr ResourceLoader::getLipSync(const Common::String &fname) {
Common::String filename = fname;
filename.toLowercase();
for (Common::List<LipSync *>::const_iterator i = _lipsyncs.begin(); i != _lipsyncs.end(); ++i) {
LipSync *l = *i;
if (filename == l->getFilename()) {
return l;
}
}
return loadLipSync(fname);
}
AnimationEmiPtr ResourceLoader::getAnimationEmi(const Common::String &fname) {
Common::String filename = fname;
filename.toLowercase();
for (Common::List<AnimationEmi *>::const_iterator i = _emiAnims.begin(); i != _emiAnims.end(); ++i) {
AnimationEmi *a = *i;
if (filename == a->getFilename()) {
return a;
}
}
return loadAnimationEmi(fname);
}
} // end of namespace Grim
|