bitburner-src/electron/main.js

149 lines
4.1 KiB
JavaScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-var-requires */
const { app, BrowserWindow, Menu, shell } = require("electron");
2021-11-27 21:07:25 +01:00
const greenworks = require("./greenworks");
2021-09-21 19:29:16 +02:00
2021-11-27 21:07:25 +01:00
if (greenworks.init()) {
console.log("Steam API has been initialized.");
} else {
console.log("Steam API has failed to initialize.");
}
2021-11-29 05:51:01 +01:00
const debug = false;
2021-11-05 05:14:11 +01:00
2021-12-15 04:34:09 +01:00
function createWindow(killall) {
const win = new BrowserWindow({
2021-09-15 18:22:36 +02:00
show: false,
2021-12-16 21:46:26 +01:00
backgroundThrottling: false,
2021-12-18 16:43:19 +01:00
backgroundColor: "#000000",
2021-09-15 18:22:36 +02:00
});
win.removeMenu();
win.maximize();
2021-12-15 04:34:09 +01:00
noScripts = killall ? { query: { noScripts: killall } } : {};
win.loadFile("index.html", noScripts);
2021-09-15 18:22:36 +02:00
win.show();
2021-11-05 05:19:41 +01:00
if (debug) win.webContents.openDevTools();
win.webContents.on("new-window", function (e, url) {
// make sure local urls stay in electron perimeter
2021-09-25 07:26:03 +02:00
if (url.substr(0, "file://".length) === "file://") {
return;
}
// and open every other protocols on the browser
e.preventDefault();
shell.openExternal(url);
});
2021-12-14 04:26:22 +01:00
win.webContents.backgroundThrottling = false;
// This is backward but the game fills in an array called `document.achievements` and we retrieve it from
// here. Hey if it works it works.
const achievements = greenworks.getAchievementNames();
const intervalID = setInterval(async () => {
const achs = await win.webContents.executeJavaScript("document.achievements");
console.log(achs);
for (const ach of achs) {
if (!achievements.includes(ach)) continue;
greenworks.activateAchievement(ach, () => undefined);
}
}, 1000);
win.achievementsIntervalID = intervalID;
2021-11-27 21:07:25 +01:00
2021-12-03 00:42:30 +01:00
// Create the Application's main menu
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" },
],
},
2021-12-12 18:48:42 +01:00
{
label: "reloads",
submenu: [
{
label: "reload",
accelerator: "f5",
click: () => {
win.loadFile("index.html");
},
},
{
label: "reload & kill all scripts",
click: () => {
setStopProcessHandler(app, win, false);
2021-12-14 04:26:22 +01:00
if (intervalID) clearInterval(intervalID);
2021-12-12 18:48:42 +01:00
win.webContents.forcefullyCrashRenderer();
2021-12-14 04:26:22 +01:00
win.close();
createWindow(true);
2021-12-12 18:48:42 +01:00
},
},
],
},
2021-12-12 18:58:57 +01:00
{
label: "fullscreen",
submenu: [
{
label: "toggle",
accelerator: "f9",
click: (() => {
let full = false;
return () => {
full = !full;
win.setFullScreen(full);
};
})(),
},
],
},
2021-12-12 19:25:28 +01:00
{
label: "debug",
submenu: [
{
label: "activate",
click: () => win.webContents.openDevTools(),
},
],
},
2021-12-03 00:42:30 +01:00
]),
);
return win;
2021-09-15 18:22:36 +02:00
}
function setStopProcessHandler(app, window, enabled) {
const clearWindowHandler = () => {
if (window.achievementsIntervalID) {
clearInterval(window.achievementsIntervalID);
}
window = null;
};
const stopProcessHandler = () => {
if (process.platform !== "darwin") {
app.quit();
// eslint-disable-next-line no-process-exit
process.exit(0);
}
};
2021-12-17 02:07:34 +01:00
if (enabled) {
window.on("closed", clearWindowHandler);
app.on("window-all-closed", stopProcessHandler);
} else {
window.removeListener("closed", clearWindowHandler);
app.removeListener("window-all-closed", stopProcessHandler);
}
}
2021-09-15 18:22:36 +02:00
app.whenReady().then(() => {
2021-12-18 16:43:19 +01:00
const win = createWindow(process.argv.includes("--no-scripts"));
setStopProcessHandler(app, win, true);
2021-12-17 07:32:23 +01:00
});