diff options
-rw-r--r-- | src/app/main.js | 97 | ||||
-rw-r--r-- | src/index.js | 8 | ||||
-rw-r--r-- | src/modules/settings.js | 8 |
3 files changed, 86 insertions, 27 deletions
diff --git a/src/app/main.js b/src/app/main.js index 51d9e73..4e30cd0 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -2,6 +2,8 @@ const fs = require("fs"); const path = require("path"); const { app, ipcRenderer, shell } = require("electron"); +const json = require("../modules/json"); + const lang = require("../lang"); var modsobj = { all: [], @@ -33,39 +35,85 @@ ipcRenderer.send("setlang", settings.lang); // Loads the settings if (fs.existsSync("viper.json")) { - let conf = fs.readFileSync("viper.json", "utf8"); - let json = {}; + let iteration = 0; + + // loads the config file, it's loaded in an interval like this in + // case the config file is currently being written to, if we were to + // read from the file during that, then it'd be empty or similar. + // + // and because of that, we check if the file is empty when loading + // it, if so we wait 100ms, then check again, and we keep doing that + // hoping it'll become normal again. unless we've checked it 50 + // times, then we simply give up, and force the user to re-select + // the gamepath, as this'll make the config file readable again. + // + // ideally it'll never have to check those 50 times, it's only in + // case something terrible has gone awry, like if a write got + // interrupted, or a user messed with the file. + // + // were it to truly be broken, then it'd take up to 5 seconds to + // then reset, this is basically nothing, especially considering + // this should only happen in very rare cases... hopefully! + let config_interval = setInterval(() => { + iteration++; + + config = json("viper.json") || {}; + + // checks whether `settings.gamepath` is set, and if so, + // it'll attempt to load ns_startup_args.txt + let parse_settings = () => { + // if gamepath is not set, attempt to set it + if (settings.gamepath.length === 0) { + setpath(false); + } else { + // if the gamepath is set, we'll simply tell the main + // process about it, and it'll then show the main + // renderer BrowserWindow + setpath(true); + } - // Validates viper.json - try { - json = JSON.parse(conf); - }catch (e) { - let reset = confirm(lang("general.invalidconfig", navigator.language) + e); - if (! reset) { - ipcRenderer.send("exit"); - } else { - fs.rmSync("viper.json"); - ipcRenderer.send("relaunch"); + // filepath to Northstar's startup args file + let args = path.join(settings.gamepath, "ns_startup_args.txt"); + + // check file exists + if (fs.existsSync(args)) { + // load file into `settings` + settings.nsargs = fs.readFileSync(args, "utf8"); + } } - - } - settings = {...settings, ...json}; + // make sure config isn't empty + if (Object.keys(config).length !== 0) { + // add `config` to `settings` + settings = { + ...settings, + ...config + }; parse_settings(); - if (settings.gamepath.length === 0) { - setpath(false); - } else { - setpath(true); - } + clearInterval(config_interval); + return; + } - let args = path.join(settings.gamepath, "ns_startup_args.txt"); - if (fs.existsSync(args)) { - settings.nsargs = fs.readFileSync(args, "utf8"); - } + // we've attempted to load the config 50 times now, give up + if (iteration >= 50) { + // request a new gamepath be set + setpath(false); + clearInterval(config_interval); + } + }, 100) } else { setpath(); } +ipcRenderer.on("changed-settings", (e, new_settings) => { + // attempt to set `settings` to `new_settings` + try { + settings = { + ...settings, + ...new_settings + } + }catch(e) {} +}) // Show a toast message if no Internet connection has been detected. if (! navigator.onLine) { @@ -235,6 +283,7 @@ function isModInstalled(modname) { ipcRenderer.on("newpath", (event, newpath) => { settings.gamepath = newpath; ipcRenderer.send("gui-getmods"); + ipcRenderer.send("save-settings", settings); }) // Continuation of log() diff --git a/src/index.js b/src/index.js index ed0fb3a..301c622 100644 --- a/src/index.js +++ b/src/index.js @@ -130,7 +130,13 @@ function start() { } }); - ipcMain.on("save-settings", (event, obj) => {settings.save(obj)}); + ipcMain.on("save-settings", (event, obj) => { + settings.save(obj, false) + }); + + ipcMain.on("saved-settings", (event, obj) => { + send("changed-settings", obj); + }); // allows renderer to check for updates ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)}); diff --git a/src/modules/settings.js b/src/modules/settings.js index 3833e4f..e1f68c6 100644 --- a/src/modules/settings.js +++ b/src/modules/settings.js @@ -1,6 +1,6 @@ const fs = require("fs"); const path = require("path"); -const app = require("electron").app; +const { app, ipcMain } = require("electron"); const json = require("./json"); const lang = require("../lang"); @@ -58,7 +58,7 @@ if (fs.existsSync("viper.json")) { // // you can also pass a settings object to the function and it'll try and // merge it together with the already existing settings -settings.save = (obj = {}) => { +settings.save = (obj = {}, notify_renderer = true) => { // refuse to save if settings aren't valid if (invalid_settings) { return false; @@ -88,6 +88,10 @@ settings.save = (obj = {}) => { settings.gamepath, "ns_startup_args.txt" ), settings.nsargs); } + + if (notify_renderer) { + ipcMain.emit("saved-settings", settings_content); + } } module.exports = settings; |