From ae86781b0dbfe9a782e28d555be974f15ba20982 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 3 Apr 2020 13:40:48 +0200 Subject: [PATCH] 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. --- CHANGELOG.md | 2 + README.md | 18 ++++-- client.js | 3 +- lib/config/definitions.js | 6 ++ lib/factorio/server.js | 106 +++++++++++++++++++++++++++++++++--- test/integration/server.js | 2 +- test/lib/factorio/server.js | 69 +++++++++++++++++++++-- 7 files changed, 186 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53365239..16d7a559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ Version 2.0.0 - Added export of pollution statitics. - Reconnection logic that esures no data is dropped talking to the master server provided the session can be resumed. +- Added support for having multiple Factorio installs and selecting which + version to use on a per instance basis. ### Changes diff --git a/README.md b/README.md index 1ab89f2a..c7a96caf 100644 --- a/README.md +++ b/README.md @@ -167,19 +167,25 @@ reboot when you are done, then proceed to the next steps. *reboots matter* - Via the stand alone version on from their website - 1. Download the MS Windows (64-bit zip package) from https://www.factorio.com/download + 1. Create a new folder named "factorio" in the the factorioClusterio + folder. - 2. Open the zip file and drag the folder called "Factorio_0.17.x" into the factorioClusterio folder + 2. Download the MS Windows (64-bit zip package) from + https://www.factorio.com/download . - 3. Rename the folder to "factorio" + 3. Open the zip file and drag the folder called "Factorio_0.x.y" into + the factorio folder created in step 1. - Via steam installation - 1. Locate the game files by right clicking the game in steam, selecting properties, then Local Files, then Browse local files. + 1. Create a new folder named "factorio" in the the factorioClusterio + folder. - 2. Go to the parent folder of the folder that Steam opened and copy the Factorio folder into the factorioClusterio folder + 2. Locate the game files by right clicking the game in steam, + selecting properties, then Local Files, then Browse local files. - 3. Rename the folder to "factorio" + 3. Go to the parent folder of the folder that Steam opened and copy + the Factorio folder into the factorio folder created in step 1. ## Running Clusterio diff --git a/client.js b/client.js index 91b960ab..1dd53343 100644 --- a/client.js +++ b/client.js @@ -29,13 +29,14 @@ class Instance extends link.Link{ this.config = instanceConfig; let serverOptions = { + version: this.config.get("factorio.version"), gamePort: this.config.get('factorio.game_port'), rconPort: this.config.get('factorio.rcon_port'), rconPassword: this.config.get('factorio.rcon_password'), }; this.server = new factorio.FactorioServer( - path.join(factorioDir, "data"), this._dir, serverOptions + factorioDir, this._dir, serverOptions ); this.server.on('output', (output) => { diff --git a/lib/config/definitions.js b/lib/config/definitions.js index b52c7603..576c7085 100644 --- a/lib/config/definitions.js +++ b/lib/config/definitions.js @@ -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", diff --git a/lib/factorio/server.js b/lib/factorio/server.js index 27da529d..028ee217 100644 --- a/lib/factorio/server.js +++ b/lib/factorio/server.js @@ -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, diff --git a/test/integration/server.js b/test/integration/server.js index 10d400b4..3952bf0c 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -21,7 +21,7 @@ describe("Integration of lib/factorio/server", function() { describe("class FactorioServer", function() { let writePath = path.join("test", "temp", "integration"); - let server = new factorio.FactorioServer(path.join("factorio", "data"), writePath, {}); + let server = new factorio.FactorioServer("factorio", writePath, {}); let logFile; before(async function() { diff --git a/test/lib/factorio/server.js b/test/lib/factorio/server.js index f891e835..e1b400e1 100644 --- a/test/lib/factorio/server.js +++ b/test/lib/factorio/server.js @@ -13,10 +13,71 @@ describe("lib/factorio/server", function() { let version = await factorio._getVersion(path.join("test", "file", "changelog-test.txt")); assert.equal(version, "0.1.1"); }); - it("should throw if unable to find the version", async function() { + it("should return null if unable to find the version", async function() { + let version = await factorio._getVersion(path.join("test", "file", "changelog-bad.txt")); + assert.equal(version, null); + }); + it("should return null if file does not exist", async function() { + let version = await factorio._getVersion(path.join("test", "file", "does-not-exist.txt")); + assert.equal(version, null); + }); + }); + + describe("_versionOrder()", function() { + it("should sort an array of versions", function() { + let versions = ["1.2.3", "0.1.4", "0.1.2", "1.2.3", "0.1.5", "1.10.2"]; + versions.sort(factorio._versionOrder); + assert.deepEqual( + versions, + ["1.10.2", "1.2.3", "1.2.3", "0.1.5", "0.1.4", "0.1.2"] + ); + }); + }); + + describe("_findVersion()", function() { + it("should find a given install dir with latest as target", async function() { + let installDir = path.join("test", "file", "factorio"); + let [dir, version] = await factorio._findVersion(installDir, "latest"); + assert.equal(dir, path.join(installDir, "data")); + assert.equal(version, "0.1.1"); + }); + it("should find a given install dir with correct version as target", async function() { + let installDir = path.join("test", "file", "factorio"); + let [dir, version] = await factorio._findVersion(installDir, "0.1.1"); + assert.equal(dir, path.join(installDir, "data")); + assert.equal(version, "0.1.1"); + }); + it("should reject if the install dir version does not match target version", async function() { + let installDir = path.join("test", "file", "factorio"); await assert.rejects( - factorio._getVersion(path.join("test", "file", "changelog-bad.txt")), - new Error("Unable to determine the version of Factorio") + factorio._findVersion(installDir, "0.1.2"), + new Error("Factorio version 0.1.2 was requested, but install directory contains 0.1.1") + ); + }); + it("should search given directory for latest Factorio install", async function() { + let installDir = path.join("test", "file"); + let [dir, version] = await factorio._findVersion(installDir, "latest"); + assert.equal(dir, path.join(installDir, "factorio", "data")); + assert.equal(version, "0.1.1"); + }); + it("should search given directory for given Factorio install", async function() { + let installDir = path.join("test", "file"); + let [dir, version] = await factorio._findVersion(installDir, "0.1.1"); + assert.equal(dir, path.join(installDir, "factorio", "data")); + assert.equal(version, "0.1.1"); + }); + it("should reject if no factorio install with the given version was found", async function() { + let installDir = path.join("test", "file"); + await assert.rejects( + factorio._findVersion(installDir, "0.1.2"), + new Error("Unable to find Factorio version 0.1.2") + ); + }); + it("should reject if no factorio install was found", async function() { + let installDir = path.join("test", "file", "instances"); + await assert.rejects( + factorio._findVersion(installDir, "latest"), + new Error(`Unable to find any Factorio install in ${installDir}`) ); }); }); @@ -106,7 +167,7 @@ describe("lib/factorio/server", function() { describe("class FactorioServer", function() { let writePath = path.join("test", "temp", "should_not_exist"); - let server = new factorio.FactorioServer(path.join("test", "file", "factorio", "data"), writePath, {}); + let server = new factorio.FactorioServer(path.join("test", "file", "factorio"), writePath, {}); describe(".init()", function() { it("should not throw on first call", async function() {