From 71bf07f5c085b9c02cbe5337040337c3ed8b0002 Mon Sep 17 00:00:00 2001 From: Nathan Ward Date: Wed, 9 Jun 2021 20:04:22 +0000 Subject: [PATCH] [assets] Fix `AssetServer::get_handle_path` (#2310) # Objective - Currently `AssetServer::get_handle_path` always returns `None` since the inner hash map is never written to. ## Solution - Inside the `load_untracked` function, insert the asset path into the map. This is similar to #1290 (thanks @TheRawMeatball) --- crates/bevy_asset/src/asset_server.rs | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 22b6463dca..2810b03b86 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -374,6 +374,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() } @@ -831,4 +839,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::(); + 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()); + } }