From ecbce16c99533bed6c1bdd52e38feabe10531637 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 10 Aug 2021 17:41:40 +0200 Subject: [PATCH] Be more defensive with global table initialization When upgrading or downgrading the inventory_sync plugin it's possible an different structure of the global.inventory_sync table is left behind which could cause the code to crash when certain sub tables are not present. Be more defensive in the initialization of the table to make sure all sub-tables are present on startup. --- .../inventory_sync/module/inventory_sync.lua | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 7c2f59ec..72b7c1f7 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -29,15 +29,24 @@ inventory_sync.upload_inventory = upload_inventory inventory_sync.add_commands = add_commands inventory_sync.events = {} inventory_sync.events[clusterio_api.events.on_server_startup] = function(event) - -- set up global table + -- Set up global table if not global.inventory_sync then - global.inventory_sync = { - download_start_tick = {}, - saved_crafting_queue = {}, - download_cache = {}, - players_waiting_for_download = {}, - players = {}, - } + global.inventory_sync = {} + end + if not global.inventory_sync.download_start_tick then + global.inventory_sync.download_start_tick = {} + end + if not global.inventory_sync.saved_crafting_queue then + global.inventory_sync.saved_crafting_queue = {} + end + if not global.inventory_sync.download_cache then + global.inventory_sync.download_cache = {} + end + if not global.inventory_sync.players_waiting_for_download then + global.inventory_sync.players_waiting_for_download = {} + end + if not global.inventory_sync.players then + global.inventory_sync.players = {} end end