2020-10-02 19:24:30 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-09-25 22:26:23 +00:00
|
|
|
extern crate console_error_panic_hook;
|
2020-10-02 19:24:30 +00:00
|
|
|
|
2020-09-25 22:26:23 +00:00
|
|
|
use bevy::{asset::AssetLoader, prelude::*};
|
2020-10-02 19:24:30 +00:00
|
|
|
use std::path::PathBuf;
|
2020-09-25 22:26:23 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-10-02 19:24:30 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
{
|
|
|
|
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
|
|
|
|
console_log::init_with_level(log::Level::Debug).expect("cannot initialize console_log");
|
|
|
|
}
|
2020-09-25 22:26:23 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|