mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 07:04:53 +09:00
With the controller off, a god or ghost player was still turned into a spectator while downloading, which threw away the local state the sync is meant to leave alone. Only spectate a player without a character when the controller is synced, and skip the ghost round trip when the ghost is the local state. A ghost on an instance that syncs inventories but not the controller never received the stored character, so after respawning it uploaded an empty one over it. Respawn such a ghost straight away when there is a stored character to give it. Clear the saved local controller when the player picks a temporary inventory after a failed download, otherwise a later download restores the stale controller over the temporary character. An empty download only gives a character when the controller is synced or the player was spectated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
2.6 KiB
Lua
82 lines
2.6 KiB
Lua
local progress_dialog = require("modules/player_sync/gui/progress_dialog")
|
|
local ensure_character = require("modules/player_sync/ensure_character")
|
|
local restore_position = require("modules/player_sync/restore_position")
|
|
local get_script_data = require("modules/player_sync/get_script_data")
|
|
local serialize = require("modules/player_sync/serialize")
|
|
|
|
|
|
local function download_inventory(player_name, data, number, total)
|
|
local player = game.get_player(player_name)
|
|
if player == nil then
|
|
rcon.print("Player " .. player_name .. " does not exist")
|
|
return
|
|
end
|
|
|
|
local script_data = get_script_data()
|
|
local record = script_data.active_downloads[player_name]
|
|
if record == nil then
|
|
rcon.print("No active download is in progress for " .. player_name)
|
|
return
|
|
end
|
|
|
|
local player_record = script_data.players[player_name]
|
|
if record.restart then
|
|
rcon.print("Restarting outdated download")
|
|
player_sync.initiate_inventory_download(player, player_record, record.generation)
|
|
return
|
|
end
|
|
|
|
if total == 0 then
|
|
-- Download was requested but the player had no data stored.
|
|
log("ERROR: Player sync failed, got empty player data for " .. player.name .. " from the controller")
|
|
player.print("ERROR: Player sync failed, got empty player data from the controller")
|
|
|
|
progress_dialog.remove(player)
|
|
|
|
-- Give the player a character and pretend that's the synced player data
|
|
if serialize.syncs(script_data.components, "controller") or player_record.local_controller then
|
|
ensure_character(player)
|
|
end
|
|
player_record.local_controller = nil
|
|
|
|
-- Restore player position and driving state
|
|
restore_position(player, record)
|
|
|
|
script_data.active_downloads[player_name] = nil
|
|
player_record.dirty = player.connected
|
|
player_record.sync = true
|
|
player_record.generation = record.generation
|
|
return
|
|
end
|
|
|
|
record.data = record.data .. data
|
|
|
|
-- Show progress in console
|
|
-- player.print("Downloaded "..(number + 1).."/"..total.." parts")
|
|
if number ~= total then
|
|
-- Show progress in GUI
|
|
progress_dialog.display(player, number, total)
|
|
|
|
-- Update activity to indicate this session is still going
|
|
record.last_active = game.ticks_played
|
|
return
|
|
end
|
|
|
|
-- Download is complete
|
|
progress_dialog.remove(player)
|
|
|
|
-- Remove download session and store finished download
|
|
script_data.active_downloads[player_name] = nil
|
|
script_data.finished_downloads[player_name] = record
|
|
|
|
local ticks = game.ticks_played - record.started
|
|
player.print("Imported player data in " .. ticks .. " ticks")
|
|
log("Imported player data for " .. player_name .. " in " .. ticks .. " ticks")
|
|
|
|
if player.connected then
|
|
player_sync.finish_download(player, record)
|
|
end
|
|
end
|
|
|
|
return download_inventory
|