Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StrongHandle TypeId mismatch when using labels #10465

Closed
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
30 changes: 28 additions & 2 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ pub trait AssetLoader: Send + Sync + 'static {

/// Returns a list of extensions supported by this asset loader, without the preceding dot.
fn extensions(&self) -> &[&str];
/// Returns the type name of the sub-asset type with the given `label`.
fn label_type_name(&self, #[allow(unused_variables)] label: &str) -> Option<&'static str> {
None
}
/// Returns the [`TypeId`] of the sub-asset type with the given `label`.
fn label_type_id(&self, #[allow(unused_variables)] label: &str) -> Option<TypeId> {
None
}
}

/// Provides type-erased access to an [`AssetLoader`].
Expand Down Expand Up @@ -68,6 +76,10 @@ pub trait ErasedAssetLoader: Send + Sync + 'static {
fn asset_type_name(&self) -> &'static str;
/// Returns the [`TypeId`] of the top-level [`Asset`] loaded by the [`AssetLoader`].
fn asset_type_id(&self) -> TypeId;
/// Returns the type name of the sub-asset type with the given `label`.
fn label_type_name(&self, label: Option<&str>) -> Option<&'static str>;
/// Returns the [`TypeId`] of the sub-asset type with the given `label`.
fn label_type_id(&self, label: Option<&str>) -> Option<TypeId>;
}

impl<L> ErasedAssetLoader for L
Expand Down Expand Up @@ -119,12 +131,26 @@ where
TypeId::of::<L>()
}

fn asset_type_name(&self) -> &'static str {
std::any::type_name::<L::Asset>()
}

fn asset_type_id(&self) -> TypeId {
TypeId::of::<L::Asset>()
}

fn asset_type_name(&self) -> &'static str {
std::any::type_name::<L::Asset>()
fn label_type_name(&self, label: Option<&str>) -> Option<&'static str> {
match label {
None => Some(self.asset_type_name()),
Some(label) => <L as AssetLoader>::label_type_name(self, label),
}
}

fn label_type_id(&self, label: Option<&str>) -> Option<TypeId> {
match label {
None => Some(self.asset_type_id()),
Some(label) => <L as AssetLoader>::label_type_id(self, label),
}
}
}

Expand Down
18 changes: 16 additions & 2 deletions crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,19 @@ impl AssetServer {
}
None => {
let mut infos = self.data.infos.write();
let label = path.label();
let (Some(type_id), Some(type_name)) =
(loader.label_type_id(label), loader.label_type_name(label))
else {
return Err(AssetLoadError::WrongLabel {
base_path: path.without_label().into_owned(),
label: label.unwrap_or_default().to_owned(),
});
};
infos.get_or_create_path_handle_untyped(
path.clone(),
loader.asset_type_id(),
loader.asset_type_name(),
type_id,
type_name,
HandleLoadingMode::Request,
meta_transform,
)
Expand Down Expand Up @@ -1053,6 +1062,11 @@ pub enum AssetLoadError {
base_path: AssetPath<'static>,
label: String,
},
#[error("The type of the file at '{base_path}' does not support the label format '{label}'.")]
WrongLabel {
base_path: AssetPath<'static>,
label: String,
},
}

/// An error that occurs when an [`AssetLoader`] is not registered for a given extension.
Expand Down
49 changes: 48 additions & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{vertex_attributes::convert_attribute, Gltf, GltfExtras, GltfNode};
use crate::{vertex_attributes::convert_attribute, Gltf, GltfExtras, GltfMesh, GltfNode};
use bevy_animation::AnimationClip;
use bevy_asset::{
io::Reader, AssetLoadError, AssetLoader, AsyncReadExt, Handle, LoadContext, ReadAssetBytesError,
};
Expand Down Expand Up @@ -41,6 +42,7 @@ use gltf::{
};
use serde::Deserialize;
use std::{
any::TypeId,
collections::VecDeque,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -125,6 +127,51 @@ impl AssetLoader for GltfLoader {
fn extensions(&self) -> &[&str] {
&["gltf", "glb"]
}

fn label_type_name(&self, label: &str) -> Option<&'static str> {
match label_type(label) {
"Scene" => Some(std::any::type_name::<Scene>()),
"Node" => Some(std::any::type_name::<GltfNode>()),
"Mesh" => Some(std::any::type_name::<GltfMesh>()),
"Primitive" => Some(std::any::type_name::<Mesh>()),
//"MorphTarget" => Some(std::any::type_name::<MorphTarget>()),
"Texture" => Some(std::any::type_name::<Image>()),
"Material" | "DefaultMaterial" => Some(std::any::type_name::<StandardMaterial>()),
"Animation" => Some(std::any::type_name::<AnimationClip>()),
"Skin" => Some(std::any::type_name::<SkinnedMeshInverseBindposes>()),
_ => None,
}
}

fn label_type_id(&self, label: &str) -> Option<TypeId> {
match label_type(label) {
"Scene" => Some(TypeId::of::<Scene>()),
"Node" => Some(TypeId::of::<GltfNode>()),
"Mesh" => Some(TypeId::of::<GltfMesh>()),
"Primitive" => Some(TypeId::of::<Mesh>()),
//"MorphTarget" => Some(TypeId::of::<MorphTarget>()),
"Texture" => Some(TypeId::of::<Image>()),
"Material" | "DefaultMaterial" => Some(TypeId::of::<StandardMaterial>()),
"Animation" => Some(TypeId::of::<AnimationClip>()),
"Skin" => Some(TypeId::of::<SkinnedMeshInverseBindposes>()),
_ => None,
}
}
}

fn label_type(label: &str) -> &str {
let mut label_type_range = 0..label.len();
for (index, char) in label.char_indices() {
match char {
'/' => label_type_range = index + 1..label.len(),
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
label_type_range.end = index;
}
_ => (),
}
}

&label[label_type_range]
}

/// Loads an entire glTF file.
Expand Down