From 43e15dfb0e12365e215c7a3a825fe090dd89bccb Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 16:07:19 -0800 Subject: [PATCH] Save collateral files when creating a new map --- project.cpp | 67 +++++++++++++++++++++++++++++++++++++---------------- project.h | 2 ++ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/project.cpp b/project.cpp index 69f69516..7e5ec69c 100755 --- a/project.cpp +++ b/project.cpp @@ -176,7 +176,7 @@ void Project::setNewMapHeader(Map* map, int mapIndex) { map->scripts_label = QString("%1_MapScripts").arg(map->name);; map->connections_label = "0x0"; map->song = "BGM_DAN02"; - map->index = mapIndex; + map->index = QString("%1").arg(mapIndex); map->location = "0"; map->visibility = "0"; map->weather = "2"; @@ -552,6 +552,11 @@ void Project::setNewMapBorder(Map *map) { map->border = blockdata; } +void Project::saveMapBorder(Map *map) { + QString path = getMapBorderPath(map); + writeBlockdata(path, map->border); +} + void Project::saveBlockdata(Map* map) { QString path = getBlockdataPath(map); writeBlockdata(path, map->blockdata); @@ -576,14 +581,45 @@ void Project::saveAllMaps() { } void Project::saveMap(Map *map) { - saveBlockdata(map); + // Create/Modify a few collateral files for brand new maps. + if (!map->isPersistedToFile) { + QString newMapDataDir = QString(root + "/data/maps/%1").arg(map->name); + if (!QDir::root().mkdir(newMapDataDir)) { + qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; + } + + // Create file data/scripts/maps/.inc + QString text = QString("%1_MapScripts::\n\t.byte 0\n").arg(map->name); + saveTextFile(root + "/data/scripts/maps/" + map->name + ".inc", text); + + // Create file data/text/maps/.inc + saveTextFile(root + "/data/text/maps/" + map->name + ".inc", "\n"); + + // Simply append to data/event_scripts.s. + // TODO: In the future, this file needs more structure to allow for proper parsing. + text = QString("\n\t.include \"data/scripts/maps/%1.inc\"\n").arg(map->name); + text += QString("\t.include \"data/text/maps/%1.inc\"\n").arg(map->name); + appendTextFile(root + "/data/event_scripts.s", text); + + // Simply append to data/map_events.s. + text = QString("\n\t.include \"data/maps/events/%1.inc\"\n").arg(map->name); + appendTextFile(root + "/data/map_events.s", text); + + // Simply append to data/maps/headers.inc. + text = QString("\t.include \"data/maps/%1/header.inc\"\n").arg(map->name); + appendTextFile(root + "/data/maps/headers.inc", text); + } + + saveMapBorder(map); saveMapHeader(map); + saveBlockdata(map); saveMapEvents(map); } void Project::saveAllDataStructures() { saveMapAttributesTable(); saveAllMapAttributes(); + // TODO: saveMapGroupsTable(); } void Project::loadTilesetAssets(Tileset* tileset) { @@ -786,6 +822,15 @@ void Project::saveTextFile(QString path, QString text) { } } +void Project::appendTextFile(QString path, QString text) { + QFile file(path); + if (file.open(QIODevice::Append)) { + file.write(text.toUtf8()); + } else { + qDebug() << QString("Could not open '%1' for appending: ").arg(path) + file.errorString(); + } +} + void Project::readMapGroups() { QString text = readTextFile(root + "/data/maps/_groups.inc"); if (text.isNull()) { @@ -852,24 +897,6 @@ void Project::readMapGroups() { } void Project::addNewMapToGroup(QString mapName, int groupNum) { - // Write new map to project files. - // 1. Create directory data/maps// - // 2. Create file data/maps//border.bin - // 3. Create file data/maps//header.inc - // 4. Create file data/maps//map.bin - // 5. Create file data/maps/events/.inc - // 6. Create file data/scripts/maps/.inc - // 7. Create file data/text/maps/.inc - // 8. Modify data/event_scripts.s: - // .include "data/scripts/maps/.inc" - // .include "data/text/maps/.inc" - // 9. Modify data/map_events.s: - // .include "data/maps/events/.inc" - // 10. Modify data/maps/_assets.inc - // 11. Modify data/maps/_groups.inc - // 12. Modify data/maps/attributes_table.inc - // 13. Modify data/maps/headers.inc - int mapIndex = mapAttributesTable->count() + 1; mapAttributesTable->insert(mapIndex, mapName); diff --git a/project.h b/project.h index 10996955..fa5cd9ac 100755 --- a/project.h +++ b/project.h @@ -36,6 +36,7 @@ public: QString readTextFile(QString path); void saveTextFile(QString path, QString text); + void appendTextFile(QString path, QString text); void readMapGroups(); void addNewMapToGroup(QString mapName, int groupNum); @@ -60,6 +61,7 @@ public: QString getBlockdataPath(Map*); void saveBlockdata(Map*); + void saveMapBorder(Map*); void writeBlockdata(QString, Blockdata*); void saveAllMaps(); void saveMap(Map*);