diff --git a/Cargo.toml b/Cargo.toml index 9388563..0b972b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,11 @@ categories = ["network-programming", "game-development"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["serde"] +default = ["serde", "ron", "bincode"] wasm-bindgen = ["instant/wasm-bindgen", "ggrs/wasm-bindgen"] -serde = ["dep:serde", "dep:postcard"] +serde = ["dep:serde"] +ron = ["serde", "dep:ron"] +bincode = ["serde", "dep:bincode"] [dependencies] bevy = { version = "0.12", default-features = false } @@ -24,15 +26,16 @@ instant = { version = "0.1", optional = true } log = "0.4" #ggrs = { version= "0.9.4", features=["sync-send"]} ggrs = { git = "https://github.com/gschup/ggrs", features=["sync-send"]} -serde = { version = "1.0.130", optional = true } -postcard = { version = "1.0.8", optional = true, features = ["use-std", "alloc"] } +serde = { version = "1", optional = true } +ron = { version = "0.8", optional = true } +bincode = { version = "1.3", optional = true } [dev-dependencies] bevy = { version = "0.12", default-features = true } clap = { version = "4.4", features = ["derive"] } rand = "0.8.4" rand_xoshiro = "0.6" -serde = "1.0.130" +serde = "1" serde_json = "1.0" serial_test = "2.0" diff --git a/src/lib.rs b/src/lib.rs index 08d8a24..68b6bc6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -207,16 +207,30 @@ impl Plugin for GgrsPlugin { /// Extension trait to add the GGRS plugin idiomatically to Bevy Apps pub trait GgrsApp { /// Registers a component type for saving and loading from the world. This - /// uses [Serde](`serde`) and [Postcard](`postcard`). - #[cfg(feature = "serde")] - fn rollback_component_with_postcard(&mut self) -> &mut Self + /// uses [Serde](`serde`) and [RON](`ron`). + #[cfg(feature = "ron")] + fn rollback_component_with_ron(&mut self) -> &mut Self where Type: Component + serde::Serialize + serde::de::DeserializeOwned; /// Registers a resource type for saving and loading from the world. This - /// uses [Serde](`serde`) and [Postcard](`postcard`). - #[cfg(feature = "serde")] - fn rollback_resource_with_postcard(&mut self) -> &mut Self + /// uses [Serde](`serde`) and [RON](`ron`). + #[cfg(feature = "ron")] + fn rollback_resource_with_ron(&mut self) -> &mut Self + where + Type: Resource + serde::Serialize + serde::de::DeserializeOwned; + + /// Registers a component type for saving and loading from the world. This + /// uses [Serde](`serde`) and [bincode](`bincode`). + #[cfg(feature = "bincode")] + fn rollback_component_with_bincode(&mut self) -> &mut Self + where + Type: Component + serde::Serialize + serde::de::DeserializeOwned; + + /// Registers a resource type for saving and loading from the world. This + /// uses [Serde](`serde`) and [bincode](`bincode`). + #[cfg(feature = "bincode")] + fn rollback_resource_with_bincode(&mut self) -> &mut Self where Type: Resource + serde::Serialize + serde::de::DeserializeOwned; @@ -366,19 +380,35 @@ impl GgrsApp for App { self.add_plugins(ResourceMapEntitiesPlugin::::default()) } - #[cfg(feature = "serde")] - fn rollback_component_with_postcard(&mut self) -> &mut Self + #[cfg(feature = "ron")] + fn rollback_component_with_ron(&mut self) -> &mut Self + where + Type: Component + serde::Serialize + serde::de::DeserializeOwned, + { + self.add_plugins(ComponentSnapshotPlugin::>::default()) + } + + #[cfg(feature = "ron")] + fn rollback_resource_with_ron(&mut self) -> &mut Self + where + Type: Resource + serde::Serialize + serde::de::DeserializeOwned, + { + self.add_plugins(ResourceSnapshotPlugin::>::default()) + } + + #[cfg(feature = "bincode")] + fn rollback_component_with_bincode(&mut self) -> &mut Self where Type: Component + serde::Serialize + serde::de::DeserializeOwned, { - self.add_plugins(ComponentSnapshotPlugin::>::default()) + self.add_plugins(ComponentSnapshotPlugin::>::default()) } - #[cfg(feature = "serde")] - fn rollback_resource_with_postcard(&mut self) -> &mut Self + #[cfg(feature = "bincode")] + fn rollback_resource_with_bincode(&mut self) -> &mut Self where Type: Resource + serde::Serialize + serde::de::DeserializeOwned, { - self.add_plugins(ResourceSnapshotPlugin::>::default()) + self.add_plugins(ResourceSnapshotPlugin::>::default()) } } diff --git a/src/snapshot/entity_checksum.rs b/src/snapshot/entity_checksum.rs index 8e48677..d7526bf 100644 --- a/src/snapshot/entity_checksum.rs +++ b/src/snapshot/entity_checksum.rs @@ -24,10 +24,7 @@ impl EntityChecksumPlugin { let result = ChecksumPart(hasher.finish() as u128); - trace!( - "Rollback Entities have checksum {:X}", - result.0 - ); + trace!("Rollback Entities have checksum {:X}", result.0); if let Ok(mut checksum) = checksum.get_single_mut() { *checksum = result; diff --git a/src/snapshot/strategy.rs b/src/snapshot/strategy.rs index f4b0095..6e6b029 100644 --- a/src/snapshot/strategy.rs +++ b/src/snapshot/strategy.rs @@ -93,34 +93,64 @@ impl Strategy for ReflectStrategy { } } -#[cfg(feature = "serde")] -mod serde_strategy { +#[cfg(feature = "ron")] +mod ron_strategy { use std::marker::PhantomData; - use postcard::{from_bytes, to_allocvec}; use serde::{de::DeserializeOwned, Serialize}; use crate::Strategy; - /// A [`Strategy`] based on [`Reflect`] and [`FromWorld`] - pub struct PostcardStrategy(PhantomData); + /// A [`Strategy`] based on [`serde`] and [`ron`] + pub struct RonStrategy(PhantomData); - impl Strategy for PostcardStrategy { + impl Strategy for RonStrategy { + type Target = T; + + type Stored = String; + + #[inline(always)] + fn store(target: &Self::Target) -> Self::Stored { + ron::to_string(target).unwrap() + } + + #[inline(always)] + fn load(stored: &Self::Stored) -> Self::Target { + ron::from_str(stored).unwrap() + } + } +} + +#[cfg(feature = "ron")] +pub use ron_strategy::*; + +#[cfg(feature = "bincode")] +mod bincode_strategy { + use std::marker::PhantomData; + + use serde::{de::DeserializeOwned, Serialize}; + + use crate::Strategy; + + /// A [`Strategy`] based on [`serde`] and [`bincode`] + pub struct BincodeStrategy(PhantomData); + + impl Strategy for BincodeStrategy { type Target = T; type Stored = Vec; #[inline(always)] fn store(target: &Self::Target) -> Self::Stored { - to_allocvec(target).unwrap() + bincode::serialize(target).unwrap() } #[inline(always)] fn load(stored: &Self::Stored) -> Self::Target { - from_bytes(stored).unwrap() + bincode::deserialize(stored).unwrap() } } } -#[cfg(feature = "serde")] -pub use serde_strategy::*; +#[cfg(feature = "bincode")] +pub use bincode_strategy::*;