ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

resource.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "gui/error.h"
00024 
00025 #include "engines/grim/resource.h"
00026 #include "engines/grim/colormap.h"
00027 #include "engines/grim/costume.h"
00028 #include "engines/grim/keyframe.h"
00029 #include "engines/grim/material.h"
00030 #include "engines/grim/grim.h"
00031 #include "engines/grim/lipsync.h"
00032 #include "engines/grim/savegame.h"
00033 #include "engines/grim/lab.h"
00034 #include "engines/grim/bitmap.h"
00035 #include "engines/grim/font.h"
00036 #include "engines/grim/model.h"
00037 #include "engines/grim/sprite.h"
00038 #include "engines/grim/inputdialog.h"
00039 #include "engines/grim/debug.h"
00040 #include "engines/grim/emi/animationemi.h"
00041 #include "engines/grim/emi/costumeemi.h"
00042 #include "engines/grim/emi/modelemi.h"
00043 #include "engines/grim/emi/skeleton.h"
00044 #include "engines/grim/patchr.h"
00045 #include "engines/grim/md5check.h"
00046 #include "engines/grim/update/update.h"
00047 
00048 #include "common/algorithm.h"
00049 #include "common/zlib.h"
00050 #include "common/memstream.h"
00051 #include "common/file.h"
00052 #include "common/config-manager.h"
00053 #include "common/translation.h"
00054 
00055 namespace Grim {
00056 
00057 ResourceLoader *g_resourceloader = nullptr;
00058 
00059 class LabListComperator {
00060     const Common::String _labName;
00061 public:
00062     LabListComperator() {}
00063     LabListComperator(const Common::String &ln) : _labName(ln) {}
00064 
00065     bool operator()(const Common::ArchiveMemberPtr &l) {
00066         return _labName.compareToIgnoreCase(l->getName()) == 0;
00067     }
00068 
00069     bool operator()(const Common::ArchiveMemberPtr &l, const Common::ArchiveMemberPtr &r) {
00070         return (l->getName().compareToIgnoreCase(r->getName()) > 0);
00071     }
00072 };
00073 
00074 ResourceLoader::ResourceLoader() {
00075     _cacheDirty = false;
00076     _cacheMemorySize = 0;
00077 
00078     Lab *l;
00079     Common::ArchiveMemberList files, updFiles;
00080 
00081     //Load the update from the executable, if needed
00082     const char *updateFilename = g_grim->getUpdateFilename();
00083     if (updateFilename) {
00084         Common::File *updStream = new Common::File();
00085         if (updStream && updStream->open(updateFilename)) {
00086             Common::Archive *update = loadUpdateArchive(updStream);
00087             if (update)
00088                 SearchMan.add("update", update, 1);
00089         } else
00090             delete updStream;
00091 
00092         // Check if the update has been correctly loaded
00093         if (!SearchMan.hasArchive("update")) {
00094             const char *errorMessage = nullptr;
00095             if (g_grim->getGameType() == GType_GRIM) {
00096                 errorMessage = _("The original patch of Grim Fandango\n"
00097                                 "is missing. Please download it from\n"
00098                                 "http://www.residualvm.org/downloads/\n"
00099                                 "and put it in the game data files directory");
00100             } else if (g_grim->getGameType() == GType_MONKEY4) {
00101                 errorMessage = _("The original patch of Escape from Monkey Island is missing. \n"
00102                                 "Please download it from http://www.residualvm.org/downloads/\n"
00103                                 "and put it in the game data files directory.\n"
00104                                 "Pay attention to download the correct version according to the game's language");
00105             }
00106 
00107             GUI::displayErrorDialog(errorMessage);
00108             error("%s not found", updateFilename);
00109         }
00110     }
00111 
00112     if (g_grim->getGameType() == GType_GRIM) {
00113         if (g_grim->getGameFlags() & ADGF_DEMO) {
00114             SearchMan.listMatchingMembers(files, "gfdemo01.lab");
00115             SearchMan.listMatchingMembers(files, "gdemo001.lab"); // For the english demo with video.
00116             SearchMan.listMatchingMembers(files, "grimdemo.mus");
00117             SearchMan.listMatchingMembers(files, "sound001.lab");
00118             SearchMan.listMatchingMembers(files, "voice001.lab");
00119         } else {
00120             if (!SearchMan.hasFile("residualvm-grim-patch.lab"))
00121                 error("%s", _("residualvm-grim-patch.lab not found"));
00122 
00123             SearchMan.listMatchingMembers(files, "residualvm-grim-patch.lab");
00124             SearchMan.listMatchingMembers(files, "data005.lab");
00125             SearchMan.listMatchingMembers(files, "data004.lab");
00126             SearchMan.listMatchingMembers(files, "data003.lab");
00127             SearchMan.listMatchingMembers(files, "data002.lab");
00128             SearchMan.listMatchingMembers(files, "data001.lab");
00129             SearchMan.listMatchingMembers(files, "data000.lab");
00130             SearchMan.listMatchingMembers(files, "movie??.lab");
00131             SearchMan.listMatchingMembers(files, "vox????.lab");
00132             SearchMan.listMatchingMembers(files, "year?mus.lab");
00133             SearchMan.listMatchingMembers(files, "local.lab");
00134             SearchMan.listMatchingMembers(files, "credits.lab");
00135 
00136             //Sort the archives in order to ensure that they are loaded with the correct order
00137             Common::sort(files.begin(), files.end(), LabListComperator());
00138 
00139             //Check the presence of datausr.lab and if the user wants to load it.
00140             //In this case put it in the top of the list
00141             const char *datausr_name = "datausr.lab";
00142             if (SearchMan.hasFile(datausr_name) && ConfMan.getBool("datausr_load")) {
00143                 warning("%s", _("Loading datausr.lab. Please note that the ResidualVM-team doesn't provide support for using such patches"));
00144                 files.push_front(SearchMan.getMember(datausr_name));
00145             }
00146         }
00147     } else if (g_grim->getGameType() == GType_MONKEY4) {
00148         const char *emi_patches_filename = "residualvm-emi-patch.m4b";
00149         if (!SearchMan.hasFile(emi_patches_filename))
00150             error(_("%s not found"), emi_patches_filename);
00151 
00152         SearchMan.listMatchingMembers(files, emi_patches_filename);
00153 
00154         if (g_grim->getGameFlags() & ADGF_DEMO) {
00155             SearchMan.listMatchingMembers(files, "i9n.lab");
00156             SearchMan.listMatchingMembers(files, "lip.lab");
00157             SearchMan.listMatchingMembers(files, "MagDemo.lab");
00158             SearchMan.listMatchingMembers(files, "tile.lab");
00159             SearchMan.listMatchingMembers(files, "voice.lab");
00160         } else {
00161 
00162             //Keep i9n.m4b before patch.m4b for a better efficiency
00163             //in decompressing from Monkey Update.exe
00164             SearchMan.listMatchingMembers(files, "i9n.m4b");
00165             SearchMan.listMatchingMembers(files, "patch.m4b");
00166             SearchMan.listMatchingMembers(files, "art???.m4b");
00167             SearchMan.listMatchingMembers(files, "lip.m4b");
00168             SearchMan.listMatchingMembers(files, "local.m4b");
00169             SearchMan.listMatchingMembers(files, "sfx.m4b");
00170             SearchMan.listMatchingMembers(files, "voice???.m4b");
00171             SearchMan.listMatchingMembers(files, "music?.m4b");
00172 
00173             if (g_grim->getGamePlatform() == Common::kPlatformPS2) {
00174                 SearchMan.listMatchingMembers(files, "???.m4b");
00175             }
00176 
00177             //Check the presence of datausr.m4b and if the user wants to load it.
00178             //In this case put it in the top of the list
00179             const char *datausr_name = "datausr.m4b";
00180             if (SearchMan.hasFile(datausr_name) && ConfMan.getBool("datausr_load")) {
00181                 warning("%s", _("Loading datausr.m4b. Please note that the ResidualVM-team doesn't provide support for using such patches"));
00182                 files.push_front(SearchMan.getMember(datausr_name));
00183             }
00184         }
00185     }
00186 
00187     if (files.empty())
00188         error("%s", _("Cannot find game data - check configuration file"));
00189 
00190     //load labs
00191     int priority = files.size();
00192     for (Common::ArchiveMemberList::const_iterator x = files.begin(); x != files.end(); ++x) {
00193         Common::String filename = (*x)->getName();
00194         filename.toLowercase();
00195 
00196         //Avoid duplicates
00197         if (SearchMan.hasArchive(filename))
00198             continue;
00199 
00200         l = new Lab();
00201         // Caching "local.m4b" to speed up the launch of the mac version,
00202         // we _COULD_ protect this with a platform check, but the file isn't
00203         // really big anyhow...
00204         bool useCache = (filename == "local.m4b");
00205         if (l->open(filename, useCache))
00206             SearchMan.add(filename, l, priority--, true);
00207         else
00208             delete l;
00209     }
00210 
00211     files.clear();
00212 }
00213 
00214 template<typename T>
00215 void clearList(Common::List<T> &list) {
00216     while (!list.empty()) {
00217         T p = list.front();
00218         list.erase(list.begin());
00219         delete p;
00220     }
00221 }
00222 
00223 ResourceLoader::~ResourceLoader() {
00224     for (Common::Array<ResourceCache>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
00225         ResourceCache &r = *i;
00226         delete[] r.fname;
00227         delete[] r.resPtr;
00228     }
00229     clearList(_models);
00230     clearList(_colormaps);
00231     clearList(_keyframeAnims);
00232     clearList(_lipsyncs);
00233     MD5Check::clear();
00234 }
00235 
00236 static int sortCallback(const void *entry1, const void *entry2) {
00237     return scumm_stricmp(((const ResourceLoader::ResourceCache *)entry1)->fname, ((const ResourceLoader::ResourceCache *)entry2)->fname);
00238 }
00239 
00240 Common::SeekableReadStream *ResourceLoader::getFileFromCache(const Common::String &filename) const {
00241     ResourceLoader::ResourceCache *entry = getEntryFromCache(filename);
00242     if (!entry)
00243         return nullptr;
00244 
00245     return new Common::MemoryReadStream(entry->resPtr, entry->len);
00246 
00247 }
00248 
00249 ResourceLoader::ResourceCache *ResourceLoader::getEntryFromCache(const Common::String &filename) const {
00250     if (_cache.empty())
00251         return nullptr;
00252 
00253     if (_cacheDirty) {
00254         qsort(_cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
00255         _cacheDirty = false;
00256     }
00257 
00258     ResourceCache key;
00259     key.fname = const_cast<char *>(filename.c_str());
00260 
00261     return (ResourceLoader::ResourceCache *)bsearch(&key, _cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
00262 }
00263 
00264 Common::SeekableReadStream *ResourceLoader::loadFile(const Common::String &filename) const {
00265     Common::SeekableReadStream *rs = nullptr;
00266     if (SearchMan.hasFile(filename))
00267         rs = SearchMan.createReadStreamForMember(filename);
00268     else
00269         return nullptr;
00270 
00271     rs = wrapPatchedFile(rs, filename);
00272     return rs;
00273 }
00274 
00275 Common::SeekableReadStream *ResourceLoader::openNewStreamFile(Common::String fname, bool cache) const {
00276     Common::SeekableReadStream *s;
00277     fname.toLowercase();
00278 
00279     if (cache) {
00280         s = getFileFromCache(fname);
00281         if (!s) {
00282             s = loadFile(fname);
00283             if (!s)
00284                 return nullptr;
00285 
00286             uint32 size = s->size();
00287             byte *buf = new byte[size];
00288             s->read(buf, size);
00289             putIntoCache(fname, buf, size);
00290             delete s;
00291             s = new Common::MemoryReadStream(buf, size);
00292         }
00293     } else {
00294         s = loadFile(fname);
00295     }
00296     // This will only have an effect if the stream is actually compressed.
00297     return Common::wrapCompressedReadStream(s);
00298 }
00299 
00300 void ResourceLoader::putIntoCache(const Common::String &fname, byte *res, uint32 len) const {
00301     ResourceCache entry;
00302     entry.resPtr = res;
00303     entry.len = len;
00304     entry.fname = new char[fname.size() + 1];
00305     strcpy(entry.fname, fname.c_str());
00306     _cacheMemorySize += len;
00307     _cache.push_back(entry);
00308     _cacheDirty = true;
00309 }
00310 
00311 CMap *ResourceLoader::loadColormap(const Common::String &filename) {
00312     Common::SeekableReadStream *stream = openNewStreamFile(filename.c_str());
00313     if (!stream) {
00314         error("Could not find colormap %s", filename.c_str());
00315     }
00316 
00317     CMap *result = new CMap(filename, stream);
00318     _colormaps.push_back(result);
00319     delete stream;
00320 
00321     return result;
00322 }
00323 
00324 Common::String ResourceLoader::fixFilename(const Common::String &filename, bool append) {
00325     Common::String fname(filename);
00326     if (g_grim->getGameType() == GType_MONKEY4) {
00327         int len = fname.size();
00328         for (int i = 0; i < len; i++) {
00329             if (fname[i] == '\\') {
00330                 fname.setChar('/', i);
00331             }
00332         }
00333         // Append b to end of filename for EMI
00334         if (append)
00335             fname += "b";
00336     }
00337     return fname;
00338 }
00339 
00340 Costume *ResourceLoader::loadCostume(const Common::String &filename, Actor *owner, Costume *prevCost) {
00341     Common::String fname = fixFilename(filename);
00342     fname.toLowercase();
00343 
00344     Common::SeekableReadStream *stream = openNewStreamFile(fname.c_str(), true);
00345     if (!stream) {
00346         error("Could not find costume \"%s\"", filename.c_str());
00347     }
00348     Costume *result;
00349     if (g_grim->getGameType() == GType_MONKEY4) {
00350         result = new EMICostume(filename, owner, prevCost);
00351     } else {
00352         result = new Costume(filename, owner, prevCost);
00353     }
00354     result->load(stream);
00355     delete stream;
00356 
00357     return result;
00358 }
00359 
00360 Font *ResourceLoader::loadFont(const Common::String &filename) {
00361     Common::SeekableReadStream *stream;
00362 
00363     stream = openNewStreamFile(filename.c_str(), true);
00364     if (!stream)
00365         error("Could not find font file %s", filename.c_str());
00366 
00367     Font *result = new Font();
00368     result->load(filename, stream);
00369     delete stream;
00370 
00371     return result;
00372 }
00373 
00374 KeyframeAnim *ResourceLoader::loadKeyframe(const Common::String &filename) {
00375     Common::SeekableReadStream *stream;
00376 
00377     stream = openNewStreamFile(filename.c_str());
00378     if (!stream)
00379         error("Could not find keyframe file %s", filename.c_str());
00380 
00381     KeyframeAnim *result = new KeyframeAnim(filename, stream);
00382     _keyframeAnims.push_back(result);
00383     delete stream;
00384 
00385     return result;
00386 }
00387 
00388 LipSync *ResourceLoader::loadLipSync(const Common::String &filename) {
00389     LipSync *result;
00390     Common::SeekableReadStream *stream;
00391 
00392     stream = openNewStreamFile(filename.c_str());
00393     if (!stream)
00394         return nullptr;
00395 
00396     result = new LipSync(filename, stream);
00397 
00398     // Some lipsync files have no data
00399     if (result->isValid())
00400         _lipsyncs.push_back(result);
00401     else {
00402         delete result;
00403         result = nullptr;
00404     }
00405     delete stream;
00406 
00407     return result;
00408 }
00409 
00410 Material *ResourceLoader::loadMaterial(const Common::String &filename, CMap *c, bool clamp) {
00411     Common::String fname = fixFilename(filename, false);
00412     fname.toLowercase();
00413     Common::SeekableReadStream *stream;
00414 
00415     stream = openNewStreamFile(fname.c_str(), true);
00416     if (!stream && !filename.hasPrefix("specialty")) {
00417         // FIXME: EMI demo references files that aren't included. Return a known material.
00418         // This should be fixed in the data files instead.
00419         if (g_grim->getGameType() == GType_MONKEY4 && g_grim->getGameFlags() & ADGF_DEMO) {
00420             const Common::String replacement("fx/candle.sprb");
00421             warning("Could not find material %s, using %s instead", filename.c_str(), replacement.c_str());
00422             return loadMaterial(replacement, nullptr, clamp);
00423         } else {
00424             error("Could not find material %s", filename.c_str());
00425         }
00426     }
00427 
00428     Material *result = new Material(fname, stream, c, clamp);
00429     delete stream;
00430 
00431     return result;
00432 }
00433 
00434 Model *ResourceLoader::loadModel(const Common::String &filename, CMap *c, Model *parent) {
00435     Common::String fname = fixFilename(filename);
00436     Common::SeekableReadStream *stream;
00437 
00438     stream = openNewStreamFile(fname.c_str());
00439     if (!stream)
00440         error("Could not find model %s", filename.c_str());
00441 
00442     Model *result = new Model(filename, stream, c, parent);
00443     _models.push_back(result);
00444     delete stream;
00445 
00446     return result;
00447 }
00448 
00449 EMIModel *ResourceLoader::loadModelEMI(const Common::String &filename, EMICostume *costume) {
00450     Common::String fname = fixFilename(filename);
00451     Common::SeekableReadStream *stream;
00452 
00453     stream = openNewStreamFile(fname.c_str());
00454     if (!stream) {
00455         warning("Could not find model %s", filename.c_str());
00456         return nullptr;
00457     }
00458 
00459     EMIModel *result = new EMIModel(filename, stream, costume);
00460     _emiModels.push_back(result);
00461     delete stream;
00462 
00463     return result;
00464 }
00465 
00466 Skeleton *ResourceLoader::loadSkeleton(const Common::String &filename) {
00467     Common::String fname = fixFilename(filename);
00468     Common::SeekableReadStream *stream;
00469 
00470     stream = openNewStreamFile(fname.c_str(), true);
00471     if (!stream) {
00472         warning("Could not find skeleton %s", filename.c_str());
00473         return nullptr;
00474     }
00475 
00476     Skeleton *result = new Skeleton(filename, stream);
00477     delete stream;
00478 
00479     return result;
00480 }
00481 
00482 Sprite *ResourceLoader::loadSprite(const Common::String &filename, EMICostume *costume) {
00483     assert(g_grim->getGameType() == GType_MONKEY4);
00484     Common::SeekableReadStream *stream;
00485 
00486     const Common::String fname = fixFilename(filename, true);
00487 
00488     stream = openNewStreamFile(fname.c_str(), true);
00489     if (!stream) {
00490         warning("Could not find sprite %s", fname.c_str());
00491         return nullptr;
00492     }
00493 
00494     Sprite *result = new Sprite();
00495     result->loadBinary(stream, costume);
00496     delete stream;
00497 
00498     return result;
00499 }
00500 
00501 AnimationEmi *ResourceLoader::loadAnimationEmi(const Common::String &filename) {
00502     Common::String fname = fixFilename(filename);
00503     Common::SeekableReadStream *stream;
00504 
00505     stream = openNewStreamFile(fname.c_str(), true);
00506     if (!stream) {
00507         warning("Could not find animation %s", filename.c_str());
00508         return nullptr;
00509     }
00510 
00511     AnimationEmi *result = new AnimationEmi(filename, stream);
00512     _emiAnims.push_back(result);
00513     delete stream;
00514 
00515     return result;
00516 }
00517 
00518 void ResourceLoader::uncache(const char *filename) const {
00519     Common::String fname = filename;
00520     fname.toLowercase();
00521 
00522     if (_cacheDirty) {
00523         qsort(_cache.begin(), _cache.size(), sizeof(ResourceCache), sortCallback);
00524         _cacheDirty = false;
00525     }
00526 
00527     for (unsigned int i = 0; i < _cache.size(); i++) {
00528         if (fname.compareTo(_cache[i].fname) == 0) {
00529             delete[] _cache[i].fname;
00530             _cacheMemorySize -= _cache[i].len;
00531             delete[] _cache[i].resPtr;
00532             _cache.remove_at(i);
00533             _cacheDirty = true;
00534         }
00535     }
00536 }
00537 
00538 void ResourceLoader::uncacheModel(Model *m) {
00539     _models.remove(m);
00540 }
00541 
00542 void ResourceLoader::uncacheColormap(CMap *c) {
00543     _colormaps.remove(c);
00544 }
00545 
00546 void ResourceLoader::uncacheKeyframe(KeyframeAnim *k) {
00547     _keyframeAnims.remove(k);
00548 }
00549 
00550 void ResourceLoader::uncacheLipSync(LipSync *s) {
00551     _lipsyncs.remove(s);
00552 }
00553 
00554 void ResourceLoader::uncacheAnimationEmi(AnimationEmi *a) {
00555     _emiAnims.remove(a);
00556 }
00557 
00558 ModelPtr ResourceLoader::getModel(const Common::String &fname, CMap *c) {
00559     Common::String filename = fname;
00560     filename.toLowercase();
00561     for (Common::List<Model *>::const_iterator i = _models.begin(); i != _models.end(); ++i) {
00562         Model *m = *i;
00563         if (filename == m->getFilename() && *m->getCMap() == *c) {
00564             return m;
00565         }
00566     }
00567 
00568     return loadModel(fname, c);
00569 }
00570 
00571 CMapPtr ResourceLoader::getColormap(const Common::String &fname) {
00572     Common::String filename = fname;
00573     filename.toLowercase();
00574     for (Common::List<CMap *>::const_iterator i = _colormaps.begin(); i != _colormaps.end(); ++i) {
00575         CMap *c = *i;
00576         if (filename.equals(c->_fname)) {
00577             return c;
00578         }
00579     }
00580 
00581     return loadColormap(fname);
00582 }
00583 
00584 KeyframeAnimPtr ResourceLoader::getKeyframe(const Common::String &fname) {
00585     Common::String filename = fname;
00586     filename.toLowercase();
00587     for (Common::List<KeyframeAnim *>::const_iterator i = _keyframeAnims.begin(); i != _keyframeAnims.end(); ++i) {
00588         KeyframeAnim *k = *i;
00589         if (filename == k->getFilename()) {
00590             return k;
00591         }
00592     }
00593 
00594     return loadKeyframe(fname);
00595 }
00596 
00597 LipSyncPtr ResourceLoader::getLipSync(const Common::String &fname) {
00598     Common::String filename = fname;
00599     filename.toLowercase();
00600     for (Common::List<LipSync *>::const_iterator i = _lipsyncs.begin(); i != _lipsyncs.end(); ++i) {
00601         LipSync *l = *i;
00602         if (filename == l->getFilename()) {
00603             return l;
00604         }
00605     }
00606 
00607     return loadLipSync(fname);
00608 }
00609 
00610 AnimationEmiPtr ResourceLoader::getAnimationEmi(const Common::String &fname) {
00611     Common::String filename = fname;
00612     filename.toLowercase();
00613     for (Common::List<AnimationEmi *>::const_iterator i = _emiAnims.begin(); i != _emiAnims.end(); ++i) {
00614         AnimationEmi *a = *i;
00615         if (filename == a->getFilename()) {
00616             return a;
00617         }
00618     }
00619 
00620     return loadAnimationEmi(fname);
00621 }
00622 
00623 } // end of namespace Grim


Generated on Sat Mar 16 2019 05:01:51 for ResidualVM by doxygen 1.7.1
curved edge   curved edge