浏览代码

Save collateral files when creating a new map

Marcus Huderle 6 年前
父节点
当前提交
43e15dfb0e
共有 2 个文件被更改,包括 49 次插入20 次删除
  1. 47
    20
      project.cpp
  2. 2
    0
      project.h

+ 47
- 20
project.cpp 查看文件

@@ -176,7 +176,7 @@ void Project::setNewMapHeader(Map* map, int mapIndex) {
176 176
     map->scripts_label = QString("%1_MapScripts").arg(map->name);;
177 177
     map->connections_label = "0x0";
178 178
     map->song = "BGM_DAN02";
179
-    map->index = mapIndex;
179
+    map->index = QString("%1").arg(mapIndex);
180 180
     map->location = "0";
181 181
     map->visibility = "0";
182 182
     map->weather = "2";
@@ -552,6 +552,11 @@ void Project::setNewMapBorder(Map *map) {
552 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 560
 void Project::saveBlockdata(Map* map) {
556 561
     QString path = getBlockdataPath(map);
557 562
     writeBlockdata(path, map->blockdata);
@@ -576,14 +581,45 @@ void Project::saveAllMaps() {
576 581
 }
577 582
 
578 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 614
     saveMapHeader(map);
615
+    saveBlockdata(map);
581 616
     saveMapEvents(map);
582 617
 }
583 618
 
584 619
 void Project::saveAllDataStructures() {
585 620
     saveMapAttributesTable();
586 621
     saveAllMapAttributes();
622
+    // TODO: saveMapGroupsTable();
587 623
 }
588 624
 
589 625
 void Project::loadTilesetAssets(Tileset* tileset) {
@@ -786,6 +822,15 @@ void Project::saveTextFile(QString path, QString text) {
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 834
 void Project::readMapGroups() {
790 835
     QString text = readTextFile(root + "/data/maps/_groups.inc");
791 836
     if (text.isNull()) {
@@ -852,24 +897,6 @@ void Project::readMapGroups() {
852 897
 }
853 898
 
854 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 900
     int mapIndex = mapAttributesTable->count() + 1;
874 901
     mapAttributesTable->insert(mapIndex, mapName);
875 902
 

+ 2
- 0
project.h 查看文件

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