ux: if the game crashes unexpectedly, show a toast (#568)

Fixes #290

Displays a toast with an error message if the game just crashes on
startup. This does not cover launching a mod since that code-path does
not monitor the process' execution.
This commit is contained in:
Tyler Wilding 2024-09-21 22:12:35 -04:00 committed by GitHub
parent 8429b898a9
commit 86e68c90dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View file

@ -756,6 +756,12 @@ pub async fn get_launch_game_string(
))
}
#[derive(Clone, serde::Serialize)]
struct ToastPayload {
toast: String,
level: String,
}
#[tauri::command]
pub async fn launch_game(
config: tauri::State<'_, tokio::sync::Mutex<LauncherConfig>>,
@ -795,10 +801,23 @@ pub async fn launch_game(
tokio::spawn(async move {
let start_time = Instant::now(); // get the start time of the game
// start waiting for the game to exit
if let Err(err) = child.wait() {
match child.wait() {
Ok(status_code) => {
if !status_code.code().is_some() || status_code.code().unwrap() != 0 {
let _ = app_handle.emit_all(
"toast_msg",
ToastPayload {
toast: "Game crashed unexpectedly!".to_string(),
level: "error".to_string(),
},
);
}
}
Err(err) => {
log::error!("Error occured when waiting for game to exit: {}", err);
return;
}
}
// once the game exits pass the time the game started to the track_playtine function
if let Err(err) = track_playtime(start_time, game_name).await {
log::error!("Error occured when tracking playtime: {}", err);

View file

@ -1,6 +1,6 @@
<script>
<script lang="ts">
// Other Imports
import { onMount } from "svelte";
import { onDestroy, onMount } from "svelte";
import { Router, Route } from "svelte-navigator";
import Game from "./routes/Game.svelte";
import Settings from "./routes/Settings.svelte";
@ -15,8 +15,11 @@
import { isLoading } from "svelte-i18n";
import { getLocale, setLocale } from "$lib/rpc/config";
import GameFeature from "./routes/GameFeature.svelte";
import { listen } from "@tauri-apps/api/event";
import { toastStore } from "$lib/stores/ToastStore";
let revokeSpecificActions = false;
let toastListener: any = undefined;
// Events
onMount(async () => {
@ -32,6 +35,16 @@
if (locale !== null) {
setLocale(locale);
}
toastListener = await listen("toast_msg", (event) => {
toastStore.makeToast(event.payload.toast, event.payload.level);
});
});
onDestroy(() => {
if (toastListener !== undefined) {
toastListener();
}
});
if (!isInDebugMode()) {