aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author0neGal <mail@0negal.com>2022-05-03 14:25:38 +0200
committerGitHub <noreply@github.com>2022-05-03 14:25:38 +0200
commit39b1f59608deac73caf8efff59280ef170020a04 (patch)
tree36172af38fc22f7eb254f9fe548b2a17c218bc2d /src
parenta870336695da962623c87e30f4a0cbd29684b8c2 (diff)
parentec85ca36735cbebe81e0f9e2bbd4006572e25d46 (diff)
downloadViper-39b1f59608deac73caf8efff59280ef170020a04.tar.gz
Viper-39b1f59608deac73caf8efff59280ef170020a04.zip
Merge pull request #45 from 0neGal/enabledmods
Implement enabledmods.json
Diffstat (limited to 'src')
-rw-r--r--src/utils.js145
1 files changed, 88 insertions, 57 deletions
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();