Skip to content

Commit

Permalink
Switch to ron and bincode as serde strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
bushrat011899 committed Nov 9, 2023
1 parent eb86221 commit f30a67a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 27 deletions.
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"

Expand Down
54 changes: 42 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,30 @@ impl<C: Config> Plugin for GgrsPlugin<C> {
/// 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<Type>(&mut self) -> &mut Self
/// uses [Serde](`serde`) and [RON](`ron`).
#[cfg(feature = "ron")]
fn rollback_component_with_ron<Type>(&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<Type>(&mut self) -> &mut Self
/// uses [Serde](`serde`) and [RON](`ron`).
#[cfg(feature = "ron")]
fn rollback_resource_with_ron<Type>(&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<Type>(&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<Type>(&mut self) -> &mut Self
where
Type: Resource + serde::Serialize + serde::de::DeserializeOwned;

Expand Down Expand Up @@ -411,19 +425,35 @@ impl GgrsApp for App {
self.add_plugins(ResourceChecksumPlugin::<Type>(hasher))
}

#[cfg(feature = "serde")]
fn rollback_component_with_postcard<Type>(&mut self) -> &mut Self
#[cfg(feature = "ron")]
fn rollback_component_with_ron<Type>(&mut self) -> &mut Self
where
Type: Component + serde::Serialize + serde::de::DeserializeOwned,
{
self.add_plugins(ComponentSnapshotPlugin::<RonStrategy<Type>>::default())
}

#[cfg(feature = "ron")]
fn rollback_resource_with_ron<Type>(&mut self) -> &mut Self
where
Type: Resource + serde::Serialize + serde::de::DeserializeOwned,
{
self.add_plugins(ResourceSnapshotPlugin::<RonStrategy<Type>>::default())
}

#[cfg(feature = "bincode")]
fn rollback_component_with_bincode<Type>(&mut self) -> &mut Self
where
Type: Component + serde::Serialize + serde::de::DeserializeOwned,
{
self.add_plugins(ComponentSnapshotPlugin::<PostcardStrategy<Type>>::default())
self.add_plugins(ComponentSnapshotPlugin::<BincodeStrategy<Type>>::default())
}

#[cfg(feature = "serde")]
fn rollback_resource_with_postcard<Type>(&mut self) -> &mut Self
#[cfg(feature = "bincode")]
fn rollback_resource_with_bincode<Type>(&mut self) -> &mut Self
where
Type: Resource + serde::Serialize + serde::de::DeserializeOwned,
{
self.add_plugins(ResourceSnapshotPlugin::<PostcardStrategy<Type>>::default())
self.add_plugins(ResourceSnapshotPlugin::<BincodeStrategy<Type>>::default())
}
}
50 changes: 40 additions & 10 deletions src/snapshot/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,64 @@ impl<T: Reflect + FromWorld> Strategy for ReflectStrategy<T> {
}
}

#[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<T: Serialize + DeserializeOwned>(PhantomData<T>);
/// A [`Strategy`] based on [`serde`] and [`ron`]
pub struct RonStrategy<T: Serialize + DeserializeOwned>(PhantomData<T>);

impl<T: Serialize + DeserializeOwned> Strategy for PostcardStrategy<T> {
impl<T: Serialize + DeserializeOwned> Strategy for RonStrategy<T> {
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<T: Serialize + DeserializeOwned>(PhantomData<T>);

impl<T: Serialize + DeserializeOwned> Strategy for BincodeStrategy<T> {
type Target = T;

type Stored = Vec<u8>;

#[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::*;

0 comments on commit f30a67a

Please sign in to comment.