1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! A wrapper for panics using Bevy's plugin system.
//!
//! On supported platforms (windows, macos, linux) will produce a popup using the `msgbox` crate in addition to writing via `log::error!`, or if `bevy::log::LogPlugin` is not enabled, `stderr`.

use std::sync::Arc;

use bevy::prelude::*;

pub trait PanicHandleFn<Res>: Fn(&std::panic::PanicInfo) -> Res + Send + Sync + 'static {}
impl<Res, T: Fn(&std::panic::PanicInfo) -> Res + Send + Sync + 'static> PanicHandleFn<Res> for T {}

#[derive(Default)]
pub struct PanicHandlerBuilder {
    custom_name: Option<Arc<dyn PanicHandleFn<String>>>,
    custom_body: Option<Arc<dyn PanicHandleFn<String>>>,
    custom_hook: Option<Arc<dyn PanicHandleFn<()>>>,
}
impl PanicHandlerBuilder {
    #[must_use]
    /// Builds the `PanicHandler`
    pub fn build(self) -> PanicHandler {
        PanicHandler {
            custom_title: {
                self.custom_name
                    .unwrap_or(Arc::new(|_: &std::panic::PanicInfo| {
                        "Fatal Error".to_owned()
                    }))
            },
            custom_body: {
                self.custom_body.unwrap_or(Arc::new(|info| {
                    format!(
                        "Unhandled panic! @ {}:\n{}",
                        info.location()
                            .map_or("Unknown Location".to_owned(), ToString::to_string),
                        info.payload().downcast_ref::<String>().unwrap_or(
                            &((*info.payload().downcast_ref::<&str>().unwrap_or(&"No Info"))
                                .to_string())
                        )
                    )
                }))
            },
            custom_hook: { self.custom_hook.unwrap_or(Arc::new(|_| {})) },
        }
    }

    #[must_use]
    /// After the popup is closed, the previously existing panic hook will be called
    pub fn take_call_from_existing(mut self) -> Self {
        self.custom_hook = Some(Arc::new(std::panic::take_hook()));
        self
    }

    #[must_use]
    /// After the popup is closed, this function will be called
    pub fn set_call_func(mut self, call_func: impl PanicHandleFn<()>) -> Self {
        self.custom_hook = Some(Arc::new(call_func));
        self
    }

    #[must_use]
    /// The popup title will be set to the result of this function
    pub fn set_title_func(mut self, title_func: impl PanicHandleFn<String>) -> Self {
        self.custom_name = Some(Arc::new(title_func));
        self
    }

    #[must_use]
    /// The popup body will be set to the result of this function
    pub fn set_body_func(mut self, body_func: impl PanicHandleFn<String>) -> Self {
        self.custom_body = Some(Arc::new(body_func));
        self
    }
}

/// Bevy plugin that opens a popup window on panic & logs an error
#[derive(Clone)]
pub struct PanicHandler {
    pub custom_title: Arc<dyn PanicHandleFn<String>>,
    pub custom_body: Arc<dyn PanicHandleFn<String>>,
    pub custom_hook: Arc<dyn PanicHandleFn<()>>,
}
impl PanicHandler {
    #[must_use]
    #[allow(clippy::new_ret_no_self)]
    /// Create a new builder. The custom hook does nothing.
    pub fn new() -> PanicHandlerBuilder {
        PanicHandlerBuilder::default()
    }

    #[must_use]
    /// Create a new builder. The custom hook is taken from `std::panic::take_hook()`
    pub fn new_take_old() -> PanicHandlerBuilder {
        PanicHandlerBuilder::default().take_call_from_existing()
    }
}

impl Plugin for PanicHandler {
    fn build(&self, _: &mut App) {
        let handler = self.clone();
        std::panic::set_hook(Box::new(move |info| {
            let title_string = (handler.custom_title)(info);
            let info_string = (handler.custom_body)(info);

            // Known limitations: Logging in tests prints to stdout immediately.
            // This will print duplicate messages to stdout if the default panic hook is being used & env_logger is initialized.
            bevy::log::error!("{title_string}\n{info_string}");

            // Don't interrupt test execution with a popup, and dont try on unsupported platforms.
            #[cfg(all(not(test), any(target_os = "windows", target_os = "macos", target_os = "linux")))]
            { _ = msgbox::create(&title_string, &info_string, msgbox::IconType::Error); }

            (handler.custom_hook)(info);
        }));
    }
}