From 8ccfe43cef3c05233b8f2148fc5c610fcfc15aef Mon Sep 17 00:00:00 2001 From: plague006 Date: Mon, 18 Feb 2019 01:43:59 -0500 Subject: [PATCH] Make lifecycle more readable. --- .luacheckrc | 2 +- control.lua | 3 ++- features/force_control.lua | 2 +- features/server.lua | 2 +- map_gen/maps/crash_site.lua | 2 +- resources/data_stages.lua | 12 ++++++++++++ utils/debug.lua | 2 +- utils/event.lua | 16 ++++++++-------- utils/global.lua | 4 ++-- utils/gui.lua | 2 +- utils/redmew_settings.lua | 2 +- utils/state_machine.lua | 7 ++++--- utils/task.lua | 8 ++++---- 13 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 resources/data_stages.lua diff --git a/.luacheckrc b/.luacheckrc index 6df4be46..97fb13cd 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -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 diff --git a/control.lua b/control.lua index 2c02c104..dac9bd9d 100644 --- a/control.lua +++ b/control.lua @@ -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' diff --git a/features/force_control.lua b/features/force_control.lua index 34adb74c..f1095385 100644 --- a/features/force_control.lua +++ b/features/force_control.lua @@ -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') diff --git a/features/server.lua b/features/server.lua index f9b3cfbc..381218d1 100644 --- a/features/server.lua +++ b/features/server.lua @@ -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 diff --git a/map_gen/maps/crash_site.lua b/map_gen/maps/crash_site.lua index 92fa5a16..49f88296 100644 --- a/map_gen/maps/crash_site.lua +++ b/map_gen/maps/crash_site.lua @@ -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) diff --git a/resources/data_stages.lua b/resources/data_stages.lua new file mode 100644 index 00000000..516fb321 --- /dev/null +++ b/resources/data_stages.lua @@ -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 +} diff --git a/utils/debug.lua b/utils/debug.lua index 39793f16..40456997 100644 --- a/utils/debug.lua +++ b/utils/debug.lua @@ -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) diff --git a/utils/event.lua b/utils/event.lua index 997c8064..9467c647 100644 --- a/utils/event.lua +++ b/utils/event.lua @@ -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 -- @param token 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 -- @param func 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 -- @param func 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 -- @param token 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 -- @param token 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 -- @param func 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 -- @param func 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] diff --git a/utils/global.lua b/utils/global.lua index 9dde19b8..4d23bd43 100644 --- a/utils/global.lua +++ b/utils/global.lua @@ -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) diff --git a/utils/gui.lua b/utils/gui.lua index fc07d56e..7ff901aa 100644 --- a/utils/gui.lua +++ b/utils/gui.lua @@ -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 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 diff --git a/utils/redmew_settings.lua b/utils/redmew_settings.lua index efc43231..9ddc7971 100644 --- a/utils/redmew_settings.lua +++ b/utils/redmew_settings.lua @@ -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 diff --git a/utils/state_machine.lua b/utils/state_machine.lua index 7a79cda9..a3295caf 100644 --- a/utils/state_machine.lua +++ b/utils/state_machine.lua @@ -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 diff --git a/utils/task.lua b/utils/task.lua index 999696b1..a43c3174 100644 --- a/utils/task.lua +++ b/utils/task.lua @@ -77,8 +77,8 @@ end -- @param func_token a token for a function store via the token system -- @param params 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 a token for a function store via the token system -- @param params 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