Support the ARM64 Linux headless build of Factorio

Factorio publishes a linux-arm64 headless build from 2.1.18. On ARM64
Linux the host now downloads that build, refuses versions older than
2.1.18 that are not already installed, and finds the binary in bin/arm64
or bin/x64, whichever is present.

The host builds the download URL from the version instead of using
headlessUrl from the controller, so the version list on the wire stays
the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-18 11:14:54 +00:00
1 parent 35f9609865
commit 5aaea1f213
7 files changed
+167 -15

No files matched your search

+44 -9
View File
@@ -1,6 +1,6 @@
// Factorio Server interface
import fs from "node:fs/promises";
import { type FSWatcher, createWriteStream, watch as fsWatch, writeFileSync } from "node:fs";
import { type FSWatcher, createWriteStream, existsSync, watch as fsWatch, writeFileSync } from "node:fs";
import child_process from "child_process";
import dgram from "dgram";
import path from "path";
@@ -182,6 +182,19 @@ async function downloadAndExtractTar(url: string, targetDir: string) {
}
}
/**
* Factorio Linux build architecture matching this host
*
* @returns "arm64" on ARM64 Linux, otherwise "x64".
*/
function factorioArch(): lib.FactorioHeadlessArch {
return process.platform === "linux" && process.arch === "arm64" ? "arm64" : "x64";
}
function hasArm64Headless(version: lib.FullVersion) {
return lib.integerFullVersion(version) >= lib.integerFullVersion(lib.minimumArm64HeadlessVersion);
}
// Factorio downloads in progress by target directory, shared by every server in the process
const downloadsInProgress = new Map<string, Promise<void>>();
@@ -202,14 +215,27 @@ export async function checkForUpdates(
logger: lib.Logger = lib.logger,
) {
const latestVersion = availableVersions
.filter(v => (
targetVersion === "latest" || v.version === targetVersion || v.version.startsWith(targetVersion)
))
const matchesTarget = (version: string) => (
targetVersion === "latest" || version === targetVersion || version.startsWith(targetVersion)
);
const arch = factorioArch();
const matching = availableVersions.filter(v => matchesTarget(v.version));
const latestVersion = matching
.filter(v => arch !== "arm64" || hasArm64Headless(v.version))
.sort((v1, v2) => versionOrder(v1.version, v2.version))
[0];
if (!latestVersion) {
if (matching.length && arch === "arm64" && process.platform === "linux") {
const installed = await listFactorioVersions(factorioDir);
if (!installed.direct && ![...installed.versions].some(matchesTarget)) {
throw new Error(
`Factorio ${targetVersion} has no ARM64 Linux headless build to download, ` +
`ARM64 builds start at ${lib.minimumArm64HeadlessVersion}. Install it manually ` +
`or use version ${lib.minimumArm64HeadlessVersion} or later.`
);
}
}
return;
}
@@ -242,7 +268,7 @@ export async function checkForUpdates(
logger.info(`A newer version of factorio is available (${v}), waiting for download...`);
} else {
logger.info(`A newer version of factorio is available (${v}), starting download...`);
download = downloadAndExtractTar(latestVersion.headlessUrl, dst).finally(() => {
download = downloadAndExtractTar(lib.factorioHeadlessUrl(v, arch), dst).finally(() => {
downloadsInProgress.delete(dst);
});
downloadsInProgress.set(dst, download);
@@ -1734,8 +1760,8 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
/**
* Get Factorio binary path
*
* Get the path to the factorio binary depending on the configuration
* and or the platform (MacOS support)
* Get the path to the factorio binary depending on the configuration,
* the platform (MacOS support) and the bin/x64 or bin/arm64 dir present
*
* @returns Path to factorio binary
*/
@@ -1746,7 +1772,16 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
if (process.platform === "darwin") {
return this.dataPath("..", "MacOS", "factorio");
}
return this.dataPath("..", "bin", "x64", "factorio");
// Prefer the native build, fall back to whichever one is installed
const arches = factorioArch() === "arm64" ? ["arm64", "x64"] : ["x64", "arm64"];
for (const arch of arches) {
const binary = this.dataPath("..", "bin", arch, "factorio");
// eslint-disable-next-line node/no-sync
if (existsSync(binary)) {
return binary;
}
}
return this.dataPath("..", "bin", arches[0], "factorio");
}
/**
+19 -1
View File
@@ -14,6 +14,24 @@ export const ExternalFactorioVersionSchema = Type.Object({
export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchema>;
/** CPU architectures Factorio publishes Linux headless builds for */
export type FactorioHeadlessArch = "x64" | "arm64";
/** First Factorio version with an ARM64 Linux headless build */
export const minimumArm64HeadlessVersion = "2.1.18";
/**
* Download URL of the Linux headless build of a Factorio version
*
* @param version - Factorio version to download.
* @param arch - CPU architecture of the build.
* @returns URL redirecting to the tarball.
*/
export function factorioHeadlessUrl(version: string, arch: FactorioHeadlessArch = "x64") {
const build = arch === "arm64" ? "linux-arm64" : "linux64";
return `https://www.factorio.com/get-download/${version}/headless/${build}`;
}
/**
* Fetch all factorio versions that support headless
*
@@ -60,7 +78,7 @@ function parseFactorioVersions(html: string): ExternalFactorioVersion[] {
versions.set(version, {
version: version,
stable: kind === "stable",
headlessUrl: `https://www.factorio.com/get-download/${version}/headless/linux64`,
headlessUrl: factorioHeadlessUrl(version),
});
}
}