get rid of more path function usages from AssetPath
This commit is contained in:
parent
da9ecbc4ed
commit
f73f88f439
@ -105,7 +105,9 @@ impl AssetReader for FileAssetReader {
|
|||||||
Ok(file) => Ok(FileReader(file)),
|
Ok(file) => Ok(FileReader(file)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if e.kind() == std::io::ErrorKind::NotFound {
|
if e.kind() == std::io::ErrorKind::NotFound {
|
||||||
Err(AssetReaderError::NotFound(full_path))
|
Err(AssetReaderError::NotFound(
|
||||||
|
full_path.to_str().unwrap().to_owned(),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
Err(e.into())
|
Err(e.into())
|
||||||
}
|
}
|
||||||
@ -120,7 +122,9 @@ impl AssetReader for FileAssetReader {
|
|||||||
Ok(file) => Ok(FileReader(file)),
|
Ok(file) => Ok(FileReader(file)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if e.kind() == std::io::ErrorKind::NotFound {
|
if e.kind() == std::io::ErrorKind::NotFound {
|
||||||
Err(AssetReaderError::NotFound(full_path))
|
Err(AssetReaderError::NotFound(
|
||||||
|
full_path.to_str().unwrap().to_owned(),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
Err(e.into())
|
Err(e.into())
|
||||||
}
|
}
|
||||||
@ -164,7 +168,9 @@ impl AssetReader for FileAssetReader {
|
|||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if e.kind() == std::io::ErrorKind::NotFound {
|
if e.kind() == std::io::ErrorKind::NotFound {
|
||||||
Err(AssetReaderError::NotFound(full_path))
|
Err(AssetReaderError::NotFound(
|
||||||
|
full_path.to_str().unwrap().to_owned(),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
Err(e.into())
|
Err(e.into())
|
||||||
}
|
}
|
||||||
@ -176,7 +182,7 @@ impl AssetReader for FileAssetReader {
|
|||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
let metadata = full_path
|
let metadata = full_path
|
||||||
.metadata()
|
.metadata()
|
||||||
.map_err(|_e| AssetReaderError::NotFound(path.to_owned()))?;
|
.map_err(|_e| AssetReaderError::NotFound(path.to_str().unwrap().to_owned()))?;
|
||||||
Ok(metadata.file_type().is_dir())
|
Ok(metadata.file_type().is_dir())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,9 +43,9 @@ impl ProcessorGatedReader {
|
|||||||
path: &AssetPath<'static>,
|
path: &AssetPath<'static>,
|
||||||
) -> Result<RwLockReadGuardArc<()>, AssetReaderError> {
|
) -> Result<RwLockReadGuardArc<()>, AssetReaderError> {
|
||||||
let infos = self.processor_data.asset_infos.read().await;
|
let infos = self.processor_data.asset_infos.read().await;
|
||||||
let info = infos.get(path).ok_or_else(|| {
|
let info = infos
|
||||||
AssetReaderError::NotFound(PathBuf::from(path.path()).to_str().unwrap().to_owned())
|
.get(path)
|
||||||
})?;
|
.ok_or_else(|| AssetReaderError::NotFound(path.internal_path().to_owned()))?;
|
||||||
Ok(info.file_transaction_lock.read_arc().await)
|
Ok(info.file_transaction_lock.read_arc().await)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -357,7 +357,7 @@ impl<'a> LoadContext<'a> {
|
|||||||
/// # use bevy_reflect::TypePath;
|
/// # use bevy_reflect::TypePath;
|
||||||
/// # #[derive(Asset, TypePath, Default)]
|
/// # #[derive(Asset, TypePath, Default)]
|
||||||
/// # struct Image;
|
/// # struct Image;
|
||||||
/// # let load_context: LoadContext = panic!();
|
/// # let mut load_context: LoadContext = panic!();
|
||||||
/// let mut handles = Vec::new();
|
/// let mut handles = Vec::new();
|
||||||
/// for i in 0..2 {
|
/// for i in 0..2 {
|
||||||
/// let mut labeled = load_context.begin_labeled_asset();
|
/// let mut labeled = load_context.begin_labeled_asset();
|
||||||
@ -459,8 +459,8 @@ impl<'a> LoadContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the source path for this load context.
|
/// Gets the source path for this load context.
|
||||||
pub fn path(&self) -> &Path {
|
pub fn path(&self) -> &str {
|
||||||
self.asset_path.path()
|
self.asset_path.internal_path()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the source asset path for this load context.
|
/// Gets the source asset path for this load context.
|
||||||
|
@ -304,6 +304,10 @@ impl<'a> AssetPath<'a> {
|
|||||||
Path::new(self.path.as_ref())
|
Path::new(self.path.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn internal_path(&self) -> &str {
|
||||||
|
self.path.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the path to the asset in the "virtual filesystem" without a label (if a label is currently set).
|
/// Gets the path to the asset in the "virtual filesystem" without a label (if a label is currently set).
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn without_label(&self) -> AssetPath<'_> {
|
pub fn without_label(&self) -> AssetPath<'_> {
|
||||||
@ -510,7 +514,14 @@ impl<'a> AssetPath<'a> {
|
|||||||
/// Returns the file name
|
/// Returns the file name
|
||||||
/// Ex: Returns `"a.json"` for `"a/b/c/a.json"`
|
/// Ex: Returns `"a.json"` for `"a/b/c/a.json"`
|
||||||
pub fn file_name(&self) -> Option<String> {
|
pub fn file_name(&self) -> Option<String> {
|
||||||
self.path().file_name()?.to_str().map(String::from)
|
self.path.split("/").last().map(ToString::to_string)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new AssetPath with the additional extension, joined by a `/`.
|
||||||
|
pub fn join(&self, suffix: &str) -> AssetPath<'a> {
|
||||||
|
let mut new_path = self.clone();
|
||||||
|
new_path.path = CowArc::from(alloc::format!("{new_path}/{suffix}"));
|
||||||
|
new_path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the full extension (including multiple '.' values).
|
/// Returns the full extension (including multiple '.' values).
|
||||||
|
@ -30,7 +30,7 @@ pub(crate) fn texture_handle(
|
|||||||
if let Ok(_data_uri) = DataUri::parse(uri) {
|
if let Ok(_data_uri) = DataUri::parse(uri) {
|
||||||
load_context.get_label_handle(texture_label(texture).to_string())
|
load_context.get_label_handle(texture_label(texture).to_string())
|
||||||
} else {
|
} else {
|
||||||
let parent = load_context.path().parent().unwrap();
|
let parent = load_context.asset_path().parent().unwrap();
|
||||||
let image_path = parent.join(uri);
|
let image_path = parent.join(uri);
|
||||||
load_context.load(image_path)
|
load_context.load(image_path)
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ use std::{
|
|||||||
#[cfg(feature = "bevy_animation")]
|
#[cfg(feature = "bevy_animation")]
|
||||||
use bevy_animation::{prelude::*, AnimationTarget, AnimationTargetId};
|
use bevy_animation::{prelude::*, AnimationTarget, AnimationTargetId};
|
||||||
use bevy_asset::{
|
use bevy_asset::{
|
||||||
io::Reader, AssetLoadError, AssetLoader, Handle, LoadContext, ReadAssetBytesError,
|
io::Reader, AssetLoadError, AssetLoader, AssetPath, Handle, LoadContext, ReadAssetBytesError,
|
||||||
RenderAssetUsages,
|
RenderAssetUsages,
|
||||||
};
|
};
|
||||||
use bevy_color::{Color, LinearRgba};
|
use bevy_color::{Color, LinearRgba};
|
||||||
@ -237,15 +237,7 @@ async fn load_gltf<'a, 'b, 'c>(
|
|||||||
) -> Result<Gltf, GltfError> {
|
) -> Result<Gltf, GltfError> {
|
||||||
let gltf = gltf::Gltf::from_slice(bytes)?;
|
let gltf = gltf::Gltf::from_slice(bytes)?;
|
||||||
|
|
||||||
let file_name = load_context
|
let file_name = load_context.asset_path().internal_path().to_string();
|
||||||
.asset_path()
|
|
||||||
.path()
|
|
||||||
.to_str()
|
|
||||||
.ok_or(GltfError::Gltf(gltf::Error::Io(Error::new(
|
|
||||||
std::io::ErrorKind::InvalidInput,
|
|
||||||
"Gltf file name invalid",
|
|
||||||
))))?
|
|
||||||
.to_string();
|
|
||||||
let buffer_data = load_buffers(&gltf, load_context).await?;
|
let buffer_data = load_buffers(&gltf, load_context).await?;
|
||||||
|
|
||||||
let linear_textures = get_linear_textures(&gltf.document);
|
let linear_textures = get_linear_textures(&gltf.document);
|
||||||
@ -531,12 +523,12 @@ async fn load_gltf<'a, 'b, 'c>(
|
|||||||
let mut _texture_handles = Vec::new();
|
let mut _texture_handles = Vec::new();
|
||||||
if gltf.textures().len() == 1 || cfg!(target_arch = "wasm32") {
|
if gltf.textures().len() == 1 || cfg!(target_arch = "wasm32") {
|
||||||
for texture in gltf.textures() {
|
for texture in gltf.textures() {
|
||||||
let parent_path = load_context.path().parent().unwrap();
|
let parent_path = load_context.asset_path().parent().unwrap();
|
||||||
let image = load_image(
|
let image = load_image(
|
||||||
texture,
|
texture,
|
||||||
&buffer_data,
|
&buffer_data,
|
||||||
&linear_textures,
|
&linear_textures,
|
||||||
parent_path,
|
&parent_path,
|
||||||
loader.supported_compressed_formats,
|
loader.supported_compressed_formats,
|
||||||
default_sampler,
|
default_sampler,
|
||||||
settings,
|
settings,
|
||||||
@ -549,7 +541,7 @@ async fn load_gltf<'a, 'b, 'c>(
|
|||||||
IoTaskPool::get()
|
IoTaskPool::get()
|
||||||
.scope(|scope| {
|
.scope(|scope| {
|
||||||
gltf.textures().for_each(|gltf_texture| {
|
gltf.textures().for_each(|gltf_texture| {
|
||||||
let parent_path = load_context.path().parent().unwrap();
|
let parent_path = load_context.asset_path().parent().unwrap();
|
||||||
let linear_textures = &linear_textures;
|
let linear_textures = &linear_textures;
|
||||||
let buffer_data = &buffer_data;
|
let buffer_data = &buffer_data;
|
||||||
scope.spawn(async move {
|
scope.spawn(async move {
|
||||||
@ -557,7 +549,7 @@ async fn load_gltf<'a, 'b, 'c>(
|
|||||||
gltf_texture,
|
gltf_texture,
|
||||||
buffer_data,
|
buffer_data,
|
||||||
linear_textures,
|
linear_textures,
|
||||||
parent_path,
|
&parent_path,
|
||||||
loader.supported_compressed_formats,
|
loader.supported_compressed_formats,
|
||||||
default_sampler,
|
default_sampler,
|
||||||
settings,
|
settings,
|
||||||
@ -970,11 +962,11 @@ async fn load_gltf<'a, 'b, 'c>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Loads a glTF texture as a bevy [`Image`] and returns it together with its label.
|
/// Loads a glTF texture as a bevy [`Image`] and returns it together with its label.
|
||||||
async fn load_image<'a, 'b>(
|
async fn load_image<'a, 'b, 'c>(
|
||||||
gltf_texture: gltf::Texture<'a>,
|
gltf_texture: gltf::Texture<'a>,
|
||||||
buffer_data: &[Vec<u8>],
|
buffer_data: &[Vec<u8>],
|
||||||
linear_textures: &HashSet<usize>,
|
linear_textures: &HashSet<usize>,
|
||||||
parent_path: &'b Path,
|
parent_path: &'b AssetPath<'c>,
|
||||||
supported_compressed_formats: CompressedImageFormats,
|
supported_compressed_formats: CompressedImageFormats,
|
||||||
default_sampler: &ImageSamplerDescriptor,
|
default_sampler: &ImageSamplerDescriptor,
|
||||||
settings: &GltfLoaderSettings,
|
settings: &GltfLoaderSettings,
|
||||||
@ -1026,7 +1018,7 @@ async fn load_image<'a, 'b>(
|
|||||||
} else {
|
} else {
|
||||||
let image_path = parent_path.join(uri);
|
let image_path = parent_path.join(uri);
|
||||||
Ok(ImageOrPath::Path {
|
Ok(ImageOrPath::Path {
|
||||||
path: image_path,
|
path: PathBuf::from(image_path),
|
||||||
is_srgb,
|
is_srgb,
|
||||||
sampler_descriptor,
|
sampler_descriptor,
|
||||||
})
|
})
|
||||||
@ -1624,7 +1616,7 @@ async fn load_buffers(
|
|||||||
Ok(_) => return Err(GltfError::BufferFormatUnsupported),
|
Ok(_) => return Err(GltfError::BufferFormatUnsupported),
|
||||||
Err(()) => {
|
Err(()) => {
|
||||||
// TODO: Remove this and add dep
|
// TODO: Remove this and add dep
|
||||||
let buffer_path = load_context.path().parent().unwrap().join(uri);
|
let buffer_path = load_context.asset_path().parent().unwrap().join(uri);
|
||||||
load_context.read_asset_bytes(buffer_path).await?
|
load_context.read_asset_bytes(buffer_path).await?
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -3,6 +3,7 @@ use bevy_asset::{io::Reader, AssetLoader, LoadContext, RenderAssetUsages};
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::{CompressedImageFormats, ImageSampler};
|
use super::{CompressedImageFormats, ImageSampler};
|
||||||
|
use bevy_ecs::intern::Internable;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Loader for images that can be read by the `image` crate.
|
/// Loader for images that can be read by the `image` crate.
|
||||||
@ -151,19 +152,20 @@ impl AssetLoader for ImageLoader {
|
|||||||
let image_type = match settings.format {
|
let image_type = match settings.format {
|
||||||
ImageFormatSetting::FromExtension => {
|
ImageFormatSetting::FromExtension => {
|
||||||
// use the file extension for the image type
|
// use the file extension for the image type
|
||||||
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
let ext = Box::new(load_context.asset_path().get_full_extension().unwrap());
|
||||||
ImageType::Extension(ext)
|
// TODO: this is a workaround. There is a better solution for this.
|
||||||
|
ImageType::Extension(ext.leak())
|
||||||
}
|
}
|
||||||
ImageFormatSetting::Format(format) => ImageType::Format(format),
|
ImageFormatSetting::Format(format) => ImageType::Format(format),
|
||||||
ImageFormatSetting::Guess => {
|
ImageFormatSetting::Guess => {
|
||||||
let format = image::guess_format(&bytes).map_err(|err| FileTextureError {
|
let format = image::guess_format(&bytes).map_err(|err| FileTextureError {
|
||||||
error: err.into(),
|
error: err.into(),
|
||||||
path: format!("{}", load_context.path().display()),
|
path: format!("{}", load_context.path()),
|
||||||
})?;
|
})?;
|
||||||
ImageType::Format(ImageFormat::from_image_crate_format(format).ok_or_else(
|
ImageType::Format(ImageFormat::from_image_crate_format(format).ok_or_else(
|
||||||
|| FileTextureError {
|
|| FileTextureError {
|
||||||
error: TextureError::UnsupportedTextureFormat(format!("{format:?}")),
|
error: TextureError::UnsupportedTextureFormat(format!("{format:?}")),
|
||||||
path: format!("{}", load_context.path().display()),
|
path: format!("{}", load_context.path()),
|
||||||
},
|
},
|
||||||
)?)
|
)?)
|
||||||
}
|
}
|
||||||
@ -178,7 +180,7 @@ impl AssetLoader for ImageLoader {
|
|||||||
)
|
)
|
||||||
.map_err(|err| FileTextureError {
|
.map_err(|err| FileTextureError {
|
||||||
error: err,
|
error: err,
|
||||||
path: format!("{}", load_context.path().display()),
|
path: format!("{}", load_context.path()),
|
||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,7 +341,7 @@ impl AssetLoader for ShaderLoader {
|
|||||||
settings: &Self::Settings,
|
settings: &Self::Settings,
|
||||||
load_context: &mut LoadContext<'_>,
|
load_context: &mut LoadContext<'_>,
|
||||||
) -> Result<Shader, Self::Error> {
|
) -> Result<Shader, Self::Error> {
|
||||||
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
let ext = load_context.asset_path().get_full_extension().unwrap();
|
||||||
let path = load_context.asset_path().to_string();
|
let path = load_context.asset_path().to_string();
|
||||||
// On windows, the path will inconsistently use \ or /.
|
// On windows, the path will inconsistently use \ or /.
|
||||||
// TODO: remove this once AssetPath forces cross-platform "slash" consistency. See #10511
|
// TODO: remove this once AssetPath forces cross-platform "slash" consistency. See #10511
|
||||||
@ -354,8 +354,8 @@ impl AssetLoader for ShaderLoader {
|
|||||||
The shader defs will be ignored."
|
The shader defs will be ignored."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let mut shader = match ext {
|
let mut shader = match ext.as_str() {
|
||||||
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
|
"spv" => Shader::from_spirv(bytes, load_context.path()),
|
||||||
"wgsl" => Shader::from_wgsl_with_defs(
|
"wgsl" => Shader::from_wgsl_with_defs(
|
||||||
String::from_utf8(bytes)?,
|
String::from_utf8(bytes)?,
|
||||||
path,
|
path,
|
||||||
|
Loading…
Reference in New Issue
Block a user