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
use legion::{
serialize::{Canon, DeserializeNewWorld},
storage::Component,
Registry, World,
};
use serde::{de::DeserializeSeed, Deserializer, Serialize, Serializer};
pub(crate) trait RegistryExt {
fn register_with_type_name<C>(&mut self) -> &mut Self
where
C: Component + serde::Serialize + for<'de> serde::Deserialize<'de>;
}
impl RegistryExt for Registry<String> {
fn register_with_type_name<C>(&mut self) -> &mut Self
where
C: Component + serde::Serialize + for<'de> serde::Deserialize<'de>,
{
self.register::<C>(std::any::type_name::<C>().to_string());
self
}
}
pub fn registry() -> Registry<String> {
let mut registry = Registry::new();
crate::parse::register_components(&mut registry);
crate::lowering::register_components(&mut registry);
crate::type_check::register_components(&mut registry);
crate::codegen::register_components(&mut registry);
registry
}
pub fn serialize_world<S>(
world: &World,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let registry = registry();
let canon = Canon::default();
world
.as_serializable(legion::any(), ®istry, &canon)
.serialize(serializer)
}
pub fn deserialize_world<'de, D>(deserializer: D) -> Result<World, D::Error>
where
D: Deserializer<'de>,
{
let registry = crate::serialize::registry();
let canon = Canon::default();
DeserializeNewWorld {
world_deserializer: ®istry,
entity_serializer: &canon,
}
.deserialize(deserializer)
}