From 2a7b0690f4dbc33c9fc2b0b1eeb814b44eb20c30 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Tue, 11 Mar 2025 14:55:07 -0400 Subject: Update --- src/lib.rs | 25 ++++++++++++------------- src/state.rs | 12 ++++++------ src/utils.rs | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 3c00719..9bbc60d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,8 @@ pub mod audio; pub mod net; pub mod save; -pub use utils::{erm, Erm}; -pub use anyhow::Context as ErmContext; +pub use utils::{erm, install_error_handler, Erm}; +pub use color_eyre::eyre::WrapErr; #[cfg(target_arch = "wasm32")] use winit::platform::web::EventLoopExtWebSys; @@ -54,7 +54,7 @@ where } #[cfg(not(target_arch = "wasm32"))] -pub async fn run<'a, F, G, Fut>(title: &str, w: u32, h: u32, options: Options, gnew: F) +pub async fn run<'a, F, G, Fut>(title: &str, w: u32, h: u32, options: Options, gnew: F) -> Erm<()> where Fut: std::future::Future, G: state::Game + 'static, @@ -63,6 +63,7 @@ where env_logger::Builder::new() .filter(None, log::LevelFilter::Info) .init(); + color_eyre::install().expect("failed to install panic handler"); log::info!("hello computer, starting up..."); @@ -172,19 +173,15 @@ where }, } } - st.run_update(&ctx, game); - st.run_render(&ctx, game); + st.run_update(&ctx, game)?; + st.run_render(&ctx, game)?; ctx.window.borrow_mut().swap_buffers(); } - - // event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll); - // event_loop.run(|event, elwt| { - // event_loop_body::(event, elwt); - // }).expect("window closed"); + Ok(()) } #[cfg(target_arch = "wasm32")] -pub async fn run<'a, F, G, Fut>(w: u32, h: u32, options: Options, gnew: F) +pub async fn run<'a, F, G, Fut>(w: u32, h: u32, options: Options, gnew: F) -> Erm<()> where Fut: std::future::Future, G: state::Game + 'static, @@ -193,6 +190,7 @@ where console_log::init_with_level(log::Level::Debug).unwrap(); console_error_panic_hook::set_once(); tracing_wasm::set_as_global_default(); + color_eyre::install().expect("failed to install panic handler"); log::info!("hello computer, starting up..."); @@ -303,8 +301,8 @@ where } // f.poll(); } - st.run_update(&ctx, game); - st.run_render(&ctx, game); + st.run_update(&ctx, game)?; + st.run_render(&ctx, game)?; ctx.window.request_redraw(); }, @@ -312,4 +310,5 @@ where } }); }); + Ok(()) } diff --git a/src/state.rs b/src/state.rs index 7b2de9e..1316cf2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -483,7 +483,7 @@ impl State { game.request_return(ctx, self, res); } - pub fn run_update(&mut self, ctx: &context::Context, game: &mut G) where G: Game { + pub fn run_update(&mut self, ctx: &context::Context, game: &mut G) -> utils::Erm<()> where G: Game { let now = now(ctx); let diff = now - self.last; @@ -496,20 +496,19 @@ impl State { self.acc -= DELTA_TIME; self.tick += 1; - // TODO: handle failure here in a nicer way - game.update(ctx, self).expect("game update failed"); + game.update(ctx, self)?; // if a lot of time has elapsed (e.g. if window is unfocused and not // running update loop), prevent "death spiral" if self.acc >= DELTA_TIME { self.acc = 0.0 } } + Ok(()) } - pub fn run_render(&mut self, ctx: &context::Context, game: &mut G) where G: Game { + pub fn run_render(&mut self, ctx: &context::Context, game: &mut G) -> utils::Erm<()> where G: Game { self.render_framebuffer.bind(&ctx); - // TODO: handle failure here in a nicer way - game.render(ctx, self).expect("game render failed"); + game.render(ctx, self)?; self.screen.bind(&ctx); ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 0.0)); @@ -517,5 +516,6 @@ impl State { self.shader_upscale.bind(&ctx); self.render_framebuffer.bind_texture(&ctx); ctx.render_no_geometry(); + Ok(()) } } diff --git a/src/utils.rs b/src/utils.rs index b2d1868..5d89310 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,38 @@ use serde::{Serialize, Deserialize}; -pub type Erm = anyhow::Result; +pub type Erm = color_eyre::Result; pub fn erm(e: E) -> Erm where E: std::error::Error + std::marker::Send + std::marker::Sync + 'static { - Err(anyhow::Error::from(e)) + Err(e.into()) +} + +pub struct ErrorHandler; +impl color_eyre::eyre::EyreHandler for ErrorHandler { + fn debug( + &self, + error: &(dyn std::error::Error + 'static), + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + if f.alternate() { + return core::fmt::Debug::fmt(error, f); + } + let mut first = true; + if let Some(s) = error.source() { + let errors: Vec<_> = std::iter::successors(Some(s), |e| (*e).source()).collect(); + for err in errors.iter().rev() { + writeln!(f)?; write!(f, "{}{}", if first {""} else {" - "}, err)?; + first = false; + } + } + writeln!(f)?; write!(f, "{}{}", if first {""} else {" - "}, error)?; + Ok(()) + } +} + +pub fn install_error_handler() { + let (panic_hook, _) = color_eyre::config::HookBuilder::default().into_hooks(); + panic_hook.install(); + color_eyre::eyre::set_hook(Box::new(move |_| Box::new(ErrorHandler))).expect("failed to install error handler"); } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] -- cgit v1.2.3