diff options
author | 0neGal <mail@0negal.com> | 2023-01-12 19:33:49 +0100 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2023-01-12 19:34:39 +0100 |
commit | 2889b310c18acc5fbc926d63659a624e4675fe57 (patch) | |
tree | 265bef0435d71d7c0bbc79f05962d7f6e7bf5e95 | |
parent | a67040498d238c3d7b1b947f7be02034f3b71d52 (diff) | |
download | Viper-2889b310c18acc5fbc926d63659a624e4675fe57.tar.gz Viper-2889b310c18acc5fbc926d63659a624e4675fe57.zip |
added: src/modules/json.js
This module makes it easier to read JSON files, simply returning false
on errors, and attempting to repair the JSON automatically.
-rw-r--r-- | src/cli.js | 3 | ||||
-rw-r--r-- | src/lang.js | 8 | ||||
-rw-r--r-- | src/modules/json.js | 35 | ||||
-rw-r--r-- | src/modules/settings.js | 10 | ||||
-rw-r--r-- | src/utils.js | 36 |
5 files changed, 62 insertions, 30 deletions
@@ -6,6 +6,7 @@ const events = new Emitter(); const cli = app.commandLine; const lang = require("./lang"); +const json = require("./modules/json"); function hasArgs() { // Makes sure the GUI isn't launched. @@ -38,7 +39,7 @@ function exit(code) { // gamepath to be able to work. function gamepathExists() { if (fs.existsSync("viper.json")) { - gamepath = JSON.parse(fs.readFileSync("viper.json", "utf8")).gamepath; + gamepath = json("viper.json").gamepath; if (! fs.existsSync(gamepath)) { console.error(`error: ${lang("cli.gamepath.lost")}`); diff --git a/src/lang.js b/src/lang.js index f2fab3a..85488d8 100644 --- a/src/lang.js +++ b/src/lang.js @@ -1,6 +1,8 @@ const fs = require("fs"); -const enLang = JSON.parse(fs.readFileSync(__dirname + `/lang/en.json`, "utf8")); +const json = require("./modules/json"); + +const enLang = json(__dirname + "/lang/en.json"); let lang = ""; var langObj = {}; @@ -14,7 +16,7 @@ function _loadTranslation(forcedlang) { } try { - opts = JSON.parse(fs.readFileSync("viper.json", "utf8")); + opts = json("viper.json"); }catch (e) {} lang = opts.lang; @@ -39,7 +41,7 @@ function _loadTranslation(forcedlang) { lang = "en"; } - langObj = JSON.parse(fs.readFileSync(__dirname + `/lang/${lang}.json`, "utf8")); + langObj = json(__dirname + `/lang/${lang}.json`); } diff --git a/src/modules/json.js b/src/modules/json.js new file mode 100644 index 0000000..5c099b1 --- /dev/null +++ b/src/modules/json.js @@ -0,0 +1,35 @@ +const fs = require("fs"); +const repair = require("jsonrepair"); + +function read(file) { + let json = false; + + // make sure the file actually exists + if (! fs.existsSync(file)) { + return false; + } + + // make sure we're actually reading a file + if (! fs.statSync(file).isFile()) { + return false; + } + + // read the file + let file_content = fs.readFileSync(file, "utf8"); + + // attempt to parse it + try { + json = JSON.parse(file_content); + }catch(err) { + // attempt to repair then parse + try { + json = JSON.parse(repair(file_content)); + }catch(repair_err) { + return false; + } + } + + return json; +} + +module.exports = read; diff --git a/src/modules/settings.js b/src/modules/settings.js index a24f1e0..d0a2db1 100644 --- a/src/modules/settings.js +++ b/src/modules/settings.js @@ -2,6 +2,7 @@ const fs = require("fs"); const path = require("path"); const app = require("electron").app; +const json = require("./json"); const lang = require("../lang"); var invalid_settings = false; @@ -28,17 +29,14 @@ var settings = { // Creates the settings file with the base settings if it doesn't exist. if (fs.existsSync("viper.json")) { - let conf = fs.readFileSync("viper.json", "utf8"); - let json = "{}"; + let conf = json("viper.json"); // Validates viper.json - try { - json = JSON.parse(conf); - }catch (e) { + if (! conf) { invalid_settings = true; } - settings = {...settings, ...json}; + settings = {...settings, ...conf}; settings.zip = path.join(settings.gamepath + "/northstar.zip"); let args = path.join(settings.gamepath, "ns_startup_args.txt"); diff --git a/src/utils.js b/src/utils.js index e3e47e5..6cfdb19 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,12 +8,12 @@ const events = new Emitter(); const cli = require("./cli"); const lang = require("./lang"); +const json = require("./modules/json"); const settings = require("./modules/settings"); const requests = require("./modules/requests"); const findgame = require("./modules/findgame"); const unzip = require("unzipper"); -const repair = require("jsonrepair"); const exec = require("child_process").exec; const { https } = require("follow-redirects"); @@ -226,7 +226,7 @@ function getNSVersion() { } try { - add("v" + JSON.parse(fs.readFileSync(versionFile, "utf8")).Version); + add("v" + json(versionFile).Version); }catch(err) { add("unknown"); } @@ -479,12 +479,8 @@ const mods = { if (fs.statSync(path.join(modpath, file)).isDirectory()) { let modjson = path.join(modpath, file, "mod.json"); if (fs.existsSync(modjson)) { - let mod = {}; - try { - mod = JSON.parse(repair(fs.readFileSync(modjson, "utf8"))); - }catch(err) { - return; - } + let mod = json(modjson); + if (! mod) {return} let obj = { Version: "unknown", @@ -496,11 +492,12 @@ const mods = { let manifestfile = path.join(modpath, file, "manifest.json"); if (fs.existsSync(manifestfile)) { - let manifest = JSON.parse(repair(fs.readFileSync(manifestfile, "utf8"))); - - obj.ManifestName = manifest.name; - if (obj.Version == "unknown") { - obj.Version = manifest.version_number; + let manifest = json(manifestfile); + if (manifest != false) { + obj.ManifestName = manifest.name; + if (obj.Version == "unknown") { + obj.Version = manifest.version_number; + } } } @@ -574,17 +571,17 @@ const mods = { fs.writeFileSync(file, JSON.stringify(names)); }, disable: (mod) => { - let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); + let data = json(file); data[mod] = false; fs.writeFileSync(file, JSON.stringify(data)); }, enable: (mod) => { - let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); + let data = json(file); data[mod] = true; fs.writeFileSync(file, JSON.stringify(data)); }, toggle: (mod) => { - let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); + let data = json(file); if (data[mod] != undefined) { data[mod] = ! data[mod]; } else { @@ -594,7 +591,7 @@ const mods = { fs.writeFileSync(file, JSON.stringify(data)); }, get: (mod) => { - let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); + let data = json(file); let names = Object.keys(data); if (data[mod]) { @@ -678,9 +675,8 @@ const mods = { if (fs.existsSync(path.join(mod, "mod.json")) && fs.statSync(path.join(mod, "mod.json")).isFile()) { - try { - JSON.parse(fs.readFileSync(path.join(mod, "mod.json"))); - }catch(err) { + + if (! json(path.join(mod, "mod.json"))) { ipcMain.emit("failed-mod"); return notamod(); } |