use bevy_app::{App, Plugin};
use bevy_asset::io::Reader;
use bevy_asset::{Asset, AssetApp, AssetLoader, LoadContext};
use bevy_reflect::TypePath;
use quick_xml::de::from_str;
use std::marker::PhantomData;
use std::str::from_utf8;
use thiserror::Error;
pub struct XmlAssetPlugin<A> {
extensions: Vec<&'static str>,
_marker: PhantomData<A>,
}
impl<A> Plugin for XmlAssetPlugin<A>
where
for<'de> A: serde::Deserialize<'de> + Asset,
{
fn build(&self, app: &mut App) {
app.init_asset::<A>()
.register_asset_loader(XmlAssetLoader::<A> {
extensions: self.extensions.clone(),
_marker: PhantomData,
});
}
}
impl<A> XmlAssetPlugin<A>
where
for<'de> A: serde::Deserialize<'de> + Asset,
{
pub fn new(extensions: &[&'static str]) -> Self {
Self {
extensions: extensions.to_owned(),
_marker: PhantomData,
}
}
}
#[derive(TypePath)]
pub struct XmlAssetLoader<A> {
extensions: Vec<&'static str>,
_marker: PhantomData<A>,
}
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum XmlLoaderError {
#[error("Could not read the file: {0}")]
Io(#[from] std::io::Error),
#[error("Could not interpret as UTF-8: {0}")]
FormatError(#[from] std::str::Utf8Error),
#[error("Could not parse XML: {0}")]
XmlError(#[from] quick_xml::DeError),
}
impl<A> AssetLoader for XmlAssetLoader<A>
where
for<'de> A: serde::Deserialize<'de> + Asset,
{
type Asset = A;
type Settings = ();
type Error = XmlLoaderError;
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &(),
_load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let asset = from_str::<A>(from_utf8(&bytes)?)?;
Ok(asset)
}
fn extensions(&self) -> &[&str] {
&self.extensions
}
}