From 9b0f6867815d23bd9b182995a17303e3e29412fb Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sun, 11 Mar 2018 13:33:08 -0700 Subject: [PATCH] Properly support saving map connections --- editor.cpp | 1 + mainwindow.cpp | 1 + project.cpp | 106 ++++++++++++++++++++++++++++++++++++++++--------- project.h | 5 +++ 4 files changed, 94 insertions(+), 19 deletions(-) diff --git a/editor.cpp b/editor.cpp index a2680c5d..d2ea5c65 100755 --- a/editor.cpp +++ b/editor.cpp @@ -409,6 +409,7 @@ void Editor::displayMapConnections() { for (ConnectionPixmapItem* item : connection_edit_items) { delete item; } + current_connection_edit_item = NULL; connection_edit_items.clear(); for (Connection *connection : map->connections) { diff --git a/mainwindow.cpp b/mainwindow.cpp index d7f076b3..f55a0b04 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -289,6 +289,7 @@ void MainWindow::loadDataStructures() { project->readItemNames(); project->readFlagNames(); project->readVarNames(); + project->readMapsWithConnections(); } void MainWindow::populateMapList() { diff --git a/project.cpp b/project.cpp index 9053b164..dc4dd11b 100755 --- a/project.cpp +++ b/project.cpp @@ -197,7 +197,14 @@ void Project::saveMapHeader(Map *map) { text += QString("\t.4byte %1\n").arg(map->attributes_label); text += QString("\t.4byte %1\n").arg(map->events_label); text += QString("\t.4byte %1\n").arg(map->scripts_label); + + if (map->connections.length() == 0) { + map->connections_label = "0x0"; + } else { + map->connections_label = QString("%1_MapConnections").arg(map->name); + } text += QString("\t.4byte %1\n").arg(map->connections_label); + text += QString("\t.2byte %1\n").arg(map->song); text += QString("\t.2byte %1\n").arg(map->index); text += QString("\t.byte %1\n").arg(map->location); @@ -211,27 +218,45 @@ void Project::saveMapHeader(Map *map) { } void Project::saveMapConnections(Map *map) { - QString connections_path = root + "/data/maps/" + map->name + "/connections.inc"; - QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name); - int numValidConnections = 0; - QString text = ""; - text += QString("%1::\n").arg(connectionsListLabel); - for (Connection* connection : map->connections) { - if (mapNamesToMapConstants->contains(connection->map_name)) { - text += QString("\tconnection %1, %2, %3\n") - .arg(connection->direction) - .arg(connection->offset) - .arg(mapNamesToMapConstants->value(connection->map_name)); - numValidConnections++; - } else { - qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name); + QString path = root + "/data/maps/" + map->name + "/connections.inc"; + if (map->connections.length() > 0) { + QString text = ""; + QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name); + int numValidConnections = 0; + text += QString("%1::\n").arg(connectionsListLabel); + for (Connection* connection : map->connections) { + if (mapNamesToMapConstants->contains(connection->map_name)) { + text += QString("\tconnection %1, %2, %3\n") + .arg(connection->direction) + .arg(connection->offset) + .arg(mapNamesToMapConstants->value(connection->map_name)); + numValidConnections++; + } else { + qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name); + } + } + text += QString("\n"); + text += QString("%1::\n").arg(map->connections_label); + text += QString("\t.4byte %1\n").arg(numValidConnections); + text += QString("\t.4byte %1\n").arg(connectionsListLabel); + saveTextFile(path, text); + } else { + deleteFile(path); + } + + updateMapsWithConnections(map); +} + +void Project::updateMapsWithConnections(Map *map) { + if (map->connections.length() > 0) { + if (!mapsWithConnections.contains(map->name)) { + mapsWithConnections.append(map->name); + } + } else { + if (mapsWithConnections.contains(map->name)) { + mapsWithConnections.removeOne(map->name); } } - text += QString("\n"); - text += QString("%1::\n").arg(map->connections_label); - text += QString("\t.4byte %1\n").arg(numValidConnections); - text += QString("\t.4byte %1\n").arg(connectionsListLabel); - saveTextFile(connections_path, text); } void Project::readMapAttributesTable() { @@ -728,6 +753,7 @@ void Project::saveAllDataStructures() { saveAllMapAttributes(); saveMapGroupsTable(); saveMapConstantsHeader(); + saveMapsWithConnections(); } void Project::loadTilesetAssets(Tileset* tileset) { @@ -939,6 +965,13 @@ void Project::appendTextFile(QString path, QString text) { } } +void Project::deleteFile(QString path) { + QFile file(path); + if (file.exists() && !file.remove()) { + qDebug() << QString("Could not delete file '%1': ").arg(path) + file.errorString(); + } +} + void Project::readMapGroups() { QString text = readTextFile(root + "/data/maps/_groups.inc"); if (text.isNull()) { @@ -1124,6 +1157,41 @@ void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QString } } +void Project::readMapsWithConnections() { + QString path = root + "/data/maps/connections.inc"; + QString text = readTextFile(path); + if (text.isNull()) { + return; + } + + mapsWithConnections.clear(); + QRegularExpression re("data\\/maps\\/(?\\w+)\\/connections.inc"); + QList* includes = parseAsm(text); + for (QStringList values : *includes) { + if (values.length() != 2) + continue; + + QRegularExpressionMatch match = re.match(values.value(1)); + if (match.hasMatch()) { + QString mapName = match.captured("mapName"); + mapsWithConnections.append(mapName); + } + } +} + +void Project::saveMapsWithConnections() { + QString path = root + "/data/maps/connections.inc"; + QString text = ""; + for (QString mapName : mapsWithConnections) { + if (mapNamesToMapConstants->contains(mapName)) { + text += QString("\t.include \"data/maps/%1/connections.inc\"\n").arg(mapName); + } else { + qDebug() << QString("Failed to write connection include. %1 not a valid map name").arg(mapName); + } + } + saveTextFile(path, text); +} + QStringList Project::getSongNames() { QStringList names; QString text = readTextFile(root + "/include/constants/songs.h"); diff --git a/project.h b/project.h index 0c064444..58502c2f 100755 --- a/project.h +++ b/project.h @@ -26,6 +26,7 @@ public: QStringList *itemNames = NULL; QStringList *flagNames = NULL; QStringList *varNames = NULL; + QStringList mapsWithConnections; QMap *map_cache; Map* loadMap(QString); @@ -41,6 +42,7 @@ public: QString readTextFile(QString path); void saveTextFile(QString path, QString text); void appendTextFile(QString path, QString text); + void deleteFile(QString path); void readMapGroups(); Map* addNewMapToGroup(QString mapName, int groupNum); @@ -53,6 +55,7 @@ public: void readMapAttributesTable(); void readAllMapAttributes(); void readMapAttributes(Map*); + void readMapsWithConnections(); void getTilesets(Map*); void loadTilesetAssets(Tileset*); @@ -98,6 +101,8 @@ private: QString getMapAssetsFilepath(); void saveMapHeader(Map*); void saveMapConnections(Map*); + void updateMapsWithConnections(Map*); + void saveMapsWithConnections(); void saveMapAttributesTable(); void updateMapAttributes(Map* map); void readCDefinesSorted(QString, QStringList, QStringList*);