From 1669fadc72d7291d52ff1e72fabed808454d5cc2 Mon Sep 17 00:00:00 2001 From: Brandon Dillon Date: Sun, 2 Dec 2012 20:35:05 -0800 Subject: [PATCH] Adding command-line export to tiled.exe --- src/tiled/main.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/tiled/main.cpp b/src/tiled/main.cpp index 77cd0b44e9..308aac173e 100644 --- a/src/tiled/main.cpp +++ b/src/tiled/main.cpp @@ -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 +#include #include #ifdef STATIC_BUILD @@ -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 @@ -77,6 +83,7 @@ CommandLineHandler::CommandLineHandler() : quit(false) , showedVersion(false) , disableOpenGL(false) + , exportMap(false) { option<&CommandLineHandler::showVersion>( QLatin1Char('v'), @@ -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() @@ -115,6 +127,10 @@ void CommandLineHandler::setDisableOpenGL() disableOpenGL = true; } +void CommandLineHandler::setExportMap() +{ + exportMap = true; +} int main(int argc, char *argv[]) { @@ -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 "); + 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 writers = PluginManager::instance()->interfaces(); + 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();