瀏覽代碼

Parse all map attributes from _assets into memory

Marcus Huderle 6 年之前
父節點
當前提交
50fc906971
共有 4 個文件被更改,包括 95 次插入1 次删除
  1. 1
    1
      asm.cpp
  2. 1
    0
      mainwindow.cpp
  3. 90
    0
      project.cpp
  4. 3
    0
      project.h

+ 1
- 1
asm.cpp 查看文件

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

+ 1
- 0
mainwindow.cpp 查看文件

@@ -283,6 +283,7 @@ void MainWindow::on_checkBox_ShowLocation_clicked(bool checked)
283 283
 void MainWindow::loadDataStructures() {
284 284
     Project *project = editor->project;
285 285
     project->readMapAttributesTable();
286
+    project->readAllMapAttributes();
286 287
 }
287 288
 
288 289
 void MainWindow::populateMapList() {

+ 90
- 0
project.cpp 查看文件

@@ -23,6 +23,7 @@ Project::Project()
23 23
     mapConstantsToMapNames = new QMap<QString, QString>;
24 24
     mapNamesToMapConstants = new QMap<QString, QString>;
25 25
     mapAttributesTable = new QMap<int, QString>;
26
+    mapAttributes = new QMap<QString, QMap<QString, QString>*>;
26 27
     tileset_cache = new QMap<QString, Tileset*>;
27 28
 }
28 29
 
@@ -273,6 +274,95 @@ void Project::readMapAttributes(Map* map) {
273 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 366
 void Project::setNewMapAttributes(Map* map) {
277 367
     map->width = "20";
278 368
     map->height = "20";

+ 3
- 0
project.h 查看文件

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