bevy/crates/bevy_scene/src/scene_loader.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

use crate::serde::SceneDeserializer;
use anyhow::Result;
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;
use bevy_utils::BoxedFuture;
use serde::de::DeserializeSeed;
#[derive(Debug)]
pub struct SceneLoader {
2020-11-28 00:39:59 +00:00
type_registry: TypeRegistryArc,
}
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();
SceneLoader {
2020-11-28 00:39:59 +00:00
type_registry: (&*type_registry).clone(),
}
}
}
impl AssetLoader for SceneLoader {
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(),
};
let scene = scene_deserializer.deserialize(&mut deserializer)?;
load_context.set_default_asset(LoadedAsset::new(scene));
Ok(())
})
}
2020-07-28 21:24:03 +00:00
fn extensions(&self) -> &[&str] {
&["scn"]
}
}