1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-11 14:49:24 +02:00

Event - on_configuration_changed

Added support to handle multiple handlers
This commit is contained in:
Gerkiz 2021-03-16 22:44:40 +01:00
parent f213de2e74
commit 717d838f39
5 changed files with 42 additions and 20 deletions

View File

@ -606,10 +606,7 @@ function Public.bottom_button(value)
end
end
Event.on_configuration_changed = function()
do_whitelist()
log('[Autostash] on_configuration_changed was called, rebuilding resource whitelist.')
end
Event.on_configuration_changed(do_whitelist)
Event.on_init(do_whitelist)
Event.add(defines.events.on_player_joined_game, on_player_joined_game)

View File

@ -10,7 +10,7 @@ _STAGE = {
control = 4,
init = 5,
load = 6,
--config_change = 7,
config_change = 7,
runtime = 8
}

View File

@ -105,6 +105,7 @@ local core_add = EventCore.add
local core_on_init = EventCore.on_init
local core_on_load = EventCore.on_load
local core_on_nth_tick = EventCore.on_nth_tick
local core_on_configuration_changed = EventCore.on_configuration_changed
local stage_load = _STAGE.load
local script_on_event = script.on_event
local script_on_nth_tick = script.on_nth_tick
@ -190,6 +191,16 @@ function Event.on_load(handler)
core_on_load(handler)
end
--- Register a handler for the script.on_configuration_changed event.
-- @param handler<function>
function Event.on_configuration_changed(handler)
if _LIFECYCLE == 8 then
error('Calling Event.on_configuration_changed after on_init() or on_load() has run is a desync risk.', 2)
end
core_on_configuration_changed(handler)
end
--- Register a handler for the nth_tick event.
-- This function must be called in the control stage or in Event.on_init or Event.on_load.
-- See documentation at top of file for details on using events.
@ -278,10 +289,7 @@ function Event.add_removable_function(event_name, func, name)
end
if Debug.is_closure(f) then
error(
'func cannot be a closure as that is a desync risk. Consider using Event.add_removable(event, token) instead.',
2
)
error('func cannot be a closure as that is a desync risk. Consider using Event.add_removable(event, token) instead.', 2)
end
local funcs = function_handlers[name]
@ -419,10 +427,7 @@ function Event.add_removable_nth_tick_function(tick, func, name)
end
if Debug.is_closure(f) then
error(
'func cannot be a closure as that is a desync risk. Consider using Event.add_removable_nth_tick(tick, token) instead.',
2
)
error('func cannot be a closure as that is a desync risk. Consider using Event.add_removable_nth_tick(tick, token) instead.', 2)
end
local funcs = function_nth_tick_handlers[name]
@ -507,12 +512,6 @@ function Event.generate_event_name(name)
return event_id
end
function Event.on_configuration_changed(func)
if type(func) == 'function' then
script.on_configuration_changed(func)
end
end
function Event.add_event_filter(event, filter)
local current_filters = script.get_event_filter(event)
@ -584,6 +583,7 @@ end
core_on_init(add_handlers)
core_on_load(add_handlers)
core_on_configuration_changed(add_handlers)
function_table = {}
function_nth_tick_table = {}

View File

@ -5,6 +5,7 @@ local Public = {}
local init_event_name = -1
local load_event_name = -2
local configuration_changed_name = -3
-- map of event_name to handlers[]
local event_handlers = {}
@ -25,6 +26,7 @@ local trace = debug.traceback
local log = log
local script_on_event = script.on_event
local script_on_nth_tick = script.on_nth_tick
local script_on_configuration_changed = script.on_configuration_changed
local function handler_error(err)
log('\n\t' .. trace(err))
@ -73,6 +75,13 @@ local function on_load()
_LIFECYCLE = 8 -- Runtime
end
local function configuration_changed()
_LIFECYCLE = 7 -- config_change
local handlers = event_handlers[configuration_changed_name]
call_handlers(handlers)
_LIFECYCLE = 8 -- Runtime
end
local function on_nth_tick_event(event)
local handlers = on_nth_tick_event_handlers[event.nth_tick]
call_handlers(handlers, event)
@ -106,6 +115,20 @@ function Public.on_init(handler)
end
end
--- Do not use this function, use Event.on_configuration_changed instead as it has safety checks.
function Public.on_configuration_changed(handler)
local handlers = event_handlers[configuration_changed_name]
if not handlers then
event_handlers[configuration_changed_name] = {handler}
script_on_configuration_changed(configuration_changed)
else
table.insert(handlers, handler)
if #handlers == 1 then
script.on_configuration_changed(configuration_changed)
end
end
end
--- Do not use this function, use Event.on_load instead as it has safety checks.
function Public.on_load(handler)
local handlers = event_handlers[load_event_name]

View File

@ -222,7 +222,7 @@ Event.on_init(
end
)
Event.on_configuration_changed = function()
local on_configuration_changed = function()
this.created_items = this.created_items or created_items()
this.respawn_items = this.respawn_items or respawn_items()
this.crashed_ship_items = this.crashed_ship_items or ship_items()
@ -233,6 +233,8 @@ Event.on_configuration_changed = function()
end
end
Event.on_configuration_changed(on_configuration_changed)
Event.add(defines.events.on_player_created, on_player_created)
Event.add(defines.events.on_player_respawned, on_player_respawned)
Event.add(defines.events.on_cutscene_waypoint_reached, on_cutscene_waypoint_reached)