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-05-28 02:27:55 +00:00
|
|
|
use bevy_property::PropertyTypeRegistry;
|
2020-06-04 03:08:20 +00:00
|
|
|
use bevy_type_registry::TypeRegistry;
|
2020-10-20 00:29:31 +00:00
|
|
|
use bevy_utils::BoxedFuture;
|
2020-08-21 21:55:16 +00:00
|
|
|
use parking_lot::RwLock;
|
2020-05-25 19:03:50 +00:00
|
|
|
use serde::de::DeserializeSeed;
|
2020-10-18 20:48:15 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-25 19:03:50 +00:00
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug)]
|
2020-05-25 19:03:50 +00:00
|
|
|
pub struct SceneLoader {
|
2020-05-28 02:27:55 +00:00
|
|
|
property_type_registry: Arc<RwLock<PropertyTypeRegistry>>,
|
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-05-28 02:27:55 +00:00
|
|
|
let type_registry = resources.get::<TypeRegistry>().unwrap();
|
2020-05-25 19:03:50 +00:00
|
|
|
SceneLoader {
|
2020-05-28 02:27:55 +00:00
|
|
|
property_type_registry: type_registry.property.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 registry = self.property_type_registry.read();
|
|
|
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
|
|
|
let scene_deserializer = SceneDeserializer {
|
|
|
|
property_type_registry: ®istry,
|
|
|
|
};
|
|
|
|
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] {
|
|
|
|
static EXTENSIONS: &[&str] = &["scn"];
|
|
|
|
EXTENSIONS
|
|
|
|
}
|
2020-05-27 20:07:32 +00:00
|
|
|
}
|