Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize resolved asset paths using path_clean. #255

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
focustense marked this conversation as resolved.
Show resolved Hide resolved
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