use crate::{serde::SceneDeserializer, Scene}; use anyhow::Result; use bevy_app::FromResources; use bevy_asset::AssetLoader; use bevy_property::PropertyTypeRegistry; use bevy_type_registry::TypeRegistry; use legion::prelude::Resources; use serde::de::DeserializeSeed; use std::{ path::Path, sync::{Arc, RwLock}, }; pub struct SceneLoader { property_type_registry: Arc>, } impl FromResources for SceneLoader { fn from_resources(resources: &Resources) -> Self { let type_registry = resources.get::().unwrap(); SceneLoader { property_type_registry: type_registry.property.clone(), } } } impl AssetLoader for SceneLoader { fn from_bytes(&self, _asset_path: &Path, bytes: Vec) -> Result { let registry = self.property_type_registry.read().unwrap(); let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?; let scene_deserializer = SceneDeserializer { property_type_registry: ®istry, }; let scene = scene_deserializer.deserialize(&mut deserializer)?; Ok(scene) } fn extensions(&self) -> &[&str] { static EXTENSIONS: &[&str] = &["scn"]; EXTENSIONS } }