From 9c4b2aa7bfe2a96354f968c23a98213995242380 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sun, 2 Jan 2022 03:36:39 +0100 Subject: toggling, removing and installing mods works Mostly, the installing part needs a bit more look at, to support archives and different layouts for the mod. Such as searching through an archive to find the right folder because some mods don't use a proper layout. I also somewhat mitigated the whole issue of JSON files not being formatted properly by the mod developer (please just fix your formatting, I beg you.) by simply assigning the absolute basics, however we can't know the versions of the mods. I am not going to go out of my way to write code which can parse a file that wasn't made to be parsed because whoever wrote it doesn't know what a JSON file is made of. Simply not happening. I also added a few locatiolization related things, along with more info for --mods, so besides the normal counter for "Installed mods" you also have "Enabled mods" and "Disabled mods", very useful. The GUI also has a new added "Disabled" tag to mods that are disabled, however this is a temporary, it looks bad and I don't want it in release, I just needed a way to distinquish when testing. Because you can now also enable and disable mods, mods.list() gives back an Object that goes more or less something like: {all: ..., enabled: ..., disabled: ... }, take your guesses as to what everything means, you might even get it in the first try. --- src/utils.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 5 deletions(-) (limited to 'src/utils.js') diff --git a/src/utils.js b/src/utils.js index c89a033..88e5f3a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,6 @@ -const fs = require("fs"); const path = require("path"); +const fs = require("fs-extra"); +const copy = require("copy-dir"); const { app, dialog, ipcMain } = require("electron"); const Emitter = require("events"); @@ -164,23 +165,111 @@ function winLog(msg) { ipcMain.emit("winLog", msg, msg); } +let modpath = path.join(settings.gamepath, "R2Northstar/mods"); const mods = { list: () => { let mods = []; - let modpath = path.join(settings.gamepath, "R2Northstar/mods"); + let disabled = []; files = fs.readdirSync(modpath) files.forEach((file) => { if (fs.statSync(path.join(modpath, file)).isDirectory()) { if (fs.existsSync(path.join(modpath, file, "mod.json"))) { - mods.push(require(path.join(modpath, file, "mod.json"))) + try { + mods.push({...require(path.join(modpath, file, "mod.json")), FolderName: file, Disabled: false}) + }catch(err) { + console.log("error: " + lang("cli.mods.improperjson"), file) + mods.push({Name: file, FolderName: file, Version: "unknown", Disabled: false}) + } + } + } + }) + + let disabledPath = path.join(modpath, "disabled") + 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) { + console.log("error: " + lang("cli.mods.improperjson"), file) + disabled.push({Name: file, FolderName: file, Version: "unknown", Disabled: false}) + } } } }) - return mods; + return { + enabled: mods, + disabled: disabled, + all: [...mods, ...disabled] + }; }, -} + get: (mod) => { + let list = mods.list().all; + + for (let i = 0; i < list.length; i++) { + if (list[i].Name == mod) { + return list[i]; + } else {continue} + } + + return false; + }, + install: (mod) => { + if (fs.statSync(mod).isDirectory()) { + if (fs.statSync(path.join(mod, "mod.json"))) { + copy(mod, path.join(modpath, mod.replace(/^.*(\\|\/|\:)/, "")), { + mode: true, + cover: true, + utimes: true, + }, (err) => { + if(err) {console.log("error:", err)}; + console.log(); + }); + cli.exit(); + return + } else { + console.log("error: " + lang("cli.mods.notamod")) + cli.exit(1); + return; + } + } + + fs.createReadStream(mod).pipe(unzip.Extract({path: modpath})) + .on("finish", () => { + cli.exit(); + }); + }, + remove: (mod) => { + let modName = mods.get(mod).FolderName; + let modPath = path.join(modpath, modName); + if (fs.statSync(modPath).isDirectory()) { + fs.rmSync(modPath, {recursive: true}); + cli.exit(); + } else { + cli.exit(1); + } + }, + toggle: (mod) => { + let disabled = path.join(modpath, "disabled"); + if (! fs.existsSync(disabled)) { + fs.mkdirSync(disabled) + } + + let modName = mods.get(mod).FolderName; + 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) + } +}; module.exports = { mods, -- cgit v1.2.3