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

+1
View File
@@ -1,4 +1,5 @@
export { default as libConfig } from "./config";
export { default as libData } from "./data";
export { default as libErrors } from "./errors";
export { default as libFactorio } from "./factorio/exchange_string";
export { default as libHelpers } from "./helpers";
+7
View File
@@ -19,6 +19,13 @@ MasterGroup.define({
type: "string",
initial_value: "Your Cluster",
});
MasterGroup.define({
name: "mods_directory",
title: "Mods Directory",
description: "Path to directory where mods shared with the cluster are stored.",
type: "string",
initial_value: "mods",
});
MasterGroup.define({
name: "database_directory",
title: "Database directory",
+230
View File
@@ -0,0 +1,230 @@
"use strict";
const fs = require("fs-extra");
const JSZip = require("jszip");
const path = require("path");
const libHash = require("../hash");
const libSchema = require("../schema");
const { findRoot } = require("../zip_ops");
/**
* Info about a mod available on the master server.
*
* See https://wiki.factorio.com/Tutorial:Mod_structure#info.json for
* details of the invidiual fields sourced from info.json.
* @alias module:lib/data/ModInfo
*/
class ModInfo {
/**
* Internal name of this mod.
* This is the name of the zip file as well as the name that appears in
* info.json.
* @type {string}
*/
name = "";
/**
* Version of the mod.
* Sourced from info.json.
* @type {string}
*/
version = "";
/**
* Integer representation of the version
* @type {number}
*/
get integerVersion() {
const [major, minor, sub] = this.version.split(".").map(n => Number.parseInt(n, 10));
return major * 0x100000000 + minor * 0x10000 + sub; // Can't use bitwise here because this is 48-bits.
}
/**
* Display name of the mod.
* Sourced from info.json.
* @type {string}
*/
title = "";
/**
* Author of the mod.
* Sourced from info.json.
* @type {string}
*/
author = "";
/**
* Contact field for mod.
* Sourced from info.json.
* @type {string}
*/
contact = "";
/**
* Homepage of the mod.
* Sourced from info.json.
* @type {string}
*/
homepage = "";
/**
* Description of the mod.
* Sourced from info.json.
* @type {string}
*/
description = "";
/**
* Major version of Factorio this mod supports.
* Sourced from info.json.
* @type {string}
*/
factorioVersion = "0.12";
/**
* Integer representation of the factorioVersion
* @type {number}
*/
get integerFactorioVersion() {
const [major, minor] = this.factorioVersion.split(".").map(n => Number.parseInt(n, 10));
return major * 0x100000000 + minor * 0x10000; // Can't use bitwise here because this is 48-bits.
}
/**
* Dependiences for this mod.
* Sourced from info.json.
* @type {Array<string>}
*/
dependencies = ["base"];
/**
* Name of the file containing this mod.
* @type {number}
*/
filename = "";
/**
* Size of the mod in bytes
* @type {number}
*/
size = 0;
/**
* Hash of this mod
* @type {string=}
*/
hash;
/**
* True if this mod has been deleted
* @type {boolean}
*/
isDeleted = false;
// Content of info.json found in mod files
static infoJsonSchema = {
type: "object",
required: ["name", "version", "title", "author"],
properties: {
"name": { type: "string" },
"version": { type: "string" },
"title": { type: "string" },
"author": { type: "string" },
"contact": { type: "string" },
"homepage": { type: "string" },
"description": { type: "string" },
"factorio_version": { type: "string" },
"dependencies": { type: "array", items: { type: "string" } },
},
};
static validateInfo = libSchema.compile(this.infoJsonSchema);
static jsonSchema = {
type: "object",
properties: {
...this.infoJsonSchema.properties,
"filename": { type: "string" },
"size": { type: "integer" },
"hash": { type: "string" },
"is_deleted": { type: "boolean" },
},
};
static validate = libSchema.compile(this.jsonSchema);
constructor(json = {}) {
// info.json fields
if (json.name) { this.name = json.name; }
if (json.version) { this.version = json.version; }
if (json.title) { this.title = json.title; }
if (json.author) { this.author = json.author; }
if (json.contact) { this.contact = json.contact; }
if (json.homepage) { this.homepage = json.homepage; }
if (json.description) { this.description = json.description; }
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
if (json.dependencies) { this.dependencies = json.dependencies; }
// Additional data
if (json.filename) { this.filename = json.filename; }
if (json.size) { this.size = json.size; }
if (json.hash) { this.hash = json.hash; }
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
}
toJSON() {
let json = {
name: this.name,
version: this.version,
title: this.title,
author: this.author,
};
if (this.contact) { json.contact = this.contact; }
if (this.homepage) { json.homepage = this.homepage; }
if (this.description) { json.description = this.description; }
if (this.factorio_version !== "0.12") { json.factorio_version = this.factorioVersion; }
if (this.dependencies.length !== 1 || this.dependencies[0] !== "base") {
json.dependencies = this.dependencies;
}
if (this.filename) { json.filename = this.filename; }
if (this.size) { json.size = this.size; }
if (this.hash) { json.hash = this.hash; }
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
return json;
}
static async fromModFile(modPath) {
let modInfo;
{
// XXX: JSZip needs the whole archive loaded in memory to work.
// This is clearly untenable and will be replaced later.
let zip = await JSZip.loadAsync(await fs.readFile(modPath));
let root = zip.folder(findRoot(zip));
let infoFile = root.file("info.json");
if (!infoFile) {
throw new Error("Mod contains no info.json file");
}
modInfo = JSON.parse(await infoFile.async("string"));
}
let valid = this.validateInfo(modInfo);
if (!valid) {
throw new Error("Mod's info.json is not valid");
}
let hash = `sha1:${await libHash.hashFile(modPath)}`;
return new this({
...modInfo,
filename: path.basename(modPath),
size: (await fs.stat(modPath)).size,
hash,
is_deleted: false,
});
}
}
module.exports = ModInfo;
+12
View File
@@ -0,0 +1,12 @@
/**
* Shared data types used in Clusterio
* @module lib/data
* @author Hornwitser
*/
"use strict";
const ModInfo = require("./ModInfo");
module.exports = {
ModInfo,
};
+2
View File
@@ -2,6 +2,7 @@
const libBuildMod = require("./build_mod");
const libCommand = require("./command");
const libConfig = require("./config");
const libData = require("./data");
const libDatabase = require("./database");
const libErrors = require("./errors");
const libFactorio = require("./factorio");
@@ -28,6 +29,7 @@ module.exports = {
libBuildMod,
libCommand,
libConfig,
libData,
libDatabase,
libErrors,
libFactorio,
+71 -1
View File
@@ -1,8 +1,9 @@
// Message definitions for links
"use strict";
const libSchema = require("../schema");
const libData = require("../data");
const libErrors = require("../errors");
const { logger } = require("../logging");
const libSchema = require("../schema");
const PlayerStats = require("../PlayerStats");
class MissingLinkHandlerError extends Error {
@@ -709,6 +710,67 @@ messages.sendRcon = new Request({
},
});
messages.getMod = new Request({
type: "get_mod",
links: ["control-master"],
permission: "core.mod.get",
requestProperties: {
"name": { type: "string" },
"version": { type: "string" },
},
responseProperties: {
"mod": libData.ModInfo.jsonSchema,
},
});
messages.listMods = new Request({
type: "list_mods",
links: ["control-master"],
permission: "core.mod.list",
responseProperties: {
"list": {
type: "array",
items: libData.ModInfo.jsonSchema,
},
},
});
messages.setModSubscriptions = new Request({
type: "set_mod_subscriptions",
links: ["control-master"],
permission: "core.mod.subscribe",
requestProperties: {
"all": { type: "boolean" },
"mod_names": {
type: "array",
items: { type: "string" },
},
},
});
messages.downloadMod = new Request({
type: "download_mod",
links: ["control-master"],
permission: "core.mod.download",
requestProperties: {
"name": { type: "string" },
"version": { type: "string" },
},
responseProperties: {
"stream_id": { type: "string" },
},
});
messages.deleteMod = new Request({
type: "delete_mod",
links: ["control-master"],
permission: "core.mod.delete",
requestProperties: {
"name": { type: "string" },
"version": { type: "string" },
},
});
messages.listPermissions = new Request({
type: "list_permissions",
links: ["control-master"],
@@ -1263,6 +1325,14 @@ messages.saveListUpdate = new Event({
},
});
messages.modUpdate = new Event({
type: "mod_update",
links: ["master-control"],
eventProperties: {
"mod": libData.ModInfo.jsonSchema,
},
});
messages.userUpdate = new Event({
type: "user_update",
links: ["master-control"],
+12 -1
View File
@@ -479,7 +479,6 @@ class BaseMasterPlugin {
*/
onControlConnectionEvent(connection, event) { }
/**
* Called when a slave is preparing to disconnect from the master
*
@@ -494,6 +493,18 @@ class BaseMasterPlugin {
*/
async onPrepareSlaveDisconnect(connection) { }
/**
* Called when a mod stored on the master server is updated
*
* Invoked when a mod has been added, updated or deleted from the pool
* of shared mods stored on the cluster.
*
* If a mod has been deleted its `.isDeleted` property will be true.
*
* @param {module:lib/data/ModInfo} mod - Mod that updated.
*/
async onModUpdated(mod) { }
/**
* Called when a player joins or leaves an instance
*
+35
View File
@@ -511,6 +511,41 @@ definePermission({
description: "Send arbitrary RCON commands to instances.",
});
definePermission({
name: "core.mod.get",
title: "Get mods",
description: "Get the details of a mod stored in the cluster.",
grantByDefault: true,
});
definePermission({
name: "core.mod.list",
title: "List mods",
description: "Get the full list of mods stored on the master",
grantByDefault: true,
});
definePermission({
name: "core.mod.subscribe",
title: "Subscribe to mod updates",
description: "Subscribe to be notified on updates on the details of mods.",
grantByDefault: true,
});
definePermission({
name: "core.mod.upload",
title: "Upload mod",
description: "Upload mods to the master.",
});
definePermission({
name: "core.mod.download",
title: "Download mod",
description: "Download mods stored on the master.",
grantByDefault: true,
});
definePermission({
name: "core.mod.delete",
title: "Delete mod",
description: "Delete mods stored on the master.",
});
definePermission({
name: "core.permission.list",
title: "List permissions",