Merge pull request #996 from bbassie/feature/instance-udp

Add UDP communication support for instances
This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-10-17 20:47:54 +01:00
commit ba800e853d
19 files changed
+708 -25

No files matched your search

+6 -1
View File
@@ -138,6 +138,10 @@ export function hasFactorio() {
return haveFactorioInstall;
}
export function getFactorioVersion() {
return latestFactorioVersion;
}
export async function get(urlPath) {
const url = new URL("https://localhost:4443");
url.pathname = urlPath;
@@ -170,6 +174,7 @@ let controllerProcess;
let hostProcess;
let control;
let haveFactorioInstall;
let latestFactorioVersion;
const baseHostConfig = loadJSON("config-host.json");
@@ -356,7 +361,7 @@ before(async function() {
controllerProcess = await spawnNode("controller:", "../../packages/controller run", /Started controller/);
const factorioVersions = (await _listFactorioVersions(factorioDir)).versions;
const latestFactorioVersion = [...factorioVersions.values()]
latestFactorioVersion = [...factorioVersions.values()]
.sort((a, b) => lib.integerFullVersion(b) - lib.integerFullVersion(a))[0];
haveFactorioInstall = factorioVersions.size > 0;
+60 -1
View File
@@ -1,4 +1,6 @@
import path from "node:path";
import dgram from "node:dgram";
import events from "node:events";
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import fs from "node:fs/promises";
@@ -6,7 +8,7 @@ import fs from "node:fs/promises";
import { slowTest, testMatrix } from "../common.js";
import {
execCtl, execCtlProcess, sendRcon, getControl,
requiresFactorio, hasFactorio, instancesDir,
requiresFactorio, hasFactorio, getFactorioVersion, instancesDir,
} from "./index.js";
const instId = 48;
@@ -21,6 +23,19 @@ const requireApi = [
"or package.loaded['__level__/modules/clusterio/api.lua']", // 2.0.0
].join(" ");
// Lua UDP needs Factorio 2.1.10, see docs/configuration.md
function hasLuaUdp() {
return lib.integerFullVersion(getFactorioVersion()) >= lib.integerFullVersion("2.1.10");
}
async function freeUdpPort() {
const socket = dgram.createSocket("udp4");
await events.once(socket.bind(0, "127.0.0.1"), "listening");
const port = socket.address().port;
socket.close();
return port;
}
function getUser(name) {
return getControl().send(new lib.UserGetRequest(name));
}
@@ -105,12 +120,16 @@ describe("Clusterio Instance", function() {
}
}
describe(`Integration - ${strSavePatching}, ${strScriptCommands}`, function() {
let luaUdpPort;
before(async function() {
this.timeout(20000);
// Create and start a patched save
await execCtl(`${instSetConfig} factorio.enable_whitelist true`);
await execCtl(`${instSetConfig} factorio.enable_save_patching ${savePatchingEnabled}`);
await execCtl(`${instSetConfig} factorio.enable_script_commands ${scriptCommandsEnabled}`);
await execCtl(`${instSetConfig} factorio.enable_lua_udp ${hasLuaUdp()}`);
luaUdpPort = await freeUdpPort();
await execCtl(`${instSetConfig} factorio.lua_udp_port ${luaUdpPort}`);
await execCtl(`instance save create ${instName}`);
await execCtl(`instance start ${instName}`);
});
@@ -125,6 +144,46 @@ describe("Clusterio Instance", function() {
assert.equal(res === "Disabled\n", scriptCommandsEnabled);
});
});
if (savePatchingEnabled) {
describe("Lua UDP", function() {
it("should drop send_udp before the module is initialised", async function() {
// Mods can call send_udp from on_init, before the first tick sets up script data
await allowSendingScriptCommands();
const res = await sendRcon(instId,
`/sc ${requireApi} local data = storage or global ` +
"local saved = data.clusterio data.clusterio = nil " +
"local module_ok = pcall(api.send_udp, 'early', 'data') " +
"local mod_ok, port = pcall(remote.call, 'clusterio_api', 'get_host_udp_port') " +
"data.clusterio = saved " +
"rcon.print(tostring(module_ok) .. ' ' .. tostring(mod_ok) .. ' ' .. tostring(port))"
);
assert.equal(res, "true true nil\n");
});
it("should dispatch packets sent to the game", async function() {
if (!hasLuaUdp()) {
this.skip();
}
await allowSendingScriptCommands();
await sendRcon(instId,
"/sc script.on_event(defines.events.on_udp_packet_received, " +
"function(event) storage.udp_test = event.payload end)"
);
const socket = dgram.createSocket("udp4");
let payload;
try {
for (let i = 0; i < 10 && payload !== "hello\n"; i++) {
socket.send("hello", luaUdpPort, "127.0.0.1");
await lib.wait(10);
// The server does not tick without players, commands make it tick
payload = await sendRcon(instId, "/sc rcon.print(tostring(storage.udp_test))");
}
} finally {
socket.close();
}
assert.equal(payload, "hello\n");
});
});
}
describe("Player events", function() {
if (savePatchingEnabled) {
// IPC expects save patching to be enabled
+52
View File
@@ -125,6 +125,8 @@ describe("Integration of host/src/server", function() {
let mapPath = server.writePath("saves", "test.zip");
await assert.doesNotReject(fs.access(mapPath), "save is missing");
// Lua UDP needs Factorio 2.1.10, see docs/configuration.md
server.enableLuaUdp = lib.integerFullVersion(server.version) >= lib.integerFullVersion("2.1.10");
await server.start("test.zip");
});
});
@@ -159,6 +161,56 @@ describe("Integration of host/src/server", function() {
});
});
describe("Lua UDP", function() {
it("emits udp events for packets sent from the game", async function() {
slowTest(this);
if (!server.enableLuaUdp) {
this.skip();
}
log("Lua UDP from game");
assert(server.hostUdpPort > 0, "host UDP socket is not open");
let waiter = events.once(server, "udp-test_channel");
await server.sendRcon(`/sc helpers.send_udp(${server.hostUdpPort}, "test_channel?hello", 0)`);
let [data] = await waiter;
assert.equal(data.toString(), "hello");
});
it("delivers packets sent with sendUdp to the game", async function() {
slowTest(this);
if (!server.enableLuaUdp) {
this.skip();
}
log("Lua UDP to game");
// Poll from on_tick like the clusterio module does
await server.sendRcon(
"/sc script.on_event(defines.events.on_udp_packet_received, " +
"function(event) print('udp:' .. event.payload) end) " +
"script.on_nth_tick(1, function() helpers.recv_udp(0) end)"
);
let pass = false;
function filter(output) {
if (output.message === "udp:hello") {
pass = true;
}
}
server.on("output", filter);
await server.sendUdp("hello");
// The server does not tick without players, commands make it tick
for (let i = 0; i < 10; i++) {
await server.sendRcon("/sc rcon.print('tick')");
await lib.wait(10);
if (pass) {
break;
}
}
server.off("output", filter);
assert(pass, "server did not output the packet payload");
});
});
describe(".stop()", function() {
it("stops the server", async function() {
slowTest(this);