Skip to content

Commit

Permalink
Auto-detect JSON file format when importing custom types
Browse files Browse the repository at this point in the history
Now it is no longer up to the user to select the right JSON file type
when importing object types. Selecting the wrong type for a particular
JSON file would silently import broken types.

Closes #3472
  • Loading branch information
bjorn committed Sep 16, 2022
1 parent 80220a9 commit f1fbb82
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Unreleased

* Allow adding maps to image collection tilesets (#3447)
* Auto-detect JSON file format when importing custom types (#3472)
* Added file system actions to the tile context menu (#3448)
* Fixed possible crash in Custom Types Editor (#3465)
* Fixed display of overridden values from a nested class
Expand Down
48 changes: 32 additions & 16 deletions src/tiled/propertytypeseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,27 +657,27 @@ void PropertyTypesEditor::importPropertyTypes()
const QString lastPath = session.lastPath(Session::PropertyTypesFile);

PropertyTypesFilter filter(lastPath);
// When importing, we don't use the "objectTypesJsonFilter". Instead, we
// will auto-detect the format of the JSON file.
const QString filters = QStringList { filter.propertyTypesFilter, filter.objectTypesXmlFilter }.join(QStringLiteral(";;"));
const QString fileName =
QFileDialog::getOpenFileName(this, tr("Import Types"),
lastPath,
filter.filters,
filters,
&filter.selectedFilter);
if (fileName.isEmpty())
return;

session.setLastPath(Session::PropertyTypesFile, fileName);

if (filter.selectedFilter == filter.objectTypesJsonFilter ||
filter.selectedFilter == filter.objectTypesXmlFilter) {
ObjectTypesSerializer serializer;
ObjectTypes objectTypes;
const ExportContext context(*mPropertyTypesModel->propertyTypes(),
QFileInfo(fileName).path());
ObjectTypes objectTypes;
const ExportContext context(*mPropertyTypesModel->propertyTypes(),
QFileInfo(fileName).path());

if (serializer.readObjectTypes(fileName, objectTypes, context)) {
mPropertyTypesModel->importObjectTypes(objectTypes);
applyPropertyTypes();
} else {
if (filter.selectedFilter == filter.objectTypesXmlFilter) {
ObjectTypesSerializer serializer(ObjectTypesSerializer::Xml);

if (!serializer.readObjectTypes(fileName, objectTypes, context)) {
QMessageBox::critical(this, tr("Error Reading Object Types"),
serializer.errorString());
return;
Expand All @@ -698,14 +698,30 @@ void PropertyTypesEditor::importPropertyTypes()
return;
}

PropertyTypes typesToImport;
typesToImport.loadFromJson(document.array(), QFileInfo(fileName).path());
// We can detect the old format by the absence of an "id" property:
//
// Object Types: "[ { color, name, properties } ]"
// Custom Types: "[ { id, name, type, ... } ]"
//
const QJsonArray array = document.array();
const bool oldFormat = array.first().toObject().value(QLatin1String("id")).isUndefined();
if (oldFormat) {
fromJson(array, objectTypes, context);
} else {
PropertyTypes typesToImport;
typesToImport.loadFromJson(array, QFileInfo(fileName).path());

if (typesToImport.count() > 0) {
mPropertyTypesModel->importPropertyTypes(std::move(typesToImport));
applyPropertyTypes();
if (typesToImport.count() > 0) {
mPropertyTypesModel->importPropertyTypes(std::move(typesToImport));
applyPropertyTypes();
}
}
}

if (!objectTypes.isEmpty()) {
mPropertyTypesModel->importObjectTypes(objectTypes);
applyPropertyTypes();
}
}

void PropertyTypesEditor::exportPropertyTypes()
Expand Down

0 comments on commit f1fbb82

Please sign in to comment.