-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Wrong asset loader is used after extensionless assets change #11638
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
Comments
Or here's a smaller repro: Openuse bevy::utils::thiserror;
use bevy::{
asset::{io::Reader, AssetLoader, LoadContext},
prelude::*,
reflect::TypePath,
utils::BoxedFuture,
};
use thiserror::Error;
#[non_exhaustive]
#[derive(Debug, Error)]
#[error("error")]
pub struct GenericLoaderError;
#[derive(Asset, TypePath, Debug, Default)]
pub struct C(String);
#[derive(Default)]
pub struct ALoader;
impl AssetLoader for ALoader {
type Asset = C;
type Settings = ();
type Error = GenericLoaderError;
fn load<'a>(
&'a self,
_reader: &'a mut Reader,
_settings: &'a (),
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move { Ok(C("A".to_string())) })
}
fn extensions(&self) -> &[&str] {
&["a"]
}
}
#[derive(Default)]
pub struct BLoader;
impl AssetLoader for BLoader {
type Asset = C;
type Settings = ();
type Error = GenericLoaderError;
fn load<'a>(
&'a self,
_reader: &'a mut Reader,
_settings: &'a (),
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move { Ok(C("B".to_string())) })
}
fn extensions(&self) -> &[&str] {
&["b"]
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_resource::<State>()
.init_asset::<C>()
.register_asset_loader(ALoader)
.register_asset_loader(BLoader)
.add_systems(Startup, setup)
.add_systems(Update, print_on_load)
.run();
}
#[derive(Resource, Default)]
struct State {
handle_a: Handle<C>,
handle_b: Handle<C>,
printed: bool,
}
fn setup(mut state: ResMut<State>, asset_server: Res<AssetServer>) {
state.handle_a = asset_server.load("data/custom.a");
state.handle_b = asset_server.load("data/custom.b");
}
fn print_on_load(mut state: ResMut<State>, a_assets: Res<Assets<C>>) {
if state.printed {
return;
}
let a_asset = a_assets.get(&state.handle_a);
let b_asset = a_assets.get(&state.handle_b);
if b_asset.is_none() && b_asset.is_none() {
return;
}
info!("Loaded: {:?} {:?}", a_asset.unwrap(), b_asset.unwrap());
state.printed = true;
} Before #10153: |
Hi, I believe the issue here is caused by your two different loaders producing the same asset type I believe this can be fixed by amending I'm terribly sorry this popped up for you, I'll make a PR based on my outline here and hopefully get that fixed soon! |
I believe I'm hitting a related issue but in my case it's a custom use bevy::asset::io::Reader;
use bevy::asset::{AssetLoader, LoadContext};
use bevy::prelude::*;
use bevy::utils::BoxedFuture;
#[derive(Default)]
struct UnreachableLoader;
impl AssetLoader for UnreachableLoader {
type Asset = Image;
type Error = std::io::Error;
type Settings = ();
fn load<'a>(
&'a self,
_reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext<'_>,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async { Ok(Image::default()) })
}
fn extensions(&self) -> &[&str] { &["unreachable"] }
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_asset_loader::<UnreachableLoader>()
.run();
}
The above PR also fixes my issue so I'm mainly posting this for posterity. |
# Objective - Fixes #11638 - See [here](#11638 (comment)) for details on the cause of this issue. ## Solution - Modified `AssetLoaders` to capture possibility of multiple `AssetLoader` registrations operating on the same `Asset` type, but different extensions. - Added an algorithm which will attempt to resolve via `AssetLoader` name, then `Asset` type, then by extension. If at any point multiple loaders fit a particular criteria, the next criteria is used as a tie breaker.
Bevy version
main, after #10153
What you did
With the following fork/branch of
bevy_common_assets
:https://github.com/rparrett/bevy_common_assets/tree/bevy13
cargo run --example multiple_formats --features=json,ron
What went wrong
The
ron
file is parsed asjson
and throws an error.The text was updated successfully, but these errors were encountered: