forked from amethyst/specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveload.rs
163 lines (135 loc) · 3.62 KB
/
saveload.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
extern crate ron;
#[macro_use]
extern crate serde;
extern crate specs;
use std::fmt;
use specs::error::NoError;
use specs::prelude::*;
use specs::saveload::{DeserializeComponents, MarkedBuilder, SerializeComponents, U64Marker, U64MarkerAllocator};
const ENTITIES: &str = "
[
(
marker: (0),
components: (
Some((
x: 10,
y: 20,
)),
Some((30.5)),
),
),
(
marker: (1),
components: (
Some(Pos(
x: 5,
y: 2,
)),
None,
),
),
]
";
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct Pos {
x: f32,
y: f32,
}
impl Component for Pos {
type Storage = VecStorage<Self>;
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct Mass(f32);
impl Component for Mass {
type Storage = VecStorage<Self>;
}
#[derive(Debug)]
enum Combined {
Ron(ron::ser::Error),
}
impl fmt::Display for Combined {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Combined::Ron(ref e) => write!(f, "{}", e),
}
}
}
impl From<ron::ser::Error> for Combined {
fn from(x: ron::ser::Error) -> Self {
Combined::Ron(x)
}
}
impl From<NoError> for Combined {
fn from(e: NoError) -> Self {
match e {}
}
}
fn main() {
let mut world = World::new();
world.register::<Pos>();
world.register::<Mass>();
world.register::<U64Marker>();
world.add_resource(U64MarkerAllocator::new());
world
.create_entity()
.with(Pos { x: 1.0, y: 2.0 })
.with(Mass(0.5))
.marked::<U64Marker>()
.build();
world
.create_entity()
.with(Pos { x: 7.0, y: 2.0 })
.with(Mass(4.5))
.marked::<U64Marker>()
.build();
struct Serialize;
impl<'a> System<'a> for Serialize {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Mass>,
ReadStorage<'a, U64Marker>,
);
fn run(&mut self, (ents, pos, mass, markers): Self::SystemData) {
let mut ser = ron::ser::Serializer::new(Some(Default::default()), true);
SerializeComponents::<NoError, U64Marker>::serialize(
&(&pos, &mass),
&ents,
&markers,
&mut ser,
).unwrap_or_else(|e| eprintln!("Error: {}", e));
// TODO: Specs should return an error which combines serialization
// and component errors.
println!("{}", ser.into_output_string());
}
}
Serialize.run_now(&world.res);
// -----------------
struct Deserialize;
impl<'a> System<'a> for Deserialize {
type SystemData = (
Entities<'a>,
Write<'a, U64MarkerAllocator>,
WriteStorage<'a, Pos>,
WriteStorage<'a, Mass>,
WriteStorage<'a, U64Marker>,
);
fn run(&mut self, (ent, mut alloc, pos, mass, mut markers): Self::SystemData) {
use ron::de::Deserializer;
if let Ok(mut de) = Deserializer::from_str(ENTITIES) {
DeserializeComponents::<Combined, _>::deserialize(
&mut (pos, mass),
&ent,
&mut markers,
&mut alloc,
&mut de,
).unwrap_or_else(|e| eprintln!("Error: {}", e));
}
}
}
Deserialize.run_now(&world.res);
println!(
"{:#?}",
(&world.read_storage::<Pos>()).join().collect::<Vec<_>>()
);
}