diff --git a/editor.cpp b/editor.cpp index b00c7948..4ccb9df9 100755 --- a/editor.cpp +++ b/editor.cpp @@ -9,6 +9,7 @@ Editor::Editor() void Editor::saveProject() { if (project) { + project->saveAllDataStructures(); project->saveAllMaps(); } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 7b367347..42320bee 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -61,10 +61,12 @@ void MainWindow::openProject(QString dir) { editor->project = new Project; editor->project->root = dir; setWindowTitle(editor->project->getProjectTitle() + " - pretmap"); + loadDataStructures(); populateMapList(); setMap(getDefaultMap()); } else { setWindowTitle(editor->project->getProjectTitle() + " - pretmap"); + loadDataStructures(); populateMapList(); } } @@ -278,6 +280,10 @@ void MainWindow::on_checkBox_ShowLocation_clicked(bool checked) } } +void MainWindow::loadDataStructures() { + Project *project = editor->project; + project->readMapAttributesTable(); +} void MainWindow::populateMapList() { Project *project = editor->project; diff --git a/mainwindow.h b/mainwindow.h index a0dc1c1b..f9a911e1 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -76,6 +76,7 @@ private: Editor *editor = NULL; QIcon* mapIcon; void setMap(QString); + void loadDataStructures(); void populateMapList(); QString getExistingDirectory(QString); void openProject(QString dir); diff --git a/project.cpp b/project.cpp index c74f370f..aa94b9af 100755 --- a/project.cpp +++ b/project.cpp @@ -22,6 +22,7 @@ Project::Project() map_cache = new QMap; mapConstantsToMapNames = new QMap; mapNamesToMapConstants = new QMap; + mapAttributesTable = new QMap; tileset_cache = new QMap; } @@ -171,6 +172,53 @@ void Project::saveMapHeader(Map *map) { saveTextFile(header_path, text); } +void Project::readMapAttributesTable() { + int curMapIndex = 1; + QString attributesText = readTextFile(getMapAttributesTableFilepath()); + QList* values = parse(attributesText); + bool inAttributePointers = false; + for (int i = 0; i < values->length(); i++) { + QStringList params = values->value(i); + QString macro = params.value(0); + if (macro == ".label") { + if (inAttributePointers) { + break; + } + if (params.value(1) == "gMapAttributes") { + inAttributePointers = true; + } + } else if (macro == ".4byte" && inAttributePointers) { + QString mapName = params.value(1); + if (!mapName.contains("UnknownMapAttributes")) { + // Strip off "_MapAttributes" from the label if it's a real map label. + mapName = mapName.remove(mapName.length() - 14, 14); + } + mapAttributesTable->insert(curMapIndex, mapName); + curMapIndex++; + } + } +} + +void Project::saveMapAttributesTable() { + QString text = ""; + text += QString("\t.align 2\n"); + text += QString("gMapAttributes::\n"); + for (int i = 0; i < mapAttributesTable->count(); i++) { + int mapIndex = i + 1; + QString mapName = mapAttributesTable->value(mapIndex); + if (!mapName.contains("UnknownMapAttributes")) { + text += QString("\t.4byte %1_MapAttributes\n").arg(mapName); + } else { + text += QString("\t.4byte %1\n").arg(mapName); + } + } + saveTextFile(getMapAttributesTableFilepath(), text); +} + +QString Project::getMapAttributesTableFilepath() { + return QString("%1/data/maps/attributes_table.inc").arg(root); +} + void Project::readMapAttributes(Map* map) { Asm *parser = new Asm; @@ -277,6 +325,10 @@ void Project::saveMap(Map *map) { saveMapEvents(map); } +void Project::saveAllDataStructures() { + saveMapAttributesTable(); +} + void Project::loadTilesetAssets(Tileset* tileset) { Asm* parser = new Asm; QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary"; @@ -543,8 +595,6 @@ void Project::readMapGroups() { } void Project::addNewMapToGroup(QString mapName, int groupNum) { - int mapIndex = 0;// TODO: need to calculate the new map index. - // Write new map to project files. // 1. Create directory data/maps// // 2. Create file data/maps//border.bin @@ -563,10 +613,14 @@ void Project::addNewMapToGroup(QString mapName, int groupNum) { // 12. Modify data/maps/attributes_table.inc // 13. Modify data/maps/headers.inc - // 1. Create directory data/maps// + int mapIndex = mapAttributesTable->count() + 1; + mapAttributesTable->insert(mapIndex, mapName); + QString dataDir = QString("%1/data/").arg(root); QString dataMapsDir = QString("%1maps/").arg(dataDir); QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName); + + // 1. Create directory data/maps// if (!QDir::root().mkdir(newMapDataDir)) { qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; return; diff --git a/project.h b/project.h index 34987668..b37a69ef 100755 --- a/project.h +++ b/project.h @@ -19,6 +19,7 @@ public: QStringList *mapNames = NULL; QMap *mapConstantsToMapNames; QMap *mapNamesToMapConstants; + QMap *mapAttributesTable; QMap *map_cache; Map* loadMap(QString); @@ -42,6 +43,7 @@ public: QList* getLabelMacros(QList*, QString); QStringList* getLabelValues(QList*, QString); void readMapHeader(Map*); + void readMapAttributesTable(); void readMapAttributes(Map*); void getTilesets(Map*); void loadTilesetAssets(Tileset*); @@ -51,7 +53,7 @@ public: void writeBlockdata(QString, Blockdata*); void saveAllMaps(); void saveMap(Map*); - void saveMapHeader(Map*); + void saveAllDataStructures(); QList* parse(QString text); QStringList getSongNames(); @@ -77,6 +79,10 @@ public: QStringList readCArray(QString text, QString label); QString readCIncbin(QString text, QString label); QMap readCDefines(QString text, QStringList prefixes); +private: + QString getMapAttributesTableFilepath(); + void saveMapHeader(Map*); + void saveMapAttributesTable(); }; #endif // PROJECT_H