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-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 {
|
|
|
|
fn load(&self, bytes: &[u8], load_context: &mut LoadContext) -> Result<()> {
|
2020-08-21 21:55:16 +00:00
|
|
|
let registry = self.property_type_registry.read();
|
2020-09-10 01:51:59 +00:00
|
|
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
2020-05-25 19:03:50 +00:00
|
|
|
let scene_deserializer = SceneDeserializer {
|
|
|
|
property_type_registry: ®istry,
|
|
|
|
};
|
2020-06-06 00:32:32 +00:00
|
|
|
let scene = scene_deserializer.deserialize(&mut deserializer)?;
|
2020-10-18 20:48:15 +00:00
|
|
|
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
|
|
|
}
|