Browse Source

Save all map attributes to _assets file

Marcus Huderle 6 years ago
parent
commit
a9d71d15f8
2 changed files with 94 additions and 10 deletions
  1. 92
    10
      project.cpp
  2. 2
    0
      project.h

+ 92
- 10
project.cpp View File

261
 
261
 
262
     Asm *parser = new Asm;
262
     Asm *parser = new Asm;
263
 
263
 
264
-    QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
264
+    QString assets_text = readTextFile(getMapAssetsFilepath());
265
     if (assets_text.isNull()) {
265
     if (assets_text.isNull()) {
266
         return;
266
         return;
267
     }
267
     }
278
     mapAttributes->clear();
278
     mapAttributes->clear();
279
 
279
 
280
     Asm *parser = new Asm;
280
     Asm *parser = new Asm;
281
-    QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
281
+    QString assets_text = readTextFile(getMapAssetsFilepath());
282
     if (assets_text.isNull()) {
282
     if (assets_text.isNull()) {
283
         return;
283
         return;
284
     }
284
     }
306
             // Unknown map name has to match the MapAttributes label.
306
             // Unknown map name has to match the MapAttributes label.
307
             mapName = borderLabel.replace("Border", "Attributes");
307
             mapName = borderLabel.replace("Border", "Attributes");
308
         }
308
         }
309
-        mapAttributes->insert(mapName, new QMap<QString, QString>);
310
-        mapAttributes->value(mapName)->insert("border_label", borderParams.value(1));
309
+
310
+        QMap<QString, QString>* attrs = new QMap<QString, QString>;
311
+        mapAttributes->insert(mapName, attrs);
312
+        attrs->insert("border_label", borderParams.value(1));
311
         borderParams = commands->value(i++);
313
         borderParams = commands->value(i++);
312
-        mapAttributes->value(mapName)->insert("border_filepath", borderParams.value(1).replace("\"", ""));
314
+        attrs->insert("border_filepath", borderParams.value(1).replace("\"", ""));
313
 
315
 
314
         // Read MapBlockData assets.
316
         // Read MapBlockData assets.
315
         QStringList blockDataParams = commands->value(i++);
317
         QStringList blockDataParams = commands->value(i++);
319
             continue;
321
             continue;
320
         }
322
         }
321
         QString blockDataLabel = blockDataParams.value(1);
323
         QString blockDataLabel = blockDataParams.value(1);
322
-        mapAttributes->value(mapName)->insert("blockdata_label", blockDataLabel);
324
+        attrs->insert("blockdata_label", blockDataLabel);
323
         blockDataParams = commands->value(i++);
325
         blockDataParams = commands->value(i++);
324
-        mapAttributes->value(mapName)->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", ""));
326
+        attrs->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", ""));
325
 
327
 
326
         // Read MapAttributes assets.
328
         // Read MapAttributes assets.
327
         i++; // skip .align
329
         i++; // skip .align
328
         // Maps can share MapAttributes, so  gather a list of them.
330
         // Maps can share MapAttributes, so  gather a list of them.
329
         QStringList attributeMapLabels;
331
         QStringList attributeMapLabels;
330
         QStringList attributesParams;
332
         QStringList attributesParams;
333
+        QStringList* sharedAttrMaps = new QStringList;
331
         while (i < commands->length()) {
334
         while (i < commands->length()) {
332
             attributesParams = commands->value(i);
335
             attributesParams = commands->value(i);
333
             if (attributesParams.value(0) != ".label") {
336
             if (attributesParams.value(0) != ".label") {
334
                 break;
337
                 break;
335
             }
338
             }
336
-            attributeMapLabels.append(attributesParams.value(1));
339
+            QString attrLabel = attributesParams.value(1);
340
+            attributeMapLabels.append(attrLabel);
341
+            sharedAttrMaps->append(attrLabel);
337
             i++;
342
             i++;
338
         }
343
         }
339
 
344
 
359
             mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel);
364
             mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel);
360
             mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary);
365
             mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary);
361
             mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary);
366
             mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary);
367
+
368
+            if (sharedAttrMaps->length() > 1) {
369
+                mapAttributes->value(altMapName)->insert("shared_attr_maps", sharedAttrMaps->join(":"));
370
+            }
362
         }
371
         }
363
     }
372
     }
364
 }
373
 }
365
 
374
 
375
+void Project::saveAllMapAttributes() {
376
+    QString text = "";
377
+    for (int i = 0; i < mapAttributesTable->count(); i++) {
378
+        int mapIndex = i + 1;
379
+        QString mapName = mapAttributesTable->value(mapIndex);
380
+        QMap<QString, QString>* attrs = mapAttributes->value(mapName);
381
+
382
+        // Find the map attributes object that contains the border data.
383
+        QMap<QString, QString>* attrsWithBorder;
384
+        if (attrs->contains("border_filepath")) {
385
+            attrsWithBorder = attrs;
386
+        } else {
387
+            QStringList labels = attrs->value("shared_attr_maps").split(":");
388
+            for (QString label : labels) {
389
+                label.remove(label.length() - 14, 14);
390
+                if (mapAttributes->contains(label) && mapAttributes->value(label)->contains("border_filepath")) {
391
+                    attrsWithBorder = mapAttributes->value(label);
392
+                    break;
393
+                }
394
+            }
395
+        }
396
+        if (attrsWithBorder != nullptr) {
397
+            text += QString("%1::\n").arg(attrsWithBorder->value("border_label"));
398
+            text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder->value("border_filepath"));
399
+            text += QString("\n");
400
+        }
401
+
402
+        // Find the map attributes object that contains the blockdata.
403
+        QMap<QString, QString>* attrsWithBlockdata;
404
+        if (attrs->contains("blockdata_filepath")) {
405
+            attrsWithBlockdata = attrs;
406
+        } else {
407
+            QStringList labels = attrs->value("shared_attr_maps").split(":");
408
+            for (QString label : labels) {
409
+                label.remove(label.length() - 14, 14);
410
+                if (mapAttributes->contains(label) && mapAttributes->value(label)->contains("blockdata_filepath")) {
411
+                    attrsWithBlockdata = mapAttributes->value(label);
412
+                    break;
413
+                }
414
+            }
415
+        }
416
+        if (attrsWithBlockdata != nullptr) {
417
+            text += QString("%1::\n").arg(attrsWithBlockdata->value("blockdata_label"));
418
+            text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder->value("blockdata_filepath"));
419
+            text += QString("\n");
420
+        }
421
+
422
+        text += QString("\t.align 2\n");
423
+        if (attrs->contains("shared_attr_maps")) {
424
+            QStringList labels = attrs->value("shared_attr_maps").split(":");
425
+            for (QString label : labels) {
426
+                text += QString("%1::\n").arg(label);
427
+            }
428
+        } else {
429
+            text += QString("%1::\n").arg(attrs->value("attributes_label"));
430
+        }
431
+        text += QString("\t.4byte %1\n").arg(attrs->value("width"));
432
+        text += QString("\t.4byte %1\n").arg(attrs->value("height"));
433
+        text += QString("\t.4byte %1\n").arg(attrs->value("border_label"));
434
+        text += QString("\t.4byte %1\n").arg(attrs->value("blockdata_label"));
435
+        text += QString("\t.4byte %1\n").arg(attrs->value("tileset_primary"));
436
+        text += QString("\t.4byte %1\n").arg(attrs->value("tileset_secondary"));
437
+        text += QString("\n");
438
+    }
439
+
440
+    saveTextFile(getMapAssetsFilepath(), text);
441
+}
442
+
443
+QString Project::getMapAssetsFilepath() {
444
+    return root + "/data/maps/_assets.inc";
445
+}
446
+
366
 void Project::setNewMapAttributes(Map* map) {
447
 void Project::setNewMapAttributes(Map* map) {
367
     map->width = "20";
448
     map->width = "20";
368
     map->height = "20";
449
     map->height = "20";
400
 }
481
 }
401
 
482
 
402
 QString Project::getBlockdataPath(Map* map) {
483
 QString Project::getBlockdataPath(Map* map) {
403
-    QString text = readTextFile(root + "/data/maps/_assets.inc");
484
+    QString text = readTextFile(getMapAssetsFilepath());
404
     QStringList *values = getLabelValues(parse(text), map->blockdata_label);
485
     QStringList *values = getLabelValues(parse(text), map->blockdata_label);
405
     QString path;
486
     QString path;
406
     if (!values->isEmpty()) {
487
     if (!values->isEmpty()) {
412
 }
493
 }
413
 
494
 
414
 QString Project::getMapBorderPath(Map *map) {
495
 QString Project::getMapBorderPath(Map *map) {
415
-    QString text = readTextFile(root + "/data/maps/_assets.inc");
496
+    QString text = readTextFile(getMapAssetsFilepath());
416
     QStringList *values = getLabelValues(parse(text), map->border_label);
497
     QStringList *values = getLabelValues(parse(text), map->border_label);
417
     QString path;
498
     QString path;
418
     if (!values->isEmpty()) {
499
     if (!values->isEmpty()) {
489
 
570
 
490
 void Project::saveAllDataStructures() {
571
 void Project::saveAllDataStructures() {
491
     saveMapAttributesTable();
572
     saveMapAttributesTable();
573
+    saveAllMapAttributes();
492
 }
574
 }
493
 
575
 
494
 void Project::loadTilesetAssets(Tileset* tileset) {
576
 void Project::loadTilesetAssets(Tileset* tileset) {

+ 2
- 0
project.h View File

64
     void saveAllMaps();
64
     void saveAllMaps();
65
     void saveMap(Map*);
65
     void saveMap(Map*);
66
     void saveAllDataStructures();
66
     void saveAllDataStructures();
67
+    void saveAllMapAttributes();
67
 
68
 
68
     QList<QStringList>* parse(QString text);
69
     QList<QStringList>* parse(QString text);
69
     QStringList getSongNames();
70
     QStringList getSongNames();
91
     QMap<QString, int> readCDefines(QString text, QStringList prefixes);
92
     QMap<QString, int> readCDefines(QString text, QStringList prefixes);
92
 private:
93
 private:
93
     QString getMapAttributesTableFilepath();
94
     QString getMapAttributesTableFilepath();
95
+    QString getMapAssetsFilepath();
94
     void saveMapHeader(Map*);
96
     void saveMapHeader(Map*);
95
     void saveMapAttributesTable();
97
     void saveMapAttributesTable();
96
 };
98
 };