bitburner-src/electron/gameWindow.js

106 lines
3.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-var-requires */
2022-01-03 16:29:56 +01:00
const { app, BrowserWindow } = require("electron");
const log = require("electron-log");
const utils = require("./utils");
const achievements = require("./achievements");
const menu = require("./menu");
const api = require("./api-server");
2022-01-09 20:02:11 +01:00
const cp = require("child_process");
const path = require("path");
const { windowTracker } = require("./windowTracker");
const debug = process.argv.includes("--debug");
async function createWindow(killall) {
2022-01-09 20:02:11 +01:00
const setStopProcessHandler = global.app_handlers.stopProcess;
2022-04-07 01:30:08 +02:00
app.setAppUserModelId("Bitburner");
2022-01-23 18:38:47 +01:00
let icon;
2022-04-07 01:30:08 +02:00
if (process.platform == "linux") {
icon = path.join(__dirname, "icon.png");
2022-01-23 18:38:47 +01:00
}
2022-04-07 01:30:08 +02:00
const tracker = windowTracker("main");
const window = new BrowserWindow({
2022-01-23 18:38:47 +01:00
icon,
show: false,
backgroundThrottling: false,
backgroundColor: "#000000",
2022-04-07 01:30:08 +02:00
title: "Bitburner",
x: tracker.state.x,
y: tracker.state.y,
width: tracker.state.width,
height: tracker.state.height,
minWidth: 600,
minHeight: 400,
webPreferences: {
nativeWindowOpen: true,
2022-04-07 01:30:08 +02:00
preload: path.join(__dirname, "preload.js"),
},
});
setTimeout(() => tracker.track(window), 1000);
if (tracker.state.isMaximized) window.maximize();
window.removeMenu();
noScripts = killall ? { query: { noScripts: killall } } : {};
window.loadFile("index.html", noScripts);
window.show();
if (debug) window.webContents.openDevTools();
window.webContents.on("new-window", async function (e, url) {
// Let's make sure sure we have a proper url
2022-04-07 01:30:08 +02:00
let parsedUrl;
try {
parsedUrl = new URL(url);
} catch (_) {
// This is an invalid url, let's just do nothing
2022-04-07 01:30:08 +02:00
log.warn(`Invalid url found: ${url}`);
e.preventDefault();
return;
}
2023-01-11 01:12:23 +01:00
// Just use the default handling for file requests, they should be intercepted in main.js file protocol intercept.
if (url.startswith("file://")) return;
2022-01-09 20:02:11 +01:00
if (process.platform === "win32") {
// If we have parameters in the URL, explorer.exe won't register the URL and will open the file explorer instead.
let urlToOpen = parsedUrl.toString();
if (parsedUrl.search) {
log.log(`Cannot open a path with parameters: ${parsedUrl.search}`);
2022-04-07 01:30:08 +02:00
urlToOpen = urlToOpen.replace(parsedUrl.search, "");
// It would be possible to launch an URL with parameter using this, but it would mess up the process again...
// const escapedUri = parsedUrl.href.replace('&', '^&');
// cp.spawn("cmd.exe", ["/c", "start", escapedUri], { detached: true, stdio: "ignore" });
2022-01-09 20:02:11 +01:00
}
cp.spawn("explorer", [urlToOpen], { detached: true, stdio: "ignore" });
} else {
2022-01-09 20:02:11 +01:00
// and open every other protocols on the browser
utils.openExternal(url);
}
e.preventDefault();
});
window.webContents.backgroundThrottling = false;
achievements.enableAchievementsInterval(window);
utils.attachUnresponsiveAppHandler(window);
2022-01-05 03:08:06 +01:00
utils.setZoomFactor(window);
try {
await api.initialize(window);
} catch (error) {
log.error(error);
2022-01-09 20:02:11 +01:00
utils.showErrorBox("Error starting http server", error);
}
menu.refreshMenu(window);
2023-01-11 01:12:23 +01:00
setStopProcessHandler(app, window);
return window;
}
module.exports = {
createWindow,
2022-01-09 20:02:11 +01:00
};