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.
This commit is contained in:
Hornwitser committed 2020-03-12 15:39:25 +01:00
1 parent 5ef6d02054
commit 1e6b807ae3
8 files changed
+328 -59

No files matched your search

+7 -29
View File
@@ -1,42 +1,20 @@
-- Important: Keep this API in sync with mod/api.lua
local api = {}
-- Table of events raised by clusterio
api.events = {
-- Raised after the name and id of an instance has been updated
-- This may occur even if the id and name didn't change.
-- Event data:
-- instance_id: The id of the instance.
-- instance_name: the name of the instance.
on_instance_updated = script.generate_event_name(),
-- Raised when the server is starting up
-- This event serves as the stand-in for the on_init and
-- on_configuration_changed events for modules. It is invoked as early
-- as possible, before most but not all other events, every time the
-- server is started up.
-- Use this event to initialize and and/or migrate the data structures
-- you need. Keep in mind that Clusterio can switch from any version of
-- your module to any version of your module so it should be able to
-- handle both forwards and backwards migrations.
on_server_startup = script.generate_event_name(),
}
-- Send a table as json to Clusterio
-- Send data to Clusterio over the given channel. Clusterio plugins can
-- listen to channels and will receive an event with the data sent here.
-- See docs/writing-plugins.md for more information.
function api.get_instance_name()
return global.clusterio.instance_name
end
-- Note: Payloads greater than 4 MB will cause stuttering.
function api.get_instance_id()
return global.clusterio.instance_id
end
-- Note: This is not a binary safe way of sending data. Strings embedded
-- into the tables sent must be valid UTF-8 text.
-- Parameters:
-- channel: string identifying which channel to send it on.
-- data: table that can be converted to json with game.table_to_json
function api.send_json(channel, data)
-- Escape bad characters. The question mark is used for separating the
+22 -18
View File
@@ -32,26 +32,30 @@ function clusterio_private.update_instance(new_id, new_name)
end
function impl.add_remote_interface()
remote.add_interface('clusterio_api', {
-- Returns a table of events raised by clusterio
get_events = function()
return api.events
end,
-- This is not part of the add_remote_interface callback to ensure it is
-- available when the clusterio_lib mod is loaded. The reason this is
-- neccessary is that on_init for newly added mods happen before on_load
-- for existing mods, and the add_remote_interface callback is done in
-- on_load. See https://forums.factorio.com/viewtopic.php?f=25&t=81552 for
-- more details.
remote.add_interface('clusterio_api', {
get_events = function()
return api.events
end,
-- Returns the instance id the game is run under
-- This may change over time and/or be nil.
get_instance_id = function()
return global.clusterio.instance_id
end,
get_instance_id = function()
return global.clusterio.instance_id
end,
-- Returns the name of the instance the game runs under.
-- This may change over time and/or be nil.
get_instance_name = function()
return global.clusterio.instance_name
end,
})
end
get_instance_name = function()
return global.clusterio.instance_name
end,
get_file_no = function()
global.clusterio_file_no = (global.clusterio_file_no or 0) + 1
return global.clusterio_file_no
end,
})
return impl