From 476d1a7aea4f5114ae52ebac529e0a4eabf2091c Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Fri, 17 Nov 2023 16:11:28 +0100 Subject: [PATCH] Add checksum_hasher --- examples/stress_tests/particles.rs | 6 +++--- src/snapshot/checksum.rs | 8 ++++---- src/snapshot/component_checksum.rs | 10 ++++++---- src/snapshot/entity_checksum.rs | 8 +++++--- src/snapshot/mod.rs | 12 ++++++++++-- src/snapshot/resource_checksum.rs | 6 +++--- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/examples/stress_tests/particles.rs b/examples/stress_tests/particles.rs index 7319b69..15d7a33 100644 --- a/examples/stress_tests/particles.rs +++ b/examples/stress_tests/particles.rs @@ -1,10 +1,10 @@ use bevy::{math::vec3, prelude::*, utils::HashMap, window::WindowResolution}; -use bevy_ggrs::{prelude::*, LocalInputs, LocalPlayers}; +use bevy_ggrs::{checksum_hasher, prelude::*, LocalInputs, LocalPlayers}; use clap::Parser; use ggrs::{DesyncDetection, UdpNonBlockingSocket}; use rand::{Rng, SeedableRng}; use std::{ - hash::{BuildHasher, Hash, Hasher}, + hash::{Hash, Hasher}, net::SocketAddr, }; @@ -201,7 +201,7 @@ fn main() -> Result<(), Box> { .checksum_component_with_hash::() // ...or you can provide a custom hashing process .checksum_component::(|transform| { - let mut hasher = bevy::utils::FixedState.build_hasher(); + let mut hasher = checksum_hasher(); // In this demo we only translate particles, so only that value // needs to be tracked. diff --git a/src/snapshot/checksum.rs b/src/snapshot/checksum.rs index 51fdc0f..ff8f98f 100644 --- a/src/snapshot/checksum.rs +++ b/src/snapshot/checksum.rs @@ -1,11 +1,11 @@ use std::{ - hash::{BuildHasher, Hash, Hasher}, + hash::{Hash, Hasher}, marker::PhantomData, }; use bevy::prelude::*; -use crate::{SaveWorld, SaveWorldSet}; +use crate::{checksum_hasher, SaveWorld, SaveWorldSet}; /// Flags an entity as containing a checksum for a type `T` #[derive(Component)] @@ -26,9 +26,9 @@ impl Default for ChecksumFlag { pub struct ChecksumPart(pub u128); impl ChecksumPart { - /// Converts a provided value `T` into a [Hash] using Bevy's [`FixedState`](`bevy::utils::FixedState`) hasher. + /// Converts a provided value `T` into a [Hash] using [`checksum_hasher`]. pub fn from_value(value: &T) -> Self { - let mut hasher = bevy::utils::FixedState.build_hasher(); + let mut hasher = checksum_hasher(); value.hash(&mut hasher); diff --git a/src/snapshot/component_checksum.rs b/src/snapshot/component_checksum.rs index a7999cd..e7efa4a 100644 --- a/src/snapshot/component_checksum.rs +++ b/src/snapshot/component_checksum.rs @@ -1,8 +1,10 @@ -use std::hash::{BuildHasher, Hash, Hasher}; +use std::hash::{Hash, Hasher}; use bevy::prelude::*; -use crate::{ChecksumFlag, ChecksumPart, Rollback, RollbackOrdered, SaveWorld, SaveWorldSet}; +use crate::{ + checksum_hasher, ChecksumFlag, ChecksumPart, Rollback, RollbackOrdered, SaveWorld, SaveWorldSet, +}; /// A [`Plugin`] which will track the [`Component`] `C` on [`Rollback Entities`](`Rollback`) and ensure a /// [`ChecksumPart`] is available and updated. This can be used to generate a [`Checksum`](`crate::Checksum`). @@ -33,7 +35,7 @@ use crate::{ChecksumFlag, ChecksumPart, Rollback, RollbackOrdered, SaveWorld, Sa pub struct ComponentChecksumPlugin(pub for<'a> fn(&'a C) -> u64); fn default_hasher(component: &C) -> u64 { - let mut hasher = bevy::utils::FixedState.build_hasher(); + let mut hasher = checksum_hasher(); component.hash(&mut hasher); hasher.finish() } @@ -64,7 +66,7 @@ where &mut ChecksumPart, (Without, With>), >| { - let mut hasher = bevy::utils::FixedState.build_hasher(); + let mut hasher = checksum_hasher(); let mut result = 0; diff --git a/src/snapshot/entity_checksum.rs b/src/snapshot/entity_checksum.rs index d7526bf..f45bdc0 100644 --- a/src/snapshot/entity_checksum.rs +++ b/src/snapshot/entity_checksum.rs @@ -1,8 +1,10 @@ -use std::hash::{BuildHasher, Hash, Hasher}; +use std::hash::{Hash, Hasher}; use bevy::prelude::*; -use crate::{ChecksumFlag, ChecksumPart, Rollback, RollbackOrdered, SaveWorld, SaveWorldSet}; +use crate::{ + checksum_hasher, ChecksumFlag, ChecksumPart, Rollback, RollbackOrdered, SaveWorld, SaveWorldSet, +}; pub struct EntityChecksumPlugin; @@ -14,7 +16,7 @@ impl EntityChecksumPlugin { active_entities: Query<&Rollback, (With, Without>)>, mut checksum: Query<&mut ChecksumPart, (Without, With>)>, ) { - let mut hasher = bevy::utils::FixedState.build_hasher(); + let mut hasher = checksum_hasher(); // The quantity of active rollback entities must be synced. active_entities.iter().len().hash(&mut hasher); diff --git a/src/snapshot/mod.rs b/src/snapshot/mod.rs index 09d2b5d..f2b8c82 100644 --- a/src/snapshot/mod.rs +++ b/src/snapshot/mod.rs @@ -1,6 +1,9 @@ use crate::{ConfirmedFrameCount, Rollback, DEFAULT_FPS}; -use bevy::{prelude::*, utils::HashMap}; -use std::{collections::VecDeque, marker::PhantomData}; +use bevy::{ + prelude::*, + utils::{AHasher, FixedState, HashMap}, +}; +use std::{collections::VecDeque, hash::BuildHasher, marker::PhantomData}; mod checksum; mod component_checksum; @@ -238,3 +241,8 @@ impl GgrsComponentSnapshot { self.snapshot.iter() } } + +/// Returns a hasher built using Bevy's [FixedState] appropriate for creating checksums +pub fn checksum_hasher() -> AHasher { + FixedState.build_hasher() +} diff --git a/src/snapshot/resource_checksum.rs b/src/snapshot/resource_checksum.rs index b8e470f..5a1d319 100644 --- a/src/snapshot/resource_checksum.rs +++ b/src/snapshot/resource_checksum.rs @@ -1,8 +1,8 @@ -use std::hash::{BuildHasher, Hash, Hasher}; +use std::hash::{Hash, Hasher}; use bevy::prelude::*; -use crate::{ChecksumFlag, ChecksumPart, Rollback, SaveWorld, SaveWorldSet}; +use crate::{checksum_hasher, ChecksumFlag, ChecksumPart, Rollback, SaveWorld, SaveWorldSet}; /// Plugin which will track the [`Resource`] `R` and ensure a [`ChecksumPart`] is /// available and updated. This can be used to generate a [`Checksum`](`crate::Checksum`). @@ -33,7 +33,7 @@ use crate::{ChecksumFlag, ChecksumPart, Rollback, SaveWorld, SaveWorldSet}; pub struct ResourceChecksumPlugin(pub for<'a> fn(&'a R) -> u64); fn default_hasher(resource: &R) -> u64 { - let mut hasher = bevy::utils::FixedState.build_hasher(); + let mut hasher = checksum_hasher(); resource.hash(&mut hasher); hasher.finish() }