 35073cf7aa
			
		
	
	
		35073cf7aa
		
			
		
	
	
	
	
		
			
			This adds support for **Multiple Asset Sources**. You can now register a
named `AssetSource`, which you can load assets from like you normally
would:
```rust
let shader: Handle<Shader> = asset_server.load("custom_source://path/to/shader.wgsl");
```
Notice that `AssetPath` now supports `some_source://` syntax. This can
now be accessed through the `asset_path.source()` accessor.
Asset source names _are not required_. If one is not specified, the
default asset source will be used:
```rust
let shader: Handle<Shader> = asset_server.load("path/to/shader.wgsl");
```
The behavior of the default asset source has not changed. Ex: the
`assets` folder is still the default.
As referenced in #9714
## Why?
**Multiple Asset Sources** enables a number of often-asked-for
scenarios:
* **Loading some assets from other locations on disk**: you could create
a `config` asset source that reads from the OS-default config folder
(not implemented in this PR)
* **Loading some assets from a remote server**: you could register a new
`remote` asset source that reads some assets from a remote http server
(not implemented in this PR)
* **Improved "Binary Embedded" Assets**: we can use this system for
"embedded-in-binary assets", which allows us to replace the old
`load_internal_asset!` approach, which couldn't support asset
processing, didn't support hot-reloading _well_, and didn't make
embedded assets accessible to the `AssetServer` (implemented in this pr)
## Adding New Asset Sources
An `AssetSource` is "just" a collection of `AssetReader`, `AssetWriter`,
and `AssetWatcher` entries. You can configure new asset sources like
this:
```rust
app.register_asset_source(
    "other",
    AssetSource::build()
        .with_reader(|| Box::new(FileAssetReader::new("other")))
    )
)
```
Note that `AssetSource` construction _must_ be repeatable, which is why
a closure is accepted.
`AssetSourceBuilder` supports `with_reader`, `with_writer`,
`with_watcher`, `with_processed_reader`, `with_processed_writer`, and
`with_processed_watcher`.
Note that the "asset source" system replaces the old "asset providers"
system.
## Processing Multiple Sources
The `AssetProcessor` now supports multiple asset sources! Processed
assets can refer to assets in other sources and everything "just works".
Each `AssetSource` defines an unprocessed and processed `AssetReader` /
`AssetWriter`.
Currently this is all or nothing for a given `AssetSource`. A given
source is either processed or it is not. Later we might want to add
support for "lazy asset processing", where an `AssetSource` (such as a
remote server) can be configured to only process assets that are
directly referenced by local assets (in order to save local disk space
and avoid doing extra work).
## A new `AssetSource`: `embedded`
One of the big features motivating **Multiple Asset Sources** was
improving our "embedded-in-binary" asset loading. To prove out the
**Multiple Asset Sources** implementation, I chose to build a new
`embedded` `AssetSource`, which replaces the old `load_interal_asset!`
system.
The old `load_internal_asset!` approach had a number of issues:
* The `AssetServer` was not aware of (or capable of loading) internal
assets.
* Because internal assets weren't visible to the `AssetServer`, they
could not be processed (or used by assets that are processed). This
would prevent things "preprocessing shaders that depend on built in Bevy
shaders", which is something we desperately need to start doing.
* Each "internal asset" needed a UUID to be defined in-code to reference
it. This was very manual and toilsome.
The new `embedded` `AssetSource` enables the following pattern:
```rust
// Called in `crates/bevy_pbr/src/render/mesh.rs`
embedded_asset!(app, "mesh.wgsl");
// later in the app
let shader: Handle<Shader> = asset_server.load("embedded://bevy_pbr/render/mesh.wgsl");
```
Notice that this always treats the crate name as the "root path", and it
trims out the `src` path for brevity. This is generally predictable, but
if you need to debug you can use the new `embedded_path!` macro to get a
`PathBuf` that matches the one used by `embedded_asset`.
You can also reference embedded assets in arbitrary assets, such as WGSL
shaders:
```rust
#import "embedded://bevy_pbr/render/mesh.wgsl"
```
This also makes `embedded` assets go through the "normal" asset
lifecycle. They are only loaded when they are actually used!
We are also discussing implicitly converting asset paths to/from shader
modules, so in the future (not in this PR) you might be able to load it
like this:
```rust
#import bevy_pbr::render::mesh::Vertex
```
Compare that to the old system!
```rust
pub const MESH_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(3252377289100772450);
load_internal_asset!(app, MESH_SHADER_HANDLE, "mesh.wgsl", Shader::from_wgsl);
// The mesh asset is the _only_ accessible via MESH_SHADER_HANDLE and _cannot_ be loaded via the AssetServer.
```
## Hot Reloading `embedded`
You can enable `embedded` hot reloading by enabling the
`embedded_watcher` cargo feature:
```
cargo run --features=embedded_watcher
```
## Improved Hot Reloading Workflow
First: the `filesystem_watcher` cargo feature has been renamed to
`file_watcher` for brevity (and to match the `FileAssetReader` naming
convention).
More importantly, hot asset reloading is no longer configured in-code by
default. If you enable any asset watcher feature (such as `file_watcher`
or `rust_source_watcher`), asset watching will be automatically enabled.
This removes the need to _also_ enable hot reloading in your app code.
That means you can replace this:
```rust
app.add_plugins(DefaultPlugins.set(AssetPlugin::default().watch_for_changes()))
```
with this:
```rust
app.add_plugins(DefaultPlugins)
```
If you want to hot reload assets in your app during development, just
run your app like this:
```
cargo run --features=file_watcher
```
This means you can use the same code for development and deployment! To
deploy an app, just don't include the watcher feature
```
cargo build --release
```
My intent is to move to this approach for pretty much all dev workflows.
In a future PR I would like to replace `AssetMode::ProcessedDev` with a
`runtime-processor` cargo feature. We could then group all common "dev"
cargo features under a single `dev` feature:
```sh
# this would enable file_watcher, embedded_watcher, runtime-processor, and more
cargo run --features=dev
```
## AssetMode
`AssetPlugin::Unprocessed`, `AssetPlugin::Processed`, and
`AssetPlugin::ProcessedDev` have been replaced with an `AssetMode` field
on `AssetPlugin`.
```rust
// before 
app.add_plugins(DefaultPlugins.set(AssetPlugin::Processed { /* fields here */ })
// after 
app.add_plugins(DefaultPlugins.set(AssetPlugin { mode: AssetMode::Processed, ..default() })
```
This aligns `AssetPlugin` with our other struct-like plugins. The old
"source" and "destination" `AssetProvider` fields in the enum variants
have been replaced by the "asset source" system. You no longer need to
configure the AssetPlugin to "point" to custom asset providers.
## AssetServerMode
To improve the implementation of **Multiple Asset Sources**,
`AssetServer` was made aware of whether or not it is using "processed"
or "unprocessed" assets. You can check that like this:
```rust
if asset_server.mode() == AssetServerMode::Processed {
    /* do something */
}
```
Note that this refactor should also prepare the way for building "one to
many processed output files", as it makes the server aware of whether it
is loading from processed or unprocessed sources. Meaning we can store
and read processed and unprocessed assets differently!
## AssetPath can now refer to folders
The "file only" restriction has been removed from `AssetPath`. The
`AssetServer::load_folder` API now accepts an `AssetPath` instead of a
`Path`, meaning you can load folders from other asset sources!
## Improved AssetPath Parsing
AssetPath parsing was reworked to support sources, improve error
messages, and to enable parsing with a single pass over the string.
`AssetPath::new` was replaced by `AssetPath::parse` and
`AssetPath::try_parse`.
## AssetWatcher broken out from AssetReader
`AssetReader` is no longer responsible for constructing `AssetWatcher`.
This has been moved to `AssetSourceBuilder`.
## Duplicate Event Debouncing
Asset V2 already debounced duplicate filesystem events, but this was
_input_ events. Multiple input event types can produce the same _output_
`AssetSourceEvent`. Now that we have `embedded_watcher`, which does
expensive file io on events, it made sense to debounce output events
too, so I added that! This will also benefit the AssetProcessor by
preventing integrity checks for duplicate events (and helps keep the
noise down in trace logs).
## Next Steps
* **Port Built-in Shaders**: Currently the primary (and essentially
only) user of `load_interal_asset` in Bevy's source code is "built-in
shaders". I chose not to do that in this PR for a few reasons:
1. We need to add the ability to pass shader defs in to shaders via meta
files. Some shaders (such as MESH_VIEW_TYPES) need to pass shader def
values in that are defined in code.
2. We need to revisit the current shader module naming system. I think
we _probably_ want to imply modules from source structure (at least by
default). Ideally in a way that can losslessly convert asset paths
to/from shader modules (to enable the asset system to resolve modules
using the asset server).
  3. I want to keep this change set minimal / get this merged first.
* **Deprecate `load_internal_asset`**: we can't do that until we do (1)
and (2)
* **Relative Asset Paths**: This PR significantly increases the need for
relative asset paths (which was already pretty high). Currently when
loading dependencies, it is assumed to be an absolute path, which means
if in an `AssetLoader` you call `context.load("some/path/image.png")` it
will assume that is the "default" asset source, _even if the current
asset is in a different asset source_. This will cause breakage for
AssetLoaders that are not designed to add the current source to whatever
paths are being used. AssetLoaders should generally not need to be aware
of the name of their current asset source, or need to think about the
"current asset source" generally. We should build apis that support
relative asset paths and then encourage using relative paths as much as
possible (both via api design and docs). Relative paths are also
important because they will allow developers to move folders around
(even across providers) without reprocessing, provided there is no path
breakage.
		
	
			
		
			
				
	
	
		
			561 lines
		
	
	
		
			22 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			561 lines
		
	
	
		
			22 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use crate::{
 | |
|     io::{AssetReaderError, MissingAssetSourceError, MissingProcessedAssetReaderError, Reader},
 | |
|     meta::{
 | |
|         loader_settings_meta_transform, AssetHash, AssetMeta, AssetMetaDyn, ProcessedInfoMinimal,
 | |
|         Settings,
 | |
|     },
 | |
|     path::AssetPath,
 | |
|     Asset, AssetLoadError, AssetServer, AssetServerMode, Assets, Handle, UntypedAssetId,
 | |
|     UntypedHandle,
 | |
| };
 | |
| use bevy_ecs::world::World;
 | |
| use bevy_utils::{BoxedFuture, CowArc, HashMap, HashSet};
 | |
| use downcast_rs::{impl_downcast, Downcast};
 | |
| use futures_lite::AsyncReadExt;
 | |
| use ron::error::SpannedError;
 | |
| use serde::{Deserialize, Serialize};
 | |
| use std::{
 | |
|     any::{Any, TypeId},
 | |
|     path::Path,
 | |
| };
 | |
| use thiserror::Error;
 | |
| 
 | |
| /// Loads an [`Asset`] from a given byte [`Reader`]. This can accept [`AssetLoader::Settings`], which configure how the [`Asset`]
 | |
| /// should be loaded.
 | |
| pub trait AssetLoader: Send + Sync + 'static {
 | |
|     /// The top level [`Asset`] loaded by this [`AssetLoader`].
 | |
|     type Asset: crate::Asset;
 | |
|     /// The settings type used by this [`AssetLoader`].
 | |
|     type Settings: Settings + Default + Serialize + for<'a> Deserialize<'a>;
 | |
|     /// The type of [error](`std::error::Error`) which could be encountered by this loader.
 | |
|     type Error: std::error::Error + Send + Sync + 'static;
 | |
|     /// Asynchronously loads [`AssetLoader::Asset`] (and any other labeled assets) from the bytes provided by [`Reader`].
 | |
|     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>>;
 | |
| 
 | |
|     /// Returns a list of extensions supported by this asset loader, without the preceding dot.
 | |
|     fn extensions(&self) -> &[&str];
 | |
| }
 | |
| 
 | |
| /// Provides type-erased access to an [`AssetLoader`].
 | |
| pub trait ErasedAssetLoader: Send + Sync + 'static {
 | |
|     /// Asynchronously loads the asset(s) from the bytes provided by [`Reader`].
 | |
|     fn load<'a>(
 | |
|         &'a self,
 | |
|         reader: &'a mut Reader,
 | |
|         meta: Box<dyn AssetMetaDyn>,
 | |
|         load_context: LoadContext<'a>,
 | |
|     ) -> BoxedFuture<
 | |
|         'a,
 | |
|         Result<ErasedLoadedAsset, Box<dyn std::error::Error + Send + Sync + 'static>>,
 | |
|     >;
 | |
| 
 | |
|     /// Returns a list of extensions supported by this asset loader, without the preceding dot.
 | |
|     fn extensions(&self) -> &[&str];
 | |
|     /// Deserializes metadata from the input `meta` bytes into the appropriate type (erased as [`Box<dyn AssetMetaDyn>`]).
 | |
|     fn deserialize_meta(&self, meta: &[u8]) -> Result<Box<dyn AssetMetaDyn>, DeserializeMetaError>;
 | |
|     /// Returns the default meta value for the [`AssetLoader`] (erased as [`Box<dyn AssetMetaDyn>`]).
 | |
|     fn default_meta(&self) -> Box<dyn AssetMetaDyn>;
 | |
|     /// Returns the type name of the [`AssetLoader`].
 | |
|     fn type_name(&self) -> &'static str;
 | |
|     /// Returns the [`TypeId`] of the [`AssetLoader`].
 | |
|     fn type_id(&self) -> TypeId;
 | |
|     /// Returns the type name of the top-level [`Asset`] loaded by the [`AssetLoader`].
 | |
|     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;
 | |
| }
 | |
| 
 | |
| impl<L> ErasedAssetLoader for L
 | |
| where
 | |
|     L: AssetLoader + Send + Sync,
 | |
| {
 | |
|     /// Processes the asset in an asynchronous closure.
 | |
|     fn load<'a>(
 | |
|         &'a self,
 | |
|         reader: &'a mut Reader,
 | |
|         meta: Box<dyn AssetMetaDyn>,
 | |
|         mut load_context: LoadContext<'a>,
 | |
|     ) -> BoxedFuture<
 | |
|         'a,
 | |
|         Result<ErasedLoadedAsset, Box<dyn std::error::Error + Send + Sync + 'static>>,
 | |
|     > {
 | |
|         Box::pin(async move {
 | |
|             let settings = meta
 | |
|                 .loader_settings()
 | |
|                 .expect("Loader settings should exist")
 | |
|                 .downcast_ref::<L::Settings>()
 | |
|                 .expect("AssetLoader settings should match the loader type");
 | |
|             let asset = <L as AssetLoader>::load(self, reader, settings, &mut load_context).await?;
 | |
|             Ok(load_context.finish(asset, Some(meta)).into())
 | |
|         })
 | |
|     }
 | |
| 
 | |
|     fn deserialize_meta(&self, meta: &[u8]) -> Result<Box<dyn AssetMetaDyn>, DeserializeMetaError> {
 | |
|         let meta = AssetMeta::<L, ()>::deserialize(meta)?;
 | |
|         Ok(Box::new(meta))
 | |
|     }
 | |
| 
 | |
|     fn default_meta(&self) -> Box<dyn AssetMetaDyn> {
 | |
|         Box::new(AssetMeta::<L, ()>::new(crate::meta::AssetAction::Load {
 | |
|             loader: self.type_name().to_string(),
 | |
|             settings: L::Settings::default(),
 | |
|         }))
 | |
|     }
 | |
| 
 | |
|     fn extensions(&self) -> &[&str] {
 | |
|         <L as AssetLoader>::extensions(self)
 | |
|     }
 | |
| 
 | |
|     fn type_name(&self) -> &'static str {
 | |
|         std::any::type_name::<L>()
 | |
|     }
 | |
| 
 | |
|     fn type_id(&self) -> TypeId {
 | |
|         TypeId::of::<L>()
 | |
|     }
 | |
| 
 | |
|     fn asset_type_id(&self) -> TypeId {
 | |
|         TypeId::of::<L::Asset>()
 | |
|     }
 | |
| 
 | |
|     fn asset_type_name(&self) -> &'static str {
 | |
|         std::any::type_name::<L::Asset>()
 | |
|     }
 | |
| }
 | |
| 
 | |
| pub(crate) struct LabeledAsset {
 | |
|     pub(crate) asset: ErasedLoadedAsset,
 | |
|     pub(crate) handle: UntypedHandle,
 | |
| }
 | |
| 
 | |
| /// The successful result of an [`AssetLoader::load`] call. This contains the loaded "root" asset and any other "labeled" assets produced
 | |
| /// by the loader. It also holds the input [`AssetMeta`] (if it exists) and tracks dependencies:
 | |
| /// * normal dependencies: dependencies that must be loaded as part of this asset load (ex: assets a given asset has handles to).
 | |
| /// * Loader dependencies: dependencies whose actual asset values are used during the load process
 | |
| pub struct LoadedAsset<A: Asset> {
 | |
|     pub(crate) value: A,
 | |
|     pub(crate) dependencies: HashSet<UntypedAssetId>,
 | |
|     pub(crate) loader_dependencies: HashMap<AssetPath<'static>, AssetHash>,
 | |
|     pub(crate) labeled_assets: HashMap<CowArc<'static, str>, LabeledAsset>,
 | |
|     pub(crate) meta: Option<Box<dyn AssetMetaDyn>>,
 | |
| }
 | |
| 
 | |
| impl<A: Asset> LoadedAsset<A> {
 | |
|     /// Create a new loaded asset. This will use [`VisitAssetDependencies`](crate::VisitAssetDependencies) to populate `dependencies`.
 | |
|     pub fn new_with_dependencies(value: A, meta: Option<Box<dyn AssetMetaDyn>>) -> Self {
 | |
|         let mut dependencies = HashSet::new();
 | |
|         value.visit_dependencies(&mut |id| {
 | |
|             dependencies.insert(id);
 | |
|         });
 | |
|         LoadedAsset {
 | |
|             value,
 | |
|             dependencies,
 | |
|             loader_dependencies: HashMap::default(),
 | |
|             labeled_assets: HashMap::default(),
 | |
|             meta,
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl<A: Asset> From<A> for LoadedAsset<A> {
 | |
|     fn from(asset: A) -> Self {
 | |
|         LoadedAsset::new_with_dependencies(asset, None)
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// A "type erased / boxed" counterpart to [`LoadedAsset`]. This is used in places where the loaded type is not statically known.
 | |
| pub struct ErasedLoadedAsset {
 | |
|     pub(crate) value: Box<dyn AssetContainer>,
 | |
|     pub(crate) dependencies: HashSet<UntypedAssetId>,
 | |
|     pub(crate) loader_dependencies: HashMap<AssetPath<'static>, AssetHash>,
 | |
|     pub(crate) labeled_assets: HashMap<CowArc<'static, str>, LabeledAsset>,
 | |
|     pub(crate) meta: Option<Box<dyn AssetMetaDyn>>,
 | |
| }
 | |
| 
 | |
| impl<A: Asset> From<LoadedAsset<A>> for ErasedLoadedAsset {
 | |
|     fn from(asset: LoadedAsset<A>) -> Self {
 | |
|         ErasedLoadedAsset {
 | |
|             value: Box::new(asset.value),
 | |
|             dependencies: asset.dependencies,
 | |
|             loader_dependencies: asset.loader_dependencies,
 | |
|             labeled_assets: asset.labeled_assets,
 | |
|             meta: asset.meta,
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl ErasedLoadedAsset {
 | |
|     /// Cast (and take ownership) of the [`Asset`] value of the given type. This will return [`Some`] if
 | |
|     /// the stored type matches `A` and [`None`] if it does not.
 | |
|     pub fn take<A: Asset>(self) -> Option<A> {
 | |
|         self.value.downcast::<A>().map(|a| *a).ok()
 | |
|     }
 | |
| 
 | |
|     /// Retrieves a reference to the internal [`Asset`] type, if it matches the type `A`. Otherwise returns [`None`].
 | |
|     pub fn get<A: Asset>(&self) -> Option<&A> {
 | |
|         self.value.downcast_ref::<A>()
 | |
|     }
 | |
| 
 | |
|     /// Retrieves the [`TypeId`] of the stored [`Asset`] type.
 | |
|     pub fn asset_type_id(&self) -> TypeId {
 | |
|         (*self.value).type_id()
 | |
|     }
 | |
| 
 | |
|     /// Retrieves the `type_name` of the stored [`Asset`] type.
 | |
|     pub fn asset_type_name(&self) -> &'static str {
 | |
|         self.value.asset_type_name()
 | |
|     }
 | |
| 
 | |
|     /// Returns the [`ErasedLoadedAsset`] for the given label, if it exists.
 | |
|     pub fn get_labeled(
 | |
|         &self,
 | |
|         label: impl Into<CowArc<'static, str>>,
 | |
|     ) -> Option<&ErasedLoadedAsset> {
 | |
|         self.labeled_assets.get(&label.into()).map(|a| &a.asset)
 | |
|     }
 | |
| 
 | |
|     /// Iterate over all labels for "labeled assets" in the loaded asset
 | |
|     pub fn iter_labels(&self) -> impl Iterator<Item = &str> {
 | |
|         self.labeled_assets.keys().map(|s| &**s)
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// A type erased container for an [`Asset`] value that is capable of inserting the [`Asset`] into a [`World`]'s [`Assets`] collection.
 | |
| pub trait AssetContainer: Downcast + Any + Send + Sync + 'static {
 | |
|     fn insert(self: Box<Self>, id: UntypedAssetId, world: &mut World);
 | |
|     fn asset_type_name(&self) -> &'static str;
 | |
| }
 | |
| 
 | |
| impl_downcast!(AssetContainer);
 | |
| 
 | |
| impl<A: Asset> AssetContainer for A {
 | |
|     fn insert(self: Box<Self>, id: UntypedAssetId, world: &mut World) {
 | |
|         world.resource_mut::<Assets<A>>().insert(id.typed(), *self);
 | |
|     }
 | |
| 
 | |
|     fn asset_type_name(&self) -> &'static str {
 | |
|         std::any::type_name::<A>()
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// An error that occurs when attempting to call [`LoadContext::load_direct`]
 | |
| #[derive(Error, Debug)]
 | |
| #[error("Failed to load dependency {dependency:?} {error}")]
 | |
| pub struct LoadDirectError {
 | |
|     pub dependency: AssetPath<'static>,
 | |
|     pub error: AssetLoadError,
 | |
| }
 | |
| 
 | |
| /// An error that occurs while deserializing [`AssetMeta`].
 | |
| #[derive(Error, Debug)]
 | |
| pub enum DeserializeMetaError {
 | |
|     #[error("Failed to deserialize asset meta: {0:?}")]
 | |
|     DeserializeSettings(#[from] SpannedError),
 | |
|     #[error("Failed to deserialize minimal asset meta: {0:?}")]
 | |
|     DeserializeMinimal(SpannedError),
 | |
| }
 | |
| 
 | |
| /// A context that provides access to assets in [`AssetLoader`]s, tracks dependencies, and collects asset load state.
 | |
| /// Any asset state accessed by [`LoadContext`] will be tracked and stored for use in dependency events and asset preprocessing.
 | |
| pub struct LoadContext<'a> {
 | |
|     asset_server: &'a AssetServer,
 | |
|     should_load_dependencies: bool,
 | |
|     populate_hashes: bool,
 | |
|     asset_path: AssetPath<'static>,
 | |
|     dependencies: HashSet<UntypedAssetId>,
 | |
|     /// Direct dependencies used by this loader.
 | |
|     loader_dependencies: HashMap<AssetPath<'static>, AssetHash>,
 | |
|     labeled_assets: HashMap<CowArc<'static, str>, LabeledAsset>,
 | |
| }
 | |
| 
 | |
| impl<'a> LoadContext<'a> {
 | |
|     /// Creates a new [`LoadContext`] instance.
 | |
|     pub(crate) fn new(
 | |
|         asset_server: &'a AssetServer,
 | |
|         asset_path: AssetPath<'static>,
 | |
|         should_load_dependencies: bool,
 | |
|         populate_hashes: bool,
 | |
|     ) -> Self {
 | |
|         Self {
 | |
|             asset_server,
 | |
|             asset_path,
 | |
|             populate_hashes,
 | |
|             should_load_dependencies,
 | |
|             dependencies: HashSet::default(),
 | |
|             loader_dependencies: HashMap::default(),
 | |
|             labeled_assets: HashMap::default(),
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Begins a new labeled asset load. Use the returned [`LoadContext`] to load
 | |
|     /// dependencies for the new asset and call [`LoadContext::finish`] to finalize the asset load.
 | |
|     /// When finished, make sure you call [`LoadContext::add_labeled_asset`] to add the results back to the parent
 | |
|     /// context.
 | |
|     /// Prefer [`LoadContext::labeled_asset_scope`] when possible, which will automatically add
 | |
|     /// the labeled [`LoadContext`] back to the parent context.
 | |
|     /// [`LoadContext::begin_labeled_asset`] exists largely to enable parallel asset loading.
 | |
|     ///
 | |
|     /// See [`AssetPath`] for more on labeled assets.
 | |
|     ///
 | |
|     /// ```no_run
 | |
|     /// # use bevy_asset::{Asset, LoadContext};
 | |
|     /// # use bevy_reflect::TypePath;
 | |
|     /// # #[derive(Asset, TypePath, Default)]
 | |
|     /// # struct Image;
 | |
|     /// # let load_context: LoadContext = panic!();
 | |
|     /// let mut handles = Vec::new();
 | |
|     /// for i in 0..2 {
 | |
|     ///     let mut labeled = load_context.begin_labeled_asset();
 | |
|     ///     handles.push(std::thread::spawn(move || {
 | |
|     ///         (i.to_string(), labeled.finish(Image::default(), None))
 | |
|     ///     }));
 | |
|     /// }
 | |
| 
 | |
|     /// for handle in handles {
 | |
|     ///     let (label, loaded_asset) = handle.join().unwrap();
 | |
|     ///     load_context.add_loaded_labeled_asset(label, loaded_asset);
 | |
|     /// }
 | |
|     /// ```
 | |
|     pub fn begin_labeled_asset(&self) -> LoadContext {
 | |
|         LoadContext::new(
 | |
|             self.asset_server,
 | |
|             self.asset_path.clone(),
 | |
|             self.should_load_dependencies,
 | |
|             self.populate_hashes,
 | |
|         )
 | |
|     }
 | |
| 
 | |
|     /// Creates a new [`LoadContext`] for the given `label`. The `load` function is responsible for loading an [`Asset`] of
 | |
|     /// type `A`. `load` will be called immediately and the result will be used to finalize the [`LoadContext`], resulting in a new
 | |
|     /// [`LoadedAsset`], which is registered under the `label` label.
 | |
|     ///
 | |
|     /// This exists to remove the need to manually call [`LoadContext::begin_labeled_asset`] and then manually register the
 | |
|     /// result with [`LoadContext::add_labeled_asset`].
 | |
|     ///
 | |
|     /// See [`AssetPath`] for more on labeled assets.
 | |
|     pub fn labeled_asset_scope<A: Asset>(
 | |
|         &mut self,
 | |
|         label: String,
 | |
|         load: impl FnOnce(&mut LoadContext) -> A,
 | |
|     ) -> Handle<A> {
 | |
|         let mut context = self.begin_labeled_asset();
 | |
|         let asset = load(&mut context);
 | |
|         let loaded_asset = context.finish(asset, None);
 | |
|         self.add_loaded_labeled_asset(label, loaded_asset)
 | |
|     }
 | |
| 
 | |
|     /// This will add the given `asset` as a "labeled [`Asset`]" with the `label` label.
 | |
|     ///
 | |
|     /// See [`AssetPath`] for more on labeled assets.
 | |
|     pub fn add_labeled_asset<A: Asset>(&mut self, label: String, asset: A) -> Handle<A> {
 | |
|         self.labeled_asset_scope(label, |_| asset)
 | |
|     }
 | |
| 
 | |
|     /// Add a [`LoadedAsset`] that is a "labeled sub asset" of the root path of this load context.
 | |
|     /// This can be used in combination with [`LoadContext::begin_labeled_asset`] to parallelize
 | |
|     /// sub asset loading.
 | |
|     ///
 | |
|     /// See [`AssetPath`] for more on labeled assets.
 | |
|     pub fn add_loaded_labeled_asset<A: Asset>(
 | |
|         &mut self,
 | |
|         label: impl Into<CowArc<'static, str>>,
 | |
|         loaded_asset: LoadedAsset<A>,
 | |
|     ) -> Handle<A> {
 | |
|         let label = label.into();
 | |
|         let loaded_asset: ErasedLoadedAsset = loaded_asset.into();
 | |
|         let labeled_path = self.asset_path.clone().with_label(label.clone());
 | |
|         let handle = self
 | |
|             .asset_server
 | |
|             .get_or_create_path_handle(labeled_path, None);
 | |
|         self.labeled_assets.insert(
 | |
|             label,
 | |
|             LabeledAsset {
 | |
|                 asset: loaded_asset,
 | |
|                 handle: handle.clone().untyped(),
 | |
|             },
 | |
|         );
 | |
|         handle
 | |
|     }
 | |
| 
 | |
|     /// Returns `true` if an asset with the label `label` exists in this context.
 | |
|     ///
 | |
|     /// See [`AssetPath`] for more on labeled assets.
 | |
|     pub fn has_labeled_asset<'b>(&self, label: impl Into<CowArc<'b, str>>) -> bool {
 | |
|         let path = self.asset_path.clone().with_label(label.into());
 | |
|         self.asset_server.get_handle_untyped(&path).is_some()
 | |
|     }
 | |
| 
 | |
|     /// "Finishes" this context by populating the final [`Asset`] value (and the erased [`AssetMeta`] value, if it exists).
 | |
|     /// The relevant asset metadata collected in this context will be stored in the returned [`LoadedAsset`].
 | |
|     pub fn finish<A: Asset>(self, value: A, meta: Option<Box<dyn AssetMetaDyn>>) -> LoadedAsset<A> {
 | |
|         LoadedAsset {
 | |
|             value,
 | |
|             dependencies: self.dependencies,
 | |
|             loader_dependencies: self.loader_dependencies,
 | |
|             labeled_assets: self.labeled_assets,
 | |
|             meta,
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /// Gets the source path for this load context.
 | |
|     pub fn path(&self) -> &Path {
 | |
|         self.asset_path.path()
 | |
|     }
 | |
| 
 | |
|     /// Gets the source asset path for this load context.
 | |
|     pub fn asset_path(&self) -> &AssetPath<'static> {
 | |
|         &self.asset_path
 | |
|     }
 | |
| 
 | |
|     /// Gets the source asset path for this load context.
 | |
|     pub async fn read_asset_bytes<'b, 'c>(
 | |
|         &'b mut self,
 | |
|         path: impl Into<AssetPath<'c>>,
 | |
|     ) -> Result<Vec<u8>, ReadAssetBytesError> {
 | |
|         let path = path.into();
 | |
|         let source = self.asset_server.get_source(path.source())?;
 | |
|         let asset_reader = match self.asset_server.mode() {
 | |
|             AssetServerMode::Unprocessed { .. } => source.reader(),
 | |
|             AssetServerMode::Processed { .. } => source.processed_reader()?,
 | |
|         };
 | |
|         let mut reader = asset_reader.read(path.path()).await?;
 | |
|         let hash = if self.populate_hashes {
 | |
|             // NOTE: ensure meta is read while the asset bytes reader is still active to ensure transactionality
 | |
|             // See `ProcessorGatedReader` for more info
 | |
|             let meta_bytes = asset_reader.read_meta_bytes(path.path()).await?;
 | |
|             let minimal: ProcessedInfoMinimal = ron::de::from_bytes(&meta_bytes)
 | |
|                 .map_err(DeserializeMetaError::DeserializeMinimal)?;
 | |
|             let processed_info = minimal
 | |
|                 .processed_info
 | |
|                 .ok_or(ReadAssetBytesError::MissingAssetHash)?;
 | |
|             processed_info.full_hash
 | |
|         } else {
 | |
|             Default::default()
 | |
|         };
 | |
|         let mut bytes = Vec::new();
 | |
|         reader.read_to_end(&mut bytes).await?;
 | |
|         self.loader_dependencies.insert(path.clone_owned(), hash);
 | |
|         Ok(bytes)
 | |
|     }
 | |
| 
 | |
|     /// Retrieves a handle for the asset at the given path and adds that path as a dependency of the asset.
 | |
|     /// If the current context is a normal [`AssetServer::load`], an actual asset load will be kicked off immediately, which ensures the load happens
 | |
|     /// as soon as possible.
 | |
|     /// "Normal loads" kicked from within a normal Bevy App will generally configure the context to kick off loads immediately.  
 | |
|     /// If the current context is configured to not load dependencies automatically (ex: [`AssetProcessor`](crate::processor::AssetProcessor)),
 | |
|     /// a load will not be kicked off automatically. It is then the calling context's responsibility to begin a load if necessary.
 | |
|     pub fn load<'b, A: Asset>(&mut self, path: impl Into<AssetPath<'b>>) -> Handle<A> {
 | |
|         let path = path.into().to_owned();
 | |
|         let handle = if self.should_load_dependencies {
 | |
|             self.asset_server.load(path)
 | |
|         } else {
 | |
|             self.asset_server.get_or_create_path_handle(path, None)
 | |
|         };
 | |
|         self.dependencies.insert(handle.id().untyped());
 | |
|         handle
 | |
|     }
 | |
| 
 | |
|     /// Loads the [`Asset`] of type `A` at the given `path` with the given [`AssetLoader::Settings`] settings `S`. This is a "deferred"
 | |
|     /// load. If the settings type `S` does not match the settings expected by `A`'s asset loader, an error will be printed to the log
 | |
|     /// and the asset load will fail.  
 | |
|     pub fn load_with_settings<'b, A: Asset, S: Settings + Default>(
 | |
|         &mut self,
 | |
|         path: impl Into<AssetPath<'b>>,
 | |
|         settings: impl Fn(&mut S) + Send + Sync + 'static,
 | |
|     ) -> Handle<A> {
 | |
|         let path = path.into();
 | |
|         let handle = if self.should_load_dependencies {
 | |
|             self.asset_server.load_with_settings(path.clone(), settings)
 | |
|         } else {
 | |
|             self.asset_server
 | |
|                 .get_or_create_path_handle(path, Some(loader_settings_meta_transform(settings)))
 | |
|         };
 | |
|         self.dependencies.insert(handle.id().untyped());
 | |
|         handle
 | |
|     }
 | |
| 
 | |
|     /// Returns a handle to an asset of type `A` with the label `label`. This [`LoadContext`] must produce an asset of the
 | |
|     /// given type and the given label or the dependencies of this asset will never be considered "fully loaded". However you
 | |
|     /// can call this method before _or_ after adding the labeled asset.
 | |
|     pub fn get_label_handle<'b, A: Asset>(
 | |
|         &mut self,
 | |
|         label: impl Into<CowArc<'b, str>>,
 | |
|     ) -> Handle<A> {
 | |
|         let path = self.asset_path.clone().with_label(label);
 | |
|         let handle = self.asset_server.get_or_create_path_handle::<A>(path, None);
 | |
|         self.dependencies.insert(handle.id().untyped());
 | |
|         handle
 | |
|     }
 | |
| 
 | |
|     /// Loads the asset at the given `path` directly. This is an async function that will wait until the asset is fully loaded before
 | |
|     /// returning. Use this if you need the _value_ of another asset in order to load the current asset. For example, if you are
 | |
|     /// deriving a new asset from the referenced asset, or you are building a collection of assets. This will add the `path` as a
 | |
|     /// "load dependency".
 | |
|     ///
 | |
|     /// If the current loader is used in a [`Process`] "asset preprocessor", such as a [`LoadAndSave`] preprocessor,
 | |
|     /// changing a "load dependency" will result in re-processing of the asset.
 | |
|     ///
 | |
|     /// [`Process`]: crate::processor::Process
 | |
|     /// [`LoadAndSave`]: crate::processor::LoadAndSave
 | |
|     pub async fn load_direct<'b>(
 | |
|         &mut self,
 | |
|         path: impl Into<AssetPath<'b>>,
 | |
|     ) -> Result<ErasedLoadedAsset, LoadDirectError> {
 | |
|         let path = path.into().into_owned();
 | |
|         let to_error = |e: AssetLoadError| -> LoadDirectError {
 | |
|             LoadDirectError {
 | |
|                 dependency: path.clone(),
 | |
|                 error: e,
 | |
|             }
 | |
|         };
 | |
|         let loaded_asset = {
 | |
|             let (meta, loader, mut reader) = self
 | |
|                 .asset_server
 | |
|                 .get_meta_loader_and_reader(&path)
 | |
|                 .await
 | |
|                 .map_err(to_error)?;
 | |
|             self.asset_server
 | |
|                 .load_with_meta_loader_and_reader(
 | |
|                     &path,
 | |
|                     meta,
 | |
|                     &*loader,
 | |
|                     &mut *reader,
 | |
|                     false,
 | |
|                     self.populate_hashes,
 | |
|                 )
 | |
|                 .await
 | |
|                 .map_err(to_error)?
 | |
|         };
 | |
|         let info = loaded_asset
 | |
|             .meta
 | |
|             .as_ref()
 | |
|             .and_then(|m| m.processed_info().as_ref());
 | |
|         let hash = info.map(|i| i.full_hash).unwrap_or(Default::default());
 | |
|         self.loader_dependencies.insert(path, hash);
 | |
|         Ok(loaded_asset)
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// An error produced when calling [`LoadContext::read_asset_bytes`]
 | |
| #[derive(Error, Debug)]
 | |
| pub enum ReadAssetBytesError {
 | |
|     #[error(transparent)]
 | |
|     DeserializeMetaError(#[from] DeserializeMetaError),
 | |
|     #[error(transparent)]
 | |
|     AssetReaderError(#[from] AssetReaderError),
 | |
|     #[error(transparent)]
 | |
|     MissingAssetSourceError(#[from] MissingAssetSourceError),
 | |
|     #[error(transparent)]
 | |
|     MissingProcessedAssetReaderError(#[from] MissingProcessedAssetReaderError),
 | |
|     /// Encountered an I/O error while loading an asset.
 | |
|     #[error("Encountered an io error while loading asset: {0}")]
 | |
|     Io(#[from] std::io::Error),
 | |
|     #[error("The LoadContext for this read_asset_bytes call requires hash metadata, but it was not provided. This is likely an internal implementation error.")]
 | |
|     MissingAssetHash,
 | |
| }
 |