2020-10-20 00:29:31 +00:00
|
|
|
use bevy::{
|
|
|
|
asset::{AssetLoader, AssetServerSettings, LoadContext, LoadedAsset},
|
|
|
|
prelude::*,
|
2020-11-28 00:39:59 +00:00
|
|
|
reflect::TypeUuid,
|
2020-10-20 00:29:31 +00:00
|
|
|
utils::BoxedFuture,
|
|
|
|
};
|
2020-09-25 22:26:23 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
2020-10-20 00:29:31 +00:00
|
|
|
.add_resource(AssetServerSettings {
|
|
|
|
asset_folder: "/".to_string(),
|
|
|
|
})
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-09-25 22:26:23 +00:00
|
|
|
.add_asset::<RustSourceCode>()
|
2020-10-18 20:48:15 +00:00
|
|
|
.init_asset_loader::<RustSourceCodeLoader>()
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_startup_system(load_asset.system())
|
|
|
|
.add_system(print_asset.system())
|
2020-09-25 22:26:23 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2020-10-20 00:29:31 +00:00
|
|
|
struct State {
|
|
|
|
handle: Handle<RustSourceCode>,
|
|
|
|
printed: bool,
|
|
|
|
}
|
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
fn load_asset(commands: &mut Commands, asset_server: Res<AssetServer>) {
|
2020-10-20 00:29:31 +00:00
|
|
|
commands.insert_resource(State {
|
|
|
|
handle: asset_server.load("assets_wasm.rs"),
|
|
|
|
printed: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_asset(mut state: ResMut<State>, rust_sources: Res<Assets<RustSourceCode>>) {
|
|
|
|
if state.printed {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(code) = rust_sources.get(&state.handle) {
|
2020-11-13 01:23:57 +00:00
|
|
|
info!("code: {}", code.0);
|
2020-10-20 00:29:31 +00:00
|
|
|
state.printed = true;
|
|
|
|
}
|
2020-09-25 22:26:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
#[derive(Debug, TypeUuid)]
|
|
|
|
#[uuid = "1c3445ab-97d3-449c-ab35-16ba30e4c29d"]
|
2020-09-25 22:26:23 +00:00
|
|
|
pub struct RustSourceCode(pub String);
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct RustSourceCodeLoader;
|
2020-10-18 20:48:15 +00:00
|
|
|
|
|
|
|
impl AssetLoader for RustSourceCodeLoader {
|
2020-10-20 00:29:31 +00:00
|
|
|
fn load<'a>(
|
|
|
|
&'a self,
|
|
|
|
bytes: &'a [u8],
|
|
|
|
load_context: &'a mut LoadContext,
|
|
|
|
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
|
|
|
|
Box::pin(async move {
|
|
|
|
load_context.set_default_asset(LoadedAsset::new(RustSourceCode(String::from_utf8(
|
|
|
|
bytes.into(),
|
|
|
|
)?)));
|
|
|
|
Ok(())
|
|
|
|
})
|
2020-09-25 22:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extensions(&self) -> &[&str] {
|
2020-11-17 21:40:18 +00:00
|
|
|
&["rs"]
|
2020-09-25 22:26:23 +00:00
|
|
|
}
|
|
|
|
}
|