Permalink
Browse files

Extended the --export-map command-line option

* The source file can now be any readable format, not just TMX files.
* The target file can now be a TMX file as well.

Most notably, this makes --export-map useful for those using JSON maps
and for converting files to TMX format.
  • Loading branch information...
bjorn committed Nov 7, 2017
1 parent da06fe6 commit f55ea17c40f4369ac89aac1494ec6c81cabbfbb5
@@ -54,6 +54,7 @@ DynamicLibrary {
"logginginterface.h",
"map.cpp",
"map.h",
"mapformat.cpp",
"mapformat.h",
"mapobject.cpp",
"mapobject.h",
View
@@ -0,0 +1,73 @@
/*
* mapformat.cpp
* Copyright 2017, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of libtiled.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mapformat.h"
#include "mapreader.h"
namespace Tiled {
Map *readMap(const QString &fileName, QString *error)
{
// Try the first registered map format that claims to support the file
if (MapFormat *format = findSupportingMapFormat(fileName)) {
Map *map = format->read(fileName);
if (error) {
if (!map)
*error = format->errorString();
else
*error = QString();
}
return map;
}
// Fall back to default reader (TMX format)
MapReader reader;
Map *map = reader.readMap(fileName);
if (error) {
if (!map)
*error = reader.errorString();
else
*error = QString();
}
return map;
}
MapFormat *findSupportingMapFormat(const QString &fileName)
{
for (MapFormat *format : PluginManager::objects<MapFormat>())
if (format->supportsFile(fileName))
return format;
return nullptr;
}
} // namespace Tiled
View
@@ -208,4 +208,16 @@ class FormatHelper
QMap<QString, Format*> mFormatByNameFilter;
};
/**
* Attempt to read the given map using any of the map formats added
* to the plugin manager, falling back to the TMX format if none are capable.
*/
TILEDSHARED_EXPORT Map *readMap(const QString &fileName,
QString *error = nullptr);
/**
* Attempts to find a map format supporting the given file.
*/
TILEDSHARED_EXPORT MapFormat *findSupportingMapFormat(const QString &fileName);
} // namespace Tiled
@@ -35,7 +35,7 @@ namespace Tiled {
SharedTileset readTileset(const QString &fileName, QString *error)
{
// Try the first registered tileset format that claims to support the file
if (TilesetFormat *format = findSupportingFormat(fileName)) {
if (TilesetFormat *format = findSupportingTilesetFormat(fileName)) {
SharedTileset tileset = format->read(fileName);
if (error) {
@@ -65,7 +65,7 @@ SharedTileset readTileset(const QString &fileName, QString *error)
return tileset;
}
TilesetFormat *findSupportingFormat(const QString &fileName)
TilesetFormat *findSupportingTilesetFormat(const QString &fileName)
{
for (TilesetFormat *format : PluginManager::objects<TilesetFormat>())
if (format->supportsFile(fileName))
@@ -76,7 +76,7 @@ TILEDSHARED_EXPORT SharedTileset readTileset(const QString &fileName,
/**
* Attempts to find a tileset format supporting the given file.
*/
TILEDSHARED_EXPORT TilesetFormat *findSupportingFormat(const QString &fileName);
TILEDSHARED_EXPORT TilesetFormat *findSupportingTilesetFormat(const QString &fileName);
} // namespace Tiled
View
@@ -33,6 +33,7 @@
#include "stylehelper.h"
#include "tiledapplication.h"
#include "tileset.h"
#include "tmxmapformat.h"
#include "winsparkleautoupdater.h"
#include <QDebug>
@@ -160,12 +161,16 @@ void CommandLineHandler::showExportFormats()
{
PluginManager::instance()->loadPlugins();
qWarning().noquote() << tr("Export formats:");
const auto formats = PluginManager::objects<MapFormat>();
for (MapFormat *format : formats) {
QStringList formats;
for (MapFormat *format : PluginManager::objects<MapFormat>()) {
if (format->hasCapabilities(MapFormat::Write))
qWarning(" %s", qUtf8Printable(format->shortName()));
formats.append(format->shortName());
}
formats.sort(Qt::CaseSensitive);
qWarning().noquote() << tr("Export formats:");
for (const QString &name : formats)
qWarning(" %s", qUtf8Printable(name));
quit = true;
}
@@ -212,6 +217,16 @@ int main(int argc, char *argv[])
LanguageManager *languageManager = LanguageManager::instance();
languageManager->installTranslators();
// Add the built-in file formats
TmxMapFormat tmxMapFormat;
PluginManager::addObject(&tmxMapFormat);
TsxTilesetFormat tsxTilesetFormat;
PluginManager::addObject(&tsxTilesetFormat);
TgxTemplateGroupFormat tgxTemplateGroupFormat;
PluginManager::addObject(&tgxTemplateGroupFormat);
CommandLineHandler commandLine;
if (!commandLine.parse(QCoreApplication::arguments()))
@@ -226,28 +241,28 @@ int main(int argc, char *argv[])
if (commandLine.exportMap) {
// Get the path to the source file and target file
if (commandLine.filesToOpen().length() < 2) {
qWarning().noquote() << QCoreApplication::translate("Command line", "Export syntax is --export-map [format] <tmx file> <target file>");
qWarning().noquote() << QCoreApplication::translate("Command line", "Export syntax is --export-map [format] <source> <target>");
return 1;
}
int index = 0;
const QString *filter = commandLine.filesToOpen().length() > 2 ? &commandLine.filesToOpen().at(index++) : nullptr;
const QString &sourceFile = commandLine.filesToOpen().at(index++);
const QString &targetFile = commandLine.filesToOpen().at(index++);
MapFormat *chosenFormat = nullptr;
auto formats = PluginManager::objects<MapFormat>();
MapFormat *outputFormat = nullptr;
const auto formats = PluginManager::objects<MapFormat>();
if (filter) {
// Find the map format supporting the given filter
for (MapFormat *format : formats) {
if (!format->hasCapabilities(MapFormat::Write))
continue;
if (format->shortName().compare(*filter, Qt::CaseInsensitive) == 0) {
chosenFormat = format;
outputFormat = format;
break;
}
}
if (!chosenFormat) {
if (!outputFormat) {
qWarning().noquote() << QCoreApplication::translate("Command line", "Format not recognized (see --export-formats)");
return 1;
}
@@ -258,29 +273,28 @@ int main(int argc, char *argv[])
if (!format->hasCapabilities(MapFormat::Write))
continue;
if (format->nameFilter().contains(suffix, Qt::CaseInsensitive)) {
if (chosenFormat) {
if (outputFormat) {
qWarning().noquote() << QCoreApplication::translate("Command line", "Non-unique file extension. Can't determine correct export format.");
return 1;
}
chosenFormat = format;
outputFormat = format;
}
}
if (!chosenFormat) {
if (!outputFormat) {
qWarning().noquote() << QCoreApplication::translate("Command line", "No exporter found for target file.");
return 1;
}
}
// Load the source file
MapReader reader;
QScopedPointer<Map> map(reader.readMap(sourceFile));
QScopedPointer<Map> map(readMap(sourceFile, nullptr));
if (!map) {
qWarning().noquote() << QCoreApplication::translate("Command line", "Failed to load source map.");
return 1;
}
// Write out the file
bool success = chosenFormat->write(map.data(), targetFile);
bool success = outputFormat->write(map.data(), targetFile);
if (!success) {
qWarning().noquote() << QCoreApplication::translate("Command line", "Failed to export map to target file.");
View
@@ -109,16 +109,9 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
, mObjectTypesEditor(new ObjectTypesEditor(this))
, mAutomappingManager(new AutomappingManager(this))
, mDocumentManager(DocumentManager::instance())
, mTmxMapFormat(new TmxMapFormat(this))
, mTsxTilesetFormat(new TsxTilesetFormat(this))
, mTgxTemplateGroupFormat(new TgxTemplateGroupFormat(this))
{
mUi->setupUi(this);
PluginManager::addObject(mTmxMapFormat);
PluginManager::addObject(mTsxTilesetFormat);
PluginManager::addObject(mTgxTemplateGroupFormat);
ActionManager::registerAction(mUi->actionNewMap, "file.new_map");
ActionManager::registerAction(mUi->actionNewTileset, "file.new_tileset");
@@ -467,9 +460,6 @@ MainWindow::~MainWindow()
mDocumentManager->deleteEditor(Document::MapDocumentType);
mDocumentManager->deleteEditor(Document::TilesetDocumentType);
PluginManager::removeObject(mTmxMapFormat);
PluginManager::removeObject(mTsxTilesetFormat);
DocumentManager::deleteInstance();
TilesetManager::deleteInstance();
TemplateManager::deleteInstance();
View
@@ -56,9 +56,6 @@ class MapDocumentActionHandler;
class MapScene;
class MapView;
class ObjectTypesEditor;
class TmxMapFormat;
class TsxTilesetFormat;
class TgxTemplateGroupFormat;
class Zoomable;
/**
@@ -216,10 +213,6 @@ private slots:
AutomappingManager *mAutomappingManager;
DocumentManager *mDocumentManager;
TmxMapFormat *mTmxMapFormat;
TsxTilesetFormat *mTsxTilesetFormat;
TgxTemplateGroupFormat *mTgxTemplateGroupFormat;
QPointer<PreferencesDialog> mPreferencesDialog;
QMap<QMainWindow*, QByteArray> mMainWindowStates;
View
@@ -828,7 +828,7 @@ void MapEditor::handleExternalTilesetsAndImages(const QStringList &fileNames,
}
// Check if the file is has a supported tileset format
TilesetFormat *tilesetFormat = findSupportingFormat(fileName);
TilesetFormat *tilesetFormat = findSupportingTilesetFormat(fileName);
if (tilesetFormat) {
tileset = tilesetFormat->read(fileName);
if (tileset) {

0 comments on commit f55ea17

Please sign in to comment.