Properly support saving map connections

This commit is contained in:
Marcus Huderle 2018-03-11 13:33:08 -07:00
parent 6f71c15629
commit 9b0f686781
4 changed files with 94 additions and 19 deletions

View file

@ -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) {

View file

@ -289,6 +289,7 @@ void MainWindow::loadDataStructures() {
project->readItemNames();
project->readFlagNames();
project->readVarNames();
project->readMapsWithConnections();
}
void MainWindow::populateMapList() {

View file

@ -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\\/(?<mapName>\\w+)\\/connections.inc");
QList<QStringList>* 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");

View file

@ -26,6 +26,7 @@ public:
QStringList *itemNames = NULL;
QStringList *flagNames = NULL;
QStringList *varNames = NULL;
QStringList mapsWithConnections;
QMap<QString, Map*> *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*);