Merge pull request #1228 from bbassie/feature/arm-headless

Support the ARM64 Linux headless build of Factorio
This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-11-18 13:33:28 +00:00
commit b8e96989af
7 files changed
+222 -16

No files matched your search

+40 -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,15 @@ 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";
}
// Factorio downloads in progress by target directory, shared by every server in the process
const downloadsInProgress = new Map<string, Promise<void>>();
@@ -202,14 +211,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 => process.platform !== "linux" || lib.hasFactorioHeadless(v.version, "linux", arch))
.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 +264,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, "linux", arch), dst).finally(() => {
downloadsInProgress.delete(dst);
});
downloadsInProgress.set(dst, download);
@@ -1734,8 +1756,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 +1768,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");
}
/**
+52 -2
View File
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js";
import { FullVersion, FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js";
const ARCHIVE_URL = "https://factorio.com/download/archive/";
@@ -9,11 +9,61 @@ const ARCHIVE_URL = "https://factorio.com/download/archive/";
export const ExternalFactorioVersionSchema = Type.Object({
stable: Type.Boolean(),
version: FullVersionSchema,
/** @deprecated Linux x64 only, use {@link factorioHeadlessUrl} instead. */
headlessUrl: Type.String(),
});
export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchema>;
/** Platforms Factorio may publish headless builds for, named as in Node's process.platform */
export type FactorioHeadlessPlatform = "linux" | "win32";
/** CPU architectures Factorio may publish headless builds for, named as in Node's process.arch */
export type FactorioHeadlessArch = "x64" | "arm64";
/** First Factorio version with an ARM64 Linux headless build */
export const minimumArm64HeadlessVersion = "2.1.18";
/**
* Check if Factorio publishes a headless build of a version
*
* @param version - Factorio version to check.
* @param platform - Platform of the build.
* @param arch - CPU architecture of the build.
* @returns true if the build can be downloaded.
*/
export function hasFactorioHeadless(
version: FullVersion,
platform: FactorioHeadlessPlatform,
arch: FactorioHeadlessArch,
) {
if (platform !== "linux") {
return false;
}
return arch === "x64" || integerFullVersion(version) >= integerFullVersion(minimumArm64HeadlessVersion);
}
/**
* Download URL of the headless build of a Factorio version
*
* @param version - Factorio version to download.
* @param platform - Platform of the build.
* @param arch - CPU architecture of the build.
* @returns URL redirecting to the archive.
* @throws Error if Factorio has no headless build for the platform and arch.
*/
export function factorioHeadlessUrl(
version: FullVersion,
platform: FactorioHeadlessPlatform,
arch: FactorioHeadlessArch,
) {
if (!hasFactorioHeadless(version, platform, arch)) {
throw new Error(`Factorio ${version} has no headless build for ${platform} ${arch}`);
}
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 +110,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, "linux", "x64"),
});
}
}