Fix reading songs

This commit is contained in:
Marcus Huderle 2018-02-11 18:24:48 -08:00
parent 67e5b40b4b
commit 26d4f234aa
2 changed files with 44 additions and 19 deletions

View file

@ -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<QStringList> *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<QString, int> 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<QStringList> *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<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
// Loop through song defines, and fine the one with the matching song number.
QMap<QString, int>::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<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
QMap<QString, int> defines;
QString combinedPrefixes = "[" + prefixes.join('|') + "]";
QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\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;
}

View file

@ -70,6 +70,7 @@ public:
QStringList readCArray(QString text, QString label);
QString readCIncbin(QString text, QString label);
QMap<QString, int> readCDefines(QString text, QStringList prefixes);
};
#endif // PROJECT_H