diff --git a/asm.cpp b/asm.cpp index 60855b76..f291e3ba 100755 --- a/asm.cpp +++ b/asm.cpp @@ -26,7 +26,7 @@ QList* Asm::parse(QString text) { //QString macro; //QStringList *params; strip_comment(&line); - if (line.isEmpty()) { + if (line.trimmed().isEmpty()) { } else if (line.contains(':')) { label = line.left(line.indexOf(':')); QStringList *list = new QStringList; diff --git a/mainwindow.cpp b/mainwindow.cpp index 07079ba1..3d3b78ab 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -283,6 +283,7 @@ void MainWindow::on_checkBox_ShowLocation_clicked(bool checked) void MainWindow::loadDataStructures() { Project *project = editor->project; project->readMapAttributesTable(); + project->readAllMapAttributes(); } void MainWindow::populateMapList() { diff --git a/project.cpp b/project.cpp index 7eb6c6a8..07f7c757 100755 --- a/project.cpp +++ b/project.cpp @@ -23,6 +23,7 @@ Project::Project() mapConstantsToMapNames = new QMap; mapNamesToMapConstants = new QMap; mapAttributesTable = new QMap; + mapAttributes = new QMap*>; tileset_cache = new QMap; } @@ -273,6 +274,95 @@ void Project::readMapAttributes(Map* map) { map->tileset_secondary_label = attributes->value(5); } +void Project::readAllMapAttributes() { + mapAttributes->clear(); + + Asm *parser = new Asm; + QString assets_text = readTextFile(root + "/data/maps/_assets.inc"); + if (assets_text.isNull()) { + return; + } + + QList *commands = parser->parse(assets_text); + + // Assume the _assets.inc file is grouped consistently in the order of: + // 1. _MapBorder + // 2. _MapBlockdata + // 3. _MapAttributes + int i = 0; + while (i < commands->length()) { + // Read MapBorder assets. + QStringList borderParams = commands->value(i++); + bool isUnknownMapBorder = borderParams.value(1).startsWith("UnknownMapBorder_"); + if (borderParams.value(0) != ".label" || (!borderParams.value(1).endsWith("_MapBorder") && !isUnknownMapBorder)) { + qDebug() << QString("Expected MapBorder label, but found %1").arg(borderParams.value(1)); + continue; + } + QString borderLabel = borderParams.value(1); + QString mapName; + if (!isUnknownMapBorder) { + mapName = borderLabel.remove(borderLabel.length() - 10, 10); + } else { + // Unknown map name has to match the MapAttributes label. + mapName = borderLabel.replace("Border", "Attributes"); + } + mapAttributes->insert(mapName, new QMap); + mapAttributes->value(mapName)->insert("border_label", borderParams.value(1)); + borderParams = commands->value(i++); + mapAttributes->value(mapName)->insert("border_filepath", borderParams.value(1).replace("\"", "")); + + // Read MapBlockData assets. + QStringList blockDataParams = commands->value(i++); + bool isUnknownMapBlockdata = blockDataParams.value(1).startsWith("UnknownMapBlockdata_"); + if (blockDataParams.value(0) != ".label" || (!blockDataParams.value(1).endsWith("_MapBlockdata") && !isUnknownMapBlockdata)) { + qDebug() << QString("Expected MapBlockdata label, but found %1").arg(blockDataParams.value(1)); + continue; + } + QString blockDataLabel = blockDataParams.value(1); + mapAttributes->value(mapName)->insert("blockdata_label", blockDataLabel); + blockDataParams = commands->value(i++); + mapAttributes->value(mapName)->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", "")); + + // Read MapAttributes assets. + i++; // skip .align + // Maps can share MapAttributes, so gather a list of them. + QStringList attributeMapLabels; + QStringList attributesParams; + while (i < commands->length()) { + attributesParams = commands->value(i); + if (attributesParams.value(0) != ".label") { + break; + } + attributeMapLabels.append(attributesParams.value(1)); + i++; + } + + // Apply the map attributes to each of the shared maps. + QString attrWidth = commands->value(i++).value(1); + QString attrHeight = commands->value(i++).value(1); + QString attrBorderLabel = commands->value(i++).value(1); + QString attrBlockdataLabel = commands->value(i++).value(1); + QString attrTilesetPrimary = commands->value(i++).value(1); + QString attrTilesetSecondary = commands->value(i++).value(1); + for (QString attributeMapLabel: attributeMapLabels) { + QString altMapName = attributeMapLabel; + if (!altMapName.startsWith("UnknownMapAttributes_")) { + altMapName.remove(altMapName.length() - 14, 14); + } + if (!mapAttributes->contains(altMapName)) { + mapAttributes->insert(altMapName, new QMap); + } + mapAttributes->value(altMapName)->insert("attributes_label", attributeMapLabel); + mapAttributes->value(altMapName)->insert("width", attrWidth); + mapAttributes->value(altMapName)->insert("height", attrHeight); + mapAttributes->value(altMapName)->insert("border_label", attrBorderLabel); + mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel); + mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary); + mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary); + } + } +} + void Project::setNewMapAttributes(Map* map) { map->width = "20"; map->height = "20"; diff --git a/project.h b/project.h index dc14baea..0617c3cd 100755 --- a/project.h +++ b/project.h @@ -20,6 +20,8 @@ public: QMap *mapConstantsToMapNames; QMap *mapNamesToMapConstants; QMap *mapAttributesTable; + QMap*> *mapAttributes; + QMap *map_cache; Map* loadMap(QString); @@ -44,6 +46,7 @@ public: QStringList* getLabelValues(QList*, QString); void readMapHeader(Map*); void readMapAttributesTable(); + void readAllMapAttributes(); void readMapAttributes(Map*); void getTilesets(Map*); void loadTilesetAssets(Tileset*);