mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Expand compat to 0.17 (#711)
This commit is contained in:
1 parent
87746edbf5
commit
ff9ba0271f
8 files changed
+138
-89
No files matched your search
@@ -1,14 +1,30 @@
|
||||
--[[
|
||||
Adds various functions used to ensure compatibly between version including some polyfills
|
||||
Adds various functions used to ensure compatibly between version including some polyfills.
|
||||
|
||||
Implementation Guidelines
|
||||
- If a function is only available at runtime in one version, then all versions must be made to error.
|
||||
- Where possible, directly assign the function / table to the member of compat.
|
||||
- When not possible, then it should replace itself with a direct assignment asap.
|
||||
- Versions are handled in ascending order covering all supported versions and ending with an error.
|
||||
]]
|
||||
|
||||
local json = require("json")
|
||||
local lib_json = require("json")
|
||||
|
||||
--- @diagnostic disable: deprecated
|
||||
|
||||
--- @class LibCompat
|
||||
--- @field script_data table
|
||||
--- @field prototypes LuaPrototypes
|
||||
--- @field active_mods table<string, string>
|
||||
--- @field write_file fun(filename: string, data: LocalisedString, append: boolean?, for_player: uint?)
|
||||
--- @field table_to_json fun(data: table): string
|
||||
--- @field json_to_table fun(json: string): AnyBasic?
|
||||
local compat = {}
|
||||
|
||||
--- @class LibCompatMt
|
||||
--- @package
|
||||
local compat_mt = {}
|
||||
|
||||
--- @alias VersionTable { [1]: number, [2]: number, [3]: number }
|
||||
|
||||
--- Concert a version string into a version table
|
||||
@@ -37,12 +53,6 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
--- Raise an error because the current version is unsupported
|
||||
--- @return any
|
||||
local function unsupported_version()
|
||||
error("Unsupported version: " .. current_version_str, 3)
|
||||
end
|
||||
|
||||
--- Returns true if the current version is equal to the provided version
|
||||
--- @param version string Version to compare against
|
||||
--- @return boolean # True if versions are equal
|
||||
@@ -86,85 +96,127 @@ function compat.version_le(version)
|
||||
return true
|
||||
end
|
||||
|
||||
--- The major versions of factorio we support
|
||||
local major_v2 = compat.version_ge("2.0.0")
|
||||
local major_v1_1 = not major_v2 and compat.version_ge("1.1.0")
|
||||
-- local major_v1 = not major_v1_1 and compat.version_ge("1.0.0")
|
||||
--- The versions of factorio we support
|
||||
local v2_0 = compat.version_ge("2.0")
|
||||
local v1_1 = not v2_0 and compat.version_ge("1.1")
|
||||
local v1_0 = not v1_1 and compat.version_ge("1.0")
|
||||
local v0_18 = not v1_0 and compat.version_ge("0.18")
|
||||
local v0_17 = not v0_18 and compat.version_ge("0.17")
|
||||
|
||||
--- Returns the table reference used to store script data
|
||||
--- @return table script_data
|
||||
function compat.script_data()
|
||||
if major_v1_1 then
|
||||
--- @diagnostic disable-next-line
|
||||
return global
|
||||
elseif major_v2 then
|
||||
return storage
|
||||
end
|
||||
return unsupported_version()
|
||||
local v2 = compat.version_ge("2.0")
|
||||
local v1 = not v2 and compat.version_ge("1.0")
|
||||
local v0 = not v1 and compat.version_ge("0.17")
|
||||
|
||||
--- Raise an error because the current version is unsupported
|
||||
local function unsupported_version()
|
||||
error("Unsupported version: " .. current_version_str, 3)
|
||||
end
|
||||
|
||||
--- Returns the table reference used to store prototypes
|
||||
--- @param category string Category of prototype to access, eg "item", "fluid", "recipe"
|
||||
--- @return table # Table of prototype data
|
||||
function compat.prototype_data(category)
|
||||
if major_v1_1 then
|
||||
return game[category .. "_prototypes"]
|
||||
elseif major_v2 then
|
||||
return prototypes[category]
|
||||
end
|
||||
return unsupported_version()
|
||||
--- Raise an error because the function is only available at runtime
|
||||
local function runtime_only()
|
||||
error("To maintain compatibly, this value can only be used during runtime.", 3)
|
||||
end
|
||||
|
||||
--- Returns the table reference used to list active mods
|
||||
--- @return table # Table of active mods
|
||||
function compat.active_mods()
|
||||
if major_v1_1 or major_v2 then
|
||||
--- Keys of this table will raise an unsupported error when accessed
|
||||
local unsupported_properties = {}
|
||||
|
||||
--- Keys of this table can only be accessed at runtime
|
||||
--- The value is a function which returns the real value
|
||||
local runtime_properties = {}
|
||||
|
||||
--- Handles the indexing of the tables above
|
||||
function compat_mt:__index(key)
|
||||
if unsupported_properties[key] then
|
||||
return unsupported_version()
|
||||
elseif runtime_properties[key] then
|
||||
if game == nil then
|
||||
return runtime_only()
|
||||
end
|
||||
local value = runtime_properties[key]()
|
||||
if value == nil then
|
||||
return unsupported_version()
|
||||
end
|
||||
rawset(self, key, value)
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
--- Prototype data contains all the definitions for the game, it is only available during runtime
|
||||
function runtime_properties.prototypes()
|
||||
if v0 or v1 then
|
||||
--- Lazy loading of prototype data
|
||||
return setmetatable({}, {
|
||||
-- Prevent iteration due to lazy loading, its a really rare use case
|
||||
__pairs = function() error("Pairs not supported in " .. current_version_str, 2) end,
|
||||
__ipairs = function() error("IPairs not supported in " .. current_version_str, 2) end,
|
||||
__index = function(self, key)
|
||||
local value = game[key .. "_prototypes"]
|
||||
rawset(self, key, value)
|
||||
return value
|
||||
end
|
||||
})
|
||||
elseif v2 then
|
||||
return prototypes
|
||||
end
|
||||
end
|
||||
|
||||
--- List of all active mods
|
||||
function runtime_properties.active_mods()
|
||||
if v0_17 then
|
||||
return game.active_mods
|
||||
elseif v0 or v1 or v2 then
|
||||
return script.active_mods
|
||||
end
|
||||
return unsupported_version()
|
||||
end
|
||||
|
||||
--- @param tbl table
|
||||
--- @return string
|
||||
function compat.table_to_json(tbl)
|
||||
return unsupported_version()
|
||||
--- Writes a string to file
|
||||
function runtime_properties.write_file()
|
||||
if v0 or v1 then
|
||||
return game.write_file
|
||||
elseif v2 then
|
||||
return helpers.write_file
|
||||
end
|
||||
end
|
||||
|
||||
--- @param json string
|
||||
--- @return table
|
||||
function compat.json_to_table(json)
|
||||
return unsupported_version()
|
||||
end
|
||||
|
||||
--- @param filename string
|
||||
--- @param data LocalisedString
|
||||
--- @param append boolean?
|
||||
--- @param for_player uint?
|
||||
function compat.write_file(filename, data, append, for_player)
|
||||
unsupported_version()
|
||||
end
|
||||
|
||||
--- Select the appropriate polyfill implementation
|
||||
if major_v1_1 then
|
||||
--- Convert tables to and from json, we have backported this to work before runtime
|
||||
if v0 or v1 then
|
||||
compat.table_to_json = function(tbl)
|
||||
if game then
|
||||
compat.table_to_json = game.table_to_json
|
||||
return game.table_to_json(tbl)
|
||||
end
|
||||
return json.encode(tbl)
|
||||
return lib_json.encode(tbl)
|
||||
end
|
||||
compat.json_to_table = function(json)
|
||||
if game then
|
||||
compat.json_to_table = game.json_to_table
|
||||
return game.json_to_table(json)
|
||||
end
|
||||
return json.decode(json)
|
||||
return lib_json.decode(json)
|
||||
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 v2 then
|
||||
compat.table_to_json = helpers.table_to_json
|
||||
compat.json_to_table = helpers.json_to_table
|
||||
compat.write_file = helpers.write_file
|
||||
else
|
||||
unsupported_properties.table_to_json = true
|
||||
unsupported_properties.json_to_table = true
|
||||
end
|
||||
|
||||
return compat
|
||||
--- Script data refers to lua data which is persisted between loads
|
||||
--- This is called immediately, during init and during load
|
||||
local function set_script_data()
|
||||
if v0 or v1 then
|
||||
--- @diagnostic disable-next-line
|
||||
compat.script_data = global
|
||||
elseif v2 then
|
||||
compat.script_data = storage
|
||||
else
|
||||
unsupported_properties.script_data = true
|
||||
end
|
||||
end
|
||||
|
||||
set_script_data()
|
||||
compat.on_init = set_script_data --- @package
|
||||
compat.on_load = set_script_data --- @package
|
||||
|
||||
return setmetatable(compat, compat_mt)
|
||||
Reference in new issue
Block a user