Browse Source

Parse all map attributes from _assets into memory

Marcus Huderle 6 years ago
parent
commit
50fc906971
4 changed files with 95 additions and 1 deletions
  1. 1
    1
      asm.cpp
  2. 1
    0
      mainwindow.cpp
  3. 90
    0
      project.cpp
  4. 3
    0
      project.h

+ 1
- 1
asm.cpp View File

26
         //QString macro;
26
         //QString macro;
27
         //QStringList *params;
27
         //QStringList *params;
28
         strip_comment(&line);
28
         strip_comment(&line);
29
-        if (line.isEmpty()) {
29
+        if (line.trimmed().isEmpty()) {
30
         } else if (line.contains(':')) {
30
         } else if (line.contains(':')) {
31
             label = line.left(line.indexOf(':'));
31
             label = line.left(line.indexOf(':'));
32
             QStringList *list = new QStringList;
32
             QStringList *list = new QStringList;

+ 1
- 0
mainwindow.cpp View File

283
 void MainWindow::loadDataStructures() {
283
 void MainWindow::loadDataStructures() {
284
     Project *project = editor->project;
284
     Project *project = editor->project;
285
     project->readMapAttributesTable();
285
     project->readMapAttributesTable();
286
+    project->readAllMapAttributes();
286
 }
287
 }
287
 
288
 
288
 void MainWindow::populateMapList() {
289
 void MainWindow::populateMapList() {

+ 90
- 0
project.cpp View File

23
     mapConstantsToMapNames = new QMap<QString, QString>;
23
     mapConstantsToMapNames = new QMap<QString, QString>;
24
     mapNamesToMapConstants = new QMap<QString, QString>;
24
     mapNamesToMapConstants = new QMap<QString, QString>;
25
     mapAttributesTable = new QMap<int, QString>;
25
     mapAttributesTable = new QMap<int, QString>;
26
+    mapAttributes = new QMap<QString, QMap<QString, QString>*>;
26
     tileset_cache = new QMap<QString, Tileset*>;
27
     tileset_cache = new QMap<QString, Tileset*>;
27
 }
28
 }
28
 
29
 
273
     map->tileset_secondary_label = attributes->value(5);
274
     map->tileset_secondary_label = attributes->value(5);
274
 }
275
 }
275
 
276
 
277
+void Project::readAllMapAttributes() {
278
+    mapAttributes->clear();
279
+
280
+    Asm *parser = new Asm;
281
+    QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
282
+    if (assets_text.isNull()) {
283
+        return;
284
+    }
285
+
286
+    QList<QStringList> *commands = parser->parse(assets_text);
287
+
288
+    // Assume the _assets.inc file is grouped consistently in the order of:
289
+    // 1. <map_name>_MapBorder
290
+    // 2. <map_name>_MapBlockdata
291
+    // 3. <map_name>_MapAttributes
292
+    int i = 0;
293
+    while (i < commands->length()) {
294
+        // Read MapBorder assets.
295
+        QStringList borderParams = commands->value(i++);
296
+        bool isUnknownMapBorder = borderParams.value(1).startsWith("UnknownMapBorder_");
297
+        if (borderParams.value(0) != ".label" || (!borderParams.value(1).endsWith("_MapBorder") && !isUnknownMapBorder)) {
298
+            qDebug() << QString("Expected MapBorder label, but found %1").arg(borderParams.value(1));
299
+            continue;
300
+        }
301
+        QString borderLabel = borderParams.value(1);
302
+        QString mapName;
303
+        if (!isUnknownMapBorder) {
304
+            mapName = borderLabel.remove(borderLabel.length() - 10, 10);
305
+        } else {
306
+            // Unknown map name has to match the MapAttributes label.
307
+            mapName = borderLabel.replace("Border", "Attributes");
308
+        }
309
+        mapAttributes->insert(mapName, new QMap<QString, QString>);
310
+        mapAttributes->value(mapName)->insert("border_label", borderParams.value(1));
311
+        borderParams = commands->value(i++);
312
+        mapAttributes->value(mapName)->insert("border_filepath", borderParams.value(1).replace("\"", ""));
313
+
314
+        // Read MapBlockData assets.
315
+        QStringList blockDataParams = commands->value(i++);
316
+        bool isUnknownMapBlockdata = blockDataParams.value(1).startsWith("UnknownMapBlockdata_");
317
+        if (blockDataParams.value(0) != ".label" || (!blockDataParams.value(1).endsWith("_MapBlockdata") && !isUnknownMapBlockdata)) {
318
+            qDebug() << QString("Expected MapBlockdata label, but found %1").arg(blockDataParams.value(1));
319
+            continue;
320
+        }
321
+        QString blockDataLabel = blockDataParams.value(1);
322
+        mapAttributes->value(mapName)->insert("blockdata_label", blockDataLabel);
323
+        blockDataParams = commands->value(i++);
324
+        mapAttributes->value(mapName)->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", ""));
325
+
326
+        // Read MapAttributes assets.
327
+        i++; // skip .align
328
+        // Maps can share MapAttributes, so  gather a list of them.
329
+        QStringList attributeMapLabels;
330
+        QStringList attributesParams;
331
+        while (i < commands->length()) {
332
+            attributesParams = commands->value(i);
333
+            if (attributesParams.value(0) != ".label") {
334
+                break;
335
+            }
336
+            attributeMapLabels.append(attributesParams.value(1));
337
+            i++;
338
+        }
339
+
340
+        // Apply the map attributes to each of the shared maps.
341
+        QString attrWidth = commands->value(i++).value(1);
342
+        QString attrHeight = commands->value(i++).value(1);
343
+        QString attrBorderLabel = commands->value(i++).value(1);
344
+        QString attrBlockdataLabel = commands->value(i++).value(1);
345
+        QString attrTilesetPrimary = commands->value(i++).value(1);
346
+        QString attrTilesetSecondary = commands->value(i++).value(1);
347
+        for (QString attributeMapLabel: attributeMapLabels) {
348
+            QString altMapName = attributeMapLabel;
349
+            if (!altMapName.startsWith("UnknownMapAttributes_")) {
350
+                altMapName.remove(altMapName.length() - 14, 14);
351
+            }
352
+            if (!mapAttributes->contains(altMapName)) {
353
+                mapAttributes->insert(altMapName, new QMap<QString, QString>);
354
+            }
355
+            mapAttributes->value(altMapName)->insert("attributes_label", attributeMapLabel);
356
+            mapAttributes->value(altMapName)->insert("width", attrWidth);
357
+            mapAttributes->value(altMapName)->insert("height", attrHeight);
358
+            mapAttributes->value(altMapName)->insert("border_label", attrBorderLabel);
359
+            mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel);
360
+            mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary);
361
+            mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary);
362
+        }
363
+    }
364
+}
365
+
276
 void Project::setNewMapAttributes(Map* map) {
366
 void Project::setNewMapAttributes(Map* map) {
277
     map->width = "20";
367
     map->width = "20";
278
     map->height = "20";
368
     map->height = "20";

+ 3
- 0
project.h View File

20
     QMap<QString, QString> *mapConstantsToMapNames;
20
     QMap<QString, QString> *mapConstantsToMapNames;
21
     QMap<QString, QString> *mapNamesToMapConstants;
21
     QMap<QString, QString> *mapNamesToMapConstants;
22
     QMap<int, QString> *mapAttributesTable;
22
     QMap<int, QString> *mapAttributesTable;
23
+    QMap<QString, QMap<QString, QString>*> *mapAttributes;
24
+
23
 
25
 
24
     QMap<QString, Map*> *map_cache;
26
     QMap<QString, Map*> *map_cache;
25
     Map* loadMap(QString);
27
     Map* loadMap(QString);
44
     QStringList* getLabelValues(QList<QStringList>*, QString);
46
     QStringList* getLabelValues(QList<QStringList>*, QString);
45
     void readMapHeader(Map*);
47
     void readMapHeader(Map*);
46
     void readMapAttributesTable();
48
     void readMapAttributesTable();
49
+    void readAllMapAttributes();
47
     void readMapAttributes(Map*);
50
     void readMapAttributes(Map*);
48
     void getTilesets(Map*);
51
     void getTilesets(Map*);
49
     void loadTilesetAssets(Tileset*);
52
     void loadTilesetAssets(Tileset*);