From 56f2cb84412252ae0a90076b861f3b1be32be01d Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 4 Aug 2024 18:44:53 +0200 Subject: feat: support thunderstore ror2mm protocol for installing mods --- src/modules/packages.js | 2 ++ src/modules/protocol.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/modules/protocol.js (limited to 'src/modules') diff --git a/src/modules/packages.js b/src/modules/packages.js index 8df6665..f7e7d62 100644 --- a/src/modules/packages.js +++ b/src/modules/packages.js @@ -16,6 +16,7 @@ var packages = {}; // lets renderer install packages ipcMain.on("install-from-url", (event, url, author, package_name, version) => { + console.log(url); packages.install(url, author, package_name, version); }) @@ -194,6 +195,7 @@ packages.install = async (url, author, package_name, version) => { return false; } + let name = packages.format_name(author, package_name, version); // removes zip's and folders diff --git a/src/modules/protocol.js b/src/modules/protocol.js new file mode 100644 index 0000000..8b6cfc6 --- /dev/null +++ b/src/modules/protocol.js @@ -0,0 +1,66 @@ +const { app, ipcMain } = require("electron"); + +const requests = require("./requests"); +const version = require("./version"); + +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}/` + )); + + for (const dep of package_data.dependencies) { + let fragments = dep.split("-"); + if (fragments.length != 3) { + console.error("bad dep") + return; + } + + if (fragments[0] != "northstar") + await install_mod(domain, ...fragments); + } + + let result = ipcMain.emit("install-from-url", null, package_data.download_url, author, package_name, version); + if (!result) { + console.error("no install-from-url handler") + } +} + +module.exports = async () => { + if (version.northstar() == "unknown") + return; + + const args = process.argv.slice(app.isPackaged ? 1 : 2); + + for (const key of args) { + if (key.startsWith("ror2mm://")) { + let fragments = key.slice(9).split("/"); + + if (fragments.length < 6) + return; + + const ver = fragments[0]; + const term = fragments[1]; + const domain = fragments[2]; + const author = fragments[3]; + const mod = fragments[4]; + const version = fragments[5]; + + // There is only v1 + if (ver != "v1") + continue; + + // No support for custom thunderstore instances + if (domain != "thunderstore.io") + continue; + + try { + if (term == "install") { + await install_mod(domain, author, mod, version); + } + }catch(err) { + console.error(err); + continue; + } + } + } +} -- cgit v1.2.3 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(-) (limited to 'src/modules') 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 From 05d9c0acd4c87d34ed31edc6683e2932058a30dd Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 13 Aug 2024 23:02:34 +0200 Subject: chore: use existing mod installation logic for r2mm protocol --- src/app/js/browser.js | 5 ++++- src/app/js/mods.js | 35 +++++++++++++++++++++++++++++++---- src/modules/protocol.js | 49 +++---------------------------------------------- 3 files changed, 38 insertions(+), 51 deletions(-) (limited to 'src/modules') diff --git a/src/app/js/browser.js b/src/app/js/browser.js index 153c7b5..b2e0493 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -477,7 +477,10 @@ browser.mod_el = (properties) => { browserEntries.appendChild(entry); } -browser.packages = () => { return packages; } +browser.packages = async () => { + await browser.loadfront(); + return packages; +} let recent_toasts = {}; function add_recent_toast(name, timeout = 3000) { diff --git a/src/app/js/mods.js b/src/app/js/mods.js index b257753..349f5b9 100644 --- a/src/app/js/mods.js +++ b/src/app/js/mods.js @@ -328,10 +328,37 @@ 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]); +ipcRenderer.on("protocol-install-mod", async (event, data) => { + const domain = data[0]; + const author = data[1]; + const package_name = data[2]; + const version = data[3]; + + const packages = await browser.packages(); + + const package = packages.find((package) => { return package.owner == author && package.name == package_name; }) + if (!package) { + console.error("Couldn't find package") + return; + } + + const package_obj = package.versions.find((package_version) => { return package_version.version_number == version; }) + if (!package_obj) { + console.error("Couldn't find package_version") + return; + } + + console.log(package_obj); + + mods.install_from_url( + package_obj.download_url, + package_obj.dependencies, + false, + + author, + package_name, + version + ); }) module.exports = mods; diff --git a/src/modules/protocol.js b/src/modules/protocol.js index e8701b2..a40b67e 100644 --- a/src/modules/protocol.js +++ b/src/modules/protocol.js @@ -1,51 +1,8 @@ -const { app, ipcMain } = require("electron"); +const { app } = 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) { - 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("-"); - if (fragments.length != 3) { - console.error("bad dep") - return; - } - - if (fragments[0] != "northstar") - await install_mod(domain, ...fragments); - } - - let result = ipcMain.emit("install-from-url", null, package_data.download_url, author, package_name, version); - if (!result) { - console.error("no install-from-url handler") - } -} - module.exports = async () => { if (version.northstar() == "unknown") return; @@ -63,7 +20,7 @@ module.exports = async () => { const term = fragments[1]; const domain = fragments[2]; const author = fragments[3]; - const mod = fragments[4]; + const package_name = fragments[4]; const version = fragments[5]; // There is only v1 @@ -76,7 +33,7 @@ module.exports = async () => { try { if (term == "install") { - await install_mod(domain, author, mod, version); + win().send("protocol-install-mod", [domain, author, package_name, version]); } }catch(err) { console.error(err); -- cgit v1.2.3 From 29e7a323d26e985f47015ad8b64315fe8afa5b72 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 14 Aug 2024 12:13:14 +0200 Subject: feat: parse commandLine of secondary instances --- src/index.js | 5 +++++ src/modules/protocol.js | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src/modules') diff --git a/src/index.js b/src/index.js index d8107e8..ab15039 100644 --- a/src/index.js +++ b/src/index.js @@ -128,4 +128,9 @@ if (cli.hasArgs()) { app.on("ready", () => { start(); }) + + app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => { + protocol(commandLine); + }) + } diff --git a/src/modules/protocol.js b/src/modules/protocol.js index a40b67e..7f2f7c0 100644 --- a/src/modules/protocol.js +++ b/src/modules/protocol.js @@ -3,11 +3,11 @@ const { app } = require("electron"); const win = require("../win"); const version = require("./version"); -module.exports = async () => { +module.exports = async (argv) => { if (version.northstar() == "unknown") return; - const args = process.argv.slice(app.isPackaged ? 1 : 2); + const args = argv || process.argv; for (const key of args) { if (key.startsWith("ror2mm://")) { -- cgit v1.2.3 From de2e5bc6e041e8b42834d928ba898df4f328e6e1 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 14 Aug 2024 19:40:15 +0200 Subject: chore: remove useless try catch --- src/modules/protocol.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src/modules') diff --git a/src/modules/protocol.js b/src/modules/protocol.js index 7f2f7c0..1aeb7f9 100644 --- a/src/modules/protocol.js +++ b/src/modules/protocol.js @@ -31,13 +31,8 @@ module.exports = async (argv) => { if (domain != "thunderstore.io") continue; - try { - if (term == "install") { - win().send("protocol-install-mod", [domain, author, package_name, version]); - } - }catch(err) { - console.error(err); - continue; + if (term == "install") { + win().send("protocol-install-mod", [domain, author, package_name, version]); } } } -- cgit v1.2.3 From 723c9eb8f87e434d4d00c7ac82ac06f7a8436c32 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 14 Aug 2024 19:40:40 +0200 Subject: chore: remove debug logging --- src/modules/packages.js | 1 - 1 file changed, 1 deletion(-) (limited to 'src/modules') diff --git a/src/modules/packages.js b/src/modules/packages.js index f7e7d62..125705a 100644 --- a/src/modules/packages.js +++ b/src/modules/packages.js @@ -16,7 +16,6 @@ var packages = {}; // lets renderer install packages ipcMain.on("install-from-url", (event, url, author, package_name, version) => { - console.log(url); packages.install(url, author, package_name, version); }) -- cgit v1.2.3