Browse Source

Save collateral files when creating a new map

Marcus Huderle 6 years ago
parent
commit
43e15dfb0e
2 changed files with 49 additions and 20 deletions
  1. 47
    20
      project.cpp
  2. 2
    0
      project.h

+ 47
- 20
project.cpp View File

176
     map->scripts_label = QString("%1_MapScripts").arg(map->name);;
176
     map->scripts_label = QString("%1_MapScripts").arg(map->name);;
177
     map->connections_label = "0x0";
177
     map->connections_label = "0x0";
178
     map->song = "BGM_DAN02";
178
     map->song = "BGM_DAN02";
179
-    map->index = mapIndex;
179
+    map->index = QString("%1").arg(mapIndex);
180
     map->location = "0";
180
     map->location = "0";
181
     map->visibility = "0";
181
     map->visibility = "0";
182
     map->weather = "2";
182
     map->weather = "2";
552
     map->border = blockdata;
552
     map->border = blockdata;
553
 }
553
 }
554
 
554
 
555
+void Project::saveMapBorder(Map *map) {
556
+    QString path = getMapBorderPath(map);
557
+    writeBlockdata(path, map->border);
558
+}
559
+
555
 void Project::saveBlockdata(Map* map) {
560
 void Project::saveBlockdata(Map* map) {
556
     QString path = getBlockdataPath(map);
561
     QString path = getBlockdataPath(map);
557
     writeBlockdata(path, map->blockdata);
562
     writeBlockdata(path, map->blockdata);
576
 }
581
 }
577
 
582
 
578
 void Project::saveMap(Map *map) {
583
 void Project::saveMap(Map *map) {
579
-    saveBlockdata(map);
584
+    // Create/Modify a few collateral files for brand new maps.
585
+    if (!map->isPersistedToFile) {
586
+        QString newMapDataDir = QString(root + "/data/maps/%1").arg(map->name);
587
+        if (!QDir::root().mkdir(newMapDataDir)) {
588
+            qDebug() << "Error: failed to create directory for new map. " << newMapDataDir;
589
+        }
590
+
591
+        // Create file data/scripts/maps/<map_name>.inc
592
+        QString text = QString("%1_MapScripts::\n\t.byte 0\n").arg(map->name);
593
+        saveTextFile(root + "/data/scripts/maps/" + map->name + ".inc", text);
594
+
595
+        // Create file data/text/maps/<map_name>.inc
596
+        saveTextFile(root + "/data/text/maps/" + map->name + ".inc", "\n");
597
+
598
+        // Simply append to data/event_scripts.s.
599
+        // TODO: In the future, this file needs more structure to allow for proper parsing.
600
+        text = QString("\n\t.include \"data/scripts/maps/%1.inc\"\n").arg(map->name);
601
+        text += QString("\t.include \"data/text/maps/%1.inc\"\n").arg(map->name);
602
+        appendTextFile(root + "/data/event_scripts.s", text);
603
+
604
+        // Simply append to data/map_events.s.
605
+        text = QString("\n\t.include \"data/maps/events/%1.inc\"\n").arg(map->name);
606
+        appendTextFile(root + "/data/map_events.s", text);
607
+
608
+        // Simply append to data/maps/headers.inc.
609
+        text = QString("\t.include \"data/maps/%1/header.inc\"\n").arg(map->name);
610
+        appendTextFile(root + "/data/maps/headers.inc", text);
611
+    }
612
+
613
+    saveMapBorder(map);
580
     saveMapHeader(map);
614
     saveMapHeader(map);
615
+    saveBlockdata(map);
581
     saveMapEvents(map);
616
     saveMapEvents(map);
582
 }
617
 }
583
 
618
 
584
 void Project::saveAllDataStructures() {
619
 void Project::saveAllDataStructures() {
585
     saveMapAttributesTable();
620
     saveMapAttributesTable();
586
     saveAllMapAttributes();
621
     saveAllMapAttributes();
622
+    // TODO: saveMapGroupsTable();
587
 }
623
 }
588
 
624
 
589
 void Project::loadTilesetAssets(Tileset* tileset) {
625
 void Project::loadTilesetAssets(Tileset* tileset) {
786
     }
822
     }
787
 }
823
 }
788
 
824
 
825
+void Project::appendTextFile(QString path, QString text) {
826
+    QFile file(path);
827
+    if (file.open(QIODevice::Append)) {
828
+        file.write(text.toUtf8());
829
+    } else {
830
+        qDebug() << QString("Could not open '%1' for appending: ").arg(path) + file.errorString();
831
+    }
832
+}
833
+
789
 void Project::readMapGroups() {
834
 void Project::readMapGroups() {
790
     QString text = readTextFile(root + "/data/maps/_groups.inc");
835
     QString text = readTextFile(root + "/data/maps/_groups.inc");
791
     if (text.isNull()) {
836
     if (text.isNull()) {
852
 }
897
 }
853
 
898
 
854
 void Project::addNewMapToGroup(QString mapName, int groupNum) {
899
 void Project::addNewMapToGroup(QString mapName, int groupNum) {
855
-    // Write new map to project files.
856
-    // 1. Create directory data/maps/<map_name>/
857
-    // 2. Create file data/maps/<map_name>/border.bin
858
-    // 3. Create file data/maps/<map_name>/header.inc
859
-    // 4. Create file data/maps/<map_name>/map.bin
860
-    // 5. Create file data/maps/events/<map_name>.inc
861
-    // 6. Create file data/scripts/maps/<map_name>.inc
862
-    // 7. Create file data/text/maps/<map_name>.inc
863
-    // 8. Modify data/event_scripts.s:
864
-    //     .include "data/scripts/maps/<map_name>.inc"
865
-    //     .include "data/text/maps/<map_name>.inc"
866
-    // 9. Modify data/map_events.s:
867
-    //     .include "data/maps/events/<map_name>.inc"
868
-    // 10. Modify data/maps/_assets.inc
869
-    // 11. Modify data/maps/_groups.inc
870
-    // 12. Modify data/maps/attributes_table.inc
871
-    // 13. Modify data/maps/headers.inc
872
-
873
     int mapIndex = mapAttributesTable->count() + 1;
900
     int mapIndex = mapAttributesTable->count() + 1;
874
     mapAttributesTable->insert(mapIndex, mapName);
901
     mapAttributesTable->insert(mapIndex, mapName);
875
 
902
 

+ 2
- 0
project.h View File

36
 
36
 
37
     QString readTextFile(QString path);
37
     QString readTextFile(QString path);
38
     void saveTextFile(QString path, QString text);
38
     void saveTextFile(QString path, QString text);
39
+    void appendTextFile(QString path, QString text);
39
 
40
 
40
     void readMapGroups();
41
     void readMapGroups();
41
     void addNewMapToGroup(QString mapName, int groupNum);
42
     void addNewMapToGroup(QString mapName, int groupNum);
60
 
61
 
61
     QString getBlockdataPath(Map*);
62
     QString getBlockdataPath(Map*);
62
     void saveBlockdata(Map*);
63
     void saveBlockdata(Map*);
64
+    void saveMapBorder(Map*);
63
     void writeBlockdata(QString, Blockdata*);
65
     void writeBlockdata(QString, Blockdata*);
64
     void saveAllMaps();
66
     void saveAllMaps();
65
     void saveMap(Map*);
67
     void saveMap(Map*);