Skip to content

Commit

Permalink
Add more load_direct implementations (#13415)
Browse files Browse the repository at this point in the history
# Objective
- Introduce variants of `LoadContext::load_direct` which allow picking
asset type & configuring settings.
- Fixes #12963.

## Solution
- Implements `ErasedLoadedAsset::downcast` and adds some accessors to
`LoadedAsset<A>`.
- Changes `load_direct`/`load_direct_with_reader` to be typed, and
introduces `load_direct_untyped`/`load_direct_untyped_with_reader`.
- Introduces `load_direct_with_settings` and
`load_direct_with_reader_and_settings`.

## Testing
- I've run cargo test and played with the examples which use
`load_direct`.
- I also extended the `asset_processing` example to use the new typed
version of `load_direct` and use `load_direct_with_settings`.

---

## Changelog
- Introduced new `load_direct` methods in `LoadContext` to allow
specifying type & settings

## Migration Guide
- `LoadContext::load_direct` has been renamed to
`LoadContext::load_direct_untyped`. You may find the new `load_direct`
is more appropriate for your use case (and the migration may only be
moving one type parameter).
- `LoadContext::load_direct_with_reader` has been renamed to
`LoadContext::load_direct_untyped_with_reader`.

---

This might not be an obvious win as a solution because it introduces
quite a few new `load_direct` alternatives - but it does follow the
existing pattern pretty well. I'm very open to alternatives.
:sweat_smile:
  • Loading branch information
ricky26 authored May 21, 2024
1 parent 2857eb6 commit 26df1c1
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 83 deletions.
11 changes: 6 additions & 5 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,13 @@ mod tests {
let mut ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
let mut embedded = String::new();
for dep in ron.embedded_dependencies {
let loaded = load_context.load_direct(&dep).await.map_err(|_| {
Self::Error::CannotLoadDependency {
let loaded = load_context
.load_direct::<CoolText>(&dep)
.await
.map_err(|_| Self::Error::CannotLoadDependency {
dependency: dep.into(),
}
})?;
let cool = loaded.get::<CoolText>().unwrap();
})?;
let cool = loaded.get();
embedded.push_str(&cool.text);
}
Ok(CoolText {
Expand Down
302 changes: 245 additions & 57 deletions crates/bevy_asset/src/loader.rs

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions crates/bevy_asset/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,26 @@ impl AssetLoader for () {
}
}

pub(crate) fn meta_transform_settings<S: Settings>(
meta: &mut dyn AssetMetaDyn,
settings: &(impl Fn(&mut S) + Send + Sync + 'static),
) {
if let Some(loader_settings) = meta.loader_settings_mut() {
if let Some(loader_settings) = loader_settings.downcast_mut::<S>() {
settings(loader_settings);
} else {
error!(
"Configured settings type {} does not match AssetLoader settings type",
std::any::type_name::<S>(),
);
}
}
}

pub(crate) fn loader_settings_meta_transform<S: Settings>(
settings: impl Fn(&mut S) + Send + Sync + 'static,
) -> MetaTransform {
Box::new(move |meta| {
if let Some(loader_settings) = meta.loader_settings_mut() {
if let Some(loader_settings) = loader_settings.downcast_mut::<S>() {
settings(loader_settings);
} else {
error!(
"Configured settings type {} does not match AssetLoader settings type",
std::any::type_name::<S>(),
);
}
}
})
Box::new(move |meta| meta_transform_settings(meta, &settings))
}

pub type AssetHash = [u8; 32];
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/asset_decompression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl AssetLoader for GzAssetLoader {
let mut reader = VecReader::new(bytes_uncompressed);

let uncompressed = load_context
.load_direct_with_reader(&mut reader, contained_path)
.load_direct_untyped_with_reader(&mut reader, contained_path)
.await?;

Ok(GzAsset { uncompressed })
Expand Down
16 changes: 12 additions & 4 deletions examples/asset/processing/asset_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Text(String);
#[derive(Default)]
struct TextLoader;

#[derive(Default, Serialize, Deserialize)]
#[derive(Clone, Default, Serialize, Deserialize)]
struct TextSettings {
text_override: Option<String>,
}
Expand Down Expand Up @@ -107,6 +107,7 @@ struct CoolTextRon {
text: String,
dependencies: Vec<String>,
embedded_dependencies: Vec<String>,
dependencies_with_settings: Vec<(String, TextSettings)>,
}

#[derive(Asset, TypePath, Debug)]
Expand Down Expand Up @@ -145,9 +146,16 @@ impl AssetLoader for CoolTextLoader {
let ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
let mut base_text = ron.text;
for embedded in ron.embedded_dependencies {
let loaded = load_context.load_direct(&embedded).await?;
let text = loaded.get::<Text>().unwrap();
base_text.push_str(&text.0);
let loaded = load_context.load_direct::<Text>(&embedded).await?;
base_text.push_str(&loaded.get().0);
}
for (path, settings_override) in ron.dependencies_with_settings {
let loaded = load_context
.load_direct_with_settings::<Text, _>(&path, move |settings| {
*settings = settings_override.clone();
})
.await?;
base_text.push_str(&loaded.get().0);
}
Ok(CoolText {
text: base_text,
Expand Down
3 changes: 2 additions & 1 deletion examples/asset/processing/assets/a.cool.ron
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
"foo/c.cool.ron",
],
embedded_dependencies: [],
)
dependencies_with_settings: [],
)
5 changes: 4 additions & 1 deletion examples/asset/processing/assets/d.cool.ron
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
"foo/c.cool.ron",
"embedded://asset_processing/e.txt"
],
)
dependencies_with_settings: [
("embedded://asset_processing/e.txt", (text_override: Some("E"))),
],
)
3 changes: 2 additions & 1 deletion examples/asset/processing/assets/foo/b.cool.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
text: "b",
dependencies: [],
embedded_dependencies: [],
)
dependencies_with_settings: [],
)
3 changes: 2 additions & 1 deletion examples/asset/processing/assets/foo/c.cool.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
text: "c",
dependencies: [],
embedded_dependencies: ["a.cool.ron", "foo/b.cool.ron"],
)
dependencies_with_settings: [],
)

0 comments on commit 26df1c1

Please sign in to comment.