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

Removed Redundant RwLock and Arc; Removed parking_lot Dependency #75

Merged
merged 2 commits into from
Oct 19, 2023
Merged
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ 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"]}
parking_lot = "0.12.1"

[dev-dependencies]
structopt = "0.3"
Expand Down
41 changes: 19 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
use bevy::{
ecs::schedule::{LogLevel, ScheduleBuildSettings, ScheduleLabel},
prelude::*,
reflect::{FromType, GetTypeRegistration, TypeRegistry, TypeRegistryInternal},
reflect::{FromType, GetTypeRegistration, TypeRegistryInternal},
utils::{Duration, HashMap},
};
use ggrs::{Config, InputStatus, P2PSession, PlayerHandle, SpectatorSession, SyncTestSession};
use parking_lot::RwLock;

use std::{fmt::Debug, hash::Hash, marker::PhantomData, net::SocketAddr, sync::Arc};

use world_snapshot::RollbackSnapshots;

pub use ggrs;
Expand Down Expand Up @@ -91,7 +92,7 @@ impl Default for FixedTimestepData {
pub struct RollbackFrameCount(i32);

#[derive(Resource)]
struct RollbackTypeRegistry(TypeRegistry);
struct RollbackTypeRegistry(TypeRegistryInternal);

/// Inputs from local players. You have to fill this resource in the ReadInputs schedule.
#[derive(Resource)]
Expand All @@ -103,21 +104,19 @@ pub struct LocalPlayers(pub Vec<PlayerHandle>);

impl Default for RollbackTypeRegistry {
fn default() -> Self {
Self(TypeRegistry {
internal: Arc::new(RwLock::new({
let mut r = TypeRegistryInternal::empty();
// `Parent` and `Children` must be registered so that their `ReflectMapEntities`
// data may be used.
//
// While this is a little bit of a weird spot to register these, are the only
// Bevy core types implementing `MapEntities`, so for now it's probably fine to
// just manually register these here.
//
// The user can still register any custom types with `register_rollback_type()`.
r.register::<Parent>();
r.register::<Children>();
r
})),
Self({
let mut r = TypeRegistryInternal::empty();
// `Parent` and `Children` must be registered so that their `ReflectMapEntities`
// data may be used.
//
// While this is a little bit of a weird spot to register these, are the only
// Bevy core types implementing `MapEntities`, so for now it's probably fine to
// just manually register these here.
//
// The user can still register any custom types with `register_rollback_type()`.
r.register::<Parent>();
r.register::<Children>();
r
})
}
}
Expand All @@ -128,12 +127,11 @@ impl RollbackTypeRegistry {
where
Type: GetTypeRegistration + Reflect + Default + Component,
{
let mut registry = self.0.write();
let registry = &mut self.0;
registry.register::<Type>();

let registration = registry.get_mut(std::any::TypeId::of::<Type>()).unwrap();
registration.insert(<ReflectComponent as FromType<Type>>::from_type());
drop(registry);
self
}

Expand All @@ -142,12 +140,11 @@ impl RollbackTypeRegistry {
where
Type: GetTypeRegistration + Reflect + Default + Resource,
{
let mut registry = self.0.write();
let registry = &mut self.0;
registry.register::<Type>();

let registration = registry.get_mut(std::any::TypeId::of::<Type>()).unwrap();
registration.insert(<ReflectResource as FromType<Type>>::from_type());
drop(registry);
self
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/world_snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{
ecs::{entity::EntityMap, reflect::ReflectMapEntities},
prelude::*,
reflect::{Reflect, TypeRegistry},
reflect::{Reflect, TypeRegistryInternal},
utils::HashMap,
};
use std::{fmt::Debug, num::Wrapping};
Expand Down Expand Up @@ -59,9 +59,8 @@ pub(crate) struct WorldSnapshot {
}

impl WorldSnapshot {
pub(crate) fn from_world(world: &World, type_registry: &TypeRegistry) -> Self {
pub(crate) fn from_world(world: &World, type_registry: &TypeRegistryInternal) -> Self {
let mut snapshot = WorldSnapshot::default();
let type_registry = type_registry.read();

// create a `RollbackEntity` for every entity tagged with rollback
for archetype in world.archetypes().iter() {
Expand Down Expand Up @@ -133,8 +132,7 @@ impl WorldSnapshot {
snapshot
}

pub(crate) fn write_to_world(&self, world: &mut World, type_registry: &TypeRegistry) {
let type_registry = type_registry.read();
pub(crate) fn write_to_world(&self, world: &mut World, type_registry: &TypeRegistryInternal) {
let mut rid_map = rollback_id_map(world);

// Mapping of the old entity ids ( when snapshot was taken ) to new entity ids
Expand Down