Browse Source

Populate flag and var dropdown menus with their respective constants

Marcus Huderle 6 years ago
parent
commit
669c81b50b
3 changed files with 78 additions and 29 deletions
  1. 16
    4
      mainwindow.cpp
  2. 55
    25
      project.cpp
  3. 7
    0
      project.h

+ 16
- 4
mainwindow.cpp View File

285
     project->readMapAttributesTable();
285
     project->readMapAttributesTable();
286
     project->readAllMapAttributes();
286
     project->readAllMapAttributes();
287
     project->readItemNames();
287
     project->readItemNames();
288
+    project->readFlagNames();
289
+    project->readVarNames();
288
 }
290
 }
289
 
291
 
290
 void MainWindow::populateMapList() {
292
 void MainWindow::populateMapList() {
570
         field_labels["sight_radius"] = "Sight Radius";
572
         field_labels["sight_radius"] = "Sight Radius";
571
         field_labels["destination_warp"] = "Destination Warp";
573
         field_labels["destination_warp"] = "Destination Warp";
572
         field_labels["destination_map_name"] = "Destination Map";
574
         field_labels["destination_map_name"] = "Destination Map";
573
-        field_labels["coord_unknown1"] = "Unknown 1";
574
-        field_labels["coord_unknown2"] = "Unknown 2";
575
+        field_labels["script_var"] = "Var";
576
+        field_labels["script_var_value"] = "Var Value";
575
         field_labels["type"] = "Type";
577
         field_labels["type"] = "Type";
576
         field_labels["item"] = "Item";
578
         field_labels["item"] = "Item";
577
         field_labels["item_unknown5"] = "Unknown 5";
579
         field_labels["item_unknown5"] = "Unknown 5";
614
         }
616
         }
615
         else if (event_type == "trap") {
617
         else if (event_type == "trap") {
616
             fields << "script_label";
618
             fields << "script_label";
617
-            fields << "coord_unknown1";
618
-            fields << "coord_unknown2";
619
+            fields << "script_var";
620
+            fields << "script_var_value";
619
         }
621
         }
620
         else if (event_type == "trap_weather") {
622
         else if (event_type == "trap_weather") {
621
             fields << "weather";
623
             fields << "weather";
650
                     combo->addItem(value);
652
                     combo->addItem(value);
651
                 }
653
                 }
652
                 combo->addItems(*editor->project->itemNames);
654
                 combo->addItems(*editor->project->itemNames);
655
+            } else if (key == "flag") {
656
+                if (!editor->project->flagNames->contains(value)) {
657
+                    combo->addItem(value);
658
+                }
659
+                combo->addItems(*editor->project->flagNames);
660
+            } else if (key == "script_var") {
661
+                if (!editor->project->varNames->contains(value)) {
662
+                    combo->addItem(value);
663
+                }
664
+                combo->addItems(*editor->project->varNames);
653
             } else {
665
             } else {
654
                 combo->addItem(value);
666
                 combo->addItem(value);
655
             }
667
             }

+ 55
- 25
project.cpp View File

19
     map_groups = new QMap<QString, int>;
19
     map_groups = new QMap<QString, int>;
20
     mapNames = new QStringList;
20
     mapNames = new QStringList;
21
     itemNames = new QStringList;
21
     itemNames = new QStringList;
22
+    flagNames = new QStringList;
23
+    varNames = new QStringList;
22
     map_cache = new QMap<QString, Map*>;
24
     map_cache = new QMap<QString, Map*>;
23
     mapConstantsToMapNames = new QMap<QString, QString>;
25
     mapConstantsToMapNames = new QMap<QString, QString>;
24
     mapNamesToMapConstants = new QMap<QString, QString>;
26
     mapNamesToMapConstants = new QMap<QString, QString>;
1064
 }
1066
 }
1065
 
1067
 
1066
 void Project::readItemNames() {
1068
 void Project::readItemNames() {
1067
-    QString text = readTextFile(root + "/include/constants/items.h");
1069
+    QString filepath = root + "/include/constants/items.h";
1070
+    QStringList prefixes = (QStringList() << "ITEM_");
1071
+    readCDefinesSorted(filepath, prefixes, itemNames);
1072
+}
1073
+
1074
+void Project::readFlagNames() {
1075
+    QString filepath = root + "/include/constants/flags.h";
1076
+    QStringList prefixes = (QStringList() << "FLAG_");
1077
+    readCDefinesSorted(filepath, prefixes, flagNames, "SYSTEM_FLAGS", 0x800);
1078
+}
1079
+
1080
+void Project::readVarNames() {
1081
+    QString filepath = root + "/include/constants/vars.h";
1082
+    QStringList prefixes = (QStringList() << "VAR_");
1083
+    readCDefinesSorted(filepath, prefixes, varNames);
1084
+}
1085
+
1086
+void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QStringList* definesToSet) {
1087
+    return readCDefinesSorted(filepath, prefixes, definesToSet, "", 0);
1088
+}
1089
+
1090
+void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QStringList* definesToSet, QString hardcodedDefine, int hardcodedDefineValue) {
1091
+    QString text = readTextFile(filepath);
1068
     if (!text.isNull()) {
1092
     if (!text.isNull()) {
1069
-        QStringList itemDefinePrefixes;
1070
-        itemDefinePrefixes << "ITEM_";
1071
-        QMap<QString, int> itemDefines = readCDefines(text, itemDefinePrefixes);
1093
+        QMap<QString, int> defines = readCDefines(text, prefixes, hardcodedDefine, hardcodedDefineValue);
1072
 
1094
 
1073
-        // The item names should to be sorted by their underlying value, not alphabetically.
1095
+        // The defines should to be sorted by their underlying value, not alphabetically.
1074
         // Reverse the map and read out the resulting keys in order.
1096
         // Reverse the map and read out the resulting keys in order.
1075
-        QMultiMap<int, QString> itemDefinesInverse;
1076
-        for (QString itemName : itemDefines.keys()) {
1077
-            itemDefinesInverse.insert(itemDefines[itemName], itemName);
1078
-        }
1079
-
1080
-        for (int itemValue : itemDefinesInverse.keys()) {
1081
-            QList<QString> names = itemDefinesInverse.values(itemValue);
1082
-            for (QString name : names) {
1083
-                itemNames->append(name);
1084
-            }
1097
+        QMultiMap<int, QString> definesInverse;
1098
+        for (QString defineName : defines.keys()) {
1099
+            definesInverse.insert(defines[defineName], defineName);
1085
         }
1100
         }
1101
+        *definesToSet = definesInverse.values();
1086
     }
1102
     }
1087
 }
1103
 }
1088
 
1104
 
1252
             text += QString(", %1").arg(coords->get("y"));
1268
             text += QString(", %1").arg(coords->get("y"));
1253
             text += QString(", %1").arg(coords->get("elevation"));
1269
             text += QString(", %1").arg(coords->get("elevation"));
1254
             text += QString(", 0");
1270
             text += QString(", 0");
1255
-            text += QString(", %1").arg(coords->get("coord_unknown1"));
1256
-            text += QString(", %1").arg(coords->get("coord_unknown2"));
1271
+            text += QString(", %1").arg(coords->get("script_var"));
1272
+            text += QString(", %1").arg(coords->get("script_var_value"));
1257
             text += QString(", 0");
1273
             text += QString(", 0");
1258
             text += QString(", %1").arg(coords->get("script_label"));
1274
             text += QString(", %1").arg(coords->get("script_label"));
1259
             text += "\n";
1275
             text += "\n";
1413
             coord->put("x", command.value(i++));
1429
             coord->put("x", command.value(i++));
1414
             coord->put("y", command.value(i++));
1430
             coord->put("y", command.value(i++));
1415
             coord->put("elevation", command.value(i++));
1431
             coord->put("elevation", command.value(i++));
1416
-            coord->put("coord_unknown1", command.value(i++));
1417
-            coord->put("coord_unknown2", command.value(i++));
1432
+            coord->put("script_var", command.value(i++));
1433
+            coord->put("script_var_value", command.value(i++));
1418
             coord->put("script_label", command.value(i++));
1434
             coord->put("script_label", command.value(i++));
1419
             //coord_unknown3
1435
             //coord_unknown3
1420
             //coord_unknown4
1436
             //coord_unknown4
1539
 }
1555
 }
1540
 
1556
 
1541
 QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
1557
 QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
1558
+    return readCDefines(text, prefixes, "", 0);
1559
+}
1560
+
1561
+QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes, QString hardcodedDefine, int hardcodedDefineValue) {
1542
     QMap<QString, int> defines;
1562
     QMap<QString, int> defines;
1543
 
1563
 
1544
-    QString combinedPrefixes = "[" + prefixes.join('|') + "]";
1545
-    QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
1564
+    QString combinedPrefixes = "(" + prefixes.join('|') + ")";
1565
+    QString regex;
1566
+    if (hardcodedDefine.isEmpty()) {
1567
+        regex = QString("#define\\s+(?<defineName>(%1)\\w+)\\s+(?<defineValue>\\w+)").arg(combinedPrefixes);
1568
+    } else {
1569
+        regex = QString("#define\\s+(?<defineName>(%1)\\w+)\\s+\\(*(?<hardcodedDefineName>\\\s*%2\\s+\\+\\s+)*(?<defineValue>\\w+\)\\)*").arg(combinedPrefixes, hardcodedDefine);
1570
+    }
1571
+
1572
+    QRegularExpression re(regex);
1546
     QRegularExpressionMatchIterator iter = re.globalMatch(text);
1573
     QRegularExpressionMatchIterator iter = re.globalMatch(text);
1547
     while (iter.hasNext()) {
1574
     while (iter.hasNext()) {
1548
         QRegularExpressionMatch match = iter.next();
1575
         QRegularExpressionMatch match = iter.next();
1549
         QString name = match.captured("defineName");
1576
         QString name = match.captured("defineName");
1577
+        QString hardcodedDefineName = match.captured("hardcodedDefineName");
1550
         QString value = match.captured("defineValue");
1578
         QString value = match.captured("defineValue");
1551
         bool valid;
1579
         bool valid;
1552
         int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
1580
         int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
1553
         if (valid) {
1581
         if (valid) {
1554
-            if (!defines.contains(name)) {
1555
-                defines.insert(name, parsedValue);
1556
-            } else {
1557
-                qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
1582
+            int actualValue = parsedValue;
1583
+            if (!hardcodedDefine.isEmpty() && !hardcodedDefineName.isEmpty()) {
1584
+                actualValue += hardcodedDefineValue;
1558
             }
1585
             }
1586
+            defines.insert(name, actualValue);
1587
+        } else if (defines.contains(value)) {
1588
+            defines.insert(name, defines.value(value));
1559
         } else {
1589
         } else {
1560
             qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
1590
             qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
1561
         }
1591
         }

+ 7
- 0
project.h View File

24
     QMap<QString, QMap<QString, QString>> mapAttributes;
24
     QMap<QString, QMap<QString, QString>> mapAttributes;
25
     QMap<QString, QMap<QString, QString>> mapAttributesMaster;
25
     QMap<QString, QMap<QString, QString>> mapAttributesMaster;
26
     QStringList *itemNames = NULL;
26
     QStringList *itemNames = NULL;
27
+    QStringList *flagNames = NULL;
28
+    QStringList *varNames = NULL;
27
 
29
 
28
     QMap<QString, Map*> *map_cache;
30
     QMap<QString, Map*> *map_cache;
29
     Map* loadMap(QString);
31
     Map* loadMap(QString);
74
     QStringList getMapTypes();
76
     QStringList getMapTypes();
75
     QStringList getBattleScenes();
77
     QStringList getBattleScenes();
76
     void readItemNames();
78
     void readItemNames();
79
+    void readFlagNames();
80
+    void readVarNames();
77
 
81
 
78
     void loadObjectPixmaps(QList<Event*> objects);
82
     void loadObjectPixmaps(QList<Event*> objects);
79
     QMap<QString, int> getMapObjGfxConstants();
83
     QMap<QString, int> getMapObjGfxConstants();
90
     QStringList readCArray(QString text, QString label);
94
     QStringList readCArray(QString text, QString label);
91
     QString readCIncbin(QString text, QString label);
95
     QString readCIncbin(QString text, QString label);
92
     QMap<QString, int> readCDefines(QString text, QStringList prefixes);
96
     QMap<QString, int> readCDefines(QString text, QStringList prefixes);
97
+    QMap<QString, int> readCDefines(QString text, QStringList prefixes, QString hardcodedDefine, int hardcodedDefineValue);
93
 private:
98
 private:
94
     QString getMapAttributesTableFilepath();
99
     QString getMapAttributesTableFilepath();
95
     QString getMapAssetsFilepath();
100
     QString getMapAssetsFilepath();
96
     void saveMapHeader(Map*);
101
     void saveMapHeader(Map*);
97
     void saveMapAttributesTable();
102
     void saveMapAttributesTable();
98
     void updateMapAttributes(Map* map);
103
     void updateMapAttributes(Map* map);
104
+    void readCDefinesSorted(QString, QStringList, QStringList*);
105
+    void readCDefinesSorted(QString, QStringList, QStringList*, QString, int);
99
 
106
 
100
     void setNewMapHeader(Map* map, int mapIndex);
107
     void setNewMapHeader(Map* map, int mapIndex);
101
     void setNewMapAttributes(Map* map);
108
     void setNewMapAttributes(Map* map);