Skip to content

Commit 7e94613

Browse files
authored
Merge pull request #20 from vleue/update-for-bevy-0.14
update for Bevy 0.14
2 parents 3cd44fc + af98623 commit 7e94613

File tree

7 files changed

+125
-118
lines changed

7 files changed

+125
-118
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bevy_embedded_assets"
3-
version = "0.10.2"
3+
version = "0.11.0"
44
authors = ["François Mockers <[email protected]>"]
55
edition = "2021"
66
license = "MIT OR Apache-2.0"
@@ -18,13 +18,14 @@ default = ["default-source"]
1818
default-source = ["futures-io", "futures-lite"]
1919

2020
[dependencies.bevy]
21-
version = "0.13"
21+
version = "0.14.0-rc.2"
2222
default-features = false
2323
features = ["bevy_asset"]
2424

2525
[dependencies]
2626
futures-io = { version = "0.3", optional = true }
2727
futures-lite = { version = "2.0", optional = true }
28+
thiserror = "1.0"
2829

2930
[build-dependencies]
3031
cargo-emit = "0.2.1"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn main() {
2828
|Bevy|bevy_embedded_assets|
2929
|---|---|
3030
|main|main|
31+
|0.14|0.11|
3132
|0.13|0.10|
3233
|0.12|0.9|
3334
|0.11|0.8|

src/asset_reader.rs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use std::{
66
};
77

88
use bevy::{
9-
asset::io::{AssetReader, AssetReaderError, PathStream, Reader},
9+
asset::io::{AssetReader, AssetReaderError, ErasedAssetReader, PathStream, Reader},
1010
utils::HashMap,
1111
};
12-
use futures_io::AsyncRead;
12+
use futures_io::{AsyncRead, AsyncSeek};
1313
use futures_lite::Stream;
14+
use thiserror::Error;
1415

1516
use crate::{include_all_assets, EmbeddedRegistry};
1617

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

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

161+
impl AsyncSeek for DataReader {
162+
fn poll_seek(
163+
self: Pin<&mut Self>,
164+
_: &mut std::task::Context<'_>,
165+
_pos: futures_io::SeekFrom,
166+
) -> Poll<futures_io::Result<u64>> {
167+
Poll::Ready(Err(futures_io::Error::new(
168+
futures_io::ErrorKind::Other,
169+
EmbeddedDataReaderError::SeekNotSupported,
170+
)))
171+
}
172+
}
173+
174+
#[derive(Error, Debug)]
175+
enum EmbeddedDataReaderError {
176+
#[error("Seek is not supported when embeded")]
177+
SeekNotSupported,
178+
}
179+
160180
struct DirReader(Vec<PathBuf>);
161181

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

185205
impl AssetReader for EmbeddedAssetReader {
186-
fn read<'a>(
187-
&'a self,
188-
path: &'a Path,
189-
) -> bevy::utils::BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
206+
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
190207
if self.has_file_sync(path) {
191-
Box::pin(async move {
192-
self.load_path_sync(path).map(|reader| {
193-
let boxed: Box<Reader> = Box::new(reader);
194-
boxed
195-
})
208+
self.load_path_sync(path).map(|reader| {
209+
let boxed: Box<Reader> = Box::new(reader);
210+
boxed
196211
})
197212
} else if let Some(fallback) = self.fallback.as_ref() {
198-
fallback.read(path)
213+
fallback.read(path).await
199214
} else {
200-
Box::pin(async move { Err(AssetReaderError::NotFound(path.to_path_buf())) })
215+
Err(AssetReaderError::NotFound(path.to_path_buf()))
201216
}
202217
}
203218

204-
fn read_meta<'a>(
205-
&'a self,
206-
path: &'a Path,
207-
) -> bevy::utils::BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
219+
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
208220
let meta_path = get_meta_path(path);
209221
if self.has_file_sync(&meta_path) {
210-
Box::pin(async move {
211-
self.load_path_sync(&meta_path).map(|reader| {
212-
let boxed: Box<Reader> = Box::new(reader);
213-
boxed
214-
})
222+
self.load_path_sync(&meta_path).map(|reader| {
223+
let boxed: Box<Reader> = Box::new(reader);
224+
boxed
215225
})
216226
} else if let Some(fallback) = self.fallback.as_ref() {
217-
fallback.read_meta(path)
227+
fallback.read_meta(path).await
218228
} else {
219-
Box::pin(async move { Err(AssetReaderError::NotFound(meta_path)) })
229+
Err(AssetReaderError::NotFound(meta_path))
220230
}
221231
}
222232

223-
fn read_directory<'a>(
233+
async fn read_directory<'a>(
224234
&'a self,
225235
path: &'a Path,
226-
) -> bevy::utils::BoxedFuture<'a, Result<Box<PathStream>, AssetReaderError>> {
227-
Box::pin(async move {
228-
self.read_directory_sync(path).map(|read_dir| {
229-
let boxed: Box<PathStream> = Box::new(read_dir);
230-
boxed
231-
})
236+
) -> Result<Box<PathStream>, AssetReaderError> {
237+
self.read_directory_sync(path).map(|read_dir| {
238+
let boxed: Box<PathStream> = Box::new(read_dir);
239+
boxed
232240
})
233241
}
234242

235-
fn is_directory<'a>(
236-
&'a self,
237-
path: &'a Path,
238-
) -> bevy::utils::BoxedFuture<'a, Result<bool, AssetReaderError>> {
239-
Box::pin(async move { Ok(self.is_directory_sync(path)) })
243+
async fn is_directory<'a>(&'a self, path: &'a Path) -> Result<bool, AssetReaderError> {
244+
Ok(self.is_directory_sync(path))
240245
}
241246
}
242247

src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ include!(concat!(env!("OUT_DIR"), "/include_all_assets.rs"));
5050
/// # let mut app = App::new();
5151
/// app.add_plugins((EmbeddedAssetPlugin::default(), DefaultPlugins));
5252
/// # app.init_asset::<MyAsset>();
53-
/// # let asset_server: Mut<'_, AssetServer> = app.world.resource_mut::<AssetServer>();
53+
/// # let asset_server: Mut<'_, AssetServer> = app.world_mut().resource_mut::<AssetServer>();
5454
/// let handle: Handle<MyAsset> = asset_server.load("embedded://example_asset.test");
5555
/// # }
5656
/// ```
@@ -69,7 +69,7 @@ include!(concat!(env!("OUT_DIR"), "/include_all_assets.rs"));
6969
/// # let mut app = App::new();
7070
/// app.add_plugins((EmbeddedAssetPlugin { mode: PluginMode::ReplaceDefault }, DefaultPlugins));
7171
/// # app.init_asset::<MyAsset>();
72-
/// # let asset_server: Mut<'_, AssetServer> = app.world.resource_mut::<AssetServer>();
72+
/// # let asset_server: Mut<'_, AssetServer> = app.world_mut().resource_mut::<AssetServer>();
7373
/// let handle: Handle<MyAsset> = asset_server.load("example_asset.test");
7474
/// # }
7575
/// ```
@@ -124,7 +124,7 @@ impl Plugin for EmbeddedAssetPlugin {
124124
match &self.mode {
125125
PluginMode::AutoLoad => {
126126
if app.is_plugin_added::<AssetPlugin>() {
127-
let mut registry = app.world.resource_mut::<EmbeddedAssetRegistry>();
127+
let mut registry = app.world_mut().resource_mut::<EmbeddedAssetRegistry>();
128128
include_all_assets(registry.as_mut());
129129
app.init_resource::<AllTheEmbedded>();
130130
}
@@ -165,9 +165,12 @@ impl Plugin for EmbeddedAssetPlugin {
165165

166166
fn finish(&self, app: &mut App) {
167167
if matches!(self.mode, PluginMode::AutoLoad)
168-
&& app.world.remove_resource::<AllTheEmbedded>().is_none()
168+
&& app
169+
.world_mut()
170+
.remove_resource::<AllTheEmbedded>()
171+
.is_none()
169172
{
170-
let mut registry = app.world.resource_mut::<EmbeddedAssetRegistry>();
173+
let mut registry = app.world_mut().resource_mut::<EmbeddedAssetRegistry>();
171174
include_all_assets(registry.as_mut());
172175
}
173176
}

tests/as_default.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
use std::fmt::Display;
44

5-
use bevy::{prelude::*, utils::thiserror::Error};
5+
use bevy::prelude::*;
66
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
7+
use thiserror::Error;
78

89
#[derive(Asset, TypePath, Debug)]
910
pub struct TestAsset {
@@ -26,21 +27,19 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
2627
type Asset = TestAsset;
2728
type Settings = ();
2829
type Error = TestError;
29-
fn load<'a>(
30+
async fn load<'a>(
3031
&'a self,
31-
reader: &'a mut bevy::asset::io::Reader,
32+
reader: &'a mut bevy::asset::io::Reader<'_>,
3233
_: &'a (),
33-
_: &'a mut bevy::asset::LoadContext,
34-
) -> bevy::utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
35-
Box::pin(async move {
36-
let mut bytes = Vec::new();
37-
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
38-
.await
39-
.unwrap();
34+
_: &'a mut bevy::asset::LoadContext<'_>,
35+
) -> Result<Self::Asset, Self::Error> {
36+
let mut bytes = Vec::new();
37+
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
38+
.await
39+
.unwrap();
4040

41-
Ok(TestAsset {
42-
value: String::from_utf8(bytes).unwrap(),
43-
})
41+
Ok(TestAsset {
42+
value: String::from_utf8(bytes).unwrap(),
4443
})
4544
}
4645

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

66-
let asset_server = app.world.resource_mut::<AssetServer>();
65+
let asset_server = app.world_mut().resource_mut::<AssetServer>();
6766
let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
6867
let handle_2: Handle<TestAsset> = asset_server.load("açèt.test");
6968
let handle_3: Handle<TestAsset> = asset_server.load("subdir/other_asset.test");
7069
app.update();
71-
let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
72-
let asset = test_assets.get(handle_1).unwrap();
70+
let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
71+
let asset = test_assets.get(&handle_1).unwrap();
7372
assert_eq!(asset.value, "hello");
74-
let asset = test_assets.get(handle_2).unwrap();
73+
let asset = test_assets.get(&handle_2).unwrap();
7574
assert_eq!(asset.value, "with special chars");
76-
let asset = test_assets.get(handle_3).unwrap();
75+
let asset = test_assets.get(&handle_3).unwrap();
7776
assert_eq!(asset.value, "in subdirectory");
7877
}
7978

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

95-
let asset_server = app.world.resource_mut::<AssetServer>();
94+
let asset_server = app.world_mut().resource_mut::<AssetServer>();
9695
let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
9796
app.update();
98-
let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
99-
test_assets.get(handle_1).unwrap();
97+
let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
98+
test_assets.get(&handle_1).unwrap();
10099
}
101100

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

111-
// let asset_server = app.world.resource_mut::<AssetServer>();
110+
// let asset_server = app.world_mut().resource_mut::<AssetServer>();
112111
// let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
113112
// let handle_2: Handle<TestAsset> = asset_server.load("açèt.test");
114113
// let handle_3: Handle<TestAsset> = asset_server.load("subdir/other_asset.test");
115114
// app.update();
116-
// let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
117-
// let asset = test_assets.get(handle_1).unwrap();
115+
// let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
116+
// let asset = test_assets.get(&handle_1).unwrap();
118117
// assert_eq!(asset.value, "hello");
119-
// let asset = test_assets.get(handle_2).unwrap();
118+
// let asset = test_assets.get(&handle_2).unwrap();
120119
// assert_eq!(asset.value, "with special chars");
121-
// let asset = test_assets.get(handle_3).unwrap();
120+
// let asset = test_assets.get(&handle_3).unwrap();
122121
// assert_eq!(asset.value, "in subdirectory");
123122
// }

tests/as_default_with_fallback.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
use std::fmt::Display;
44

5-
use bevy::{prelude::*, utils::thiserror::Error};
5+
use bevy::prelude::*;
66
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
7+
use thiserror::Error;
78

89
#[derive(Asset, TypePath, Debug)]
910
pub struct TestAsset {
@@ -26,21 +27,19 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
2627
type Asset = TestAsset;
2728
type Settings = ();
2829
type Error = TestError;
29-
fn load<'a>(
30+
async fn load<'a>(
3031
&'a self,
31-
reader: &'a mut bevy::asset::io::Reader,
32+
reader: &'a mut bevy::asset::io::Reader<'_>,
3233
_: &'a (),
33-
_: &'a mut bevy::asset::LoadContext,
34-
) -> bevy::utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
35-
Box::pin(async move {
36-
let mut bytes = Vec::new();
37-
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
38-
.await
39-
.unwrap();
34+
_: &'a mut bevy::asset::LoadContext<'_>,
35+
) -> Result<Self::Asset, Self::Error> {
36+
let mut bytes = Vec::new();
37+
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
38+
.await
39+
.unwrap();
4040

41-
Ok(TestAsset {
42-
value: String::from_utf8(bytes).unwrap(),
43-
})
41+
Ok(TestAsset {
42+
value: String::from_utf8(bytes).unwrap(),
4443
})
4544
}
4645

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

68-
let asset_server = app.world.resource_mut::<AssetServer>();
67+
let asset_server = app.world_mut().resource_mut::<AssetServer>();
6968
let handle_1: Handle<TestAsset> = asset_server.load("example_asset.test");
7069
let handle_2: Handle<TestAsset> = asset_server.load("açèt.test");
7170
let handle_3: Handle<TestAsset> = asset_server.load("subdir/other_asset.test");
7271
let handle_4: Handle<TestAsset> = asset_server.load("asset.test");
7372
app.update();
74-
let test_assets = app.world.resource_mut::<Assets<TestAsset>>();
75-
let asset = test_assets.get(handle_1).unwrap();
73+
let test_assets = app.world_mut().resource_mut::<Assets<TestAsset>>();
74+
let asset = test_assets.get(&handle_1).unwrap();
7675
assert_eq!(asset.value, "hello");
77-
let asset = test_assets.get(handle_2).unwrap();
76+
let asset = test_assets.get(&handle_2).unwrap();
7877
assert_eq!(asset.value, "with special chars");
79-
let asset = test_assets.get(handle_3).unwrap();
78+
let asset = test_assets.get(&handle_3).unwrap();
8079
assert_eq!(asset.value, "in subdirectory");
81-
let asset = test_assets.get(handle_4).unwrap();
80+
let asset = test_assets.get(&handle_4).unwrap();
8281
assert_eq!(asset.value, "at runtime");
8382
}

0 commit comments

Comments
 (0)