diff options
author | 0neGal <mail@0negal.com> | 2024-06-09 18:15:45 +0200 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2024-06-09 18:23:29 +0200 |
commit | 760031c079ce830755ba4fea029e149f4140e00b (patch) | |
tree | 8dbb9f8d5bb4e19cdaffb40e8a091b457a597769 /src/app/js/mods.js | |
parent | 1c1e8fb730b9974cd9b8060499773b0f37ff28d2 (diff) | |
parent | 04b0e9fcea6c60257d7bc68994103eacb340a82b (diff) | |
download | Viper-760031c079ce830755ba4fea029e149f4140e00b.tar.gz Viper-760031c079ce830755ba4fea029e149f4140e00b.zip |
Merge branch 'main' into linux-launch-v2linux-launch-v2
Diffstat (limited to 'src/app/js/mods.js')
-rw-r--r-- | src/app/js/mods.js | 141 |
1 files changed, 130 insertions, 11 deletions
diff --git a/src/app/js/mods.js b/src/app/js/mods.js index 182bddf..f463ddb 100644 --- a/src/app/js/mods.js +++ b/src/app/js/mods.js @@ -1,17 +1,35 @@ -var mods = {}; +const ipcRenderer = require("electron").ipcRenderer; + +const lang = require("../../lang"); + +const version = require("./version"); +const set_buttons = require("./set_buttons"); + +let mods = {}; + +let mods_list = { + all: [], + enabled: [], + disabled: [] +} + +// returns the list of mods +mods.list = () => { + return mods_list; +} mods.load = (mods_obj) => { modcount.innerHTML = `${lang("gui.mods.count")} ${mods_obj.all.length}`; let normalized_names = []; - + let set_mod = (mod) => { let name = mod.name; if (mod.package) { name = mod.package.package_name; } - let normalized_name = "mod-list-" + normalize(name); + let normalized_name = "mod-list-" + mods.normalize(name); normalized_names.push(normalized_name); @@ -133,8 +151,8 @@ mods.load = (mods_obj) => { let image_el = image_container.querySelector("img") let image_blur_el = image_container.querySelector("img.blur") - if (mod_versions[mod]) { - image_el.src = mod_versions[mod].package.versions[0].icon; + if (browser.mod_versions[mod]) { + image_el.src = browser.mod_versions[mod].package.versions[0].icon; } if (image_el.getAttribute("src") && @@ -148,13 +166,13 @@ mods.load = (mods_obj) => { image_container.parentElement.classList.add("has-icon"); } - if (mod_versions[mod] - && mod_versions[mod].has_update) { + if (browser.mod_versions[mod] + && browser.mod_versions[mod].has_update) { mod_els[i].querySelector(".update").style.display = null; mod_els[i].querySelector(".update").setAttribute( - "onclick", `mod_versions["${mod}"].install()` + "onclick", `browser.mod_versions["${mod}"].install()` ) if (mod_update_els.includes(mod_els[i].id)) { @@ -170,11 +188,11 @@ mods.load = (mods_obj) => { mod_el.classList.add("no-animation"); mod_el.querySelector(".switch").addEventListener("click", () => { - if (mod_versions[mod].local_name) { - mods.toggle(mod_versions[mod].local_name); + if (browser.mod_versions[mod].local_name) { + mods.toggle(browser.mod_versions[mod].local_name); } }) - + mod_els[i].remove(); modsdiv.querySelector(".line").after(mod_el); } else { @@ -210,3 +228,104 @@ mods.toggle = (mod) => { ipcRenderer.send("toggle-mod", mod); } + +mods.install_queue = []; + +// tells the main process to install a mod through the file selector +mods.install_prompt = () => { + set_buttons(false); + ipcRenderer.send("install-mod"); +} + +// tells the main process to directly install a mod from this path +mods.install_from_path = (path) => { + set_buttons(false); + ipcRenderer.send("install-from-path", path); +} + +// tells the main process to install a mod from a URL +mods.install_from_url = (url, dependencies, clearqueue, author, package_name, version) => { + if (clearqueue) {mods.install_queue = []}; + + let prettydepends = []; + + if (dependencies) { + let newdepends = []; + for (let i = 0; i < dependencies.length; i++) { + let depend = dependencies[i].toLowerCase(); + if (! depend.match(/northstar-northstar-.*/)) { + depend = dependencies[i].replaceAll("-", "/"); + let pkg = depend.split("/"); + if (! mods.is_installed(pkg[1])) { + newdepends.push({ + pkg: depend, + author: pkg[0], + version: pkg[2], + package_name: pkg[1] + }); + + prettydepends.push(`${pkg[1]} v${pkg[2]} - ${lang("gui.browser.made_by")} ${pkg[0]}`); + } + } + } + + dependencies = newdepends; + } + + if (dependencies && dependencies.length != 0) { + let confirminstall = confirm(lang("gui.mods.confirm_dependencies") + prettydepends.join("\n")); + if (! confirminstall) { + return; + } + } + + set_buttons(false); + ipcRenderer.send("install-from-url", url, author, package_name, version); + + if (dependencies) { + mods.install_queue = dependencies; + } +} + +mods.is_installed = (modname) => { + for (let i = 0; i < mods.list().all.length; i++) { + let mod = mods.list().all[i]; + if (mod.manifest_name) { + if (mod.manifest_name.match(modname)) { + return true; + } + } else if (mod.name.match(modname)) { + return true; + } + } + + return false; +} + +mods.normalize = (items) => { + let main = (string) => { + return string.replaceAll(" ", "") + .replaceAll(".", "").replaceAll("-", "") + .replaceAll("_", "").toLowerCase(); + } + if (typeof items == "string") { + return main(items); + } else { + let newArray = []; + for (let i = 0; i < items.length; i++) { + newArray.push(main(items[i])); + } + + return newArray; + } +} + +// updates the installed mods +ipcRenderer.on("mods", (event, mods_obj) => { + mods_list = mods_obj; + if (! mods_obj) {return} + + mods.load(mods_obj); +}) + +module.exports = mods; |