Implement mod pack storage

Add ModPack data type with corresponding storage on the master, messages
to create, update and delete them, as well as commands to invoke them.
The ModPack data type contains a list of mods with versions and optional
sha1 hashes, and mod settings to be used.
This commit is contained in:
Hornwitser committed 2022-09-16 02:44:06 +02:00
1 parent dfcbc559b8
commit 9fb3d6d8db
16 files changed
+987 -3

No files matched your search

+62
View File
@@ -5,7 +5,9 @@ const jwt = require("jsonwebtoken");
const path = require("path");
const libConfig = require("@clusterio/lib/config");
const libData = require("@clusterio/lib/data");
const libErrors = require("@clusterio/lib/errors");
const libHelpers = require("@clusterio/lib/helpers");
const libLink = require("@clusterio/lib/link");
const { logger } = require("@clusterio/lib/logging");
const libLoggingUtils = require("@clusterio/lib/logging_utils");
@@ -55,6 +57,11 @@ class ControlConnection extends BaseConnection {
instance_ids: [],
};
this.modPackSubscriptions = {
all: false,
mod_pack_ids: [],
};
this.modSubscriptions = {
all: false,
mod_names: [],
@@ -465,6 +472,61 @@ class ControlConnection extends BaseConnection {
return { save: result.save };
}
async getModPackRequestHandler(message) {
let { id } = message.data;
let modPack = this._master.modPacks.get(id);
if (!modPack) {
throw new libErrors.RequestError(`Mod pack with ID ${id} does not exist`);
}
return { mod_pack: modPack.toJSON() };
}
async listModPacksRequestHandler(message) {
return { list: [...this._master.modPacks.values()].map(pack => pack.toJSON()) };
}
async setModPackSubscriptionsRequestHandler(message) {
this.modPackSubscriptions = message.data;
}
async createModPackRequestHandler(message) {
let modPack = new libData.ModPack(message.data.mod_pack);
if (this._master.modPacks.has(modPack.id)) {
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} already exist`);
}
this._master.modPacks.set(modPack.id, modPack);
this._master.modPackUpdated(modPack);
}
async updateModPackRequestHandler(message) {
let modPack = new libData.ModPack(message.data.mod_pack);
if (!this._master.modPacks.has(modPack.id)) {
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
}
this._master.modPacks.set(modPack.id, modPack);
this._master.modPackUpdated(modPack);
}
async deleteModPackRequestHandler(message) {
let { id } = message.data;
let modPack = this._master.modPacks.get(id);
if (!modPack) {
throw new libErrors.RequestError(`Mod pack with ID ${id} does not exist`);
}
modPack.isDeleted = true;
this._master.modPacks.delete(id);
this._master.modPackUpdated(modPack);
}
modPackUpdated(modPack) {
if (
this.modPackSubscriptions.all
|| this.modPackSubscriptions.mod_pack_ids.includes(modPack.id)
) {
libLink.messages.modPackUpdate.send(this, { mod_pack: modPack.toJSON() });
}
}
async getModRequestHandler(message) {
let { name, version } = message.data;
let key = `${name}_${version}`;
+39
View File
@@ -66,6 +66,12 @@ class Master {
*/
this.instances = null;
/**
* Mapping of mod pack id to mod pack
* @type {Map<number, module:lib/data/ModPack>}
*/
this.modPacks = null;
/**
* Mapping of mod names to mod infos
* @type {Map<string, module:lib/data/ModInfo>}
@@ -168,6 +174,7 @@ class Master {
this.slaves = await Master.loadSlaves(path.join(databaseDirectory, "slaves.json"));
this.instances = await Master.loadInstances(path.join(databaseDirectory, "instances.json"));
this.modPacks = await Master.loadModPacks(path.join(databaseDirectory, "mod-packs.json"));
this.userManager = new UserManager(this.config);
await this.userManager.load(path.join(databaseDirectory, "users.json"));
this.exportManifest = await Master.loadJsonObject(path.join(databaseDirectory, "export_manifest.json"));
@@ -295,6 +302,10 @@ class Master {
await Master.saveInstances(path.join(databaseDirectory, "instances.json"), this.instances);
}
if (this.modPacks) {
await Master.saveModPacks(path.join(databaseDirectory, "mod-packs.json"), this.modPacks);
}
if (this.userManager) {
await this.userManager.save(path.join(databaseDirectory, "users.json"));
}
@@ -371,6 +382,23 @@ class Master {
await libFileOps.safeOutputFile(filePath, JSON.stringify(serialized, null, 4));
}
static async loadModPacks(filePath) {
let json;
try {
json = JSON.parse(await fs.readFile(filePath));
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
return new Map();
}
return new Map(json.map(e => [e.id, new libData.ModPack(e)]));
}
static async saveModPacks(filePath, modPacks) {
await libFileOps.safeOutputFile(filePath, JSON.stringify([...modPacks.values()]));
}
static async loadModInfos(modsDirectory) {
let mods = new Map();
for (let entry of await fs.readdir(modsDirectory, { withFileTypes: true })) {
@@ -562,6 +590,17 @@ class Master {
}
}
modPackUpdated(modPack) {
for (let controlConnection of this.wsServer.controlConnections) {
if (controlConnection.connector.closing) {
continue;
}
controlConnection.modPackUpdated(modPack);
}
libPlugin.invokeHook(this.plugins, "onModPackUpdated", modPack);
}
modUpdated(mod) {
for (let controlConnection of this.wsServer.controlConnections) {
if (controlConnection.connector.closing) {
+5
View File
@@ -3,6 +3,7 @@ const libConfig = require("@clusterio/lib/config");
const libLink = require("@clusterio/lib/link");
const { logger } = require("@clusterio/lib/logging");
const libPlugin = require("@clusterio/lib/plugin");
const libErrors = require("@clusterio/lib/errors");
const PlayerStats = require("@clusterio/lib/PlayerStats");
const BaseConnection = require("./BaseConnection");
@@ -167,6 +168,10 @@ class SlaveConnection extends BaseConnection {
this._master.saveListUpdate(message.data);
}
async getModPackRequestHandler(message) {
throw new libErrors.RequestError("Not implemented");
}
async logMessageEventHandler(message) {
this._master.clusterLogger.log({
...message.data.info,