2020-10-18 20:48:15 +00:00
|
|
|
use crate::serde::SceneDeserializer;
|
2020-05-25 19:03:50 +00:00
|
|
|
use anyhow::Result;
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_asset::{AssetLoader, LoadContext, LoadedAsset};
|
2020-07-10 08:37:06 +00:00
|
|
|
use bevy_ecs::{FromResources, Resources};
|
2020-11-28 00:39:59 +00:00
|
|
|
use bevy_reflect::TypeRegistryArc;
|
2020-10-20 00:29:31 +00:00
|
|
|
use bevy_utils::BoxedFuture;
|
2020-05-25 19:03:50 +00:00
|
|
|
use serde::de::DeserializeSeed;
|
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug)]
|
2020-05-25 19:03:50 +00:00
|
|
|
pub struct SceneLoader {
|
2020-11-28 00:39:59 +00:00
|
|
|
type_registry: TypeRegistryArc,
|
2020-05-25 19:03:50 +00:00
|
|
|
}
|
2020-05-26 00:50:17 +00:00
|
|
|
|
2020-05-25 19:03:50 +00:00
|
|
|
impl FromResources for SceneLoader {
|
|
|
|
fn from_resources(resources: &Resources) -> Self {
|
2020-11-28 00:39:59 +00:00
|
|
|
let type_registry = resources.get::<TypeRegistryArc>().unwrap();
|
2020-05-25 19:03:50 +00:00
|
|
|
SceneLoader {
|
2020-11-28 00:39:59 +00:00
|
|
|
type_registry: (&*type_registry).clone(),
|
2020-05-25 19:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
impl AssetLoader for SceneLoader {
|
2020-10-20 00:29:31 +00:00
|
|
|
fn load<'a>(
|
|
|
|
&'a self,
|
|
|
|
bytes: &'a [u8],
|
|
|
|
load_context: &'a mut LoadContext,
|
|
|
|
) -> BoxedFuture<'a, Result<()>> {
|
|
|
|
Box::pin(async move {
|
|
|
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
|
|
|
let scene_deserializer = SceneDeserializer {
|
2020-11-28 00:39:59 +00:00
|
|
|
type_registry: &*self.type_registry.read(),
|
2020-10-20 00:29:31 +00:00
|
|
|
};
|
|
|
|
let scene = scene_deserializer.deserialize(&mut deserializer)?;
|
|
|
|
load_context.set_default_asset(LoadedAsset::new(scene));
|
|
|
|
Ok(())
|
|
|
|
})
|
2020-05-25 19:03:50 +00:00
|
|
|
}
|
2020-07-28 21:24:03 +00:00
|
|
|
|
2020-05-25 19:03:50 +00:00
|
|
|
fn extensions(&self) -> &[&str] {
|
2020-11-17 21:40:18 +00:00
|
|
|
&["scn"]
|
2020-05-25 19:03:50 +00:00
|
|
|
}
|
2020-05-27 20:07:32 +00:00
|
|
|
}
|