forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
171 additions
and
25 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
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,109 @@ | ||
use crate::io::{AssetIo, AssetIoError}; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use std::{ | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use js_sys::Uint8Array; | ||
use wasm_bindgen::{JsCast, JsValue}; | ||
use wasm_bindgen_futures::JsFuture; | ||
use web_sys::Response; | ||
|
||
pub struct WebAssetIo { | ||
pub root_path: PathBuf, | ||
} | ||
|
||
impl WebAssetIo { | ||
pub fn new<P: AsRef<Path>>(path: P) -> Self { | ||
WebAssetIo { | ||
root_path: path.as_ref().into() | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct SafeJsFuture(JsFuture); | ||
|
||
impl SafeJsFuture { | ||
pub fn new(js_future: JsFuture) -> SafeJsFuture { | ||
SafeJsFuture(js_future) | ||
} | ||
} | ||
|
||
unsafe impl Send for SafeJsFuture {} | ||
|
||
#[derive(Debug)] | ||
pub struct SafeJsValue(JsValue); | ||
unsafe impl Send for SafeJsValue {} | ||
|
||
impl SafeJsValue { | ||
pub fn dyn_into<T>(self) -> std::result::Result<T, JsValue> | ||
where | ||
T: JsCast, | ||
{ | ||
self.0.dyn_into() | ||
} | ||
} | ||
|
||
use futures_lite::FutureExt; | ||
|
||
impl std::future::Future for SafeJsFuture { | ||
type Output = Result<SafeJsValue, SafeJsValue>; | ||
|
||
fn poll( | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Self::Output> { | ||
let ret = self.0.poll(cx); | ||
ret.map(|r| r.map(|f| SafeJsValue(f)).map_err(|f| SafeJsValue(f))) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl AssetIo for WebAssetIo { | ||
async fn load_path(&self, path: &Path) -> Result<Vec<u8>, AssetIoError> { | ||
log::info!("root_path: {:?} path: {:?}", self.root_path, path); | ||
let path = self.root_path.join(Path::new(path)); | ||
log::info!("path: {:?}", path); | ||
let resp_value = SafeJsFuture::new(JsFuture::from( | ||
web_sys::window() | ||
.unwrap() | ||
.fetch_with_str(path.to_str().unwrap()) | ||
)); | ||
let response_data = SafeJsFuture::new(JsFuture::from( | ||
resp_value | ||
.await | ||
.unwrap() | ||
.dyn_into::<Response>() | ||
.unwrap() | ||
.array_buffer() | ||
.unwrap(), | ||
)); | ||
let data = response_data.await.unwrap(); | ||
Ok(Uint8Array::new(&data.0).to_vec()) | ||
} | ||
|
||
fn read_directory( | ||
&self, | ||
_path: &Path, | ||
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> { | ||
panic!("not supported on this platform"); | ||
} | ||
|
||
async fn save_path(&self, _path: &Path, _bytes: &[u8]) -> Result<(), AssetIoError> { | ||
panic!("not supported on this platform"); | ||
} | ||
|
||
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 { | ||
false | ||
} | ||
} |
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