Add dialog when app is unresponsive to reload

Checks the electron event 'unresponsive' and triggers a reload / cancel
dialog. Allows all scripts to be killed, checked by default.
Also adds a warning to the terminal when noScript has been executed.
This commit is contained in:
Martin Fournier 2021-12-22 14:32:36 -05:00
parent a97d2a93cb
commit 9ce8bdd29b
2 changed files with 34 additions and 8 deletions

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const { app, BrowserWindow, Menu, shell } = require("electron"); const { app, BrowserWindow, Menu, shell, dialog } = require("electron");
const greenworks = require("./greenworks"); const greenworks = require("./greenworks");
if (greenworks.init()) { if (greenworks.init()) {
@ -66,6 +66,36 @@ function createWindow(killall) {
}, 1000); }, 1000);
win.achievementsIntervalID = intervalID; win.achievementsIntervalID = intervalID;
const reloadAndKill = (killScripts = true) => {
setStopProcessHandler(app, win, false);
if (intervalID) clearInterval(intervalID);
win.webContents.forcefullyCrashRenderer();
win.close();
createWindow(killScripts);
};
const promptForReload = () => {
win.off('unresponsive', promptForReload);
dialog.showMessageBox({
type: 'error',
title: 'Bitburner > Application Unresponsive',
message: 'The application is unresponsive, possibly due to an infinite loop in your scripts.',
detail:' Did you forget a ns.sleep(x)?\n\n' +
'The application will be restarted for you, do you want to kill all running scripts?',
buttons: ['Restart', 'Cancel'],
defaultId: 0,
checkboxLabel: 'Kill all running scripts',
checkboxChecked: true,
noLink: true,
}).then(({response, checkboxChecked}) => {
if (response === 0) {
reloadAndKill(checkboxChecked);
} else {
win.on('unresponsive', promptForReload)
}
});
}
win.on('unresponsive', promptForReload);
// Create the Application's main menu // Create the Application's main menu
Menu.setApplicationMenu( Menu.setApplicationMenu(
Menu.buildFromTemplate([ Menu.buildFromTemplate([
@ -93,13 +123,7 @@ function createWindow(killall) {
}, },
{ {
label: "reload & kill all scripts", label: "reload & kill all scripts",
click: () => { click: reloadAndKill
setStopProcessHandler(app, win, false);
if (intervalID) clearInterval(intervalID);
win.webContents.forcefullyCrashRenderer();
win.close();
createWindow(true);
},
}, },
], ],
}, },

@ -34,6 +34,7 @@ import { parse } from "acorn";
import { simple as walksimple } from "acorn-walk"; import { simple as walksimple } from "acorn-walk";
import { areFilesEqual } from "./Terminal/DirectoryHelpers"; import { areFilesEqual } from "./Terminal/DirectoryHelpers";
import { Player } from "./Player"; import { Player } from "./Player";
import { Terminal } from "./Terminal";
// Netscript Ports are instantiated here // Netscript Ports are instantiated here
export const NetscriptPorts: IPort[] = []; export const NetscriptPorts: IPort[] = [];
@ -609,6 +610,7 @@ export function updateOnlineScriptTimes(numCycles = 1): void {
export function loadAllRunningScripts(): void { export function loadAllRunningScripts(): void {
const skipScriptLoad = window.location.href.toLowerCase().indexOf("?noscripts") !== -1; const skipScriptLoad = window.location.href.toLowerCase().indexOf("?noscripts") !== -1;
if (skipScriptLoad) { if (skipScriptLoad) {
Terminal.warn('Skipped loading player scripts during startup');
console.info("Skipping the load of any scripts during startup"); console.info("Skipping the load of any scripts during startup");
} }
for (const server of GetAllServers()) { for (const server of GetAllServers()) {