From 64107e982faf196537b403eb6319796914d7585e Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 13 Aug 2024 22:44:24 +0200 Subject: chore: rework protocol mod installs to use renderer package list This is achieved by sending an IPC event to the renderer and waiting for a reply once Since this is async we return from the function after sending the event and recursively invoke it once the reply arrives The package data is returned a a JSON String because Electron is unable to copy the Object over IPC --- src/app/js/browser.js | 2 ++ src/app/js/mods.js | 6 ++++++ src/modules/protocol.js | 27 ++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 3415ebf..153c7b5 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -477,6 +477,8 @@ browser.mod_el = (properties) => { browserEntries.appendChild(entry); } +browser.packages = () => { return packages; } + let recent_toasts = {}; function add_recent_toast(name, timeout = 3000) { if (recent_toasts[name]) {return} diff --git a/src/app/js/mods.js b/src/app/js/mods.js index f463ddb..b257753 100644 --- a/src/app/js/mods.js +++ b/src/app/js/mods.js @@ -328,4 +328,10 @@ ipcRenderer.on("mods", (event, mods_obj) => { mods.load(mods_obj); }) +ipcRenderer.on("packages", (event) => { + // Its not possible to send over the package object directly, so we serialize it + const packages = JSON.stringify(browser.packages()); + event.sender.send("packages-reply", [packages]); +}) + module.exports = mods; diff --git a/src/modules/protocol.js b/src/modules/protocol.js index 8b6cfc6..e8701b2 100644 --- a/src/modules/protocol.js +++ b/src/modules/protocol.js @@ -1,12 +1,33 @@ const { app, ipcMain } = require("electron"); +const win = require("../win"); const requests = require("./requests"); const version = require("./version"); +var packages = undefined; async function install_mod(domain, author, package_name, version) { - let package_data = JSON.parse(await requests.get( - domain, `/api/experimental/package/${author}/${package_name}/${version}/` - )); + if (packages == undefined) + { + ipcMain.once("packages-reply", (event, packages_string) => { + packages = JSON.parse(packages_string); + install_mod(domain, author, package_name, version); + }); + + win().send("packages"); + return; + } + + const package = packages.find((package) => { return package.owner == author && package.name == package_name; }) + if (!package) { + console.error("Couldn't find package") + return; + } + + const package_data = package.versions.find((package_version) => { return package_version.version_number == version; }) + if (!package_data) { + console.error("Couldn't find package_version") + return; + } for (const dep of package_data.dependencies) { let fragments = dep.split("-"); -- cgit v1.2.3