From 0a6f614074f9c3812297eb5e0f0635ad390519e4 Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Sat, 17 Oct 2026 10:29:35 +0000 Subject: [PATCH] Fix Lua UDP before startup and without script commands send_udp and get_host_udp_port indexed script data that is only created on the first tick after patching, so calling them from on_init stopped the server. They now return nothing until then. Received packets were only polled once the host port was known, which needs script commands. Poll whenever recv_udp exists instead, it does nothing when the server runs without --enable-lua-udp. Co-Authored-By: Claude Opus 5.5 --- packages/host/modules/clusterio/api.lua | 3 +- packages/host/modules/clusterio/impl.lua | 13 ++++-- test/integration/instance.js | 53 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/packages/host/modules/clusterio/api.lua b/packages/host/modules/clusterio/api.lua index ec42588c..36ab00cc 100644 --- a/packages/host/modules/clusterio/api.lua +++ b/packages/host/modules/clusterio/api.lua @@ -44,7 +44,8 @@ function api.send_json(channel, data) end function api.send_udp(channel, data) - local port = compat.script_data.clusterio.host_udp_port + local clusterio = compat.script_data.clusterio + local port = clusterio and clusterio.host_udp_port if not port then return -- Lua UDP is not enabled for this instance end diff --git a/packages/host/modules/clusterio/impl.lua b/packages/host/modules/clusterio/impl.lua index 202058f6..344a1754 100644 --- a/packages/host/modules/clusterio/impl.lua +++ b/packages/host/modules/clusterio/impl.lua @@ -12,14 +12,17 @@ local function check_patch() end end +-- Added in Factorio 2.0.59, indexing a missing key on a LuaObject errors +local has_recv_udp = helpers ~= nil and pcall(function() return helpers.recv_udp end) + local impl = {} impl.events = {} impl.events[defines.events.on_tick] = function() check_patch() - -- Dispatch on_udp_packet_received for packets sent by the host - local clusterio = compat.script_data.clusterio - if clusterio and clusterio.host_udp_port then + -- Dispatch on_udp_packet_received for packets sent by the host, this is + -- a no-op when the server was started without --enable-lua-udp + if has_recv_udp then helpers.recv_udp(0) end end @@ -115,7 +118,9 @@ remote.add_interface('clusterio_api', { end, get_host_udp_port = function() - return compat.script_data.clusterio.host_udp_port + -- Not initialised until the first tick after the save is patched + local clusterio = compat.script_data.clusterio + return clusterio and clusterio.host_udp_port end, get_file_no = function() diff --git a/test/integration/instance.js b/test/integration/instance.js index af77db8f..ff856717 100644 --- a/test/integration/instance.js +++ b/test/integration/instance.js @@ -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"; @@ -26,6 +28,14 @@ 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)); } @@ -110,6 +120,7 @@ describe("Clusterio Instance", function() { } } describe(`Integration - ${strSavePatching}, ${strScriptCommands}`, function() { + let luaUdpPort; before(async function() { this.timeout(20000); // Create and start a patched save @@ -117,6 +128,8 @@ describe("Clusterio Instance", function() { 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}`); }); @@ -131,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