1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! World serialization types.

use serde::ser::{Serialize, SerializeMap, Serializer};

use super::{
    archetypes::ser::ArchetypeLayoutSerializer, entities::ser::EntitiesLayoutSerializer,
    EntitySerializer, UnknownType, WorldField,
};
use crate::{
    internals::{query::filter::LayoutFilter, storage::component::ComponentTypeId, world::World},
    serialize::set_entity_serializer,
    storage::{ArchetypeIndex, UnknownComponentStorage},
};

/// Describes a type which knows how to deserialize the components in a world.
pub trait WorldSerializer {
    /// The stable type ID used to identify each component type in the serialized data.
    type TypeId: Serialize + Ord;

    /// Converts a runtime component type ID into the serialized type ID.
    fn map_id(&self, type_id: ComponentTypeId) -> Result<Self::TypeId, UnknownType>;

    /// Serializes a single component.
    ///
    /// # Safety
    /// The pointer must point to a valid instance of the component type represented by
    /// the given component type ID.
    unsafe fn serialize_component<S: Serializer>(
        &self,
        ty: ComponentTypeId,
        ptr: *const u8,
        serializer: S,
    ) -> Result<S::Ok, S::Error>;

    /// Serializes a slice of components.
    ///
    /// # Safety
    /// The pointer must point to a valid instance of the component type represented by
    /// the given component type ID.
    unsafe fn serialize_component_slice<S: Serializer>(
        &self,
        ty: ComponentTypeId,
        storage: &dyn UnknownComponentStorage,
        archetype: ArchetypeIndex,
        serializer: S,
    ) -> Result<S::Ok, S::Error>;
}

/// A serializable representation of a world.
pub struct SerializableWorld<'a, F: LayoutFilter, W: WorldSerializer, E: EntitySerializer> {
    world: &'a World,
    filter: F,
    world_serializer: &'a W,
    entity_serializer: &'a E,
}

impl<'a, F: LayoutFilter, W: WorldSerializer, E: EntitySerializer> SerializableWorld<'a, F, W, E> {
    pub(crate) fn new(
        world: &'a World,
        filter: F,
        world_serializer: &'a W,
        entity_serializer: &'a E,
    ) -> Self {
        Self {
            world,
            filter,
            world_serializer,
            entity_serializer,
        }
    }
}

impl<'a, F: LayoutFilter, W: WorldSerializer, E: EntitySerializer> Serialize
    for SerializableWorld<'a, F, W, E>
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serialize_world(
            serializer,
            self.world,
            &self.filter,
            self.world_serializer,
            self.entity_serializer,
        )
    }
}

fn serialize_world<S, F, W, E>(
    serializer: S,
    world: &World,
    filter: &F,
    world_serializer: &W,
    entity_serializer: &E,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
    F: LayoutFilter,
    W: WorldSerializer,
    E: EntitySerializer,
{
    let human_readable = serializer.is_human_readable();
    let mut root = serializer.serialize_map(Some(1))?;

    let mut hoist = core::cell::Cell::new(None);
    let hoist_ref = &mut hoist;
    let root_ref = &mut root;

    set_entity_serializer(entity_serializer, || {
        let result = if human_readable {
            // serialize per-entity representation
            root_ref.serialize_entry(
                &WorldField::Entities,
                &EntitiesLayoutSerializer {
                    world_serializer,
                    world,
                    filter,
                },
            )
        } else {
            // serialize machine-optimised representation
            root_ref.serialize_entry(
                &WorldField::Packed,
                &ArchetypeLayoutSerializer {
                    world_serializer,
                    world,
                    filter,
                },
            )
        };
        hoist_ref.set(Some(result));
    });

    hoist.into_inner().unwrap()?;
    root.end()
}