From fbaa7c6aa4be7700337ba1e2660e2aa99c1598f8 Mon Sep 17 00:00:00 2001 From: NathanSWard Date: Sun, 6 Jun 2021 10:04:55 -0600 Subject: [PATCH 1/7] Fix 'get_handle_path' --- crates/bevy_asset/src/asset_server.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index d7b355b879f2a..6558d4fdf9ab5 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -233,7 +233,7 @@ impl AssetServer { async fn load_async( &self, - asset_path: AssetPath<'_>, + asset_path: AssetPath<'static>, force: bool, ) -> Result { let asset_loader = self.get_path_asset_loader(asset_path.path())?; @@ -265,6 +265,13 @@ impl AssetServer { return Ok(asset_path_id); } + // add the asset to internal HashMap + let handle_id: HandleId = asset_path.get_id().into(); + self.server + .handle_to_path + .write() + .insert(handle_id, asset_path.clone()); + source_info.load_state = LoadState::Loading; source_info.committed_assets.clear(); source_info.version += 1; From b8130f5fcbb7b8e0ce078411264245cc57d2d2c8 Mon Sep 17 00:00:00 2001 From: NathanW Date: Mon, 7 Jun 2021 09:18:44 -0600 Subject: [PATCH 2/7] remove unnecessary type --- crates/bevy_asset/src/asset_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 6558d4fdf9ab5..6336d8573233d 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -266,7 +266,7 @@ impl AssetServer { } // add the asset to internal HashMap - let handle_id: HandleId = asset_path.get_id().into(); + let handle_id = asset_path.get_id().into(); self.server .handle_to_path .write() From 607dc0ca324589fe7914b00d1365c8291fe3e60b Mon Sep 17 00:00:00 2001 From: NathanW Date: Mon, 7 Jun 2021 13:50:50 -0600 Subject: [PATCH 3/7] move inserting of asset path to load_untracked --- crates/bevy_asset/src/asset_server.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 6336d8573233d..1af349bcddc5c 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -233,7 +233,7 @@ impl AssetServer { async fn load_async( &self, - asset_path: AssetPath<'static>, + asset_path: AssetPath<'_>, force: bool, ) -> Result { let asset_loader = self.get_path_asset_loader(asset_path.path())?; @@ -265,13 +265,6 @@ impl AssetServer { return Ok(asset_path_id); } - // add the asset to internal HashMap - let handle_id = asset_path.get_id().into(); - self.server - .handle_to_path - .write() - .insert(handle_id, asset_path.clone()); - source_info.load_state = LoadState::Loading; source_info.committed_assets.clear(); source_info.version += 1; @@ -373,6 +366,15 @@ impl AssetServer { } }) .detach(); + + let handle_id = asset_path.get_id().into(); + if !self.server.handle_to_path.read().contains_key(&handle_id) { + self.server + .handle_to_path + .write() + .insert(handle_id, asset_path.to_owned()); + } + asset_path.into() } From 1bdd9ff92b5caa47e17cbe47c0870a56a3535d23 Mon Sep 17 00:00:00 2001 From: NathanW Date: Mon, 7 Jun 2021 13:52:49 -0600 Subject: [PATCH 4/7] add comment to clarify read/write --- crates/bevy_asset/src/asset_server.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 1af349bcddc5c..b77d8546be630 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -368,6 +368,8 @@ impl AssetServer { .detach(); let handle_id = asset_path.get_id().into(); + + // check if the `handle_id` exists first to avoid unnecessary `write()` calls. if !self.server.handle_to_path.read().contains_key(&handle_id) { self.server .handle_to_path From 9af29759d846fac8cfd95a419a67abca3761988e Mon Sep 17 00:00:00 2001 From: NathanW Date: Mon, 7 Jun 2021 15:17:24 -0600 Subject: [PATCH 5/7] remove extra read() call --- crates/bevy_asset/src/asset_server.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index b77d8546be630..4399007b08827 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -368,14 +368,11 @@ impl AssetServer { .detach(); let handle_id = asset_path.get_id().into(); - - // check if the `handle_id` exists first to avoid unnecessary `write()` calls. - if !self.server.handle_to_path.read().contains_key(&handle_id) { - self.server - .handle_to_path - .write() - .insert(handle_id, asset_path.to_owned()); - } + self.server + .handle_to_path + .write() + .entry(handle_id) + .or_insert_with(|| asset_path.to_owned()); asset_path.into() } From 7e86244323cfb292e0a8d0fec8dcfb1bd34ace6b Mon Sep 17 00:00:00 2001 From: NathanSWard Date: Tue, 8 Jun 2021 00:14:16 -0600 Subject: [PATCH 6/7] add get_handle_path tests --- crates/bevy_asset/src/asset_server.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 4399007b08827..d44dd1ed10331 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -831,4 +831,25 @@ 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 + let invalid = HandleId::new(Uuid::new_v4(), 42); + assert!(server.get_handle_path(invalid).is_none()); + } } From 8c8bafbe48adc927f1de3ecd2c9b5575a0e28970 Mon Sep 17 00:00:00 2001 From: NathanW Date: Tue, 8 Jun 2021 10:30:11 -0600 Subject: [PATCH 7/7] add more testing --- crates/bevy_asset/src/asset_server.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index d44dd1ed10331..41c396a264a26 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -848,8 +848,17 @@ mod test { let path_id: HandleId = handle_path.get_id().into(); assert_eq!(handle_id, path_id); - // invalid handle - let invalid = HandleId::new(Uuid::new_v4(), 42); - assert!(server.get_handle_path(invalid).is_none()); + // 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()); } }