Merge uploads by the components they were serialized with

The controller merged uploads using the uploading instance's current config. An upload serialized under different settings, for example one sent from the save right after an admin turned on sync_inventories and restarted the instance, was then merged as if it held a character and wiped the stored one. Uploads now carry the components they were serialized with, the controller merges by those and drops the key before storing.

The scenario keeps the components a player's synced session started with and serializes their upload with them, so changing the options while a player is online takes effect the next time they join. A player whose session lacks a component that is now synced downloads again even if the generation matches. Downloads snapshot the components when they start, so a change during a download can't mix two sets.

A failed acquire unsyncs the player again regardless of the inventories setting, as before this branch.

The god and dead player inventories move from the inventories component to the controller one, matching what the scenario syncs, which removes the special case in mergePlayerData.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-10-17 15:25:06 +00:00
1 parent 8487c6d923
commit b0e07ea73b
11 files changed
+150 -58

No files matched your search

+30 -15
View File
@@ -3,10 +3,11 @@ import type { IpcPlayerData } from "./messages.js";
/** Parts of the player data an instance can choose to sync, and the fields of the player data holding them. */
export const components = {
controller: ["controller", "ticks_to_respawn", "cheat_mode"],
// inventories holds the god inventory and the hidden ghost inventory, both belong to the controller
controller: ["controller", "ticks_to_respawn", "cheat_mode", "inventories"],
force: ["force"],
appearance: ["color", "chat_color", "tag"],
inventories: ["character", "inventories", "crafting_queue"],
inventories: ["character", "crafting_queue"],
logistics: ["personal_logistic_slots"],
quick_bar: ["quick_bar", "hotbar"],
settings: ["flashlight", "shortcuts", "game_view_settings"],
@@ -19,15 +20,15 @@ export type Component = keyof typeof components;
const characterFields = ["character", "crafting_queue", "personal_logistic_slots"] as const;
/**
* Components synced on an instance.
* Build the set of synced components.
*
* The controller is only synced together with the inventories, as
* switching controller can destroy the character holding them.
*/
export function enabledComponents(config: lib.InstanceConfig) {
function componentSet(isEnabled: (component: Component) => boolean) {
const enabled = new Set<Component>();
for (const component of Object.keys(components) as Component[]) {
if (config.get(`player_sync.sync_${component}`)) {
if (isEnabled(component)) {
enabled.add(component);
}
}
@@ -37,6 +38,23 @@ export function enabledComponents(config: lib.InstanceConfig) {
return enabled;
}
/** Components synced on an instance. */
export function enabledComponents(config: lib.InstanceConfig) {
return componentSet(component => Boolean(config.get(`player_sync.sync_${component}`)));
}
/**
* Components an upload was serialized with, all of them when not given.
*
* Lua may encode an empty table as an array, which reads as no components.
*/
export function uploadedComponents(uploaded: Record<string, boolean> | unknown[] | undefined) {
if (uploaded === undefined) {
return componentSet(() => true);
}
return componentSet(component => !Array.isArray(uploaded) && uploaded[component] === true);
}
/** Remove the fields of components which are not synced. */
export function stripPlayerData(playerData: IpcPlayerData, enabled: Set<Component>) {
const fields = playerData as Record<string, unknown>;
@@ -53,13 +71,13 @@ export function stripPlayerData(playerData: IpcPlayerData, enabled: Set<Componen
/**
* Combine data uploaded by an instance with the stored data.
*
* Components the instance does not sync keep the stored fields. Without
* the controller synced the god and ghost inventories are left as the
* synced controller had them, and character fields are only taken from a
* Components the upload was not serialized with keep the stored fields.
* Without the controller synced character fields are only taken from a
* player which has a character.
*/
export function mergePlayerData(stored: IpcPlayerData | undefined, uploaded: IpcPlayerData, enabled: Set<Component>) {
const merged: Record<string, unknown> = { ...uploaded };
export function mergePlayerData(stored: IpcPlayerData | undefined, uploaded: IpcPlayerData) {
const { components: uploadComponents, ...merged } = uploaded as IpcPlayerData & Record<string, unknown>;
const enabled = uploadedComponents(uploadComponents);
function keep(keys: readonly string[]) {
for (const key of keys) {
const value = (stored as Record<string, unknown> | undefined)?.[key];
@@ -76,11 +94,8 @@ export function mergePlayerData(stored: IpcPlayerData | undefined, uploaded: Ipc
keep(keys);
}
}
if (!enabled.has("controller")) {
keep(["inventories"]);
if (!uploaded.character) {
keep(characterFields);
}
if (!enabled.has("controller") && !uploaded.character) {
keep(characterFields);
}
return merged as IpcPlayerData;
}