Properly use project root as base path for relative prefab filepaths

This commit is contained in:
Marcus Huderle 2022-09-11 10:19:13 -05:00
parent ce4d8bfc54
commit 9e4a143b3a
2 changed files with 23 additions and 10 deletions

View file

@ -742,8 +742,7 @@ void ProjectConfig::setPrefabFilepath(QString filepath) {
QString ProjectConfig::getPrefabFilepath() {
if (this->prefabFilepath.isEmpty()) {
// Default to the <projectroot>/prefabs.json, if no path exists.
this->setPrefabFilepath(QDir(this->projectDir).filePath("prefabs.json"));
this->setPrefabFilepath("prefabs.json");
}
return this->prefabFilepath;
}

View file

@ -26,12 +26,13 @@ void Prefab::loadPrefabs() {
ParseUtil parser;
QJsonDocument prefabDoc;
QFileInfo info(filepath);
if (info.isRelative()) {
filepath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
}
if (!QFile::exists(filepath) || !parser.tryParseJsonFile(&prefabDoc, filepath)) {
QString relativePath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
if (!parser.tryParseJsonFile(&prefabDoc, relativePath)) {
logError(QString("Failed to prefab data from %1").arg(filepath));
return;
}
logError(QString("Failed to prefab data from %1").arg(filepath));
return;
}
QJsonArray prefabs = prefabDoc.array();
@ -86,9 +87,20 @@ void Prefab::loadPrefabs() {
void Prefab::savePrefabs() {
QString filepath = projectConfig.getPrefabFilepath();
if (filepath.isEmpty()) return;
QFileInfo info(filepath);
if (info.isRelative()) {
filepath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
}
QFile prefabsFile(filepath);
if (!prefabsFile.open(QIODevice::WriteOnly)) {
logError(QString("Error: Could not open %1 for writing").arg(filepath));
QMessageBox messageBox;
messageBox.setText("Failed to save prefabs file!");
messageBox.setInformativeText(QString("Could not open \"%1\" for writing").arg(filepath));
messageBox.setDetailedText("Any created prefabs will not be available next time Porymap is opened. Please fix your prefabs_filepath in the Porymap project config file.");
messageBox.setIcon(QMessageBox::Warning);
messageBox.exec();
return;
}
@ -106,13 +118,15 @@ void Prefab::savePrefabs() {
int index = y * item.selection.dimensions.x() + x;
auto metatileItem = item.selection.metatileItems.at(index);
if (metatileItem.enabled) {
auto collisionItem = item.selection.collisionItems.at(index);
OrderedJson::object metatileObj;
metatileObj["x"] = x;
metatileObj["y"] = y;
metatileObj["metatile_id"] = metatileItem.metatileId;
metatileObj["collision"] = collisionItem.collision;
metatileObj["elevation"] = collisionItem.elevation;
if (item.selection.hasCollision && index < item.selection.collisionItems.size()) {
auto collisionItem = item.selection.collisionItems.at(index);
metatileObj["collision"] = collisionItem.collision;
metatileObj["elevation"] = collisionItem.elevation;
}
metatiles.push_back(metatileObj);
}
}