From 9ce8bdd29b97c916d64e2700de2d27dcf546712b Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Wed, 22 Dec 2021 14:32:36 -0500 Subject: [PATCH] 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. --- electron/main.js | 40 ++++++++++++++++++++++++++++++++-------- src/NetscriptWorker.ts | 2 ++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/electron/main.js b/electron/main.js index b7b1d17c0..6fe5c8c05 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,5 +1,5 @@ /* 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"); if (greenworks.init()) { @@ -66,6 +66,36 @@ function createWindow(killall) { }, 1000); 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 Menu.setApplicationMenu( Menu.buildFromTemplate([ @@ -93,13 +123,7 @@ function createWindow(killall) { }, { label: "reload & kill all scripts", - click: () => { - setStopProcessHandler(app, win, false); - if (intervalID) clearInterval(intervalID); - win.webContents.forcefullyCrashRenderer(); - win.close(); - createWindow(true); - }, + click: reloadAndKill }, ], }, diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index 82b8c6861..82dd53c17 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -34,6 +34,7 @@ import { parse } from "acorn"; import { simple as walksimple } from "acorn-walk"; import { areFilesEqual } from "./Terminal/DirectoryHelpers"; import { Player } from "./Player"; +import { Terminal } from "./Terminal"; // Netscript Ports are instantiated here export const NetscriptPorts: IPort[] = []; @@ -609,6 +610,7 @@ export function updateOnlineScriptTimes(numCycles = 1): void { export function loadAllRunningScripts(): void { const skipScriptLoad = window.location.href.toLowerCase().indexOf("?noscripts") !== -1; if (skipScriptLoad) { + Terminal.warn('Skipped loading player scripts during startup'); console.info("Skipping the load of any scripts during startup"); } for (const server of GetAllServers()) {