bevy/examples/serializing.rs

46 lines
1.5 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-02-22 23:02:01 +00:00
let app = AppBuilder::new().add_defaults().setup_world(setup).build();
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);
let serializable = legion::ser::serializable_world(&app.world, &ser_helper);
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
let mut new_world = app.universe.create_world();
let mut deserializer = serde_json::Deserializer::from_str(&serialized_data);
legion::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-01-15 06:19:28 +00:00
let serializable = legion::ser::serializable_world(&new_world, &ser_helper);
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"]
pub struct Test {
pub x: f32,
pub y: f32,
}
fn setup(world: &mut World) {
// 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
}