Clean up tileset C parsing

This commit is contained in:
GriffinR 2022-10-07 14:29:51 -04:00 committed by Marcus Huderle
parent 9b4c55106b
commit 374da65b7f
5 changed files with 43 additions and 37 deletions

View file

@ -37,10 +37,11 @@ public:
static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*, bool useTruePalettes = false); static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*, bool useTruePalettes = false);
static QList<QRgb> getPalette(int, Tileset*, Tileset*, bool useTruePalettes = false); static QList<QRgb> getPalette(int, Tileset*, Tileset*, bool useTruePalettes = false);
static bool metatileIsValid(uint16_t metatileId, Tileset *, Tileset *); 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 appendToHeaders(QString root, QString friendlyName, bool usingAsm);
bool appendToGraphics(QString root, QString friendlyName, bool primary, bool usingAsm); bool appendToGraphics(QString root, QString friendlyName, bool usingAsm);
bool appendToMetatiles(QString root, QString friendlyName, bool primary, bool usingAsm); bool appendToMetatiles(QString root, QString friendlyName, bool usingAsm);
}; };
#endif // TILESET_H #endif // TILESET_H

View file

@ -168,9 +168,7 @@ bool Tileset::appendToHeaders(QString root, QString friendlyName, bool usingAsm)
return true; return true;
} }
// TODO: Interpet isSecondary to remove primary argument here and below bool Tileset::appendToGraphics(QString root, QString friendlyName, bool usingAsm) {
// TODO: friendlyName.toLower() is not the usual format for tileset folders
bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary, bool usingAsm) {
QString graphicsFile = root + "/" + (usingAsm ? projectConfig.getFilePath(ProjectFilePath::tilesets_graphics_asm) QString graphicsFile = root + "/" + (usingAsm ? projectConfig.getFilePath(ProjectFilePath::tilesets_graphics_asm)
: projectConfig.getFilePath(ProjectFilePath::tilesets_graphics)); : projectConfig.getFilePath(ProjectFilePath::tilesets_graphics));
QFile file(graphicsFile); QFile file(graphicsFile);
@ -179,10 +177,9 @@ bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary,
return false; return false;
} }
const QString primaryString = primary ? "primary" : "secondary"; const QString tilesetDir = this->getExpectedDir();
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 tilesPath = tilesetDir + "tiles.4bpp.lz"; const QString palettesPath = tilesetDir + "/palettes/";
const QString palettesPath = tilesetDir + "palettes/";
QString dataString = "\n"; QString dataString = "\n";
if (usingAsm) { 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)); dataString.append(QString("const u16 gTilesetPalettes_%1[][16] =\n{\n").arg(friendlyName));
for (int i = 0; i < Project::getNumPalettesTotal(); i++) 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(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)); dataString.append(QString("\nconst u32 gTilesetTiles_%1[] = INCBIN_U32(\"%2\");\n").arg(friendlyName, tilesPath));
} }
file.write(dataString.toUtf8()); file.write(dataString.toUtf8());
@ -205,7 +203,7 @@ bool Tileset::appendToGraphics(QString root, QString friendlyName, bool primary,
return true; 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) QString metatileFile = root + "/" + (usingAsm ? projectConfig.getFilePath(ProjectFilePath::tilesets_metatiles_asm)
: projectConfig.getFilePath(ProjectFilePath::tilesets_metatiles)); : projectConfig.getFilePath(ProjectFilePath::tilesets_metatiles));
QFile file(metatileFile); QFile file(metatileFile);
@ -214,10 +212,9 @@ bool Tileset::appendToMetatiles(QString root, QString friendlyName, bool primary
return false; return false;
} }
const QString primaryString = primary ? "primary" : "secondary"; const QString tilesetDir = this->getExpectedDir();
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 metatilesPath = tilesetDir + "metatiles.bin"; const QString metatileAttrsPath = tilesetDir + "/metatile_attributes.bin";
const QString metatileAttrsPath = tilesetDir + "metatile_attributes.bin";
QString dataString = "\n"; QString dataString = "\n";
if (usingAsm) { if (usingAsm) {
@ -237,3 +234,18 @@ bool Tileset::appendToMetatiles(QString root, QString friendlyName, bool primary
file.close(); file.close();
return true; 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();
}

View file

@ -1266,7 +1266,7 @@ void MainWindow::on_actionNew_Tileset_triggered() {
msgBox.exec(); msgBox.exec();
return; return;
} }
QString fullDirectoryPath = editor->project->root + createTilesetDialog->path; QString fullDirectoryPath = editor->project->root + "/" + createTilesetDialog->path;
QDir directory; QDir directory;
if(directory.exists(fullDirectoryPath)) { if(directory.exists(fullDirectoryPath)) {
logError(QString("Could not create tileset \"%1\", the folder \"%2\" already exists.").arg(createTilesetDialog->friendlyName, 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 //append to tileset specific files
newSet.appendToHeaders(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); newSet.appendToHeaders(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets);
newSet.appendToGraphics(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, !createTilesetDialog->isSecondary, editor->project->usingAsmTilesets); newSet.appendToMetatiles(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets);
if(!createTilesetDialog->isSecondary) { if(!createTilesetDialog->isSecondary) {
this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName); this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName);

View file

@ -1503,6 +1503,7 @@ void Project::loadTilesetAssets(Tileset* tileset) {
void Project::readTilesetPaths(Tileset* tileset) { void Project::readTilesetPaths(Tileset* tileset) {
// Parse the tileset data files to try and get explicit file paths for this tileset's assets // 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) { if (this->usingAsmTilesets) {
// Read asm tileset data files. Backwards compatibility // Read asm tileset data files. Backwards compatibility
const QList<QStringList> graphics = parser.parseAsm(projectConfig.getFilePath(ProjectFilePath::tilesets_graphics_asm)); const QList<QStringList> 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); const QStringList metatile_attrs_values = parser.getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
if (!tiles_values.isEmpty()) 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()) 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()) 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) 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 { } else {
// Read C tileset data files // Read C tileset data files
const QString graphicsFile = projectConfig.getFilePath(ProjectFilePath::tilesets_graphics); 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); const QString metatileAttrsPath = parser.readCIncbin(metatilesFile, tileset->metatile_attrs_label);
if (!tilesImagePath.isEmpty()) if (!tilesImagePath.isEmpty())
tileset->tilesImagePath = this->fixGraphicPath(root + '/' + tilesImagePath); tileset->tilesImagePath = this->fixGraphicPath(rootDir + tilesImagePath);
if (!metatilesPath.isEmpty()) if (!metatilesPath.isEmpty())
tileset->metatiles_path = root + '/' + metatilesPath; tileset->metatiles_path = rootDir + metatilesPath;
if (!metatileAttrsPath.isEmpty()) if (!metatileAttrsPath.isEmpty())
tileset->metatile_attrs_path = root + '/' + metatileAttrsPath; tileset->metatile_attrs_path = rootDir + metatileAttrsPath;
for (const auto &path : palettePaths) 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. // Try to set default paths, if any weren't found by reading the files above
// This will be used for e.g. gTileset_General, which has paths specified in graphics.c instead QString defaultPath = rootDir + tileset->getExpectedDir();
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
if (tileset->tilesImagePath.isEmpty()) if (tileset->tilesImagePath.isEmpty())
tileset->tilesImagePath = defaultPath + "/tiles.png"; tileset->tilesImagePath = defaultPath + "/tiles.png";
if (tileset->metatiles_path.isEmpty()) if (tileset->metatiles_path.isEmpty())

View file

@ -35,7 +35,6 @@ void NewTilesetDialog::NameOrSecondaryChanged() {
this->friendlyName = this->ui->nameLineEdit->text(); this->friendlyName = this->ui->nameLineEdit->text();
this->fullSymbolName = "gTileset_" + this->friendlyName; this->fullSymbolName = "gTileset_" + this->friendlyName;
this->ui->symbolNameLineEdit->setText(this->fullSymbolName); this->ui->symbolNameLineEdit->setText(this->fullSymbolName);
QString basePath = "/" + projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders); this->path = Tileset::getExpectedDir(this->fullSymbolName, this->isSecondary);
this->path = basePath + (this->isSecondary ? "secondary/" : "primary/") + this->friendlyName.toLower();
this->ui->pathLineEdit->setText(this->path); this->ui->pathLineEdit->setText(this->path);
} }