-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asset: WasmAssetIo
- Loading branch information
Showing
19 changed files
with
422 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#[cfg(not(target_arch = "wasm32"))] | ||
mod file_asset_io; | ||
#[cfg(target_arch = "wasm32")] | ||
mod wasm_asset_io; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
pub use file_asset_io::*; | ||
#[cfg(target_arch = "wasm32")] | ||
pub use wasm_asset_io::*; | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use downcast_rs::{impl_downcast, Downcast}; | ||
use std::{ | ||
io, | ||
path::{Path, PathBuf}, | ||
}; | ||
use thiserror::Error; | ||
|
||
/// Errors that occur while loading assets | ||
#[derive(Error, Debug)] | ||
pub enum AssetIoError { | ||
#[error("Path not found")] | ||
NotFound(PathBuf), | ||
#[error("Encountered an io error while loading asset.")] | ||
Io(#[from] io::Error), | ||
#[error("Failed to watch path")] | ||
PathWatchError(PathBuf), | ||
} | ||
|
||
/// 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 { | ||
async fn load_path(&self, path: &Path) -> Result<Vec<u8>, AssetIoError>; | ||
fn read_directory( | ||
&self, | ||
path: &Path, | ||
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError>; | ||
fn is_directory(&self, path: &Path) -> bool; | ||
fn watch_path_for_changes(&self, path: &Path) -> Result<(), AssetIoError>; | ||
fn watch_for_changes(&self) -> Result<(), AssetIoError>; | ||
} | ||
|
||
impl_downcast!(AssetIo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::{AssetIo, AssetIoError}; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use js_sys::Uint8Array; | ||
use std::path::{Path, PathBuf}; | ||
use wasm_bindgen::JsCast; | ||
use wasm_bindgen_futures::JsFuture; | ||
use web_sys::Response; | ||
|
||
pub struct WasmAssetIo { | ||
root_path: PathBuf, | ||
} | ||
|
||
impl WasmAssetIo { | ||
pub fn new<P: AsRef<Path>>(path: P) -> Self { | ||
WasmAssetIo { | ||
root_path: path.as_ref().to_owned(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait(?Send)] | ||
impl AssetIo for WasmAssetIo { | ||
async fn load_path(&self, path: &Path) -> Result<Vec<u8>, AssetIoError> { | ||
let path = self.root_path.join(path); | ||
let window = web_sys::window().unwrap(); | ||
let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap())) | ||
.await | ||
.unwrap(); | ||
let resp: Response = resp_value.dyn_into().unwrap(); | ||
let data = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap(); | ||
let bytes = Uint8Array::new(&data).to_vec(); | ||
Ok(bytes) | ||
} | ||
|
||
fn read_directory( | ||
&self, | ||
_path: &Path, | ||
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> { | ||
Ok(Box::new(std::iter::empty::<PathBuf>())) | ||
} | ||
|
||
fn watch_path_for_changes(&self, _path: &Path) -> Result<(), AssetIoError> { | ||
Ok(()) | ||
} | ||
|
||
fn watch_for_changes(&self) -> Result<(), AssetIoError> { | ||
Ok(()) | ||
} | ||
|
||
fn is_directory(&self, path: &Path) -> bool { | ||
self.root_path.join(path).is_dir() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.