fix wasm assets (#714)

fix wasm assets
This commit is contained in:
Mariusz Kryński 2020-10-22 00:55:15 +02:00 committed by GitHub
parent d01ba9e4fc
commit 677cb1fc67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 34 deletions

View file

@ -37,7 +37,6 @@ log = { version = "0.4", features = ["release_max_level_info"] }
notify = { version = "5.0.0-pre.2", optional = true } notify = { version = "5.0.0-pre.2", optional = true }
parking_lot = "0.11.0" parking_lot = "0.11.0"
rand = "0.7.3" rand = "0.7.3"
async-trait = "0.1.40"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" } wasm-bindgen = { version = "0.2" }

View file

@ -1,7 +1,6 @@
use crate::{filesystem_watcher::FilesystemWatcher, AssetIo, AssetIoError, AssetServer}; use crate::{filesystem_watcher::FilesystemWatcher, AssetIo, AssetIoError, AssetServer};
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use bevy_ecs::{bevy_utils::BoxedFuture, Res};
use bevy_ecs::Res;
use bevy_utils::HashSet; use bevy_utils::HashSet;
use crossbeam_channel::TryRecvError; use crossbeam_channel::TryRecvError;
use fs::File; use fs::File;
@ -42,23 +41,24 @@ impl FileAssetIo {
} }
} }
#[async_trait]
impl AssetIo for FileAssetIo { impl AssetIo for FileAssetIo {
async fn load_path(&self, path: &Path) -> Result<Vec<u8>, AssetIoError> { fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
let mut bytes = Vec::new(); Box::pin(async move {
match File::open(self.root_path.join(path)) { let mut bytes = Vec::new();
Ok(mut file) => { match File::open(self.root_path.join(path)) {
file.read_to_end(&mut bytes)?; Ok(mut file) => {
} file.read_to_end(&mut bytes)?;
Err(e) => { }
if e.kind() == std::io::ErrorKind::NotFound { Err(e) => {
return Err(AssetIoError::NotFound(path.to_owned())); if e.kind() == std::io::ErrorKind::NotFound {
} else { return Err(AssetIoError::NotFound(path.to_owned()));
return Err(e.into()); } else {
return Err(e.into());
}
} }
} }
} Ok(bytes)
Ok(bytes) })
} }
fn read_directory( fn read_directory(
@ -103,7 +103,7 @@ impl AssetIo for FileAssetIo {
} }
} }
#[cfg(feature = "filesystem_watcher")] #[cfg(all(feature = "filesystem_watcher", not(target_arch = "wasm32")))]
pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) { pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) {
let mut changed = HashSet::default(); let mut changed = HashSet::default();
let asset_io = let asset_io =

View file

@ -9,7 +9,7 @@ pub use file_asset_io::*;
pub use wasm_asset_io::*; pub use wasm_asset_io::*;
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use bevy_ecs::bevy_utils::BoxedFuture;
use downcast_rs::{impl_downcast, Downcast}; use downcast_rs::{impl_downcast, Downcast};
use std::{ use std::{
io, io,
@ -29,10 +29,8 @@ pub enum AssetIoError {
} }
/// Handles load requests from an AssetServer /// Handles load requests from an AssetServer
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait AssetIo: Downcast + Send + Sync + 'static { pub trait AssetIo: Downcast + Send + Sync + 'static {
async fn load_path(&self, path: &Path) -> Result<Vec<u8>, AssetIoError>; fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>>;
fn read_directory( fn read_directory(
&self, &self,
path: &Path, path: &Path,

View file

@ -1,6 +1,6 @@
use crate::{AssetIo, AssetIoError}; use crate::{AssetIo, AssetIoError};
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use bevy_ecs::bevy_utils::BoxedFuture;
use js_sys::Uint8Array; use js_sys::Uint8Array;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
@ -19,18 +19,19 @@ impl WasmAssetIo {
} }
} }
#[async_trait(?Send)]
impl AssetIo for WasmAssetIo { impl AssetIo for WasmAssetIo {
async fn load_path(&self, path: &Path) -> Result<Vec<u8>, AssetIoError> { fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
let path = self.root_path.join(path); Box::pin(async move {
let window = web_sys::window().unwrap(); let path = self.root_path.join(path);
let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap())) let window = web_sys::window().unwrap();
.await let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap()))
.unwrap(); .await
let resp: Response = resp_value.dyn_into().unwrap(); .unwrap();
let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap(); let resp: Response = resp_value.dyn_into().unwrap();
let bytes = Uint8Array::new(&data).to_vec(); let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap();
Ok(bytes) let bytes = Uint8Array::new(&data).to_vec();
Ok(bytes)
})
} }
fn read_directory( fn read_directory(

View file

@ -3,7 +3,12 @@ use std::{future::Future, pin::Pin};
pub use ahash::AHasher; pub use ahash::AHasher;
#[cfg(not(target_arch = "wasm32"))]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>; pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
pub type HashSet<K> = std::collections::HashSet<K, RandomState>; pub type HashSet<K> = std::collections::HashSet<K, RandomState>;