Repository: StarArawn/bevy_tiled (language: rust)

Issue:
Incomplete path passed to tiled::parse_with_path
```text
├── Cargo.toml
├── src/
│      └──  main.rs
├── assets/
│       ├── world.tmx
│       ├── tiles_world.tsx
│       └── tiles_world.png
```

For a project with the above file structure and where `tiles_world.tsx` is an external tileset used by `world.tmx`

when we try to run `asset_server.load("working_map.tmx")`

https://github.com/StarArawn/bevy_tiled/blob/2af6208d6ccd7c9b8da5aed27c46a2e24cf6cce2/src/loader.rs#L25-L32

`load_context.path()` returns the path relative to the assets folder (i.e. `"world.tmx"`)

https://github.com/StarArawn/bevy_tiled/blob/2af6208d6ccd7c9b8da5aed27c46a2e24cf6cce2/src/map.rs#L53-L54

this `asset_path` is then used in `tiled::parse_with_path` to resolve external tilesets which panics with
```rust
Other("External tileset file not found: \"tiles_world.tsx\"")
```
because it will try to look for `"<execution path>/tiles_world.tsx"` instead of `"<execution path>/assets/tiles_world.tsx"`



Discussion / hints:

Fix patch:
diff --git a/src/lib.rs b/src/lib.rs
index 14a4383..51f93bf 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-use bevy::prelude::*;
+use bevy::{asset::AssetServerSettings, prelude::*};
 
 mod utils;
 pub use utils::*;
@@ -19,8 +19,15 @@ pub struct TiledMapPlugin;
 
 impl Plugin for TiledMapPlugin {
     fn build(&self, app: &mut AppBuilder) {
+        let asset_folder = app
+            .world()
+            .get_resource::<AssetServerSettings>()
+            .unwrap()
+            .asset_folder
+            .clone();
+
         app.add_asset::<map::Map>()
-            .init_asset_loader::<loader::TiledMapLoader>()
+            .add_asset_loader(loader::TiledMapLoader::new(asset_folder))
             .add_event::<ObjectReadyEvent>()
             .add_event::<MapReadyEvent>()
             .add_system(process_loaded_tile_maps.system());
diff --git a/src/loader.rs b/src/loader.rs
index 3cd41e2..061ffcc 100644
--- a/src/loader.rs
+++ b/src/loader.rs
@@ -1,14 +1,22 @@
+use std::path::{Path, PathBuf};
+
 use crate::map::Map;
 use anyhow::Result;
 use bevy::{
     asset::{AssetLoader, AssetPath, LoadContext, LoadedAsset},
     utils::BoxedFuture,
 };
-
-#[derive(Default)]
-pub struct TiledMapLoader;
+pub struct TiledMapLoader {
+    asset_folder: PathBuf,
+}
 
 impl TiledMapLoader {
+    pub fn new<P: AsRef<Path>>(path: P) -> Self {
+        TiledMapLoader {
+            asset_folder: path.as_ref().to_path_buf(),
+        }
+    }
+
     pub fn remove_tile_flags(tile: u32) -> u32 {
         let tile = tile & !ALL_FLIP_FLAGS;
         tile
@@ -29,7 +37,7 @@ impl AssetLoader for TiledMapLoader {
     ) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
         Box::pin(async move {
             let path = load_context.path();
-            let mut map = Map::try_from_bytes(path, bytes.into())?;
+            let mut map = Map::try_from_bytes(self.asset_folder.as_path(), path, bytes.into())?;
             let dependencies = map
                 .asset_dependencies
                 .drain(..)
diff --git a/src/map.rs b/src/map.rs
index 735e708..1aeae1a 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -1,4 +1,7 @@
-use crate::{ChunkBundle, TilesetLayer, TileMapChunk, MapLayer, objects::ObjectGroup, utils::project_iso, utils::project_ortho};
+use crate::{
+    objects::ObjectGroup, utils::project_iso, utils::project_ortho, ChunkBundle, MapLayer,
+    TileMapChunk, TilesetLayer,
+};
 use anyhow::Result;
 use bevy::{
     prelude::*,
@@ -50,8 +53,12 @@ impl Map {
         }
     }
 
-    pub fn try_from_bytes(asset_path: &Path, bytes: Vec<u8>) -> Result<Map> {
-        let map = tiled::parse_with_path(BufReader::new(bytes.as_slice()), asset_path).unwrap();
+    pub fn try_from_bytes(asset_folder: &Path, asset_path: &Path, bytes: Vec<u8>) -> Result<Map> {
+        let map = tiled::parse_with_path(
+            BufReader::new(bytes.as_slice()),
+            &asset_folder.join(asset_path),
+        )
+        .unwrap();
 
         let mut layers = Vec::new();
         let mut groups = Vec::new();


Test patch: