Skip to content

Commit 71bf07f

Browse files
committed
[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)
1 parent b07b2f5 commit 71bf07f

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

crates/bevy_asset/src/asset_server.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ impl AssetServer {
374374
}
375375
})
376376
.detach();
377+
378+
let handle_id = asset_path.get_id().into();
379+
self.server
380+
.handle_to_path
381+
.write()
382+
.entry(handle_id)
383+
.or_insert_with(|| asset_path.to_owned());
384+
377385
asset_path.into()
378386
}
379387

@@ -831,4 +839,34 @@ mod test {
831839
assert_eq!(LoadState::Loaded, get_load_state(&handle, &world));
832840
assert!(get_asset(&handle, &world).is_some());
833841
}
842+
843+
#[test]
844+
fn test_get_handle_path() {
845+
const PATH: &str = "path/file.png";
846+
847+
// valid handle
848+
let server = setup(".");
849+
let handle = server.load_untyped(PATH);
850+
let handle_path = server.get_handle_path(&handle).unwrap();
851+
852+
assert_eq!(handle_path.path(), Path::new(PATH));
853+
assert!(handle_path.label().is_none());
854+
855+
let handle_id: HandleId = handle.into();
856+
let path_id: HandleId = handle_path.get_id().into();
857+
assert_eq!(handle_id, path_id);
858+
859+
// invalid handle (not loaded through server)
860+
let mut assets = server.register_asset_type::<PngAsset>();
861+
let handle = assets.add(PngAsset);
862+
assert!(server.get_handle_path(&handle).is_none());
863+
864+
// invalid HandleId
865+
let invalid_id = HandleId::new(Uuid::new_v4(), 42);
866+
assert!(server.get_handle_path(invalid_id).is_none());
867+
868+
// invalid AssetPath
869+
let invalid_path = AssetPath::new("some/path.ext".into(), None);
870+
assert!(server.get_handle_path(invalid_path).is_none());
871+
}
834872
}

0 commit comments

Comments
 (0)