Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saveload overhaul #337

Merged
merged 12 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ features = ["common", "serde"]
[dev-dependencies]
cgmath = { version = "0.14", features = ["eders"] }
criterion = "0.2"
ron = "0.1.3"
ron = "0.1.5"
rand = "0.3"
serde_json = "1.0"
specs-derive = { path = "specs-derive", version = "0.2.0" }
Expand Down
77 changes: 63 additions & 14 deletions examples/saveload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ extern crate ron;
extern crate serde;
extern crate specs;

use std::fmt;

use specs::error::NoError;
use specs::prelude::*;
use specs::saveload::{U64Marker, U64MarkerAllocator, WorldDeserialize, WorldSerialize};
use specs::saveload::{DeserializeComponents, SerializeComponents, U64Marker, U64MarkerAllocator};

const ENTITIES: &str = "
[
Expand Down Expand Up @@ -49,6 +51,31 @@ 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();

Expand All @@ -75,14 +102,25 @@ fn main() {
struct Serialize;

impl<'a> System<'a> for Serialize {
type SystemData = WorldSerialize<'a, U64Marker, NoError, (Pos, Mass)>;

fn run(&mut self, mut world: Self::SystemData) {
let s = ron::ser::to_string_pretty(&world, ron::ser::PrettyConfig::default()).unwrap();

println!("{}", s);

world.remove_serialized();
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this TODO to be left to future PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


println!("{}", ser.into_output_string());
}
}

Expand All @@ -93,14 +131,25 @@ fn main() {
struct Deserialize;

impl<'a> System<'a> for Deserialize {
type SystemData = WorldDeserialize<'a, U64Marker, NoError, (Pos, Mass)>;

fn run(&mut self, world: Self::SystemData) {
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;
use serde::de::DeserializeSeed;

let mut de = Deserializer::from_str(ENTITIES);
world.deserialize(&mut de).unwrap();
DeserializeComponents::<Combined, _>::deserialize(
&mut (pos, mass),
&ent,
&mut markers,
&mut alloc,
&mut de,
).unwrap_or_else(|e| eprintln!("Error: {}", e));
}
}

Expand Down
6 changes: 2 additions & 4 deletions examples/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ fn main() {
// Get a list of all entities in the world
let entity_list: Vec<_> = (&*data.entities).join().collect();

// Remove all components
for (entity, _) in (&*data.entities, &data.comp.mask().clone()).join() {
data.comp.remove(entity);
}
// Remove all components by consuming a draining iterator.
(&*data.entities, data.comp.drain()).join().count();

// Deserialize with entity list
let list: PackedData<CompSerialize> = json_from_str(&serialized).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ impl Errors {
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct Merge<F> {
#[derivative(Default(value = "PhantomData"))]
future_type: PhantomData<F>,
#[derivative(Default(value = "PhantomData"))] future_type: PhantomData<F>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is this some new rustfmt setting again?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this attribute necessary at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, otherwise F would have to implement Default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. PhantomData implements default for any type parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does now but I think it was a rather recent fix which I don't want to rely on yet.

Copy link
Member

@zakarumych zakarumych Mar 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does at least since 1.0.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, the attribute on PhantomData isn't required, but we need derivative here.

spawns: Vec<(Entity, Spawn<F>)>,
}

Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl Display for Error {
}
}

impl From<NoError> for Error {
fn from(e: NoError) -> Self {
match e {}
}
}

impl From<WrongGeneration> for Error {
fn from(e: WrongGeneration) -> Self {
Error::WrongGeneration(e)
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(missing_docs)]
#![warn(missing_docs)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there are missing documentations?

#![cfg_attr(feature = "nightly", feature(core_intrinsics))]

//! # SPECS Parallel ECS
Expand Down Expand Up @@ -199,9 +199,6 @@ extern crate futures;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
#[cfg(feature = "serde")]
#[macro_use]
extern crate shred_derive;

#[cfg(feature = "rudy")]
extern crate rudy;
Expand Down
Loading