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_app::FromResources;
|
|
|
|
use bevy_asset::AssetLoader;
|
2020-05-25 21:51:38 +00:00
|
|
|
use bevy_component_registry::PropertyTypeRegistryContext;
|
2020-05-25 19:03:50 +00:00
|
|
|
use legion::prelude::Resources;
|
|
|
|
use serde::de::DeserializeSeed;
|
2020-05-27 20:07:32 +00:00
|
|
|
use std::path::Path;
|
2020-05-25 19:03:50 +00:00
|
|
|
|
|
|
|
pub struct SceneLoader {
|
|
|
|
property_type_registry: PropertyTypeRegistryContext,
|
|
|
|
}
|
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 {
|
|
|
|
let property_type_registry = resources.get::<PropertyTypeRegistryContext>().unwrap();
|
|
|
|
SceneLoader {
|
|
|
|
property_type_registry: property_type_registry.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssetLoader<Scene> for SceneLoader {
|
|
|
|
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<Scene> {
|
|
|
|
let registry = self.property_type_registry.value.read().unwrap();
|
|
|
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes).unwrap();
|
|
|
|
let scene_deserializer = SceneDeserializer {
|
|
|
|
property_type_registry: ®istry,
|
|
|
|
};
|
|
|
|
let scene = scene_deserializer.deserialize(&mut deserializer).unwrap();
|
|
|
|
Ok(scene)
|
|
|
|
}
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
|
|
static EXTENSIONS: &[&str] = &["scn"];
|
|
|
|
EXTENSIONS
|
|
|
|
}
|
2020-05-27 20:07:32 +00:00
|
|
|
}
|