-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asset: WasmAssetIo #703
Merged
Merged
asset: WasmAssetIo #703
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work in web workers and
WorkerGlobalScope
should be used instead. However,web-sys
doesn't seem to provide any abstraction to access this cross-environments asWindowOrWorkerGlobalScope
mixin is not supported.I think the best option would be to access
js_sys::global()
and try casting it to eitherWindow
orWorkerGlobalScope
, whichever succeeds. Similar fix was done in rustwasm/gloo#106, but it's kind of ugly. Maybe it could land inbevy_utils
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha anything specific about web-sys / webworkers is going to be out of my depth at the moment. but that sounds reasonable to me. given that we know this works outside of webworkers, im inclined to merge as-is for now. Then someone who knows what they're doing (like you) can follow up with webworker compat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for making loaders async - I did it on my branch and it was very simple change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hesitant to make asset loaders async because I didn't want consumers to need the
#[async_trait]
proc-macro. But:#[async_trait]
in consumers by using BoxFutureI'll make the change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is my implementation, for example working gltf loader: mrk-its@05bf8ae
BTW I didn't know how to conditionally enable '?Send' without copying whole struct definition, that's why I've ended with wrapping web_sys types with
unsafe impl Send
:) good to know about#[cfg_attr]
:)