use bevy::asset::io::{
AssetReader, AssetReaderError, AssetSourceBuilder, AssetSourceId, PathStream, Reader, VecReader,
};
use bevy::prelude::*;
use include_dir::{Dir, include_dir};
use std::path::Path;
static ASSETS: Dir = include_dir!("$CARGO_MANIFEST_DIR/assets");
struct EmbeddedAssets;
impl AssetReader for EmbeddedAssets {
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
ASSETS
.get_file(path)
.map(|file| VecReader::new(file.contents().to_vec()))
.ok_or_else(|| AssetReaderError::NotFound(path.to_path_buf()))
}
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
Err::<VecReader, _>(AssetReaderError::NotFound(path.to_path_buf()))
}
async fn read_directory<'a>(
&'a self,
path: &'a Path,
) -> Result<Box<PathStream>, AssetReaderError> {
Err(AssetReaderError::NotFound(path.to_path_buf()))
}
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
Ok(ASSETS.get_dir(path).is_some())
}
}
pub(super) fn register(app: &mut App) {
app.register_asset_source(
AssetSourceId::Default,
AssetSourceBuilder::new(|| Box::new(EmbeddedAssets)),
);
}