diff --git a/project.cpp b/project.cpp index a0a2f890..e38a1e72 100755 --- a/project.cpp +++ b/project.cpp @@ -577,33 +577,31 @@ QStringList Project::getBattleScenes() { QStringList Project::getSongNames() { QStringList names; - QString text = readTextFile(root + "/constants/songs.inc"); + QString text = readTextFile(root + "/include/constants/songs.h"); if (!text.isNull()) { - QList *commands = parse(text); - for (int i = 0; i < commands->length(); i++) { - QStringList params = commands->value(i); - QString macro = params.value(0); - if (macro == ".equiv") { - names.append(params.value(1)); - } - } + QStringList songDefinePrefixes; + songDefinePrefixes << "SE_" << "BGM_"; + QMap songDefines = readCDefines(text, songDefinePrefixes); + names = songDefines.keys(); } return names; } -QString Project::getSongName(int value) { +QString Project::getSongName(int songNumber) { QStringList names; - QString text = readTextFile(root + "/constants/songs.inc"); + QString text = readTextFile(root + "/include/constants/songs.h"); if (!text.isNull()) { - QList *commands = parse(text); - for (int i = 0; i < commands->length(); i++) { - QStringList params = commands->value(i); - QString macro = params.value(0); - if (macro == ".equiv") { - if (value == ((QString)(params.value(2))).toInt(nullptr, 0)) { - return params.value(1); - } + QStringList songDefinePrefixes; + songDefinePrefixes << "SE_" << "BGM_"; + QMap songDefines = readCDefines(text, songDefinePrefixes); + + // Loop through song defines, and fine the one with the matching song number. + QMap::iterator iter = songDefines.begin(); + while (iter != songDefines.end()) { + if (iter.value() == songNumber) { + return iter.key(); } + iter++; } } return ""; @@ -969,3 +967,29 @@ QString Project::readCIncbin(QString text, QString label) { return path; } + +QMap Project::readCDefines(QString text, QStringList prefixes) { + QMap defines; + + QString combinedPrefixes = "[" + prefixes.join('|') + "]"; + QRegularExpression re(QString("#define\\s+(?%1\\w+)\\s(?\\w+)").arg(combinedPrefixes)); + QRegularExpressionMatchIterator iter = re.globalMatch(text); + while (iter.hasNext()) { + QRegularExpressionMatch match = iter.next(); + QString name = match.captured("defineName"); + QString value = match.captured("defineValue"); + bool valid; + int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10); + if (valid) { + if (!defines.contains(name)) { + defines.insert(name, parsedValue); + } else { + qDebug() << QString("Define '%1' is defined multiple times'").arg(name); + } + } else { + qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value); + } + } + + return defines; +} diff --git a/project.h b/project.h index 24811e51..c06be738 100755 --- a/project.h +++ b/project.h @@ -70,6 +70,7 @@ public: QStringList readCArray(QString text, QString label); QString readCIncbin(QString text, QString label); + QMap readCDefines(QString text, QStringList prefixes); }; #endif // PROJECT_H