diff --git a/cybersyn/changelog.txt b/cybersyn/changelog.txt index 00818f9..d012120 100644 --- a/cybersyn/changelog.txt +++ b/cybersyn/changelog.txt @@ -11,6 +11,7 @@ Date: 2022-12-23 - Nonempty trains in depot are no longer put in manual mode, instead they are forced to park at the depot - Made several alerts persistent - Fixed bug with depot priority not working + - Fixed a memory leak relating to train layouts --------------------------------------------------------------------------------------------------- Version: 1.1.7 Date: 2022-12-17 diff --git a/cybersyn/scripts/layout.lua b/cybersyn/scripts/layout.lua index 30baf6b..a598350 100644 --- a/cybersyn/scripts/layout.lua +++ b/cybersyn/scripts/layout.lua @@ -87,6 +87,19 @@ function remove_train(map_data, train_id, train) depot.available_train_id = nil end remove_available_train(map_data, train_id, train) + + local layout_id = train.layout_id + local count = global.layout_train_count[layout_id] + if count <= 1 then + global.layout_train_count[layout_id] = nil + global.layouts[layout_id] = nil + for _, station in pairs(global.stations) do + station.accepted_layouts[layout_id] = nil + end + else + global.layout_train_count[layout_id] = count - 1 + end + map_data.trains[train_id] = nil interface_raise_train_removed(train_id, train) end diff --git a/cybersyn/scripts/migrations.lua b/cybersyn/scripts/migrations.lua index 4507bff..737448d 100644 --- a/cybersyn/scripts/migrations.lua +++ b/cybersyn/scripts/migrations.lua @@ -135,7 +135,20 @@ local migrations_table = { control.parameters = params end for id, station in pairs(map_data.stations) do - station.display_state = (station.display_state >= 2 and 1) + (station.display_state%2)*2 + station.display_state = (station.display_state >= 2 and 1 or 0) + (station.display_state%2)*2 + end + + map_data.layout_train_count = {} + for id, train in pairs(map_data.trains) do + map_data.layout_train_count[train.layout_id] = (map_data.layout_train_count[train.layout_id] or 0) + 1 + end + for layout_id, _ in pairs(map_data.layouts) do + if not map_data.layout_train_count[layout_id] then + map_data.layouts[layout_id] = nil + for id, station in pairs(map_data.stations) do + station.accepted_layouts[layout_id] = nil + end + end end end, }