Skip to content

Commit

Permalink
fix: normalize resolved asset paths using path_clean (#255)
Browse files Browse the repository at this point in the history
Makes all paths relative to the `assets` root. Ensures that LDtk-linked
assets are shared with assets loaded by `AssetServer`,
[bevy_asset_loader](https://github.com/NiklasEi/bevy_asset_loader) and
so on, as long as normalized paths are used for those (as they typically
are), especially when the LDtk project is in a subdirectory.

Fixes #240
  • Loading branch information
focustense authored Oct 24, 2023
1 parent 59eb6b3 commit 33a8998
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ anyhow = "1.0"
thiserror = "1.0"
paste = "1.0"
derive_more = "0.99.17"
path-clean = "1.0.1"

[dev-dependencies]
bevy = "0.11"
Expand Down
51 changes: 50 additions & 1 deletion src/assets/ldtk_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bevy::{
};
use derive_getters::Getters;
use derive_more::From;
use path_clean::PathClean;
use std::collections::HashMap;
use thiserror::Error;

Expand All @@ -24,7 +25,12 @@ use crate::assets::InternalLevels;
use crate::assets::{ExternalLevelMetadata, ExternalLevels};

fn ldtk_path_to_asset_path<'b>(ldtk_path: &Path, rel_path: &str) -> AssetPath<'b> {
ldtk_path.parent().unwrap().join(Path::new(rel_path)).into()
ldtk_path
.parent()
.unwrap()
.join(Path::new(rel_path))
.clean()
.into()
}

/// Main asset for loading LDtk project data.
Expand Down Expand Up @@ -365,6 +371,49 @@ mod tests {
}
}

#[test]
fn normalizes_asset_paths() {
let resolve_path = |project_path, rel_path| {
let asset_path = ldtk_path_to_asset_path(Path::new(project_path), rel_path);
asset_path.path().to_owned()
};

assert_eq!(
resolve_path("project.ldtk", "images/tiles.png"),
Path::new("images/tiles.png")
);
assert_eq!(
resolve_path("projects/sub/project.ldtk", "../images/tiles.png"),
Path::new("projects/images/tiles.png")
);
assert_eq!(
resolve_path("projects/sub/project.ldtk", "../../tiles.png"),
Path::new("tiles.png")
);
}

#[cfg(target_os = "windows")]
#[test]
fn normalizes_windows_asset_paths() {
let resolve_path = |project_path, rel_path| {
let asset_path = ldtk_path_to_asset_path(Path::new(project_path), rel_path);
asset_path.path().to_owned()
};

assert_eq!(
resolve_path("projects\\sub/project.ldtk", "../images/tiles.png"),
Path::new("projects/images/tiles.png")
);
assert_eq!(
resolve_path("projects\\sub/project.ldtk", "../../images/tiles.png"),
Path::new("images/tiles.png")
);
assert_eq!(
resolve_path("projects/sub\\project.ldtk", "../../tiles.png"),
Path::new("tiles.png")
);
}

#[cfg(feature = "internal_levels")]
mod internal_levels {
use crate::{
Expand Down

0 comments on commit 33a8998

Please sign in to comment.