mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
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:
1 parent
dfcbc559b8
commit
9fb3d6d8db
16 files changed
+987
-3
No files matched your search
@@ -198,6 +198,39 @@ async function resolveInstance(client, instanceName) {
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a string to a mod pack ID
|
||||
*
|
||||
* Resolevs a string with either a mod pack name or an id into an integer
|
||||
* with the mod pack ID.
|
||||
*
|
||||
* @param {module:lib/link.Link} client -
|
||||
* link to master server to query mod pack on.
|
||||
* @param {string} modPackName - string with name or id of mod pack.
|
||||
* @returns {Promise<number>} mod pack ID.
|
||||
* @static
|
||||
*/
|
||||
async function resolveModPack(client, modPackName) {
|
||||
let modPackId;
|
||||
if (/^-?\d+/.test(modPackName)) {
|
||||
modPackId = parseInt(modPackName, 10);
|
||||
} else {
|
||||
let response = await libLink.messages.listModPacks.send(client);
|
||||
for (let mod_pack of response.list) {
|
||||
if (mod_pack["name"] === modPackName) {
|
||||
modPackId = mod_pack["id"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (modPackId === undefined) {
|
||||
throw new libErrors.CommandError(`No mod pack named ${modPackName}`);
|
||||
}
|
||||
}
|
||||
|
||||
return modPackId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve role object from string
|
||||
*
|
||||
@@ -244,5 +277,6 @@ module.exports = {
|
||||
|
||||
resolveSlave,
|
||||
resolveInstance,
|
||||
resolveModPack,
|
||||
retrieveRole,
|
||||
};
|
||||
@@ -0,0 +1,215 @@
|
||||
"use strict";
|
||||
const zlib = require("zlib");
|
||||
|
||||
const libSchema = require("../schema");
|
||||
|
||||
|
||||
/**
|
||||
* A setting for a mod.
|
||||
* @typedef {object} module:lib/data/ModPack~ModSetting
|
||||
* @property {boolean|number|string} value - Value of the given mod setting.
|
||||
*/
|
||||
const ModSettingJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["value"],
|
||||
properties: {
|
||||
"value": { type: ["boolean", "number", "string"] },
|
||||
},
|
||||
};
|
||||
|
||||
const ModSettingsJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: ModSettingJsonSchema,
|
||||
};
|
||||
|
||||
/**
|
||||
* A mod in a mod pack
|
||||
* @typedef {object} module:lib/data/ModPack~ModRecord
|
||||
* @property {string} name - name of the mod.
|
||||
* @property {string} version - version of the mod.
|
||||
* @property {string} hash - hash of the zip file.
|
||||
*/
|
||||
const ModRecordJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["name", "version"],
|
||||
properties: {
|
||||
"name": { type: "string" },
|
||||
"version": { type: "string" },
|
||||
"hash": { type: "string" },
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Factorio Mod Pack
|
||||
*
|
||||
* Tracks mods and settings for a collection of Factorio mods.
|
||||
* @alias module:lib/data/ModPack
|
||||
*/
|
||||
class ModPack {
|
||||
/**
|
||||
* Id of this mod pack
|
||||
* @type {number}
|
||||
*/
|
||||
id;
|
||||
|
||||
/**
|
||||
* Name of this mod pack.
|
||||
* @type {string}
|
||||
*/
|
||||
name = "";
|
||||
|
||||
/**
|
||||
* Description for this modpcak.
|
||||
* @type {string}
|
||||
*/
|
||||
description = "";
|
||||
|
||||
/**
|
||||
* Version of Factorio this mod pack is for
|
||||
* @type {string}
|
||||
*/
|
||||
factorioVersion = "1.1.0";
|
||||
|
||||
/**
|
||||
* Integer representation of the factorioVersion
|
||||
* @type {number}
|
||||
*/
|
||||
get integerFactorioVersion() {
|
||||
const [major, minor, sub] = this.factorioVersion.split(".").map(n => Number.parseInt(n, 10));
|
||||
return major * 0x100000000 + minor * 0x10000 + (sub || 0); // Can't use bitwise here because this is 48-bits.
|
||||
}
|
||||
|
||||
/**
|
||||
* Mods included in this mod pack
|
||||
* @type {Map<string, module:lib/data/ModPack~ModRecord>}
|
||||
*/
|
||||
mods = new Map();
|
||||
|
||||
/**
|
||||
* Mod settings for this mod pack
|
||||
* @type {Object<string, Map<string, module:lib/data/ModPack~ModSetting>>=}
|
||||
*/
|
||||
settings = {
|
||||
"startup": new Map(),
|
||||
"runtime-global": new Map(),
|
||||
"runtime-per-user": new Map(),
|
||||
};
|
||||
|
||||
/**
|
||||
* True if this mod pack has been deleted from the list of mod packs.
|
||||
* @type {boolean}
|
||||
*/
|
||||
isDeleted = false;
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
"id": { type: "integer" },
|
||||
"name": { type: "string" },
|
||||
"description": { type: "string" },
|
||||
"factorio_version": { type: "string" },
|
||||
"mods": {
|
||||
type: "array",
|
||||
items: ModRecordJsonSchema,
|
||||
},
|
||||
"settings": {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["startup", "runtime-global", "runtime-per-user"],
|
||||
properties: {
|
||||
"startup": ModSettingsJsonSchema,
|
||||
"runtime-global": ModSettingsJsonSchema,
|
||||
"runtime-per-user": ModSettingsJsonSchema,
|
||||
},
|
||||
},
|
||||
"is_deleted": { type: "boolean" },
|
||||
},
|
||||
};
|
||||
|
||||
static validate = libSchema.compile(this.jsonSchema);
|
||||
|
||||
constructor(json = {}) {
|
||||
if (json.id) {
|
||||
this.id = json.id;
|
||||
} else {
|
||||
this.id = Math.random() * 2**31 | 0;
|
||||
}
|
||||
if (json.name) { this.name = json.name; }
|
||||
if (json.description) { this.description = json.description; }
|
||||
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
|
||||
if (json.mods) { this.mods = new Map(json.mods.map(m => [m.name, m])); }
|
||||
if (json.settings) {
|
||||
this.settings = {
|
||||
"startup": new Map(Object.entries(json.settings["startup"])),
|
||||
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
|
||||
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])),
|
||||
};
|
||||
}
|
||||
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
let json = {
|
||||
id: this.id,
|
||||
};
|
||||
if (this.name) { json.name = this.name; }
|
||||
if (this.description) { json.description = this.description; }
|
||||
json.factorio_version = this.factorioVersion;
|
||||
if (this.mods.size) { json.mods = [...this.mods.values()]; }
|
||||
if (this.settings) {
|
||||
json.settings = {
|
||||
"startup": Object.fromEntries(this.settings["startup"]),
|
||||
"runtime-global": Object.fromEntries(this.settings["runtime-global"]),
|
||||
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]),
|
||||
};
|
||||
}
|
||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||
return json;
|
||||
}
|
||||
|
||||
toModPackString(includeHash = false) {
|
||||
const json = this.toJSON();
|
||||
delete json.id;
|
||||
if (json.mods && !includeHash) {
|
||||
json.mods = json.mods.map(m => {
|
||||
m = { ...m };
|
||||
delete m.hash;
|
||||
return m;
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line node/no-sync
|
||||
let buf = zlib.deflateSync(JSON.stringify(json));
|
||||
return buf.toString("base64");
|
||||
}
|
||||
|
||||
static fromModPackString(modPackString) {
|
||||
let buf = Buffer.from(modPackString, "base64");
|
||||
try {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
buf = zlib.inflateSync(buf);
|
||||
} catch (err) {
|
||||
if (err.code.startsWith("Z_")) {
|
||||
throw new Error("Malformed mod pack string: zlib inflate failed");
|
||||
}
|
||||
}
|
||||
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(buf);
|
||||
} catch (err) {
|
||||
throw new Error(`Malformed mod pack string: ${err.message}`);
|
||||
}
|
||||
|
||||
if (!this.validate(json)) {
|
||||
throw new Error("Malformed mod pack string: Schema validation failed");
|
||||
}
|
||||
|
||||
return new this(json);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ModPack;
|
||||
@@ -5,8 +5,9 @@
|
||||
*/
|
||||
"use strict";
|
||||
const ModInfo = require("./ModInfo");
|
||||
const ModPack = require("./ModPack");
|
||||
|
||||
module.exports = {
|
||||
ModInfo,
|
||||
ModPack,
|
||||
};
|
||||
|
||||
@@ -710,6 +710,71 @@ messages.sendRcon = new Request({
|
||||
},
|
||||
});
|
||||
|
||||
messages.getModPack = new Request({
|
||||
type: "get_mod_pack",
|
||||
links: ["control-master", "instance-slave", "slave-master"],
|
||||
permission: "core.mod_pack.get",
|
||||
forwardTo: "master",
|
||||
requestProperties: {
|
||||
"id": { type: "integer" },
|
||||
},
|
||||
responseProperties: {
|
||||
"mod_pack": libData.ModPack.jsonSchema,
|
||||
},
|
||||
});
|
||||
|
||||
messages.listModPacks = new Request({
|
||||
type: "list_mod_packs",
|
||||
links: ["control-master"],
|
||||
permission: "core.mod_pack.list",
|
||||
responseProperties: {
|
||||
"list": {
|
||||
type: "array",
|
||||
items: libData.ModPack.jsonSchema,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
messages.createModPack = new Request({
|
||||
type: "create_mod_pack",
|
||||
links: ["control-master"],
|
||||
permission: "core.mod_pack.create",
|
||||
requestProperties: {
|
||||
"mod_pack": libData.ModPack.jsonSchema,
|
||||
},
|
||||
});
|
||||
|
||||
messages.updateModPack = new Request({
|
||||
type: "update_mod_pack",
|
||||
links: ["control-master"],
|
||||
permission: "core.mod_pack.update",
|
||||
requestProperties: {
|
||||
"mod_pack": libData.ModPack.jsonSchema,
|
||||
},
|
||||
});
|
||||
|
||||
messages.deleteModPack = new Request({
|
||||
type: "delete_mod_pack",
|
||||
links: ["control-master"],
|
||||
permission: "core.mod_pack.delete",
|
||||
requestProperties: {
|
||||
"id": { type: "integer" },
|
||||
},
|
||||
});
|
||||
|
||||
messages.setModPackSubscriptions = new Request({
|
||||
type: "set_mod_pack_subscriptions",
|
||||
links: ["control-master"],
|
||||
permission: "core.mod_pack.subscribe",
|
||||
requestProperties: {
|
||||
"all": { type: "boolean" },
|
||||
"mod_pack_ids": {
|
||||
type: "array",
|
||||
items: { type: "integer" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
messages.getMod = new Request({
|
||||
type: "get_mod",
|
||||
links: ["control-master"],
|
||||
@@ -1325,6 +1390,14 @@ messages.saveListUpdate = new Event({
|
||||
},
|
||||
});
|
||||
|
||||
messages.modPackUpdate = new Event({
|
||||
type: "mod_pack_update",
|
||||
links: ["master-control"],
|
||||
eventProperties: {
|
||||
"mod_pack": libData.ModPack.jsonSchema,
|
||||
},
|
||||
});
|
||||
|
||||
messages.modUpdate = new Event({
|
||||
type: "mod_update",
|
||||
links: ["master-control"],
|
||||
|
||||
@@ -493,6 +493,19 @@ class BaseMasterPlugin {
|
||||
*/
|
||||
async onPrepareSlaveDisconnect(connection) { }
|
||||
|
||||
/**
|
||||
* Called when a mod pack is updated
|
||||
*
|
||||
* Invoked when a mod pack has been added, updated or deleted in the
|
||||
* cluster.
|
||||
*
|
||||
* If the mod pack has been deleted its `.isDeleted` property will be
|
||||
* true.
|
||||
*
|
||||
* @param {module:lib/data/ModPack} modPack - Mod pack that updated.
|
||||
*/
|
||||
async onModPackUpdated(modPack) { }
|
||||
|
||||
/**
|
||||
* Called when a mod stored on the master server is updated
|
||||
*
|
||||
|
||||
@@ -511,6 +511,39 @@ definePermission({
|
||||
description: "Send arbitrary RCON commands to instances.",
|
||||
});
|
||||
|
||||
definePermission({
|
||||
name: "core.mod_pack.get",
|
||||
title: "Retrieve mod pack",
|
||||
description: "Get the details of a mod pack in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.list",
|
||||
title: "List mod packs",
|
||||
description: "Get the full list of a mod packs in the cluster.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.subscribe",
|
||||
title: "Subscribe to mod pack updates",
|
||||
description: "Subscribe to be notified on updates on the details of mod packs.",
|
||||
grantByDefault: true,
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.create",
|
||||
title: "Create mod pack",
|
||||
description: "Create new mod pack in the cluster.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.update",
|
||||
title: "Update mod pack",
|
||||
description: "Edit existing mod pack definition in the cluster.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod_pack.delete",
|
||||
title: "Delete mod pack",
|
||||
description: "Delete mod pack definition in the cluster.",
|
||||
});
|
||||
definePermission({
|
||||
name: "core.mod.get",
|
||||
title: "Get mods",
|
||||
|
||||
Reference in new issue
Block a user