From 5302d33b7433c68947083ef57b6bb784bd02124f Mon Sep 17 00:00:00 2001 From: 0neGal Date: Thu, 28 Apr 2022 00:23:34 +0200 Subject: added: error if config file isn't valid Essentially just validates the config file and then prompts you about it, it allows you to reset it directly or just to exit and let yourself fix it. And because the error message appears directly in the renderer we have access to navigator.language, and can therefore still localize the string. However! We can't actually care if the user has disabled auto detection of their language, since... y'know, the config file where that's stored isn't able to be read properly. And so I added an argument to lang(), which allows you to force it to use a specific language if that language is available, if not it defaults back to English. --- src/app/main.js | 19 ++++++++++++++++++- src/index.js | 2 ++ src/lang.js | 24 ++++++++++++++++++++---- src/lang/en.json | 4 +++- src/utils.js | 36 +++++++++++++++++++++++++----------- 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/app/main.js b/src/app/main.js index 70d1b0c..95b6f4c 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -27,7 +27,24 @@ ipcRenderer.send("setlang", settings.lang); // Loads the settings if (fs.existsSync("viper.json")) { - settings = {...settings, ...JSON.parse(fs.readFileSync("viper.json", "utf8"))}; + let conf = fs.readFileSync("viper.json", "utf8"); + let json = {}; + + // 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.writeFileSync("viper.json", "{}") + ipcRenderer.send("relaunch"); + } + + } + + settings = {...settings, ...json}; settings.zip = path.join(settings.gamepath + "/northstar.zip"); if (settings.gamepath.length === 0) { diff --git a/src/index.js b/src/index.js index b38b65d..6c3c79c 100644 --- a/src/index.js +++ b/src/index.js @@ -41,8 +41,10 @@ function start() { win.removeMenu(); win.loadFile(__dirname + "/app/index.html"); + ipcMain.on("exit", () => {process.exit(0)}) ipcMain.on("minimize", () => {win.minimize()}) + ipcMain.on("relaunch", () => {app.relaunch();app.exit()}) ipcMain.on("installfrompath", (event, path) => {utils.mods.install(path)}) ipcMain.on("installfromurl", (event, url) => {utils.mods.installFromURL(url)}) ipcMain.on("winLog", (event, ...args) => {win.webContents.send("log", ...args)}); diff --git a/src/lang.js b/src/lang.js index 041ef35..f2fab3a 100644 --- a/src/lang.js +++ b/src/lang.js @@ -5,13 +5,24 @@ let lang = ""; var langObj = {}; -function _loadTranslation() { +function _loadTranslation(forcedlang) { if (fs.existsSync("viper.json")) { - opts = JSON.parse(fs.readFileSync("viper.json", "utf8")); + // Validate viper.json + let opts = { + lang: "en", + autolang: true, + } + + try { + opts = JSON.parse(fs.readFileSync("viper.json", "utf8")); + }catch (e) {} + lang = opts.lang; if (! lang) {lang = "en"} + if (forcedlang) {lang = forcedlang} + if (opts.autolang == false) { lang = opts.forcedlang; if (! lang) {lang = "en"} @@ -32,9 +43,14 @@ function _loadTranslation() { } -module.exports = (string) => { - if (lang === "") +module.exports = (string, forcedlang) => { + if (lang === "") { _loadTranslation(); + } + + if (forcedlang) { + _loadTranslation(forcedlang); + } if (langObj[string]) { return langObj[string]; diff --git a/src/lang/en.json b/src/lang/en.json index af388d8..6849573 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -145,5 +145,7 @@ "general.mods.installed": "Installed mods:", "general.missingpath": "Game location could not be found automatically! Please select it manually!", "general.notinstalled": "Northstar is not installed!", - "general.launching": "Launching" + "general.launching": "Launching", + "general.reset": "Reset", + "general.invalidconfig": "Your config file is improperly formatted, if it's been manually edited, please validate that everything is typed correctly.\n\nIf you did not manually edit the config file, it is recommended to simply reset the config.\n\nTo reset your config file simply click \"Ok\" below.\n\nMore details:\n" } diff --git a/src/utils.js b/src/utils.js index 89bcf92..77e8fc7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -18,6 +18,8 @@ const { https } = require("follow-redirects"); process.chdir(app.getPath("appData")); +var invalidsettings = false; + // Base settings var settings = { gamepath: "", @@ -37,9 +39,29 @@ var settings = { ] } +// Logs into the dev tools of the renderer +function winLog(msg) { + ipcMain.emit("winLog", msg, msg); +} + +// Sends an alert to the renderer +function winAlert(msg) { + ipcMain.emit("winAlert", msg, msg); +} + // Creates the settings file with the base settings if it doesn't exist. if (fs.existsSync("viper.json")) { - settings = {...settings, ...JSON.parse(fs.readFileSync("viper.json", "utf8"))}; + let conf = fs.readFileSync("viper.json", "utf8"); + let json = "{}"; + + // Validates viper.json + try { + json = JSON.parse(conf); + }catch (e) { + invalidsettings = true; + } + + settings = {...settings, ...json}; settings.zip = path.join(settings.gamepath + "/northstar.zip"); let args = path.join(settings.gamepath, "ns_startup_args.txt"); @@ -183,6 +205,8 @@ async function setpath(win, forcedialog) { // You can also pass a settings object to the function and it'll try and // merge it together with the already existing settings function saveSettings(obj = {}) { + if (invalidsettings) {return false} + settings = {...settings, ...obj}; if (fs.existsSync(settings.gamepath)) { @@ -372,16 +396,6 @@ function launch(version) { } } -// Logs into the dev tools of the renderer -function winLog(msg) { - ipcMain.emit("winLog", msg, msg); -} - -// Sends an alert to the renderer -function winAlert(msg) { - ipcMain.emit("winAlert", msg, msg); -} - // Returns true/false depending on if the gamepath currently exists/is // mounted, used to avoid issues... function gamepathExists() { -- cgit v1.2.3