mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
25c3c9991e
commit
0a6f614074
3 files changed
+64
-5
No files matched your search
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user