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
229 additions
and
37 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,31 @@ | ||
use crate::{AssetLoader, AssetResult, AssetVersion, HandleId}; | ||
use crossbeam_channel::Sender; | ||
use std::path::PathBuf; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[path = "platform_default.rs"] | ||
mod platform_specific; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[path = "platform_wasm.rs"] | ||
mod platform_specific; | ||
|
||
pub use platform_specific::*; | ||
|
||
/// A request from an [AssetServer](crate::AssetServer) to load an asset. | ||
#[derive(Debug)] | ||
pub struct LoadRequest { | ||
pub path: PathBuf, | ||
pub handle_id: HandleId, | ||
pub handler_index: usize, | ||
pub version: AssetVersion, | ||
} | ||
|
||
pub(crate) struct ChannelAssetHandler<TLoader, TAsset> | ||
where | ||
TLoader: AssetLoader<TAsset>, | ||
TAsset: 'static, | ||
{ | ||
sender: Sender<AssetResult<TAsset>>, | ||
loader: TLoader, | ||
} |
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,62 @@ | ||
use super::{ChannelAssetHandler, LoadRequest}; | ||
use crate::{AssetLoadError, AssetLoader, AssetResult, Handle}; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use crossbeam_channel::Sender; | ||
|
||
use js_sys::Uint8Array; | ||
use wasm_bindgen::JsCast; | ||
use wasm_bindgen_futures::JsFuture; | ||
use web_sys::Response; | ||
|
||
#[async_trait(?Send)] | ||
pub trait AssetLoadRequestHandler: Send + Sync + 'static { | ||
async fn handle_request(&self, load_request: &LoadRequest); | ||
fn extensions(&self) -> &[&str]; | ||
} | ||
|
||
impl<TLoader, TAsset> ChannelAssetHandler<TLoader, TAsset> | ||
where | ||
TLoader: AssetLoader<TAsset>, | ||
{ | ||
pub fn new(loader: TLoader, sender: Sender<AssetResult<TAsset>>) -> Self { | ||
ChannelAssetHandler { sender, loader } | ||
} | ||
|
||
async fn load_asset(&self, load_request: &LoadRequest) -> Result<TAsset, AssetLoadError> { | ||
// TODO - get rid of some unwraps below (do some retrying maybe?) | ||
let window = web_sys::window().unwrap(); | ||
let resp_value = JsFuture::from(window.fetch_with_str(load_request.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(); | ||
let asset = self.loader.from_bytes(&load_request.path, bytes).unwrap(); | ||
Ok(asset) | ||
} | ||
} | ||
|
||
#[async_trait(?Send)] | ||
impl<TLoader, TAsset> AssetLoadRequestHandler for ChannelAssetHandler<TLoader, TAsset> | ||
where | ||
TLoader: AssetLoader<TAsset> + 'static, | ||
TAsset: Send + 'static, | ||
{ | ||
async fn handle_request(&self, load_request: &LoadRequest) { | ||
let asset = self.load_asset(load_request).await; | ||
let asset_result = AssetResult { | ||
handle: Handle::from(load_request.handle_id), | ||
result: asset, | ||
path: load_request.path.clone(), | ||
version: load_request.version, | ||
}; | ||
self.sender | ||
.send(asset_result) | ||
.expect("loaded asset should have been sent"); | ||
} | ||
|
||
fn extensions(&self) -> &[&str] { | ||
self.loader.extensions() | ||
} | ||
} |
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,65 @@ | ||
extern crate console_error_panic_hook; | ||
use bevy::{asset::AssetLoader, prelude::*}; | ||
use std::{panic, path::PathBuf}; | ||
|
||
fn main() { | ||
panic::set_hook(Box::new(console_error_panic_hook::hook)); | ||
console_log::init_with_level(log::Level::Debug).expect("cannot initialize console_log"); | ||
|
||
App::build() | ||
.add_default_plugins() | ||
.add_asset::<RustSourceCode>() | ||
.add_asset_loader::<RustSourceCode, RustSourceCodeLoader>() | ||
.add_startup_system(asset_system.system()) | ||
.add_system(asset_events.system()) | ||
.run(); | ||
} | ||
|
||
fn asset_system(asset_server: Res<AssetServer>) { | ||
asset_server | ||
.load::<Handle<RustSourceCode>, _>(PathBuf::from("assets_wasm.rs")) | ||
.unwrap(); | ||
log::info!("hello wasm"); | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct RustSourceCode(pub String); | ||
|
||
#[derive(Default)] | ||
pub struct RustSourceCodeLoader; | ||
impl AssetLoader<RustSourceCode> for RustSourceCodeLoader { | ||
fn from_bytes( | ||
&self, | ||
_asset_path: &std::path::Path, | ||
bytes: Vec<u8>, | ||
) -> Result<RustSourceCode, anyhow::Error> { | ||
Ok(RustSourceCode(String::from_utf8(bytes)?)) | ||
} | ||
|
||
fn extensions(&self) -> &[&str] { | ||
static EXT: &[&str] = &["rs"]; | ||
EXT | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct AssetEventsState { | ||
reader: EventReader<AssetEvent<RustSourceCode>>, | ||
} | ||
|
||
pub fn asset_events( | ||
mut state: Local<AssetEventsState>, | ||
rust_sources: Res<Assets<RustSourceCode>>, | ||
events: Res<Events<AssetEvent<RustSourceCode>>>, | ||
) { | ||
for event in state.reader.iter(&events) { | ||
match event { | ||
AssetEvent::Created { handle } => { | ||
if let Some(code) = rust_sources.get(handle) { | ||
log::info!("code: {}", code.0); | ||
} | ||
} | ||
_ => continue, | ||
}; | ||
} | ||
} |