2021-12-20 12:33:46 +01:00
|
|
|
/* eslint-disable no-process-exit */
|
2021-12-19 19:04:34 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2022-01-02 17:44:47 +01:00
|
|
|
const { app, dialog, BrowserWindow } = require("electron");
|
2021-12-29 14:46:56 +01:00
|
|
|
const log = require("electron-log");
|
2021-11-27 21:07:25 +01:00
|
|
|
const greenworks = require("./greenworks");
|
2021-12-28 21:21:11 +01:00
|
|
|
const api = require("./api-server");
|
2021-12-29 14:46:56 +01:00
|
|
|
const gameWindow = require("./gameWindow");
|
|
|
|
const achievements = require("./achievements");
|
2021-12-30 17:51:15 +01:00
|
|
|
const utils = require("./utils");
|
2021-09-21 19:29:16 +02:00
|
|
|
|
2021-12-20 12:33:46 +01:00
|
|
|
log.catchErrors();
|
|
|
|
log.info(`Started app: ${JSON.stringify(process.argv)}`);
|
|
|
|
|
|
|
|
process.on('uncaughtException', function () {
|
|
|
|
// The exception will already have been logged by electron-log
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2022-01-14 12:06:59 +01:00
|
|
|
// We want to fail gracefully if we cannot connect to Steam
|
|
|
|
try {
|
|
|
|
if (greenworks.init()) {
|
|
|
|
log.info("Steam API has been initialized.");
|
|
|
|
} else {
|
|
|
|
const error = "Steam API has failed to initialize.";
|
|
|
|
log.warn(error);
|
|
|
|
global.greenworksError = error;
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
log.warn(ex.message);
|
|
|
|
global.greenworksError = ex.message;
|
2021-11-27 21:07:25 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 02:21:25 +01:00
|
|
|
function setStopProcessHandler(app, window, enabled) {
|
2021-12-23 13:27:25 +01:00
|
|
|
const closingWindowHandler = async (e) => {
|
|
|
|
// We need to prevent the default closing event to add custom logic
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// First we clear the achievement timer
|
2021-12-29 14:46:56 +01:00
|
|
|
achievements.disableAchievementsInterval(window);
|
2021-12-23 13:27:25 +01:00
|
|
|
|
2021-12-29 14:46:56 +01:00
|
|
|
// Shutdown the http server
|
2021-12-28 21:21:11 +01:00
|
|
|
api.disable();
|
|
|
|
|
2021-12-31 10:22:04 +01:00
|
|
|
// Because of a steam limitation, if the player has launched an external browser,
|
|
|
|
// steam will keep displaying the game as "Running" in their UI as long as the browser is up.
|
|
|
|
// So we'll alert the player to close their browser.
|
|
|
|
if (global.app_playerOpenedExternalLink) {
|
|
|
|
await dialog.showMessageBox({
|
|
|
|
title: 'Bitburner',
|
|
|
|
message: 'You may have to close your browser to properly exit the game.',
|
|
|
|
detail: 'Steam will keep tracking Bitburner as "Running" if any process started within the game is still running.' +
|
|
|
|
' This includes launching an external link, which opens up your browser.',
|
|
|
|
type: 'warning', buttons: ['OK']
|
|
|
|
});
|
|
|
|
}
|
2021-12-23 13:27:25 +01:00
|
|
|
// We'll try to execute javascript on the page to see if we're stuck
|
|
|
|
let canRunJS = false;
|
2021-12-29 14:46:56 +01:00
|
|
|
window.webContents.executeJavaScript('window.stop(); document.close()', true)
|
2021-12-23 13:27:25 +01:00
|
|
|
.then(() => canRunJS = true);
|
|
|
|
setTimeout(() => {
|
|
|
|
// Wait a few milliseconds to prevent a race condition before loading the exit screen
|
2021-12-29 14:46:56 +01:00
|
|
|
window.webContents.stop();
|
|
|
|
window.loadFile("exit.html")
|
2021-12-23 13:27:25 +01:00
|
|
|
}, 20);
|
|
|
|
|
|
|
|
// Wait 200ms, if the promise has not yet resolved, let's crash the process since we're possibly in a stuck scenario
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!canRunJS) {
|
|
|
|
// We're stuck, let's crash the process
|
|
|
|
log.warn('Forcefully crashing the renderer process');
|
2021-12-30 15:07:31 +01:00
|
|
|
window.webContents.forcefullyCrashRenderer();
|
2021-12-23 13:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.debug('Destroying the window');
|
2021-12-29 14:46:56 +01:00
|
|
|
window.destroy();
|
2021-12-23 13:27:25 +01:00
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
const clearWindowHandler = () => {
|
2021-12-17 02:21:25 +01:00
|
|
|
window = null;
|
2021-12-17 17:50:03 +01:00
|
|
|
};
|
2021-12-17 02:21:25 +01:00
|
|
|
|
|
|
|
const stopProcessHandler = () => {
|
2021-12-22 22:16:24 +01:00
|
|
|
log.info('Quitting the app...');
|
2021-12-22 22:15:56 +01:00
|
|
|
app.isQuiting = true;
|
|
|
|
app.quit();
|
|
|
|
process.exit(0);
|
2021-12-17 17:50:03 +01:00
|
|
|
};
|
2021-12-17 02:07:34 +01:00
|
|
|
|
2021-12-17 02:21:25 +01:00
|
|
|
if (enabled) {
|
2021-12-23 13:27:25 +01:00
|
|
|
log.debug('Adding closing handlers');
|
2021-12-17 17:50:03 +01:00
|
|
|
window.on("closed", clearWindowHandler);
|
2021-12-23 13:27:25 +01:00
|
|
|
window.on("close", closingWindowHandler)
|
2021-12-17 17:50:03 +01:00
|
|
|
app.on("window-all-closed", stopProcessHandler);
|
2021-12-17 02:21:25 +01:00
|
|
|
} else {
|
2021-12-23 13:27:25 +01:00
|
|
|
log.debug('Removing closing handlers');
|
2021-12-17 17:50:03 +01:00
|
|
|
window.removeListener("closed", clearWindowHandler);
|
2021-12-23 13:27:25 +01:00
|
|
|
window.removeListener("close", closingWindowHandler);
|
2021-12-17 17:50:03 +01:00
|
|
|
app.removeListener("window-all-closed", stopProcessHandler);
|
2021-12-17 02:21:25 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-17 00:26:20 +01:00
|
|
|
|
2022-01-23 18:47:04 +01:00
|
|
|
async function startWindow(noScript) {
|
|
|
|
return gameWindow.createWindow(noScript);
|
2021-12-29 14:46:56 +01:00
|
|
|
}
|
|
|
|
|
2021-12-30 15:07:31 +01:00
|
|
|
global.app_handlers = {
|
|
|
|
stopProcess: setStopProcessHandler,
|
|
|
|
createWindow: startWindow,
|
|
|
|
}
|
2021-12-29 14:46:56 +01:00
|
|
|
|
2021-12-30 17:51:15 +01:00
|
|
|
app.whenReady().then(async () => {
|
2021-12-20 12:33:46 +01:00
|
|
|
log.info('Application is ready!');
|
2021-12-30 17:51:15 +01:00
|
|
|
|
|
|
|
if (process.argv.includes("--export-save")) {
|
|
|
|
const window = new BrowserWindow({ show: false });
|
|
|
|
await window.loadFile("export.html", false);
|
|
|
|
window.show();
|
|
|
|
setStopProcessHandler(app, window, true);
|
|
|
|
await utils.exportSave(window);
|
|
|
|
} else {
|
2022-01-23 18:47:04 +01:00
|
|
|
const window = await startWindow(process.argv.includes("--no-scripts"));
|
|
|
|
if (global.greenworksError) {
|
|
|
|
await dialog.showMessageBox(window, {
|
|
|
|
title: "Bitburner",
|
|
|
|
message: "Could not connect to Steam",
|
|
|
|
detail: `${global.greenworksError}\n\nYou won't be able to receive achievements until this is resolved and you restart the game.`,
|
|
|
|
type: 'warning', buttons: ['OK']
|
|
|
|
});
|
|
|
|
}
|
2022-01-14 12:06:59 +01:00
|
|
|
}
|
2021-12-17 07:32:23 +01:00
|
|
|
});
|