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

[Merged by Bors] - [assets] Fix AssetServer::get_handle_path #2310

Closed
Closed
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
38 changes: 38 additions & 0 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ impl AssetServer {
}
})
.detach();

let handle_id = asset_path.get_id().into();
self.server
.handle_to_path
.write()
.entry(handle_id)
.or_insert_with(|| asset_path.to_owned());

asset_path.into()
}

Expand Down Expand Up @@ -823,4 +831,34 @@ mod test {
assert_eq!(LoadState::Loaded, get_load_state(&handle, &world));
assert!(get_asset(&handle, &world).is_some());
}

#[test]
fn test_get_handle_path() {
const PATH: &str = "path/file.png";

// valid handle
let server = setup(".");
let handle = server.load_untyped(PATH);
let handle_path = server.get_handle_path(&handle).unwrap();

assert_eq!(handle_path.path(), Path::new(PATH));
assert!(handle_path.label().is_none());

let handle_id: HandleId = handle.into();
let path_id: HandleId = handle_path.get_id().into();
assert_eq!(handle_id, path_id);

// invalid handle (not loaded through server)
let mut assets = server.register_asset_type::<PngAsset>();
let handle = assets.add(PngAsset);
assert!(server.get_handle_path(&handle).is_none());

// invalid HandleId
let invalid_id = HandleId::new(Uuid::new_v4(), 42);
assert!(server.get_handle_path(invalid_id).is_none());

// invalid AssetPath
let invalid_path = AssetPath::new("some/path.ext".into(), None);
assert!(server.get_handle_path(invalid_path).is_none());
}
}