From f577cc994818b5a529092b3d2f600daec729bfa0 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 12 Nov 2018 11:12:14 -0800 Subject: [PATCH] consolidate the panic/unwind code --- editor/src/plugins/sim_controls.rs | 21 ++++----------------- editor/src/ui.rs | 8 +++++++- sim/src/sim.rs | 23 +++++++++++++++-------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/editor/src/plugins/sim_controls.rs b/editor/src/plugins/sim_controls.rs index e2cf397289..be31788cb5 100644 --- a/editor/src/plugins/sim_controls.rs +++ b/editor/src/plugins/sim_controls.rs @@ -4,7 +4,6 @@ use objects::SIM; use piston::input::Key; use sim::{Benchmark, ScoreSummary, TIMESTEP}; use std::mem; -use std::panic; use std::time::{Duration, Instant}; use ui::{PerMapUI, PluginsPerMap}; @@ -82,10 +81,10 @@ impl SimController { self.last_step = Some(Instant::now()); self.benchmark = Some(primary.sim.start_benchmark()); } else if input.unimportant_key_pressed(Key::M, SIM, "run one step") { - step_sim("Primary", primary); + primary.sim.step(&primary.map, &primary.control_map); primary.recalculate_current_selection = true; if let Some((s, _)) = secondary { - step_sim("Secondary", s); + s.sim.step(&s.map, &s.control_map); } } } @@ -117,10 +116,10 @@ impl SimController { // TODO https://gafferongames.com/post/fix_your_timestep/ let dt_s = elapsed_seconds(tick); if dt_s >= TIMESTEP.value_unsafe / self.desired_speed { - step_sim("Primary", primary); + primary.sim.step(&primary.map, &primary.control_map); primary.recalculate_current_selection = true; if let Some((s, _)) = secondary { - step_sim("Secondary", s); + s.sim.step(&s.map, &s.control_map); } self.last_step = Some(Instant::now()); } @@ -200,15 +199,3 @@ fn summarize(txt: &mut Text, summary: ScoreSummary) { )); txt.add_line(format!(" {} total", summary.total_driving_trip_time)); } - -fn step_sim(name: &str, ui: &mut PerMapUI) { - if let Err(err) = panic::catch_unwind(panic::AssertUnwindSafe(|| { - ui.sim.step(&ui.map, &ui.control_map); - })) { - error!( - "{} sim failed at {} while processing {:?}", - name, ui.sim.time, ui.sim.current_agent_for_debugging - ); - panic::resume_unwind(err); - } -} diff --git a/editor/src/ui.rs b/editor/src/ui.rs index 2fd4fd8450..a4c89fa05d 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -53,7 +53,13 @@ impl GUI for UI { })) { Ok(()) => {} Err(err) => { - error!("UI broke. Sim time is {}", self.primary.sim.time); + error!("********************************************************************************"); + error!("UI broke! Primary sim:"); + self.primary.sim.dump_before_abort(); + if let Some((s, _)) = &self.secondary { + error!("Secondary sim:"); + s.sim.dump_before_abort(); + } self.save_editor_state(); panic::resume_unwind(err); } diff --git a/sim/src/sim.rs b/sim/src/sim.rs index 0de266ddba..c2d3e7de4e 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -55,7 +55,7 @@ pub struct Sim { // This should only be Some in the middle of step(). The caller of step() can grab this if // step() panics. - pub current_agent_for_debugging: Option, + current_agent_for_debugging: Option, } impl Sim { @@ -129,17 +129,24 @@ impl Sim { } } + pub fn dump_before_abort(&self) { + error!("********************************************************************************"); + error!( + "At {} while processing {:?}", + self.time, self.current_agent_for_debugging + ); + // TODO Most recent before current time + if let Ok(s) = self.find_most_recent_savestate() { + error!("Debug from {}", s); + } + } + pub fn step(&mut self, map: &Map, control_map: &ControlMap) -> Vec { match self.inner_step(map, control_map) { Ok(events) => events, Err(e) => { - error!( - "\nAt {} while processing {:?}:{}", - self.time, self.current_agent_for_debugging, e - ); - if let Ok(s) = self.find_most_recent_savestate() { - error!("Debug from {}", s); - } + self.dump_before_abort(); + error!("{}", e); process::exit(1); } }