Browse Source

Properly support saving map connections

Marcus Huderle 6 years ago
parent
commit
9b0f686781
4 changed files with 94 additions and 19 deletions
  1. 1
    0
      editor.cpp
  2. 1
    0
      mainwindow.cpp
  3. 87
    19
      project.cpp
  4. 5
    0
      project.h

+ 1
- 0
editor.cpp View File

@@ -409,6 +409,7 @@ void Editor::displayMapConnections() {
409 409
     for (ConnectionPixmapItem* item : connection_edit_items) {
410 410
         delete item;
411 411
     }
412
+    current_connection_edit_item = NULL;
412 413
     connection_edit_items.clear();
413 414
 
414 415
     for (Connection *connection : map->connections) {

+ 1
- 0
mainwindow.cpp View File

@@ -289,6 +289,7 @@ void MainWindow::loadDataStructures() {
289 289
     project->readItemNames();
290 290
     project->readFlagNames();
291 291
     project->readVarNames();
292
+    project->readMapsWithConnections();
292 293
 }
293 294
 
294 295
 void MainWindow::populateMapList() {

+ 87
- 19
project.cpp View File

@@ -197,7 +197,14 @@ void Project::saveMapHeader(Map *map) {
197 197
     text += QString("\t.4byte %1\n").arg(map->attributes_label);
198 198
     text += QString("\t.4byte %1\n").arg(map->events_label);
199 199
     text += QString("\t.4byte %1\n").arg(map->scripts_label);
200
+
201
+    if (map->connections.length() == 0) {
202
+        map->connections_label = "0x0";
203
+    } else {
204
+        map->connections_label = QString("%1_MapConnections").arg(map->name);
205
+    }
200 206
     text += QString("\t.4byte %1\n").arg(map->connections_label);
207
+
201 208
     text += QString("\t.2byte %1\n").arg(map->song);
202 209
     text += QString("\t.2byte %1\n").arg(map->index);
203 210
     text += QString("\t.byte %1\n").arg(map->location);
@@ -211,27 +218,45 @@ void Project::saveMapHeader(Map *map) {
211 218
 }
212 219
 
213 220
 void Project::saveMapConnections(Map *map) {
214
-    QString connections_path = root + "/data/maps/" + map->name + "/connections.inc";
215
-    QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name);
216
-    int numValidConnections = 0;
217
-    QString text = "";
218
-    text += QString("%1::\n").arg(connectionsListLabel);
219
-    for (Connection* connection : map->connections) {
220
-        if (mapNamesToMapConstants->contains(connection->map_name)) {
221
-            text += QString("\tconnection %1, %2, %3\n")
222
-                    .arg(connection->direction)
223
-                    .arg(connection->offset)
224
-                    .arg(mapNamesToMapConstants->value(connection->map_name));
225
-            numValidConnections++;
226
-        } else {
227
-            qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name);
221
+    QString path = root + "/data/maps/" + map->name + "/connections.inc";
222
+    if (map->connections.length() > 0) {
223
+        QString text = "";
224
+        QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name);
225
+        int numValidConnections = 0;
226
+        text += QString("%1::\n").arg(connectionsListLabel);
227
+        for (Connection* connection : map->connections) {
228
+            if (mapNamesToMapConstants->contains(connection->map_name)) {
229
+                text += QString("\tconnection %1, %2, %3\n")
230
+                        .arg(connection->direction)
231
+                        .arg(connection->offset)
232
+                        .arg(mapNamesToMapConstants->value(connection->map_name));
233
+                numValidConnections++;
234
+            } else {
235
+                qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name);
236
+            }
237
+        }
238
+        text += QString("\n");
239
+        text += QString("%1::\n").arg(map->connections_label);
240
+        text += QString("\t.4byte %1\n").arg(numValidConnections);
241
+        text += QString("\t.4byte %1\n").arg(connectionsListLabel);
242
+        saveTextFile(path, text);
243
+    } else {
244
+        deleteFile(path);
245
+    }
246
+
247
+    updateMapsWithConnections(map);
248
+}
249
+
250
+void Project::updateMapsWithConnections(Map *map) {
251
+    if (map->connections.length() > 0) {
252
+        if (!mapsWithConnections.contains(map->name)) {
253
+            mapsWithConnections.append(map->name);
254
+        }
255
+    } else {
256
+        if (mapsWithConnections.contains(map->name)) {
257
+            mapsWithConnections.removeOne(map->name);
228 258
         }
229 259
     }
230
-    text += QString("\n");
231
-    text += QString("%1::\n").arg(map->connections_label);
232
-    text += QString("\t.4byte %1\n").arg(numValidConnections);
233
-    text += QString("\t.4byte %1\n").arg(connectionsListLabel);
234
-    saveTextFile(connections_path, text);
235 260
 }
236 261
 
237 262
 void Project::readMapAttributesTable() {
@@ -728,6 +753,7 @@ void Project::saveAllDataStructures() {
728 753
     saveAllMapAttributes();
729 754
     saveMapGroupsTable();
730 755
     saveMapConstantsHeader();
756
+    saveMapsWithConnections();
731 757
 }
732 758
 
733 759
 void Project::loadTilesetAssets(Tileset* tileset) {
@@ -939,6 +965,13 @@ void Project::appendTextFile(QString path, QString text) {
939 965
     }
940 966
 }
941 967
 
968
+void Project::deleteFile(QString path) {
969
+    QFile file(path);
970
+    if (file.exists() && !file.remove()) {
971
+        qDebug() << QString("Could not delete file '%1': ").arg(path) + file.errorString();
972
+    }
973
+}
974
+
942 975
 void Project::readMapGroups() {
943 976
     QString text = readTextFile(root + "/data/maps/_groups.inc");
944 977
     if (text.isNull()) {
@@ -1124,6 +1157,41 @@ void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QString
1124 1157
     }
1125 1158
 }
1126 1159
 
1160
+void Project::readMapsWithConnections() {
1161
+    QString path = root + "/data/maps/connections.inc";
1162
+    QString text = readTextFile(path);
1163
+    if (text.isNull()) {
1164
+        return;
1165
+    }
1166
+
1167
+    mapsWithConnections.clear();
1168
+    QRegularExpression re("data\\/maps\\/(?<mapName>\\w+)\\/connections.inc");
1169
+    QList<QStringList>* includes = parseAsm(text);
1170
+    for (QStringList values : *includes) {
1171
+        if (values.length() != 2)
1172
+            continue;
1173
+
1174
+        QRegularExpressionMatch match = re.match(values.value(1));
1175
+        if (match.hasMatch()) {
1176
+            QString mapName = match.captured("mapName");
1177
+            mapsWithConnections.append(mapName);
1178
+        }
1179
+    }
1180
+}
1181
+
1182
+void Project::saveMapsWithConnections() {
1183
+    QString path = root + "/data/maps/connections.inc";
1184
+    QString text = "";
1185
+    for (QString mapName : mapsWithConnections) {
1186
+        if (mapNamesToMapConstants->contains(mapName)) {
1187
+            text += QString("\t.include \"data/maps/%1/connections.inc\"\n").arg(mapName);
1188
+        } else {
1189
+            qDebug() << QString("Failed to write connection include. %1 not a valid map name").arg(mapName);
1190
+        }
1191
+    }
1192
+    saveTextFile(path, text);
1193
+}
1194
+
1127 1195
 QStringList Project::getSongNames() {
1128 1196
     QStringList names;
1129 1197
     QString text = readTextFile(root + "/include/constants/songs.h");

+ 5
- 0
project.h View File

@@ -26,6 +26,7 @@ public:
26 26
     QStringList *itemNames = NULL;
27 27
     QStringList *flagNames = NULL;
28 28
     QStringList *varNames = NULL;
29
+    QStringList mapsWithConnections;
29 30
 
30 31
     QMap<QString, Map*> *map_cache;
31 32
     Map* loadMap(QString);
@@ -41,6 +42,7 @@ public:
41 42
     QString readTextFile(QString path);
42 43
     void saveTextFile(QString path, QString text);
43 44
     void appendTextFile(QString path, QString text);
45
+    void deleteFile(QString path);
44 46
 
45 47
     void readMapGroups();
46 48
     Map* addNewMapToGroup(QString mapName, int groupNum);
@@ -53,6 +55,7 @@ public:
53 55
     void readMapAttributesTable();
54 56
     void readAllMapAttributes();
55 57
     void readMapAttributes(Map*);
58
+    void readMapsWithConnections();
56 59
     void getTilesets(Map*);
57 60
     void loadTilesetAssets(Tileset*);
58 61
 
@@ -98,6 +101,8 @@ private:
98 101
     QString getMapAssetsFilepath();
99 102
     void saveMapHeader(Map*);
100 103
     void saveMapConnections(Map*);
104
+    void updateMapsWithConnections(Map*);
105
+    void saveMapsWithConnections();
101 106
     void saveMapAttributesTable();
102 107
     void updateMapAttributes(Map* map);
103 108
     void readCDefinesSorted(QString, QStringList, QStringList*);