Add on_server_startup event

Add startup event to the Clusterio module API which is raised at startup
of a Factorio server.  This is to server as a replacement for the
on_init and on_configuration_changed events which cannot be used in
modules.

Adds clusterio_patch_number global from the patcher in order to
implement this.  This is a number that's incremented every time the save
is patched.
This commit is contained in:
Hornwitser committed 2020-01-05 23:19:34 +01:00
1 parent b8fb73f863
commit 892c803496
6 files changed
+46 -9

No files matched your search

+10 -5
View File
@@ -1,9 +1,17 @@
local api = require('modules/clusterio/api')
local impl = {}
local function init()
impl.events = {}
impl.events[defines.events.on_tick] = function()
if global.clusterio_patch_number ~= clusterio_patch_number then
global.clusterio_patch_number = clusterio_patch_number
script.raise_event(api.events.on_server_startup, {})
end
end
impl.events[api.events.on_server_startup] = function()
if not global.clusterio then
global.clusterio = {
instance_id = nil,
@@ -15,7 +23,6 @@ end
-- Internal API
clusterio_private = {}
function clusterio_private.update_instance(new_id, new_name)
init()
global.clusterio.instance_id = new_id
global.clusterio.instance_name = new_name
script.raise_event(api.events.on_instance_updated, {
@@ -35,14 +42,12 @@ function impl.add_remote_interface()
-- Returns the instance id the game is run under
-- This may change over time and/or be nil.
get_instance_id = function()
init()
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()
init()
return global.clusterio.instance_name
end,
})