mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Add 2.0 support to core lua
This commit is contained in:
6 files changed
+46
-54
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
-- Important: Keep this API in sync with modules/clusterio/api.lua
|
-- Important: Keep this API in sync with modules/clusterio/api.lua
|
||||||
|
local compat = require("compat")
|
||||||
local api = {}
|
local api = {}
|
||||||
|
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ function api.send_json(channel, data)
|
|||||||
return "\\x" .. string.format("%02x", match:byte())
|
return "\\x" .. string.format("%02x", match:byte())
|
||||||
end)
|
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
|
-- If there's more than about 4kB of data users running with the Windows
|
||||||
-- console open will start to experience stuttering, otherwise we could
|
-- console open will start to experience stuttering, otherwise we could
|
||||||
@@ -58,7 +59,7 @@ function api.send_json(channel, data)
|
|||||||
else
|
else
|
||||||
local file_no = remote.call("clusterio_api", "get_file_no")
|
local file_no = remote.call("clusterio_api", "get_file_no")
|
||||||
local file_name = "clst_" .. file_no .. ".json"
|
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)
|
print("\f$ipc:" .. channel .. "?f" .. file_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
local function send_json(channel, data)
|
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)
|
print("\f$ipc:" .. channel .. "?j" .. data)
|
||||||
end
|
end
|
||||||
|
|
||||||
script.on_init(function()
|
script.on_init(function()
|
||||||
send_json("mod_list", game.active_mods)
|
send_json("mod_list", script.active_mods)
|
||||||
for name, mod_setting in pairs(game.mod_setting_prototypes) do
|
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 })
|
send_json("mod_setting_mod", { name = name, mod = mod_setting.mod })
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
-- Important: Keep this API in sync with mod/api.lua
|
-- Important: Keep this API in sync with mod/api.lua
|
||||||
|
local compat = require("modules/clusterio/compat")
|
||||||
local api = {}
|
local api = {}
|
||||||
|
|
||||||
|
|
||||||
@@ -8,11 +9,11 @@ api.events = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function api.get_instance_name()
|
function api.get_instance_name()
|
||||||
return global.clusterio.instance_name
|
return compat.script_data().clusterio.instance_name
|
||||||
end
|
end
|
||||||
|
|
||||||
function api.get_instance_id()
|
function api.get_instance_id()
|
||||||
return global.clusterio.instance_id
|
return compat.script_data().clusterio.instance_id
|
||||||
end
|
end
|
||||||
|
|
||||||
function api.send_json(channel, data)
|
function api.send_json(channel, data)
|
||||||
@@ -23,7 +24,7 @@ function api.send_json(channel, data)
|
|||||||
return "\\x" .. string.format("%02x", match:byte())
|
return "\\x" .. string.format("%02x", match:byte())
|
||||||
end)
|
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
|
-- If there's more than about 4kB of data users running with the Windows
|
||||||
-- console open will start to experience stuttering, otherwise we could
|
-- console open will start to experience stuttering, otherwise we could
|
||||||
@@ -31,9 +32,10 @@ function api.send_json(channel, data)
|
|||||||
if #data < 4000 then
|
if #data < 4000 then
|
||||||
print("\f$ipc:" .. channel .. "?j" .. data)
|
print("\f$ipc:" .. channel .. "?j" .. data)
|
||||||
else
|
else
|
||||||
global.clusterio_file_no = (global.clusterio_file_no or 0) + 1
|
local script_data = compat.script_data()
|
||||||
local file_name = "clst_" .. global.clusterio_file_no .. ".json"
|
script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1
|
||||||
game.write_file(file_name, data, false, 0)
|
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)
|
print("\f$ipc:" .. channel .. "?f" .. file_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -137,12 +137,16 @@ end
|
|||||||
|
|
||||||
if major_v1_1 then
|
if major_v1_1 then
|
||||||
-- Game is not always available so it needs to be called within another function
|
-- 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?
|
-- 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.table_to_json = function(tbl) return game.table_to_json(tbl) end
|
||||||
compat.write_file = function(...) return game.write_file(...) 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
|
elseif major_v2 then
|
||||||
compat.table_to_json = helpers.table_to_json
|
compat.table_to_json = helpers.table_to_json
|
||||||
compat.json_to_table = helpers.json_to_table
|
compat.json_to_table = helpers.json_to_table
|
||||||
compat.write_file = helpers.write_file
|
compat.write_file = helpers.write_file
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return compat
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
local api = require('modules/clusterio/api')
|
local api = require('modules/clusterio/api')
|
||||||
|
local compat = require("modules/clusterio/compat")
|
||||||
|
|
||||||
local function check_patch()
|
local function check_patch()
|
||||||
if global.clusterio_patch_number ~= clusterio_patch_number then
|
local script_data = compat.script_data()
|
||||||
global.clusterio_patch_number = clusterio_patch_number
|
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, {})
|
script.raise_event(api.events.on_server_startup, {})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -14,8 +15,9 @@ impl.events = {}
|
|||||||
impl.events[defines.events.on_tick] = check_patch
|
impl.events[defines.events.on_tick] = check_patch
|
||||||
|
|
||||||
impl.events[api.events.on_server_startup] = function()
|
impl.events[api.events.on_server_startup] = function()
|
||||||
if not global.clusterio then
|
local script_data = compat.script_data()
|
||||||
global.clusterio = {
|
if not script_data.clusterio then
|
||||||
|
script_data.clusterio = {
|
||||||
instance_id = nil,
|
instance_id = nil,
|
||||||
instance_name = nil,
|
instance_name = nil,
|
||||||
}
|
}
|
||||||
@@ -49,9 +51,10 @@ end
|
|||||||
-- Internal API
|
-- Internal API
|
||||||
clusterio_private = {}
|
clusterio_private = {}
|
||||||
function clusterio_private.update_instance(new_id, new_name)
|
function clusterio_private.update_instance(new_id, new_name)
|
||||||
|
local script_data = compat.script_data()
|
||||||
check_patch()
|
check_patch()
|
||||||
global.clusterio.instance_id = new_id
|
script_data.clusterio.instance_id = new_id
|
||||||
global.clusterio.instance_name = new_name
|
script_data.clusterio.instance_name = new_name
|
||||||
script.raise_event(api.events.on_instance_updated, {
|
script.raise_event(api.events.on_instance_updated, {
|
||||||
instance_id = new_id,
|
instance_id = new_id,
|
||||||
instance_name = new_name,
|
instance_name = new_name,
|
||||||
@@ -71,16 +74,17 @@ remote.add_interface('clusterio_api', {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
get_instance_id = function()
|
get_instance_id = function()
|
||||||
return global.clusterio.instance_id
|
return compat.script_data().clusterio.instance_id
|
||||||
end,
|
end,
|
||||||
|
|
||||||
get_instance_name = function()
|
get_instance_name = function()
|
||||||
return global.clusterio.instance_name
|
return compat.script_data().clusterio.instance_name
|
||||||
end,
|
end,
|
||||||
|
|
||||||
get_file_no = function()
|
get_file_no = function()
|
||||||
global.clusterio_file_no = (global.clusterio_file_no or 0) + 1
|
local script_data = compat.script_data()
|
||||||
return global.clusterio_file_no
|
script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1
|
||||||
|
return script_data.clusterio_file_no
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +1,22 @@
|
|||||||
-- Library for serializing items in Factorio
|
-- Library for serializing items in Factorio
|
||||||
-- Based on code from playerManager and trainTeleports
|
-- Based on code from playerManager and trainTeleports
|
||||||
|
local compat = require("modules/clusterio/compat")
|
||||||
local serialize = {}
|
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
|
-- 0.17 compatibility
|
||||||
local supports_bar, get_bar, set_bar, version
|
local supports_bar, get_bar, set_bar
|
||||||
if (pcall(function() local mods = script.active_mods end)) then
|
if compat.version_ge("1.0.0") then
|
||||||
supports_bar = "supports_bar"
|
supports_bar = "supports_bar"
|
||||||
get_bar = "get_bar"
|
get_bar = "get_bar"
|
||||||
set_bar = "set_bar"
|
set_bar = "set_bar"
|
||||||
version = version_to_table(script.active_mods.base)
|
|
||||||
else
|
else
|
||||||
supports_bar = "hasbar"
|
supports_bar = "hasbar"
|
||||||
get_bar = "getbar"
|
get_bar = "getbar"
|
||||||
set_bar = "setbar"
|
set_bar = "setbar"
|
||||||
version = version_to_table("0.17.69")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns true if the game version is greater than or equal to the given version
|
local has_create_grid = compat.version_ge("1.1.7")
|
||||||
local function version_ge(comp)
|
local has_quality = compat.version_ge("2.0.0")
|
||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
-- Equipment Grids are serialized into an array of equipment entries
|
-- Equipment Grids are serialized into an array of equipment entries
|
||||||
-- where ench entry is a table with the following fields:
|
-- 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:
|
-- Item stacks are serialized into a table with the following fields:
|
||||||
-- n: name
|
-- n: name
|
||||||
-- c: count
|
-- c: count
|
||||||
|
-- q: quality (optional)
|
||||||
-- h: health (optional)
|
-- h: health (optional)
|
||||||
-- d: durability (optional)
|
-- d: durability (optional)
|
||||||
-- a: ammo count (optional)
|
-- a: ammo count (optional)
|
||||||
@@ -141,6 +120,7 @@ function serialize.serialize_item_stack(slot, entry)
|
|||||||
|
|
||||||
entry.n = slot.name
|
entry.n = slot.name
|
||||||
entry.c = slot.count
|
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.health < 1 then entry.h = slot.health end
|
||||||
if slot.durability then entry.d = slot.durability end
|
if slot.durability then entry.d = slot.durability end
|
||||||
if slot.type == "ammo" then entry.a = slot.ammo 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,
|
name = entry.n,
|
||||||
count = entry.c,
|
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.h then item_stack.health = entry.h end
|
||||||
if entry.d then item_stack.durability = entry.d end
|
if entry.d then item_stack.durability = entry.d end
|
||||||
if entry.a then item_stack.ammo = entry.a end
|
if entry.a then item_stack.ammo = entry.a end
|
||||||
@@ -244,7 +225,7 @@ function serialize.serialize_inventory(inventory)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if item.n or item.f or item.e then
|
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
|
if item_serialized == previous_serialized then
|
||||||
local previous_item = serialized.i[#serialized.i]
|
local previous_item = serialized.i[#serialized.i]
|
||||||
previous_item.r = (previous_item.r or 0) + 1
|
previous_item.r = (previous_item.r or 0) + 1
|
||||||
@@ -305,5 +286,4 @@ function serialize.deserialize_inventory(inventory, serialized)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return serialize
|
return serialize
|
||||||
Reference in new issue
Block a user