1 /*
2  * tilesetformat.cpp
3  * Copyright 2015, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
4  *
5  * This file is part of libtiled.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *    1. Redistributions of source code must retain the above copyright notice,
11  *       this list of conditions and the following disclaimer.
12  *
13  *    2. Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in the
15  *       documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20  * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tilesetformat.h"
30 
31 #include "mapreader.h"
32 
33 namespace Tiled {
34 
readTileset(const QString & fileName,QString * error)35 SharedTileset readTileset(const QString &fileName, QString *error)
36 {
37     // Try the first registered tileset format that claims to support the file
38     if (TilesetFormat *format = findSupportingTilesetFormat(fileName)) {
39         SharedTileset tileset = format->read(fileName);
40 
41         if (error) {
42             if (!tileset)
43                 *error = format->errorString();
44             else
45                 *error = QString();
46         }
47 
48         if (tileset) {
49             tileset->setFileName(fileName);
50             tileset->setFormat(format->shortName());
51         }
52 
53         return tileset;
54     }
55 
56     // Fall back to default reader (TSX format)
57     MapReader reader;
58     SharedTileset tileset = reader.readTileset(fileName);
59 
60     if (error) {
61         if (!tileset)
62             *error = reader.errorString();
63         else
64             *error = QString();
65     }
66 
67     if (tileset)
68         tileset->setFileName(fileName);
69 
70     return tileset;
71 }
72 
findSupportingTilesetFormat(const QString & fileName)73 TilesetFormat *findSupportingTilesetFormat(const QString &fileName)
74 {
75     const auto tilesetFormats = PluginManager::objects<TilesetFormat>();
76     for (TilesetFormat *format : tilesetFormats)
77         if (format->supportsFile(fileName))
78             return format;
79     return nullptr;
80 }
81 
82 } // namespace Tiled
83 
84 #include "moc_tilesetformat.cpp"
85