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

Support for Serde Rollback #86

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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", "ron", "bincode"]
wasm-bindgen = ["instant/wasm-bindgen", "ggrs/wasm-bindgen"]
serde = ["dep:serde"]
ron = ["serde", "dep:ron"]
bincode = ["serde", "dep:bincode"]

[dependencies]
bevy = { version = "0.12", default-features = false }
Expand All @@ -22,13 +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", 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
59 changes: 59 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,34 @@ 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 [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 [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;

/// Registers a component type for saving and loading from the world. This
/// uses [`Copy`] based snapshots for rollback.
fn rollback_component_with_copy<Type>(&mut self) -> &mut Self
Expand Down Expand Up @@ -396,4 +424,35 @@ impl GgrsApp for App {
{
self.add_plugins(ResourceChecksumPlugin::<Type>(hasher))
}
#[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::<BincodeStrategy<Type>>::default())
}

#[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::<BincodeStrategy<Type>>::default())
}
}
62 changes: 62 additions & 0 deletions src/snapshot/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,65 @@ impl<T: Reflect + FromWorld> Strategy for ReflectStrategy<T> {
target
}
}

#[cfg(feature = "ron")]
mod ron_strategy {
use std::marker::PhantomData;

use serde::{de::DeserializeOwned, Serialize};

use crate::Strategy;

/// A [`Strategy`] based on [`serde`] and [`ron`]
pub struct RonStrategy<T: Serialize + DeserializeOwned>(PhantomData<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 {
bincode::serialize(target).unwrap()
}

#[inline(always)]
fn load(stored: &Self::Stored) -> Self::Target {
bincode::deserialize(stored).unwrap()
}
}
}

#[cfg(feature = "bincode")]
pub use bincode_strategy::*;