Properly use project root as base path for relative prefab filepaths
This commit is contained in:
parent
ce4d8bfc54
commit
9e4a143b3a
2 changed files with 23 additions and 10 deletions
|
@ -742,8 +742,7 @@ void ProjectConfig::setPrefabFilepath(QString filepath) {
|
||||||
|
|
||||||
QString ProjectConfig::getPrefabFilepath() {
|
QString ProjectConfig::getPrefabFilepath() {
|
||||||
if (this->prefabFilepath.isEmpty()) {
|
if (this->prefabFilepath.isEmpty()) {
|
||||||
// Default to the <projectroot>/prefabs.json, if no path exists.
|
this->setPrefabFilepath("prefabs.json");
|
||||||
this->setPrefabFilepath(QDir(this->projectDir).filePath("prefabs.json"));
|
|
||||||
}
|
}
|
||||||
return this->prefabFilepath;
|
return this->prefabFilepath;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,13 @@ void Prefab::loadPrefabs() {
|
||||||
|
|
||||||
ParseUtil parser;
|
ParseUtil parser;
|
||||||
QJsonDocument prefabDoc;
|
QJsonDocument prefabDoc;
|
||||||
|
QFileInfo info(filepath);
|
||||||
|
if (info.isRelative()) {
|
||||||
|
filepath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
|
||||||
|
}
|
||||||
if (!QFile::exists(filepath) || !parser.tryParseJsonFile(&prefabDoc, filepath)) {
|
if (!QFile::exists(filepath) || !parser.tryParseJsonFile(&prefabDoc, filepath)) {
|
||||||
QString relativePath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
|
logError(QString("Failed to prefab data from %1").arg(filepath));
|
||||||
if (!parser.tryParseJsonFile(&prefabDoc, relativePath)) {
|
return;
|
||||||
logError(QString("Failed to prefab data from %1").arg(filepath));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonArray prefabs = prefabDoc.array();
|
QJsonArray prefabs = prefabDoc.array();
|
||||||
|
@ -86,9 +87,20 @@ void Prefab::loadPrefabs() {
|
||||||
void Prefab::savePrefabs() {
|
void Prefab::savePrefabs() {
|
||||||
QString filepath = projectConfig.getPrefabFilepath();
|
QString filepath = projectConfig.getPrefabFilepath();
|
||||||
if (filepath.isEmpty()) return;
|
if (filepath.isEmpty()) return;
|
||||||
|
|
||||||
|
QFileInfo info(filepath);
|
||||||
|
if (info.isRelative()) {
|
||||||
|
filepath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
|
||||||
|
}
|
||||||
QFile prefabsFile(filepath);
|
QFile prefabsFile(filepath);
|
||||||
if (!prefabsFile.open(QIODevice::WriteOnly)) {
|
if (!prefabsFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(filepath));
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,13 +118,15 @@ void Prefab::savePrefabs() {
|
||||||
int index = y * item.selection.dimensions.x() + x;
|
int index = y * item.selection.dimensions.x() + x;
|
||||||
auto metatileItem = item.selection.metatileItems.at(index);
|
auto metatileItem = item.selection.metatileItems.at(index);
|
||||||
if (metatileItem.enabled) {
|
if (metatileItem.enabled) {
|
||||||
auto collisionItem = item.selection.collisionItems.at(index);
|
|
||||||
OrderedJson::object metatileObj;
|
OrderedJson::object metatileObj;
|
||||||
metatileObj["x"] = x;
|
metatileObj["x"] = x;
|
||||||
metatileObj["y"] = y;
|
metatileObj["y"] = y;
|
||||||
metatileObj["metatile_id"] = metatileItem.metatileId;
|
metatileObj["metatile_id"] = metatileItem.metatileId;
|
||||||
metatileObj["collision"] = collisionItem.collision;
|
if (item.selection.hasCollision && index < item.selection.collisionItems.size()) {
|
||||||
metatileObj["elevation"] = collisionItem.elevation;
|
auto collisionItem = item.selection.collisionItems.at(index);
|
||||||
|
metatileObj["collision"] = collisionItem.collision;
|
||||||
|
metatileObj["elevation"] = collisionItem.elevation;
|
||||||
|
}
|
||||||
metatiles.push_back(metatileObj);
|
metatiles.push_back(metatileObj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue