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

advancedDetector.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
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 "common/debug.h"
00024 #include "common/util.h"
00025 #include "common/file.h"
00026 #include "common/macresman.h"
00027 #include "common/md5.h"
00028 #include "common/config-manager.h"
00029 #include "common/system.h"
00030 #include "common/textconsole.h"
00031 #include "common/translation.h"
00032 #include "gui/EventRecorder.h"
00033 #include "gui/gui-manager.h"
00034 #include "gui/message.h"
00035 #include "engines/advancedDetector.h"
00036 #include "engines/obsolete.h"
00037 
00038 static Common::String sanitizeName(const char *name) {
00039     Common::String res;
00040 
00041     while (*name) {
00042         if (Common::isAlnum(*name))
00043             res += tolower(*name);
00044         name++;
00045     }
00046 
00047     return res;
00048 }
00049 
00056 static Common::String generatePreferredTarget(const ADGameDescription *desc) {
00057     Common::String res;
00058 
00059     if (desc->flags & ADGF_AUTOGENTARGET && desc->extra && *desc->extra) {
00060         res = sanitizeName(desc->extra);
00061     } else {
00062         res = desc->gameId;
00063     }
00064 
00065     if (desc->flags & ADGF_DEMO) {
00066         res = res + "-demo";
00067     }
00068 
00069     if (desc->flags & ADGF_CD) {
00070         res = res + "-cd";
00071     }
00072 
00073     if (desc->platform != Common::kPlatformDOS && desc->platform != Common::kPlatformUnknown && !(desc->flags & ADGF_DROPPLATFORM)) {
00074         res = res + "-" + getPlatformAbbrev(desc->platform);
00075     }
00076 
00077     if (desc->language != Common::EN_ANY && desc->language != Common::UNK_LANG && !(desc->flags & ADGF_DROPLANGUAGE)) {
00078         res = res + "-" + getLanguageCode(desc->language);
00079     }
00080 
00081     return res;
00082 }
00083 
00084 DetectedGame AdvancedMetaEngine::toDetectedGame(const ADDetectedGame &adGame) const {
00085     const ADGameDescription *desc = adGame.desc;
00086 
00087     const char *title;
00088     const char *extra;
00089     if (desc->flags & ADGF_USEEXTRAASTITLE) {
00090         title = desc->extra;
00091         extra = "";
00092     } else {
00093         const PlainGameDescriptor *pgd = findPlainGameDescriptor(desc->gameId, _gameIds);
00094         if (pgd) {
00095             title = pgd->description;
00096         } else {
00097             title = "";
00098         }
00099         extra = desc->extra;
00100     }
00101 
00102     DetectedGame game(getEngineId(), desc->gameId, title, desc->language, desc->platform, extra);
00103     game.hasUnknownFiles = adGame.hasUnknownFiles;
00104     game.matchedFiles = adGame.matchedFiles;
00105     game.preferredTarget = generatePreferredTarget(desc);
00106 
00107     game.gameSupportLevel = kStableGame;
00108     if (desc->flags & ADGF_UNSTABLE)
00109         game.gameSupportLevel = kUnstableGame;
00110     else if (desc->flags & ADGF_TESTING)
00111         game.gameSupportLevel = kTestingGame;
00112 
00113     game.setGUIOptions(desc->guiOptions + _guiOptions);
00114     game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(desc->language));
00115 
00116     if (desc->flags & ADGF_ADDENGLISH)
00117         game.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(Common::EN_ANY));
00118 
00119     if (_flags & kADFlagUseExtraAsHint)
00120         game.extra = desc->extra;
00121 
00122     return game;
00123 }
00124 
00125 bool cleanupPirated(ADDetectedGames &matched) {
00126     // OKay, now let's sense presence of pirated games
00127     if (!matched.empty()) {
00128         for (uint j = 0; j < matched.size();) {
00129             if (matched[j].desc->flags & ADGF_PIRATED)
00130                 matched.remove_at(j);
00131             else
00132                 ++j;
00133         }
00134 
00135         // We ruled out all variants and now have nothing
00136         if (matched.empty()) {
00137             warning("Illegitimate game copy detected. We provide no support in such cases");
00138             if (GUI::GuiManager::hasInstance()) {
00139                 GUI::MessageDialog dialog(_("Illegitimate game copy detected. We provide no support in such cases"));
00140                 dialog.runModal();
00141             };
00142             return true;
00143         }
00144     }
00145 
00146     return false;
00147 }
00148 
00149 
00150 DetectedGames AdvancedMetaEngine::detectGames(const Common::FSList &fslist) const {
00151     FileMap allFiles;
00152 
00153     if (fslist.empty())
00154         return DetectedGames();
00155 
00156     // Compose a hashmap of all files in fslist.
00157     composeFileHashMap(allFiles, fslist, (_maxScanDepth == 0 ? 1 : _maxScanDepth));
00158 
00159     // Run the detector on this
00160     ADDetectedGames matches = detectGame(fslist.begin()->getParent(), allFiles, Common::UNK_LANG, Common::kPlatformUnknown, "");
00161 
00162     cleanupPirated(matches);
00163 
00164     DetectedGames detectedGames;
00165     for (uint i = 0; i < matches.size(); i++) {
00166         DetectedGame game = toDetectedGame(matches[i]);
00167 
00168         if (game.hasUnknownFiles) {
00169             // Non fallback games with unknown files cannot be added/launched
00170             game.canBeAdded = false;
00171         }
00172 
00173         detectedGames.push_back(game);
00174     }
00175 
00176     bool foundKnownGames = false;
00177     for (uint i = 0; i < detectedGames.size(); i++) {
00178         foundKnownGames |= detectedGames[i].canBeAdded;
00179     }
00180 
00181     if (!foundKnownGames) {
00182         // Use fallback detector if there were no matches by other means
00183         ADDetectedGame fallbackDetectionResult = fallbackDetect(allFiles, fslist);
00184 
00185         if (fallbackDetectionResult.desc) {
00186             DetectedGame fallbackDetectedGame = toDetectedGame(fallbackDetectionResult);
00187             fallbackDetectedGame.preferredTarget += "-fallback";
00188 
00189             detectedGames.push_back(fallbackDetectedGame);
00190         }
00191     }
00192 
00193     return detectedGames;
00194 }
00195 
00196 const ExtraGuiOptions AdvancedMetaEngine::getExtraGuiOptions(const Common::String &target) const {
00197     if (!_extraGuiOptions)
00198         return ExtraGuiOptions();
00199 
00200     ExtraGuiOptions options;
00201 
00202     // If there isn't any target specified, return all available GUI options.
00203     // Only used when an engine starts in order to set option defaults.
00204     if (target.empty()) {
00205         for (const ADExtraGuiOptionsMap *entry = _extraGuiOptions; entry->guioFlag; ++entry)
00206             options.push_back(entry->option);
00207 
00208         return options;
00209     }
00210 
00211     // Query the GUI options
00212     const Common::String guiOptionsString = ConfMan.get("guioptions", target);
00213     const Common::String guiOptions = parseGameGUIOptions(guiOptionsString);
00214 
00215     // Add all the applying extra GUI options.
00216     for (const ADExtraGuiOptionsMap *entry = _extraGuiOptions; entry->guioFlag; ++entry) {
00217         if (guiOptions.contains(entry->guioFlag))
00218             options.push_back(entry->option);
00219     }
00220 
00221     return options;
00222 }
00223 
00224 Common::Error AdvancedMetaEngine::createInstance(OSystem *syst, Engine **engine) const {
00225     assert(engine);
00226 
00227     Common::Language language = Common::UNK_LANG;
00228     Common::Platform platform = Common::kPlatformUnknown;
00229     Common::String extra;
00230 
00231     if (ConfMan.hasKey("language"))
00232         language = Common::parseLanguage(ConfMan.get("language"));
00233     if (ConfMan.hasKey("platform"))
00234         platform = Common::parsePlatform(ConfMan.get("platform"));
00235     if (_flags & kADFlagUseExtraAsHint) {
00236         if (ConfMan.hasKey("extra"))
00237             extra = ConfMan.get("extra");
00238     }
00239 
00240     Common::String gameid = ConfMan.get("gameid");
00241 
00242     Common::String path;
00243     if (ConfMan.hasKey("path")) {
00244         path = ConfMan.get("path");
00245     } else {
00246         path = ".";
00247         warning("No path was provided. Assuming the data files are in the current directory");
00248     }
00249     Common::FSNode dir(path);
00250     Common::FSList files;
00251     if (!dir.isDirectory() || !dir.getChildren(files, Common::FSNode::kListAll)) {
00252         warning("Game data path does not exist or is not a directory (%s)", path.c_str());
00253         return Common::kNoGameDataFoundError;
00254     }
00255 
00256     if (files.empty())
00257         return Common::kNoGameDataFoundError;
00258 
00259     // Compose a hashmap of all files in fslist.
00260     FileMap allFiles;
00261     composeFileHashMap(allFiles, files, (_maxScanDepth == 0 ? 1 : _maxScanDepth));
00262 
00263     // Run the detector on this
00264     ADDetectedGames matches = detectGame(files.begin()->getParent(), allFiles, language, platform, extra);
00265 
00266     if (cleanupPirated(matches))
00267         return Common::kNoGameDataFoundError;
00268 
00269     ADDetectedGame agdDesc;
00270     for (uint i = 0; i < matches.size(); i++) {
00271         if (matches[i].desc->gameId == gameid && !matches[i].hasUnknownFiles) {
00272             agdDesc = matches[i];
00273             break;
00274         }
00275     }
00276 
00277     if (!agdDesc.desc) {
00278         // Use fallback detector if there were no matches by other means
00279         ADDetectedGame fallbackDetectedGame = fallbackDetect(allFiles, files);
00280         agdDesc = fallbackDetectedGame;
00281         if (agdDesc.desc) {
00282             // Seems we found a fallback match. But first perform a basic
00283             // sanity check: the gameid must match.
00284             if (agdDesc.desc->gameId != gameid)
00285                 agdDesc = ADDetectedGame();
00286         }
00287     }
00288 
00289     if (!agdDesc.desc)
00290         return Common::kNoGameDataFoundError;
00291 
00292     // If the GUI options were updated, we catch this here and update them in the users config
00293     // file transparently.
00294     Common::String lang = getGameGUIOptionsDescriptionLanguage(agdDesc.desc->language);
00295     if (agdDesc.desc->flags & ADGF_ADDENGLISH)
00296         lang += " " + getGameGUIOptionsDescriptionLanguage(Common::EN_ANY);
00297 
00298     Common::updateGameGUIOptions(agdDesc.desc->guiOptions + _guiOptions, lang);
00299 
00300     DetectedGame gameDescriptor = toDetectedGame(agdDesc);
00301 
00302     bool showTestingWarning = false;
00303 
00304 #ifdef RELEASE_BUILD
00305     showTestingWarning = true;
00306 #endif
00307 
00308     if (((gameDescriptor.gameSupportLevel == kUnstableGame
00309             || (gameDescriptor.gameSupportLevel == kTestingGame
00310                     && showTestingWarning)))
00311             && !Engine::warnUserAboutUnsupportedGame())
00312         return Common::kUserCanceled;
00313 
00314     debug(2, "Running %s", gameDescriptor.description.c_str());
00315     initSubSystems(agdDesc.desc);
00316     if (!createInstance(syst, engine, agdDesc.desc))
00317         return Common::kNoGameDataFoundError;
00318     else
00319         return Common::kNoError;
00320 }
00321 
00322 void AdvancedMetaEngine::composeFileHashMap(FileMap &allFiles, const Common::FSList &fslist, int depth, const Common::String &parentName) const {
00323     if (depth <= 0)
00324         return;
00325 
00326     if (fslist.empty())
00327         return;
00328 
00329     for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
00330         Common::String tstr = (_matchFullPaths && !parentName.empty() ? parentName + "/" : "") + file->getName();
00331 
00332         if (file->isDirectory()) {
00333             Common::FSList files;
00334 
00335             if (!_directoryGlobs)
00336                 continue;
00337 
00338             bool matched = false;
00339             for (const char * const *glob = _directoryGlobs; *glob; glob++)
00340                 if (file->getName().matchString(*glob, true)) {
00341                     matched = true;
00342                     break;
00343                 }
00344 
00345             if (!matched)
00346                 continue;
00347 
00348             if (!file->getChildren(files, Common::FSNode::kListAll))
00349                 continue;
00350 
00351             composeFileHashMap(allFiles, files, depth - 1, tstr);
00352         }
00353 
00354         // Strip any trailing dot
00355         if (tstr.lastChar() == '.')
00356             tstr.deleteLastChar();
00357 
00358         allFiles[tstr] = *file; // Record the presence of this file
00359     }
00360 }
00361 
00362 bool AdvancedMetaEngine::getFileProperties(const Common::FSNode &parent, const FileMap &allFiles, const ADGameDescription &game, const Common::String fname, FileProperties &fileProps) const {
00363     // FIXME/TODO: We don't handle the case that a file is listed as a regular
00364     // file and as one with resource fork.
00365 
00366     if (game.flags & ADGF_MACRESFORK) {
00367         Common::MacResManager macResMan;
00368 
00369         if (!macResMan.open(parent, fname))
00370             return false;
00371 
00372         fileProps.md5 = macResMan.computeResForkMD5AsString(_md5Bytes);
00373         fileProps.size = macResMan.getResForkDataSize();
00374 
00375         if (fileProps.size != 0)
00376             return true;
00377     }
00378 
00379     if (!allFiles.contains(fname))
00380         return false;
00381 
00382     Common::File testFile;
00383 
00384     if (!testFile.open(allFiles[fname]))
00385         return false;
00386 
00387     fileProps.size = (int32)testFile.size();
00388     fileProps.md5 = Common::computeStreamMD5AsString(testFile, _md5Bytes);
00389     return true;
00390 }
00391 
00392 ADDetectedGames AdvancedMetaEngine::detectGame(const Common::FSNode &parent, const FileMap &allFiles, Common::Language language, Common::Platform platform, const Common::String &extra) const {
00393     FilePropertiesMap filesProps;
00394     ADDetectedGames matched;
00395 
00396     const ADGameFileDescription *fileDesc;
00397     const ADGameDescription *g;
00398     const byte *descPtr;
00399 
00400     debug(3, "Starting detection in dir '%s'", parent.getPath().c_str());
00401 
00402     // Check which files are included in some ADGameDescription *and* whether
00403     // they are present. Compute MD5s and file sizes for the available files.
00404     for (descPtr = _gameDescriptors; ((const ADGameDescription *)descPtr)->gameId != nullptr; descPtr += _descItemSize) {
00405         g = (const ADGameDescription *)descPtr;
00406 
00407         for (fileDesc = g->filesDescriptions; fileDesc->fileName; fileDesc++) {
00408             Common::String fname = fileDesc->fileName;
00409 
00410             if (filesProps.contains(fname))
00411                 continue;
00412 
00413             FileProperties tmp;
00414             if (getFileProperties(parent, allFiles, *g, fname, tmp)) {
00415                 debug(3, "> '%s': '%s'", fname.c_str(), tmp.md5.c_str());
00416             }
00417 
00418             // Both positive and negative results are cached to avoid
00419             // repeatedly checking for files.
00420             filesProps[fname] = tmp;
00421         }
00422     }
00423 
00424     int maxFilesMatched = 0;
00425     bool gotAnyMatchesWithAllFiles = false;
00426 
00427     // MD5 based matching
00428     uint i;
00429     for (i = 0, descPtr = _gameDescriptors; ((const ADGameDescription *)descPtr)->gameId != nullptr; descPtr += _descItemSize, ++i) {
00430         g = (const ADGameDescription *)descPtr;
00431 
00432         // Do not even bother to look at entries which do not have matching
00433         // language and platform (if specified).
00434         if ((language != Common::UNK_LANG && g->language != Common::UNK_LANG && g->language != language
00435              && !(language == Common::EN_ANY && (g->flags & ADGF_ADDENGLISH))) ||
00436             (platform != Common::kPlatformUnknown && g->platform != Common::kPlatformUnknown && g->platform != platform)) {
00437             continue;
00438         }
00439 
00440         if ((_flags & kADFlagUseExtraAsHint) && !extra.empty() && g->extra != extra)
00441             continue;
00442 
00443         ADDetectedGame game(g);
00444         bool allFilesPresent = true;
00445         int curFilesMatched = 0;
00446 
00447         // Try to match all files for this game
00448         for (fileDesc = game.desc->filesDescriptions; fileDesc->fileName; fileDesc++) {
00449             Common::String tstr = fileDesc->fileName;
00450 
00451             if (!filesProps.contains(tstr) || filesProps[tstr].size == -1) {
00452                 allFilesPresent = false;
00453                 break;
00454             }
00455 
00456             game.matchedFiles[tstr] = filesProps[tstr];
00457 
00458             if (game.hasUnknownFiles)
00459                 continue;
00460 
00461             if (fileDesc->md5 != nullptr && fileDesc->md5 != filesProps[tstr].md5) {
00462                 debug(3, "MD5 Mismatch. Skipping (%s) (%s)", fileDesc->md5, filesProps[tstr].md5.c_str());
00463                 game.hasUnknownFiles = true;
00464                 continue;
00465             }
00466 
00467             if (fileDesc->fileSize != -1 && fileDesc->fileSize != filesProps[tstr].size) {
00468                 debug(3, "Size Mismatch. Skipping");
00469                 game.hasUnknownFiles = true;
00470                 continue;
00471             }
00472 
00473             debug(3, "Matched file: %s", tstr.c_str());
00474             curFilesMatched++;
00475         }
00476 
00477         // We found at least one entry with all required files present.
00478         // That means that we got new variant of the game.
00479         //
00480         // Without this check we would have erroneous checksum display
00481         // where only located files will be enlisted.
00482         //
00483         // Potentially this could rule out variants where some particular file
00484         // is really missing, but the developers should better know about such
00485         // cases.
00486         if (allFilesPresent && !gotAnyMatchesWithAllFiles) {
00487             if (matched.empty() || strcmp(matched.back().desc->gameId, g->gameId) != 0)
00488                 matched.push_back(game);
00489         }
00490 
00491         if (allFilesPresent && !game.hasUnknownFiles) {
00492             debug(2, "Found game: %s (%s %s/%s) (%d)", g->gameId, g->extra,
00493              getPlatformDescription(g->platform), getLanguageDescription(g->language), i);
00494 
00495             if (curFilesMatched > maxFilesMatched) {
00496                 debug(2, " ... new best match, removing all previous candidates");
00497                 maxFilesMatched = curFilesMatched;
00498 
00499                 matched.clear();    // Remove any prior, lower ranked matches.
00500                 matched.push_back(game);
00501             } else if (curFilesMatched == maxFilesMatched) {
00502                 matched.push_back(game);
00503             } else {
00504                 debug(2, " ... skipped");
00505             }
00506 
00507             gotAnyMatchesWithAllFiles = true;
00508         } else {
00509             debug(5, "Skipping game: %s (%s %s/%s) (%d)", g->gameId, g->extra,
00510              getPlatformDescription(g->platform), getLanguageDescription(g->language), i);
00511         }
00512     }
00513 
00514     return matched;
00515 }
00516 
00517 ADDetectedGame AdvancedMetaEngine::detectGameFilebased(const FileMap &allFiles, const Common::FSList &fslist, const ADFileBasedFallback *fileBasedFallback) const {
00518     const ADFileBasedFallback *ptr;
00519     const char* const* filenames;
00520 
00521     int maxNumMatchedFiles = 0;
00522     ADDetectedGame result;
00523 
00524     for (ptr = fileBasedFallback; ptr->desc; ++ptr) {
00525         const ADGameDescription *agdesc = ptr->desc;
00526         int numMatchedFiles = 0;
00527         bool fileMissing = false;
00528 
00529         for (filenames = ptr->filenames; *filenames; ++filenames) {
00530             debug(3, "++ %s", *filenames);
00531             if (!allFiles.contains(*filenames)) {
00532                 fileMissing = true;
00533                 break;
00534             }
00535 
00536             numMatchedFiles++;
00537         }
00538 
00539         if (!fileMissing) {
00540             debug(4, "Matched: %s", agdesc->gameId);
00541 
00542             if (numMatchedFiles > maxNumMatchedFiles) {
00543                 maxNumMatchedFiles = numMatchedFiles;
00544 
00545                 debug(4, "and overridden");
00546 
00547                 ADDetectedGame game(agdesc);
00548                 game.hasUnknownFiles = true;
00549 
00550                 for (filenames = ptr->filenames; *filenames; ++filenames) {
00551                     FileProperties tmp;
00552 
00553                     if (getFileProperties(fslist.begin()->getParent(), allFiles, *agdesc, *filenames, tmp))
00554                         game.matchedFiles[*filenames] = tmp;
00555                 }
00556 
00557                 result = game;
00558             }
00559         }
00560     }
00561 
00562     return result;
00563 }
00564 
00565 PlainGameList AdvancedMetaEngine::getSupportedGames() const {
00566     return PlainGameList(_gameIds);
00567 }
00568 
00569 PlainGameDescriptor AdvancedMetaEngine::findGame(const char *gameId) const {
00570     // First search the list of supported gameids for a match.
00571     const PlainGameDescriptor *g = findPlainGameDescriptor(gameId, _gameIds);
00572     if (g)
00573         return *g;
00574 
00575     // No match found
00576     return PlainGameDescriptor::empty();
00577 }
00578 
00579 AdvancedMetaEngine::AdvancedMetaEngine(const void *descs, uint descItemSize, const PlainGameDescriptor *gameIds, const ADExtraGuiOptionsMap *extraGuiOptions)
00580     : _gameDescriptors((const byte *)descs), _descItemSize(descItemSize), _gameIds(gameIds),
00581       _extraGuiOptions(extraGuiOptions) {
00582 
00583     _md5Bytes = 5000;
00584     _flags = 0;
00585     _guiOptions = GUIO_NONE;
00586     _maxScanDepth = 1;
00587     _directoryGlobs = NULL;
00588     _matchFullPaths = false;
00589 }
00590 
00591 void AdvancedMetaEngine::initSubSystems(const ADGameDescription *gameDesc) const {
00592 #ifdef ENABLE_EVENTRECORDER
00593     if (gameDesc) {
00594         g_eventRec.processGameDescription(gameDesc);
00595     }
00596 #endif
00597 }


Generated on Sat May 30 2020 05:00:43 for ResidualVM by doxygen 1.7.1
curved edge   curved edge