Merge pull request #1012 from bbassie/feature/inventory-sync-tools

Add inspect and modify tools for inventory sync
This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-10-20 00:43:33 +01:00
commit 4128f75328
17 files changed
+1861 -65

No files matched your search

@@ -29,7 +29,7 @@ local function download_inventory(player_name, data, number, total, components)
end
if record.restart then
rcon.print("Restarting outdated download")
player_sync.initiate_inventory_download(player, player_record, record.generation)
player_sync.initiate_inventory_download(player, player_record, record.generation, record.epoch)
return
end
@@ -55,6 +55,7 @@ local function download_inventory(player_name, data, number, total, components)
player_record.sync = true
player_record.components = record.components
player_record.generation = record.generation
player_record.epoch = record.epoch
return
end
+30 -4
View File
@@ -112,6 +112,8 @@ function player_sync.serialize_player(player, player_record)
local failed_deserialization = get_script_data().failed_deserialization[player.name] or {}
local serialized_player = serialize.serialize_player(player, failed_deserialization, player_record.components)
serialized_player.generation = player_record.generation
-- Always sent so the controller can tell this instance supports epochs
serialized_player.epoch = player_record.epoch or 0
-- Leaving on the tick of a download the temporary bonus is still there and must not be uploaded. Removing it
-- from the character here would shrink the main inventory before the armor's bonus applies and spill items.
@@ -299,9 +301,15 @@ function player_sync.sync_player(acquire_response)
end
local script_data = get_script_data()
-- Epoch is bumped when an admin replaces or deletes the stored data
local epoch = acquire_response.epoch or 0
local finished_record = script_data.finished_downloads[acquire_response.player_name]
if finished_record then
if acquire_response.status == "acquired" and finished_record.generation == acquire_response.generation then
if
acquire_response.status == "acquired"
and finished_record.generation == acquire_response.generation
and (finished_record.epoch or 0) == epoch
then
player_sync.finish_download(player, finished_record)
return
end
@@ -314,7 +322,11 @@ function player_sync.sync_player(acquire_response)
local download_record = script_data.active_downloads[acquire_response.player_name]
if download_record then
-- Ignore the response if it affirms the current active download
if acquire_response.status == "acquired" and download_record.generation == acquire_response.generation then
if
acquire_response.status == "acquired"
and download_record.generation == acquire_response.generation
and (download_record.epoch or 0) == epoch
then
game.print("Continuing active download")
return
end
@@ -335,6 +347,8 @@ function player_sync.sync_player(acquire_response)
-- download was started. Restart it.
game.print("Restarting download")
download_record.restart = true
download_record.generation = acquire_response.generation
download_record.epoch = epoch
return
end
@@ -363,15 +377,17 @@ function player_sync.sync_player(acquire_response)
return
end
local stale = (player_record.epoch or 0) ~= epoch
if
acquire_response.has_data
and (
not player_record.sync
or stale
or acquire_response.generation > player_record.generation
or serialize.newly_synced(player_record.components, script_data.components)
)
then
player_sync.initiate_inventory_download(player, player_record, acquire_response.generation)
player_sync.initiate_inventory_download(player, player_record, acquire_response.generation, epoch)
else
if acquire_response.generation == 0 then
@@ -385,6 +401,14 @@ function player_sync.sync_player(acquire_response)
start_sync(player_record, script_data.components)
player_record.dirty = true
player_record.sync_in_progress = false
-- Deleted data keeps its generation and epoch so the next upload is accepted as the new baseline
player_record.epoch = epoch
-- A copy from another epoch has nothing to do with the stored generation
if stale then
player_record.generation = acquire_response.generation
else
player_record.generation = math.max(player_record.generation, acquire_response.generation)
end
end
end
@@ -408,6 +432,7 @@ function player_sync.finish_download(player, finished_record)
player_record.sync_in_progress = false
start_sync(player_record, finished_record.components)
player_record.generation = finished_record.generation
player_record.epoch = finished_record.epoch
end
-- Download inventory from controller
@@ -514,12 +539,13 @@ function player_sync.confirm_upload(player_name, generation)
player_record.dirty = false
end
function player_sync.initiate_inventory_download(player, player_record, generation)
function player_sync.initiate_inventory_download(player, player_record, generation, epoch)
player.print("Initiating inventory download...")
local record = {
started = game.ticks_played,
last_active = game.ticks_played,
generation = generation,
epoch = epoch,
data = "",
}
local script_data = get_script_data()
+13 -8
View File
@@ -70,7 +70,10 @@ end
--- Restore the stats of a character, the inventories are restored with place_items
function serialize.deserialize_character(character, serialized)
for _, key in pairs(character_stat_keys) do
character[key] = serialized[key]
-- Missing from imports and from data saved by another Factorio version
if serialized[key] ~= nil then
character[key] = serialized[key]
end
end
end
@@ -382,13 +385,13 @@ function serialize.deserialize_personal_logistic_slots(player, serialized)
logistic_point.remove_section(i)
end
-- If this is an array instead of a table, migrate to v2 format
-- Data from before 2.0 maps slot indices to requests, migrate it to a section
if next(serialized) == nil then
return
elseif serialized[1] ~= nil then
elseif serialized.sections == nil then
local section = logistic_point.add_section()
for i, slot in pairs(serialized) do
section.set_slot(i, {
section.set_slot(tonumber(i), {
value = {
name = slot.name,
quality = "normal",
@@ -936,6 +939,7 @@ end
--- @class SerializedPlayerData
--- @field generation number
--- @field epoch number?
--- @field name string
--- @field controller string?
--- @field color Color?
@@ -1360,10 +1364,11 @@ function serialize.deserialize_player(player, serialized, components, local_cont
end
end
if syncs(components, "appearance") and serialized.color then
player.color = serialized.color
player.chat_color = serialized.chat_color
player.tag = serialized.tag
if syncs(components, "appearance") then
-- Imports may supply any subset of these
if serialized.color then player.color = serialized.color end
if serialized.chat_color then player.chat_color = serialized.chat_color end
if serialized.tag then player.tag = serialized.tag end
end
if syncs(components, "force") and serialized.force then
player.force = serialized.force