1 /*
2 * scriptfileformatwrappers.h
3 * Copyright 2019, Phlosioneer <mattmdrr2@gmail.com>
4 *
5 * This file is part of Tiled.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "scriptfileformatwrappers.h"
22
23 #include "editablemap.h"
24 #include "editabletileset.h"
25 #include "mapformat.h"
26 #include "scriptedfileformat.h"
27 #include "scriptmanager.h"
28 #include "tilesetformat.h"
29
30 #include <QCoreApplication>
31 #include <QQmlEngine>
32
33 namespace Tiled {
34
ScriptFileFormatWrapper(FileFormat * format,QObject * parent)35 ScriptFileFormatWrapper::ScriptFileFormatWrapper(FileFormat *format, QObject *parent)
36 : QObject(parent),
37 mFormat(format)
38 {}
39
supportsFile(const QString & filename) const40 bool ScriptFileFormatWrapper::supportsFile(const QString &filename) const
41 {
42 return mFormat->supportsFile(filename);
43 }
44
canRead() const45 bool ScriptFileFormatWrapper::canRead() const
46 {
47 return mFormat->capabilities() & FileFormat::Read;
48 }
49
canWrite() const50 bool ScriptFileFormatWrapper::canWrite() const
51 {
52 return mFormat->capabilities() & FileFormat::Write;
53 }
54
assertCanRead() const55 bool ScriptFileFormatWrapper::assertCanRead() const
56 {
57 if (canRead())
58 return true;
59 auto message = QCoreApplication::translate("Script Errors", "File format doesn't support `read`");
60 ScriptManager::instance().throwError(message);
61 return false;
62 }
63
assertCanWrite() const64 bool ScriptFileFormatWrapper::assertCanWrite() const
65 {
66 if (canWrite())
67 return true;
68 auto message = QCoreApplication::translate("Script Errors", "File format doesn't support `write`");
69 ScriptManager::instance().throwError(message);
70 return false;
71 }
72
73
ScriptTilesetFormatWrapper(TilesetFormat * format,QObject * parent)74 ScriptTilesetFormatWrapper::ScriptTilesetFormatWrapper(TilesetFormat* format, QObject *parent)
75 : ScriptFileFormatWrapper(format, parent)
76 {}
77
read(const QString & filename)78 EditableTileset *ScriptTilesetFormatWrapper::read(const QString &filename)
79 {
80 if (!assertCanRead())
81 return nullptr;
82
83 auto tileset = static_cast<TilesetFormat*>(mFormat)->read(filename);
84 if (!tileset) {
85 auto message = QCoreApplication::translate("Script Errors", "Error reading tileset");
86 ScriptManager::instance().throwError(message);
87 return nullptr;
88 }
89
90 return new EditableTileset(tileset.data());
91 }
92
write(EditableTileset * editable,const QString & filename)93 void ScriptTilesetFormatWrapper::write(EditableTileset *editable, const QString &filename)
94 {
95 if (!editable) {
96 ScriptManager::instance().throwNullArgError(0);
97 return;
98 }
99 if (!assertCanWrite())
100 return;
101
102 auto tileset = editable->tileset();
103 auto success = static_cast<TilesetFormat*>(mFormat)->write(*tileset, filename);
104 if (!success)
105 ScriptManager::instance().throwError(mFormat->errorString());
106 }
107
108
ScriptMapFormatWrapper(MapFormat * format,QObject * parent)109 ScriptMapFormatWrapper::ScriptMapFormatWrapper(MapFormat *format, QObject *parent)
110 : ScriptFileFormatWrapper(format, parent)
111 {}
112
read(const QString & filename)113 EditableMap *ScriptMapFormatWrapper::read(const QString &filename)
114 {
115 if (!assertCanRead())
116 return nullptr;
117
118 auto map = static_cast<MapFormat*>(mFormat)->read(filename);
119 if (!map) {
120 auto message = QCoreApplication::translate("Script Errors", "Error reading map");
121 ScriptManager::instance().throwError(message);
122 return nullptr;
123 }
124
125 return new EditableMap(std::move(map));
126 }
127
write(EditableMap * editable,const QString & filename)128 void ScriptMapFormatWrapper::write(EditableMap *editable, const QString &filename)
129 {
130 if (!editable) {
131 ScriptManager::instance().throwNullArgError(0);
132 return;
133 }
134 if (!assertCanWrite())
135 return;
136
137 auto map = editable->map();
138 auto success = static_cast<MapFormat*>(mFormat)->write(map, filename);
139 if (!success)
140 ScriptManager::instance().throwError(mFormat->errorString());
141 }
142
143 } // namespace Tiled
144
145 #include "moc_scriptfileformatwrappers.cpp"
146