use bevy::{
asset::{io::Reader, AssetLoader, LoadedAsset, LoadContext},
prelude::*,
reflect::TypePath,
tasks::BoxedFuture,
};
use thiserror::Error;
use tracing::{error, info, warn};
use serde::Deserialize;
use bevy_wasm_modding_shared::resource_id::{ResourceId, resource_id};
#[derive(Asset, Debug, Deserialize, TypePath)]
pub struct WasmAsset {
pub bytes: Vec<u8>,
}
impl WasmAsset {
pub fn get_resource_id() -> ResourceId {
resource_id::<WasmAsset>()
}
}
#[derive(Default, TypePath)]
pub struct WasmAssetLoader;
impl AssetLoader for WasmAssetLoader {
type Asset = WasmAsset;
type Settings = ();
type Error = CustomAssetLoaderError;
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &Self::Settings,
_load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Ok(WasmAsset { bytes })
}
fn extensions(&self) -> &[&str] {
&["wasm"]
}
}
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum CustomAssetLoaderError {
#[error("Could not load asset: {0}")]
Io(#[from] std::io::Error),
}