mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 23:04:52 +09:00
Add script data to allow persistence of unknown recipes
This commit is contained in:
3 files changed
+33
-8
No files matched your search
@@ -344,8 +344,9 @@ function serialize.deserialize_crafting_queue(player, serialized)
|
||||
end
|
||||
|
||||
---@param player LuaPlayer
|
||||
---@param failed string[]?
|
||||
---@return string?
|
||||
function serialize.serialize_crafting_notifications(player)
|
||||
function serialize.serialize_crafting_notifications(player, failed)
|
||||
-- Get all unlocked recipes
|
||||
local recipes = {}
|
||||
for _, recipe in pairs(player.force.recipes) do
|
||||
@@ -360,8 +361,8 @@ function serialize.serialize_crafting_notifications(player)
|
||||
end
|
||||
|
||||
-- Convert to a list of names
|
||||
local index = 1
|
||||
local recipe_names = {}
|
||||
local recipe_names = failed or {} -- Includes previous recipes if any
|
||||
local index = #recipe_names + 1
|
||||
for name in pairs(recipes) do
|
||||
recipe_names[index] = name
|
||||
index = index + 1
|
||||
@@ -372,6 +373,7 @@ end
|
||||
|
||||
--- @param player LuaPlayer
|
||||
--- @param serialized string
|
||||
--- @return string[]? failed Recipe names that do not exist
|
||||
function serialize.deserialize_crafting_notifications(player, serialized)
|
||||
-- Decode the recipe name strings
|
||||
local recipe_names = helpers.json_to_table(assert(helpers.decode_string(serialized)))
|
||||
@@ -379,6 +381,7 @@ function serialize.deserialize_crafting_notifications(player, serialized)
|
||||
local clear_notification = player.clear_recipe_notification
|
||||
local add_notification = player.add_recipe_notification
|
||||
local notifications = {}
|
||||
local failed = {}
|
||||
|
||||
-- Assume all recipes need a notification
|
||||
for _, recipe in pairs(player.force.recipes) do
|
||||
@@ -391,6 +394,9 @@ function serialize.deserialize_crafting_notifications(player, serialized)
|
||||
for _, recipe_name in pairs(recipe_names) do
|
||||
if notifications[recipe_name] then
|
||||
notifications[recipe_name] = false
|
||||
else
|
||||
-- Recipe does not exist on this server
|
||||
failed[#failed+1] = recipe_name
|
||||
end
|
||||
end
|
||||
|
||||
@@ -408,6 +414,9 @@ function serialize.deserialize_crafting_notifications(player, serialized)
|
||||
add_notification(recipe_name)
|
||||
end
|
||||
end
|
||||
|
||||
-- Return failed recipes if any
|
||||
return next(failed) and failed or nil
|
||||
end
|
||||
|
||||
local controller_to_name = {}
|
||||
@@ -416,6 +425,7 @@ for name, value in pairs(defines.controllers) do
|
||||
end
|
||||
|
||||
--- @class SerializedPlayerData
|
||||
--- @field generation number
|
||||
--- @field name string
|
||||
--- @field controller string
|
||||
--- @field color Color
|
||||
@@ -433,10 +443,12 @@ end
|
||||
--- @field recipe_notifications string?
|
||||
|
||||
--- @param player LuaPlayer
|
||||
--- @param failed_deserialization FailedDeserializationPlayerData
|
||||
--- @return SerializedPlayerData
|
||||
function serialize.serialize_player(player)
|
||||
function serialize.serialize_player(player, failed_deserialization)
|
||||
--- @type SerializedPlayerData
|
||||
local serialized = {
|
||||
generation = 0, -- Gets replaced later
|
||||
controller = controller_to_name[player.controller_type],
|
||||
name = player.name,
|
||||
color = player.color,
|
||||
@@ -492,15 +504,21 @@ function serialize.serialize_player(player)
|
||||
|
||||
-- Serialize recipe notifications
|
||||
if recipe_notifications_api then
|
||||
serialized.recipe_notifications = serialize.serialize_crafting_notifications(player)
|
||||
serialized.recipe_notifications = serialize.serialize_crafting_notifications(player, failed_deserialization.recipe_notifications)
|
||||
end
|
||||
|
||||
return serialized
|
||||
end
|
||||
|
||||
--- @class FailedDeserializationPlayerData
|
||||
--- @field recipe_notifications string[]?
|
||||
|
||||
--- @param player LuaPlayer
|
||||
--- @param serialized SerializedPlayerData
|
||||
--- @return FailedDeserializationPlayerData?
|
||||
function serialize.deserialize_player(player, serialized)
|
||||
local failed_deserialization = {}
|
||||
|
||||
if player.controller_type ~= defines.controllers[serialized.controller] or serialized.controller == "ghost" then
|
||||
-- If targeting the character or ghost controller then create a character
|
||||
if serialized.controller == "character" or serialized.controller == "ghost" then
|
||||
@@ -582,8 +600,11 @@ function serialize.deserialize_player(player, serialized)
|
||||
|
||||
-- Deserialize recipe notifications
|
||||
if recipe_notifications_api and serialized.recipe_notifications then
|
||||
serialize.deserialize_crafting_notifications(player, serialized.recipe_notifications)
|
||||
failed_deserialization.recipe_notifications =
|
||||
serialize.deserialize_crafting_notifications(player, serialized.recipe_notifications)
|
||||
end
|
||||
|
||||
return next(failed_deserialization) and failed_deserialization or nil
|
||||
end
|
||||
|
||||
return serialize
|
||||
Reference in new issue
Block a user