Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support thunderstore ror2mm protocol for installing mods #248

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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 +89,7 @@ function start() {

// load list of mods on initial load
win.webContents.on("dom-ready", () => {
protocol();
0neGal marked this conversation as resolved.
Show resolved Hide resolved
send("mods", mods.list());
})

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


// starts the GUI or CLI
if (cli.hasArgs()) {
if (cli.hasParam("update-viper")) {
Expand All @@ -112,9 +115,10 @@ if (cli.hasArgs()) {
cli.init();
}
} else {
app.setAsDefaultProtocolClient("ror2mm");
Jan200101 marked this conversation as resolved.
Show resolved Hide resolved

// start the window/GUI
app.on("ready", () => {

start();
})
}
2 changes: 2 additions & 0 deletions src/modules/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var packages = {};

// lets renderer install packages
ipcMain.on("install-from-url", (event, url, author, package_name, version) => {
console.log(url);
Jan200101 marked this conversation as resolved.
Show resolved Hide resolved
packages.install(url, author, package_name, version);
})

Expand Down Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions src/modules/protocol.js
Original file line number Diff line number Diff line change
@@ -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}/`
));
Jan200101 marked this conversation as resolved.
Show resolved Hide resolved

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this for the sake of sanity
when packaged the first argument is the bundled electron app
when not packaged the first argument is electron and the second the script.
Not really needed, can remove.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ternary operators are the devil and not as readable imo, something like this seems better:

let args = process.argv;
if (app.isPackaged) {args.shift()}


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 {
Jan200101 marked this conversation as resolved.
Show resolved Hide resolved
if (term == "install") {
await install_mod(domain, author, mod, version);
}
}catch(err) {
console.error(err);
continue;
}
}
}
}
Loading