2020-10-29 00:08:33 +00:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
mod android_asset_io;
|
|
|
|
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
|
2020-10-20 00:29:31 +00:00
|
|
|
mod file_asset_io;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
mod wasm_asset_io;
|
|
|
|
|
2020-10-29 00:08:33 +00:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
pub use android_asset_io::*;
|
|
|
|
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
|
2020-10-20 00:29:31 +00:00
|
|
|
pub use file_asset_io::*;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub use wasm_asset_io::*;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2020-10-21 22:55:15 +00:00
|
|
|
use bevy_ecs::bevy_utils::BoxedFuture;
|
2020-10-20 00:29:31 +00:00
|
|
|
use downcast_rs::{impl_downcast, Downcast};
|
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
/// Errors that occur while loading assets
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum AssetIoError {
|
2021-01-23 21:23:16 +00:00
|
|
|
#[error("path not found: {0}")]
|
2020-10-20 00:29:31 +00:00
|
|
|
NotFound(PathBuf),
|
2021-01-23 21:23:16 +00:00
|
|
|
#[error("encountered an io error while loading asset: {0}")]
|
2020-10-20 00:29:31 +00:00
|
|
|
Io(#[from] io::Error),
|
2021-01-23 21:23:16 +00:00
|
|
|
#[error("failed to watch path: {0}")]
|
2020-10-20 00:29:31 +00:00
|
|
|
PathWatchError(PathBuf),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles load requests from an AssetServer
|
|
|
|
pub trait AssetIo: Downcast + Send + Sync + 'static {
|
2020-10-21 22:55:15 +00:00
|
|
|
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>>;
|
2020-10-20 00:29:31 +00:00
|
|
|
fn read_directory(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError>;
|
|
|
|
fn is_directory(&self, path: &Path) -> bool;
|
|
|
|
fn watch_path_for_changes(&self, path: &Path) -> Result<(), AssetIoError>;
|
|
|
|
fn watch_for_changes(&self) -> Result<(), AssetIoError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_downcast!(AssetIo);
|