MISC: Prevent running multiple instances of Electron app (#2095)

This commit is contained in:
catloversg
2025-05-11 12:14:58 +07:00
committed by GitHub
parent 4044b4a223
commit 6a0853ddf8

View File

@@ -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",