mirror of
https://github.com/bevyengine/bevy
synced 2024-12-25 12:33:07 +00:00
72b2fc9843
Bevy Reflection
43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
use crate::serde::SceneDeserializer;
|
|
use anyhow::Result;
|
|
use bevy_asset::{AssetLoader, LoadContext, LoadedAsset};
|
|
use bevy_ecs::{FromResources, Resources};
|
|
use bevy_reflect::TypeRegistryArc;
|
|
use bevy_utils::BoxedFuture;
|
|
use serde::de::DeserializeSeed;
|
|
|
|
#[derive(Debug)]
|
|
pub struct SceneLoader {
|
|
type_registry: TypeRegistryArc,
|
|
}
|
|
|
|
impl FromResources for SceneLoader {
|
|
fn from_resources(resources: &Resources) -> Self {
|
|
let type_registry = resources.get::<TypeRegistryArc>().unwrap();
|
|
SceneLoader {
|
|
type_registry: (&*type_registry).clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AssetLoader for SceneLoader {
|
|
fn load<'a>(
|
|
&'a self,
|
|
bytes: &'a [u8],
|
|
load_context: &'a mut LoadContext,
|
|
) -> BoxedFuture<'a, Result<()>> {
|
|
Box::pin(async move {
|
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes)?;
|
|
let scene_deserializer = SceneDeserializer {
|
|
type_registry: &*self.type_registry.read(),
|
|
};
|
|
let scene = scene_deserializer.deserialize(&mut deserializer)?;
|
|
load_context.set_default_asset(LoadedAsset::new(scene));
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
&["scn"]
|
|
}
|
|
}
|