Skip to content

Commit

Permalink
refactor: Move loading to LoadWorld schedule
Browse files Browse the repository at this point in the history
Co-authored-by: Georg Friedrich Schuppe <georg.schuppe@gmail.com>
  • Loading branch information
johanhelsing and gschup committed Oct 19, 2023
1 parent 07c4628 commit 5450a11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
utils::{Duration, HashMap},
};
use ggrs::{Config, InputStatus, P2PSession, PlayerHandle, SpectatorSession, SyncTestSession};
use schedule_systems::save_world;
use schedule_systems::{load_world, save_world};
use std::{fmt::Debug, hash::Hash, marker::PhantomData, net::SocketAddr};
use world_snapshot::RollbackSnapshots;

Expand Down Expand Up @@ -152,6 +152,10 @@ impl RollbackTypeRegistry {
#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
pub struct ReadInputs;

/// Label for the schedule which loads and overwrites a snapshot of the world.
#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
pub struct LoadWorld;

/// Label for the schedule which saves a snapshot of the current world.
#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
pub struct SaveWorld;
Expand Down Expand Up @@ -186,6 +190,7 @@ impl<C: Config> Plugin for GgrsPlugin<C> {
.add_schedule(GgrsSchedule, schedule)
.add_schedule(ReadInputs, Schedule::new())
.add_systems(PreUpdate, schedule_systems::run_ggrs_schedules::<C>)
.add_systems(LoadWorld, load_world)
.add_systems(SaveWorld, save_world);
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/schedule_systems.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
world_snapshot::{RollbackSnapshots, WorldSnapshot},
FixedTimestepData, GgrsSchedule, LocalInputs, LocalPlayers, PlayerInputs, ReadInputs,
RollbackFrameCount, RollbackTypeRegistry, SaveWorld, Session,
FixedTimestepData, GgrsSchedule, LoadWorld, LocalInputs, LocalPlayers, PlayerInputs,
ReadInputs, RollbackFrameCount, RollbackTypeRegistry, SaveWorld, Session,
};
use bevy::{prelude::*, utils::Duration};
use ggrs::{
Expand Down Expand Up @@ -156,13 +156,15 @@ pub(crate) fn handle_requests<T: Config>(requests: Vec<GGRSRequest<T>>, world: &
cell.save(frame, None, Some(checksum as u128));
}
GGRSRequest::LoadGameState { frame, .. } => {
// we don't really use the buffer provided by GGRS
debug!("restoring snapshot for frame {frame}");

world
.get_resource_mut::<RollbackFrameCount>()
.expect("Unable to find GGRS RollbackFrameCount. Did you remove it?")
.0 = frame;

// we don't really use the buffer provided by GGRS
load_world(frame, world)
world.run_schedule(LoadWorld);
}
GGRSRequest::AdvanceFrame { inputs } => advance_frame::<T>(inputs, world),
}
Expand Down Expand Up @@ -191,8 +193,11 @@ pub(crate) fn save_world(world: &mut World) {
snapshots.0[pos] = snapshot;
}

pub(crate) fn load_world(frame: i32, world: &mut World) {
debug!("restoring snapshot for frame {frame}");
pub(crate) fn load_world(world: &mut World) {
let frame = world
.get_resource::<RollbackFrameCount>()
.expect("Unable to find GGRS RollbackFrameCount. Did you remove it?")
.0;

let rollback_registry = world
.remove_resource::<RollbackTypeRegistry>()
Expand Down

0 comments on commit 5450a11

Please sign in to comment.