Преглед изворни кода

Populate flag and var dropdown menus with their respective constants

Marcus Huderle пре 6 година
родитељ
комит
669c81b50b
3 измењених фајлова са 78 додато и 29 уклоњено
  1. 16
    4
      mainwindow.cpp
  2. 55
    25
      project.cpp
  3. 7
    0
      project.h

+ 16
- 4
mainwindow.cpp Прегледај датотеку

@@ -285,6 +285,8 @@ void MainWindow::loadDataStructures() {
285 285
     project->readMapAttributesTable();
286 286
     project->readAllMapAttributes();
287 287
     project->readItemNames();
288
+    project->readFlagNames();
289
+    project->readVarNames();
288 290
 }
289 291
 
290 292
 void MainWindow::populateMapList() {
@@ -570,8 +572,8 @@ void MainWindow::updateSelectedObjects() {
570 572
         field_labels["sight_radius"] = "Sight Radius";
571 573
         field_labels["destination_warp"] = "Destination Warp";
572 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 577
         field_labels["type"] = "Type";
576 578
         field_labels["item"] = "Item";
577 579
         field_labels["item_unknown5"] = "Unknown 5";
@@ -614,8 +616,8 @@ void MainWindow::updateSelectedObjects() {
614 616
         }
615 617
         else if (event_type == "trap") {
616 618
             fields << "script_label";
617
-            fields << "coord_unknown1";
618
-            fields << "coord_unknown2";
619
+            fields << "script_var";
620
+            fields << "script_var_value";
619 621
         }
620 622
         else if (event_type == "trap_weather") {
621 623
             fields << "weather";
@@ -650,6 +652,16 @@ void MainWindow::updateSelectedObjects() {
650 652
                     combo->addItem(value);
651 653
                 }
652 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 665
             } else {
654 666
                 combo->addItem(value);
655 667
             }

+ 55
- 25
project.cpp Прегледај датотеку

@@ -19,6 +19,8 @@ Project::Project()
19 19
     map_groups = new QMap<QString, int>;
20 20
     mapNames = new QStringList;
21 21
     itemNames = new QStringList;
22
+    flagNames = new QStringList;
23
+    varNames = new QStringList;
22 24
     map_cache = new QMap<QString, Map*>;
23 25
     mapConstantsToMapNames = new QMap<QString, QString>;
24 26
     mapNamesToMapConstants = new QMap<QString, QString>;
@@ -1064,25 +1066,39 @@ QStringList Project::getBattleScenes() {
1064 1066
 }
1065 1067
 
1066 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 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 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,8 +1268,8 @@ void Project::saveMapEvents(Map *map) {
1252 1268
             text += QString(", %1").arg(coords->get("y"));
1253 1269
             text += QString(", %1").arg(coords->get("elevation"));
1254 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 1273
             text += QString(", 0");
1258 1274
             text += QString(", %1").arg(coords->get("script_label"));
1259 1275
             text += "\n";
@@ -1413,8 +1429,8 @@ void Project::readMapEvents(Map *map) {
1413 1429
             coord->put("x", command.value(i++));
1414 1430
             coord->put("y", command.value(i++));
1415 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 1434
             coord->put("script_label", command.value(i++));
1419 1435
             //coord_unknown3
1420 1436
             //coord_unknown4
@@ -1539,23 +1555,37 @@ QString Project::readCIncbin(QString text, QString label) {
1539 1555
 }
1540 1556
 
1541 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 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 1573
     QRegularExpressionMatchIterator iter = re.globalMatch(text);
1547 1574
     while (iter.hasNext()) {
1548 1575
         QRegularExpressionMatch match = iter.next();
1549 1576
         QString name = match.captured("defineName");
1577
+        QString hardcodedDefineName = match.captured("hardcodedDefineName");
1550 1578
         QString value = match.captured("defineValue");
1551 1579
         bool valid;
1552 1580
         int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
1553 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 1589
         } else {
1560 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 Прегледај датотеку

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