StarArawn / bevy_tiled

A plugin for rendering tiled maps.
MIT License
151 stars 40 forks source link

"Index out of bounds" when TMX data is missing newlines #48

Open wlinna opened 3 years ago

wlinna commented 3 years ago

Hello,

bevy_tiled crashes with Index out of bounds when a TMX's data (CSV) is missing new-lines. I used ldtk's TMX export to create my TMX file. Here's a small file to reproduce the issue:

<map version="1.4" tiledversion="1.4.2" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="4" height="4" tilewidth="32" tileheight="32" infinite="0" backgroundcolor="#696A79" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="Mountain_landscape" tilewidth="32" tileheight="32" tilecount="256" columns="16" objectalignment="topleft" margin="0" spacing="0">
    <image source="../../mountain_landscape.png" width="512" height="512"/>
</tileset>
<layer id="1" name="Ground" width="4" height="4" opacity="1">
    <data encoding="csv">
236,236,236,236,252,252,252,252,236,96,79,80,252,95,95,79
    </data>
</layer>
</map>

It works if I change data node to this:

    <data encoding="csv">
236,236,236,236,
252,252,252,252,
236,96,79,80,
252,95,95,79
    </data>

Here's how I use bevy_tiled

commands.spawn_bundle(bevy_tiled_prototype::TiledMapBundle {
      map_asset: assets.load("map/stuff/tiled/Arena01.tmx"),
      center: TiledMapCenter(true),
      origin: Transform::from_scale(Vec3::new(1.0, 1.0, 1.0)),
        ..Default::default()
});

Here's the error: thread 'IO Task Pool (0)' panicked at 'index out of bounds: the len is 1 but the index is 1', /*/github.com-1ecc6299db9ec823/bevy_tiled_prototype-0.2.3/src/map.rs:186:46

It works if I break the lines manually or first import my TMX into Tiled editor and then save.

dmtaub commented 3 years ago

@wlinna - it seems that this stems from an ambiguity in the Tiled TMX spec that doesn't specify whether newlines are meaningful. Take the assumption made by tiled-rs (which we rely on) that they are, coupled with ldtk's exporter flattening this CSV data , and you have the perfect storm for this bug.

We could write in a workaround, but it seems like that effort would be better served opening a PR to tiled-rs or ldtk...

wlinna commented 3 years ago

Thanks for the info. I'll report there

wlinna commented 3 years ago

A new version of rs-tiled addressing the issue has been published. I haven't tried it with bevy_tiled yet though (cargo-lock specifies 0.9.4 anyway)

wlinna commented 3 years ago

Alright, I found an easy way to update a transitive dependency (yay): cargo update -p tiled --precise 0.9.5

Now it works. This issue can be closed once you upgrade tiled.

Again, thanks for the information – it helped me report the issue