From 8192ab09ef2dbdb10ba146a9b7210b3b3c4ba1c4 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Fri, 14 Jan 2022 06:06:59 -0500 Subject: [PATCH] Handle greenworks.init() errors gracefully Since we are only using it to track achievements, we can still launch the game if it fails to initialize, we just have to not run the achievements interval. Adds a dialog that tells the user to fix the issue & restart the game to enable achievements. --- electron/achievements.js | 3 +++ electron/main.js | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/electron/achievements.js b/electron/achievements.js index 906fac788..5905f3912 100644 --- a/electron/achievements.js +++ b/electron/achievements.js @@ -3,6 +3,9 @@ const greenworks = require("./greenworks"); const log = require("electron-log"); function enableAchievementsInterval(window) { + // If the Steam API could not be initialized on game start, we'll abort this. + if (global.greenworksError) return; + // 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 steamAchievements = greenworks.getAchievementNames(); diff --git a/electron/main.js b/electron/main.js index 8b959d947..9621233ba 100644 --- a/electron/main.js +++ b/electron/main.js @@ -16,10 +16,18 @@ process.on('uncaughtException', function () { process.exit(1); }); -if (greenworks.init()) { - log.info("Steam API has been initialized."); -} else { - log.warn("Steam API has failed to initialize."); +// 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; } function setStopProcessHandler(app, window, enabled) { @@ -113,4 +121,13 @@ app.whenReady().then(async () => { } else { startWindow(process.argv.includes("--no-scripts")); } + + if (global.greenworksError) { + dialog.showMessageBox({ + 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'] + }); + } });