Skip to content

Commit

Permalink
update for Bevy 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jun 8, 2024
1 parent 3cd44fc commit af98623
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 118 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_embedded_assets"
version = "0.10.2"
version = "0.11.0"
authors = ["François Mockers <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -18,13 +18,14 @@ default = ["default-source"]
default-source = ["futures-io", "futures-lite"]

[dependencies.bevy]
version = "0.13"
version = "0.14.0-rc.2"
default-features = false
features = ["bevy_asset"]

[dependencies]
futures-io = { version = "0.3", optional = true }
futures-lite = { version = "2.0", optional = true }
thiserror = "1.0"

[build-dependencies]
cargo-emit = "0.2.1"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() {
|Bevy|bevy_embedded_assets|
|---|---|
|main|main|
|0.14|0.11|
|0.13|0.10|
|0.12|0.9|
|0.11|0.8|
Expand Down
81 changes: 43 additions & 38 deletions src/asset_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use std::{
};

use bevy::{
asset::io::{AssetReader, AssetReaderError, PathStream, Reader},
asset::io::{AssetReader, AssetReaderError, ErasedAssetReader, PathStream, Reader},
utils::HashMap,
};
use futures_io::AsyncRead;
use futures_io::{AsyncRead, AsyncSeek};
use futures_lite::Stream;
use thiserror::Error;

use crate::{include_all_assets, EmbeddedRegistry};

Expand All @@ -36,7 +37,7 @@ use crate::{include_all_assets, EmbeddedRegistry};
#[allow(clippy::module_name_repetitions)]
pub struct EmbeddedAssetReader {
loaded: HashMap<&'static Path, &'static [u8]>,
fallback: Option<Box<dyn AssetReader>>,
fallback: Option<Box<dyn ErasedAssetReader>>,
}

impl std::fmt::Debug for EmbeddedAssetReader {
Expand Down Expand Up @@ -86,7 +87,7 @@ impl EmbeddedAssetReader {
/// Create an [`EmbeddedAssetReader`] loaded with all the assets found by the build script.
#[must_use]
pub(crate) fn preloaded_with_default(
mut default: impl FnMut() -> Box<dyn AssetReader> + Send + Sync + 'static,
mut default: impl FnMut() -> Box<dyn ErasedAssetReader> + Send + Sync + 'static,
) -> Self {
let mut new = Self {
loaded: HashMap::default(),
Expand Down Expand Up @@ -157,6 +158,25 @@ impl AsyncRead for DataReader {
}
}

impl AsyncSeek for DataReader {
fn poll_seek(
self: Pin<&mut Self>,
_: &mut std::task::Context<'_>,
_pos: futures_io::SeekFrom,
) -> Poll<futures_io::Result<u64>> {
Poll::Ready(Err(futures_io::Error::new(
futures_io::ErrorKind::Other,
EmbeddedDataReaderError::SeekNotSupported,
)))
}
}

#[derive(Error, Debug)]
enum EmbeddedDataReaderError {
#[error("Seek is not supported when embeded")]
SeekNotSupported,
}

struct DirReader(Vec<PathBuf>);

impl Stream for DirReader {
Expand All @@ -183,60 +203,45 @@ pub(crate) fn get_meta_path(path: &Path) -> PathBuf {
}

impl AssetReader for EmbeddedAssetReader {
fn read<'a>(
&'a self,
path: &'a Path,
) -> bevy::utils::BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
if self.has_file_sync(path) {
Box::pin(async move {
self.load_path_sync(path).map(|reader| {
let boxed: Box<Reader> = Box::new(reader);
boxed
})
self.load_path_sync(path).map(|reader| {
let boxed: Box<Reader> = Box::new(reader);
boxed
})
} else if let Some(fallback) = self.fallback.as_ref() {
fallback.read(path)
fallback.read(path).await
} else {
Box::pin(async move { Err(AssetReaderError::NotFound(path.to_path_buf())) })
Err(AssetReaderError::NotFound(path.to_path_buf()))
}
}

fn read_meta<'a>(
&'a self,
path: &'a Path,
) -> bevy::utils::BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
let meta_path = get_meta_path(path);
if self.has_file_sync(&meta_path) {
Box::pin(async move {
self.load_path_sync(&meta_path).map(|reader| {
let boxed: Box<Reader> = Box::new(reader);
boxed
})
self.load_path_sync(&meta_path).map(|reader| {
let boxed: Box<Reader> = Box::new(reader);
boxed
})
} else if let Some(fallback) = self.fallback.as_ref() {
fallback.read_meta(path)
fallback.read_meta(path).await
} else {
Box::pin(async move { Err(AssetReaderError::NotFound(meta_path)) })
Err(AssetReaderError::NotFound(meta_path))
}
}

fn read_directory<'a>(
async fn read_directory<'a>(
&'a self,
path: &'a Path,
) -> bevy::utils::BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
Box::pin(async move {
self.read_directory_sync(path).map(|read_dir| {
let boxed: Box<PathStream> = Box::new(read_dir);
boxed
})
) -> Result<Box<PathStream>, AssetReaderError> {
self.read_directory_sync(path).map(|read_dir| {
let boxed: Box<PathStream> = Box::new(read_dir);
boxed
})
}

fn is_directory<'a>(
&'a self,
path: &'a Path,
) -> bevy::utils::BoxedFuture<'a, Result<bool, AssetReaderError>> {
Box::pin(async move { Ok(self.is_directory_sync(path)) })
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
Ok(self.is_directory_sync(path))
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ include!(concat!(env!("OUT_DIR"), "/include_all_assets.rs"));
/// # let mut app = App::new();
/// app.add_plugins((EmbeddedAssetPlugin::default(), DefaultPlugins));
/// # app.init_asset::<MyAsset>();
/// # let asset_server: Mut<'_, AssetServer> = app.world.resource_mut::<AssetServer>();
/// # let asset_server: Mut<'_, AssetServer> = app.world_mut().resource_mut::<AssetServer>();
/// let handle: Handle<MyAsset> = asset_server.load("embedded://example_asset.test");
/// # }
/// ```
Expand All @@ -69,7 +69,7 @@ include!(concat!(env!("OUT_DIR"), "/include_all_assets.rs"));
/// # let mut app = App::new();
/// app.add_plugins((EmbeddedAssetPlugin { mode: PluginMode::ReplaceDefault }, DefaultPlugins));
/// # app.init_asset::<MyAsset>();
/// # let asset_server: Mut<'_, AssetServer> = app.world.resource_mut::<AssetServer>();
/// # let asset_server: Mut<'_, AssetServer> = app.world_mut().resource_mut::<AssetServer>();
/// let handle: Handle<MyAsset> = asset_server.load("example_asset.test");
/// # }
/// ```
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Plugin for EmbeddedAssetPlugin {
match &self.mode {
PluginMode::AutoLoad => {
if app.is_plugin_added::<AssetPlugin>() {
let mut registry = app.world.resource_mut::<EmbeddedAssetRegistry>();
let mut registry = app.world_mut().resource_mut::<EmbeddedAssetRegistry>();
include_all_assets(registry.as_mut());
app.init_resource::<AllTheEmbedded>();
}
Expand Down Expand Up @@ -165,9 +165,12 @@ impl Plugin for EmbeddedAssetPlugin {

fn finish(&self, app: &mut App) {
if matches!(self.mode, PluginMode::AutoLoad)
&& app.world.remove_resource::<AllTheEmbedded>().is_none()
&& app
.world_mut()
.remove_resource::<AllTheEmbedded>()
.is_none()
{
let mut registry = app.world.resource_mut::<EmbeddedAssetRegistry>();
let mut registry = app.world_mut().resource_mut::<EmbeddedAssetRegistry>();
include_all_assets(registry.as_mut());
}
}
Expand Down
51 changes: 25 additions & 26 deletions tests/as_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use std::fmt::Display;

use bevy::{prelude::*, utils::thiserror::Error};
use bevy::prelude::*;
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
use thiserror::Error;

#[derive(Asset, TypePath, Debug)]
pub struct TestAsset {
Expand All @@ -26,21 +27,19 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
type Asset = TestAsset;
type Settings = ();
type Error = TestError;
fn load<'a>(
async fn load<'a>(
&'a self,
reader: &'a mut bevy::asset::io::Reader,
reader: &'a mut bevy::asset::io::Reader<'_>,
_: &'a (),
_: &'a mut bevy::asset::LoadContext,
) -> bevy::utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
.await
.unwrap();
_: &'a mut bevy::asset::LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
.await
.unwrap();

Ok(TestAsset {
value: String::from_utf8(bytes).unwrap(),
})
Ok(TestAsset {
value: String::from_utf8(bytes).unwrap(),
})
}

Expand All @@ -63,17 +62,17 @@ fn work_with_embedded_source_plugin_before() {
.init_asset_loader::<TestAssetLoader>();
app.finish();

let asset_server = app.world.resource_mut::<AssetServer>();
let asset_server = app.world_mut().resource_mut::<AssetServer>();
let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
let handle_2: Handle<TestAsset> = asset_server.load("açèt.test");
let handle_3: Handle<TestAsset> = asset_server.load("subdir/other_asset.test");
app.update();
let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
let asset = test_assets.get(handle_1).unwrap();
let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
let asset = test_assets.get(&handle_1).unwrap();
assert_eq!(asset.value, "hello");
let asset = test_assets.get(handle_2).unwrap();
let asset = test_assets.get(&handle_2).unwrap();
assert_eq!(asset.value, "with special chars");
let asset = test_assets.get(handle_3).unwrap();
let asset = test_assets.get(&handle_3).unwrap();
assert_eq!(asset.value, "in subdirectory");
}

Expand All @@ -92,11 +91,11 @@ fn work_with_embedded_source_plugin_after() {
.init_asset_loader::<TestAssetLoader>();
app.finish();

let asset_server = app.world.resource_mut::<AssetServer>();
let asset_server = app.world_mut().resource_mut::<AssetServer>();
let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
app.update();
let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
test_assets.get(handle_1).unwrap();
let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
test_assets.get(&handle_1).unwrap();
}

// #[test]
Expand All @@ -108,16 +107,16 @@ fn work_with_embedded_source_plugin_after() {
// .init_asset_loader::<TestAssetLoader>();
// app.finish();

// let asset_server = app.world.resource_mut::<AssetServer>();
// let asset_server = app.world_mut().resource_mut::<AssetServer>();
// let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
// let handle_2: Handle<TestAsset> = asset_server.load("açèt.test");
// let handle_3: Handle<TestAsset> = asset_server.load("subdir/other_asset.test");
// app.update();
// let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
// let asset = test_assets.get(handle_1).unwrap();
// let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
// let asset = test_assets.get(&handle_1).unwrap();
// assert_eq!(asset.value, "hello");
// let asset = test_assets.get(handle_2).unwrap();
// let asset = test_assets.get(&handle_2).unwrap();
// assert_eq!(asset.value, "with special chars");
// let asset = test_assets.get(handle_3).unwrap();
// let asset = test_assets.get(&handle_3).unwrap();
// assert_eq!(asset.value, "in subdirectory");
// }
37 changes: 18 additions & 19 deletions tests/as_default_with_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use std::fmt::Display;

use bevy::{prelude::*, utils::thiserror::Error};
use bevy::prelude::*;
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
use thiserror::Error;

#[derive(Asset, TypePath, Debug)]
pub struct TestAsset {
Expand All @@ -26,21 +27,19 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
type Asset = TestAsset;
type Settings = ();
type Error = TestError;
fn load<'a>(
async fn load<'a>(
&'a self,
reader: &'a mut bevy::asset::io::Reader,
reader: &'a mut bevy::asset::io::Reader<'_>,
_: &'a (),
_: &'a mut bevy::asset::LoadContext,
) -> bevy::utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
.await
.unwrap();
_: &'a mut bevy::asset::LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
.await
.unwrap();

Ok(TestAsset {
value: String::from_utf8(bytes).unwrap(),
})
Ok(TestAsset {
value: String::from_utf8(bytes).unwrap(),
})
}

Expand All @@ -65,19 +64,19 @@ fn work_with_embedded_source_plugin_before() {
.init_asset_loader::<TestAssetLoader>();
app.finish();

let asset_server = app.world.resource_mut::<AssetServer>();
let asset_server = app.world_mut().resource_mut::<AssetServer>();
let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
let handle_2: Handle<TestAsset> = asset_server.load("açèt.test");
let handle_3: Handle<TestAsset> = asset_server.load("subdir/other_asset.test");
let handle_4: Handle<TestAsset> = asset_server.load("asset.test");
app.update();
let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
let asset = test_assets.get(handle_1).unwrap();
let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
let asset = test_assets.get(&handle_1).unwrap();
assert_eq!(asset.value, "hello");
let asset = test_assets.get(handle_2).unwrap();
let asset = test_assets.get(&handle_2).unwrap();
assert_eq!(asset.value, "with special chars");
let asset = test_assets.get(handle_3).unwrap();
let asset = test_assets.get(&handle_3).unwrap();
assert_eq!(asset.value, "in subdirectory");
let asset = test_assets.get(handle_4).unwrap();
let asset = test_assets.get(&handle_4).unwrap();
assert_eq!(asset.value, "at runtime");
}
Loading

0 comments on commit af98623

Please sign in to comment.