2020-05-25 21:51:38 +00:00
|
|
|
use crate::{serde::SceneDeserializer, Scene};
|
2020-05-25 19:03:50 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use bevy_asset::AssetLoader;
|
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-05-25 19:03:50 +00:00
|
|
|
use serde::de::DeserializeSeed;
|
2020-07-10 04:18:35 +00:00
|
|
|
use bevy_ecs::{Resources, FromResources};
|
2020-06-04 03:08:20 +00:00
|
|
|
use std::{
|
|
|
|
path::Path,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssetLoader<Scene> for SceneLoader {
|
|
|
|
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<Scene> {
|
2020-05-28 02:27:55 +00:00
|
|
|
let registry = self.property_type_registry.read().unwrap();
|
2020-06-06 00:32:32 +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-05-25 19:03:50 +00:00
|
|
|
Ok(scene)
|
|
|
|
}
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
|
|
static EXTENSIONS: &[&str] = &["scn"];
|
|
|
|
EXTENSIONS
|
|
|
|
}
|
2020-05-27 20:07:32 +00:00
|
|
|
}
|