Skip to content

Commit

Permalink
Merge pull request #248 from Jan200101/PR/protocol
Browse files Browse the repository at this point in the history
feat: support thunderstore ror2mm protocol for installing mods
  • Loading branch information
0neGal authored Dec 20, 2024
2 parents 8fddf38 + b9c0243 commit 28130f4
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/app/js/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ browser.mod_el = (properties) => {
browserEntries.appendChild(entry);
}

browser.packages = async () => {
await browser.loadfront();
return packages;
}

let recent_toasts = {};
function add_recent_toast(name, timeout = 3000) {
if (recent_toasts[name]) {return}
Expand Down
40 changes: 40 additions & 0 deletions src/app/js/mods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const util = require('util');
const ipcRenderer = require("electron").ipcRenderer;

const lang = require("../../lang");

const version = require("./version");
const toasts = require("./toasts");
const set_buttons = require("./set_buttons");

let mods = {};
Expand Down Expand Up @@ -328,4 +330,42 @@ ipcRenderer.on("mods", (event, mods_obj) => {
mods.load(mods_obj);
})

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) {
alert(util.format(lang("gui.mods.cant_find_specific"), author, package_name));
return;
}

const package_obj = package.versions.find((package_version) => { return package_version.version_number == version; })
if (!package_obj) {
alert(util.format(lang("gui.mods.cant_find_version"), version, author, package_name))
return;
}

toasts.show({
timeout: 3000,
scheme: "info",
title: lang("gui.mods.installing"),
description: package_obj.full_name
})

mods.install_from_url(
package_obj.download_url,
package_obj.dependencies,
false,

author,
package_name,
version
);
})

module.exports = mods;
4 changes: 4 additions & 0 deletions src/app/js/toasts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ toasts.show = (properties) => {
toast.fg = "#FFFFFF";
toast.bg = "#FF9B85";
break
case "info":
toast.fg = "#FFFFFF";
toast.bg = "rgb(var(--blue))";
break
}


Expand Down
26 changes: 25 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require("path");
const { app, BrowserWindow } = require("electron");
const { app, BrowserWindow, dialog } = require("electron");

// makes it so Electron cache doesn't get stored in your system's config
// folder, and instead changing it over to using the system's cache
Expand All @@ -10,11 +10,13 @@ app.setPath("userData", path.join(app.getPath("cache"), app.name));
process.chdir(app.getPath("appData"));

const cli = require("./cli");
const lang = require("./lang");

const mods = require("./modules/mods");
const update = require("./modules/update");
const version = require("./modules/version");
const settings = require("./modules/settings");
const protocol = require("./modules/protocol");

// loads `ipcMain` events that dont fit in any of the modules directly
require("./modules/ipc");
Expand Down Expand Up @@ -88,6 +90,7 @@ function start() {

// load list of mods on initial load
win.webContents.on("dom-ready", () => {
protocol();
send("mods", mods.list());
})

Expand All @@ -103,6 +106,7 @@ function start() {
}
}


// starts the GUI or CLI
if (cli.hasArgs()) {
if (cli.hasParam("update-viper")) {
Expand All @@ -112,9 +116,29 @@ if (cli.hasArgs()) {
cli.init();
}
} else {
app.setAsDefaultProtocolClient("ror2mm");

const app_lock = app.requestSingleInstanceLock()

// start the window/GUI
app.on("ready", () => {
if (!app_lock) {
// Viper is already running
if (process.argv.length <= (app.isPackaged ? 1 : 2))
{
dialog.showMessageBoxSync({
title: lang("viper.menu.main"),
message: lang("viper.already_running")
});
}
app.quit();
}

start();
})

app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
protocol(commandLine);
})

}
3 changes: 3 additions & 0 deletions src/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"install": "Installieren",
"launch": "Starten",
"mods": {
"cant_find_specific": "Mod %s-%s kann nicht gefunden werden!",
"cant_find_version": "Version %s von Mod %s-%s kann nicht gefunden werden!",
"confirm_dependencies": "Dieser Mod benötigt weitere Mods, diese werden unter dieser Nachricht angezeigt. Beim drücken auf \"Ok\" stimmst du zu da diese Installiert werden.\n\n",
"confirm_plugins": {
"description": "Native plugins haben sehr viel mehr Rechte als reguläre Mods, da durch ist das nutzen dieser um einiges unsicherer denn es ist einfacher ihnen zuschaden! Bitte installieren sie nur native plugins von vertrauten Entwicklern oder ähnliches, falls dir bewusst ist was du machst kannst du diese Nachricht ignorieren.",
Expand Down Expand Up @@ -284,6 +286,7 @@
"settings": "Einstellungen"
},
"viper": {
"already_running": "Viper läuft bereits",
"info": {
"credits": "Credits",
"discord": "Tritt dem Discord bei:",
Expand Down
4 changes: 4 additions & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
"installed_mod": "Installed mod!",
"drag_n_drop": "Drag and drop a mod to install",
"confirm_dependencies": "This package has dependencies, shown below, clicking \"Ok\" will install the package and the dependencies.\n\n",
"cant_find_specific": "Can't find mod %s-%s!",
"cant_find_version": "Can't find version %s of mod %s-%s!",

"confirm_plugins": {
"title": "The following package has native plugins:",
Expand Down Expand Up @@ -282,6 +284,8 @@
},

"viper": {
"already_running": "Viper is already running",

"menu": {
"main": "Viper",
"release": "Release Notes",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"install": "Installer",
"launch": "Jouer",
"mods": {
"cant_find_specific": "Incapable de trouver le mod %s-%s!",
"cant_find_version": "Incapable de trouver la version %s du mod %s-%s!",
"confirm_dependencies": "Ce mod a des dépendances (affichées ci-dessous), cliquer \"Ok\" les installera en même temps que le mod.\n\n",
"confirm_plugins": {
"description": "Les plugins ont des accès à votre système, comparés aux mods classiques, et sont de fait plus dangereux à l'installation, comme pourrait l'être un plugin contenant un malware. Si ce plugin provient d'un tiers de confiance ou si vous savez ce que vous faites, ne tenez pas compte de ce message.",
Expand Down Expand Up @@ -284,6 +286,7 @@
"settings": "Paramètres"
},
"viper": {
"already_running": "Viper est déjà en cours d'éxecution",
"info": {
"credits": "Remerciements",
"discord": "Rejoingnez le serveur Discord :",
Expand Down
3 changes: 3 additions & 0 deletions src/lang/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"install": "安装",
"launch": "启动",
"mods": {
"cant_find_specific": "无法找到模组%s-%s!",
"cant_find_version": "无法找到%s版的模组%s-%s!",
"confirm_dependencies": "此包含有依赖项, 以下展示, 点击 \"Ok\" 将会安装此包和它的依赖项.\n\n",
"confirm_plugins": {
"description": "Native插件比普通的模组拥有更多对系统的访问权限, 如果安装了恶意插件可能会导致您的计算机受到损害. 如果这个插件是来自于一位信任的开发者或您知道自己在干什么, 那么请忽略这条信息.",
Expand Down Expand Up @@ -284,6 +286,7 @@
"settings": "设置"
},
"viper": {
"already_running": "Viper已在运行中",
"info": {
"credits": "鸣谢",
"discord": "加入Discord:",
Expand Down
1 change: 1 addition & 0 deletions src/modules/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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
Expand Down
39 changes: 39 additions & 0 deletions src/modules/protocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { app } = require("electron");

const win = require("../win");
const version = require("./version");

module.exports = async (argv) => {
if (version.northstar() == "unknown")
return;

const args = argv || process.argv;

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 package_name = 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;

if (term == "install") {
win().send("protocol-install-mod", [domain, author, package_name, version]);
}
}
}
}

0 comments on commit 28130f4

Please sign in to comment.