From ab3be846aa488637405988d1b656cd854bdc1eb5 Mon Sep 17 00:00:00 2001 From: Maarten Bezemer Date: Thu, 23 May 2024 13:05:39 +0200 Subject: [PATCH] Try to fix missing player data In order to be more flexible to unexpected situations. --- changelog.txt | 2 ++ control.lua | 33 ++++++++++++++++++++++++++++----- locale/en/locale.cfg | 1 + migrations/tlbe.1.4.4.lua | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/changelog.txt b/changelog.txt index d18d0fd..a85cddf 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,8 @@ --------------------------------------------------------------------------------------------------- Version: 1.5.2 + Minor Features: + - Try to fix missing player data in order to be more flexible to unexpected situations. Bugfixes: - Fix optional dependency on StatsGui. diff --git a/control.lua b/control.lua index cfdbc1e..4db8e0f 100644 --- a/control.lua +++ b/control.lua @@ -12,16 +12,25 @@ local function register_sensor() end end +--- @param index string|integer Index of player in game.players dictionary +--- @param player LuaPlayer +local function init_new_player(index, player) + -- initialize player(s) when mod is loaded into existing game + player.print("init_new_player") + TLBE.Config.reload({ player_index = index }) + TLBE.GUI.initialize(player, global.playerSettings[index]) + + player.print({ "mod-loaded" }, { r = 1, g = 0.5, b = 0 }) + player.print({ "mod-loaded2" }) +end + local function on_init() global.playerSettings = {} for index, player in pairs(game.players) do -- initialize player(s) when mod is loaded into existing game - TLBE.Config.reload({ player_index = index }) - TLBE.GUI.initialize(player, global.playerSettings[index]) - - player.print({ "mod-loaded" }, { r = 1, g = 0.5, b = 0 }) - player.print({ "mod-loaded2" }) + player.print("on_init") + init_new_player(index, player) end local baseBBox = TLBE.Main.getBaseBBox(game.surfaces[1].name) @@ -42,6 +51,19 @@ local function on_load() register_sensor() end +-- The game configuration got changed (e.g. a mod like ourselves is added) +--- @param event ConfigurationChangedData +local function on_configuration_changed(event) + -- Sometimes playerSettings does not seem to be present when upgrading + -- from older versions. We can just fix this issue here. + for index, player in pairs(game.players) do + if global.playerSettings[index] == nil then + player.print({ "migration-fix-missing-player-data" }) + init_new_player(index, player) + end + end +end + -- A player got created (or joined the game) --- @param event EventData.on_player_created local function on_player_created(event) @@ -62,6 +84,7 @@ end script.on_init(on_init) script.on_load(on_load) +script.on_configuration_changed(on_configuration_changed) script.on_event(defines.events.on_gui_click, TLBE.GUI.onClick) script.on_event(defines.events.on_gui_selection_state_changed, TLBE.GUI.onSelected) diff --git a/locale/en/locale.cfg b/locale/en/locale.cfg index 1af58ff..dc4ba90 100644 --- a/locale/en/locale.cfg +++ b/locale/en/locale.cfg @@ -6,6 +6,7 @@ migration-issue-framerate=Could not recover 'frame rate', please set manually as migration-issue-transitiondata1=There is an active camera transition. Transitions changed with this update and cannot be migrated to the new version. migration-issue-transitiondata2=Please finish the camera transition before updating to this version to prevent a broken transition in the video. migration-issue-zoomperiod=Could not recover 'zoom period', please set manually as soon as possible. (only rocket tracker will not work as before). +migration-fix-missing-player-data=Inserted missing TLBE player data. mod-loaded=TBLE got loaded into an existing game, calculating base dimensions... mod-loaded2=Remember to enable the TLBE camera to start taking screenshots. msg-once=This message will only show once! diff --git a/migrations/tlbe.1.4.4.lua b/migrations/tlbe.1.4.4.lua index fd21fcb..89415ae 100644 --- a/migrations/tlbe.1.4.4.lua +++ b/migrations/tlbe.1.4.4.lua @@ -2,7 +2,7 @@ if global.playerSettings == nil then goto SkipMigration end --- Set camera alwaysDay to true for backwards compability +-- Set camera alwaysDay to true for backwards compatibility -- Make tracker untilBuild available for all trackers for player_index, _ in pairs(game.players) do local playerSettings = global.playerSettings[player_index]