diff --git a/include/core/parseutil.h b/include/core/parseutil.h index 96d56c27..5cb07366 100644 --- a/include/core/parseutil.h +++ b/include/core/parseutil.h @@ -71,7 +71,7 @@ public: static QString removeLineComments(QString text, const QStringList &commentSymbols); static QStringList splitShellCommand(QStringView command); - static bool gameStringToBool(QString gameString); + static bool gameStringToBool(QString gameString, bool * ok = nullptr); private: QString root; diff --git a/include/project.h b/include/project.h index 59f7a798..0828284f 100644 --- a/include/project.h +++ b/include/project.h @@ -167,6 +167,8 @@ public: QString defaultSong; QStringList getVisibilities(); + void insertTilesetLabel(QString label, bool isSecondary); + void insertTilesetLabel(QString label, QString isSecondaryStr); bool readTilesetLabels(); bool readTilesetProperties(); bool readMaxMapDataSize(); diff --git a/src/core/parseutil.cpp b/src/core/parseutil.cpp index 485154bc..63fbd84d 100644 --- a/src/core/parseutil.cpp +++ b/src/core/parseutil.cpp @@ -402,15 +402,13 @@ QMap ParseUtil::readNamedIndexCArray(const QString &filename, return map; } -bool ParseUtil::gameStringToBool(QString gameString) { +bool ParseUtil::gameStringToBool(QString gameString, bool * ok) { + if (ok) *ok = true; if (QString::compare(gameString, "TRUE", Qt::CaseInsensitive) == 0) return true; if (QString::compare(gameString, "FALSE", Qt::CaseInsensitive) == 0) return false; - bool ok; - int num = gameString.toInt(&ok); - if (!ok) logWarn(QString("Unable to convert string '%1' to bool, will be interpreted as 'FALSE'").arg(gameString)); - return num != 0; + return gameString.toInt(ok) != 0; } QMap> ParseUtil::readCStructs(const QString &filename, const QString &label, const QHash memberMap) { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d930ed77..c9406d30 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1342,13 +1342,10 @@ void MainWindow::on_actionNew_Tileset_triggered() { if(!createTilesetDialog->isSecondary) { this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName); - editor->project->tilesetLabels["primary"].append(createTilesetDialog->fullSymbolName); - } else { this->ui->comboBox_SecondaryTileset->addItem(createTilesetDialog->fullSymbolName); - editor->project->tilesetLabels["secondary"].append(createTilesetDialog->fullSymbolName); } - editor->project->tilesetLabelsOrdered.append(createTilesetDialog->fullSymbolName); + editor->project->insertTilesetLabel(createTilesetDialog->fullSymbolName, createTilesetDialog->isSecondary); QMessageBox msgBox(this); msgBox.setText("Successfully created tileset."); diff --git a/src/project.cpp b/src/project.cpp index 8cd96c34..55243012 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1930,6 +1930,22 @@ Project::DataQualifiers Project::getDataQualifiers(QString text, QString label) return qualifiers; } +void Project::insertTilesetLabel(QString label, bool isSecondary) { + QString category = isSecondary ? "secondary" : "primary"; + this->tilesetLabels[category].append(label); + this->tilesetLabelsOrdered.append(label); +} + +void Project::insertTilesetLabel(QString label, QString isSecondaryStr) { + bool ok; + bool isSecondary = ParseUtil::gameStringToBool(isSecondaryStr, &ok); + if (!ok) { + logError(QString("Unable to convert value '%1' of isSecondary to bool for tileset %2.").arg(isSecondaryStr).arg(label)); + return; + } + insertTilesetLabel(label, isSecondary); +} + bool Project::readTilesetLabels() { QStringList primaryTilesets; QStringList secondaryTilesets; @@ -1953,25 +1969,16 @@ bool Project::readTilesetLabels() { QRegularExpressionMatchIterator iter = re.globalMatch(text); while (iter.hasNext()) { QRegularExpressionMatch match = iter.next(); - QString tilesetLabel = match.captured("label"); - if (ParseUtil::gameStringToBool(match.captured("isSecondary"))) - this->tilesetLabels["secondary"].append(tilesetLabel); - else - this->tilesetLabels["primary"].append(tilesetLabel); - this->tilesetLabelsOrdered.append(tilesetLabel); + insertTilesetLabel(match.captured("label"), match.captured("isSecondary")); } filename = asm_filename; // For error reporting further down } else { this->usingAsmTilesets = false; const auto structs = parser.readCStructs(filename, "", Tileset::getHeaderMemberMap(this->usingAsmTilesets)); QStringList labels = structs.keys(); - for (const auto tilesetLabel : labels) { - if (tilesetLabel.isEmpty()) continue; - if (ParseUtil::gameStringToBool(structs[tilesetLabel].value("isSecondary"))) - this->tilesetLabels["secondary"].append(tilesetLabel); - else - this->tilesetLabels["primary"].append(tilesetLabel); - this->tilesetLabelsOrdered.append(tilesetLabel); // TODO: This is alphabetical, AdvanceMap import wants the vanilla order + // TODO: This is alphabetical, AdvanceMap import wants the vanilla order in tilesetLabelsOrdered + for (const auto tilesetLabel : labels){ + insertTilesetLabel(tilesetLabel, structs[tilesetLabel].value("isSecondary")); } }