diff --git a/Cargo.lock b/Cargo.lock index be563916..a4c7da04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2316,6 +2316,7 @@ dependencies = [ "materialcraft-automation", "materialcraft-engine", "materialcraft-ui-egui", + "rfd", "serde_json", "winresource", ] diff --git a/apps/materialcraft/Cargo.toml b/apps/materialcraft/Cargo.toml index 7b5a2caa..83f388f9 100644 --- a/apps/materialcraft/Cargo.toml +++ b/apps/materialcraft/Cargo.toml @@ -19,6 +19,8 @@ egui = { workspace = true } image = { workspace = true } serde_json = { workspace = true } log = { workspace = true } +# A start that never opens a window says why in an error dialog (src/start_failure.rs). +rfd = { workspace = true } [build-dependencies] winresource = "0.1" diff --git a/apps/materialcraft/src/main.rs b/apps/materialcraft/src/main.rs index 4e9b18bf..90270a9b 100644 --- a/apps/materialcraft/src/main.rs +++ b/apps/materialcraft/src/main.rs @@ -6,6 +6,7 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod control_server; +mod start_failure; use materialcraft_engine::Session; use materialcraft_ui_egui::MaterialCraftApp; @@ -109,6 +110,8 @@ fn main() { ); if let Err(e) = r { eprintln!("materialcraft: {e}"); + // Say why instead of exiting silently (`start_failure`). + start_failure::report(&e); std::process::exit(1); } } diff --git a/apps/materialcraft/src/start_failure.rs b/apps/materialcraft/src/start_failure.rs new file mode 100644 index 00000000..17596045 --- /dev/null +++ b/apps/materialcraft/src/start_failure.rs @@ -0,0 +1,52 @@ +//! A window that can't start, or stops, says why instead of the app quitting silently. +//! +//! Without a usable GPU, graphics driver or desktop (a broken or very old driver, a remote or +//! virtual machine, a program started by a service or a test harness), the window's surface +//! can't be configured and eframe returns an error. MaterialCraft used to log it and exit with no +//! window, which looks like "the app doesn't open". +//! winget-pkgs validation starts installed programs without a desktop, so a version that fails +//! like this never reaches WinGet. +//! +//! Now the reason also goes to an error dialog, as FilmCraft does since 0.14.0. The dialog keeps +//! the process alive until it is dismissed; the exit code afterwards is still the error's. + +/// What the dialog and the log say. Any error `run_native` returns lands here. One from the app's +/// own start-up (`AppCreation`, e.g. a `--control` file it can't write) says only that it couldn't +/// start. Any other comes from the window or graphics, possibly after the window was up, so +/// graphics is named as the likely cause, not the certain one. +pub(crate) fn message(error: &eframe::Error) -> String { + match error { + eframe::Error::AppCreation(_) => format!("MaterialCraft could not start: {error}"), + _ => format!( + "MaterialCraft's window could not start or stopped with an error: {error}\n\nIf this keeps happening, updating the graphics driver often fixes it." + ), + } +} + +/// Logs the failure and shows it in a dialog, which stays until the user closes it. +pub(crate) fn report(error: &eframe::Error) { + let message = message(error); + log::error!("{message}"); + let _ = rfd::MessageDialog::new().set_title("MaterialCraft").set_description(&message).set_level(rfd::MessageLevel::Error).show(); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_window_or_graphics_error_names_the_driver_as_the_likely_fix() { + let text = message(&eframe::Error::Wgpu(eframe::egui_wgpu::WgpuError::NoSurfaceFormatsAvailable)); + assert!(text.starts_with("MaterialCraft's window could not start or stopped"), "{text}"); + assert!(text.contains("no valid format for the surface"), "{text}"); + assert!(text.contains("graphics driver"), "{text}"); + } + + #[test] + fn an_app_start_up_error_does_not_blame_the_graphics() { + let text = message(&eframe::Error::AppCreation("can't write the control file".into())); + assert!(text.starts_with("MaterialCraft could not start"), "{text}"); + assert!(text.contains("can't write the control file"), "{text}"); + assert!(!text.contains("graphics"), "{text}"); + } +}