bevy/examples/serializing.rs

47 lines
1.6 KiB
Rust
Raw Normal View History

2020-01-15 06:19:28 +00:00
use bevy::{prelude::*, serialization::*};
2020-01-15 06:23:00 +00:00
use serde::{Deserialize, Serialize};
2020-01-15 06:19:28 +00:00
use type_uuid::TypeUuid;
fn main() {
2020-04-06 03:19:02 +00:00
let mut app = App::build();
app.add_default_plugins().add_startup_system(setup);
2020-01-15 06:19:28 +00:00
2020-01-15 06:23:00 +00:00
let comp_registrations = [ComponentRegistration::of::<Test>()];
2020-01-15 06:19:28 +00:00
let tag_registrations = [];
let ser_helper = SerializeImpl::new(&comp_registrations, &tag_registrations);
2020-04-06 03:19:02 +00:00
let serializable = legion::serialize::ser::serializable_world(&app.world(), &ser_helper);
2020-01-15 06:19:28 +00:00
let serialized_data = serde_json::to_string(&serializable).unwrap();
println!("{}", serialized_data);
2020-01-15 06:23:00 +00:00
let de_helper = DeserializeImpl::new(
ser_helper.comp_types,
ser_helper.tag_types,
ser_helper.entity_map,
);
2020-01-15 06:19:28 +00:00
2020-04-06 03:19:02 +00:00
let mut new_world = app.universe().create_world();
2020-01-15 06:19:28 +00:00
let mut deserializer = serde_json::Deserializer::from_str(&serialized_data);
2020-03-09 06:19:07 +00:00
legion::serialize::de::deserialize(&mut new_world, &de_helper, &mut deserializer).unwrap();
2020-01-15 06:23:00 +00:00
let ser_helper = SerializeImpl::new_with_map(
&comp_registrations,
&tag_registrations,
de_helper.entity_map.into_inner(),
);
2020-03-09 06:19:07 +00:00
let serializable = legion::serialize::ser::serializable_world(&new_world, &ser_helper);
2020-01-15 06:19:28 +00:00
let roundtrip_data = serde_json::to_string(&serializable).unwrap();
println!("{}", roundtrip_data);
assert_eq!(roundtrip_data, serialized_data);
}
#[derive(Serialize, Deserialize, TypeUuid)]
#[uuid = "14dec17f-ae14-40a3-8e44-e487fc423287"]
struct Test {
2020-01-15 06:19:28 +00:00
pub x: f32,
pub y: f32,
}
2020-03-09 06:19:07 +00:00
fn setup(world: &mut World, _resources: &mut Resources) {
2020-01-15 06:19:28 +00:00
// plane
2020-01-15 06:23:00 +00:00
world.insert((), vec![(Test { x: 3.0, y: 4.0 },)]);
2020-01-15 06:19:28 +00:00
}