Add UDP communication support for instances

Add opt-in UDP messaging between the host and the Factorio server using
the --enable-lua-udp option of Factorio 2.0.59+.  When
factorio.enable_lua_udp is set the instance binds a localhost UDP socket,
starts Factorio with --enable-lua-udp on factorio.lua_udp_port (random
if null, like the RCON port) and tells the clusterio module the host port
via clusterio_private.update_instance.

Data from Factorio is sent with clusterio_api.send_udp(channel, data) in
the same IPC format as send_json, so plugins receive it through the
existing ipc-<channel> events.  Packets not sent from the Lua UDP port
are ignored, as that is the only unauthenticated way into the IPC
handler.  Data to Factorio is sent with the new sendUdp method on
instances and instance plugins, and is dispatched in-game as
on_udp_packet_received events by polling helpers.recv_udp every tick
from the clusterio module.

Plugins requiring UDP declare the new LuaUdp feature flag, which like
SavePatching and ScriptCommands refuses to load the plugin on instances
without the option enabled.

Verified against a headless 2.1.17 server: send_udp and recv_udp are
silent no-ops when the option is off, so a stale port in a save that is
moved to an instance without UDP does no harm.  The integration test
matrix now starts the instance with UDP enabled on Factorio 2.0.59+ and
round trips a player event over it.

Closes #969

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Fable 5.1 committed 2026-10-15 16:38:28 +00:00
1 parent ec51adc71c
commit 788ae17657
15 files changed
+394 -17

No files matched your search

+26 -1
View File
@@ -482,6 +482,8 @@ export interface InstanceConfigFields {
"factorio.host_assigned_game_port": number | null;
"factorio.rcon_port": number | null;
"factorio.rcon_password": string | null;
"factorio.enable_lua_udp": boolean;
"factorio.lua_udp_port": number | null;
"factorio.player_online_autosave_slots": number;
"factorio.mod_pack_id": number | null;
"factorio.enable_save_patching": boolean;
@@ -606,6 +608,21 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
type: "string",
optional: true,
},
"factorio.enable_lua_udp": {
description:
"Enable UDP messaging between the Factorio server and the host. " +
"Required by plugins using UDP. Requires Factorio 2.0.59 or later.",
restartRequired: true, // Because of the cli option "--enable-lua-udp"
type: "boolean",
initialValue: false,
},
"factorio.lua_udp_port": {
description: "UDP port for the Factorio server to receive Lua UDP messages on, uses a random port if null",
restartRequired: true,
type: "number",
optional: true,
validator: validators.optional(validators.greaterThanEqualZero),
},
"factorio.player_online_autosave_slots": {
description:
"Rename autosaves where players have been online since the previous autosave into a separate " +
@@ -795,7 +812,12 @@ export function addPluginFieldDefinitions(
fieldDef.dependsOn.push("factorio.enable_script_commands");
}
if (scriptCommands || savePatching) {
const luaUdp = pluginInfo.features.includes("LuaUdp");
if (luaUdp) {
fieldDef.dependsOn.push("factorio.enable_lua_udp");
}
if (scriptCommands || savePatching || luaUdp) {
fieldDef.validator = (enabled, config) => {
if (!enabled) {
return;
@@ -806,6 +828,9 @@ export function addPluginFieldDefinitions(
if (scriptCommands && !config.get("factorio.enable_script_commands")) {
throw new Error(`Plugin ${pluginInfo.name} requires script commands`);
}
if (luaUdp && !config.get("factorio.enable_lua_udp")) {
throw new Error(`Plugin ${pluginInfo.name} requires Lua UDP`);
}
};
}
}
+2
View File
@@ -14,6 +14,8 @@ export const PluginFeatureFlags = [
"SavePatching",
/** The plugin requires access to script commands over rcon */
"ScriptCommands",
/** The plugin requires UDP messaging with the Factorio server */
"LuaUdp",
] as const;
/** Used to define the plugin export in plugins */