Skip to content

Commit

Permalink
[assets] Fix AssetServer::get_handle_path (#2310)
Browse files Browse the repository at this point in the history
# 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)
  • Loading branch information
NathanSWard committed Jun 9, 2021
1 parent b07b2f5 commit 71bf07f
Showing 1 changed file with 38 additions and 0 deletions.
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 @@ -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()
}

Expand Down Expand Up @@ -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::<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());
}
}

0 comments on commit 71bf07f

Please sign in to comment.