From 29ed696d9ed2bad01b68d07d2957fb44ed9fa463 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 15 Jul 2024 21:10:54 -0400 Subject: [PATCH] Auto-detect more version names --- include/config.h | 3 ++- src/config.cpp | 50 ++++++++++++++++++++++++-------------- src/ui/prefab.cpp | 3 +++ src/ui/regionmapeditor.cpp | 8 +++--- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/include/config.h b/include/config.h index 89a27d24..7e87e986 100644 --- a/include/config.h +++ b/include/config.h @@ -158,6 +158,7 @@ private: extern PorymapConfig porymapConfig; enum BaseGameVersion { + none, pokeruby, pokefirered, pokeemerald, @@ -303,7 +304,7 @@ public: static const QMap> defaultIdentifiers; static const QMap> defaultPaths; static const QStringList versionStrings; - static BaseGameVersion stringToBaseGameVersion(QString string, bool * ok = nullptr); + static BaseGameVersion stringToBaseGameVersion(const QString &string); void reset(BaseGameVersion baseGameVersion); void setFilePath(ProjectFilePath pathId, const QString &path); diff --git a/src/config.cpp b/src/config.cpp index 1ee00c18..98681264 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -608,20 +608,32 @@ const QMap baseGameVersionMap = { {BaseGameVersion::pokeemerald, ProjectConfig::versionStrings[2]}, }; -const QMap baseGameVersionReverseMap = { - {ProjectConfig::versionStrings[0], BaseGameVersion::pokeruby}, - {ProjectConfig::versionStrings[1], BaseGameVersion::pokefirered}, - {ProjectConfig::versionStrings[2], BaseGameVersion::pokeemerald}, +const QMap versionDetectNames = { + {BaseGameVersion::pokeruby, {"ruby", "sapphire"}}, + {BaseGameVersion::pokefirered, {"firered", "leafgreen"}}, + {BaseGameVersion::pokeemerald, {"emerald"}}, }; -BaseGameVersion ProjectConfig::stringToBaseGameVersion(QString string, bool * ok) { - if (baseGameVersionReverseMap.contains(string)) { - if (ok) *ok = true; - return baseGameVersionReverseMap.value(string); - } else { - if (ok) *ok = false; - return BaseGameVersion::pokeemerald; +// If a string exclusively contains one version name we assume its identity, +// otherwise we leave it unknown and we'll need the user to tell us the version. +BaseGameVersion ProjectConfig::stringToBaseGameVersion(const QString &string) { + BaseGameVersion version = BaseGameVersion::none; + for (auto i = versionDetectNames.cbegin(), end = versionDetectNames.cend(); i != end; i++) { + // Compare the given string to all the possible names for this game version + const QStringList names = i.value(); + for (auto name : names) { + if (string.contains(name)) { + if (version != BaseGameVersion::none) { + // The given string matches multiple versions, so we can't be sure which it is. + return BaseGameVersion::none; + } + version = i.key(); + break; + } + } } + // We finished checking the names for each version; the name either matched 1 version or none. + return version; } ProjectConfig projectConfig; @@ -633,10 +645,11 @@ QString ProjectConfig::getConfigFilepath() { void ProjectConfig::parseConfigKeyValue(QString key, QString value) { if (key == "base_game_version") { - bool ok; - this->baseGameVersion = this->stringToBaseGameVersion(value.toLower(), &ok); - if (!ok) + this->baseGameVersion = this->stringToBaseGameVersion(value.toLower()); + if (this->baseGameVersion == BaseGameVersion::none) { logWarn(QString("Invalid config value for base_game_version: '%1'. Must be 'pokeruby', 'pokefirered' or 'pokeemerald'.").arg(value)); + this->baseGameVersion = BaseGameVersion::pokeemerald; + } } else if (key == "use_poryscript") { this->usePoryScript = getConfigBool(key, value); } else if (key == "use_custom_border_size") { @@ -851,10 +864,11 @@ QMap ProjectConfig::getKeyValueMap() { void ProjectConfig::init() { QString dirName = QDir(this->projectDir).dirName().toLower(); - if (baseGameVersionReverseMap.contains(dirName)) { - // TODO: Improve detection (ex: emerald or pokeemerald-2 aren't currently auto-detected) - this->baseGameVersion = baseGameVersionReverseMap.value(dirName); - logInfo(QString("Auto-detected base_game_version as '%1'").arg(dirName)); + + BaseGameVersion version = stringToBaseGameVersion(dirName); + if (version != BaseGameVersion::none) { + this->baseGameVersion = version; + logInfo(QString("Auto-detected base_game_version as '%1'").arg(getBaseGameVersionString(version))); } else { QDialog dialog(nullptr, Qt::WindowTitleHint); dialog.setWindowTitle("Project Configuration"); diff --git a/src/ui/prefab.cpp b/src/ui/prefab.cpp index 3559b9a8..594e811a 100644 --- a/src/ui/prefab.cpp +++ b/src/ui/prefab.cpp @@ -338,6 +338,9 @@ bool Prefab::tryImportDefaultPrefabs(QWidget * parent, BaseGameVersion version, case BaseGameVersion::pokeemerald: content = parser.readTextFile(":/text/prefabs_default_emerald.json"); break; + default: + content = QString(); + break; } prefabsFile.write(content.toUtf8()); diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 5cd0c9cb..0a5451a7 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -206,14 +206,14 @@ poryjson::Json RegionMapEditor::buildDefaultJson() { case BaseGameVersion::pokeemerald: buildEmeraldDefaults(defaultJson); break; - case BaseGameVersion::pokeruby: buildRubyDefaults(defaultJson); break; - case BaseGameVersion::pokefirered: buildFireredDefaults(defaultJson); break; + default: + break; } return defaultJson; @@ -315,7 +315,7 @@ bool RegionMapEditor::buildConfigDialog() { QPushButton *delMapButton = new QPushButton("Delete Selected Region Map"); form.addRow(delMapButton); - connect(delMapButton, &QPushButton::clicked, [this, regionMapList, &updateJsonFromList] { + connect(delMapButton, &QPushButton::clicked, [regionMapList, &updateJsonFromList] { QListWidgetItem *item = regionMapList->currentItem(); if (item) { regionMapList->removeItemWidget(item); @@ -344,7 +344,7 @@ bool RegionMapEditor::buildConfigDialog() { // for sake of convenience, option to just use defaults for each basegame version - QPushButton *config_useProjectDefault; + QPushButton *config_useProjectDefault = nullptr; switch (projectConfig.baseGameVersion) { case BaseGameVersion::pokefirered: config_useProjectDefault = new QPushButton("\nUse pokefirered defaults\n");