diff options
author | 0neGal <mail@0negal.com> | 2023-07-21 23:10:26 +0200 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2023-07-21 23:17:15 +0200 |
commit | 928333b53e07809bef1897813a4f6588f37c677c (patch) | |
tree | fe2a1724d18c576267a6f7ea333423fa081a5b68 | |
parent | cf83f8fd648dab4eb905ea81dd258160f8b95d33 (diff) | |
download | Viper-928333b53e07809bef1897813a4f6588f37c677c.tar.gz Viper-928333b53e07809bef1897813a4f6588f37c677c.zip |
basic support for showing packages in frontend
-rw-r--r-- | src/modules/mods.js | 139 | ||||
-rw-r--r-- | src/modules/packages.js | 20 |
2 files changed, 112 insertions, 47 deletions
diff --git a/src/modules/mods.js b/src/modules/mods.js index 095e501..62b6184 100644 --- a/src/modules/mods.js +++ b/src/modules/mods.js @@ -53,57 +53,108 @@ mods.list = () => { }; } - let files = fs.readdirSync(mods.path); - files.forEach((file) => { - // return early if `file` isn't a folder - if (! fs.statSync(path.join(mods.path, file)).isDirectory()) { - return; - } + let get_in_dir = (dir, package_obj) => { + let files = fs.readdirSync(dir); + files.forEach((file) => { + // return early if `file` isn't a folder + if (! fs.statSync(path.join(dir, file)).isDirectory()) { + return; + } - let modjson = path.join(mods.path, file, "mod.json"); - - // return early if mod.json doesn't exist or isn't a file - if (! fs.existsSync(modjson) || ! fs.statSync(modjson).isFile()) { - return; - } + let modjson = path.join(dir, file, "mod.json"); + + // return early if mod.json doesn't exist or isn't a file + if (! fs.existsSync(modjson) || ! fs.statSync(modjson).isFile()) { + return; + } + + let mod = json(modjson); + if (! mod) {return} + + let obj = { + Author: mod.Author || false, + Version: mod.Version || "unknown", + Name: mod.Name || "unknown", + Description: mod.Description || "", + + FolderName: file, + FolderPath: path.join(dir, file), + Package: package_obj || false + } + + // Electron's serializer for + // BrowserWindow.webContents.send() is quite broken, and for + // some reason cant serialize the output when `package_obj` + // is anywhere inside, trying to find a solution I + // discovered that simply `JSON.stringify()`'ing + // `package_obj` does let it be able to be parsed in + // `index.js` however, of course now it is a string. + // + // wanting to not have to add extra code to the renderer + // just to fix this, I had to try and look for a better + // solution, and apparently this just somehow works, I have + // literally 0% fucking clue why or how it works and fixes + // it, but I will quite frankly not complain. + // + // whether `obj` has been serialized here or not, the actual + // output is the exact same, but somehow Electron just cant + // handle sending it to the renderer, failing to serialize + // it, by itself... + // + // anyway, you can move along now. :') + obj = JSON.stringify(obj); + obj = JSON.parse(obj); + + if (obj.Package) { + obj.Author = obj.Package.author; + } + + obj.Disabled = ! mods.modfile.get(obj.Name); - let mod = json(modjson); - if (! mod) {return} - - let obj = { - Author: false, - Version: "unknown", - Name: "unknown", - FolderName: file, - ...mod} - - obj.Disabled = ! mods.modfile.get(obj.Name); - - // add manifest data from manifest.json, if it exists - let manifest_file = path.join(mods.path, file, "manifest.json"); - if (fs.existsSync(manifest_file)) { - let manifest = json(manifest_file); - if (manifest != false) { - obj.ManifestName = manifest.name; - if (obj.Version == "unknown") { - obj.Version = manifest.version_number; + // add manifest data from manifest.json, if it exists + let manifest_file = path.join(dir, file, "manifest.json"); + if (fs.existsSync(manifest_file)) { + let manifest = json(manifest_file); + if (manifest != false) { + obj.ManifestName = manifest.name; + if (obj.Version == "unknown") { + obj.Version = manifest.version_number; + } } } - } - // add author data from author file, if it exists - let author_file = path.join(mods.path, file, "thunderstore_author.txt"); - if (fs.existsSync(author_file)) { - obj.Author = fs.readFileSync(author_file, "utf8"); - } + // add author data from author file, if it exists + let author_file = path.join(dir, file, "thunderstore_author.txt"); + if (fs.existsSync(author_file)) { + obj.Author = fs.readFileSync(author_file, "utf8"); + } - // add mod to their respective disabled or enabled Array - if (obj.Disabled) { - disabled.push(obj); - } else { - enabled.push(obj); + // add mod to their respective disabled or enabled Array + if (obj.Disabled) { + disabled.push(obj); + } else { + enabled.push(obj); + } + }) + } + + // get mods in `mods` folder + get_in_dir(mods.path); + + // get mods in `packages` folder + let package_list = require("./packages").list(); + for (let i in package_list) { + // make sure the package actually has mods + if (! package_list[i].has_mods) { + continue; } - }) + + // search the package's `mods` folder + get_in_dir( + path.join(package_list[i].package_path, "mods"), + package_list[i] + ) + } return { enabled: enabled, diff --git a/src/modules/packages.js b/src/modules/packages.js index bb610d5..62ef21e 100644 --- a/src/modules/packages.js +++ b/src/modules/packages.js @@ -4,7 +4,6 @@ const unzip = require("unzipper"); const app = require("electron").app; const https = require("follow-redirects").https; -const mods = require("./mods"); const json = require("./json"); const win = require("./window"); const settings = require("./settings"); @@ -73,6 +72,9 @@ packages.list = (dir = packages.path) => { // this is whether or not the package has plugins has_plugins: (verification == "has-plugins"), + // this will be set later on + has_mods: false, + // contents of `manifest.json` or `false` if it can // be parsed correctly manifest: json( @@ -80,6 +82,17 @@ packages.list = (dir = packages.path) => { ), } + // if the package has a `mods` folder, and it's not + // empty, then we can assume that the package does + // indeed have mods + let mods_dir = path.join(package_path, "mods"); + if (fs.existsSync(mods_dir) && + fs.lstatSync(mods_dir).isDirectory() && + fs.readdirSync(mods_dir).length >= 1) { + + package_list[files[i]].has_mods = true; + } + // add `.remove()` function, mostly just a shorthand package_list[files[i]].remove = () => { return packages.remove( @@ -199,6 +212,7 @@ packages.install = async (url, author, package_name, version) => { console.log("Deleting older version(s), if it exists:", name); // check and delete any mod with the name package details in the old // `mods` folder, if there are any at all + let mods = require("./mods"); let mods_list = mods.list().all; for (let i = 0; i < mods_list.length; i++) { let mod = mods_list[i]; @@ -313,13 +327,13 @@ packages.verify = (package_path) => { } // check if there are any plugins in the package - let mods = path.join(package_path, "mods"); + let mods_path = path.join(package_path, "mods"); let plugins = path.join(package_path, "plugins"); if (fs.existsSync(plugins) && fs.lstatSync(plugins).isDirectory()) { // package has plugins, the function calling `packages.verify()` // will have to handle this at their own discretion return "has-plugins"; - } else if (! fs.existsSync(mods) || fs.lstatSync(mods).isFile()) { + } else if (! fs.existsSync(mods_path) || fs.lstatSync(mods_path).isFile()) { // if there are no plugins, then we check if there are any mods, // if not, then it means there are both no plugins and mods, so // we signal that back |