Parcourir la source

Save all map attributes to _assets file

Marcus Huderle il y a 6 ans
Parent
révision
a9d71d15f8
2 fichiers modifiés avec 94 ajouts et 10 suppressions
  1. 92
    10
      project.cpp
  2. 2
    0
      project.h

+ 92
- 10
project.cpp Voir le fichier

@@ -261,7 +261,7 @@ void Project::readMapAttributes(Map* map) {
261 261
 
262 262
     Asm *parser = new Asm;
263 263
 
264
-    QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
264
+    QString assets_text = readTextFile(getMapAssetsFilepath());
265 265
     if (assets_text.isNull()) {
266 266
         return;
267 267
     }
@@ -278,7 +278,7 @@ void Project::readAllMapAttributes() {
278 278
     mapAttributes->clear();
279 279
 
280 280
     Asm *parser = new Asm;
281
-    QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
281
+    QString assets_text = readTextFile(getMapAssetsFilepath());
282 282
     if (assets_text.isNull()) {
283 283
         return;
284 284
     }
@@ -306,10 +306,12 @@ void Project::readAllMapAttributes() {
306 306
             // Unknown map name has to match the MapAttributes label.
307 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 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 316
         // Read MapBlockData assets.
315 317
         QStringList blockDataParams = commands->value(i++);
@@ -319,21 +321,24 @@ void Project::readAllMapAttributes() {
319 321
             continue;
320 322
         }
321 323
         QString blockDataLabel = blockDataParams.value(1);
322
-        mapAttributes->value(mapName)->insert("blockdata_label", blockDataLabel);
324
+        attrs->insert("blockdata_label", blockDataLabel);
323 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 328
         // Read MapAttributes assets.
327 329
         i++; // skip .align
328 330
         // Maps can share MapAttributes, so  gather a list of them.
329 331
         QStringList attributeMapLabels;
330 332
         QStringList attributesParams;
333
+        QStringList* sharedAttrMaps = new QStringList;
331 334
         while (i < commands->length()) {
332 335
             attributesParams = commands->value(i);
333 336
             if (attributesParams.value(0) != ".label") {
334 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 342
             i++;
338 343
         }
339 344
 
@@ -359,10 +364,86 @@ void Project::readAllMapAttributes() {
359 364
             mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel);
360 365
             mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary);
361 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 447
 void Project::setNewMapAttributes(Map* map) {
367 448
     map->width = "20";
368 449
     map->height = "20";
@@ -400,7 +481,7 @@ Tileset* Project::loadTileset(QString label) {
400 481
 }
401 482
 
402 483
 QString Project::getBlockdataPath(Map* map) {
403
-    QString text = readTextFile(root + "/data/maps/_assets.inc");
484
+    QString text = readTextFile(getMapAssetsFilepath());
404 485
     QStringList *values = getLabelValues(parse(text), map->blockdata_label);
405 486
     QString path;
406 487
     if (!values->isEmpty()) {
@@ -412,7 +493,7 @@ QString Project::getBlockdataPath(Map* map) {
412 493
 }
413 494
 
414 495
 QString Project::getMapBorderPath(Map *map) {
415
-    QString text = readTextFile(root + "/data/maps/_assets.inc");
496
+    QString text = readTextFile(getMapAssetsFilepath());
416 497
     QStringList *values = getLabelValues(parse(text), map->border_label);
417 498
     QString path;
418 499
     if (!values->isEmpty()) {
@@ -489,6 +570,7 @@ void Project::saveMap(Map *map) {
489 570
 
490 571
 void Project::saveAllDataStructures() {
491 572
     saveMapAttributesTable();
573
+    saveAllMapAttributes();
492 574
 }
493 575
 
494 576
 void Project::loadTilesetAssets(Tileset* tileset) {

+ 2
- 0
project.h Voir le fichier

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