mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
Make lifecycle more readable.
This commit is contained in:
parent
4c0c7ed41c
commit
8ccfe43cef
@ -56,7 +56,7 @@ local STD_BASE_CONTROL = 'lua52c+factorio+factorio_control+factorio_defines+fact
|
||||
--[Assume Factorio Control stage as default]--
|
||||
-------------------------------------------------------------------------------
|
||||
std = STD_CONTROL
|
||||
globals = {'print', '_DEBUG', '_CHEATS', '_DUMP_ENV', 'ServerCommands', 'Debug', '_LIFECYCLE'} -- RedMew-specific globals
|
||||
globals = {'print', '_DEBUG', '_CHEATS', '_DUMP_ENV', 'ServerCommands', 'Debug', '_LIFECYCLE', '_STAGE'} -- RedMew-specific globals
|
||||
max_line_length = LINE_LENGTH
|
||||
|
||||
not_globals = NOT_GLOBALS
|
||||
|
@ -1,7 +1,8 @@
|
||||
-- If you're looking to configure anything, you want config.lua. Nearly everything in this file is dictated by the config.
|
||||
|
||||
-- Info on the data lifecycle and how we use it: https://github.com/Refactorio/RedMew/wiki/The-data-lifecycle
|
||||
_LIFECYCLE = 4 -- Control stage
|
||||
require 'resources.data_stages'
|
||||
_LIFECYCLE = _STAGE.control -- Control stage
|
||||
|
||||
-- Omitting the math library is a very bad idea
|
||||
require 'utils.math'
|
||||
|
@ -76,7 +76,7 @@ end
|
||||
---@param callback function function(number level_reached, LuaForce force)
|
||||
---@param lua_force_name string|nil only register for this force (optional)
|
||||
function ForceControlBuilder.register(level_matches, callback, lua_force_name)
|
||||
if _LIFECYCLE > 4 then
|
||||
if _LIFECYCLE > _STAGE.control then
|
||||
error('You can only register level up callbacks before the game is initialized')
|
||||
end
|
||||
assert_type('function', level_matches, 'level_matches of function ForceControl.register_reward')
|
||||
|
@ -335,7 +335,7 @@ end
|
||||
-- end
|
||||
-- )
|
||||
function Public.on_data_set_changed(data_set, handler)
|
||||
if _LIFECYCLE == 8 then
|
||||
if _LIFECYCLE == _STAGE.runtime then
|
||||
error('cannot call during runtime', 2)
|
||||
end
|
||||
if type(data_set) ~= 'string' then
|
||||
|
@ -128,7 +128,7 @@ local spawn_callback =
|
||||
)
|
||||
|
||||
local function init()
|
||||
local on_init = _LIFECYCLE == 5
|
||||
local on_init = (_LIFECYCLE == _STAGE.init)
|
||||
|
||||
local outpost_random = Random.new(outpost_seed, outpost_seed * 2)
|
||||
|
||||
|
12
resources/data_stages.lua
Normal file
12
resources/data_stages.lua
Normal file
@ -0,0 +1,12 @@
|
||||
-- Info on the data lifecycle and how we use it: https://github.com/Refactorio/RedMew/wiki/The-data-lifecycle
|
||||
-- Non-applicable stages are commented out.
|
||||
_STAGE = {
|
||||
--settings = 1,
|
||||
--data = 2,
|
||||
--migration = 3,
|
||||
control = 4,
|
||||
init = 5,
|
||||
load = 6,
|
||||
--config_change = 7,
|
||||
runtime = 8
|
||||
}
|
@ -62,7 +62,7 @@ function Debug.print(message, trace_levels)
|
||||
message = format('%s - Traceback%s', message, traceback_string)
|
||||
end
|
||||
|
||||
if _LIFECYCLE == 8 then
|
||||
if _LIFECYCLE == _STAGE.runtime then
|
||||
game.print(message)
|
||||
end
|
||||
log(message)
|
||||
|
@ -205,7 +205,7 @@ function Event.add_removable(event_name, token)
|
||||
if type(token) ~= 'number' then
|
||||
error('token must be a number', 2)
|
||||
end
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
|
||||
@ -228,7 +228,7 @@ end
|
||||
-- @param event_name<number>
|
||||
-- @param token<number>
|
||||
function Event.remove_removable(event_name, token)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
local tokens = token_handlers[event_name]
|
||||
@ -255,7 +255,7 @@ end
|
||||
-- @param event_name<number>
|
||||
-- @param func<function>
|
||||
function Event.add_removable_function(event_name, func)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
if type(func) ~= 'function' then
|
||||
@ -287,7 +287,7 @@ end
|
||||
-- @param event_name<number>
|
||||
-- @param func<function>
|
||||
function Event.remove_removable_function(event_name, func)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
local funcs = function_handlers[event_name]
|
||||
@ -312,7 +312,7 @@ end
|
||||
-- @param tick<number>
|
||||
-- @param token<number>
|
||||
function Event.add_removable_nth_tick(tick, token)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
if type(token) ~= 'number' then
|
||||
@ -338,7 +338,7 @@ end
|
||||
-- @param tick<number>
|
||||
-- @param token<number>
|
||||
function Event.remove_removable_nth_tick(tick, token)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
local tokens = token_nth_tick_handlers[tick]
|
||||
@ -365,7 +365,7 @@ end
|
||||
-- @param tick<number>
|
||||
-- @param func<function>
|
||||
function Event.add_removable_nth_tick_function(tick, func)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
if type(func) ~= 'function' then
|
||||
@ -397,7 +397,7 @@ end
|
||||
-- @param tick<number>
|
||||
-- @param func<function>
|
||||
function Event.remove_removable_nth_tick_function(tick, func)
|
||||
if _LIFECYCLE == 6 then
|
||||
if _LIFECYCLE == _STAGE.load then
|
||||
error('cannot call during on_load', 2)
|
||||
end
|
||||
local funcs = function_nth_tick_handlers[tick]
|
||||
|
@ -4,7 +4,7 @@ local Token = require 'utils.token'
|
||||
local Global = {}
|
||||
|
||||
function Global.register(tbl, callback)
|
||||
if _LIFECYCLE ~= 4 then
|
||||
if _LIFECYCLE ~= _STAGE.control then
|
||||
error('can only be called during the control stage', 2)
|
||||
end
|
||||
local token = Token.register_global(tbl)
|
||||
@ -17,7 +17,7 @@ function Global.register(tbl, callback)
|
||||
end
|
||||
|
||||
function Global.register_init(tbl, init_handler, callback)
|
||||
if _LIFECYCLE ~= 4 then
|
||||
if _LIFECYCLE ~= _STAGE.control then
|
||||
error('can only be called during the control stage', 2)
|
||||
end
|
||||
local token = Token.register_global(tbl)
|
||||
|
@ -183,7 +183,7 @@ Gui.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers)
|
||||
-- This function must be called in the control stage, i.e not inside an event.
|
||||
-- @param element_name<string> This name must be globally unique.
|
||||
function Gui.allow_player_to_toggle_top_element_visibility(element_name)
|
||||
if _LIFECYCLE ~= 4 then
|
||||
if _LIFECYCLE ~= _STAGE.control then
|
||||
error('can only be called during the control stage', 2)
|
||||
end
|
||||
top_elements[#top_elements + 1] = element_name
|
||||
|
@ -89,7 +89,7 @@ Public.types = {fraction = 'fraction', string = 'string', boolean = 'boolean'}
|
||||
---@param setting_type string
|
||||
---@param default mixed
|
||||
function Public.register(name, setting_type, default)
|
||||
if _LIFECYCLE > 4 then
|
||||
if _LIFECYCLE ~= _STAGE.control then
|
||||
error(format('You can only register setting names in the control stage, i.e. not inside events. Tried setting "%s" with type "%s".', name, setting_type), 2)
|
||||
end
|
||||
|
||||
|
@ -12,6 +12,7 @@ local in_state_callbacks = {}
|
||||
local transaction_callbacks = {}
|
||||
local max_stack_depth = 20
|
||||
local machine_count = 0
|
||||
local control_stage = _STAGE.control
|
||||
|
||||
--- Transitions the supplied machine into a given state and executes all transaction_callbacks
|
||||
-- @param self StateMachine
|
||||
@ -75,7 +76,7 @@ end
|
||||
-- @param state number/string The state, that the machine will be in, when callback is invoked
|
||||
-- @param callback function
|
||||
function Module.register_state_tick_callback(self, state, callback)
|
||||
if _LIFECYCLE > 4 then
|
||||
if _LIFECYCLE ~= control_stage then
|
||||
error('Calling StateMachine.register_state_tick_callback after the control stage is unsupported due to desyncs.', 2)
|
||||
end
|
||||
in_state_callbacks[self.id][state] = in_state_callbacks[self.id][state] or {}
|
||||
@ -90,7 +91,7 @@ end
|
||||
-- @param state number/string entering state
|
||||
-- @param callback function
|
||||
function Module.register_transition_callback(self, old, new, callback)
|
||||
if _LIFECYCLE > 4 then
|
||||
if _LIFECYCLE ~= control_stage then
|
||||
error('Calling StateMachine.register_transition_callback after the control stage is unsupported due to desyncs.', 2)
|
||||
end
|
||||
transaction_callbacks[self.id][old] = transaction_callbacks[self.id][old] or {}
|
||||
@ -102,7 +103,7 @@ end
|
||||
-- @param init_state number/string The starting state of the machine
|
||||
-- @return StateMachine The constructed state machine object
|
||||
function Module.new(init_state)
|
||||
if _LIFECYCLE > 4 then
|
||||
if _LIFECYCLE ~= control_stage then
|
||||
error('Calling StateMachine.new after the control stage is unsupported due to desyncs.', 2)
|
||||
end
|
||||
machine_count = machine_count + 1
|
||||
|
@ -77,8 +77,8 @@ end
|
||||
-- @param func_token <number> a token for a function store via the token system
|
||||
-- @param params <any> the argument to send to the tokened function
|
||||
function Task.set_timeout_in_ticks(ticks, func_token, params)
|
||||
if _LIFECYCLE < 5 then
|
||||
error('cannot call before on_init', 2)
|
||||
if not game then
|
||||
error('cannot call when game is not available', 2)
|
||||
end
|
||||
local time = game.tick + ticks
|
||||
local callback = {time = time, func_token = func_token, params = params}
|
||||
@ -91,8 +91,8 @@ end
|
||||
-- @param func_token <number> a token for a function store via the token system
|
||||
-- @param params <any> the argument to send to the tokened function
|
||||
function Task.set_timeout(sec, func_token, params)
|
||||
if _LIFECYCLE < 5 then
|
||||
error('cannot call before on_init', 2)
|
||||
if not game then
|
||||
error('cannot call when game is not available', 2)
|
||||
end
|
||||
Task.set_timeout_in_ticks(60 * sec, func_token, params)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user