Skip to content

Commit 39ca46e

Browse files
committed
Use the TypePath trait for registering loaders and processors.
1 parent 1158855 commit 39ca46e

File tree

18 files changed

+60
-39
lines changed

18 files changed

+60
-39
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ mod tests {
762762
sub_texts: Vec<String>,
763763
}
764764

765-
#[derive(Default)]
765+
#[derive(Default, TypePath)]
766766
pub struct CoolTextLoader;
767767

768768
#[derive(Error, Debug)]
@@ -1855,6 +1855,7 @@ mod tests {
18551855
.init_asset::<SubText>()
18561856
.register_asset_loader(CoolTextLoader);
18571857

1858+
#[derive(TypePath)]
18581859
struct NestedLoadOfSubassetLoader;
18591860

18601861
impl AssetLoader for NestedLoadOfSubassetLoader {

crates/bevy_asset/src/loader.rs

Lines changed: 2 additions & 1 deletion
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`].

crates/bevy_asset/src/processor/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
mod log;
4141
mod process;
4242

43+
#[cfg(feature = "trace")]
44+
use bevy_reflect::TypePath;
4345
pub use log::*;
4446
pub use process::*;
4547

@@ -113,9 +115,9 @@ pub struct AssetProcessorData {
113115

114116
#[derive(Default)]
115117
struct Processors {
116-
/// Maps the type name of the processor to its instance.
117-
type_name_to_processor: HashMap<&'static str, Arc<dyn ErasedProcessor>>,
118-
/// Maps the file extension of an asset to the type name of the processor we should use to
118+
/// Maps the type path of the processor to its instance.
119+
type_path_to_processor: HashMap<&'static str, Arc<dyn ErasedProcessor>>,
120+
/// Maps the file extension of an asset to the type path of the processor we should use to
119121
/// process it by default.
120122
file_extension_to_default_processor: HashMap<Box<str>, &'static str>,
121123
}
@@ -599,8 +601,8 @@ impl AssetProcessor {
599601
#[cfg(feature = "trace")]
600602
let processor = InstrumentedAssetProcessor(processor);
601603
processors
602-
.type_name_to_processor
603-
.insert(core::any::type_name::<P>(), Arc::new(processor));
604+
.type_path_to_processor
605+
.insert(P::type_path(), Arc::new(processor));
604606
}
605607

606608
/// Set the default processor for the given `extension`. Make sure `P` is registered with [`AssetProcessor::register_processor`].
@@ -612,7 +614,7 @@ impl AssetProcessor {
612614
.unwrap_or_else(PoisonError::into_inner);
613615
processors
614616
.file_extension_to_default_processor
615-
.insert(extension.into(), core::any::type_name::<P>());
617+
.insert(extension.into(), P::type_path());
616618
}
617619

618620
/// Returns the default processor for the given `extension`, if it exists.
@@ -625,7 +627,7 @@ impl AssetProcessor {
625627
let key = processors
626628
.file_extension_to_default_processor
627629
.get(extension)?;
628-
processors.type_name_to_processor.get(key).cloned()
630+
processors.type_path_to_processor.get(key).cloned()
629631
}
630632

631633
/// Returns the processor with the given `processor_type_name`, if it exists.
@@ -636,7 +638,7 @@ impl AssetProcessor {
636638
.read()
637639
.unwrap_or_else(PoisonError::into_inner);
638640
processors
639-
.type_name_to_processor
641+
.type_path_to_processor
640642
.get(processor_type_name)
641643
.cloned()
642644
}
@@ -1172,6 +1174,7 @@ impl AssetProcessorData {
11721174
}
11731175

11741176
#[cfg(feature = "trace")]
1177+
#[derive(TypePath)]
11751178
struct InstrumentedAssetProcessor<T>(T);
11761179

11771180
#[cfg(feature = "trace")]
@@ -1195,7 +1198,7 @@ impl<T: Process> Process for InstrumentedAssetProcessor<T> {
11951198
};
11961199
let span = info_span!(
11971200
"asset processing",
1198-
processor = core::any::type_name::<T>(),
1201+
processor = T::type_path(),
11991202
asset = context.path().to_string(),
12001203
);
12011204
self.0.process(context, meta, writer).instrument(span)

crates/bevy_asset/src/processor/process.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use alloc::{
1515
boxed::Box,
1616
string::{String, ToString},
1717
};
18+
use bevy_reflect::TypePath;
1819
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
1920
use core::marker::PhantomData;
2021
use serde::{Deserialize, Serialize};
@@ -25,7 +26,7 @@ use thiserror::Error;
2526
///
2627
/// This is a "low level", maximally flexible interface. Most use cases are better served by the [`LoadTransformAndSave`] implementation
2728
/// of [`Process`].
28-
pub trait Process: Send + Sync + Sized + 'static {
29+
pub trait Process: TypePath + Send + Sync + Sized + 'static {
2930
/// The configuration / settings used to process the asset. This will be stored in the [`AssetMeta`] and is user-configurable per-asset.
3031
type Settings: Settings + Default + Serialize + for<'a> Deserialize<'a>;
3132
/// The [`AssetLoader`] that will be used to load the final processed asset.
@@ -59,6 +60,7 @@ pub trait Process: Send + Sync + Sized + 'static {
5960
/// This uses [`LoadTransformAndSaveSettings`] to configure the processor.
6061
///
6162
/// [`Asset`]: crate::Asset
63+
#[derive(TypePath)]
6264
pub struct LoadTransformAndSave<
6365
L: AssetLoader,
6466
T: AssetTransformer<AssetInput = L::Asset>,

crates/bevy_asset/src/saver.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
use alloc::boxed::Box;
66
use atomicow::CowArc;
77
use bevy_platform::collections::HashMap;
8+
use bevy_reflect::TypePath;
89
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
910
use core::{borrow::Borrow, hash::Hash, ops::Deref};
1011
use serde::{Deserialize, Serialize};
@@ -15,7 +16,7 @@ use serde::{Deserialize, Serialize};
1516
/// This trait is generally used in concert with [`AssetWriter`](crate::io::AssetWriter) to write assets as bytes.
1617
///
1718
/// For a complementary version of this trait that can load assets, see [`AssetLoader`].
18-
pub trait AssetSaver: Send + Sync + 'static {
19+
pub trait AssetSaver: TypePath + Send + Sync + 'static {
1920
/// The top level [`Asset`] saved by this [`AssetSaver`].
2021
type Asset: Asset;
2122
/// The settings type used by this [`AssetSaver`].

crates/bevy_asset/src/server/loaders.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::{
55
use alloc::{boxed::Box, sync::Arc, vec::Vec};
66
use async_broadcast::RecvError;
77
use bevy_platform::collections::HashMap;
8+
#[cfg(feature = "trace")]
9+
use bevy_reflect::TypePath;
810
use bevy_tasks::IoTaskPool;
911
use bevy_utils::TypeIdMap;
1012
use core::any::TypeId;
@@ -23,8 +25,8 @@ pub(crate) struct AssetLoaders {
2325
loaders: Vec<MaybeAssetLoader>,
2426
type_id_to_loaders: TypeIdMap<Vec<usize>>,
2527
extension_to_loaders: HashMap<Box<str>, Vec<usize>>,
26-
type_name_to_loader: HashMap<&'static str, usize>,
27-
preregistered_loaders: HashMap<&'static str, usize>,
28+
type_path_to_loader: HashMap<&'static str, usize>,
29+
type_path_to_preregistered_loader: HashMap<&'static str, usize>,
2830
}
2931

3032
impl AssetLoaders {
@@ -35,7 +37,7 @@ impl AssetLoaders {
3537

3638
/// Registers a new [`AssetLoader`]. [`AssetLoader`]s must be registered before they can be used.
3739
pub(crate) fn push<L: AssetLoader>(&mut self, loader: L) {
38-
let type_name = core::any::type_name::<L>();
40+
let type_path = L::type_path();
3941
let loader_asset_type = TypeId::of::<L::Asset>();
4042
let loader_asset_type_name = core::any::type_name::<L::Asset>();
4143

@@ -44,7 +46,7 @@ impl AssetLoaders {
4446
let loader = Arc::new(loader);
4547

4648
let (loader_index, is_new) =
47-
if let Some(index) = self.preregistered_loaders.remove(type_name) {
49+
if let Some(index) = self.type_path_to_preregistered_loader.remove(type_path) {
4850
(index, false)
4951
} else {
5052
(self.loaders.len(), true)
@@ -75,7 +77,7 @@ impl AssetLoaders {
7577
Loader must be specified in a .meta file in order to load assets of this type with these extensions.");
7678
}
7779

78-
self.type_name_to_loader.insert(type_name, loader_index);
80+
self.type_path_to_loader.insert(type_path, loader_index);
7981

8082
self.type_id_to_loaders
8183
.entry(loader_asset_type)
@@ -108,12 +110,13 @@ impl AssetLoaders {
108110
pub(crate) fn reserve<L: AssetLoader>(&mut self, extensions: &[&str]) {
109111
let loader_asset_type = TypeId::of::<L::Asset>();
110112
let loader_asset_type_name = core::any::type_name::<L::Asset>();
111-
let type_name = core::any::type_name::<L>();
113+
let type_path = L::type_path();
112114

113115
let loader_index = self.loaders.len();
114116

115-
self.preregistered_loaders.insert(type_name, loader_index);
116-
self.type_name_to_loader.insert(type_name, loader_index);
117+
self.type_path_to_preregistered_loader
118+
.insert(type_path, loader_index);
119+
self.type_path_to_loader.insert(type_path, loader_index);
117120

118121
let existing_loaders_for_type_id = self.type_id_to_loaders.get(&loader_asset_type);
119122
let mut duplicate_extensions = Vec::new();
@@ -152,7 +155,7 @@ impl AssetLoaders {
152155

153156
/// Get the [`AssetLoader`] by name
154157
pub(crate) fn get_by_name(&self, name: &str) -> Option<MaybeAssetLoader> {
155-
let index = self.type_name_to_loader.get(name).copied()?;
158+
let index = self.type_path_to_loader.get(name).copied()?;
156159

157160
self.get_by_index(index)
158161
}
@@ -309,6 +312,7 @@ impl MaybeAssetLoader {
309312
}
310313

311314
#[cfg(feature = "trace")]
315+
#[derive(TypePath)]
312316
struct InstrumentedAssetLoader<T>(T);
313317

314318
#[cfg(feature = "trace")]
@@ -361,6 +365,7 @@ mod tests {
361365
#[derive(Asset, TypePath, Debug)]
362366
struct C;
363367

368+
#[derive(TypePath)]
364369
struct Loader<A: Asset, const N: usize, const E: usize> {
365370
sender: Sender<()>,
366371
_phantom: PhantomData<A>,
@@ -430,7 +435,7 @@ mod tests {
430435

431436
let loader = block_on(
432437
loaders
433-
.get_by_name(core::any::type_name::<Loader<A, 1, 0>>())
438+
.get_by_name(Loader::<A, 1, 0>::type_path())
434439
.unwrap()
435440
.get(),
436441
)

crates/bevy_asset/src/transformer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{meta::Settings, Asset, ErasedLoadedAsset, Handle, LabeledAsset, Unty
22
use alloc::boxed::Box;
33
use atomicow::CowArc;
44
use bevy_platform::collections::HashMap;
5+
use bevy_reflect::TypePath;
56
use bevy_tasks::ConditionalSendFuture;
67
use core::{
78
borrow::Borrow,
@@ -15,7 +16,7 @@ use serde::{Deserialize, Serialize};
1516
/// Transforms an [`Asset`] of a given [`AssetTransformer::AssetInput`] type to an [`Asset`] of [`AssetTransformer::AssetOutput`] type.
1617
///
1718
/// This trait is commonly used in association with [`LoadTransformAndSave`](crate::processor::LoadTransformAndSave) to accomplish common asset pipeline workflows.
18-
pub trait AssetTransformer: Send + Sync + 'static {
19+
pub trait AssetTransformer: TypePath + Send + Sync + 'static {
1920
/// The [`Asset`] type which this [`AssetTransformer`] takes as and input.
2021
type AssetInput: Asset;
2122
/// The [`Asset`] type which this [`AssetTransformer`] outputs.
@@ -249,6 +250,7 @@ impl<'a, A: Asset> TransformedSubAsset<'a, A> {
249250
}
250251

251252
/// An identity [`AssetTransformer`] which infallibly returns the input [`Asset`] on transformation.]
253+
#[derive(TypePath)]
252254
pub struct IdentityAssetTransformer<A: Asset> {
253255
_phantom: PhantomData<fn(A) -> A>,
254256
}

crates/bevy_audio/src/audio_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl AsRef<[u8]> for AudioSource {
3434
/// `.mp3` with `bevy/mp3`
3535
/// `.flac` with `bevy/flac`
3636
/// `.wav` with `bevy/wav`
37-
#[derive(Default)]
37+
#[derive(Default, TypePath)]
3838
pub struct AudioLoader;
3939

4040
impl AssetLoader for AudioLoader {

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod extensions;
22
mod gltf_ext;
33

44
use alloc::sync::Arc;
5+
use bevy_reflect::TypePath;
56
use std::{
67
io::Error,
78
path::{Path, PathBuf},
@@ -136,6 +137,7 @@ pub enum GltfError {
136137
}
137138

138139
/// Loads glTF files with all of their data as their corresponding bevy representations.
140+
#[derive(TypePath)]
139141
pub struct GltfLoader {
140142
/// List of compressed image formats handled by the loader.
141143
pub supported_compressed_formats: CompressedImageFormats,

0 commit comments

Comments
 (0)