Skip to content

Commit

Permalink
Update for Bevy 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Nov 15, 2024
1 parent f3fb688 commit 44af369
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 25 deletions.
4 changes: 2 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.11.0"
version = "0.12.0-rc.1"
authors = ["François Mockers <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -18,7 +18,7 @@ default = ["default-source"]
default-source = ["futures-io", "futures-lite"]

[dependencies.bevy]
version = "0.14.0"
version = "0.15.0-rc.3"
default-features = false
features = ["bevy_asset"]

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.15|0.12|
|0.14|0.11|
|0.13|0.10|
|0.12|0.9|
Expand Down
40 changes: 35 additions & 5 deletions src/asset_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::{
};

use bevy::{
asset::io::{AssetReader, AssetReaderError, ErasedAssetReader, PathStream, Reader},
asset::io::{
AssetReader, AssetReaderError, AsyncSeekForward, ErasedAssetReader, PathStream, Reader,
},
utils::HashMap,
};
use futures_io::{AsyncRead, AsyncSeek};
Expand Down Expand Up @@ -147,6 +149,20 @@ impl EmbeddedAssetReader {
#[derive(Default, Debug, Clone, Copy)]
pub struct DataReader(pub &'static [u8]);

impl Reader for DataReader {
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> bevy::asset::io::StackFuture<
'a,
std::io::Result<usize>,
{ bevy::asset::io::STACK_FUTURE_SIZE },
> {
let future = futures_lite::AsyncReadExt::read_to_end(self, buf);
bevy::asset::io::StackFuture::from(future)
}
}

impl AsyncRead for DataReader {
fn poll_read(
self: Pin<&mut Self>,
Expand All @@ -171,6 +187,19 @@ impl AsyncSeek for DataReader {
}
}

impl AsyncSeekForward for DataReader {
fn poll_seek_forward(
self: Pin<&mut Self>,
_: &mut std::task::Context<'_>,
_offset: u64,
) -> 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")]
Expand Down Expand Up @@ -203,10 +232,11 @@ pub(crate) fn get_meta_path(path: &Path) -> PathBuf {
}

impl AssetReader for EmbeddedAssetReader {
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
// async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<dyn Reader>, AssetReaderError> {
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
if self.has_file_sync(path) {
self.load_path_sync(path).map(|reader| {
let boxed: Box<Reader> = Box::new(reader);
let boxed: Box<dyn Reader> = Box::new(reader);
boxed
})
} else if let Some(fallback) = self.fallback.as_ref() {
Expand All @@ -216,11 +246,11 @@ impl AssetReader for EmbeddedAssetReader {
}
}

async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let meta_path = get_meta_path(path);
if self.has_file_sync(&meta_path) {
self.load_path_sync(&meta_path).map(|reader| {
let boxed: Box<Reader> = Box::new(reader);
let boxed: Box<dyn Reader> = Box::new(reader);
boxed
})
} else if let Some(fallback) = self.fallback.as_ref() {
Expand Down
15 changes: 9 additions & 6 deletions tests/as_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use std::fmt::Display;

use bevy::prelude::*;
use bevy::{
asset::{io::Reader, LoadContext},
prelude::*,
};
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
use thiserror::Error;

Expand All @@ -27,11 +30,11 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
type Asset = TestAsset;
type Settings = ();
type Error = TestError;
async fn load<'a>(
&'a self,
reader: &'a mut bevy::asset::io::Reader<'_>,
_: &'a (),
_: &'a mut bevy::asset::LoadContext<'_>,
async fn load(
&self,
reader: &mut dyn Reader,
_: &(),
_: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
Expand Down
15 changes: 9 additions & 6 deletions tests/as_default_with_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use std::fmt::Display;

use bevy::prelude::*;
use bevy::{
asset::{io::Reader, LoadContext},
prelude::*,
};
use bevy_embedded_assets::{EmbeddedAssetPlugin, PluginMode};
use thiserror::Error;

Expand All @@ -27,11 +30,11 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
type Asset = TestAsset;
type Settings = ();
type Error = TestError;
async fn load<'a>(
&'a self,
reader: &'a mut bevy::asset::io::Reader<'_>,
_: &'a (),
_: &'a mut bevy::asset::LoadContext<'_>,
async fn load(
&self,
reader: &mut dyn Reader,
_: &(),
_: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
Expand Down
15 changes: 9 additions & 6 deletions tests/embedded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fmt::Display;

use bevy::prelude::*;
use bevy::{
asset::{io::Reader, LoadContext},
prelude::*,
};
use bevy_embedded_assets::EmbeddedAssetPlugin;
use thiserror::Error;

Expand All @@ -25,11 +28,11 @@ impl bevy::asset::AssetLoader for TestAssetLoader {
type Asset = TestAsset;
type Settings = ();
type Error = TestError;
async fn load<'a>(
&'a self,
reader: &'a mut bevy::asset::io::Reader<'_>,
_: &'a (),
_: &'a mut bevy::asset::LoadContext<'_>,
async fn load(
&self,
reader: &mut dyn Reader,
_: &(),
_: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
bevy::asset::AsyncReadExt::read_to_end(reader, &mut bytes)
Expand Down

0 comments on commit 44af369

Please sign in to comment.