Auto-detect more version names

This commit is contained in:
GriffinR 2024-07-15 21:10:54 -04:00
parent db598a43db
commit 29ed696d9e
4 changed files with 41 additions and 23 deletions

View file

@ -158,6 +158,7 @@ private:
extern PorymapConfig porymapConfig; extern PorymapConfig porymapConfig;
enum BaseGameVersion { enum BaseGameVersion {
none,
pokeruby, pokeruby,
pokefirered, pokefirered,
pokeemerald, pokeemerald,
@ -303,7 +304,7 @@ public:
static const QMap<ProjectIdentifier, QPair<QString, QString>> defaultIdentifiers; static const QMap<ProjectIdentifier, QPair<QString, QString>> defaultIdentifiers;
static const QMap<ProjectFilePath, QPair<QString, QString>> defaultPaths; static const QMap<ProjectFilePath, QPair<QString, QString>> defaultPaths;
static const QStringList versionStrings; static const QStringList versionStrings;
static BaseGameVersion stringToBaseGameVersion(QString string, bool * ok = nullptr); static BaseGameVersion stringToBaseGameVersion(const QString &string);
void reset(BaseGameVersion baseGameVersion); void reset(BaseGameVersion baseGameVersion);
void setFilePath(ProjectFilePath pathId, const QString &path); void setFilePath(ProjectFilePath pathId, const QString &path);

View file

@ -608,20 +608,32 @@ const QMap<BaseGameVersion, QString> baseGameVersionMap = {
{BaseGameVersion::pokeemerald, ProjectConfig::versionStrings[2]}, {BaseGameVersion::pokeemerald, ProjectConfig::versionStrings[2]},
}; };
const QMap<QString, BaseGameVersion> baseGameVersionReverseMap = { const QMap<BaseGameVersion, QStringList> versionDetectNames = {
{ProjectConfig::versionStrings[0], BaseGameVersion::pokeruby}, {BaseGameVersion::pokeruby, {"ruby", "sapphire"}},
{ProjectConfig::versionStrings[1], BaseGameVersion::pokefirered}, {BaseGameVersion::pokefirered, {"firered", "leafgreen"}},
{ProjectConfig::versionStrings[2], BaseGameVersion::pokeemerald}, {BaseGameVersion::pokeemerald, {"emerald"}},
}; };
BaseGameVersion ProjectConfig::stringToBaseGameVersion(QString string, bool * ok) { // If a string exclusively contains one version name we assume its identity,
if (baseGameVersionReverseMap.contains(string)) { // otherwise we leave it unknown and we'll need the user to tell us the version.
if (ok) *ok = true; BaseGameVersion ProjectConfig::stringToBaseGameVersion(const QString &string) {
return baseGameVersionReverseMap.value(string); BaseGameVersion version = BaseGameVersion::none;
} else { for (auto i = versionDetectNames.cbegin(), end = versionDetectNames.cend(); i != end; i++) {
if (ok) *ok = false; // Compare the given string to all the possible names for this game version
return BaseGameVersion::pokeemerald; 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; ProjectConfig projectConfig;
@ -633,10 +645,11 @@ QString ProjectConfig::getConfigFilepath() {
void ProjectConfig::parseConfigKeyValue(QString key, QString value) { void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
if (key == "base_game_version") { if (key == "base_game_version") {
bool ok; this->baseGameVersion = this->stringToBaseGameVersion(value.toLower());
this->baseGameVersion = this->stringToBaseGameVersion(value.toLower(), &ok); if (this->baseGameVersion == BaseGameVersion::none) {
if (!ok)
logWarn(QString("Invalid config value for base_game_version: '%1'. Must be 'pokeruby', 'pokefirered' or 'pokeemerald'.").arg(value)); 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") { } else if (key == "use_poryscript") {
this->usePoryScript = getConfigBool(key, value); this->usePoryScript = getConfigBool(key, value);
} else if (key == "use_custom_border_size") { } else if (key == "use_custom_border_size") {
@ -851,10 +864,11 @@ QMap<QString, QString> ProjectConfig::getKeyValueMap() {
void ProjectConfig::init() { void ProjectConfig::init() {
QString dirName = QDir(this->projectDir).dirName().toLower(); QString dirName = QDir(this->projectDir).dirName().toLower();
if (baseGameVersionReverseMap.contains(dirName)) {
// TODO: Improve detection (ex: emerald or pokeemerald-2 aren't currently auto-detected) BaseGameVersion version = stringToBaseGameVersion(dirName);
this->baseGameVersion = baseGameVersionReverseMap.value(dirName); if (version != BaseGameVersion::none) {
logInfo(QString("Auto-detected base_game_version as '%1'").arg(dirName)); this->baseGameVersion = version;
logInfo(QString("Auto-detected base_game_version as '%1'").arg(getBaseGameVersionString(version)));
} else { } else {
QDialog dialog(nullptr, Qt::WindowTitleHint); QDialog dialog(nullptr, Qt::WindowTitleHint);
dialog.setWindowTitle("Project Configuration"); dialog.setWindowTitle("Project Configuration");

View file

@ -338,6 +338,9 @@ bool Prefab::tryImportDefaultPrefabs(QWidget * parent, BaseGameVersion version,
case BaseGameVersion::pokeemerald: case BaseGameVersion::pokeemerald:
content = parser.readTextFile(":/text/prefabs_default_emerald.json"); content = parser.readTextFile(":/text/prefabs_default_emerald.json");
break; break;
default:
content = QString();
break;
} }
prefabsFile.write(content.toUtf8()); prefabsFile.write(content.toUtf8());

View file

@ -206,14 +206,14 @@ poryjson::Json RegionMapEditor::buildDefaultJson() {
case BaseGameVersion::pokeemerald: case BaseGameVersion::pokeemerald:
buildEmeraldDefaults(defaultJson); buildEmeraldDefaults(defaultJson);
break; break;
case BaseGameVersion::pokeruby: case BaseGameVersion::pokeruby:
buildRubyDefaults(defaultJson); buildRubyDefaults(defaultJson);
break; break;
case BaseGameVersion::pokefirered: case BaseGameVersion::pokefirered:
buildFireredDefaults(defaultJson); buildFireredDefaults(defaultJson);
break; break;
default:
break;
} }
return defaultJson; return defaultJson;
@ -315,7 +315,7 @@ bool RegionMapEditor::buildConfigDialog() {
QPushButton *delMapButton = new QPushButton("Delete Selected Region Map"); QPushButton *delMapButton = new QPushButton("Delete Selected Region Map");
form.addRow(delMapButton); form.addRow(delMapButton);
connect(delMapButton, &QPushButton::clicked, [this, regionMapList, &updateJsonFromList] { connect(delMapButton, &QPushButton::clicked, [regionMapList, &updateJsonFromList] {
QListWidgetItem *item = regionMapList->currentItem(); QListWidgetItem *item = regionMapList->currentItem();
if (item) { if (item) {
regionMapList->removeItemWidget(item); regionMapList->removeItemWidget(item);
@ -344,7 +344,7 @@ bool RegionMapEditor::buildConfigDialog() {
// for sake of convenience, option to just use defaults for each basegame version // for sake of convenience, option to just use defaults for each basegame version
QPushButton *config_useProjectDefault; QPushButton *config_useProjectDefault = nullptr;
switch (projectConfig.baseGameVersion) { switch (projectConfig.baseGameVersion) {
case BaseGameVersion::pokefirered: case BaseGameVersion::pokefirered:
config_useProjectDefault = new QPushButton("\nUse pokefirered defaults\n"); config_useProjectDefault = new QPushButton("\nUse pokefirered defaults\n");