mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Use async-fn in traits rather than BoxedFuture (#12550)
# Objective Simplify implementing some asset traits without Box::pin(async move{}) shenanigans. Fixes (in part) https://github.com/bevyengine/bevy/issues/11308 ## Solution Use async-fn in traits when possible in all traits. Traits with return position impl trait are not object safe however, and as AssetReader and AssetWriter are both used with dynamic dispatch, you need a Boxed version of these futures anyway. In the future, Rust is [adding ](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html)proc macros to generate these traits automatically, and at some point in the future dyn traits should 'just work'. Until then.... this seemed liked the right approach given more ErasedXXX already exist, but, no clue if there's plans here! Especially since these are public now, it's a bit of an unfortunate API, and means this is a breaking change. In theory this saves some performance when these traits are used with static dispatch, but, seems like most code paths go through dynamic dispatch, which boxes anyway. I also suspect a bunch of the lifetime annotations on these function could be simplified now as the BoxedFuture was often the only thing returned which needed a lifetime annotation, but I'm not touching that for now as traits + lifetimes can be so tricky. This is a revival of [pull/11362](https://github.com/bevyengine/bevy/pull/11362) after a spectacular merge f*ckup, with updates to the latest Bevy. Just to recap some discussion: - Overall this seems like a win for code quality, especially when implementing these traits, but a loss for having to deal with ErasedXXX variants. - `ConditionalSend` was the preferred name for the trait that might be Send, to deal with wasm platforms. - When reviewing be sure to disable whitespace difference, as that's 95% of the PR. ## Changelog - AssetReader, AssetWriter, AssetLoader, AssetSaver and Process now use async-fn in traits rather than boxed futures. ## Migration Guide - Custom implementations of AssetReader, AssetWriter, AssetLoader, AssetSaver and Process should switch to async fn rather than returning a bevy_utils::BoxedFuture. - Simultaniously, to use dynamic dispatch on these traits you should instead use dyn ErasedXXX.
This commit is contained in:
parent
ce75dec3b8
commit
ac49dce4ca
33 changed files with 1090 additions and 1095 deletions
|
@ -6,7 +6,6 @@ use std::ops::{Index, IndexMut};
|
||||||
use bevy_asset::io::Reader;
|
use bevy_asset::io::Reader;
|
||||||
use bevy_asset::{Asset, AssetId, AssetLoader, AssetPath, AsyncReadExt as _, Handle, LoadContext};
|
use bevy_asset::{Asset, AssetId, AssetLoader, AssetPath, AsyncReadExt as _, Handle, LoadContext};
|
||||||
use bevy_reflect::{Reflect, ReflectSerialize};
|
use bevy_reflect::{Reflect, ReflectSerialize};
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use petgraph::graph::{DiGraph, NodeIndex};
|
use petgraph::graph::{DiGraph, NodeIndex};
|
||||||
use ron::de::SpannedError;
|
use ron::de::SpannedError;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -336,21 +335,19 @@ impl AssetLoader for AnimationGraphAssetLoader {
|
||||||
|
|
||||||
type Error = AnimationGraphLoadError;
|
type Error = AnimationGraphLoadError;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_: &'a Self::Settings,
|
_: &'a Self::Settings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
|
|
||||||
// Deserialize a `SerializedAnimationGraph` directly, so that we can
|
// Deserialize a `SerializedAnimationGraph` directly, so that we can
|
||||||
// get the list of the animation clips it refers to and load them.
|
// get the list of the animation clips it refers to and load them.
|
||||||
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
||||||
let serialized_animation_graph =
|
let serialized_animation_graph = SerializedAnimationGraph::deserialize(&mut deserializer)
|
||||||
SerializedAnimationGraph::deserialize(&mut deserializer)
|
|
||||||
.map_err(|err| deserializer.span_error(err))?;
|
.map_err(|err| deserializer.span_error(err))?;
|
||||||
|
|
||||||
// Load all `AssetPath`s to convert from a
|
// Load all `AssetPath`s to convert from a
|
||||||
|
@ -370,7 +367,6 @@ impl AssetLoader for AnimationGraphAssetLoader {
|
||||||
),
|
),
|
||||||
root: serialized_animation_graph.root,
|
root: serialized_animation_graph.root,
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -2,7 +2,6 @@ use crate::io::{
|
||||||
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
|
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
|
||||||
};
|
};
|
||||||
use bevy_utils::tracing::error;
|
use bevy_utils::tracing::error;
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use std::{ffi::CString, path::Path};
|
use std::{ffi::CString, path::Path};
|
||||||
|
|
||||||
/// [`AssetReader`] implementation for Android devices, built on top of Android's [`AssetManager`].
|
/// [`AssetReader`] implementation for Android devices, built on top of Android's [`AssetManager`].
|
||||||
|
@ -17,11 +16,7 @@ use std::{ffi::CString, path::Path};
|
||||||
pub struct AndroidAssetReader;
|
pub struct AndroidAssetReader;
|
||||||
|
|
||||||
impl AssetReader for AndroidAssetReader {
|
impl AssetReader for AndroidAssetReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let asset_manager = bevy_winit::ANDROID_APP
|
let asset_manager = bevy_winit::ANDROID_APP
|
||||||
.get()
|
.get()
|
||||||
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
|
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
|
||||||
|
@ -32,14 +27,9 @@ impl AssetReader for AndroidAssetReader {
|
||||||
let bytes = opened_asset.buffer()?;
|
let bytes = opened_asset.buffer()?;
|
||||||
let reader: Box<Reader> = Box::new(VecReader::new(bytes.to_vec()));
|
let reader: Box<Reader> = Box::new(VecReader::new(bytes.to_vec()));
|
||||||
Ok(reader)
|
Ok(reader)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
let asset_manager = bevy_winit::ANDROID_APP
|
let asset_manager = bevy_winit::ANDROID_APP
|
||||||
.get()
|
.get()
|
||||||
|
@ -51,23 +41,22 @@ impl AssetReader for AndroidAssetReader {
|
||||||
let bytes = opened_asset.buffer()?;
|
let bytes = opened_asset.buffer()?;
|
||||||
let reader: Box<Reader> = Box::new(VecReader::new(bytes.to_vec()));
|
let reader: Box<Reader> = Box::new(VecReader::new(bytes.to_vec()));
|
||||||
Ok(reader)
|
Ok(reader)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_path: &'a Path,
|
_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
let stream: Box<PathStream> = Box::new(EmptyPathStream);
|
let stream: Box<PathStream> = Box::new(EmptyPathStream);
|
||||||
error!("Reading directories is not supported with the AndroidAssetReader");
|
error!("Reading directories is not supported with the AndroidAssetReader");
|
||||||
Box::pin(async move { Ok(stream) })
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_path: &'a Path,
|
_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<bool, AssetReaderError>> {
|
) -> std::result::Result<bool, AssetReaderError> {
|
||||||
error!("Reading directories is not supported with the AndroidAssetReader");
|
error!("Reading directories is not supported with the AndroidAssetReader");
|
||||||
Box::pin(async move { Ok(false) })
|
Ok(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ use crate::io::{
|
||||||
Reader, Writer,
|
Reader, Writer,
|
||||||
};
|
};
|
||||||
use async_fs::{read_dir, File};
|
use async_fs::{read_dir, File};
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use futures_lite::StreamExt;
|
use futures_lite::StreamExt;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -11,11 +10,7 @@ use std::path::Path;
|
||||||
use super::{FileAssetReader, FileAssetWriter};
|
use super::{FileAssetReader, FileAssetWriter};
|
||||||
|
|
||||||
impl AssetReader for FileAssetReader {
|
impl AssetReader for FileAssetReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
match File::open(&full_path).await {
|
match File::open(&full_path).await {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
|
@ -30,15 +25,10 @@ impl AssetReader for FileAssetReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(meta_path);
|
let full_path = self.root_path.join(meta_path);
|
||||||
match File::open(&full_path).await {
|
match File::open(&full_path).await {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
|
@ -53,14 +43,12 @@ impl AssetReader for FileAssetReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
match read_dir(&full_path).await {
|
match read_dir(&full_path).await {
|
||||||
Ok(read_dir) => {
|
Ok(read_dir) => {
|
||||||
|
@ -89,29 +77,19 @@ impl AssetReader for FileAssetReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
let metadata = full_path
|
let metadata = full_path
|
||||||
.metadata()
|
.metadata()
|
||||||
.map_err(|_e| AssetReaderError::NotFound(path.to_owned()))?;
|
.map_err(|_e| AssetReaderError::NotFound(path.to_owned()))?;
|
||||||
Ok(metadata.file_type().is_dir())
|
Ok(metadata.file_type().is_dir())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetWriter for FileAssetWriter {
|
impl AssetWriter for FileAssetWriter {
|
||||||
fn write<'a>(
|
async fn write<'a>(&'a self, path: &'a Path) -> Result<Box<Writer>, AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Writer>, AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
if let Some(parent) = full_path.parent() {
|
if let Some(parent) = full_path.parent() {
|
||||||
async_fs::create_dir_all(parent).await?;
|
async_fs::create_dir_all(parent).await?;
|
||||||
|
@ -119,14 +97,9 @@ impl AssetWriter for FileAssetWriter {
|
||||||
let file = File::create(&full_path).await?;
|
let file = File::create(&full_path).await?;
|
||||||
let writer: Box<Writer> = Box::new(file);
|
let writer: Box<Writer> = Box::new(file);
|
||||||
Ok(writer)
|
Ok(writer)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_meta<'a>(
|
async fn write_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Writer>, AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Writer>, AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
let full_path = self.root_path.join(meta_path);
|
let full_path = self.root_path.join(meta_path);
|
||||||
if let Some(parent) = full_path.parent() {
|
if let Some(parent) = full_path.parent() {
|
||||||
|
@ -135,32 +108,26 @@ impl AssetWriter for FileAssetWriter {
|
||||||
let file = File::create(&full_path).await?;
|
let file = File::create(&full_path).await?;
|
||||||
let writer: Box<Writer> = Box::new(file);
|
let writer: Box<Writer> = Box::new(file);
|
||||||
Ok(writer)
|
Ok(writer)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
async fn remove<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
async_fs::remove_file(full_path).await?;
|
async_fs::remove_file(full_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_meta<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
async fn remove_meta<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
let full_path = self.root_path.join(meta_path);
|
let full_path = self.root_path.join(meta_path);
|
||||||
async_fs::remove_file(full_path).await?;
|
async_fs::remove_file(full_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename<'a>(
|
async fn rename<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
old_path: &'a Path,
|
old_path: &'a Path,
|
||||||
new_path: &'a Path,
|
new_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
) -> Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_old_path = self.root_path.join(old_path);
|
let full_old_path = self.root_path.join(old_path);
|
||||||
let full_new_path = self.root_path.join(new_path);
|
let full_new_path = self.root_path.join(new_path);
|
||||||
if let Some(parent) = full_new_path.parent() {
|
if let Some(parent) = full_new_path.parent() {
|
||||||
|
@ -168,15 +135,13 @@ impl AssetWriter for FileAssetWriter {
|
||||||
}
|
}
|
||||||
async_fs::rename(full_old_path, full_new_path).await?;
|
async_fs::rename(full_old_path, full_new_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_meta<'a>(
|
async fn rename_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
old_path: &'a Path,
|
old_path: &'a Path,
|
||||||
new_path: &'a Path,
|
new_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
) -> Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let old_meta_path = get_meta_path(old_path);
|
let old_meta_path = get_meta_path(old_path);
|
||||||
let new_meta_path = get_meta_path(new_path);
|
let new_meta_path = get_meta_path(new_path);
|
||||||
let full_old_path = self.root_path.join(old_meta_path);
|
let full_old_path = self.root_path.join(old_meta_path);
|
||||||
|
@ -186,40 +151,27 @@ impl AssetWriter for FileAssetWriter {
|
||||||
}
|
}
|
||||||
async_fs::rename(full_old_path, full_new_path).await?;
|
async_fs::rename(full_old_path, full_new_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_directory<'a>(
|
async fn remove_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
async_fs::remove_dir_all(full_path).await?;
|
async_fs::remove_dir_all(full_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_empty_directory<'a>(
|
async fn remove_empty_directory<'a>(&'a self, path: &'a Path) -> Result<(), AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
async_fs::remove_dir(full_path).await?;
|
async_fs::remove_dir(full_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_assets_in_directory<'a>(
|
async fn remove_assets_in_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
) -> Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
async_fs::remove_dir_all(&full_path).await?;
|
async_fs::remove_dir_all(&full_path).await?;
|
||||||
async_fs::create_dir_all(&full_path).await?;
|
async_fs::create_dir_all(&full_path).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ use crate::io::{
|
||||||
get_meta_path, AssetReader, AssetReaderError, AssetWriter, AssetWriterError, PathStream,
|
get_meta_path, AssetReader, AssetReaderError, AssetWriter, AssetWriterError, PathStream,
|
||||||
Reader, Writer,
|
Reader, Writer,
|
||||||
};
|
};
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs::{read_dir, File},
|
fs::{read_dir, File},
|
||||||
|
@ -76,11 +75,7 @@ impl Stream for DirReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetReader for FileAssetReader {
|
impl AssetReader for FileAssetReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
match File::open(&full_path) {
|
match File::open(&full_path) {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
|
@ -95,15 +90,10 @@ impl AssetReader for FileAssetReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(meta_path);
|
let full_path = self.root_path.join(meta_path);
|
||||||
match File::open(&full_path) {
|
match File::open(&full_path) {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
|
@ -118,14 +108,12 @@ impl AssetReader for FileAssetReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
match read_dir(&full_path) {
|
match read_dir(&full_path) {
|
||||||
Ok(read_dir) => {
|
Ok(read_dir) => {
|
||||||
|
@ -154,29 +142,22 @@ impl AssetReader for FileAssetReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<bool, AssetReaderError>> {
|
) -> std::result::Result<bool, AssetReaderError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
let metadata = full_path
|
let metadata = full_path
|
||||||
.metadata()
|
.metadata()
|
||||||
.map_err(|_e| AssetReaderError::NotFound(path.to_owned()))?;
|
.map_err(|_e| AssetReaderError::NotFound(path.to_owned()))?;
|
||||||
Ok(metadata.file_type().is_dir())
|
Ok(metadata.file_type().is_dir())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetWriter for FileAssetWriter {
|
impl AssetWriter for FileAssetWriter {
|
||||||
fn write<'a>(
|
async fn write<'a>(&'a self, path: &'a Path) -> Result<Box<Writer>, AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Writer>, AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
if let Some(parent) = full_path.parent() {
|
if let Some(parent) = full_path.parent() {
|
||||||
std::fs::create_dir_all(parent)?;
|
std::fs::create_dir_all(parent)?;
|
||||||
|
@ -184,14 +165,9 @@ impl AssetWriter for FileAssetWriter {
|
||||||
let file = File::create(&full_path)?;
|
let file = File::create(&full_path)?;
|
||||||
let writer: Box<Writer> = Box::new(FileWriter(file));
|
let writer: Box<Writer> = Box::new(FileWriter(file));
|
||||||
Ok(writer)
|
Ok(writer)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_meta<'a>(
|
async fn write_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Writer>, AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Writer>, AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
let full_path = self.root_path.join(meta_path);
|
let full_path = self.root_path.join(meta_path);
|
||||||
if let Some(parent) = full_path.parent() {
|
if let Some(parent) = full_path.parent() {
|
||||||
|
@ -200,72 +176,57 @@ impl AssetWriter for FileAssetWriter {
|
||||||
let file = File::create(&full_path)?;
|
let file = File::create(&full_path)?;
|
||||||
let writer: Box<Writer> = Box::new(FileWriter(file));
|
let writer: Box<Writer> = Box::new(FileWriter(file));
|
||||||
Ok(writer)
|
Ok(writer)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove<'a>(
|
async fn remove<'a>(&'a self, path: &'a Path) -> std::result::Result<(), AssetWriterError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
std::fs::remove_file(full_path)?;
|
std::fs::remove_file(full_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_meta<'a>(
|
async fn remove_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
) -> std::result::Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let meta_path = get_meta_path(path);
|
let meta_path = get_meta_path(path);
|
||||||
let full_path = self.root_path.join(meta_path);
|
let full_path = self.root_path.join(meta_path);
|
||||||
std::fs::remove_file(full_path)?;
|
std::fs::remove_file(full_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_directory<'a>(
|
async fn remove_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
) -> std::result::Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
std::fs::remove_dir_all(full_path)?;
|
std::fs::remove_dir_all(full_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_empty_directory<'a>(
|
async fn remove_empty_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
) -> std::result::Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
std::fs::remove_dir(full_path)?;
|
std::fs::remove_dir(full_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_assets_in_directory<'a>(
|
async fn remove_assets_in_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
) -> std::result::Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_path = self.root_path.join(path);
|
let full_path = self.root_path.join(path);
|
||||||
std::fs::remove_dir_all(&full_path)?;
|
std::fs::remove_dir_all(&full_path)?;
|
||||||
std::fs::create_dir_all(&full_path)?;
|
std::fs::create_dir_all(&full_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename<'a>(
|
async fn rename<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
old_path: &'a Path,
|
old_path: &'a Path,
|
||||||
new_path: &'a Path,
|
new_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
) -> std::result::Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let full_old_path = self.root_path.join(old_path);
|
let full_old_path = self.root_path.join(old_path);
|
||||||
let full_new_path = self.root_path.join(new_path);
|
let full_new_path = self.root_path.join(new_path);
|
||||||
if let Some(parent) = full_new_path.parent() {
|
if let Some(parent) = full_new_path.parent() {
|
||||||
|
@ -273,15 +234,13 @@ impl AssetWriter for FileAssetWriter {
|
||||||
}
|
}
|
||||||
std::fs::rename(full_old_path, full_new_path)?;
|
std::fs::rename(full_old_path, full_new_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_meta<'a>(
|
async fn rename_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
old_path: &'a Path,
|
old_path: &'a Path,
|
||||||
new_path: &'a Path,
|
new_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<(), AssetWriterError>> {
|
) -> std::result::Result<(), AssetWriterError> {
|
||||||
Box::pin(async move {
|
|
||||||
let old_meta_path = get_meta_path(old_path);
|
let old_meta_path = get_meta_path(old_path);
|
||||||
let new_meta_path = get_meta_path(new_path);
|
let new_meta_path = get_meta_path(new_path);
|
||||||
let full_old_path = self.root_path.join(old_meta_path);
|
let full_old_path = self.root_path.join(old_meta_path);
|
||||||
|
@ -291,6 +250,5 @@ impl AssetWriter for FileAssetWriter {
|
||||||
}
|
}
|
||||||
std::fs::rename(full_old_path, full_new_path)?;
|
std::fs::rename(full_old_path, full_new_path)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
|
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
|
||||||
use bevy_utils::{BoxedFuture, HashMap};
|
use bevy_utils::HashMap;
|
||||||
use crossbeam_channel::{Receiver, Sender};
|
use crossbeam_channel::{Receiver, Sender};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use std::{path::Path, sync::Arc};
|
use std::{path::Path, sync::Arc};
|
||||||
|
@ -55,10 +55,7 @@ impl<R: AssetReader> GatedReader<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: AssetReader> AssetReader for GatedReader<R> {
|
impl<R: AssetReader> AssetReader for GatedReader<R> {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
let receiver = {
|
let receiver = {
|
||||||
let mut gates = self.gates.write();
|
let mut gates = self.gates.write();
|
||||||
let gates = gates
|
let gates = gates
|
||||||
|
@ -66,31 +63,23 @@ impl<R: AssetReader> AssetReader for GatedReader<R> {
|
||||||
.or_insert_with(crossbeam_channel::unbounded);
|
.or_insert_with(crossbeam_channel::unbounded);
|
||||||
gates.1.clone()
|
gates.1.clone()
|
||||||
};
|
};
|
||||||
Box::pin(async move {
|
|
||||||
receiver.recv().unwrap();
|
receiver.recv().unwrap();
|
||||||
let result = self.reader.read(path).await?;
|
let result = self.reader.read(path).await?;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
self.reader.read_meta(path).await
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
self.reader.read_meta(path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
self.reader.read_directory(path)
|
self.reader.read_directory(path).await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
||||||
&'a self,
|
self.reader.is_directory(path).await
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
|
||||||
self.reader.is_directory(path)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
|
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
|
||||||
use bevy_utils::{BoxedFuture, HashMap};
|
use bevy_utils::HashMap;
|
||||||
use futures_io::AsyncRead;
|
use futures_io::AsyncRead;
|
||||||
use futures_lite::{ready, Stream};
|
use futures_lite::{ready, Stream};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
|
@ -237,11 +237,7 @@ impl AsyncRead for DataReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetReader for MemoryAssetReader {
|
impl AssetReader for MemoryAssetReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
self.root
|
self.root
|
||||||
.get_asset(path)
|
.get_asset(path)
|
||||||
.map(|data| {
|
.map(|data| {
|
||||||
|
@ -252,14 +248,9 @@ impl AssetReader for MemoryAssetReader {
|
||||||
reader
|
reader
|
||||||
})
|
})
|
||||||
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
|
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
self.root
|
self.root
|
||||||
.get_metadata(path)
|
.get_metadata(path)
|
||||||
.map(|data| {
|
.map(|data| {
|
||||||
|
@ -270,14 +261,12 @@ impl AssetReader for MemoryAssetReader {
|
||||||
reader
|
reader
|
||||||
})
|
})
|
||||||
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
|
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
Box::pin(async move {
|
|
||||||
self.root
|
self.root
|
||||||
.get_dir(path)
|
.get_dir(path)
|
||||||
.map(|dir| {
|
.map(|dir| {
|
||||||
|
@ -285,14 +274,10 @@ impl AssetReader for MemoryAssetReader {
|
||||||
stream
|
stream
|
||||||
})
|
})
|
||||||
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
|
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
||||||
&'a self,
|
Ok(self.root.get_dir(path).is_some())
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
|
||||||
Box::pin(async move { Ok(self.root.get_dir(path).is_some()) })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ mod source;
|
||||||
pub use futures_lite::{AsyncReadExt, AsyncWriteExt};
|
pub use futures_lite::{AsyncReadExt, AsyncWriteExt};
|
||||||
pub use source::*;
|
pub use source::*;
|
||||||
|
|
||||||
use bevy_utils::BoxedFuture;
|
use bevy_utils::{BoxedFuture, ConditionalSendFuture};
|
||||||
use futures_io::{AsyncRead, AsyncWrite};
|
use futures_io::{AsyncRead, AsyncWrite};
|
||||||
use futures_lite::{ready, Stream};
|
use futures_lite::{ready, Stream};
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -59,7 +59,7 @@ pub type Reader<'a> = dyn AsyncRead + Unpin + Send + Sync + 'a;
|
||||||
|
|
||||||
/// Performs read operations on an asset storage. [`AssetReader`] exposes a "virtual filesystem"
|
/// Performs read operations on an asset storage. [`AssetReader`] exposes a "virtual filesystem"
|
||||||
/// API, where asset bytes and asset metadata bytes are both stored and accessible for a given
|
/// API, where asset bytes and asset metadata bytes are both stored and accessible for a given
|
||||||
/// `path`.
|
/// `path`. This trait is not object safe, if needed use a dyn [`ErasedAssetReader`] instead.
|
||||||
///
|
///
|
||||||
/// Also see [`AssetWriter`].
|
/// Also see [`AssetWriter`].
|
||||||
pub trait AssetReader: Send + Sync + 'static {
|
pub trait AssetReader: Send + Sync + 'static {
|
||||||
|
@ -67,35 +67,90 @@ pub trait AssetReader: Send + Sync + 'static {
|
||||||
fn read<'a>(
|
fn read<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>>;
|
) -> impl ConditionalSendFuture<Output = Result<Box<Reader<'a>>, AssetReaderError>>;
|
||||||
/// Returns a future to load the full file data at the provided path.
|
/// Returns a future to load the full file data at the provided path.
|
||||||
fn read_meta<'a>(
|
fn read_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>>;
|
) -> impl ConditionalSendFuture<Output = Result<Box<Reader<'a>>, AssetReaderError>>;
|
||||||
/// Returns an iterator of directory entry names at the provided path.
|
/// Returns an iterator of directory entry names at the provided path.
|
||||||
fn read_directory<'a>(
|
fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>>;
|
) -> impl ConditionalSendFuture<Output = Result<Box<PathStream>, AssetReaderError>>;
|
||||||
/// Returns true if the provided path points to a directory.
|
/// Returns an iterator of directory entry names at the provided path.
|
||||||
fn is_directory<'a>(
|
fn is_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>>;
|
) -> impl ConditionalSendFuture<Output = Result<bool, AssetReaderError>>;
|
||||||
|
|
||||||
/// Reads asset metadata bytes at the given `path` into a [`Vec<u8>`]. This is a convenience
|
/// Reads asset metadata bytes at the given `path` into a [`Vec<u8>`]. This is a convenience
|
||||||
/// function that wraps [`AssetReader::read_meta`] by default.
|
/// function that wraps [`AssetReader::read_meta`] by default.
|
||||||
fn read_meta_bytes<'a>(
|
fn read_meta_bytes<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Vec<u8>, AssetReaderError>> {
|
) -> impl ConditionalSendFuture<Output = Result<Vec<u8>, AssetReaderError>> {
|
||||||
Box::pin(async move {
|
async {
|
||||||
let mut meta_reader = self.read_meta(path).await?;
|
let mut meta_reader = self.read_meta(path).await?;
|
||||||
let mut meta_bytes = Vec::new();
|
let mut meta_bytes = Vec::new();
|
||||||
meta_reader.read_to_end(&mut meta_bytes).await?;
|
meta_reader.read_to_end(&mut meta_bytes).await?;
|
||||||
Ok(meta_bytes)
|
Ok(meta_bytes)
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Equivalent to an [`AssetReader`] but using boxed futures, necessary eg. when using a `dyn AssetReader`,
|
||||||
|
/// as [`AssetReader`] isn't currently object safe.
|
||||||
|
pub trait ErasedAssetReader: Send + Sync + 'static {
|
||||||
|
/// Returns a future to load the full file data at the provided path.
|
||||||
|
fn read<'a>(&'a self, path: &'a Path)
|
||||||
|
-> BoxedFuture<Result<Box<Reader<'a>>, AssetReaderError>>;
|
||||||
|
/// Returns a future to load the full file data at the provided path.
|
||||||
|
fn read_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<Reader<'a>>, AssetReaderError>>;
|
||||||
|
/// Returns an iterator of directory entry names at the provided path.
|
||||||
|
fn read_directory<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<PathStream>, AssetReaderError>>;
|
||||||
|
/// Returns true if the provided path points to a directory.
|
||||||
|
fn is_directory<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<bool, AssetReaderError>>;
|
||||||
|
/// Reads asset metadata bytes at the given `path` into a [`Vec<u8>`]. This is a convenience
|
||||||
|
/// function that wraps [`ErasedAssetReader::read_meta`] by default.
|
||||||
|
fn read_meta_bytes<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Vec<u8>, AssetReaderError>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: AssetReader> ErasedAssetReader for T {
|
||||||
|
fn read<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<Reader<'a>>, AssetReaderError>> {
|
||||||
|
Box::pin(Self::read(self, path))
|
||||||
|
}
|
||||||
|
fn read_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<Reader<'a>>, AssetReaderError>> {
|
||||||
|
Box::pin(Self::read_meta(self, path))
|
||||||
|
}
|
||||||
|
fn read_directory<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<PathStream>, AssetReaderError>> {
|
||||||
|
Box::pin(Self::read_directory(self, path))
|
||||||
|
}
|
||||||
|
fn is_directory<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<bool, AssetReaderError>> {
|
||||||
|
Box::pin(Self::is_directory(self, path))
|
||||||
|
}
|
||||||
|
fn read_meta_bytes<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Vec<u8>, AssetReaderError>> {
|
||||||
|
Box::pin(Self::read_meta_bytes(self, path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +168,7 @@ pub enum AssetWriterError {
|
||||||
|
|
||||||
/// Preforms write operations on an asset storage. [`AssetWriter`] exposes a "virtual filesystem"
|
/// Preforms write operations on an asset storage. [`AssetWriter`] exposes a "virtual filesystem"
|
||||||
/// API, where asset bytes and asset metadata bytes are both stored and accessible for a given
|
/// API, where asset bytes and asset metadata bytes are both stored and accessible for a given
|
||||||
/// `path`.
|
/// `path`. This trait is not object safe, if needed use a dyn [`ErasedAssetWriter`] instead.
|
||||||
///
|
///
|
||||||
/// Also see [`AssetReader`].
|
/// Also see [`AssetReader`].
|
||||||
pub trait AssetWriter: Send + Sync + 'static {
|
pub trait AssetWriter: Send + Sync + 'static {
|
||||||
|
@ -121,72 +176,195 @@ pub trait AssetWriter: Send + Sync + 'static {
|
||||||
fn write<'a>(
|
fn write<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<Writer>, AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<Box<Writer>, AssetWriterError>>;
|
||||||
/// Writes the full asset meta bytes at the provided path.
|
/// Writes the full asset meta bytes at the provided path.
|
||||||
/// This _should not_ include storage specific extensions like `.meta`.
|
/// This _should not_ include storage specific extensions like `.meta`.
|
||||||
fn write_meta<'a>(
|
fn write_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<Writer>, AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<Box<Writer>, AssetWriterError>>;
|
||||||
/// Removes the asset stored at the given path.
|
/// Removes the asset stored at the given path.
|
||||||
fn remove<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
fn remove<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Removes the asset meta stored at the given path.
|
/// Removes the asset meta stored at the given path.
|
||||||
/// This _should not_ include storage specific extensions like `.meta`.
|
/// This _should not_ include storage specific extensions like `.meta`.
|
||||||
fn remove_meta<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
fn remove_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Renames the asset at `old_path` to `new_path`
|
/// Renames the asset at `old_path` to `new_path`
|
||||||
fn rename<'a>(
|
fn rename<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
old_path: &'a Path,
|
old_path: &'a Path,
|
||||||
new_path: &'a Path,
|
new_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Renames the asset meta for the asset at `old_path` to `new_path`.
|
/// Renames the asset meta for the asset at `old_path` to `new_path`.
|
||||||
/// This _should not_ include storage specific extensions like `.meta`.
|
/// This _should not_ include storage specific extensions like `.meta`.
|
||||||
fn rename_meta<'a>(
|
fn rename_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
old_path: &'a Path,
|
old_path: &'a Path,
|
||||||
new_path: &'a Path,
|
new_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Removes the directory at the given path, including all assets _and_ directories in that directory.
|
/// Removes the directory at the given path, including all assets _and_ directories in that directory.
|
||||||
fn remove_directory<'a>(
|
fn remove_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Removes the directory at the given path, but only if it is completely empty. This will return an error if the
|
/// Removes the directory at the given path, but only if it is completely empty. This will return an error if the
|
||||||
/// directory is not empty.
|
/// directory is not empty.
|
||||||
fn remove_empty_directory<'a>(
|
fn remove_empty_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Removes all assets (and directories) in this directory, resulting in an empty directory.
|
/// Removes all assets (and directories) in this directory, resulting in an empty directory.
|
||||||
fn remove_assets_in_directory<'a>(
|
fn remove_assets_in_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>>;
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>>;
|
||||||
/// Writes the asset `bytes` to the given `path`.
|
/// Writes the asset `bytes` to the given `path`.
|
||||||
fn write_bytes<'a>(
|
fn write_bytes<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
bytes: &'a [u8],
|
bytes: &'a [u8],
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>> {
|
||||||
Box::pin(async move {
|
async {
|
||||||
let mut writer = self.write(path).await?;
|
let mut writer = self.write(path).await?;
|
||||||
writer.write_all(bytes).await?;
|
writer.write_all(bytes).await?;
|
||||||
writer.flush().await?;
|
writer.flush().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
/// Writes the asset meta `bytes` to the given `path`.
|
/// Writes the asset meta `bytes` to the given `path`.
|
||||||
fn write_meta_bytes<'a>(
|
fn write_meta_bytes<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
bytes: &'a [u8],
|
bytes: &'a [u8],
|
||||||
) -> BoxedFuture<'a, Result<(), AssetWriterError>> {
|
) -> impl ConditionalSendFuture<Output = Result<(), AssetWriterError>> {
|
||||||
Box::pin(async move {
|
async {
|
||||||
let mut meta_writer = self.write_meta(path).await?;
|
let mut meta_writer = self.write_meta(path).await?;
|
||||||
meta_writer.write_all(bytes).await?;
|
meta_writer.write_all(bytes).await?;
|
||||||
meta_writer.flush().await?;
|
meta_writer.flush().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Equivalent to an [`AssetWriter`] but using boxed futures, necessary eg. when using a `dyn AssetWriter`,
|
||||||
|
/// as [`AssetWriter`] isn't currently object safe.
|
||||||
|
pub trait ErasedAssetWriter: Send + Sync + 'static {
|
||||||
|
/// Writes the full asset bytes at the provided path.
|
||||||
|
fn write<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<Box<Writer>, AssetWriterError>>;
|
||||||
|
/// Writes the full asset meta bytes at the provided path.
|
||||||
|
/// This _should not_ include storage specific extensions like `.meta`.
|
||||||
|
fn write_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<Writer>, AssetWriterError>>;
|
||||||
|
/// Removes the asset stored at the given path.
|
||||||
|
fn remove<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Removes the asset meta stored at the given path.
|
||||||
|
/// This _should not_ include storage specific extensions like `.meta`.
|
||||||
|
fn remove_meta<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Renames the asset at `old_path` to `new_path`
|
||||||
|
fn rename<'a>(
|
||||||
|
&'a self,
|
||||||
|
old_path: &'a Path,
|
||||||
|
new_path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Renames the asset meta for the asset at `old_path` to `new_path`.
|
||||||
|
/// This _should not_ include storage specific extensions like `.meta`.
|
||||||
|
fn rename_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
old_path: &'a Path,
|
||||||
|
new_path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Removes the directory at the given path, including all assets _and_ directories in that directory.
|
||||||
|
fn remove_directory<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Removes the directory at the given path, but only if it is completely empty. This will return an error if the
|
||||||
|
/// directory is not empty.
|
||||||
|
fn remove_empty_directory<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Removes all assets (and directories) in this directory, resulting in an empty directory.
|
||||||
|
fn remove_assets_in_directory<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Writes the asset `bytes` to the given `path`.
|
||||||
|
fn write_bytes<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
bytes: &'a [u8],
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
/// Writes the asset meta `bytes` to the given `path`.
|
||||||
|
fn write_meta_bytes<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
bytes: &'a [u8],
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: AssetWriter> ErasedAssetWriter for T {
|
||||||
|
fn write<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<Box<Writer>, AssetWriterError>> {
|
||||||
|
Box::pin(Self::write(self, path))
|
||||||
|
}
|
||||||
|
fn write_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<Box<Writer>, AssetWriterError>> {
|
||||||
|
Box::pin(Self::write_meta(self, path))
|
||||||
|
}
|
||||||
|
fn remove<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::remove(self, path))
|
||||||
|
}
|
||||||
|
fn remove_meta<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::remove_meta(self, path))
|
||||||
|
}
|
||||||
|
fn rename<'a>(
|
||||||
|
&'a self,
|
||||||
|
old_path: &'a Path,
|
||||||
|
new_path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::rename(self, old_path, new_path))
|
||||||
|
}
|
||||||
|
fn rename_meta<'a>(
|
||||||
|
&'a self,
|
||||||
|
old_path: &'a Path,
|
||||||
|
new_path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::rename_meta(self, old_path, new_path))
|
||||||
|
}
|
||||||
|
fn remove_directory<'a>(&'a self, path: &'a Path) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::remove_directory(self, path))
|
||||||
|
}
|
||||||
|
fn remove_empty_directory<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::remove_empty_directory(self, path))
|
||||||
|
}
|
||||||
|
fn remove_assets_in_directory<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::remove_assets_in_directory(self, path))
|
||||||
|
}
|
||||||
|
fn write_bytes<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
bytes: &'a [u8],
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::write_bytes(self, path, bytes))
|
||||||
|
}
|
||||||
|
fn write_meta_bytes<'a>(
|
||||||
|
&'a self,
|
||||||
|
path: &'a Path,
|
||||||
|
bytes: &'a [u8],
|
||||||
|
) -> BoxedFuture<Result<(), AssetWriterError>> {
|
||||||
|
Box::pin(Self::write_meta_bytes(self, path, bytes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,17 @@ use crate::{
|
||||||
};
|
};
|
||||||
use async_lock::RwLockReadGuardArc;
|
use async_lock::RwLockReadGuardArc;
|
||||||
use bevy_utils::tracing::trace;
|
use bevy_utils::tracing::trace;
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use futures_io::AsyncRead;
|
use futures_io::AsyncRead;
|
||||||
use std::{path::Path, pin::Pin, sync::Arc};
|
use std::{path::Path, pin::Pin, sync::Arc};
|
||||||
|
|
||||||
|
use super::ErasedAssetReader;
|
||||||
|
|
||||||
/// An [`AssetReader`] that will prevent asset (and asset metadata) read futures from returning for a
|
/// An [`AssetReader`] that will prevent asset (and asset metadata) read futures from returning for a
|
||||||
/// given path until that path has been processed by [`AssetProcessor`].
|
/// given path until that path has been processed by [`AssetProcessor`].
|
||||||
///
|
///
|
||||||
/// [`AssetProcessor`]: crate::processor::AssetProcessor
|
/// [`AssetProcessor`]: crate::processor::AssetProcessor
|
||||||
pub struct ProcessorGatedReader {
|
pub struct ProcessorGatedReader {
|
||||||
reader: Box<dyn AssetReader>,
|
reader: Box<dyn ErasedAssetReader>,
|
||||||
source: AssetSourceId<'static>,
|
source: AssetSourceId<'static>,
|
||||||
processor_data: Arc<AssetProcessorData>,
|
processor_data: Arc<AssetProcessorData>,
|
||||||
}
|
}
|
||||||
|
@ -23,7 +24,7 @@ impl ProcessorGatedReader {
|
||||||
/// Creates a new [`ProcessorGatedReader`].
|
/// Creates a new [`ProcessorGatedReader`].
|
||||||
pub fn new(
|
pub fn new(
|
||||||
source: AssetSourceId<'static>,
|
source: AssetSourceId<'static>,
|
||||||
reader: Box<dyn AssetReader>,
|
reader: Box<dyn ErasedAssetReader>,
|
||||||
processor_data: Arc<AssetProcessorData>,
|
processor_data: Arc<AssetProcessorData>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -48,11 +49,7 @@ impl ProcessorGatedReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetReader for ProcessorGatedReader {
|
impl AssetReader for ProcessorGatedReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let asset_path = AssetPath::from(path.to_path_buf()).with_source(self.source.clone());
|
let asset_path = AssetPath::from(path.to_path_buf()).with_source(self.source.clone());
|
||||||
trace!("Waiting for processing to finish before reading {asset_path}");
|
trace!("Waiting for processing to finish before reading {asset_path}");
|
||||||
let process_result = self
|
let process_result = self
|
||||||
|
@ -68,17 +65,11 @@ impl AssetReader for ProcessorGatedReader {
|
||||||
trace!("Processing finished with {asset_path}, reading {process_result:?}",);
|
trace!("Processing finished with {asset_path}, reading {process_result:?}",);
|
||||||
let lock = self.get_transaction_lock(&asset_path).await?;
|
let lock = self.get_transaction_lock(&asset_path).await?;
|
||||||
let asset_reader = self.reader.read(path).await?;
|
let asset_reader = self.reader.read(path).await?;
|
||||||
let reader: Box<Reader<'a>> =
|
let reader: Box<Reader<'a>> = Box::new(TransactionLockedReader::new(asset_reader, lock));
|
||||||
Box::new(TransactionLockedReader::new(asset_reader, lock));
|
|
||||||
Ok(reader)
|
Ok(reader)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let asset_path = AssetPath::from(path.to_path_buf()).with_source(self.source.clone());
|
let asset_path = AssetPath::from(path.to_path_buf()).with_source(self.source.clone());
|
||||||
trace!("Waiting for processing to finish before reading meta for {asset_path}",);
|
trace!("Waiting for processing to finish before reading meta for {asset_path}",);
|
||||||
let process_result = self
|
let process_result = self
|
||||||
|
@ -96,14 +87,12 @@ impl AssetReader for ProcessorGatedReader {
|
||||||
let meta_reader = self.reader.read_meta(path).await?;
|
let meta_reader = self.reader.read_meta(path).await?;
|
||||||
let reader: Box<Reader<'a>> = Box::new(TransactionLockedReader::new(meta_reader, lock));
|
let reader: Box<Reader<'a>> = Box::new(TransactionLockedReader::new(meta_reader, lock));
|
||||||
Ok(reader)
|
Ok(reader)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
Box::pin(async move {
|
|
||||||
trace!(
|
trace!(
|
||||||
"Waiting for processing to finish before reading directory {:?}",
|
"Waiting for processing to finish before reading directory {:?}",
|
||||||
path
|
path
|
||||||
|
@ -112,14 +101,9 @@ impl AssetReader for ProcessorGatedReader {
|
||||||
trace!("Processing finished, reading directory {:?}", path);
|
trace!("Processing finished, reading directory {:?}", path);
|
||||||
let result = self.reader.read_directory(path).await?;
|
let result = self.reader.read_directory(path).await?;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
trace!(
|
trace!(
|
||||||
"Waiting for processing to finish before reading directory {:?}",
|
"Waiting for processing to finish before reading directory {:?}",
|
||||||
path
|
path
|
||||||
|
@ -128,7 +112,6 @@ impl AssetReader for ProcessorGatedReader {
|
||||||
trace!("Processing finished, getting directory status {:?}", path);
|
trace!("Processing finished, getting directory status {:?}", path);
|
||||||
let result = self.reader.is_directory(path).await?;
|
let result = self.reader.is_directory(path).await?;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
io::{
|
io::{processor_gated::ProcessorGatedReader, AssetSourceEvent, AssetWatcher},
|
||||||
processor_gated::ProcessorGatedReader, AssetReader, AssetSourceEvent, AssetWatcher,
|
|
||||||
AssetWriter,
|
|
||||||
},
|
|
||||||
processor::AssetProcessorData,
|
processor::AssetProcessorData,
|
||||||
};
|
};
|
||||||
use bevy_ecs::system::Resource;
|
use bevy_ecs::system::Resource;
|
||||||
|
@ -11,6 +8,12 @@ use bevy_utils::{CowArc, Duration, HashMap};
|
||||||
use std::{fmt::Display, hash::Hash, sync::Arc};
|
use std::{fmt::Display, hash::Hash, sync::Arc};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
use super::{ErasedAssetReader, ErasedAssetWriter};
|
||||||
|
|
||||||
|
// Needed for doc strings.
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use crate::io::{AssetReader, AssetWriter};
|
||||||
|
|
||||||
/// A reference to an "asset source", which maps to an [`AssetReader`] and/or [`AssetWriter`].
|
/// A reference to an "asset source", which maps to an [`AssetReader`] and/or [`AssetWriter`].
|
||||||
///
|
///
|
||||||
/// * [`AssetSourceId::Default`] corresponds to "default asset paths" that don't specify a source: `/path/to/asset.png`
|
/// * [`AssetSourceId::Default`] corresponds to "default asset paths" that don't specify a source: `/path/to/asset.png`
|
||||||
|
@ -110,8 +113,8 @@ impl<'a> PartialEq for AssetSourceId<'a> {
|
||||||
/// and whether or not the source is processed.
|
/// and whether or not the source is processed.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct AssetSourceBuilder {
|
pub struct AssetSourceBuilder {
|
||||||
pub reader: Option<Box<dyn FnMut() -> Box<dyn AssetReader> + Send + Sync>>,
|
pub reader: Option<Box<dyn FnMut() -> Box<dyn ErasedAssetReader> + Send + Sync>>,
|
||||||
pub writer: Option<Box<dyn FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync>>,
|
pub writer: Option<Box<dyn FnMut(bool) -> Option<Box<dyn ErasedAssetWriter>> + Send + Sync>>,
|
||||||
pub watcher: Option<
|
pub watcher: Option<
|
||||||
Box<
|
Box<
|
||||||
dyn FnMut(crossbeam_channel::Sender<AssetSourceEvent>) -> Option<Box<dyn AssetWatcher>>
|
dyn FnMut(crossbeam_channel::Sender<AssetSourceEvent>) -> Option<Box<dyn AssetWatcher>>
|
||||||
|
@ -119,9 +122,9 @@ pub struct AssetSourceBuilder {
|
||||||
+ Sync,
|
+ Sync,
|
||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
pub processed_reader: Option<Box<dyn FnMut() -> Box<dyn AssetReader> + Send + Sync>>,
|
pub processed_reader: Option<Box<dyn FnMut() -> Box<dyn ErasedAssetReader> + Send + Sync>>,
|
||||||
pub processed_writer:
|
pub processed_writer:
|
||||||
Option<Box<dyn FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync>>,
|
Option<Box<dyn FnMut(bool) -> Option<Box<dyn ErasedAssetWriter>> + Send + Sync>>,
|
||||||
pub processed_watcher: Option<
|
pub processed_watcher: Option<
|
||||||
Box<
|
Box<
|
||||||
dyn FnMut(crossbeam_channel::Sender<AssetSourceEvent>) -> Option<Box<dyn AssetWatcher>>
|
dyn FnMut(crossbeam_channel::Sender<AssetSourceEvent>) -> Option<Box<dyn AssetWatcher>>
|
||||||
|
@ -192,7 +195,7 @@ impl AssetSourceBuilder {
|
||||||
/// Will use the given `reader` function to construct unprocessed [`AssetReader`] instances.
|
/// Will use the given `reader` function to construct unprocessed [`AssetReader`] instances.
|
||||||
pub fn with_reader(
|
pub fn with_reader(
|
||||||
mut self,
|
mut self,
|
||||||
reader: impl FnMut() -> Box<dyn AssetReader> + Send + Sync + 'static,
|
reader: impl FnMut() -> Box<dyn ErasedAssetReader> + Send + Sync + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.reader = Some(Box::new(reader));
|
self.reader = Some(Box::new(reader));
|
||||||
self
|
self
|
||||||
|
@ -201,7 +204,7 @@ impl AssetSourceBuilder {
|
||||||
/// Will use the given `writer` function to construct unprocessed [`AssetWriter`] instances.
|
/// Will use the given `writer` function to construct unprocessed [`AssetWriter`] instances.
|
||||||
pub fn with_writer(
|
pub fn with_writer(
|
||||||
mut self,
|
mut self,
|
||||||
writer: impl FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync + 'static,
|
writer: impl FnMut(bool) -> Option<Box<dyn ErasedAssetWriter>> + Send + Sync + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.writer = Some(Box::new(writer));
|
self.writer = Some(Box::new(writer));
|
||||||
self
|
self
|
||||||
|
@ -222,7 +225,7 @@ impl AssetSourceBuilder {
|
||||||
/// Will use the given `reader` function to construct processed [`AssetReader`] instances.
|
/// Will use the given `reader` function to construct processed [`AssetReader`] instances.
|
||||||
pub fn with_processed_reader(
|
pub fn with_processed_reader(
|
||||||
mut self,
|
mut self,
|
||||||
reader: impl FnMut() -> Box<dyn AssetReader> + Send + Sync + 'static,
|
reader: impl FnMut() -> Box<dyn ErasedAssetReader> + Send + Sync + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.processed_reader = Some(Box::new(reader));
|
self.processed_reader = Some(Box::new(reader));
|
||||||
self
|
self
|
||||||
|
@ -231,7 +234,7 @@ impl AssetSourceBuilder {
|
||||||
/// Will use the given `writer` function to construct processed [`AssetWriter`] instances.
|
/// Will use the given `writer` function to construct processed [`AssetWriter`] instances.
|
||||||
pub fn with_processed_writer(
|
pub fn with_processed_writer(
|
||||||
mut self,
|
mut self,
|
||||||
writer: impl FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync + 'static,
|
writer: impl FnMut(bool) -> Option<Box<dyn ErasedAssetWriter>> + Send + Sync + 'static,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.processed_writer = Some(Box::new(writer));
|
self.processed_writer = Some(Box::new(writer));
|
||||||
self
|
self
|
||||||
|
@ -355,10 +358,10 @@ impl AssetSourceBuilders {
|
||||||
/// for a specific asset source, identified by an [`AssetSourceId`].
|
/// for a specific asset source, identified by an [`AssetSourceId`].
|
||||||
pub struct AssetSource {
|
pub struct AssetSource {
|
||||||
id: AssetSourceId<'static>,
|
id: AssetSourceId<'static>,
|
||||||
reader: Box<dyn AssetReader>,
|
reader: Box<dyn ErasedAssetReader>,
|
||||||
writer: Option<Box<dyn AssetWriter>>,
|
writer: Option<Box<dyn ErasedAssetWriter>>,
|
||||||
processed_reader: Option<Box<dyn AssetReader>>,
|
processed_reader: Option<Box<dyn ErasedAssetReader>>,
|
||||||
processed_writer: Option<Box<dyn AssetWriter>>,
|
processed_writer: Option<Box<dyn ErasedAssetWriter>>,
|
||||||
watcher: Option<Box<dyn AssetWatcher>>,
|
watcher: Option<Box<dyn AssetWatcher>>,
|
||||||
processed_watcher: Option<Box<dyn AssetWatcher>>,
|
processed_watcher: Option<Box<dyn AssetWatcher>>,
|
||||||
event_receiver: Option<crossbeam_channel::Receiver<AssetSourceEvent>>,
|
event_receiver: Option<crossbeam_channel::Receiver<AssetSourceEvent>>,
|
||||||
|
@ -379,13 +382,13 @@ impl AssetSource {
|
||||||
|
|
||||||
/// Return's this source's unprocessed [`AssetReader`].
|
/// Return's this source's unprocessed [`AssetReader`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn reader(&self) -> &dyn AssetReader {
|
pub fn reader(&self) -> &dyn ErasedAssetReader {
|
||||||
&*self.reader
|
&*self.reader
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return's this source's unprocessed [`AssetWriter`], if it exists.
|
/// Return's this source's unprocessed [`AssetWriter`], if it exists.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn writer(&self) -> Result<&dyn AssetWriter, MissingAssetWriterError> {
|
pub fn writer(&self) -> Result<&dyn ErasedAssetWriter, MissingAssetWriterError> {
|
||||||
self.writer
|
self.writer
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.ok_or_else(|| MissingAssetWriterError(self.id.clone_owned()))
|
.ok_or_else(|| MissingAssetWriterError(self.id.clone_owned()))
|
||||||
|
@ -393,7 +396,9 @@ impl AssetSource {
|
||||||
|
|
||||||
/// Return's this source's processed [`AssetReader`], if it exists.
|
/// Return's this source's processed [`AssetReader`], if it exists.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn processed_reader(&self) -> Result<&dyn AssetReader, MissingProcessedAssetReaderError> {
|
pub fn processed_reader(
|
||||||
|
&self,
|
||||||
|
) -> Result<&dyn ErasedAssetReader, MissingProcessedAssetReaderError> {
|
||||||
self.processed_reader
|
self.processed_reader
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.ok_or_else(|| MissingProcessedAssetReaderError(self.id.clone_owned()))
|
.ok_or_else(|| MissingProcessedAssetReaderError(self.id.clone_owned()))
|
||||||
|
@ -401,7 +406,9 @@ impl AssetSource {
|
||||||
|
|
||||||
/// Return's this source's processed [`AssetWriter`], if it exists.
|
/// Return's this source's processed [`AssetWriter`], if it exists.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn processed_writer(&self) -> Result<&dyn AssetWriter, MissingProcessedAssetWriterError> {
|
pub fn processed_writer(
|
||||||
|
&self,
|
||||||
|
) -> Result<&dyn ErasedAssetWriter, MissingProcessedAssetWriterError> {
|
||||||
self.processed_writer
|
self.processed_writer
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.ok_or_else(|| MissingProcessedAssetWriterError(self.id.clone_owned()))
|
.ok_or_else(|| MissingProcessedAssetWriterError(self.id.clone_owned()))
|
||||||
|
@ -429,7 +436,9 @@ impl AssetSource {
|
||||||
|
|
||||||
/// Returns a builder function for this platform's default [`AssetReader`]. `path` is the relative path to
|
/// Returns a builder function for this platform's default [`AssetReader`]. `path` is the relative path to
|
||||||
/// the asset root.
|
/// the asset root.
|
||||||
pub fn get_default_reader(_path: String) -> impl FnMut() -> Box<dyn AssetReader> + Send + Sync {
|
pub fn get_default_reader(
|
||||||
|
_path: String,
|
||||||
|
) -> impl FnMut() -> Box<dyn ErasedAssetReader> + Send + Sync {
|
||||||
move || {
|
move || {
|
||||||
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
|
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
|
||||||
return Box::new(super::file::FileAssetReader::new(&_path));
|
return Box::new(super::file::FileAssetReader::new(&_path));
|
||||||
|
@ -444,7 +453,7 @@ impl AssetSource {
|
||||||
/// the asset root. This will return [`None`] if this platform does not support writing assets by default.
|
/// the asset root. This will return [`None`] if this platform does not support writing assets by default.
|
||||||
pub fn get_default_writer(
|
pub fn get_default_writer(
|
||||||
_path: String,
|
_path: String,
|
||||||
) -> impl FnMut(bool) -> Option<Box<dyn AssetWriter>> + Send + Sync {
|
) -> impl FnMut(bool) -> Option<Box<dyn ErasedAssetWriter>> + Send + Sync {
|
||||||
move |_create_root: bool| {
|
move |_create_root: bool| {
|
||||||
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
|
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
|
||||||
return Some(Box::new(super::file::FileAssetWriter::new(
|
return Some(Box::new(super::file::FileAssetWriter::new(
|
||||||
|
|
|
@ -2,7 +2,6 @@ use crate::io::{
|
||||||
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
|
get_meta_path, AssetReader, AssetReaderError, EmptyPathStream, PathStream, Reader, VecReader,
|
||||||
};
|
};
|
||||||
use bevy_utils::tracing::error;
|
use bevy_utils::tracing::error;
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use js_sys::{Uint8Array, JSON};
|
use js_sys::{Uint8Array, JSON};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use wasm_bindgen::{JsCast, JsValue};
|
use wasm_bindgen::{JsCast, JsValue};
|
||||||
|
@ -59,40 +58,30 @@ impl HttpWasmAssetReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetReader for HttpWasmAssetReader {
|
impl AssetReader for HttpWasmAssetReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let path = self.root_path.join(path);
|
let path = self.root_path.join(path);
|
||||||
self.fetch_bytes(path).await
|
self.fetch_bytes(path).await
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
let meta_path = get_meta_path(&self.root_path.join(path));
|
let meta_path = get_meta_path(&self.root_path.join(path));
|
||||||
Ok(self.fetch_bytes(meta_path).await?)
|
Ok(self.fetch_bytes(meta_path).await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_path: &'a Path,
|
_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
let stream: Box<PathStream> = Box::new(EmptyPathStream);
|
let stream: Box<PathStream> = Box::new(EmptyPathStream);
|
||||||
error!("Reading directories is not supported with the HttpWasmAssetReader");
|
error!("Reading directories is not supported with the HttpWasmAssetReader");
|
||||||
Box::pin(async move { Ok(stream) })
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_path: &'a Path,
|
_path: &'a Path,
|
||||||
) -> BoxedFuture<'a, std::result::Result<bool, AssetReaderError>> {
|
) -> std::result::Result<bool, AssetReaderError> {
|
||||||
error!("Reading directories is not supported with the HttpWasmAssetReader");
|
error!("Reading directories is not supported with the HttpWasmAssetReader");
|
||||||
Box::pin(async move { Ok(false) })
|
Ok(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,8 +40,6 @@ pub use path::*;
|
||||||
pub use reflect::*;
|
pub use reflect::*;
|
||||||
pub use server::*;
|
pub use server::*;
|
||||||
|
|
||||||
pub use bevy_utils::BoxedFuture;
|
|
||||||
|
|
||||||
/// Rusty Object Notation, a crate used to serialize and deserialize bevy assets.
|
/// Rusty Object Notation, a crate used to serialize and deserialize bevy assets.
|
||||||
pub use ron;
|
pub use ron;
|
||||||
|
|
||||||
|
@ -448,7 +446,7 @@ mod tests {
|
||||||
};
|
};
|
||||||
use bevy_log::LogPlugin;
|
use bevy_log::LogPlugin;
|
||||||
use bevy_reflect::TypePath;
|
use bevy_reflect::TypePath;
|
||||||
use bevy_utils::{BoxedFuture, Duration, HashMap};
|
use bevy_utils::{Duration, HashMap};
|
||||||
use futures_lite::AsyncReadExt;
|
use futures_lite::AsyncReadExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{path::Path, sync::Arc};
|
use std::{path::Path, sync::Arc};
|
||||||
|
@ -497,13 +495,12 @@ mod tests {
|
||||||
|
|
||||||
type Error = CoolTextLoaderError;
|
type Error = CoolTextLoaderError;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
let mut ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
|
let mut ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
|
||||||
|
@ -531,7 +528,6 @@ mod tests {
|
||||||
.map(|text| load_context.add_labeled_asset(text.clone(), SubText { text }))
|
.map(|text| load_context.add_labeled_asset(text.clone(), SubText { text }))
|
||||||
.collect(),
|
.collect(),
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
@ -560,31 +556,25 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AssetReader for UnstableMemoryAssetReader {
|
impl AssetReader for UnstableMemoryAssetReader {
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
||||||
&'a self,
|
self.memory_reader.is_directory(path).await
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
|
||||||
self.memory_reader.is_directory(path)
|
|
||||||
}
|
}
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<bevy_asset::io::PathStream>, AssetReaderError>> {
|
) -> Result<Box<bevy_asset::io::PathStream>, AssetReaderError> {
|
||||||
self.memory_reader.read_directory(path)
|
self.memory_reader.read_directory(path).await
|
||||||
}
|
}
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<bevy_asset::io::Reader<'a>>, AssetReaderError>> {
|
) -> Result<Box<bevy_asset::io::Reader<'a>>, AssetReaderError> {
|
||||||
self.memory_reader.read_meta(path)
|
self.memory_reader.read_meta(path).await
|
||||||
}
|
}
|
||||||
fn read<'a>(
|
async fn read<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<
|
) -> Result<Box<bevy_asset::io::Reader<'a>>, bevy_asset::io::AssetReaderError> {
|
||||||
'a,
|
|
||||||
Result<Box<bevy_asset::io::Reader<'a>>, bevy_asset::io::AssetReaderError>,
|
|
||||||
> {
|
|
||||||
let attempt_number = {
|
let attempt_number = {
|
||||||
let mut attempt_counters = self.attempt_counters.lock().unwrap();
|
let mut attempt_counters = self.attempt_counters.lock().unwrap();
|
||||||
if let Some(existing) = attempt_counters.get_mut(path) {
|
if let Some(existing) = attempt_counters.get_mut(path) {
|
||||||
|
@ -605,13 +595,14 @@ mod tests {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
let wait = self.load_delay;
|
let wait = self.load_delay;
|
||||||
return Box::pin(async move {
|
return async move {
|
||||||
std::thread::sleep(wait);
|
std::thread::sleep(wait);
|
||||||
Err(AssetReaderError::Io(io_error.into()))
|
Err(AssetReaderError::Io(io_error.into()))
|
||||||
});
|
}
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.memory_reader.read(path)
|
self.memory_reader.read(path).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
UntypedAssetId, UntypedHandle,
|
UntypedAssetId, UntypedHandle,
|
||||||
};
|
};
|
||||||
use bevy_ecs::world::World;
|
use bevy_ecs::world::World;
|
||||||
use bevy_utils::{BoxedFuture, CowArc, HashMap, HashSet};
|
use bevy_utils::{BoxedFuture, ConditionalSendFuture, CowArc, HashMap, HashSet};
|
||||||
use downcast_rs::{impl_downcast, Downcast};
|
use downcast_rs::{impl_downcast, Downcast};
|
||||||
use futures_lite::AsyncReadExt;
|
use futures_lite::AsyncReadExt;
|
||||||
use ron::error::SpannedError;
|
use ron::error::SpannedError;
|
||||||
|
@ -35,7 +35,7 @@ pub trait AssetLoader: Send + Sync + 'static {
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader,
|
||||||
settings: &'a Self::Settings,
|
settings: &'a Self::Settings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>>;
|
) -> impl ConditionalSendFuture<Output = Result<Self::Asset, Self::Error>>;
|
||||||
|
|
||||||
/// Returns a list of extensions supported by this [`AssetLoader`], without the preceding dot.
|
/// Returns a list of extensions supported by this [`AssetLoader`], without the preceding dot.
|
||||||
/// Note that users of this [`AssetLoader`] may choose to load files with a non-matching extension.
|
/// Note that users of this [`AssetLoader`] may choose to load files with a non-matching extension.
|
||||||
|
|
|
@ -171,12 +171,12 @@ impl Process for () {
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type OutputLoader = ();
|
type OutputLoader = ();
|
||||||
|
|
||||||
fn process<'a>(
|
async fn process<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_context: &'a mut bevy_asset::processor::ProcessContext,
|
_context: &'a mut bevy_asset::processor::ProcessContext<'_>,
|
||||||
_meta: AssetMeta<(), Self>,
|
_meta: AssetMeta<(), Self>,
|
||||||
_writer: &'a mut bevy_asset::io::Writer,
|
_writer: &'a mut bevy_asset::io::Writer,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<(), bevy_asset::processor::ProcessError>> {
|
) -> Result<(), bevy_asset::processor::ProcessError> {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,12 +194,12 @@ impl AssetLoader for () {
|
||||||
type Asset = ();
|
type Asset = ();
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = std::io::Error;
|
type Error = std::io::Error;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_reader: &'a mut crate::io::Reader,
|
_reader: &'a mut crate::io::Reader<'_>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
_load_context: &'a mut crate::LoadContext,
|
_load_context: &'a mut crate::LoadContext<'_>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,9 @@ pub use process::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
io::{
|
io::{
|
||||||
AssetReader, AssetReaderError, AssetSource, AssetSourceBuilders, AssetSourceEvent,
|
AssetReaderError, AssetSource, AssetSourceBuilders, AssetSourceEvent, AssetSourceId,
|
||||||
AssetSourceId, AssetSources, AssetWriter, AssetWriterError, MissingAssetSourceError,
|
AssetSources, AssetWriterError, ErasedAssetReader, ErasedAssetWriter,
|
||||||
|
MissingAssetSourceError,
|
||||||
},
|
},
|
||||||
meta::{
|
meta::{
|
||||||
get_asset_hash, get_full_asset_hash, AssetAction, AssetActionMinimal, AssetHash, AssetMeta,
|
get_asset_hash, get_full_asset_hash, AssetAction, AssetActionMinimal, AssetHash, AssetMeta,
|
||||||
|
@ -30,6 +31,10 @@ use std::{
|
||||||
};
|
};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
// Needed for doc strings
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use crate::io::{AssetReader, AssetWriter};
|
||||||
|
|
||||||
/// A "background" asset processor that reads asset values from a source [`AssetSource`] (which corresponds to an [`AssetReader`] / [`AssetWriter`] pair),
|
/// A "background" asset processor that reads asset values from a source [`AssetSource`] (which corresponds to an [`AssetReader`] / [`AssetWriter`] pair),
|
||||||
/// processes them in some way, and writes them to a destination [`AssetSource`].
|
/// processes them in some way, and writes them to a destination [`AssetSource`].
|
||||||
///
|
///
|
||||||
|
@ -510,8 +515,8 @@ impl AssetProcessor {
|
||||||
/// Retrieves asset paths recursively. If `clean_empty_folders_writer` is Some, it will be used to clean up empty
|
/// Retrieves asset paths recursively. If `clean_empty_folders_writer` is Some, it will be used to clean up empty
|
||||||
/// folders when they are discovered.
|
/// folders when they are discovered.
|
||||||
fn get_asset_paths<'a>(
|
fn get_asset_paths<'a>(
|
||||||
reader: &'a dyn AssetReader,
|
reader: &'a dyn ErasedAssetReader,
|
||||||
clean_empty_folders_writer: Option<&'a dyn AssetWriter>,
|
clean_empty_folders_writer: Option<&'a dyn ErasedAssetWriter>,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
paths: &'a mut Vec<PathBuf>,
|
paths: &'a mut Vec<PathBuf>,
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::{
|
||||||
AssetLoadError, AssetLoader, AssetPath, DeserializeMetaError, ErasedLoadedAsset,
|
AssetLoadError, AssetLoader, AssetPath, DeserializeMetaError, ErasedLoadedAsset,
|
||||||
MissingAssetLoaderForExtensionError, MissingAssetLoaderForTypeNameError,
|
MissingAssetLoaderForExtensionError, MissingAssetLoaderForTypeNameError,
|
||||||
};
|
};
|
||||||
use bevy_utils::BoxedFuture;
|
use bevy_utils::{BoxedFuture, ConditionalSendFuture};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -32,7 +32,9 @@ pub trait Process: Send + Sync + Sized + 'static {
|
||||||
context: &'a mut ProcessContext,
|
context: &'a mut ProcessContext,
|
||||||
meta: AssetMeta<(), Self>,
|
meta: AssetMeta<(), Self>,
|
||||||
writer: &'a mut Writer,
|
writer: &'a mut Writer,
|
||||||
) -> BoxedFuture<'a, Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError>>;
|
) -> impl ConditionalSendFuture<
|
||||||
|
Output = Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError>,
|
||||||
|
>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A flexible [`Process`] implementation that loads the source [`Asset`] using the `L` [`AssetLoader`], then transforms
|
/// A flexible [`Process`] implementation that loads the source [`Asset`] using the `L` [`AssetLoader`], then transforms
|
||||||
|
@ -173,13 +175,12 @@ impl<
|
||||||
type Settings = LoadTransformAndSaveSettings<Loader::Settings, T::Settings, Saver::Settings>;
|
type Settings = LoadTransformAndSaveSettings<Loader::Settings, T::Settings, Saver::Settings>;
|
||||||
type OutputLoader = Saver::OutputLoader;
|
type OutputLoader = Saver::OutputLoader;
|
||||||
|
|
||||||
fn process<'a>(
|
async fn process<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
context: &'a mut ProcessContext,
|
context: &'a mut ProcessContext<'_>,
|
||||||
meta: AssetMeta<(), Self>,
|
meta: AssetMeta<(), Self>,
|
||||||
writer: &'a mut Writer,
|
writer: &'a mut Writer,
|
||||||
) -> BoxedFuture<'a, Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError>> {
|
) -> Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError> {
|
||||||
Box::pin(async move {
|
|
||||||
let AssetAction::Process { settings, .. } = meta.asset else {
|
let AssetAction::Process { settings, .. } = meta.asset else {
|
||||||
return Err(ProcessError::WrongMetaType);
|
return Err(ProcessError::WrongMetaType);
|
||||||
};
|
};
|
||||||
|
@ -198,8 +199,7 @@ impl<
|
||||||
.await
|
.await
|
||||||
.map_err(|err| ProcessError::AssetTransformError(err.into()))?;
|
.map_err(|err| ProcessError::AssetTransformError(err.into()))?;
|
||||||
|
|
||||||
let saved_asset =
|
let saved_asset = SavedAsset::<T::AssetOutput>::from_transformed(&post_transformed_asset);
|
||||||
SavedAsset::<T::AssetOutput>::from_transformed(&post_transformed_asset);
|
|
||||||
|
|
||||||
let output_settings = self
|
let output_settings = self
|
||||||
.saver
|
.saver
|
||||||
|
@ -207,7 +207,6 @@ impl<
|
||||||
.await
|
.await
|
||||||
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
|
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
|
||||||
Ok(output_settings)
|
Ok(output_settings)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,13 +216,12 @@ impl<Loader: AssetLoader, Saver: AssetSaver<Asset = Loader::Asset>> Process
|
||||||
type Settings = LoadAndSaveSettings<Loader::Settings, Saver::Settings>;
|
type Settings = LoadAndSaveSettings<Loader::Settings, Saver::Settings>;
|
||||||
type OutputLoader = Saver::OutputLoader;
|
type OutputLoader = Saver::OutputLoader;
|
||||||
|
|
||||||
fn process<'a>(
|
async fn process<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
context: &'a mut ProcessContext,
|
context: &'a mut ProcessContext<'_>,
|
||||||
meta: AssetMeta<(), Self>,
|
meta: AssetMeta<(), Self>,
|
||||||
writer: &'a mut Writer,
|
writer: &'a mut Writer,
|
||||||
) -> BoxedFuture<'a, Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError>> {
|
) -> Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError> {
|
||||||
Box::pin(async move {
|
|
||||||
let AssetAction::Process { settings, .. } = meta.asset else {
|
let AssetAction::Process { settings, .. } = meta.asset else {
|
||||||
return Err(ProcessError::WrongMetaType);
|
return Err(ProcessError::WrongMetaType);
|
||||||
};
|
};
|
||||||
|
@ -239,7 +237,6 @@ impl<Loader: AssetLoader, Saver: AssetSaver<Asset = Loader::Asset>> Process
|
||||||
.await
|
.await
|
||||||
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
|
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
|
||||||
Ok(output_settings)
|
Ok(output_settings)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::transformer::TransformedAsset;
|
use crate::transformer::TransformedAsset;
|
||||||
use crate::{io::Writer, meta::Settings, Asset, ErasedLoadedAsset};
|
use crate::{io::Writer, meta::Settings, Asset, ErasedLoadedAsset};
|
||||||
use crate::{AssetLoader, Handle, LabeledAsset, UntypedHandle};
|
use crate::{AssetLoader, Handle, LabeledAsset, UntypedHandle};
|
||||||
use bevy_utils::{BoxedFuture, CowArc, HashMap};
|
use bevy_utils::{BoxedFuture, ConditionalSendFuture, CowArc, HashMap};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{borrow::Borrow, hash::Hash, ops::Deref};
|
use std::{borrow::Borrow, hash::Hash, ops::Deref};
|
||||||
|
|
||||||
|
@ -24,7 +24,9 @@ pub trait AssetSaver: Send + Sync + 'static {
|
||||||
writer: &'a mut Writer,
|
writer: &'a mut Writer,
|
||||||
asset: SavedAsset<'a, Self::Asset>,
|
asset: SavedAsset<'a, Self::Asset>,
|
||||||
settings: &'a Self::Settings,
|
settings: &'a Self::Settings,
|
||||||
) -> BoxedFuture<'a, Result<<Self::OutputLoader as AssetLoader>::Settings, Self::Error>>;
|
) -> impl ConditionalSendFuture<
|
||||||
|
Output = Result<<Self::OutputLoader as AssetLoader>::Settings, Self::Error>,
|
||||||
|
>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A type-erased dynamic variant of [`AssetSaver`] that allows callers to save assets without knowing the actual type of the [`AssetSaver`].
|
/// A type-erased dynamic variant of [`AssetSaver`] that allows callers to save assets without knowing the actual type of the [`AssetSaver`].
|
||||||
|
|
|
@ -341,21 +341,19 @@ mod tests {
|
||||||
|
|
||||||
type Error = String;
|
type Error = String;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
_: &'a mut crate::io::Reader,
|
_: &'a mut crate::io::Reader<'_>,
|
||||||
_: &'a Self::Settings,
|
_: &'a Self::Settings,
|
||||||
_: &'a mut crate::LoadContext,
|
_: &'a mut crate::LoadContext<'_>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
self.sender.send(()).unwrap();
|
self.sender.send(()).unwrap();
|
||||||
|
|
||||||
Box::pin(async move {
|
|
||||||
Err(format!(
|
Err(format!(
|
||||||
"Loaded {}:{}",
|
"Loaded {}:{}",
|
||||||
std::any::type_name::<Self::Asset>(),
|
std::any::type_name::<Self::Asset>(),
|
||||||
N
|
N
|
||||||
))
|
))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -4,8 +4,8 @@ mod loaders;
|
||||||
use crate::{
|
use crate::{
|
||||||
folder::LoadedFolder,
|
folder::LoadedFolder,
|
||||||
io::{
|
io::{
|
||||||
AssetReader, AssetReaderError, AssetSource, AssetSourceEvent, AssetSourceId, AssetSources,
|
AssetReaderError, AssetSource, AssetSourceEvent, AssetSourceId, AssetSources,
|
||||||
MissingAssetSourceError, MissingProcessedAssetReaderError, Reader,
|
ErasedAssetReader, MissingAssetSourceError, MissingProcessedAssetReaderError, Reader,
|
||||||
},
|
},
|
||||||
loader::{AssetLoader, ErasedAssetLoader, LoadContext, LoadedAsset},
|
loader::{AssetLoader, ErasedAssetLoader, LoadContext, LoadedAsset},
|
||||||
meta::{
|
meta::{
|
||||||
|
@ -30,6 +30,10 @@ use std::path::PathBuf;
|
||||||
use std::{any::TypeId, path::Path, sync::Arc};
|
use std::{any::TypeId, path::Path, sync::Arc};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
// Needed for doc string
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use crate::io::{AssetReader, AssetWriter};
|
||||||
|
|
||||||
/// Loads and tracks the state of [`Asset`] values from a configured [`AssetReader`]. This can be used to kick off new asset loads and
|
/// Loads and tracks the state of [`Asset`] values from a configured [`AssetReader`]. This can be used to kick off new asset loads and
|
||||||
/// retrieve their current load states.
|
/// retrieve their current load states.
|
||||||
///
|
///
|
||||||
|
@ -657,7 +661,7 @@ impl AssetServer {
|
||||||
fn load_folder<'a>(
|
fn load_folder<'a>(
|
||||||
source: AssetSourceId<'static>,
|
source: AssetSourceId<'static>,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
reader: &'a dyn AssetReader,
|
reader: &'a dyn ErasedAssetReader,
|
||||||
server: &'a AssetServer,
|
server: &'a AssetServer,
|
||||||
handles: &'a mut Vec<UntypedHandle>,
|
handles: &'a mut Vec<UntypedHandle>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<(), AssetLoadError>> {
|
) -> bevy_utils::BoxedFuture<'a, Result<(), AssetLoadError>> {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{meta::Settings, Asset, ErasedLoadedAsset, Handle, LabeledAsset, UntypedHandle};
|
use crate::{meta::Settings, Asset, ErasedLoadedAsset, Handle, LabeledAsset, UntypedHandle};
|
||||||
use bevy_utils::{BoxedFuture, CowArc, HashMap};
|
use bevy_utils::{ConditionalSendFuture, CowArc, HashMap};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Borrow,
|
borrow::Borrow,
|
||||||
|
@ -25,7 +25,7 @@ pub trait AssetTransformer: Send + Sync + 'static {
|
||||||
&'a self,
|
&'a self,
|
||||||
asset: TransformedAsset<Self::AssetInput>,
|
asset: TransformedAsset<Self::AssetInput>,
|
||||||
settings: &'a Self::Settings,
|
settings: &'a Self::Settings,
|
||||||
) -> BoxedFuture<'a, Result<TransformedAsset<Self::AssetOutput>, Self::Error>>;
|
) -> impl ConditionalSendFuture<Output = Result<TransformedAsset<Self::AssetOutput>, Self::Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An [`Asset`] (and any "sub assets") intended to be transformed
|
/// An [`Asset`] (and any "sub assets") intended to be transformed
|
||||||
|
|
|
@ -3,7 +3,6 @@ use bevy_asset::{
|
||||||
Asset, AssetLoader, LoadContext,
|
Asset, AssetLoader, LoadContext,
|
||||||
};
|
};
|
||||||
use bevy_reflect::TypePath;
|
use bevy_reflect::TypePath;
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use std::{io::Cursor, sync::Arc};
|
use std::{io::Cursor, sync::Arc};
|
||||||
|
|
||||||
/// A source of audio data
|
/// A source of audio data
|
||||||
|
@ -43,19 +42,17 @@ impl AssetLoader for AudioLoader {
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = std::io::Error;
|
type Error = std::io::Error;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<AudioSource, Self::Error>> {
|
) -> Result<AudioSource, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
Ok(AudioSource {
|
Ok(AudioSource {
|
||||||
bytes: bytes.into(),
|
bytes: bytes.into(),
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -162,17 +162,15 @@ impl AssetLoader for GltfLoader {
|
||||||
type Asset = Gltf;
|
type Asset = Gltf;
|
||||||
type Settings = GltfLoaderSettings;
|
type Settings = GltfLoaderSettings;
|
||||||
type Error = GltfError;
|
type Error = GltfError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
settings: &'a GltfLoaderSettings,
|
settings: &'a GltfLoaderSettings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<Gltf, Self::Error>> {
|
) -> Result<Gltf, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
load_gltf(self, &bytes, load_context, settings).await
|
load_gltf(self, &bytes, load_context, settings).await
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -2,7 +2,7 @@ use super::ShaderDefVal;
|
||||||
use crate::define_atomic_id;
|
use crate::define_atomic_id;
|
||||||
use bevy_asset::{io::Reader, Asset, AssetLoader, AssetPath, Handle, LoadContext};
|
use bevy_asset::{io::Reader, Asset, AssetLoader, AssetPath, Handle, LoadContext};
|
||||||
use bevy_reflect::TypePath;
|
use bevy_reflect::TypePath;
|
||||||
use bevy_utils::{tracing::error, BoxedFuture};
|
use bevy_utils::tracing::error;
|
||||||
use futures_lite::AsyncReadExt;
|
use futures_lite::AsyncReadExt;
|
||||||
use std::{borrow::Cow, marker::Copy};
|
use std::{borrow::Cow, marker::Copy};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -259,13 +259,12 @@ impl AssetLoader for ShaderLoader {
|
||||||
type Asset = Shader;
|
type Asset = Shader;
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = ShaderLoaderError;
|
type Error = ShaderLoaderError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Shader, Self::Error>> {
|
) -> Result<Shader, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
||||||
let path = load_context.asset_path().to_string();
|
let path = load_context.asset_path().to_string();
|
||||||
// On windows, the path will inconsistently use \ or /.
|
// On windows, the path will inconsistently use \ or /.
|
||||||
|
@ -276,9 +275,7 @@ impl AssetLoader for ShaderLoader {
|
||||||
let mut shader = match ext {
|
let mut shader = match ext {
|
||||||
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
|
"spv" => Shader::from_spirv(bytes, load_context.path().to_string_lossy()),
|
||||||
"wgsl" => Shader::from_wgsl(String::from_utf8(bytes)?, path),
|
"wgsl" => Shader::from_wgsl(String::from_utf8(bytes)?, path),
|
||||||
"vert" => {
|
"vert" => Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Vertex, path),
|
||||||
Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Vertex, path)
|
|
||||||
}
|
|
||||||
"frag" => {
|
"frag" => {
|
||||||
Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Fragment, path)
|
Shader::from_glsl(String::from_utf8(bytes)?, naga::ShaderStage::Fragment, path)
|
||||||
}
|
}
|
||||||
|
@ -295,7 +292,6 @@ impl AssetLoader for ShaderLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(shader)
|
Ok(shader)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::texture::{Image, ImageFormat, ImageFormatSetting, ImageLoader, ImageLoaderSettings};
|
use crate::texture::{Image, ImageFormat, ImageFormatSetting, ImageLoader, ImageLoaderSettings};
|
||||||
use bevy_asset::saver::{AssetSaver, SavedAsset};
|
use bevy_asset::saver::{AssetSaver, SavedAsset};
|
||||||
use futures_lite::{AsyncWriteExt, FutureExt};
|
use futures_lite::AsyncWriteExt;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub struct CompressedImageSaver;
|
pub struct CompressedImageSaver;
|
||||||
|
@ -19,17 +19,18 @@ impl AssetSaver for CompressedImageSaver {
|
||||||
type OutputLoader = ImageLoader;
|
type OutputLoader = ImageLoader;
|
||||||
type Error = CompressedImageSaverError;
|
type Error = CompressedImageSaverError;
|
||||||
|
|
||||||
fn save<'a>(
|
async fn save<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
writer: &'a mut bevy_asset::io::Writer,
|
writer: &'a mut bevy_asset::io::Writer,
|
||||||
image: SavedAsset<'a, Self::Asset>,
|
image: SavedAsset<'a, Self::Asset>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<ImageLoaderSettings, Self::Error>> {
|
) -> Result<ImageLoaderSettings, Self::Error> {
|
||||||
// PERF: this should live inside the future, but CompressorParams and Compressor are not Send / can't be owned by the BoxedFuture (which _is_ Send)
|
let is_srgb = image.texture_descriptor.format.is_srgb();
|
||||||
|
|
||||||
|
let compressed_basis_data = {
|
||||||
let mut compressor_params = basis_universal::CompressorParams::new();
|
let mut compressor_params = basis_universal::CompressorParams::new();
|
||||||
compressor_params.set_basis_format(basis_universal::BasisTextureFormat::UASTC4x4);
|
compressor_params.set_basis_format(basis_universal::BasisTextureFormat::UASTC4x4);
|
||||||
compressor_params.set_generate_mipmaps(true);
|
compressor_params.set_generate_mipmaps(true);
|
||||||
let is_srgb = image.texture_descriptor.format.is_srgb();
|
|
||||||
let color_space = if is_srgb {
|
let color_space = if is_srgb {
|
||||||
basis_universal::ColorSpace::Srgb
|
basis_universal::ColorSpace::Srgb
|
||||||
} else {
|
} else {
|
||||||
|
@ -49,8 +50,9 @@ impl AssetSaver for CompressedImageSaver {
|
||||||
compressor.init(&compressor_params);
|
compressor.init(&compressor_params);
|
||||||
compressor.process().unwrap();
|
compressor.process().unwrap();
|
||||||
}
|
}
|
||||||
let compressed_basis_data = compressor.basis_file().to_vec();
|
compressor.basis_file().to_vec()
|
||||||
async move {
|
};
|
||||||
|
|
||||||
writer.write_all(&compressed_basis_data).await?;
|
writer.write_all(&compressed_basis_data).await?;
|
||||||
Ok(ImageLoaderSettings {
|
Ok(ImageLoaderSettings {
|
||||||
format: ImageFormatSetting::Format(ImageFormat::Basis),
|
format: ImageFormatSetting::Format(ImageFormat::Basis),
|
||||||
|
@ -59,6 +61,4 @@ impl AssetSaver for CompressedImageSaver {
|
||||||
asset_usage: image.asset_usage,
|
asset_usage: image.asset_usage,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
.boxed()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ use bevy_asset::{
|
||||||
io::{AsyncReadExt, Reader},
|
io::{AsyncReadExt, Reader},
|
||||||
AssetLoader, LoadContext,
|
AssetLoader, LoadContext,
|
||||||
};
|
};
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
use image::ImageDecoder;
|
use image::ImageDecoder;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -36,13 +35,12 @@ impl AssetLoader for ExrTextureLoader {
|
||||||
type Settings = ExrTextureLoaderSettings;
|
type Settings = ExrTextureLoaderSettings;
|
||||||
type Error = ExrTextureLoaderError;
|
type Error = ExrTextureLoaderError;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
settings: &'a Self::Settings,
|
settings: &'a Self::Settings,
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Image, Self::Error>> {
|
) -> Result<Image, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let format = TextureFormat::Rgba32Float;
|
let format = TextureFormat::Rgba32Float;
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
format.pixel_size(),
|
format.pixel_size(),
|
||||||
|
@ -74,7 +72,6 @@ impl AssetLoader for ExrTextureLoader {
|
||||||
format,
|
format,
|
||||||
settings.asset_usage,
|
settings.asset_usage,
|
||||||
))
|
))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -29,13 +29,12 @@ impl AssetLoader for HdrTextureLoader {
|
||||||
type Asset = Image;
|
type Asset = Image;
|
||||||
type Settings = HdrTextureLoaderSettings;
|
type Settings = HdrTextureLoaderSettings;
|
||||||
type Error = HdrTextureLoaderError;
|
type Error = HdrTextureLoaderError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
settings: &'a Self::Settings,
|
settings: &'a Self::Settings,
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<Image, Self::Error>> {
|
) -> Result<Image, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let format = TextureFormat::Rgba32Float;
|
let format = TextureFormat::Rgba32Float;
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
format.pixel_size(),
|
format.pixel_size(),
|
||||||
|
@ -70,7 +69,6 @@ impl AssetLoader for HdrTextureLoader {
|
||||||
format,
|
format,
|
||||||
settings.asset_usage,
|
settings.asset_usage,
|
||||||
))
|
))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -85,13 +85,12 @@ impl AssetLoader for ImageLoader {
|
||||||
type Asset = Image;
|
type Asset = Image;
|
||||||
type Settings = ImageLoaderSettings;
|
type Settings = ImageLoaderSettings;
|
||||||
type Error = ImageLoaderError;
|
type Error = ImageLoaderError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
settings: &'a ImageLoaderSettings,
|
settings: &'a ImageLoaderSettings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<Image, Self::Error>> {
|
) -> Result<Image, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
// use the file extension for the image type
|
// use the file extension for the image type
|
||||||
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
|
@ -115,7 +114,6 @@ impl AssetLoader for ImageLoader {
|
||||||
error: err,
|
error: err,
|
||||||
path: format!("{}", load_context.path().display()),
|
path: format!("{}", load_context.path().display()),
|
||||||
})?)
|
})?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -6,7 +6,6 @@ use bevy_asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext};
|
||||||
use bevy_ecs::reflect::AppTypeRegistry;
|
use bevy_ecs::reflect::AppTypeRegistry;
|
||||||
use bevy_ecs::world::{FromWorld, World};
|
use bevy_ecs::world::{FromWorld, World};
|
||||||
use bevy_reflect::TypeRegistryArc;
|
use bevy_reflect::TypeRegistryArc;
|
||||||
use bevy_utils::BoxedFuture;
|
|
||||||
#[cfg(feature = "serialize")]
|
#[cfg(feature = "serialize")]
|
||||||
use serde::de::DeserializeSeed;
|
use serde::de::DeserializeSeed;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -44,13 +43,12 @@ impl AssetLoader for SceneLoader {
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = SceneLoaderError;
|
type Error = SceneLoaderError;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a (),
|
_settings: &'a (),
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
||||||
|
@ -60,7 +58,6 @@ impl AssetLoader for SceneLoader {
|
||||||
Ok(scene_deserializer
|
Ok(scene_deserializer
|
||||||
.deserialize(&mut deserializer)
|
.deserialize(&mut deserializer)
|
||||||
.map_err(|e| deserializer.span_error(e))?)
|
.map_err(|e| deserializer.span_error(e))?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -21,17 +21,15 @@ impl AssetLoader for FontLoader {
|
||||||
type Asset = Font;
|
type Asset = Font;
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = FontLoaderError;
|
type Error = FontLoaderError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a (),
|
_settings: &'a (),
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> bevy_utils::BoxedFuture<'a, Result<Font, Self::Error>> {
|
) -> Result<Font, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
Ok(Font::try_from_bytes(bytes)?)
|
Ok(Font::try_from_bytes(bytes)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -36,21 +36,36 @@ use hashbrown::hash_map::RawEntryMut;
|
||||||
use std::{
|
use std::{
|
||||||
any::TypeId,
|
any::TypeId,
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
future::Future,
|
|
||||||
hash::{BuildHasher, BuildHasherDefault, Hash, Hasher},
|
hash::{BuildHasher, BuildHasherDefault, Hash, Hasher},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
mem::ManuallyDrop,
|
mem::ManuallyDrop,
|
||||||
ops::Deref,
|
ops::Deref,
|
||||||
pin::Pin,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An owned and dynamically typed Future used when you can't statically type your result or need to add some indirection.
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
|
mod conditional_send {
|
||||||
|
/// Use [`ConditionalSend`] to mark an optional Send trait bound. Useful as on certain platforms (eg. WASM),
|
||||||
|
/// futures aren't Send.
|
||||||
|
pub trait ConditionalSend: Send {}
|
||||||
|
impl<T: Send> ConditionalSend for T {}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
|
#[allow(missing_docs)]
|
||||||
|
mod conditional_send {
|
||||||
|
pub trait ConditionalSend {}
|
||||||
|
impl<T> ConditionalSend for T {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use conditional_send::*;
|
||||||
|
|
||||||
|
/// Use [`ConditionalSendFuture`] for a future with an optional Send trait bound, as on certain platforms (eg. WASM),
|
||||||
|
/// futures aren't Send.
|
||||||
|
pub trait ConditionalSendFuture: std::future::Future + ConditionalSend {}
|
||||||
|
impl<T: std::future::Future + ConditionalSend> ConditionalSendFuture for T {}
|
||||||
|
|
||||||
|
/// An owned and dynamically typed Future used when you can't statically type your result or need to add some indirection.
|
||||||
|
pub type BoxedFuture<'a, T> = std::pin::Pin<Box<dyn ConditionalSendFuture<Output = T> + 'a>>;
|
||||||
|
|
||||||
/// A shortcut alias for [`hashbrown::hash_map::Entry`].
|
/// A shortcut alias for [`hashbrown::hash_map::Entry`].
|
||||||
pub type Entry<'a, K, V, S = BuildHasherDefault<AHasher>> = hashbrown::hash_map::Entry<'a, K, V, S>;
|
pub type Entry<'a, K, V, S = BuildHasherDefault<AHasher>> = hashbrown::hash_map::Entry<'a, K, V, S>;
|
||||||
|
|
|
@ -7,7 +7,6 @@ use bevy::{
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
reflect::TypePath,
|
reflect::TypePath,
|
||||||
utils::BoxedFuture,
|
|
||||||
};
|
};
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
@ -41,13 +40,12 @@ impl AssetLoader for GzAssetLoader {
|
||||||
type Asset = GzAsset;
|
type Asset = GzAsset;
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = GzAssetLoaderError;
|
type Error = GzAssetLoaderError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a (),
|
_settings: &'a (),
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let compressed_path = load_context.path();
|
let compressed_path = load_context.path();
|
||||||
let file_name = compressed_path
|
let file_name = compressed_path
|
||||||
.file_name()
|
.file_name()
|
||||||
|
@ -78,7 +76,6 @@ impl AssetLoader for GzAssetLoader {
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(GzAsset { uncompressed })
|
Ok(GzAsset { uncompressed })
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
|
|
@ -4,7 +4,6 @@ use bevy::{
|
||||||
asset::{io::Reader, ron, AssetLoader, AsyncReadExt, LoadContext},
|
asset::{io::Reader, ron, AssetLoader, AsyncReadExt, LoadContext},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
reflect::TypePath,
|
reflect::TypePath,
|
||||||
utils::BoxedFuture,
|
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -34,18 +33,16 @@ impl AssetLoader for CustomAssetLoader {
|
||||||
type Asset = CustomAsset;
|
type Asset = CustomAsset;
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = CustomAssetLoaderError;
|
type Error = CustomAssetLoaderError;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a (),
|
_settings: &'a (),
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
let custom_asset = ron::de::from_bytes::<CustomAsset>(&bytes)?;
|
let custom_asset = ron::de::from_bytes::<CustomAsset>(&bytes)?;
|
||||||
Ok(custom_asset)
|
Ok(custom_asset)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
@ -75,19 +72,17 @@ impl AssetLoader for BlobAssetLoader {
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = BlobAssetLoaderError;
|
type Error = BlobAssetLoaderError;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a (),
|
_settings: &'a (),
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
info!("Loading Blob...");
|
info!("Loading Blob...");
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
|
|
||||||
Ok(Blob { bytes })
|
Ok(Blob { bytes })
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,42 +3,35 @@
|
||||||
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
|
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
asset::io::{AssetReader, AssetReaderError, AssetSource, AssetSourceId, PathStream, Reader},
|
asset::io::{
|
||||||
|
AssetReader, AssetReaderError, AssetSource, AssetSourceId, ErasedAssetReader, PathStream,
|
||||||
|
Reader,
|
||||||
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
utils::BoxedFuture,
|
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
/// A custom asset reader implementation that wraps a given asset reader implementation
|
/// A custom asset reader implementation that wraps a given asset reader implementation
|
||||||
struct CustomAssetReader(Box<dyn AssetReader>);
|
struct CustomAssetReader(Box<dyn ErasedAssetReader>);
|
||||||
|
|
||||||
impl AssetReader for CustomAssetReader {
|
impl AssetReader for CustomAssetReader {
|
||||||
fn read<'a>(
|
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
info!("Reading {:?}", path);
|
info!("Reading {:?}", path);
|
||||||
self.0.read(path)
|
self.0.read(path).await
|
||||||
}
|
}
|
||||||
fn read_meta<'a>(
|
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
&'a self,
|
self.0.read_meta(path).await
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
|
|
||||||
self.0.read_meta(path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_directory<'a>(
|
async fn read_directory<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
path: &'a Path,
|
path: &'a Path,
|
||||||
) -> BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
|
) -> Result<Box<PathStream>, AssetReaderError> {
|
||||||
self.0.read_directory(path)
|
self.0.read_directory(path).await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_directory<'a>(
|
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
|
||||||
&'a self,
|
self.0.is_directory(path).await
|
||||||
path: &'a Path,
|
|
||||||
) -> BoxedFuture<'a, Result<bool, AssetReaderError>> {
|
|
||||||
self.0.is_directory(path)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ use bevy::{
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
reflect::TypePath,
|
reflect::TypePath,
|
||||||
utils::BoxedFuture,
|
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
@ -83,13 +82,12 @@ impl AssetLoader for TextLoader {
|
||||||
type Asset = Text;
|
type Asset = Text;
|
||||||
type Settings = TextSettings;
|
type Settings = TextSettings;
|
||||||
type Error = std::io::Error;
|
type Error = std::io::Error;
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
settings: &'a TextSettings,
|
settings: &'a TextSettings,
|
||||||
_load_context: &'a mut LoadContext,
|
_load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<Text, Self::Error>> {
|
) -> Result<Text, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
let value = if let Some(ref text) = settings.text_override {
|
let value = if let Some(ref text) = settings.text_override {
|
||||||
|
@ -98,7 +96,6 @@ impl AssetLoader for TextLoader {
|
||||||
String::from_utf8(bytes).unwrap()
|
String::from_utf8(bytes).unwrap()
|
||||||
};
|
};
|
||||||
Ok(Text(value))
|
Ok(Text(value))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
@ -138,13 +135,12 @@ impl AssetLoader for CoolTextLoader {
|
||||||
type Settings = ();
|
type Settings = ();
|
||||||
type Error = CoolTextLoaderError;
|
type Error = CoolTextLoaderError;
|
||||||
|
|
||||||
fn load<'a>(
|
async fn load<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
reader: &'a mut Reader,
|
reader: &'a mut Reader<'_>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
load_context: &'a mut LoadContext,
|
load_context: &'a mut LoadContext<'_>,
|
||||||
) -> BoxedFuture<'a, Result<CoolText, Self::Error>> {
|
) -> Result<CoolText, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
reader.read_to_end(&mut bytes).await?;
|
reader.read_to_end(&mut bytes).await?;
|
||||||
let ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
|
let ron: CoolTextRon = ron::de::from_bytes(&bytes)?;
|
||||||
|
@ -162,7 +158,6 @@ impl AssetLoader for CoolTextLoader {
|
||||||
.map(|p| load_context.load(p))
|
.map(|p| load_context.load(p))
|
||||||
.collect(),
|
.collect(),
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
@ -184,15 +179,13 @@ impl AssetTransformer for CoolTextTransformer {
|
||||||
type Settings = CoolTextTransformerSettings;
|
type Settings = CoolTextTransformerSettings;
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
|
|
||||||
fn transform<'a>(
|
async fn transform<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
mut asset: TransformedAsset<Self::AssetInput>,
|
mut asset: TransformedAsset<Self::AssetInput>,
|
||||||
settings: &'a Self::Settings,
|
settings: &'a Self::Settings,
|
||||||
) -> BoxedFuture<'a, Result<TransformedAsset<Self::AssetOutput>, Self::Error>> {
|
) -> Result<TransformedAsset<Self::AssetOutput>, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
asset.text = format!("{}{}", asset.text, settings.appended);
|
asset.text = format!("{}{}", asset.text, settings.appended);
|
||||||
Ok(asset)
|
Ok(asset)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,16 +197,14 @@ impl AssetSaver for CoolTextSaver {
|
||||||
type OutputLoader = TextLoader;
|
type OutputLoader = TextLoader;
|
||||||
type Error = std::io::Error;
|
type Error = std::io::Error;
|
||||||
|
|
||||||
fn save<'a>(
|
async fn save<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
writer: &'a mut Writer,
|
writer: &'a mut Writer,
|
||||||
asset: SavedAsset<'a, Self::Asset>,
|
asset: SavedAsset<'a, Self::Asset>,
|
||||||
_settings: &'a Self::Settings,
|
_settings: &'a Self::Settings,
|
||||||
) -> BoxedFuture<'a, Result<TextSettings, Self::Error>> {
|
) -> Result<TextSettings, Self::Error> {
|
||||||
Box::pin(async move {
|
|
||||||
writer.write_all(asset.text.as_bytes()).await?;
|
writer.write_all(asset.text.as_bytes()).await?;
|
||||||
Ok(TextSettings::default())
|
Ok(TextSettings::default())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue