Add support for multiple Factorio installs

Allow the factorio_directory specified in the slave config to contain
multiple Factorio installs and add factior.version instance option to
select which install to use for the given instance.
This commit is contained in:
Hornwitser committed 2020-04-03 20:51:28 +02:00
1 parent 4c26fcee3a
commit ae86781b0d
7 files changed
+186 -20

No files matched your search

+6
View File
@@ -165,6 +165,12 @@ InstanceGroup.finalize();
class FactorioGroup extends classes.ConfigGroup { }
FactorioGroup.groupName = "factorio";
FactorioGroup.define({
name: "version",
description: "Version of the game to run, use latest to run the latest installed version",
type: "string",
initial_value: "latest",
});
FactorioGroup.define({
name: "game_port",
description: "UDP port to run game on, uses a random port if null",
+98 -8
View File
@@ -22,7 +22,16 @@ const errors = require("lib/errors");
* reading the changelog.txt in it.
*/
async function getVersion(changelogPath) {
let changelog = await fs.readFile(changelogPath, "utf-8");
let changelog;
try {
changelog = await fs.readFile(changelogPath, "utf-8");
} catch (err) {
if (err.code === "ENOENT") {
return null;
}
throw err;
}
for (let line of changelog.split(/[\r\n]+/)) {
let index = line.indexOf(":");
if (index !== -1) {
@@ -33,7 +42,80 @@ async function getVersion(changelogPath) {
}
}
throw new Error("Unable to determine the version of Factorio");
return null;
}
/**
* Comparison function for sorting Factorio version strings
*/
function versionOrder(a, b) {
let aParts = a.split(".").map(s => Number.parseInt(s, 10));
let bParts = b.split(".").map(s => Number.parseInt(s, 10));
for (let i = 0; i < aParts.length; i++) {
if (aParts[i] === bParts[i]) {
continue;
} else if (aParts[i] < bParts[i]) {
return 1;
} else {
return -1;
}
}
return 0;
}
/**
* Find factorio data directory of the given version
*
* Searches the given factorio dir for an installation of Factorio of
* the target version.
*/
async function findVersion(factorioDir, targetVersion) {
// There are two supported setups: having the factorio dir be the actual
// install directory, and having the factorio dir be a folder containing
// multiple install directories
let simpleVersion = await getVersion(path.join(factorioDir, "data", "changelog.txt"));
if (simpleVersion !== null) {
if (simpleVersion === targetVersion || targetVersion === "latest") {
return [path.join(factorioDir, "data"), simpleVersion];
}
throw new Error(
`Factorio version ${targetVersion} was requested, but install directory contains ${simpleVersion}`
);
}
let versions = new Map();
for (let entry of await fs.readdir(factorioDir, { withFileTypes: true })) {
if (!entry.isDirectory()) {
continue;
}
let version = await getVersion(path.join(factorioDir, entry.name, "data", "changelog.txt"));
if (version === null) {
continue;
}
if (version === targetVersion) {
return [path.join(factorioDir, entry.name, "data"), version];
}
versions.set(version, entry.name);
}
if (!versions.size) {
throw new Error(`Unable to find any Factorio install in ${factorioDir}`);
}
if (targetVersion === "latest") {
let latest = [...versions.keys()].sort(versionOrder)[0];
return [path.join(factorioDir, versions.get(latest), "data"), latest];
}
throw new Error(`Unable to find Factorio version ${targetVersion}`);
}
/**
@@ -343,16 +425,21 @@ class FactorioServer extends events.EventEmitter {
/**
* Create a Factorio server interface
*
* @param {string} dataDir - Data directory of the Factorio install.
* @param {string} factorioDir - Directory of the Factorio install(s).
* @param {string} writeDir - Directory to write runtime data to.
* @param {object} options - Optional parameters.
*/
constructor(dataDir, writeDir, options) {
constructor(factorioDir, writeDir, options) {
super();
this._dataDir = dataDir;
this._factorioDir = factorioDir;
this._writeDir = writeDir;
// Resolved in init
this._version = null;
this._dataDir = null;
this._targetVersion = options.version || "latest";
this.gamePort = options.gamePort || randomDynamicPort();
this.rconPort = options.rconPort || randomDynamicPort();
this.rconPassword = options.rconPassword;
@@ -489,7 +576,7 @@ class FactorioServer extends events.EventEmitter {
*/
async init() {
this._check(["new"]);
this._version = await getVersion(this.dataPath("changelog.txt"));
[this._dataDir, this._version] = await findVersion(this._factorioDir, this._targetVersion);
this.rconPassword = this.rconPassword || await generatePassword(10);
this._state = "init";
}
@@ -788,7 +875,8 @@ class FactorioServer extends events.EventEmitter {
* Return path in data directory
*
* Creates a path using path.join with the given parts that's relative to
* the data directory of the Factorio server.
* the data directory of the Factorio server. Not valid before init has
* been called.
*/
dataPath(...parts) {
return path.join(this._dataDir, ...parts);
@@ -811,7 +899,7 @@ class FactorioServer extends events.EventEmitter {
*
* @returns {object} the parsed server-settings.
*/
async exampleSettings(dataDir) {
async exampleSettings() {
return JSON.parse(await fs.readFile(this.dataPath("server-settings.example.json"), "utf-8"));
}
@@ -832,6 +920,8 @@ module.exports = {
// For testing only
_getVersion: getVersion,
_versionOrder: versionOrder,
_findVersion: findVersion,
_randomDynamicPort: randomDynamicPort,
_generatePassword: generatePassword,
_LineSplitter: LineSplitter,