Skip to content

Commit

Permalink
Merge pull request #104 from aleksa2808/fix/checksum-portability
Browse files Browse the repository at this point in the history
Fix checksum portability
  • Loading branch information
johanhelsing committed Feb 22, 2024
2 parents 5d3e2ff + 70a2f8b commit 87b5866
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ wasm-bindgen = ["instant/wasm-bindgen", "ggrs/wasm-bindgen"]

[dependencies]
bevy = { version = "0.12", default-features = false }
bytemuck = { version = "1.7", features=["derive"]}
bytemuck = { version = "1.7", features = ["derive"] }
instant = { version = "0.1", optional = true }
log = "0.4"
#ggrs = { version= "0.10.0", features=["sync-send"]}
ggrs = { git = "https://github.com/gschup/ggrs", features=["sync-send"]}
ggrs = { git = "https://github.com/gschup/ggrs", features = ["sync-send"] }
seahash = "4.1"

[dev-dependencies]
bevy = { version = "0.12", default-features = true }
Expand Down
6 changes: 3 additions & 3 deletions src/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ impl<'w, 's, 'a> AddRollbackCommandExtension for EntityCommands<'w, 's, 'a> {
/// A [`Resource`] which provides methods for stable ordering of [`Rollback`] flags.
#[derive(Resource, Default, Clone)]
pub struct RollbackOrdered {
order: HashMap<Rollback, usize>,
order: HashMap<Rollback, u64>,
sorted: Vec<Rollback>,
}

impl RollbackOrdered {
/// Register a new [`Rollback`] for explicit ordering.
fn push(&mut self, rollback: Rollback) -> &mut Self {
self.sorted.push(rollback);
self.order.insert(rollback, self.sorted.len() - 1);
self.order.insert(rollback, self.sorted.len() as u64 - 1);

self
}
Expand All @@ -75,7 +75,7 @@ impl RollbackOrdered {
}

/// Returns a unique and order stable index for the provided [`Rollback`].
pub fn order(&self, rollback: Rollback) -> usize {
pub fn order(&self, rollback: Rollback) -> u64 {
self.order
.get(&rollback)
.copied()
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot/component_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
let mut result = 0;

for (&rollback, component) in components.iter() {
let mut hasher = hasher.clone();
let mut hasher = hasher;

// Hashing the rollback index ensures this hash is unique and stable
rollback_ordered.order(rollback).hash(&mut hasher);
Expand Down
4 changes: 2 additions & 2 deletions src/snapshot/entity_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ impl EntityChecksumPlugin {
let mut hasher = checksum_hasher();

// The quantity of active rollback entities must be synced.
active_entities.iter().len().hash(&mut hasher);
(active_entities.iter().len() as u64).hash(&mut hasher);

// The quantity of total spawned rollback entities must be synced.
rollback_ordered.len().hash(&mut hasher);
(rollback_ordered.len() as u64).hash(&mut hasher);

let result = ChecksumPart(hasher.finish() as u128);

Expand Down
14 changes: 6 additions & 8 deletions src/snapshot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{ConfirmedFrameCount, Rollback, DEFAULT_FPS};
use bevy::{
prelude::*,
utils::{AHasher, FixedState, HashMap},
};
use std::{collections::VecDeque, hash::BuildHasher, marker::PhantomData};
use bevy::{prelude::*, utils::HashMap};
use seahash::SeaHasher;
use std::{collections::VecDeque, marker::PhantomData};

mod checksum;
mod component_checksum;
Expand Down Expand Up @@ -242,7 +240,7 @@ impl<For, As> GgrsComponentSnapshot<For, As> {
}
}

/// Returns a hasher built using Bevy's [FixedState] appropriate for creating checksums
pub fn checksum_hasher() -> AHasher {
FixedState.build_hasher()
/// Returns a hasher built using the `seahash` library appropriate for creating portable checksums.
pub fn checksum_hasher() -> SeaHasher {
SeaHasher::new()
}

0 comments on commit 87b5866

Please sign in to comment.