diff --git a/packages/host/lua/clusterio_lib/api.lua b/packages/host/lua/clusterio_lib/api.lua index 90b121e3..6af5463b 100644 --- a/packages/host/lua/clusterio_lib/api.lua +++ b/packages/host/lua/clusterio_lib/api.lua @@ -1,4 +1,5 @@ -- Important: Keep this API in sync with modules/clusterio/api.lua +local compat = require("compat") local api = {} @@ -48,7 +49,7 @@ function api.send_json(channel, data) return "\\x" .. string.format("%02x", match:byte()) end) - data = game.table_to_json(data) + data = compat.table_to_json(data) -- If there's more than about 4kB of data users running with the Windows -- console open will start to experience stuttering, otherwise we could @@ -58,7 +59,7 @@ function api.send_json(channel, data) else local file_no = remote.call("clusterio_api", "get_file_no") local file_name = "clst_" .. file_no .. ".json" - game.write_file(file_name, data, false, 0) + compat.write_file(file_name, data, false, 0) print("\f$ipc:" .. channel .. "?f" .. file_name) end end diff --git a/packages/host/lua/export/control.lua b/packages/host/lua/export/control.lua index b810f2f0..1833a4cc 100644 --- a/packages/host/lua/export/control.lua +++ b/packages/host/lua/export/control.lua @@ -1,11 +1,12 @@ local function send_json(channel, data) - data = game.table_to_json(data) + data = helpers and helpers.table_to_json(data) or game.table_to_json(data) print("\f$ipc:" .. channel .. "?j" .. data) end script.on_init(function() - send_json("mod_list", game.active_mods) - for name, mod_setting in pairs(game.mod_setting_prototypes) do + send_json("mod_list", script.active_mods) + local mod_settings = prototypes and prototypes.mod_setting or game.mod_setting_prototypes + for name, mod_setting in pairs(mod_settings) do send_json("mod_setting_mod", { name = name, mod = mod_setting.mod }) end end) diff --git a/packages/host/modules/clusterio/api.lua b/packages/host/modules/clusterio/api.lua index 08e7fbf1..fc82b080 100644 --- a/packages/host/modules/clusterio/api.lua +++ b/packages/host/modules/clusterio/api.lua @@ -1,4 +1,5 @@ -- Important: Keep this API in sync with mod/api.lua +local compat = require("modules/clusterio/compat") local api = {} @@ -8,11 +9,11 @@ api.events = { } function api.get_instance_name() - return global.clusterio.instance_name + return compat.script_data().clusterio.instance_name end function api.get_instance_id() - return global.clusterio.instance_id + return compat.script_data().clusterio.instance_id end function api.send_json(channel, data) @@ -23,7 +24,7 @@ function api.send_json(channel, data) return "\\x" .. string.format("%02x", match:byte()) end) - data = game.table_to_json(data) + data = compat.table_to_json(data) -- If there's more than about 4kB of data users running with the Windows -- console open will start to experience stuttering, otherwise we could @@ -31,9 +32,10 @@ function api.send_json(channel, data) if #data < 4000 then print("\f$ipc:" .. channel .. "?j" .. data) else - global.clusterio_file_no = (global.clusterio_file_no or 0) + 1 - local file_name = "clst_" .. global.clusterio_file_no .. ".json" - game.write_file(file_name, data, false, 0) + local script_data = compat.script_data() + script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1 + local file_name = "clst_" .. script_data.clusterio_file_no .. ".json" + compat.write_file(file_name, data, false, 0) print("\f$ipc:" .. channel .. "?f" .. file_name) end end diff --git a/packages/host/modules/clusterio/compat.lua b/packages/host/modules/clusterio/compat.lua index 1c95db24..ff0d271e 100644 --- a/packages/host/modules/clusterio/compat.lua +++ b/packages/host/modules/clusterio/compat.lua @@ -137,12 +137,16 @@ end if major_v1_1 then -- Game is not always available so it needs to be called within another function + -- Can not use ... here because of luals type suggestion breaking when it is used -- TODO maybe include a version that does not require game? - compat.table_to_json = function(...) return game.table_to_json(...) end - compat.json_to_table = function(...) return game.json_to_table(...) end - compat.write_file = function(...) return game.write_file(...) end + + compat.table_to_json = function(tbl) return game.table_to_json(tbl) end + compat.json_to_table = function(tbl) return game.json_to_table(tbl) end + compat.write_file = function(filename, data, append, for_player) return game.write_file(filename, data, append, for_player) end elseif major_v2 then compat.table_to_json = helpers.table_to_json compat.json_to_table = helpers.json_to_table compat.write_file = helpers.write_file end + +return compat diff --git a/packages/host/modules/clusterio/impl.lua b/packages/host/modules/clusterio/impl.lua index 93175950..76e713b7 100644 --- a/packages/host/modules/clusterio/impl.lua +++ b/packages/host/modules/clusterio/impl.lua @@ -1,9 +1,10 @@ local api = require('modules/clusterio/api') - +local compat = require("modules/clusterio/compat") local function check_patch() - if global.clusterio_patch_number ~= clusterio_patch_number then - global.clusterio_patch_number = clusterio_patch_number + local script_data = compat.script_data() + if script_data.clusterio_patch_number ~= clusterio_patch_number then + script_data.clusterio_patch_number = clusterio_patch_number script.raise_event(api.events.on_server_startup, {}) end end @@ -14,8 +15,9 @@ impl.events = {} impl.events[defines.events.on_tick] = check_patch impl.events[api.events.on_server_startup] = function() - if not global.clusterio then - global.clusterio = { + local script_data = compat.script_data() + if not script_data.clusterio then + script_data.clusterio = { instance_id = nil, instance_name = nil, } @@ -49,9 +51,10 @@ end -- Internal API clusterio_private = {} function clusterio_private.update_instance(new_id, new_name) + local script_data = compat.script_data() check_patch() - global.clusterio.instance_id = new_id - global.clusterio.instance_name = new_name + script_data.clusterio.instance_id = new_id + script_data.clusterio.instance_name = new_name script.raise_event(api.events.on_instance_updated, { instance_id = new_id, instance_name = new_name, @@ -71,16 +74,17 @@ remote.add_interface('clusterio_api', { end, get_instance_id = function() - return global.clusterio.instance_id + return compat.script_data().clusterio.instance_id end, get_instance_name = function() - return global.clusterio.instance_name + return compat.script_data().clusterio.instance_name end, get_file_no = function() - global.clusterio_file_no = (global.clusterio_file_no or 0) + 1 - return global.clusterio_file_no + local script_data = compat.script_data() + script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1 + return script_data.clusterio_file_no end, }) diff --git a/packages/host/modules/clusterio/serialize.lua b/packages/host/modules/clusterio/serialize.lua index b03557cf..af3d9feb 100644 --- a/packages/host/modules/clusterio/serialize.lua +++ b/packages/host/modules/clusterio/serialize.lua @@ -1,44 +1,22 @@ -- Library for serializing items in Factorio -- Based on code from playerManager and trainTeleports +local compat = require("modules/clusterio/compat") local serialize = {} -local function version_to_table(version) - local t = {} - for p in string.gmatch(version, "%d+") do - t[#t + 1] = tonumber(p) - end - return t -end - -- 0.17 compatibility -local supports_bar, get_bar, set_bar, version -if (pcall(function() local mods = script.active_mods end)) then +local supports_bar, get_bar, set_bar +if compat.version_ge("1.0.0") then supports_bar = "supports_bar" get_bar = "get_bar" set_bar = "set_bar" - version = version_to_table(script.active_mods.base) else supports_bar = "hasbar" get_bar = "getbar" set_bar = "setbar" - version = version_to_table("0.17.69") end --- returns true if the game version is greater than or equal to the given version -local function version_ge(comp) - comp = version_to_table(comp) - for i=1, 3 do - if comp[i] > version[i] then - return false - elseif comp[i] < version[i] then - return true - end - end - return true -end - -local has_create_grid = version_ge("1.1.7") - +local has_create_grid = compat.version_ge("1.1.7") +local has_quality = compat.version_ge("2.0.0") -- Equipment Grids are serialized into an array of equipment entries -- where ench entry is a table with the following fields: @@ -109,6 +87,7 @@ end -- Item stacks are serialized into a table with the following fields: -- n: name -- c: count +-- q: quality (optional) -- h: health (optional) -- d: durability (optional) -- a: ammo count (optional) @@ -141,6 +120,7 @@ function serialize.serialize_item_stack(slot, entry) entry.n = slot.name entry.c = slot.count + if has_quality and slot.quality.level ~= 0 then entry.q = slot.quality.name end if slot.health < 1 then entry.h = slot.health end if slot.durability then entry.d = slot.durability end if slot.type == "ammo" then entry.a = slot.ammo end @@ -178,6 +158,7 @@ function serialize.deserialize_item_stack(slot, entry) name = entry.n, count = entry.c, } + if entry.q then item_stack.quality = entry.q end if entry.h then item_stack.health = entry.h end if entry.d then item_stack.durability = entry.d end if entry.a then item_stack.ammo = entry.a end @@ -244,7 +225,7 @@ function serialize.serialize_inventory(inventory) end if item.n or item.f or item.e then - local item_serialized = game.table_to_json(item) + local item_serialized = compat.table_to_json(item) if item_serialized == previous_serialized then local previous_item = serialized.i[#serialized.i] previous_item.r = (previous_item.r or 0) + 1 @@ -305,5 +286,4 @@ function serialize.deserialize_inventory(inventory, serialized) end end - return serialize