Files
factorio-clusterio/modules/clusterio/api.lua
T
Hornwitser 1e6b807ae3 Add Clusterio Library mod
Add a library mod for wrapping the Clusterio remote API and providing
related utilities.  This eases development of Clusterio related Lua code
by providing the same interface for both mods and modules and also makes
the serialize library available without having to duplicate it.

It is not possible to implement this through save patching as mods
cannot load Lua code from the level code like they can with code from
other mods.
2020-03-12 15:39:25 +01:00

43 lines
1.2 KiB
Lua

-- Important: Keep this API in sync with mod/api.lua
local api = {}
api.events = {
on_instance_updated = script.generate_event_name(),
on_server_startup = script.generate_event_name(),
}
function api.get_instance_name()
return global.clusterio.instance_name
end
function api.get_instance_id()
return global.clusterio.instance_id
end
function api.send_json(channel, data)
-- Escape bad characters. The question mark is used for separating the
-- channel name from the payload.
channel = channel:gsub("([\x00-\x1f?\\])", function(match)
return "\\x" .. string.format("%02x", match:byte())
end)
data = game.table_to_json(data)
-- If there's more than about 4kB of data users running with the Windows
-- console open will start to experience stuttering, otherwise we could
-- output about 1 MB of data at a time through stdout.
if #data < 4000 then
print("\f$ipc:" .. channel .. "?j" .. data)
else
global.clusterio_file_no = (global.clusterio_file_no or 0) + 1
local file_name = "clst_" .. global.clusterio_file_no .. ".json"
game.write_file(file_name, data, false, 0)
print("\f$ipc:" .. channel .. "?f" .. file_name)
end
end
return api