Skip to content

Commit 8e24209

Browse files
authored
Make LoadContext::path return an AssetPath instead of std::path::Path. (#21713)
# Objective - In #21643, `LoadContext::path` was incorrectly being used to reference relative files. Since this was a `std::path::Path` instead of an `AssetPath`, it did not behave correctly in reference to custom asset sources. ## Solution - Make `LoadContext::path` return an `AssetPath` and remove `LoadContext::asset_path`.
1 parent f45b216 commit 8e24209

File tree

8 files changed

+45
-21
lines changed

8 files changed

+45
-21
lines changed

crates/bevy_asset/src/loader.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::any::{Any, TypeId};
1919
use downcast_rs::{impl_downcast, Downcast};
2020
use ron::error::SpannedError;
2121
use serde::{Deserialize, Serialize};
22-
use std::path::{Path, PathBuf};
22+
use std::path::PathBuf;
2323
use thiserror::Error;
2424

2525
/// Loads an [`Asset`] from a given byte [`Reader`]. This can accept [`AssetLoader::Settings`], which configure how the [`Asset`]
@@ -465,13 +465,8 @@ impl<'a> LoadContext<'a> {
465465
}
466466
}
467467

468-
/// Gets the source path for this load context.
469-
pub fn path(&self) -> &Path {
470-
self.asset_path.path()
471-
}
472-
473468
/// Gets the source asset path for this load context.
474-
pub fn asset_path(&self) -> &AssetPath<'static> {
469+
pub fn path(&self) -> &AssetPath<'static> {
475470
&self.asset_path
476471
}
477472

crates/bevy_asset/src/server/loaders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl<T: AssetLoader> AssetLoader for InstrumentedAssetLoader<T> {
326326
let span = info_span!(
327327
"asset loading",
328328
loader = core::any::type_name::<T>(),
329-
asset = load_context.asset_path().to_string(),
329+
asset = load_context.path().to_string(),
330330
);
331331
self.0.load(reader, settings, load_context).instrument(span)
332332
}

crates/bevy_gltf/src/loader/gltf_ext/texture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn texture_handle(
3131
load_context.get_label_handle(texture_label(texture).to_string())
3232
} else {
3333
let image_path = load_context
34-
.asset_path()
34+
.path()
3535
.resolve_embed(uri)
3636
.expect("all URIs were already validated when we initially loaded textures");
3737
load_context.load(image_path)

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl GltfLoader {
247247
let gltf = gltf::Gltf::from_slice(bytes)?;
248248

249249
let file_name = load_context
250-
.asset_path()
250+
.path()
251251
.path()
252252
.to_str()
253253
.ok_or(GltfError::Gltf(gltf::Error::Io(Error::new(
@@ -601,7 +601,7 @@ impl GltfLoader {
601601
texture,
602602
&buffer_data,
603603
&linear_textures,
604-
load_context.asset_path(),
604+
load_context.path(),
605605
loader.supported_compressed_formats,
606606
default_sampler,
607607
settings,
@@ -614,7 +614,7 @@ impl GltfLoader {
614614
IoTaskPool::get()
615615
.scope(|scope| {
616616
gltf.textures().for_each(|gltf_texture| {
617-
let asset_path = load_context.asset_path().clone();
617+
let asset_path = load_context.path().clone();
618618
let linear_textures = &linear_textures;
619619
let buffer_data = &buffer_data;
620620
scope.spawn(async move {
@@ -1744,7 +1744,7 @@ async fn load_buffers(
17441744
Err(()) => {
17451745
// TODO: Remove this and add dep
17461746
let buffer_path = load_context
1747-
.asset_path()
1747+
.path()
17481748
.resolve_embed(uri)
17491749
.map_err(|err| GltfError::InvalidBufferUri(uri.to_owned(), err))?;
17501750
load_context.read_asset_bytes(buffer_path).await?

crates/bevy_image/src/image_loader.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,25 @@ impl AssetLoader for ImageLoader {
180180
let image_type = match settings.format {
181181
ImageFormatSetting::FromExtension => {
182182
// use the file extension for the image type
183-
let ext = load_context.path().extension().unwrap().to_str().unwrap();
183+
let ext = load_context
184+
.path()
185+
.path()
186+
.extension()
187+
.unwrap()
188+
.to_str()
189+
.unwrap();
184190
ImageType::Extension(ext)
185191
}
186192
ImageFormatSetting::Format(format) => ImageType::Format(format),
187193
ImageFormatSetting::Guess => {
188194
let format = image::guess_format(&bytes).map_err(|err| FileTextureError {
189195
error: err.into(),
190-
path: format!("{}", load_context.path().display()),
196+
path: format!("{}", load_context.path().path().display()),
191197
})?;
192198
ImageType::Format(ImageFormat::from_image_crate_format(format).ok_or_else(
193199
|| FileTextureError {
194200
error: TextureError::UnsupportedTextureFormat(format!("{format:?}")),
195-
path: format!("{}", load_context.path().display()),
201+
path: format!("{}", load_context.path().path().display()),
196202
},
197203
)?)
198204
}
@@ -208,7 +214,7 @@ impl AssetLoader for ImageLoader {
208214
)
209215
.map_err(|err| FileTextureError {
210216
error: err,
211-
path: format!("{}", load_context.path().display()),
217+
path: format!("{}", load_context.path().path().display()),
212218
})?;
213219

214220
if let Some(format) = settings.texture_format {

crates/bevy_shader/src/shader.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,14 @@ impl AssetLoader for ShaderLoader {
367367
settings: &Self::Settings,
368368
load_context: &mut LoadContext<'_>,
369369
) -> Result<Shader, Self::Error> {
370-
let ext = load_context.path().extension().unwrap().to_str().unwrap();
371-
let path = load_context.asset_path().to_string();
370+
let ext = load_context
371+
.path()
372+
.path()
373+
.extension()
374+
.unwrap()
375+
.to_str()
376+
.unwrap();
377+
let path = load_context.path().to_string();
372378
// On windows, the path will inconsistently use \ or /.
373379
// TODO: remove this once AssetPath forces cross-platform "slash" consistency. See #10511
374380
let path = path.replace(std::path::MAIN_SEPARATOR, "/");
@@ -381,7 +387,7 @@ impl AssetLoader for ShaderLoader {
381387
);
382388
}
383389
let mut shader = match ext {
384-
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
390+
"spv" => Shader::from_spirv(bytes, load_context.path().path().to_string_lossy()),
385391
"wgsl" => Shader::from_wgsl_with_defs(
386392
String::from_utf8(bytes)?,
387393
path,

examples/asset/asset_decompression.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ impl AssetLoader for GzAssetLoader {
4848
) -> Result<Self::Asset, Self::Error> {
4949
let compressed_path = load_context.path();
5050
let file_name = compressed_path
51+
.path()
5152
.file_name()
5253
.ok_or(GzAssetLoaderError::IndeterminateFilePath)?
5354
.to_string_lossy();
5455
let uncompressed_file_name = file_name
5556
.strip_suffix(".gz")
5657
.ok_or(GzAssetLoaderError::IndeterminateFilePath)?;
57-
let contained_path = compressed_path.join(uncompressed_file_name);
58+
let contained_path = compressed_path
59+
.resolve_embed(uncompressed_file_name)
60+
.map_err(|_| GzAssetLoaderError::IndeterminateFilePath)?;
5861

5962
let mut bytes_compressed = Vec::new();
6063

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: LoadContext::path now returns `AssetPath`.
3+
pull_requests: [21713]
4+
---
5+
6+
`LoadContext::asset_path` has been removed, and `LoadContext::path` now returns `AssetPath`. So the
7+
migrations are:
8+
9+
- `load_context.asset_path()` -> `load_context.path()`
10+
- `load_context.path()` -> `load_context.asset_path().path()`
11+
- While this migration will keep your code running, seriously consider whether you need to use
12+
the `Path` itself. The `Path` does not support custom asset sources, so care needs to be taken
13+
when using it directly. Consider instead using the `AssetPath` instead, along with
14+
`AssetPath::resolve_embed`, to properly support custom asset sources.

0 commit comments

Comments
 (0)