ENGINES: Turn GameDescriptor into a simple struct

This commit is contained in:
Bastien Bouclet 2017-12-03 12:19:08 +01:00
parent 643c24db75
commit 5aff87dc15
10 changed files with 156 additions and 146 deletions

View File

@ -881,8 +881,17 @@ static GameList getGameList(const Common::FSNode &dir) {
return candidates;
}
namespace {
static void addStringToConf(const Common::String &key, const Common::String &value, const Common::String &domain) {
if (!value.empty())
ConfMan.set(key, value, domain);
}
} // End of anonymous namespace
static bool addGameToConf(const GameDescriptor &gd) {
const Common::String &domain = gd.preferredtarget();
const Common::String &domain = gd.preferredTarget;
// If game has already been added, don't add
if (ConfMan.hasGameDomain(domain))
@ -891,18 +900,22 @@ static bool addGameToConf(const GameDescriptor &gd) {
// Add the name domain
ConfMan.addGameDomain(domain);
// Copy all non-empty key/value pairs into the new domain
for (GameDescriptor::const_iterator iter = gd.begin(); iter != gd.end(); ++iter) {
if (!iter->_value.empty() && iter->_key != "preferredtarget")
ConfMan.set(iter->_key, iter->_value, domain);
}
// Copy all non-empty relevant values into the new domain
// FIXME: Factor out
addStringToConf("gameid", gd.gameId, domain);
addStringToConf("description", gd.description, domain);
addStringToConf("language", Common::getLanguageCode(gd.language), domain);
addStringToConf("platform", Common::getPlatformCode(gd.platform), domain);
addStringToConf("path", gd.path, domain);
addStringToConf("extra", gd.extra, domain);
addStringToConf("guioptions", gd.getGUIOptions(), domain);
// Display added game info
printf("Game Added: \n GameID: %s\n Name: %s\n Language: %s\n Platform: %s\n",
gd.gameid().c_str(),
gd.description().c_str(),
Common::getLanguageDescription(gd.language()),
Common::getPlatformDescription(gd.platform()));
gd.gameId.c_str(),
gd.description.c_str(),
Common::getLanguageDescription(gd.language),
Common::getPlatformDescription(gd.platform));
return true;
}
@ -916,7 +929,7 @@ static GameList recListGames(const Common::FSNode &dir, const Common::String &ga
for (Common::FSList::const_iterator file = files.begin(); file != files.end(); ++file) {
GameList rec = recListGames(*file, gameId, recursive);
for (GameList::const_iterator game = rec.begin(); game != rec.end(); ++game) {
if (gameId.empty() || game->gameid().c_str() == gameId)
if (gameId.empty() || game->gameId == gameId)
list.push_back(*game);
}
}
@ -946,23 +959,23 @@ static Common::String detectGames(const Common::String &path, const Common::Stri
printf("ID Description Full Path\n");
printf("-------------- ---------------------------------------------------------- ---------------------------------------------------------\n");
for (GameList::iterator v = candidates.begin(); v != candidates.end(); ++v) {
printf("%-14s %-58s %s\n", v->gameid().c_str(), v->description().c_str(), (*v)["path"].c_str());
printf("%-14s %-58s %s\n", v->gameId.c_str(), v->description.c_str(), v->path.c_str());
}
return candidates[0].gameid();
return candidates[0].gameId;
}
static int recAddGames(const Common::FSNode &dir, const Common::String &game, bool recursive) {
int count = 0;
GameList list = getGameList(dir);
for (GameList::iterator v = list.begin(); v != list.end(); ++v) {
if (v->gameid().c_str() != game && !game.empty()) {
printf("Found %s, only adding %s per --game option, ignoring...\n", v->gameid().c_str(), game.c_str());
if (v->gameId != game && !game.empty()) {
printf("Found %s, only adding %s per --game option, ignoring...\n", v->gameId.c_str(), game.c_str());
} else if (!addGameToConf(*v)) {
// TODO Is it reall the case that !addGameToConf iff already added?
printf("Found %s, but has already been added, skipping\n", v->gameid().c_str());
printf("Found %s, but has already been added, skipping\n", v->gameId.c_str());
} else {
printf("Found %s, adding...\n", v->gameid().c_str());
printf("Found %s, adding...\n", v->gameId.c_str());
count++;
}
}
@ -1033,7 +1046,7 @@ static void runDetectorTest() {
bool gameidDiffers = false;
GameList::iterator x;
for (x = candidates.begin(); x != candidates.end(); ++x) {
gameidDiffers |= (scumm_stricmp(gameid.c_str(), x->gameid().c_str()) != 0);
gameidDiffers |= (scumm_stricmp(gameid.c_str(), x->gameId.c_str()) != 0);
}
if (candidates.empty()) {
@ -1056,10 +1069,10 @@ static void runDetectorTest() {
for (x = candidates.begin(); x != candidates.end(); ++x) {
printf(" gameid '%s', desc '%s', language '%s', platform '%s'\n",
x->gameid().c_str(),
x->description().c_str(),
Common::getLanguageCode(x->language()),
Common::getPlatformCode(x->platform()));
x->gameId.c_str(),
x->description.c_str(),
Common::getLanguageDescription(x->language),
Common::getPlatformDescription(x->platform));
}
}
int total = domains.size();
@ -1131,7 +1144,7 @@ void upgradeTargets() {
GameList::iterator x;
int matchesFound = 0;
for (x = candidates.begin(); x != candidates.end(); ++x) {
if (x->gameid() == gameid && x->language() == lang && x->platform() == plat) {
if (x->gameId == gameid && x->language == lang && x->platform == plat) {
matchesFound++;
g = &(*x);
}
@ -1149,27 +1162,27 @@ void upgradeTargets() {
// the target referred to by dom. We update several things
// Always set the gameid explicitly (in case of legacy targets)
dom["gameid"] = g->gameid();
dom["gameid"] = g->gameId;
// Always set the GUI options. The user should not modify them, and engines might
// gain more features over time, so we want to keep this list up-to-date.
if (g->contains("guioptions")) {
printf(" -> update guioptions to '%s'\n", (*g)["guioptions"].c_str());
dom["guioptions"] = (*g)["guioptions"];
if (!g->getGUIOptions().empty()) {
printf(" -> update guioptions to '%s'\n", g->getGUIOptions().c_str());
dom["guioptions"] = g->getGUIOptions();
} else if (dom.contains("guioptions")) {
dom.erase("guioptions");
}
// Update the language setting but only if none has been set yet.
if (lang == Common::UNK_LANG && g->language() != Common::UNK_LANG) {
printf(" -> set language to '%s'\n", Common::getLanguageCode(g->language()));
dom["language"] = (*g)["language"];
if (lang == Common::UNK_LANG && g->language != Common::UNK_LANG) {
printf(" -> set language to '%s'\n", Common::getLanguageCode(g->language));
dom["language"] = Common::getLanguageCode(g->language);
}
// Update the platform setting but only if none has been set yet.
if (plat == Common::kPlatformUnknown && g->platform() != Common::kPlatformUnknown) {
printf(" -> set platform to '%s'\n", Common::getPlatformCode(g->platform()));
dom["platform"] = (*g)["platform"];
if (plat == Common::kPlatformUnknown && g->platform != Common::kPlatformUnknown) {
printf(" -> set platform to '%s'\n", Common::getPlatformCode(g->platform));
dom["platform"] = Common::getPlatformCode(g->platform);
}
// TODO: We could also update the description. But not everybody will want that.
@ -1178,8 +1191,8 @@ void upgradeTargets() {
// should only be updated if the user explicitly requests this.
#if 0
if (desc != g->description()) {
printf(" -> update desc from '%s' to\n '%s' ?\n", desc.c_str(), g->description().c_str());
dom["description"] = (*g)["description"];
printf(" -> update desc from '%s' to\n '%s' ?\n", desc.c_str(), g->description.c_str());
dom["description"] = g->description;
}
#endif
}

View File

@ -531,7 +531,7 @@ DetectionResults EngineManager::detectGames(const Common::FSList &fslist) const
for (uint i = 0; i < engineCandidates.size(); i++) {
engineCandidates[i].engineName = metaEngine.getName();
engineCandidates[i].matchedGame["path"] = path;
engineCandidates[i].matchedGame.path = path;
candidates.push_back(engineCandidates[i]);
}

View File

@ -114,22 +114,22 @@ static Common::String sanitizeName(const char *name) {
void AdvancedMetaEngine::updateGameDescriptor(GameDescriptor &desc, const ADGameDescription *realDesc) const {
if (_singleId != NULL) {
desc["preferredtarget"] = desc["gameid"];
desc["gameid"] = _singleId;
desc.preferredTarget = desc.gameId;
desc.gameId = _singleId;
}
if (!desc.contains("preferredtarget"))
desc["preferredtarget"] = desc["gameid"];
if (desc.preferredTarget.empty())
desc.preferredTarget = desc.gameId;
if (realDesc->flags & ADGF_AUTOGENTARGET) {
if (*realDesc->extra)
desc["preferredtarget"] = sanitizeName(realDesc->extra);
desc.preferredTarget = sanitizeName(realDesc->extra);
}
desc["preferredtarget"] = generatePreferredTarget(desc["preferredtarget"], realDesc);
desc.preferredTarget = generatePreferredTarget(desc.preferredTarget, realDesc);
if (_flags & kADFlagUseExtraAsHint)
desc["extra"] = realDesc->extra;
desc.extra = realDesc->extra;
desc.setGUIOptions(realDesc->guiOptions + _guiOptions);
desc.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(realDesc->language));
@ -329,13 +329,13 @@ Common::Error AdvancedMetaEngine::createInstance(OSystem *syst, Engine **engine)
showTestingWarning = true;
#endif
if (((gameDescriptor.getSupportLevel() == kUnstableGame
|| (gameDescriptor.getSupportLevel() == kTestingGame
if (((gameDescriptor.gameSupportLevel == kUnstableGame
|| (gameDescriptor.gameSupportLevel == kTestingGame
&& showTestingWarning)))
&& !Engine::warnUserAboutUnsupportedGame())
return Common::kUserCanceled;
debug(2, "Running %s", gameDescriptor.description().c_str());
debug(2, "Running %s", gameDescriptor.description.c_str());
initSubSystems(agdDesc.desc);
if (!createInstance(syst, engine, agdDesc.desc))
return Common::kNoGameDataFoundError;

View File

@ -35,94 +35,75 @@ const PlainGameDescriptor *findPlainGameDescriptor(const char *gameid, const Pla
return 0;
}
GameDescriptor::GameDescriptor() {
setVal("gameid", "");
setVal("description", "");
GameDescriptor::GameDescriptor() :
language(Common::UNK_LANG),
platform(Common::kPlatformUnknown),
gameSupportLevel(kStableGame) {
}
GameDescriptor::GameDescriptor(const PlainGameDescriptor &pgd, Common::String guioptions) {
setVal("gameid", pgd.gameId);
setVal("description", pgd.description);
GameDescriptor::GameDescriptor(const PlainGameDescriptor &pgd, const Common::String &guioptions) :
language(Common::UNK_LANG),
platform(Common::kPlatformUnknown),
gameSupportLevel(kStableGame) {
gameId = pgd.gameId;
preferredTarget = pgd.gameId;
description = pgd.description;
if (!guioptions.empty())
setVal("guioptions", Common::getGameGUIOptionsDescription(guioptions));
_guiOptions = Common::getGameGUIOptionsDescription(guioptions);
}
GameDescriptor::GameDescriptor(const Common::String &g, const Common::String &d, Common::Language l, Common::Platform p, Common::String guioptions, GameSupportLevel gsl) {
setVal("gameid", g);
setVal("description", d);
if (l != Common::UNK_LANG)
setVal("language", Common::getLanguageCode(l));
if (p != Common::kPlatformUnknown)
setVal("platform", Common::getPlatformCode(p));
if (!guioptions.empty())
setVal("guioptions", Common::getGameGUIOptionsDescription(guioptions));
GameDescriptor::GameDescriptor(const Common::String &id, const Common::String &d, Common::Language l, Common::Platform p, const Common::String &guioptions, GameSupportLevel gsl) {
gameId = id;
preferredTarget = id;
description = d;
language = l;
platform = p;
gameSupportLevel = gsl;
setSupportLevel(gsl);
if (!guioptions.empty())
_guiOptions = Common::getGameGUIOptionsDescription(guioptions);
}
void GameDescriptor::setGUIOptions(Common::String guioptions) {
if (!guioptions.empty())
setVal("guioptions", Common::getGameGUIOptionsDescription(guioptions));
void GameDescriptor::setGUIOptions(const Common::String &guioptions) {
if (guioptions.empty())
_guiOptions.clear();
else
erase("guioptions");
_guiOptions = Common::getGameGUIOptionsDescription(guioptions);
}
void GameDescriptor::appendGUIOptions(const Common::String &str) {
setVal("guioptions", getVal("guioptions", "") + " " + str);
if (!_guiOptions.empty())
_guiOptions += " ";
_guiOptions += str;
}
void GameDescriptor::updateDesc(const char *extra) {
const bool hasCustomLanguage = (language() != Common::UNK_LANG);
const bool hasCustomPlatform = (platform() != Common::kPlatformUnknown);
const bool hasExtraDesc = (extra && extra[0]);
void GameDescriptor::updateDesc(const char *extraDesc) {
const bool hasCustomLanguage = (language != Common::UNK_LANG);
const bool hasCustomPlatform = (platform != Common::kPlatformUnknown);
const bool hasExtraDesc = (extraDesc && extraDesc[0]);
// Adapt the description string if custom platform/language is set.
if (hasCustomLanguage || hasCustomPlatform || hasExtraDesc) {
Common::String descr = description();
Common::String descr = description;
descr += " (";
if (hasExtraDesc)
descr += extra;
descr += extraDesc;
if (hasCustomPlatform) {
if (hasExtraDesc)
descr += "/";
descr += Common::getPlatformDescription(platform());
descr += Common::getPlatformDescription(platform);
}
if (hasCustomLanguage) {
if (hasExtraDesc || hasCustomPlatform)
descr += "/";
descr += Common::getLanguageDescription(language());
descr += Common::getLanguageDescription(language);
}
descr += ")";
setVal("description", descr);
}
}
GameSupportLevel GameDescriptor::getSupportLevel() {
GameSupportLevel gsl = kStableGame;
if (contains("gsl")) {
Common::String gslString = getVal("gsl");
if (gslString.equals("unstable"))
gsl = kUnstableGame;
else if (gslString.equals("testing"))
gsl = kTestingGame;
}
return gsl;
}
void GameDescriptor::setSupportLevel(GameSupportLevel gsl) {
switch (gsl) {
case kUnstableGame:
setVal("gsl", "unstable");
break;
case kTestingGame:
setVal("gsl", "testing");
break;
case kStableGame:
// Fall Through intended
default:
erase("gsl");
description = descr;
}
}
@ -159,7 +140,7 @@ Common::String DetectionResults::generateUnknownGameReport(bool translate, uint3
const char *reportEngineHeader = _s("Matched game IDs for the %s engine:");
Common::String report = Common::String::format(
translate ? _(reportStart) : reportStart, _detectedGames[0].matchedGame["path"].c_str(),
translate ? _(reportStart) : reportStart, _detectedGames[0].matchedGame.path.c_str(),
"https://bugs.scummvm.org/"
);
report += "\n";
@ -190,7 +171,7 @@ Common::String DetectionResults::generateUnknownGameReport(bool translate, uint3
// Add the gameId to the list of matched games for the engine
// TODO: Use the gameId here instead of the preferred target.
// This is currently impossible due to the AD singleId feature losing the information.
report += game.matchedGame["preferredtarget"];
report += game.matchedGame.preferredTarget;
// Consolidate matched files across all engines and detection entries
for (FilePropertiesMap::const_iterator it = game.matchedFiles.begin(); it != game.matchedFiles.end(); it++) {

View File

@ -78,15 +78,15 @@ enum GameSupportLevel {
* can be contained in a GameDescriptor.
* This is an essential part of the glue between the game engines and the launcher code.
*/
class GameDescriptor : public Common::StringMap {
class GameDescriptor {
public:
GameDescriptor();
explicit GameDescriptor(const PlainGameDescriptor &pgd, Common::String guioptions = Common::String());
GameDescriptor(const Common::String &gameid,
explicit GameDescriptor(const PlainGameDescriptor &pgd, const Common::String &guioptions = Common::String());
GameDescriptor(const Common::String &id,
const Common::String &description,
Common::Language language = Common::UNK_LANG,
Common::Platform platform = Common::kPlatformUnknown,
Common::String guioptions = Common::String(),
const Common::String &guioptions = Common::String(),
GameSupportLevel gsl = kStableGame);
/**
@ -94,27 +94,27 @@ public:
* Values that are missing are omitted, so e.g. (EXTRA/LANG) would be
* added if no platform has been specified but a language and an extra string.
*/
void updateDesc(const char *extra = 0);
void updateDesc(const char *extraDesc = 0);
void setGUIOptions(Common::String options);
void setGUIOptions(const Common::String &options);
void appendGUIOptions(const Common::String &str);
Common::String getGUIOptions() const { return _guiOptions; }
Common::String gameId;
Common::String preferredTarget;
Common::String description;
Common::Language language;
Common::Platform platform;
Common::String path;
Common::String extra;
/**
* What level of support is expected of this game
*/
GameSupportLevel getSupportLevel();
void setSupportLevel(GameSupportLevel gsl);
GameSupportLevel gameSupportLevel;
Common::String &gameid() { return getVal("gameid"); }
Common::String &description() { return getVal("description"); }
const Common::String &gameid() const { return getVal("gameid"); }
const Common::String &description() const { return getVal("description"); }
Common::Language language() const { return contains("language") ? Common::parseLanguage(getVal("language")) : Common::UNK_LANG; }
Common::Platform platform() const { return contains("platform") ? Common::parsePlatform(getVal("platform")) : Common::kPlatformUnknown; }
const Common::String &preferredtarget() const {
return contains("preferredtarget") ? getVal("preferredtarget") : getVal("gameid");
}
private:
Common::String _guiOptions;
};
/** List of games. */

View File

@ -1044,7 +1044,7 @@ DetectedGames ScummMetaEngine::detectGames(const Common::FSList &fslist) const {
// Compute and set the preferred target name for this game.
// Based on generateComplexID() in advancedDetector.cpp.
game.matchedGame["preferredtarget"] = generatePreferredTarget(*x);
game.matchedGame.preferredTarget = generatePreferredTarget(*x);
game.matchedGame.setGUIOptions(x->game.guioptions + MidiDriver::musicType2GUIO(x->game.midi));
game.matchedGame.appendGUIOptions(getGameGUIOptionsDescriptionLanguage(x->language));

View File

@ -284,7 +284,7 @@ Common::Error Sword2MetaEngine::createInstance(OSystem *syst, Engine **engine) c
DetectedGames detectedGames = detectGames(fslist);
for (uint i = 0; i < detectedGames.size(); i++) {
if (detectedGames[i].matchedGame.gameid() == gameid) {
if (detectedGames[i].matchedGame.gameId == gameid) {
*engine = new Sword2::Sword2Engine(syst);
return Common::kNoError;
}

View File

@ -376,11 +376,20 @@ void LauncherDialog::addGame() {
} while (looping);
}
namespace {
static void addStringToConf(const Common::String &key, const Common::String &value, const Common::String &domain) {
if (!value.empty())
ConfMan.set(key, value, domain);
}
} // End of anonymous namespace
Common::String addGameToConf(const GameDescriptor &result) {
// The auto detector or the user made a choice.
// Pick a domain name which does not yet exist (after all, we
// are *adding* a game to the config, not replacing).
Common::String domain = result.preferredtarget();
Common::String domain = result.preferredTarget;
assert(!domain.empty());
if (ConfMan.hasGameDomain(domain)) {
@ -396,11 +405,15 @@ Common::String addGameToConf(const GameDescriptor &result) {
// Add the name domain
ConfMan.addGameDomain(domain);
// Copy all non-empty key/value pairs into the new domain
for (GameDescriptor::const_iterator iter = result.begin(); iter != result.end(); ++iter) {
if (!iter->_value.empty() && iter->_key != "preferredtarget")
ConfMan.set(iter->_key, iter->_value, domain);
}
// Copy all non-empty relevant values into the new domain
// FIXME: Factor out
addStringToConf("gameid", result.gameId, domain);
addStringToConf("description", result.description, domain);
addStringToConf("language", Common::getLanguageCode(result.language), domain);
addStringToConf("platform", Common::getPlatformCode(result.platform), domain);
addStringToConf("path", result.path, domain);
addStringToConf("extra", result.extra, domain);
addStringToConf("guioptions", result.getGUIOptions(), domain);
// TODO: Setting the description field here has the drawback
// that the user does never notice when we upgrade our descriptions.
@ -601,7 +614,7 @@ bool LauncherDialog::doGameDetection(const Common::String &path) {
// Display the candidates to the user and let her/him pick one
StringArray list;
for (idx = 0; idx < (int)candidates.size(); idx++)
list.push_back(candidates[idx].matchedGame.description());
list.push_back(candidates[idx].matchedGame.description);
ChooserDialog dialog(_("Pick the game:"));
dialog.setList(list);

View File

@ -121,13 +121,13 @@ MassAddDialog::MassAddDialog(const Common::FSNode &startDir)
struct GameTargetLess {
bool operator()(const GameDescriptor &x, const GameDescriptor &y) const {
return x.preferredtarget().compareToIgnoreCase(y.preferredtarget()) < 0;
return x.preferredTarget.compareToIgnoreCase(y.preferredTarget) < 0;
}
};
struct GameDescLess {
bool operator()(const GameDescriptor &x, const GameDescriptor &y) const {
return x.description().compareToIgnoreCase(y.description()) < 0;
return x.description.compareToIgnoreCase(y.description) < 0;
}
};
@ -143,13 +143,13 @@ void MassAddDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data
if (cmd == kOkCmd) {
// Sort the detected games. This is not strictly necessary, but nice for
// people who want to edit their config file by hand after a mass add.
sort(_games.begin(), _games.end(), GameTargetLess());
Common::sort(_games.begin(), _games.end(), GameTargetLess());
// Add all the detected games to the config
for (GameList::iterator iter = _games.begin(); iter != _games.end(); ++iter) {
debug(1, " Added gameid '%s', desc '%s'\n",
(*iter)["gameid"].c_str(),
(*iter)["description"].c_str());
(*iter)["gameid"] = addGameToConf(*iter);
iter->gameId.c_str(),
iter->description.c_str());
iter->gameId = addGameToConf(*iter);
}
// Write everything to disk
@ -157,8 +157,8 @@ void MassAddDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data
// And scroll to first detected game
if (!_games.empty()) {
sort(_games.begin(), _games.end(), GameDescLess());
ConfMan.set("temp_selection", _games.front().gameid());
Common::sort(_games.begin(), _games.end(), GameDescLess());
ConfMan.set("temp_selection", _games.front().gameId);
}
close();
@ -211,6 +211,9 @@ void MassAddDialog::handleTickle() {
// Check for existing config entries for this path/gameid/lang/platform combination
if (_pathToTargets.contains(path)) {
const char *resultPlatformCode = Common::getPlatformCode(result.platform);
const char *resultLanguageCode = Common::getLanguageCode(result.language);
bool duplicate = false;
const StringArray &targets = _pathToTargets[path];
for (StringArray::const_iterator iter = targets.begin(); iter != targets.end(); ++iter) {
@ -218,9 +221,9 @@ void MassAddDialog::handleTickle() {
Common::ConfigManager::Domain *dom = ConfMan.getDomain(*iter);
assert(dom);
if ((*dom)["gameid"] == result["gameid"] &&
(*dom)["platform"] == result["platform"] &&
(*dom)["language"] == result["language"]) {
if ((*dom)["gameid"] == result.gameId &&
(*dom)["platform"] == resultPlatformCode &&
(*dom)["language"] == resultLanguageCode) {
duplicate = true;
break;
}
@ -232,7 +235,7 @@ void MassAddDialog::handleTickle() {
}
_games.push_back(result);
_list->append(result.description());
_list->append(result.description);
}

View File

@ -44,7 +44,7 @@ public:
Common::String getFirstAddedTarget() const {
if (!_games.empty())
return _games.front().gameid();
return _games.front().gameId;
return Common::String();
}