Browse Source

Fix reading songs

Marcus Huderle 6 years ago
parent
commit
26d4f234aa
2 changed files with 44 additions and 19 deletions
  1. 43
    19
      project.cpp
  2. 1
    0
      project.h

+ 43
- 19
project.cpp View File

577
 
577
 
578
 QStringList Project::getSongNames() {
578
 QStringList Project::getSongNames() {
579
     QStringList names;
579
     QStringList names;
580
-    QString text = readTextFile(root + "/constants/songs.inc");
580
+    QString text = readTextFile(root + "/include/constants/songs.h");
581
     if (!text.isNull()) {
581
     if (!text.isNull()) {
582
-        QList<QStringList> *commands = parse(text);
583
-        for (int i = 0; i < commands->length(); i++) {
584
-            QStringList params = commands->value(i);
585
-            QString macro = params.value(0);
586
-            if (macro == ".equiv") {
587
-                names.append(params.value(1));
588
-            }
589
-        }
582
+        QStringList songDefinePrefixes;
583
+        songDefinePrefixes << "SE_" << "BGM_";
584
+        QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
585
+        names = songDefines.keys();
590
     }
586
     }
591
     return names;
587
     return names;
592
 }
588
 }
593
 
589
 
594
-QString Project::getSongName(int value) {
590
+QString Project::getSongName(int songNumber) {
595
     QStringList names;
591
     QStringList names;
596
-    QString text = readTextFile(root + "/constants/songs.inc");
592
+    QString text = readTextFile(root + "/include/constants/songs.h");
597
     if (!text.isNull()) {
593
     if (!text.isNull()) {
598
-        QList<QStringList> *commands = parse(text);
599
-        for (int i = 0; i < commands->length(); i++) {
600
-            QStringList params = commands->value(i);
601
-            QString macro = params.value(0);
602
-            if (macro == ".equiv") {
603
-                if (value == ((QString)(params.value(2))).toInt(nullptr, 0)) {
604
-                    return params.value(1);
605
-                }
594
+        QStringList songDefinePrefixes;
595
+        songDefinePrefixes << "SE_" << "BGM_";
596
+        QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
597
+
598
+        // Loop through song defines, and fine the one with the matching song number.
599
+        QMap<QString, int>::iterator iter = songDefines.begin();
600
+        while (iter != songDefines.end()) {
601
+            if (iter.value() == songNumber) {
602
+                return iter.key();
606
             }
603
             }
604
+            iter++;
607
         }
605
         }
608
     }
606
     }
609
     return "";
607
     return "";
969
 
967
 
970
     return path;
968
     return path;
971
 }
969
 }
970
+
971
+QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
972
+    QMap<QString, int> defines;
973
+
974
+    QString combinedPrefixes = "[" + prefixes.join('|') + "]";
975
+    QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
976
+    QRegularExpressionMatchIterator iter = re.globalMatch(text);
977
+    while (iter.hasNext()) {
978
+        QRegularExpressionMatch match = iter.next();
979
+        QString name = match.captured("defineName");
980
+        QString value = match.captured("defineValue");
981
+        bool valid;
982
+        int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
983
+        if (valid) {
984
+            if (!defines.contains(name)) {
985
+                defines.insert(name, parsedValue);
986
+            } else {
987
+                qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
988
+            }
989
+        } else {
990
+            qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
991
+        }
992
+    }
993
+
994
+    return defines;
995
+}

+ 1
- 0
project.h View File

70
 
70
 
71
     QStringList readCArray(QString text, QString label);
71
     QStringList readCArray(QString text, QString label);
72
     QString readCIncbin(QString text, QString label);
72
     QString readCIncbin(QString text, QString label);
73
+    QMap<QString, int> readCDefines(QString text, QStringList prefixes);
73
 };
74
 };
74
 
75
 
75
 #endif // PROJECT_H
76
 #endif // PROJECT_H