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

+2
View File
@@ -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
+12 -6
View File
@@ -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
+2 -1
View File
@@ -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) => {
+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,
+1 -1
View File
@@ -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() {
+65 -4
View File
@@ -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() {