Add shared mods management

Add the ability to manage a set of mods to be shared by the master
server to the rest of the cluster.  This implements storage, uploads,
downloads, deletetion, and listing details of mods but does not actually
do anything with the stored mods.
This commit is contained in:
Hornwitser committed 2022-09-13 19:15:46 +02:00
1 parent 2ab915cc00
commit 3b57c1e0ae
23 files changed
+1311 -5

No files matched your search

+63 -1
View File
@@ -1,4 +1,4 @@
import { libErrors, libLink, libLogging, libPlugin } from "@clusterio/lib";
import { libData, libErrors, libLink, libLogging, libPlugin } from "@clusterio/lib";
const { logger } = libLogging;
import packageJson from "../../package.json";
@@ -86,6 +86,7 @@ export class Control extends libLink.Link {
this.slaveUpdateHandlers = new Map();
this.instanceUpdateHandlers = new Map();
this.saveListUpdateHandlers = new Map();
this.modUpdateHandlers = new Map();
this.userUpdateHandlers = new Map();
this.logHandlers = new Map();
@@ -102,6 +103,9 @@ export class Control extends libLink.Link {
this.updateSaveListSubscriptions().catch(err => logger.error(
`Unexpected error updating save list subscriptions:\n${err.stack}`
));
this.updateModSubscriptions().catch(err => logger.error(
`Unexpected error updating mod subscriptions:\n${err.stack}`
));
this.updateUserSubscriptions().catch(err => logger.error(
`Unexpected error updating user subscriptions:\n${err.stack}`
));
@@ -319,6 +323,64 @@ export class Control extends libLink.Link {
});
}
async modUpdateEventHandler(message) {
let mod = new libData.ModInfo(message.data.mod);
let handlers = [].concat(
this.modUpdateHandlers.get(null) || [],
this.modUpdateHandlers.get(mod.name) || []
);
for (let handler of handlers) {
handler(mod);
}
}
async onModUpdate(name, handler) {
if (name !== null && typeof name !== "string") {
throw new Error("Invalid mod name");
}
let handlers = this.modUpdateHandlers.get(name);
if (!handlers) {
handlers = [];
this.modUpdateHandlers.set(name, handlers);
}
handlers.push(handler);
if (handlers.length === 1) {
await this.updateModSubscriptions();
}
}
async offModUpdate(name, handler) {
let handlers = this.modUpdateHandlers.get(name);
if (!handlers || !handlers.length) {
throw new Error(`No handlers for mod pack ${name} exist`);
}
let index = handlers.lastIndexOf(handler);
if (index === -1) {
throw new Error(`Given handler is not registered for mod pack ${name}`);
}
handlers.splice(index, 1);
if (!handlers.length) {
this.modUpdateHandlers.delete(name);
await this.updateModSubscriptions();
}
}
async updateModSubscriptions() {
if (!this.connector.connected) {
return;
}
await libLink.messages.setModSubscriptions.send(this, {
all: this.modUpdateHandlers.has(null),
mod_names: [...this.modUpdateHandlers.keys()].filter(k => k !== null),
});
}
async userUpdateEventHandler(message) {
let handlers = [].concat(
this.userUpdateHandlers.get(null) || [],