Skip to content

Commit

Permalink
Scripting: Fixed reset of file formats on script reload
Browse files Browse the repository at this point in the history
When the scripts got reloaded, or when a native plugins was disabled &
enabled within the same session, the map, tileset or object template
could lose track of which format they were saved in.

Upon saving a map that lost track of its format, it would silently
default to saving in the TMX format. Now, if the format can't be found
anymore, the user gets a "Save As" dialog instead.

Closes mapeditor#2911
  • Loading branch information
bjorn committed Oct 20, 2020
1 parent 9556775 commit 59f7930
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 71 deletions.
3 changes: 3 additions & 0 deletions src/libtiled/fileformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class FormatHelper
template<typename Format>
Format *findFileFormat(const QString &shortName, FileFormat::Capabilities capabilities = FileFormat::Write)
{
if (shortName.isEmpty())
return nullptr;

return PluginManager::find<Format>([&](Format *format) {
return format->hasCapabilities(capabilities) && format->shortName() == shortName;
});
Expand Down
15 changes: 3 additions & 12 deletions src/libtiled/objecttemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,15 @@ void ObjectTemplate::setObject(std::unique_ptr<MapObject> object)
mTileset.reset();
}

void ObjectTemplate::setFormat(ObjectTemplateFormat *format)
{
mFormat = format;
}

ObjectTemplateFormat *ObjectTemplate::format() const
{
return mFormat;
}

bool ObjectTemplate::save()
{
if (!mFormat)
auto format = findFileFormat<ObjectTemplateFormat>(mFormat);
if (!format)
return false;
if (mFileName.isEmpty())
return false;

const bool result = mFormat->write(this, mFileName);
const bool result = format->write(this, mFileName);

mLastSaved = QFileInfo(mFileName).lastModified();

Expand Down
15 changes: 9 additions & 6 deletions src/libtiled/objecttemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@
#include "tileset.h"

#include <QDateTime>
#include <QPointer>

#include <memory>

namespace Tiled {

class ObjectTemplateFormat;

class TILEDSHARED_EXPORT ObjectTemplate : public Object
{
public:
Expand All @@ -55,8 +52,8 @@ class TILEDSHARED_EXPORT ObjectTemplate : public Object
const QString &fileName() const;
void setFileName(const QString &fileName);

void setFormat(ObjectTemplateFormat *format);
ObjectTemplateFormat *format() const;
void setFormat(const QString &format);
QString format() const;

bool save();
QDateTime lastSaved() const;
Expand All @@ -65,7 +62,7 @@ class TILEDSHARED_EXPORT ObjectTemplate : public Object

private:
QString mFileName;
QPointer<ObjectTemplateFormat> mFormat;
QString mFormat;
std::unique_ptr<MapObject> mObject;
SharedTileset mTileset;
QDateTime mLastSaved;
Expand All @@ -80,6 +77,12 @@ inline const QString &ObjectTemplate::fileName() const
inline void ObjectTemplate::setFileName(const QString &fileName)
{ mFileName = fileName; }

inline void ObjectTemplate::setFormat(const QString &format)
{ mFormat = format; }

inline QString ObjectTemplate::format() const
{ return mFormat; }

inline QDateTime ObjectTemplate::lastSaved() const
{ return mLastSaved; }

Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/objecttemplateformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ std::unique_ptr<ObjectTemplate> readObjectTemplate(const QString &fileName, QStr
}

if (objectTemplate)
objectTemplate->setFormat(format);
objectTemplate->setFormat(format->shortName());

return objectTemplate;
}
Expand Down
5 changes: 2 additions & 3 deletions src/libtiled/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "imagecache.h"
#include "terrain.h"
#include "tile.h"
#include "tilesetformat.h"
#include "tilesetmanager.h"
#include "wangset.h"

Expand Down Expand Up @@ -82,12 +81,12 @@ Tileset::~Tileset()
qDeleteAll(mWangSets);
}

void Tileset::setFormat(TilesetFormat *format)
void Tileset::setFormat(const QString &format)
{
mFormat = format;
}

TilesetFormat *Tileset::format() const
QString Tileset::format() const
{
return mFormat;
}
Expand Down
8 changes: 3 additions & 5 deletions src/libtiled/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <QList>
#include <QPixmap>
#include <QPoint>
#include <QPointer>
#include <QSharedPointer>
#include <QString>
#include <QVector>
Expand All @@ -49,7 +48,6 @@ namespace Tiled {

class Tile;
class Tileset;
class TilesetFormat;
class Terrain;
class WangSet;

Expand Down Expand Up @@ -113,8 +111,8 @@ class TILEDSHARED_EXPORT Tileset : public Object
void setFileName(const QString &fileName);
bool isExternal() const;

void setFormat(TilesetFormat *format);
TilesetFormat *format() const;
void setFormat(const QString &format);
QString format() const;

int tileWidth() const;
int tileHeight() const;
Expand Down Expand Up @@ -273,7 +271,7 @@ class TILEDSHARED_EXPORT Tileset : public Object
bool mTerrainDistancesDirty;
LoadingStatus mStatus;
QColor mBackgroundColor;
QPointer<TilesetFormat> mFormat;
QString mFormat;

QWeakPointer<Tileset> mWeakPointer;
QWeakPointer<Tileset> mOriginalTileset;
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/tilesetformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SharedTileset readTileset(const QString &fileName, QString *error)

if (tileset) {
tileset->setFileName(fileName);
tileset->setFormat(format);
tileset->setFormat(format->shortName());
}

return tileset;
Expand Down
1 change: 0 additions & 1 deletion src/tiled/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include <QDateTime>
#include <QObject>
#include <QPointer>
#include <QSharedPointer>
#include <QString>
#include <QVariant>
Expand Down
16 changes: 8 additions & 8 deletions src/tiled/documentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,13 +646,9 @@ DocumentPtr DocumentManager::loadDocument(const QString &fileName,

if (!fileFormat) {
// Try to find a plugin that implements support for this format
const auto formats = PluginManager::objects<FileFormat>();
for (FileFormat *format : formats) {
if (format->supportsFile(fileName)) {
fileFormat = format;
break;
}
}
fileFormat = PluginManager::find<FileFormat>([&](FileFormat *format) {
return format->hasCapabilities(FileFormat::Read) && format->supportsFile(fileName);
});
}

if (!fileFormat) {
Expand Down Expand Up @@ -928,9 +924,13 @@ bool DocumentManager::reloadDocumentAt(int index)
QString error;

if (auto mapDocument = oldDocument.objectCast<MapDocument>()) {
auto readerFormat = mapDocument->readerFormat();
if (!readerFormat)
return false;

// TODO: Consider fixing the reload to avoid recreating the MapDocument
auto newDocument = MapDocument::load(oldDocument->fileName(),
mapDocument->readerFormat(),
readerFormat,
&error);
if (!newDocument) {
emit reloadError(tr("%1:\n\n%2").arg(oldDocument->fileName(), error));
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ bool MainWindow::saveFile()

const QString currentFileName = document->fileName();

if (currentFileName.isEmpty())
if (currentFileName.isEmpty() || !document->writerFormat())
return mDocumentManager->saveDocumentAs(document);
else
return mDocumentManager->saveDocument(document, currentFileName);
Expand Down
27 changes: 14 additions & 13 deletions src/tiled/mapdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "movemapobject.h"
#include "movemapobjecttogroup.h"
#include "objectgroup.h"
#include "objecttemplate.h"
#include "offsetlayer.h"
#include "orthogonalrenderer.h"
#include "painttilelayer.h"
Expand All @@ -60,7 +61,6 @@
#include "tile.h"
#include "tilelayer.h"
#include "tilesetdocument.h"
#include "tmxmapformat.h"

#include <QFileInfo>
#include <QRect>
Expand Down Expand Up @@ -126,11 +126,12 @@ MapDocument::~MapDocument()

bool MapDocument::save(const QString &fileName, QString *error)
{
MapFormat *mapFormat = mWriterFormat;

TmxMapFormat tmxMapFormat;
if (!mapFormat)
mapFormat = &tmxMapFormat;
MapFormat *mapFormat = writerFormat();
if (!mapFormat) {
if (error)
*error = tr("Map format '%s' not found").arg(mWriterFormat);
return false;
}

if (!mapFormat->write(map(), fileName)) {
if (error)
Expand Down Expand Up @@ -183,22 +184,24 @@ MapDocumentPtr MapDocument::load(const QString &fileName,

MapFormat *MapDocument::readerFormat() const
{
return mReaderFormat;
return findFileFormat<MapFormat>(mReaderFormat, FileFormat::Read);
}

void MapDocument::setReaderFormat(MapFormat *format)
{
mReaderFormat = format;
Q_ASSERT(format->hasCapabilities(FileFormat::Read));
mReaderFormat = format->shortName();
}

FileFormat *MapDocument::writerFormat() const
MapFormat *MapDocument::writerFormat() const
{
return mWriterFormat;
return findFileFormat<MapFormat>(mWriterFormat, FileFormat::Write);
}

void MapDocument::setWriterFormat(MapFormat *format)
{
mWriterFormat = format;
Q_ASSERT(format->hasCapabilities(FileFormat::Write));
mWriterFormat = format->shortName();
}

QString MapDocument::lastExportFileName() const
Expand All @@ -213,8 +216,6 @@ void MapDocument::setLastExportFileName(const QString &fileName)

MapFormat *MapDocument::exportFormat() const
{
if (map()->exportFormat.isEmpty())
return nullptr;
return findFileFormat<MapFormat>(map()->exportFormat);
}

Expand Down
9 changes: 4 additions & 5 deletions src/tiled/mapdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "tileset.h"

#include <QList>
#include <QPointer>
#include <QRegion>

#include <memory>
Expand Down Expand Up @@ -102,7 +101,7 @@ class MapDocument : public Document
MapFormat *readerFormat() const;
void setReaderFormat(MapFormat *format);

FileFormat *writerFormat() const override;
MapFormat *writerFormat() const override;
void setWriterFormat(MapFormat *format);

QString lastExportFileName() const override;
Expand Down Expand Up @@ -378,11 +377,11 @@ public slots:
void moveObjectIndex(const MapObject *object, int count);

/*
* QPointer is used since the formats referenced here may be dynamically
* QString is used since the formats referenced here may be dynamically
* added by a plugin, and can also be removed again.
*/
QPointer<MapFormat> mReaderFormat;
QPointer<MapFormat> mWriterFormat;
QString mReaderFormat;
QString mWriterFormat;
std::unique_ptr<Map> mMap;
LayerModel *mLayerModel;
QRegion mSelectedArea;
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ void MapEditor::handleExternalTilesetsAndImages(const QStringList &fileNames,
tileset = tilesetFormat->read(fileName);
if (tileset) {
tileset->setFileName(fileName);
tileset->setFormat(tilesetFormat);
tileset->setFormat(tilesetFormat->shortName());
tilesets.append(tileset);
continue;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/tiled/objectreferencetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "mapdocument.h"

#include <QList>
#include <QPointer>

namespace Tiled {

Expand Down
2 changes: 1 addition & 1 deletion src/tiled/tilesetdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ void TilesetDock::exportTileset()
}

externalTileset->setFileName(fileName);
externalTileset->setFormat(format);
externalTileset->setFormat(format->shortName());

QUndoCommand *command = new ReplaceTileset(mMapDocument,
mapTilesetIndex,
Expand Down
Loading

0 comments on commit 59f7930

Please sign in to comment.