use bevy::{
asset::{ErasedLoadedAsset, LoadedAsset},
platform::collections::HashMap,
prelude::*,
};
use serde::{Deserialize, Serialize};
use super::{AssetsBundle, BundleValue, call_load_from};
crate::reflect_serde::reflect_serde!(TextureSlicer => texture_slicer);
#[derive(Deserialize, Serialize, Debug)]
#[non_exhaustive]
pub enum BundleEntry {
File(String),
Bundle(HashMap<String, BundleEntry>),
List(Vec<BundleEntry>),
StringList(Vec<String>),
Text(String),
Value(BundleValue),
Float(f64),
UiRect(UiRect),
Color(Color),
#[serde(
serialize_with = "ser_reflect_texture_slicer",
deserialize_with = "de_reflect_texture_slicer"
)]
TextureSlicer(TextureSlicer),
#[cfg(feature = "atlas_layout")]
TextureAtlasLayout(crate::assets::atlas_layout::AtlasLayoutDefinition),
}
impl BundleEntry {
pub async fn load_asset(
self,
key: &str,
load_context: &mut bevy::asset::LoadContext<'_>,
) -> Result<ErasedLoadedAsset> {
match self {
BundleEntry::File(path) => {
let path = load_context.asset_path().parent().unwrap().resolve(&path)?;
Ok(load_context
.loader()
.immediate()
.with_unknown_type()
.load(path)
.await?)
}
BundleEntry::Bundle(assets) => Ok(LoadedAsset::new_with_dependencies(
call_load_from!(
AssetsBundle,
key.to_string(),
BundleEntry::Bundle(assets),
load_context
)
.await?,
)
.into()),
BundleEntry::List(_) => todo!(),
BundleEntry::StringList(_) => todo!(),
BundleEntry::Text(content) => Ok(LoadedAsset::new_with_dependencies(
crate::prelude::assets::Text::new(content),
)
.into()),
BundleEntry::Value(_) => todo!(),
BundleEntry::Float(_) => todo!(),
BundleEntry::Color(_) => todo!(),
BundleEntry::UiRect(_) => todo!(),
BundleEntry::TextureSlicer(_) => todo!(),
#[cfg(feature = "atlas_layout")]
BundleEntry::TextureAtlasLayout(layout) => {
let atlas: TextureAtlasLayout = layout.into();
Ok(LoadedAsset::new_with_dependencies(atlas).into())
}
}
}
}