diff --git a/electron/main.js b/electron/main.js index 34bf1cdc9..680e0b359 100644 --- a/electron/main.js +++ b/electron/main.js @@ -176,7 +176,33 @@ global.app_handlers = { stopProcess: setStopProcessHandler, }; +let window; + app.on("ready", async () => { + const gotTheLock = app.requestSingleInstanceLock(); + if (!gotTheLock) { + await dialog.showMessageBox(undefined, { + title: "Bitburner", + message: "Failed to launch", + detail: "Bitburner is already running. Please do not launch the game multiple times.", + type: "error", + buttons: ["OK"], + }); + app.quit(); + return; + } + app.on("second-instance", (event, commandLine, workingDirectory, additionalData) => { + log.error("A second instance is launched", event, commandLine, workingDirectory, additionalData); + // The player tried to run a second instance. We should focus the current window. + if (!window) { + return; + } + if (window.isMinimized()) { + window.restore(); + } + window.focus(); + }); + // Intercept file protocol requests and only let valid requests through protocol.handle("file", ({ url, method }) => { let filePath; @@ -207,13 +233,13 @@ app.on("ready", async () => { log.info("Application is ready!"); if (process.argv.includes("--export-save")) { - const window = new BrowserWindow({ show: false }); + window = new BrowserWindow({ show: false }); await window.loadFile("export.html"); window.show(); setStopProcessHandler(window); await utils.exportSave(window); } else { - const window = await startWindow(process.argv.includes("--no-scripts")); + window = await startWindow(process.argv.includes("--no-scripts")); if (global.steamworksError) { await dialog.showMessageBox(window, { title: "Bitburner",