From e5fa26a944f8a4d025ce9fcc2aa1ec8fbf26874b Mon Sep 17 00:00:00 2001 From: 0neGal Date: Tue, 3 May 2022 00:20:05 +0200 Subject: simplified get() and made list() used get() Instead of repeating code and everything, get() now only returns a boolean, based on the arg inputted into get(mod), it returns true if the mod is enabled and false if disabled. If the mod isn't found inside the enabledmods.json it is assumed that it's enabled, as Northstar doesn't update this unless you actually disable the mod first, and the mod won't be found in there. Then I used get() to simplify list() a whole lot, and adding that functionality of using the manifest for a version number if one couldn't be found in the mod.json. --- src/utils.js | 132 +++++++++++++++-------------------------------------------- 1 file changed, 33 insertions(+), 99 deletions(-) diff --git a/src/utils.js b/src/utils.js index 971ecd9..eb833d3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -424,7 +424,7 @@ const mods = { return false; } - let mods = []; + let enabled = []; let disabled = []; if (! fs.existsSync(modpath)) { @@ -439,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,7 +503,7 @@ const mods = { return false; }, - + // Manages the enabledmods.json file // // It can both return info about the file, but also toggle mods in @@ -554,73 +545,16 @@ const mods = { data[mod] = ! data[mod]; fs.writeFileSync(file, JSON.stringify(data)); }, - get: () => { - let enabled = []; - let disabled = []; + get: (mod) => { let data = require(file); let names = Object.keys(data); - for (let i = 0; i < names.length; i++) { - let manifest; - let name = names[i]; - let folder = "unknown"; - let version = "unknown"; - - if (fs.existsSync(path.join(modpath, name))) { - folder = name; - } else { - let files = fs.readdirSync(modpath); - for (let ii = 0; ii < files.length; ii++) { - let mod = path.join(modpath, files[ii], "mod.json"); - if (fs.existsSync(mod)) { - mod = JSON.parse(repair(fs.readFileSync(mod, "utf8"))); - - version = mod.Version; - if (mod.Name == name) { - folder = files[ii]; - break - } - } - - } - } - - let manifestfile = path.join(modpath, folder, "manifest.json"); - if (fs.existsSync(manifestfile)) { - manifest = JSON.parse(repair(fs.readFileSync(manifestfile, "utf8"))); - - if (version == "unknown" || version == "") { - if (manifest != false) { - console.log(name) - version = manifest.version_number; - console.log(version + name) - } - } - } - - if (folder == "unknown") {continue} - console.log(manifest) - - let obj = { - Name: name, - Version: version, - FolderName: folder, - Disabled: data[name] - } - - if (manifest) { - obj = {...obj, ManifestName: manifest.name} - } - - if (data[name]) { - enabled.push(obj); - } else {disabled.push(obj)} - } - - return { - enabled: enabled, - disabled: disabled, - all: [...enabled, ...disabled] + if (data[mod]) { + return true; + } else if (data[mod] === false) { + return false; + } else { + return true; } } }; -- cgit v1.2.3