Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line export for Tiled #318

Merged
merged 1 commit into from
Dec 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
#include "mainwindow.h"
#include "languagemanager.h"
#include "pluginmanager.h"
#include "mapdocument.h"
#include "mapreader.h"
#include "mapwriterinterface.h"
#include "preferences.h"
#include "tiledapplication.h"

#include <QDebug>
#include <QFileInfo>
#include <QtPlugin>

#ifdef STATIC_BUILD
Expand All @@ -51,11 +55,13 @@ class CommandLineHandler : public CommandLineParser
bool quit;
bool showedVersion;
bool disableOpenGL;
bool exportMap;

private:
void showVersion();
void justQuit();
void setDisableOpenGL();
void setExportMap();

// Convenience wrapper around registerOption
template <void (CommandLineHandler::*memberFunction)()>
Expand All @@ -77,6 +83,7 @@ CommandLineHandler::CommandLineHandler()
: quit(false)
, showedVersion(false)
, disableOpenGL(false)
, exportMap(false)
{
option<&CommandLineHandler::showVersion>(
QLatin1Char('v'),
Expand All @@ -93,6 +100,11 @@ CommandLineHandler::CommandLineHandler()
QChar(),
QLatin1String("--disable-opengl"),
QLatin1String("Disable hardware accelerated rendering"));

option<&CommandLineHandler::setExportMap>(
QChar(),
QLatin1String("--export-map"),
QLatin1String("Export the specified tmx file to target"));
}

void CommandLineHandler::showVersion()
Expand All @@ -115,6 +127,10 @@ void CommandLineHandler::setDisableOpenGL()
disableOpenGL = true;
}

void CommandLineHandler::setExportMap()
{
exportMap = true;
}

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -155,6 +171,60 @@ int main(int argc, char *argv[])

PluginManager::instance()->loadPlugins();

if (commandLine.exportMap)
{
// Get the path to the source file and target file
if (commandLine.filesToOpen().length() <= 2) {
qWarning() << QObject::tr("Export syntax is --export-map <tmx file> <target file>");
return 1;
}
int index = 0;
const QString *filter = commandLine.filesToOpen().length() > 2 ? &commandLine.filesToOpen().at(index++) : 0;
const QString &sourceFile = commandLine.filesToOpen().at(index++);
const QString &targetFile = commandLine.filesToOpen().at(index++);

// Find the map writer interface for the target file
Tiled::MapWriterInterface *chosenWriter = 0;
QString suffix = QFileInfo(targetFile).completeSuffix();
QList<Tiled::MapWriterInterface*> writers = PluginManager::instance()->interfaces<Tiled::MapWriterInterface>();
foreach (Tiled::MapWriterInterface *writer, writers) {
if (filter) {
if (writer->nameFilters().contains(*filter, Qt::CaseInsensitive)) {
chosenWriter = writer;
}
}
else if (!writer->nameFilters().filter(suffix, Qt::CaseInsensitive).isEmpty()) {
if (chosenWriter) {
qWarning() << QObject::tr("Non-unique file extension. Can't determine correct export format.");
return 1;
}
chosenWriter = writer;
}
}
if (!chosenWriter) {
qWarning() << QObject::tr("No exporter found for target file.");
return 1;
}

// Load the source file
Tiled::MapReader reader;
Tiled::Map *map = reader.readMap(sourceFile);
if (!map) {
qWarning() << QObject::tr("Failed to load source map.");
return 1;
}

// Write out the file
bool success = chosenWriter->write(map, targetFile);
delete map;

if (!success) {
qWarning() << QObject::tr("Failed to export map to target file.");
return 1;
}
return 0;
}

MainWindow w;
w.show();

Expand Down