use bincode, not json, for savestates. add loading screens around saving/loading

This commit is contained in:
Dustin Carlino 2019-06-27 09:45:04 -07:00
parent f6dafe2118
commit ab088479dd
3 changed files with 43 additions and 31 deletions

View File

@ -123,7 +123,11 @@ impl State for ABTestMode {
self.primary_agent_tools.event(ctx, ui, &mut self.menu);
if self.menu.action("save state") {
self.savestate(ui);
ctx.loading_screen("savestate", |_, timer| {
timer.start("save all state");
self.savestate(ui);
timer.stop("save all state");
});
}
if self.diff_trip.is_some() {

View File

@ -135,36 +135,44 @@ impl State for SandboxMode {
return Transition::Replace(Box::new(SandboxMode::new(ctx)));
}
if self.menu.action("save sim state") {
ui.primary.sim.save();
ctx.loading_screen("savestate", |_, timer| {
timer.start("save sim state");
ui.primary.sim.save();
timer.stop("save sim state");
});
}
if self.menu.action("load previous sim state") {
let prev_state = ui
.primary
.sim
.find_previous_savestate(ui.primary.sim.time());
match prev_state
.clone()
.and_then(|path| Sim::load_savestate(path).ok())
{
Some(new_sim) => {
ui.primary.sim = new_sim;
ui.recalculate_current_selection(ctx);
ctx.loading_screen("load previous savestate", |ctx, mut timer| {
let prev_state = ui
.primary
.sim
.find_previous_savestate(ui.primary.sim.time());
match prev_state
.clone()
.and_then(|path| Sim::load_savestate(path, &mut timer).ok())
{
Some(new_sim) => {
ui.primary.sim = new_sim;
ui.recalculate_current_selection(ctx);
}
None => println!("Couldn't load previous savestate {:?}", prev_state),
}
None => println!("Couldn't load previous savestate {:?}", prev_state),
}
});
}
if self.menu.action("load next sim state") {
let next_state = ui.primary.sim.find_next_savestate(ui.primary.sim.time());
match next_state
.clone()
.and_then(|path| Sim::load_savestate(path).ok())
{
Some(new_sim) => {
ui.primary.sim = new_sim;
ui.recalculate_current_selection(ctx);
ctx.loading_screen("load next savestate", |ctx, mut timer| {
let next_state = ui.primary.sim.find_next_savestate(ui.primary.sim.time());
match next_state
.clone()
.and_then(|path| Sim::load_savestate(path, &mut timer).ok())
{
Some(new_sim) => {
ui.primary.sim = new_sim;
ui.recalculate_current_selection(ctx);
}
None => println!("Couldn't load next savestate {:?}", next_state),
}
None => println!("Couldn't load next savestate {:?}", next_state),
}
});
}
if let Some(t) = time_controls(ctx, ui, &mut self.menu) {

View File

@ -571,20 +571,20 @@ impl Sim {
// If we wanted to be even more reproducible, we'd encode RNG seed, version of code, etc,
// but that's overkill right now.
let path = format!(
"../data/save/{}_{}/{}/{}.json",
"../data/save/{}_{}/{}/{}.bin",
self.map_name,
self.edits_name,
self.run_name,
self.time.as_filename()
);
abstutil::write_json(&path, &self).expect("Writing sim state failed");
abstutil::write_binary(&path, &self).expect("Writing sim state failed");
println!("Saved to {}", path);
path
}
pub fn find_previous_savestate(&self, base_time: Duration) -> Option<String> {
abstutil::find_prev_file(format!(
"../data/save/{}_{}/{}/{}.json",
"../data/save/{}_{}/{}/{}.bin",
self.map_name,
self.edits_name,
self.run_name,
@ -594,7 +594,7 @@ impl Sim {
pub fn find_next_savestate(&self, base_time: Duration) -> Option<String> {
abstutil::find_next_file(format!(
"../data/save/{}_{}/{}/{}.json",
"../data/save/{}_{}/{}/{}.bin",
self.map_name,
self.edits_name,
self.run_name,
@ -602,9 +602,9 @@ impl Sim {
))
}
pub fn load_savestate(path: String) -> Result<Sim, std::io::Error> {
pub fn load_savestate(path: String, timer: &mut Timer) -> Result<Sim, std::io::Error> {
println!("Loading {}", path);
abstutil::read_json(&path)
abstutil::read_binary(&path, timer)
}
}