bitburner-src/electron/utils.js

121 lines
3.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-var-requires */
2023-01-11 01:12:23 +01:00
const { dialog, shell } = require("electron");
const log = require("electron-log");
2022-01-05 03:08:06 +01:00
const Config = require("electron-config");
const config = new Config();
function reloadAndKill(window, killScripts) {
2022-04-07 01:30:08 +02:00
log.info("Reloading & Killing all scripts...");
window.webContents.forcefullyCrashRenderer();
2023-01-11 01:12:23 +01:00
window.loadFile("index.html", killScripts ? { query: { noScripts: true } } : {});
}
function promptForReload(window) {
detachUnresponsiveAppHandler(window);
2022-04-07 01:30:08 +02:00
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(window, checkboxChecked);
} else {
attachUnresponsiveAppHandler(window);
}
});
}
function attachUnresponsiveAppHandler(window) {
window.unresponsiveHandler = () => promptForReload(window);
2022-04-07 01:30:08 +02:00
window.on("unresponsive", window.unresponsiveHandler);
}
function detachUnresponsiveAppHandler(window) {
2022-04-07 01:30:08 +02:00
window.off("unresponsive", window.unresponsiveHandler);
}
function showErrorBox(title, error) {
2022-04-07 01:30:08 +02:00
dialog.showErrorBox(title, `${error.name}\n\n${error.message}`);
}
function exportSaveFromIndexedDb() {
return new Promise((resolve) => {
const dbRequest = indexedDB.open("bitburnerSave");
dbRequest.onsuccess = () => {
const db = dbRequest.result;
2022-04-07 01:30:08 +02:00
const transaction = db.transaction(["savestring"], "readonly");
const store = transaction.objectStore("savestring");
const request = store.get("save");
request.onsuccess = () => {
2022-04-07 01:30:08 +02:00
const file = new Blob([request.result], { type: "text/plain" });
const a = document.createElement("a");
const url = URL.createObjectURL(file);
a.href = url;
2022-04-07 01:30:08 +02:00
a.download = "save.json";
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
resolve();
}, 0);
2022-04-07 01:30:08 +02:00
};
};
});
}
async function exportSave(window) {
2022-04-07 01:30:08 +02:00
await window.webContents.executeJavaScript(`${exportSaveFromIndexedDb.toString()}; exportSaveFromIndexedDb();`, true);
}
async function writeTerminal(window, message, type = null) {
2022-04-07 01:30:08 +02:00
await window.webContents.executeJavaScript(`window.appNotifier.terminal("${message}", "${type}");`, true);
}
async function writeToast(window, message, type = "info", duration = 2000) {
2022-04-07 01:30:08 +02:00
await window.webContents.executeJavaScript(`window.appNotifier.toast("${message}", "${type}", ${duration});`, true);
}
2022-01-03 16:29:56 +01:00
function openExternal(url) {
shell.openExternal(url);
global.app_playerOpenedExternalLink = true;
}
2022-01-05 03:08:06 +01:00
function getZoomFactor() {
2022-04-07 01:30:08 +02:00
const configZoom = config.get("zoom", 1);
2022-01-05 03:08:06 +01:00
return configZoom;
}
function setZoomFactor(window, zoom = null) {
if (zoom === null) {
zoom = 1;
} else {
2022-04-07 01:30:08 +02:00
config.set("zoom", zoom);
2022-01-05 03:08:06 +01:00
}
window.webContents.setZoomFactor(zoom);
}
module.exports = {
2022-04-07 01:30:08 +02:00
reloadAndKill,
showErrorBox,
exportSave,
attachUnresponsiveAppHandler,
detachUnresponsiveAppHandler,
openExternal,
writeTerminal,
writeToast,
getZoomFactor,
setZoomFactor,
};