Skip to content

Commit

Permalink
Try to fix missing player data
Browse files Browse the repository at this point in the history
In order to be more flexible to unexpected situations.
  • Loading branch information
veger committed May 23, 2024
1 parent de48dfa commit ab3be84
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
33 changes: 28 additions & 5 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion migrations/tlbe.1.4.4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ab3be84

Please sign in to comment.