Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::any::{Any, TypeId};
use downcast_rs::{impl_downcast, Downcast};
use ron::error::SpannedError;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use thiserror::Error;

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

/// Gets the source path for this load context.
pub fn path(&self) -> &Path {
self.asset_path.path()
}

/// Gets the source asset path for this load context.
pub fn asset_path(&self) -> &AssetPath<'static> {
pub fn path(&self) -> &AssetPath<'static> {
&self.asset_path
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/server/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<T: AssetLoader> AssetLoader for InstrumentedAssetLoader<T> {
let span = info_span!(
"asset loading",
loader = core::any::type_name::<T>(),
asset = load_context.asset_path().to_string(),
asset = load_context.path().to_string(),
);
self.0.load(reader, settings, load_context).instrument(span)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_gltf/src/loader/gltf_ext/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) fn texture_handle(
load_context.get_label_handle(texture_label(texture).to_string())
} else {
let image_path = load_context
.asset_path()
.path()
.resolve_embed(uri)
.expect("all URIs were already validated when we initially loaded textures");
load_context.load(image_path)
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_gltf/src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl GltfLoader {
let gltf = gltf::Gltf::from_slice(bytes)?;

let file_name = load_context
.asset_path()
.path()
.path()
.to_str()
.ok_or(GltfError::Gltf(gltf::Error::Io(Error::new(
Expand Down Expand Up @@ -601,7 +601,7 @@ impl GltfLoader {
texture,
&buffer_data,
&linear_textures,
load_context.asset_path(),
load_context.path(),
loader.supported_compressed_formats,
default_sampler,
settings,
Expand All @@ -614,7 +614,7 @@ impl GltfLoader {
IoTaskPool::get()
.scope(|scope| {
gltf.textures().for_each(|gltf_texture| {
let asset_path = load_context.asset_path().clone();
let asset_path = load_context.path().clone();
let linear_textures = &linear_textures;
let buffer_data = &buffer_data;
scope.spawn(async move {
Expand Down Expand Up @@ -1744,7 +1744,7 @@ async fn load_buffers(
Err(()) => {
// TODO: Remove this and add dep
let buffer_path = load_context
.asset_path()
.path()
.resolve_embed(uri)
.map_err(|err| GltfError::InvalidBufferUri(uri.to_owned(), err))?;
load_context.read_asset_bytes(buffer_path).await?
Expand Down
14 changes: 10 additions & 4 deletions crates/bevy_image/src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,25 @@ impl AssetLoader for ImageLoader {
let image_type = match settings.format {
ImageFormatSetting::FromExtension => {
// use the file extension for the image type
let ext = load_context.path().extension().unwrap().to_str().unwrap();
let ext = load_context
.path()
.path()
.extension()
.unwrap()
.to_str()
.unwrap();
ImageType::Extension(ext)
}
ImageFormatSetting::Format(format) => ImageType::Format(format),
ImageFormatSetting::Guess => {
let format = image::guess_format(&bytes).map_err(|err| FileTextureError {
error: err.into(),
path: format!("{}", load_context.path().display()),
path: format!("{}", load_context.path().path().display()),
})?;
ImageType::Format(ImageFormat::from_image_crate_format(format).ok_or_else(
|| FileTextureError {
error: TextureError::UnsupportedTextureFormat(format!("{format:?}")),
path: format!("{}", load_context.path().display()),
path: format!("{}", load_context.path().path().display()),
},
)?)
}
Expand All @@ -208,7 +214,7 @@ impl AssetLoader for ImageLoader {
)
.map_err(|err| FileTextureError {
error: err,
path: format!("{}", load_context.path().display()),
path: format!("{}", load_context.path().path().display()),
})?;

if let Some(format) = settings.texture_format {
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_shader/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,14 @@ impl AssetLoader for ShaderLoader {
settings: &Self::Settings,
load_context: &mut LoadContext<'_>,
) -> Result<Shader, Self::Error> {
let ext = load_context.path().extension().unwrap().to_str().unwrap();
let path = load_context.asset_path().to_string();
let ext = load_context
.path()
.path()
.extension()
.unwrap()
.to_str()
.unwrap();
let path = load_context.path().to_string();
// On windows, the path will inconsistently use \ or /.
// TODO: remove this once AssetPath forces cross-platform "slash" consistency. See #10511
let path = path.replace(std::path::MAIN_SEPARATOR, "/");
Expand All @@ -381,7 +387,7 @@ impl AssetLoader for ShaderLoader {
);
}
let mut shader = match ext {
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
"spv" => Shader::from_spirv(bytes, load_context.path().path().to_string_lossy()),
"wgsl" => Shader::from_wgsl_with_defs(
String::from_utf8(bytes)?,
path,
Expand Down
5 changes: 4 additions & 1 deletion examples/asset/asset_decompression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ impl AssetLoader for GzAssetLoader {
) -> Result<Self::Asset, Self::Error> {
let compressed_path = load_context.path();
let file_name = compressed_path
.path()
.file_name()
.ok_or(GzAssetLoaderError::IndeterminateFilePath)?
.to_string_lossy();
let uncompressed_file_name = file_name
.strip_suffix(".gz")
.ok_or(GzAssetLoaderError::IndeterminateFilePath)?;
let contained_path = compressed_path.join(uncompressed_file_name);
let contained_path = compressed_path
.resolve_embed(uncompressed_file_name)
.map_err(|_| GzAssetLoaderError::IndeterminateFilePath)?;

let mut bytes_compressed = Vec::new();

Expand Down
14 changes: 14 additions & 0 deletions release-content/migration-guides/load_context_asset_path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: LoadContext::path now returns `AssetPath`.
pull_requests: [21713]
---

`LoadContext::asset_path` has been removed, and `LoadContext::path` now returns `AssetPath`. So the
migrations are:

- `load_context.asset_path()` -> `load_context.path()`
- `load_context.path()` -> `load_context.asset_path().path()`
- While this migration will keep your code running, seriously consider whether you need to use
the `Path` itself. The `Path` does not support custom asset sources, so care needs to be taken
when using it directly. Consider instead using the `AssetPath` instead, along with
`AssetPath::resolve_embed`, to properly support custom asset sources.