Skip to content

Commit ee8359b

Browse files
committed
Fix nested imports
1 parent fb01856 commit ee8359b

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/supertux/tile_set_parser.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@ TileSetParser::TileSetParser(TileSet& tileset, const std::string& filename) :
3939
}
4040

4141
void
42-
TileSetParser::parse(uint32_t start, uint32_t end, int32_t offset, bool imported)
42+
TileSetParser::parse(int32_t start, int32_t end, int32_t offset, bool imported)
4343
{
44-
if (offset && static_cast<int32_t>(start) + offset < 1) {
44+
if (offset && start + offset < 1) {
4545
start = -offset + 1;
4646
log_warning << "The defined offset would assign non-positive ids to tiles, tiles below " << -offset + 1 << " will be ignored." << std::endl;
4747
}
48-
if (end < start) {
49-
log_warning << "The defined range has a negative size, no tiles will be imported." << std::endl;
48+
if (end < 0) {
49+
log_warning << "Cannot import tiles with negative IDs." << std::endl;
5050
return;
5151
}
52+
if (start < 0) {
53+
log_warning << "Cannot import tiles with negative IDs. Importing will start at ID 1." << std::endl;
54+
start = 1;
55+
}
56+
if (imported && !end) {
57+
log_warning << "Importing a tileset with no upper ID limit can cause ID conflicts if the imported tileset is expanded in the future." <<std::endl;
58+
}
5259

5360
m_tiles_path = FileSystem::dirname(m_filename);
5461

@@ -104,12 +111,22 @@ TileSetParser::parse(uint32_t start, uint32_t end, int32_t offset, bool imported
104111
{
105112
ReaderMapping reader = iter.as_mapping();
106113
std::string import_filename;
107-
uint32_t import_start = 0, import_end = 0;
108-
int32_t import_offset = 0;
114+
int32_t import_start = 0, import_end = 0, import_offset = 0;
109115
reader.get("file", import_filename);
110116
reader.get("start", import_start);
111117
reader.get("end", import_end);
112118
reader.get("offset", import_offset);
119+
if (import_start + import_offset < start) {
120+
import_start = (start - import_offset) < 0 ? 0 : (start - import_offset);
121+
}
122+
if (end && (!import_end || (import_end + import_offset) > end)) {
123+
import_end = end - import_offset;
124+
}
125+
if (import_end < import_start) {
126+
if (!imported) log_warning << "The defined range has a negative size, no tiles will be imported." << std::endl;
127+
continue;
128+
}
129+
import_offset += offset;
113130
TileSetParser import_parser(m_tileset, import_filename);
114131
import_parser.parse(import_start, import_end, import_offset, true);
115132
}
@@ -126,14 +143,14 @@ TileSetParser::parse(uint32_t start, uint32_t end, int32_t offset, bool imported
126143
}
127144

128145
void
129-
TileSetParser::parse_tile(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset)
146+
TileSetParser::parse_tile(const ReaderMapping& reader, int32_t min, int32_t max, int32_t offset)
130147
{
131148
uint32_t id;
132149
if (!reader.get("id", id))
133150
{
134151
throw std::runtime_error("Missing tile-id.");
135152
}
136-
if (max && (id < min || id > max)) return;
153+
if (max && (id < static_cast<uint32_t>(min) || id > static_cast<uint32_t>(max))) return;
137154
id += offset;
138155

139156
uint32_t attributes = 0;
@@ -211,7 +228,7 @@ TileSetParser::parse_tile(const ReaderMapping& reader, uint32_t min, uint32_t ma
211228
}
212229

213230
void
214-
TileSetParser::parse_tiles(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset)
231+
TileSetParser::parse_tiles(const ReaderMapping& reader, int32_t min, int32_t max, int32_t offset)
215232
{
216233
// List of ids (use 0 if the tile should be ignored)
217234
std::vector<uint32_t> ids;
@@ -300,7 +317,7 @@ TileSetParser::parse_tiles(const ReaderMapping& reader, uint32_t min, uint32_t m
300317

301318
for (size_t i = 0; i < ids.size(); ++i)
302319
{
303-
if (!ids[i] || (max && (ids[i] < min || ids[i] > max))) continue;
320+
if (!ids[i] || (max && (ids[i] < static_cast<uint32_t>(min) || ids[i] > static_cast<uint32_t>(max)))) continue;
304321
ids[i] += offset;
305322

306323
const int x = static_cast<int>(32 * (i % width));
@@ -333,7 +350,7 @@ TileSetParser::parse_tiles(const ReaderMapping& reader, uint32_t min, uint32_t m
333350
{
334351
for (size_t i = 0; i < ids.size(); ++i)
335352
{
336-
if(!ids[i] || (max && (ids[i] < min || ids[i] > max))) continue;
353+
if(!ids[i] || (max && (ids[i] < static_cast<uint32_t>(min) || ids[i] > static_cast<uint32_t>(max)))) continue;
337354
ids[i] += offset;
338355

339356
int x = static_cast<int>(32 * (i % width));

src/supertux/tile_set_parser.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class TileSetParser final
3838
public:
3939
TileSetParser(TileSet& tileset, const std::string& filename);
4040

41-
void parse(uint32_t start = 0, uint32_t end = 0, int32_t offset = 0, bool imported = false);
41+
void parse(int32_t start = 0, int32_t end = 0, int32_t offset = 0, bool imported = false);
4242

4343
private:
44-
void parse_tile(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset);
45-
void parse_tiles(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset);
44+
void parse_tile(const ReaderMapping& reader, int32_t min, int32_t max, int32_t offset);
45+
void parse_tiles(const ReaderMapping& reader, int32_t min, int32_t max, int32_t offset);
4646
std::vector<SurfacePtr> parse_imagespecs(const ReaderMapping& cur,
4747
const boost::optional<Rect>& region = boost::none) const;
4848

0 commit comments

Comments
 (0)