diff options
-rw-r--r-- | package-lock.json | 14 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/utils.js | 145 |
3 files changed, 103 insertions, 57 deletions
diff --git a/package-lock.json b/package-lock.json index b84a0cd..22788c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "follow-redirects": "^1.14.8", "fs-extra": "^10.0.0", "fuse.js": "^6.5.3", + "jsonrepair": "^2.2.1", "marked": "^4.0.10", "marked-man": "^0.7.0", "recursive-copy": "^2.0.13", @@ -1834,6 +1835,14 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jsonrepair": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/jsonrepair/-/jsonrepair-2.2.1.tgz", + "integrity": "sha512-o9Je8TceILo872uQC9fIBJm957j1Io7z8Ca1iWIqY6S5S65HGE9XN7XEEw7+tUviB9Vq4sygV89MVTxl+rhZyg==", + "bin": { + "jsonrepair": "bin/cli.js" + } + }, "node_modules/junk": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/junk/-/junk-1.0.3.tgz", @@ -4521,6 +4530,11 @@ "universalify": "^2.0.0" } }, + "jsonrepair": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/jsonrepair/-/jsonrepair-2.2.1.tgz", + "integrity": "sha512-o9Je8TceILo872uQC9fIBJm957j1Io7z8Ca1iWIqY6S5S65HGE9XN7XEEw7+tUviB9Vq4sygV89MVTxl+rhZyg==" + }, "junk": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/junk/-/junk-1.0.3.tgz", diff --git a/package.json b/package.json index 3c3999a..6ebf10c 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "follow-redirects": "^1.14.8", "fs-extra": "^10.0.0", "fuse.js": "^6.5.3", + "jsonrepair": "^2.2.1", "marked": "^4.0.10", "marked-man": "^0.7.0", "recursive-copy": "^2.0.13", diff --git a/src/utils.js b/src/utils.js index 77e8fc7..4afbb2b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -12,6 +12,7 @@ const requests = require("./extras/requests"); const findgame = require("./extras/findgame"); const unzip = require("unzipper"); +const repair = require("jsonrepair"); const run = require("child_process").spawn; const exec = require("child_process").exec; const { https } = require("follow-redirects"); @@ -423,7 +424,7 @@ const mods = { return false; } - let mods = []; + let enabled = []; let disabled = []; if (! fs.existsSync(modpath)) { @@ -438,50 +439,41 @@ const mods = { files = fs.readdirSync(modpath) files.forEach((file) => { if (fs.statSync(path.join(modpath, file)).isDirectory()) { - if (fs.existsSync(path.join(modpath, file, "mod.json"))) { - try { - mods.push({...require(path.join(modpath, file, "mod.json")), FolderName: file, Disabled: false}) - }catch(err) { - if (cli.hasArgs()) {console.log("error: " + lang("cli.mods.improperjson"), file)} - mods.push({Name: file, FolderName: file, Version: "unknown", Disabled: false}) - } + let modjson = path.join(modpath, file, "mod.json"); + if (fs.existsSync(modjson)) { + let mod = JSON.parse(repair(fs.readFileSync(modjson, "utf8"))); - let manifest = path.join(modpath, file, "manifest.json"); - if (fs.existsSync(manifest)) { - try {mods[mods.length - 1].ManifestName = require(manifest).name}catch(err){} - } - } - } - }) + let obj = { + Version: "unknown", + Name: "unknown", + FolderName: file, + ...mod} - let disabledPath = path.join(modpath, "disabled") - if (! fs.existsSync(disabledPath)) { - fs.mkdirSync(disabledPath) - } + obj.Disabled = ! mods.modfile().get(obj.Name); - files = fs.readdirSync(disabledPath) - files.forEach((file) => { - if (fs.statSync(path.join(disabledPath, file)).isDirectory()) { - if (fs.existsSync(path.join(disabledPath, file, "mod.json"))) { - try { - disabled.push({...require(path.join(disabledPath, file, "mod.json")), FolderName: file, Disabled: true}) - }catch(err) { - if (cli.hasArgs()) {console.log("error: " + lang("cli.mods.improperjson"), file)} - disabled.push({Name: file, FolderName: file, Version: "unknown", Disabled: true}) + 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 = path.join(modpath, file, "manifest.json"); - if (fs.existsSync(manifest)) { - try {mods[mods.length - 1].ManifestName = require(manifest).name}catch(err){} + if (obj.Disabled) { + disabled.push(obj); + } else { + enabled.push(obj); + } } } }) return { - enabled: mods, + enabled: enabled, disabled: disabled, - all: [...mods, ...disabled] + all: [...enabled, ...disabled] }; }, @@ -512,6 +504,67 @@ const mods = { return false; }, + // Manages the enabledmods.json file + // + // It can both return info about the file, but also toggle mods in + // it, generate the file itself, and so on. + modfile: () => { + let modpath = path.join(settings.gamepath, "R2Northstar/mods"); + let file = path.join(modpath, "..", "enabledmods.json"); + + if (! fs.existsSync(modpath)) { + fs.mkdirSync(path.join(modpath), {recursive: true}) + } + + if (! fs.existsSync(file)) { + fs.writeFileSync(file, "{}") + } + + return { + gen: () => { + let names = {}; + let list = mods.list().all; + for (let i = 0; i < list.length; i++) { + names[list[i].Name] = true + } + + fs.writeFileSync(file, JSON.stringify(names)) + }, + disable: (mod) => { + let data = require(file); + data[mod] = false; + fs.writeFileSync(file, JSON.stringify(data)); + }, + enable: (mod) => { + let data = require(file); + data[mod] = true; + fs.writeFileSync(file, JSON.stringify(data)); + }, + toggle: (mod) => { + let data = require(file); + if (data[mod] != undefined) { + data[mod] = ! data[mod]; + } else { + data[mod] = false; + } + + fs.writeFileSync(file, JSON.stringify(data)); + }, + get: (mod) => { + let data = require(file); + let names = Object.keys(data); + + if (data[mod]) { + return true; + } else if (data[mod] === false) { + return false; + } else { + return true; + } + } + }; + }, + // Installs mods from a file path // // Either a zip or folder is supported, we'll also try to search @@ -748,8 +801,6 @@ const mods = { // you checked for if a mod is already disable and if not run the // function. However we currently have no need for that. toggle: (mod, fork) => { - let modpath = path.join(settings.gamepath, "R2Northstar/mods"); - if (getNSVersion() == "unknown") { winLog(lang("general.notinstalled")) console.log("error: " + lang("general.notinstalled")) @@ -768,27 +819,7 @@ const mods = { return } - let disabled = path.join(modpath, "disabled"); - if (! fs.existsSync(disabled)) { - fs.mkdirSync(disabled) - } - - let modName = mods.get(mod).FolderName; - if (! modName) { - console.log("error: " + lang("cli.mods.cantfind")) - cli.exit(1); - return; - } - - let modPath = path.join(modpath, modName); - let dest = path.join(disabled, modName); - - if (mods.get(mod).Disabled) { - modPath = path.join(disabled, modName); - dest = path.join(modpath, modName); - } - - fs.moveSync(modPath, dest) + mods.modfile().toggle(mod); if (! fork) { console.log(lang("cli.mods.toggled")); cli.exit(); |