diff --git a/include/core/tileset.h b/include/core/tileset.h index 31213f82..b210f8d4 100644 --- a/include/core/tileset.h +++ b/include/core/tileset.h @@ -37,10 +37,11 @@ public: static QList> getBlockPalettes(Tileset*, Tileset*, bool useTruePalettes = false); static QList getPalette(int, Tileset*, Tileset*, bool useTruePalettes = false); static bool metatileIsValid(uint16_t metatileId, Tileset *, Tileset *); - + static QString getExpectedDir(QString tilesetName, bool isSecondary); + QString getExpectedDir(); bool appendToHeaders(QString root, QString friendlyName, bool usingAsm); - bool appendToGraphics(QString root, QString friendlyName, bool primary, bool usingAsm); - bool appendToMetatiles(QString root, QString friendlyName, bool primary, bool usingAsm); + bool appendToGraphics(QString root, QString friendlyName, bool usingAsm); + bool appendToMetatiles(QString root, QString friendlyName, bool usingAsm); }; #endif // TILESET_H diff --git a/src/core/tileset.cpp b/src/core/tileset.cpp index d8ad083a..d4a1dca8 100644 --- a/src/core/tileset.cpp +++ b/src/core/tileset.cpp @@ -168,9 +168,7 @@ bool Tileset::appendToHeaders(QString root, QString friendlyName, bool usingAsm) return true; } -// TODO: Interpet isSecondary to remove primary argument here and below -// TODO: friendlyName.toLower() is not the usual format for tileset folders -bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary, bool usingAsm) { +bool Tileset::appendToGraphics(QString root, QString friendlyName, bool usingAsm) { QString graphicsFile = root + "/" + (usingAsm ? projectConfig.getFilePath(ProjectFilePath::tilesets_graphics_asm) : projectConfig.getFilePath(ProjectFilePath::tilesets_graphics)); QFile file(graphicsFile); @@ -179,10 +177,9 @@ bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary, return false; } - const QString primaryString = primary ? "primary" : "secondary"; - const QString tilesetDir = QString("%1%2/%3/").arg(projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders), primaryString, friendlyName.toLower()); - const QString tilesPath = tilesetDir + "tiles.4bpp.lz"; - const QString palettesPath = tilesetDir + "palettes/"; + const QString tilesetDir = this->getExpectedDir(); + const QString tilesPath = tilesetDir + "/tiles.4bpp.lz"; + const QString palettesPath = tilesetDir + "/palettes/"; QString dataString = "\n"; if (usingAsm) { @@ -197,6 +194,7 @@ bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary, dataString.append(QString("const u16 gTilesetPalettes_%1[][16] =\n{\n").arg(friendlyName)); for (int i = 0; i < Project::getNumPalettesTotal(); i++) dataString.append(QString(" INCBIN_U16(\"%1%2.gbapal\"),\n").arg(palettesPath).arg(i, 2, 10, QLatin1Char('0'))); + dataString.append("};\n"); dataString.append(QString("\nconst u32 gTilesetTiles_%1[] = INCBIN_U32(\"%2\");\n").arg(friendlyName, tilesPath)); } file.write(dataString.toUtf8()); @@ -205,7 +203,7 @@ bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary, return true; } -bool Tileset::appendToMetatiles(QString root, QString friendlyName, bool primary, bool usingAsm) { +bool Tileset::appendToMetatiles(QString root, QString friendlyName, bool usingAsm) { QString metatileFile = root + "/" + (usingAsm ? projectConfig.getFilePath(ProjectFilePath::tilesets_metatiles_asm) : projectConfig.getFilePath(ProjectFilePath::tilesets_metatiles)); QFile file(metatileFile); @@ -214,10 +212,9 @@ bool Tileset::appendToMetatiles(QString root, QString friendlyName, bool primary return false; } - const QString primaryString = primary ? "primary" : "secondary"; - const QString tilesetDir = QString("%1%2/%3/").arg(projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders), primaryString, friendlyName.toLower()); - const QString metatilesPath = tilesetDir + "metatiles.bin"; - const QString metatileAttrsPath = tilesetDir + "metatile_attributes.bin"; + const QString tilesetDir = this->getExpectedDir(); + const QString metatilesPath = tilesetDir + "/metatiles.bin"; + const QString metatileAttrsPath = tilesetDir + "/metatile_attributes.bin"; QString dataString = "\n"; if (usingAsm) { @@ -237,3 +234,18 @@ bool Tileset::appendToMetatiles(QString root, QString friendlyName, bool primary file.close(); return true; } + +// The path where Porymap expects a Tileset's graphics assets to be stored (but not necessarily where they actually are) +// Example: for gTileset_DepartmentStore, returns "data/tilesets/secondary/department_store/" +QString Tileset::getExpectedDir() +{ + return Tileset::getExpectedDir(this->name, ParseUtil::gameStringToBool(this->is_secondary)); +} + +QString Tileset::getExpectedDir(QString tilesetName, bool isSecondary) +{ + QRegularExpression re("([a-z])([A-Z0-9])"); + const QString category = isSecondary ? "secondary" : "primary"; + const QString basePath = projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders) + category + "/"; + return basePath + tilesetName.replace("gTileset_", "").replace(re, "\\1_\\2").toLower(); +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 478bae9a..f5ab0d03 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1266,7 +1266,7 @@ void MainWindow::on_actionNew_Tileset_triggered() { msgBox.exec(); return; } - QString fullDirectoryPath = editor->project->root + createTilesetDialog->path; + QString fullDirectoryPath = editor->project->root + "/" + createTilesetDialog->path; QDir directory; if(directory.exists(fullDirectoryPath)) { logError(QString("Could not create tileset \"%1\", the folder \"%2\" already exists.").arg(createTilesetDialog->friendlyName, fullDirectoryPath)); @@ -1335,8 +1335,8 @@ void MainWindow::on_actionNew_Tileset_triggered() { //append to tileset specific files newSet.appendToHeaders(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); - newSet.appendToGraphics(editor->project->root, createTilesetDialog->friendlyName, !createTilesetDialog->isSecondary, editor->project->usingAsmTilesets); - newSet.appendToMetatiles(editor->project->root, createTilesetDialog->friendlyName, !createTilesetDialog->isSecondary, editor->project->usingAsmTilesets); + newSet.appendToGraphics(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); + newSet.appendToMetatiles(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); if(!createTilesetDialog->isSecondary) { this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName); diff --git a/src/project.cpp b/src/project.cpp index d9ecddfe..59056a24 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1503,6 +1503,7 @@ void Project::loadTilesetAssets(Tileset* tileset) { void Project::readTilesetPaths(Tileset* tileset) { // Parse the tileset data files to try and get explicit file paths for this tileset's assets + const QString rootDir = this->root + "/"; if (this->usingAsmTilesets) { // Read asm tileset data files. Backwards compatibility const QList graphics = parser.parseAsm(projectConfig.getFilePath(ProjectFilePath::tilesets_graphics_asm)); @@ -1514,13 +1515,13 @@ void Project::readTilesetPaths(Tileset* tileset) { const QStringList metatile_attrs_values = parser.getLabelValues(metatiles_macros, tileset->metatile_attrs_label); if (!tiles_values.isEmpty()) - tileset->tilesImagePath = this->fixGraphicPath(root + '/' + tiles_values.value(0).section('"', 1, 1)); + tileset->tilesImagePath = this->fixGraphicPath(rootDir + tiles_values.value(0).section('"', 1, 1)); if (!metatiles_values.isEmpty()) - tileset->metatiles_path = root + '/' + metatiles_values.value(0).section('"', 1, 1); + tileset->metatiles_path = rootDir + metatiles_values.value(0).section('"', 1, 1); if (!metatile_attrs_values.isEmpty()) - tileset->metatile_attrs_path = root + '/' + metatile_attrs_values.value(0).section('"', 1, 1); + tileset->metatile_attrs_path = rootDir + metatile_attrs_values.value(0).section('"', 1, 1); for (const auto &value : palettes_values) - tileset->palettePaths.append(this->fixPalettePath(root + '/' + value.section('"', 1, 1))); + tileset->palettePaths.append(this->fixPalettePath(rootDir + value.section('"', 1, 1))); } else { // Read C tileset data files const QString graphicsFile = projectConfig.getFilePath(ProjectFilePath::tilesets_graphics); @@ -1532,24 +1533,17 @@ void Project::readTilesetPaths(Tileset* tileset) { const QString metatileAttrsPath = parser.readCIncbin(metatilesFile, tileset->metatile_attrs_label); if (!tilesImagePath.isEmpty()) - tileset->tilesImagePath = this->fixGraphicPath(root + '/' + tilesImagePath); + tileset->tilesImagePath = this->fixGraphicPath(rootDir + tilesImagePath); if (!metatilesPath.isEmpty()) - tileset->metatiles_path = root + '/' + metatilesPath; + tileset->metatiles_path = rootDir + metatilesPath; if (!metatileAttrsPath.isEmpty()) - tileset->metatile_attrs_path = root + '/' + metatileAttrsPath; + tileset->metatile_attrs_path = rootDir + metatileAttrsPath; for (const auto &path : palettePaths) - tileset->palettePaths.append(this->fixPalettePath(root + '/' + path)); + tileset->palettePaths.append(this->fixPalettePath(rootDir + path)); } - // Construct the expected path of the tileset's graphics, in case Porymap couldn't find any paths. - // This will be used for e.g. gTileset_General, which has paths specified in graphics.c instead - QRegularExpression re("([a-z])([A-Z0-9])"); - QString tilesetName = tileset->name; - QString category = ParseUtil::gameStringToBool(tileset->is_secondary) ? "secondary" : "primary"; - QString basePath = root + "/" + projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders) + category + "/"; - QString defaultPath = basePath + tilesetName.replace("gTileset_", "").replace(re, "\\1_\\2").toLower(); - - // Try to set defaults + // Try to set default paths, if any weren't found by reading the files above + QString defaultPath = rootDir + tileset->getExpectedDir(); if (tileset->tilesImagePath.isEmpty()) tileset->tilesImagePath = defaultPath + "/tiles.png"; if (tileset->metatiles_path.isEmpty()) diff --git a/src/ui/newtilesetdialog.cpp b/src/ui/newtilesetdialog.cpp index 23fbbc94..a6a6492d 100644 --- a/src/ui/newtilesetdialog.cpp +++ b/src/ui/newtilesetdialog.cpp @@ -35,7 +35,6 @@ void NewTilesetDialog::NameOrSecondaryChanged() { this->friendlyName = this->ui->nameLineEdit->text(); this->fullSymbolName = "gTileset_" + this->friendlyName; this->ui->symbolNameLineEdit->setText(this->fullSymbolName); - QString basePath = "/" + projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders); - this->path = basePath + (this->isSecondary ? "secondary/" : "primary/") + this->friendlyName.toLower(); + this->path = Tileset::getExpectedDir(this->fullSymbolName, this->isSecondary); this->ui->pathLineEdit->setText(this->path); }