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,9 +41,9 @@ 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>> {
Box::pin(async move {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
match File::open(self.root_path.join(path)) { match File::open(self.root_path.join(path)) {
Ok(mut file) => { Ok(mut file) => {
@ -59,6 +58,7 @@ impl AssetIo for FileAssetIo {
} }
} }
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,9 +19,9 @@ 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>> {
Box::pin(async move {
let path = self.root_path.join(path); let path = self.root_path.join(path);
let window = web_sys::window().unwrap(); let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap())) let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap()))
@ -31,6 +31,7 @@ impl AssetIo for WasmAssetIo {
let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap(); let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap();
let bytes = Uint8Array::new(&data).to_vec(); let bytes = Uint8Array::new(&data).to_vec();
Ok(bytes) 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>;