Skip to content

Commit d196050

Browse files
andriyDevcart
andauthored
Allow using short type names for asset processors. (#21339)
# Objective - Previously, asset processors needed to be **fully specified**. This meant writing met files by hand was incredibly cumbersome. - In addition, using `core::any::type_name` is generally undesirable since it is only intended for debugging. ## Solution - Require `TypePath` impl on `AssetLoader`, `AssetTransformer`, `AssetSaver`, and `AssetProcessor`. - Register loaders and processors by their `TypePath::type_path`. This makes the path format stable. - Additionally register the processors by their `TypePath::short_type_path`, and ensure there are no other processors with the same `short_type_path`. If a user tries to use an ambiguous short type path, we list out all full type paths. Note: I left doing this with asset loaders as a future PR. There's more complexity there (since we have to deal with pre-registering asset loaders), and this seems like the bigger "bang for our buck". ## Testing - I ran the asset_processing example. No change. - I changed the asset processing example to use the short type path. It still behaved as expected. - I created a new AssetTransformer in `asset_processing::sneaky::CoolTextTransformer` and a new processor that looked identical to the regular one (but using the sneaky transformer). It printed an error, listing out the full paths of both processors! --------- Co-authored-by: Carter Anderson <[email protected]>
1 parent f991d79 commit d196050

File tree

27 files changed

+487
-85
lines changed

27 files changed

+487
-85
lines changed

crates/bevy_animation/src/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bevy_ecs::{
1919
system::{Res, ResMut},
2020
};
2121
use bevy_platform::collections::HashMap;
22-
use bevy_reflect::{prelude::ReflectDefault, Reflect};
22+
use bevy_reflect::{prelude::ReflectDefault, Reflect, TypePath};
2323
use derive_more::derive::From;
2424
use petgraph::{
2525
graph::{DiGraph, NodeIndex},
@@ -238,7 +238,7 @@ pub enum AnimationNodeType {
238238
///
239239
/// The canonical extension for [`AnimationGraph`]s is `.animgraph.ron`. Plain
240240
/// `.animgraph` is supported as well.
241-
#[derive(Default)]
241+
#[derive(Default, TypePath)]
242242
pub struct AnimationGraphAssetLoader;
243243

244244
/// Errors that can occur when serializing animation graphs to RON.

crates/bevy_asset/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ mod tests {
769769
pub sub_texts: Vec<String>,
770770
}
771771

772-
#[derive(Default)]
772+
#[derive(Default, TypePath)]
773773
pub struct CoolTextLoader;
774774

775775
#[derive(Error, Debug)]
@@ -1934,6 +1934,7 @@ mod tests {
19341934
.init_asset::<SubText>()
19351935
.register_asset_loader(CoolTextLoader);
19361936

1937+
#[derive(TypePath)]
19371938
struct NestedLoadOfSubassetLoader;
19381939

19391940
impl AssetLoader for NestedLoadOfSubassetLoader {
@@ -1986,6 +1987,7 @@ mod tests {
19861987
// Extension "rsp" for Recursive Self Path (RSP).
19871988
dir.insert_asset_text(Path::new("abc.rsp"), "");
19881989

1990+
#[derive(TypePath)]
19891991
struct ImmediateSelfLoader;
19901992

19911993
impl AssetLoader for ImmediateSelfLoader {
@@ -2040,6 +2042,7 @@ mod tests {
20402042

20412043
dir.insert_asset_text(Path::new("abc.rsp"), "");
20422044

2045+
#[derive(TypePath)]
20432046
struct ImmediateSelfLoader;
20442047

20452048
impl AssetLoader for ImmediateSelfLoader {
@@ -2103,6 +2106,8 @@ mod tests {
21032106

21042107
#[derive(Asset, TypePath)]
21052108
pub struct TestAsset(Handle<TestAsset>);
2109+
2110+
#[derive(TypePath)]
21062111
struct DeferredSelfLoader;
21072112

21082113
impl AssetLoader for DeferredSelfLoader {
@@ -2186,6 +2191,7 @@ mod tests {
21862191

21872192
dir.insert_asset_text(Path::new("abc.rsp"), "");
21882193

2194+
#[derive(TypePath)]
21892195
struct ReadBytesSelfLoader;
21902196

21912197
impl AssetLoader for ReadBytesSelfLoader {
@@ -2270,6 +2276,8 @@ mod tests {
22702276

22712277
#[derive(Asset, TypePath)]
22722278
pub struct TestAssetUD(Handle<crate::LoadedUntypedAsset>);
2279+
2280+
#[derive(TypePath)]
22732281
struct ImmediateSelfLoader;
22742282

22752283
impl AssetLoader for ImmediateSelfLoader {
@@ -2531,6 +2539,7 @@ mod tests {
25312539
// Note: we can't just use the GatedReader, since currently we hold the handle until after
25322540
// we've selected the reader. The GatedReader blocks this process, so we need to wait until
25332541
// we gate in the loader instead.
2542+
#[derive(TypePath)]
25342543
struct GatedLoader {
25352544
in_loader_sender: Sender<()>,
25362545
gate_receiver: Receiver<()>,
@@ -2834,6 +2843,7 @@ mod tests {
28342843
#[derive(Serialize, Deserialize, Default)]
28352844
struct U8LoaderSettings(u8);
28362845

2846+
#[derive(TypePath)]
28372847
struct U8Loader;
28382848

28392849
impl AssetLoader for U8Loader {
@@ -2912,6 +2922,7 @@ mod tests {
29122922
let (mut app, dir) = create_app();
29132923
dir.insert_asset(Path::new("test.txt"), &[]);
29142924

2925+
#[derive(TypePath)]
29152926
struct TwoSubassetLoader;
29162927

29172928
impl AssetLoader for TwoSubassetLoader {
@@ -2952,6 +2963,7 @@ mod tests {
29522963
}
29532964

29542965
/// A loader that immediately returns a [`TestAsset`].
2966+
#[derive(TypePath)]
29552967
struct TrivialLoader;
29562968

29572969
impl AssetLoader for TrivialLoader {
@@ -3030,6 +3042,7 @@ mod tests {
30303042
#[derive(Asset, TypePath)]
30313043
struct DeferredNested(Handle<TestAsset>);
30323044

3045+
#[derive(TypePath)]
30333046
struct DeferredNestedLoader;
30343047

30353048
impl AssetLoader for DeferredNestedLoader {
@@ -3057,6 +3070,7 @@ mod tests {
30573070
#[derive(Asset, TypePath)]
30583071
struct ImmediateNested(Handle<TestAsset>);
30593072

3073+
#[derive(TypePath)]
30603074
struct ImmediateNestedLoader;
30613075

30623076
impl AssetLoader for ImmediateNestedLoader {

crates/bevy_asset/src/loader.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use alloc::{
1414
use atomicow::CowArc;
1515
use bevy_ecs::{error::BevyError, world::World};
1616
use bevy_platform::collections::{HashMap, HashSet};
17+
use bevy_reflect::TypePath;
1718
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
1819
use core::any::{Any, TypeId};
1920
use downcast_rs::{impl_downcast, Downcast};
@@ -28,7 +29,7 @@ use thiserror::Error;
2829
/// This trait is generally used in concert with [`AssetReader`](crate::io::AssetReader) to load assets from a byte source.
2930
///
3031
/// For a complementary version of this trait that can save assets, see [`AssetSaver`](crate::saver::AssetSaver).
31-
pub trait AssetLoader: Send + Sync + 'static {
32+
pub trait AssetLoader: TypePath + Send + Sync + 'static {
3233
/// The top level [`Asset`] loaded by this [`AssetLoader`].
3334
type Asset: Asset;
3435
/// The settings type used by this [`AssetLoader`].
@@ -66,8 +67,8 @@ pub trait ErasedAssetLoader: Send + Sync + 'static {
6667
fn deserialize_meta(&self, meta: &[u8]) -> Result<Box<dyn AssetMetaDyn>, DeserializeMetaError>;
6768
/// Returns the default meta value for the [`AssetLoader`] (erased as [`Box<dyn AssetMetaDyn>`]).
6869
fn default_meta(&self) -> Box<dyn AssetMetaDyn>;
69-
/// Returns the type name of the [`AssetLoader`].
70-
fn type_name(&self) -> &'static str;
70+
/// Returns the type path of the [`AssetLoader`].
71+
fn type_path(&self) -> &'static str;
7172
/// Returns the [`TypeId`] of the [`AssetLoader`].
7273
fn type_id(&self) -> TypeId;
7374
/// Returns the type name of the top-level [`Asset`] loaded by the [`AssetLoader`].
@@ -111,13 +112,13 @@ where
111112

112113
fn default_meta(&self) -> Box<dyn AssetMetaDyn> {
113114
Box::new(AssetMeta::<L, ()>::new(crate::meta::AssetAction::Load {
114-
loader: self.type_name().to_string(),
115+
loader: self.type_path().to_string(),
115116
settings: L::Settings::default(),
116117
}))
117118
}
118119

119-
fn type_name(&self) -> &'static str {
120-
core::any::type_name::<L>()
120+
fn type_path(&self) -> &'static str {
121+
L::type_path()
121122
}
122123

123124
fn type_id(&self) -> TypeId {

crates/bevy_asset/src/loader_builders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl NestedLoader<'_, '_, StaticTyped, Immediate<'_, '_>> {
503503
path,
504504
requested: TypeId::of::<A>(),
505505
actual_asset_name: loader.asset_type_name(),
506-
loader_name: loader.type_name(),
506+
loader_name: loader.type_path(),
507507
},
508508
})
509509
})

0 commit comments

Comments
 (0)