use crate::{DynamicScene, Entity}; use anyhow::Result; use bevy_reflect::{ serde::{ReflectDeserializer, ReflectSerializer}, Reflect, TypeRegistry, TypeRegistryArc, }; use serde::{ de::{DeserializeSeed, Error, MapAccess, SeqAccess, Visitor}, ser::{SerializeSeq, SerializeStruct}, Deserialize, Serialize, }; pub struct SceneSerializer<'a> { pub scene: &'a DynamicScene, pub registry: &'a TypeRegistryArc, } impl<'a> SceneSerializer<'a> { pub fn new(scene: &'a DynamicScene, registry: &'a TypeRegistryArc) -> Self { SceneSerializer { scene, registry } } } impl<'a> Serialize for SceneSerializer<'a> { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { let mut state = serializer.serialize_seq(Some(self.scene.entities.len()))?; for entity in self.scene.entities.iter() { state.serialize_element(&EntitySerializer { entity, registry: self.registry, })?; } state.end() } } pub struct EntitySerializer<'a> { pub entity: &'a Entity, pub registry: &'a TypeRegistryArc, } impl<'a> Serialize for EntitySerializer<'a> { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { let mut state = serializer.serialize_struct(ENTITY_STRUCT, 2)?; state.serialize_field(ENTITY_FIELD_ENTITY, &self.entity.entity)?; state.serialize_field( ENTITY_FIELD_COMPONENTS, &ComponentsSerializer { components: &self.entity.components, registry: self.registry, }, )?; state.end() } } pub struct ComponentsSerializer<'a> { pub components: &'a [Box], pub registry: &'a TypeRegistryArc, } impl<'a> Serialize for ComponentsSerializer<'a> { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { let mut state = serializer.serialize_seq(Some(self.components.len()))?; for component in self.components.iter() { state.serialize_element(&ReflectSerializer::new( &**component, &*self.registry.read(), ))?; } state.end() } } pub struct SceneDeserializer<'a> { pub type_registry: &'a TypeRegistry, } impl<'a, 'de> DeserializeSeed<'de> for SceneDeserializer<'a> { type Value = DynamicScene; fn deserialize(self, deserializer: D) -> Result where D: serde::Deserializer<'de>, { Ok(DynamicScene { entities: deserializer.deserialize_seq(SceneEntitySeqVisitor { type_registry: self.type_registry, })?, }) } } struct SceneEntitySeqVisitor<'a> { pub type_registry: &'a TypeRegistry, } impl<'a, 'de> Visitor<'de> for SceneEntitySeqVisitor<'a> { type Value = Vec; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("list of entities") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let mut entities = Vec::new(); while let Some(entity) = seq.next_element_seed(SceneEntityDeserializer { type_registry: self.type_registry, })? { entities.push(entity); } Ok(entities) } } pub struct SceneEntityDeserializer<'a> { pub type_registry: &'a TypeRegistry, } impl<'a, 'de> DeserializeSeed<'de> for SceneEntityDeserializer<'a> { type Value = Entity; fn deserialize(self, deserializer: D) -> Result where D: serde::Deserializer<'de>, { deserializer.deserialize_struct( ENTITY_STRUCT, &[ENTITY_FIELD_ENTITY, ENTITY_FIELD_COMPONENTS], SceneEntityVisitor { registry: self.type_registry, }, ) } } #[derive(Deserialize)] #[serde(field_identifier, rename_all = "lowercase")] enum EntityField { Entity, Components, } pub const ENTITY_STRUCT: &str = "Entity"; pub const ENTITY_FIELD_ENTITY: &str = "entity"; pub const ENTITY_FIELD_COMPONENTS: &str = "components"; struct SceneEntityVisitor<'a> { pub registry: &'a TypeRegistry, } impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> { type Value = Entity; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("entities") } fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, { let mut id = None; let mut components = None; while let Some(key) = map.next_key()? { match key { EntityField::Entity => { if id.is_some() { return Err(Error::duplicate_field(ENTITY_FIELD_ENTITY)); } id = Some(map.next_value::()?); } EntityField::Components => { if components.is_some() { return Err(Error::duplicate_field(ENTITY_FIELD_COMPONENTS)); } components = Some(map.next_value_seed(ComponentVecDeserializer { registry: self.registry, })?); } } } let entity = id .as_ref() .ok_or_else(|| Error::missing_field(ENTITY_FIELD_ENTITY))?; let components = components .take() .ok_or_else(|| Error::missing_field(ENTITY_FIELD_COMPONENTS))?; Ok(Entity { entity: *entity, components, }) } } pub struct ComponentVecDeserializer<'a> { pub registry: &'a TypeRegistry, } impl<'a, 'de> DeserializeSeed<'de> for ComponentVecDeserializer<'a> { type Value = Vec>; fn deserialize(self, deserializer: D) -> Result where D: serde::Deserializer<'de>, { deserializer.deserialize_seq(ComponentSeqVisitor { registry: self.registry, }) } } struct ComponentSeqVisitor<'a> { pub registry: &'a TypeRegistry, } impl<'a, 'de> Visitor<'de> for ComponentSeqVisitor<'a> { type Value = Vec>; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("list of components") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let mut dynamic_properties = Vec::new(); while let Some(entity) = seq.next_element_seed(ReflectDeserializer::new(self.registry))? { dynamic_properties.push(entity); } Ok(dynamic_properties) } }