diff --git a/README.md b/README.md index 272c188b..22bb69ce 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ These are the plugins supported and maintained by the Clusterio developers: - [Statistics Exporter](/plugins/statistics_exporter/README.md): collect in-game statistics from all the servers and makes it available to the Prometheus endpoint on the master server. - [Subspace Storage](https://github.com/clusterio/subspace_storage): Provide shared storage that can transport items between servers via teleport chests. - [Player Auth](/plugins/player_auth/README.md): Provides authentication to the cluster via logging into a Factorio server. +- [Inventory Sync](/plugins/inventory_sync/README.md): Synchronizes your inventory between servers. There's also plugins developed and maintained by the community: diff --git a/plugins/inventory_sync/.npmignore b/plugins/inventory_sync/.npmignore new file mode 100644 index 00000000..6de001d8 --- /dev/null +++ b/plugins/inventory_sync/.npmignore @@ -0,0 +1 @@ +webpack.config.js diff --git a/plugins/inventory_sync/README.md b/plugins/inventory_sync/README.md new file mode 100644 index 00000000..0ef56ab1 --- /dev/null +++ b/plugins/inventory_sync/README.md @@ -0,0 +1,50 @@ +# Clusterio inventory sync plugin + +Carry over player inventory between servers + +As a player, it mostly just works. Most important to know is that factorio data transfer is slow, which means if you have "big" items (blueprints mostly) in your inventory it will take a long time to transfer when joining servers. For this reason it is recommended to keep blueprints in the blueprint library. + +A note on crashing servers: + +When a server crashes while you are online there won't be time to immediately upload the inventory. Instead, the inventory is uploaded as soon as the server restarts. This means if you have been playing on a different server from the one that crashed and gathered/consumed items but are offline at the time of the first server coming online again your inventory will reset to the autosave. + +## Installation + +Run the following commands in the folder Clusterio is installed to: + + npm install @clusterio/plugin-inventory_sync + npx clusteriomaster plugin add @clusterio/plugin-inventory_sync + +Substitute clusteriomaster with clusterioslave or clusterioctl if this a dedicated slave or ctl installation respectively. + +## Method of operation + +This plugin does event based synchronization of inventories. The data flow is as follows: + +1. Player joins server, gets put in spectator mode + +2. Lua script asks instance for inventory + +3. Instance asks master for inventory + +4. Master does not find inventory, and responds to instance with undefined + +5. Instance sets player to survival + +6. Player keeps playing, and eventually leaves. Inventory is serialized and sent to instance through stdout/file + +7. Instance sends inventory to master + +8. Player joins a different server. Lua sends a request to instance, puts player into spectator mode + +9. Instance sends request to master + +10. Master responds with json data + +11. Instance calls command in scenario script using rcon + +12. Once the entire command is transfered, the scenario script clears the old inventory, applies the new inventory from the scenario script and sets the player to survival mode. + +Communication between the server and instance goes over stdout or rcon, depending on the size of the data. +Communication between the instance and the master server goes over websockets. +Overall, we are able to achieve a latency between 3 and infinite ticks from server join, depending on the size of the inventory. The major limiter is rcon transfer speeds with larger inventories, especially if they contain blueprints. diff --git a/plugins/inventory_sync/info.js b/plugins/inventory_sync/info.js new file mode 100644 index 00000000..5c80e838 --- /dev/null +++ b/plugins/inventory_sync/info.js @@ -0,0 +1,93 @@ +"use strict"; +const libLink = require("@clusterio/lib/link"); +const libConfig = require("@clusterio/lib/config"); +const libUsers = require("@clusterio/lib/users"); + +class MasterConfigGroup extends libConfig.PluginConfigGroup { } +MasterConfigGroup.defaultAccess = ["master", "slave", "control"]; +MasterConfigGroup.groupName = "inventory_sync"; +MasterConfigGroup.define({ + name: "autosave_interval", + title: "Autosave Interval", + description: "Interval the player data is autosaved at in seconds.", + type: "number", + initial_value: 600, // 10 minutes +}); +MasterConfigGroup.finalize(); + +class InstanceConfigGroup extends libConfig.PluginConfigGroup { } +InstanceConfigGroup.defaultAccess = ["master", "slave", "control"]; +InstanceConfigGroup.groupName = "inventory_sync"; +InstanceConfigGroup.define({ + name: "rcon_chunk_size", + title: "Rcon inventory chunk size", + description: "Divide inventories into multiple chunks before sending with rcon to prevent blocking the pipe", + type: "number", + initial_value: 1000, +}); +InstanceConfigGroup.finalize(); + +libUsers.definePermission({ + name: "inventory_sync.inventory.view", + title: "View player inventories", + description: "View player inventories", + grantByDefault: true, +}); + +module.exports = { + name: "inventory_sync", + title: "Inventory sync", + description: "Synchronizes players inventories between instances", + + instanceEntrypoint: "instance", + InstanceConfigGroup, + + masterEntrypoint: "master", + MasterConfigGroup, + + webEntrypoint: "./web", + routes: ["/inventory"], + + messages: { + upload: new libLink.Request({ + type: "inventory_sync:upload", + links: ["instance-slave", "slave-master"], + forwardTo: "master", + requestProperties: { + "instance_id": { type: "integer" }, + "instance_name": { type: "string" }, + "player_name": { type: "string" }, + "inventory": { type: "object" }, + }, + }), + download: new libLink.Request({ + type: "inventory_sync:download", + links: ["instance-slave", "slave-master"], + forwardTo: "master", + requestProperties: { + "player_name": { type: "string" }, + }, + responseProperties: { + "player_name": { type: "string" }, + "inventory": { type: "object" }, + "new_player": { type: "boolean" }, + }, + }), + databaseStats: new libLink.Request({ + type: "inventory_sync:databaseStats", + links: ["control-master"], + permission: "inventory_sync.inventory.view", + responseProperties: { + "database_size": { type: "integer" }, + "database_entries": { type: "integer" }, + "largest_entry": { + type: "object", + properties: { + name: { type: "string" }, + size: { type: "number" }, + }, + }, + }, + }), + }, +}; diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js new file mode 100644 index 00000000..7dbc83ff --- /dev/null +++ b/plugins/inventory_sync/instance.js @@ -0,0 +1,61 @@ +/** + * @module + */ +"use strict"; +const libPlugin = require("@clusterio/lib/plugin"); +const libLuaTools = require("@clusterio/lib/lua_tools"); + +/** + * Splits string into array of strings with max of a certain length + * @param {Number} chunkSize - Max length of each chunk + * @param {String} string - String to split into chunks + * @returns {String[]} Chunks + */ +function chunkify(chunkSize, string) { + return string.match(new RegExp(`.{1,${chunkSize}}`, "g")); +} + +class InstancePlugin extends libPlugin.BaseInstancePlugin { + async init() { + // Handle IPC from scenario script + this.instance.server.on("ipc-inventory_sync_upload", content => this.handleUpload(content) + .catch(err => this.logger.error(`Error handling ipc-inventory_sync_upload:\n${err.stack}`))); + this.instance.server.on("ipc-inventory_sync_download", content => this.handleDownload(content) + .catch(err => this.logger.error(`Error handling ipc-inventory_sync_download:\n${err.stack}`))); + } + + async handleUpload(player) { + this.logger.verbose(`Uploading ${player.name} (${JSON.stringify(player).length / 1000}kB)`); + this.info.messages.upload.send(this.instance, { + instance_id: this.instance.id, + instance_name: this.instance.name, + player_name: player.name, + inventory: player, + }); + } + + async handleDownload(player) { + this.logger.verbose(`Downloading ${player.player_name}`); + + // Retrieves inventory data. If the master is offline it waits until it comes back online. + let response = await this.info.messages.download.send(this.instance, { + player_name: player.player_name, + }); + if (response.new_player) { + await this.sendRcon(`/sc inventory_sync.welcome_new_player('${response.player_name}')`); + } else if (response.inventory) { + const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size"); + const chunks = chunkify(chunkSize, JSON.stringify(response.inventory)); + this.logger.verbose(`Sending inventory for ${player.player_name} in ${chunks.length} chunks`); + for (let i = 0; i < chunks.length; i++) { + // this.logger.verbose(`Sending chunk ${i+1} of ${chunks.length}`) + await this.sendRcon(`/sc inventory_sync.download_inventory('${response.player_name}',` + + `'${libLuaTools.escapeString(chunks[i])}', ${i + 1}, ${chunks.length})`); + } + } + } +} + +module.exports = { + InstancePlugin, +}; diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js new file mode 100644 index 00000000..789a130c --- /dev/null +++ b/plugins/inventory_sync/master.js @@ -0,0 +1,82 @@ +"use strict"; +const fs = require("fs-extra"); +const path = require("path"); + +const libPlugin = require("@clusterio/lib/plugin"); + +async function loadDatabase(config, logger) { + let itemsPath = path.resolve(config.get("master.database_directory"), "inventories.json"); + logger.verbose(`Loading ${itemsPath}`); + try { + let content = await fs.readFile(itemsPath); + return new Map(JSON.parse(content)); + + } catch (err) { + if (err.code === "ENOENT") { + logger.verbose("Creating new inventory database"); + return new Map(); + } + throw err; + } +} + +async function saveDatabase(masterConfig, inventories, logger) { + if (inventories) { + let file = path.resolve(masterConfig.get("master.database_directory"), "inventories.json"); + logger.verbose(`writing ${file}`); + let content = JSON.stringify(Array.from(inventories)); + await fs.outputFile(file, content); + } +} + +class MasterPlugin extends libPlugin.BaseMasterPlugin { + async init() { + this.inventories = await loadDatabase(this.master.config, this.logger); + this.autosaveId = setInterval(() => { + saveDatabase(this.master.config, this.inventories, this.logger).catch(err => { + this.logger.error(`Unexpected error autosaving inventories:\n${err.stack}`); + }); + }, this.master.config.get("inventory_sync.autosave_interval") * 1000); + } + + async uploadRequestHandler(message) { + this.logger.verbose(`Saving inventory for ${message.data.player_name}`); + this.inventories.set(message.data.player_name, message.data.inventory); + } + + async downloadRequestHandler(message) { + this.logger.verbose(`Downloading inventory for ${message.data.player_name}`); + let inventory = this.inventories.get(message.data.player_name); + return { + player_name: message.data.player_name, + inventory: inventory || {}, + new_player: !inventory, + }; + } + + async onShutdown() { + clearInterval(this.autosaveId); + await saveDatabase(this.master.config, this.inventories, this.logger); + } + + async databaseStatsRequestHandler(message) { + let inventories = Array.from(this.inventories.keys()) + .map(name => ({ + name, + length: JSON.stringify(this.inventories.get(name)).length, + })) + .sort((a, b) => a.length - b.length); + return { + database_size: inventories.map(x => x.length).reduce((a, b) => b - a, 0), + database_entries: inventories.length, + largest_entry: { + name: inventories[0] && inventories[0].name || "-", + size: inventories[0] && inventories[0].length || 0, + }, + }; + } +} + +module.exports = { + MasterPlugin, +}; diff --git a/plugins/inventory_sync/module/commands.lua b/plugins/inventory_sync/module/commands.lua new file mode 100644 index 00000000..bce7a58a --- /dev/null +++ b/plugins/inventory_sync/module/commands.lua @@ -0,0 +1,17 @@ +local save_crafts = require("modules/inventory_sync/save_crafts") +local restore_crafts = require("modules/inventory_sync/restore_crafts") + +function add_commands() + --[[ + Command to serialize crafting queue for transport. + When serialized, the crafting queue is cleared and stored in global. The items received are cleared as well. + Upon leaving, the value in global is cleared and sent with the inventory to the master. + Upon joining a game the crafting queue is added to the end of the existing crafting queue. + ]] + commands.add_command("save-crafts", "Save crafting queue for transport to a different server", save_crafts) + commands.add_command("csc", "Shorthand for /save-crafts", save_crafts) + commands.add_command("restore-crafts", "Load crafting queue from global", restore_crafts) + commands.add_command("crc", "Shorthand for /restore-crafts", restore_crafts) +end + +return add_commands diff --git a/plugins/inventory_sync/module/define_player_inventories.lua b/plugins/inventory_sync/module/define_player_inventories.lua new file mode 100644 index 00000000..fe5e0921 --- /dev/null +++ b/plugins/inventory_sync/module/define_player_inventories.lua @@ -0,0 +1,10 @@ +local inventories = { + defines.inventory.character_main, + defines.inventory.character_guns, + defines.inventory.character_ammo, + defines.inventory.character_armor, + defines.inventory.character_vehicle, + defines.inventory.character_trash, +} + +return inventories diff --git a/plugins/inventory_sync/module/define_player_stat_keys.lua b/plugins/inventory_sync/module/define_player_stat_keys.lua new file mode 100644 index 00000000..729d4c66 --- /dev/null +++ b/plugins/inventory_sync/module/define_player_stat_keys.lua @@ -0,0 +1,23 @@ +return { + "character_crafting_speed_modifier", + "character_mining_speed_modifier", + "character_additional_mining_categories", + "character_running_speed_modifier", + "character_build_distance_bonus", + "character_item_drop_distance_bonus", + "character_reach_distance_bonus", + "character_resource_reach_distance_bonus", + "character_item_pickup_distance_bonus", + "character_loot_pickup_distance_bonus", + "character_inventory_slots_bonus", + "character_trash_slot_count_bonus", + "character_maximum_following_robot_count_bonus", + "character_health_bonus", + "character_personal_logistic_requests_enabled", + "build_distance", + "drop_item_distance", + "reach_distance", + "item_pickup_distance", + "loot_pickup_distance", + "resource_reach_distance", +} diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua new file mode 100644 index 00000000..c1f20934 --- /dev/null +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -0,0 +1,107 @@ +local progress_dialog = require("modules/inventory_sync/gui/progress_dialog") +local serialize = require("modules/clusterio/serialize") +local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue") +local inventories = require("modules/inventory_sync/define_player_inventories") +local ensure_character = require("modules/inventory_sync/ensure_character") +local player_stat_keys = require("modules/inventory_sync/define_player_stat_keys") + +function download_inventory(player_name, data, number, total) + local player = game.get_player(player_name) + if player == nil then + log("Player not found! "..player_name) + return + end + if number then + if global.inventory_sync.download_cache[player_name] == nil then + global.inventory_sync.download_cache[player_name] = "" + end + -- Append data to download cache + global.inventory_sync.download_cache[player_name] = global.inventory_sync.download_cache[player_name] .. data + end + + if number ~= total then + -- Show progress in console + -- player.print("Downloaded "..(number + 1).."/"..total.." parts") + -- Show progress in GUI + progress_dialog(player, number+1, total) + + -- Request next segment + rcon.print("Segment downloaded") + + -- Reset sync_start_tick to indicate master connection + global.inventory_sync.players[player_name].sync_start_tick = game.tick + else + -- Stop tracking master connection status, sync is complete + global.inventory_sync.players[player_name].sync_start_tick = 0 + + -- Handle dirty inventory + if global.inventory_sync.players[player_name].dirty_inventory then + clean_dirty_inventory(player) + end + + -- Recreate player character + ensure_character(player) + + -- Remove freeplay skip cutscene label + if player.gui.screen.skip_cutscene_label then + player.gui.screen.skip_cutscene_label.destroy() + end + + local character = player.character or player.cutscene_character + + -- Load downloaded inventory + local serialized_player = game.json_to_table(global.inventory_sync.download_cache[player_name]) + global.inventory_sync.download_cache[player_name] = nil -- remove data to lower mapsize + + -- Load inventories + for _, inv in pairs(inventories) do + local inventory = player.get_inventory(inv) + if inventory ~= nil and serialized_player.inventories[tostring(inv)] ~= nil then + serialize.deserialize_inventory(inventory, serialized_player.inventories[tostring(inv)]) + end + end + + -- Load personal logistics slots + for i = 1, 200 do + local slot = serialized_player.personal_logistic_slots[tostring(i+1)] -- 1 is empty to force array to be spare + if slot ~= nil then + player.set_personal_logistic_slot(i, slot) + end + end + + -- Load hotbar, don't overwrite empty slots + for i = 1, 100 do + -- if serialized_player.hotbar[i] ~= nil then + player.set_quick_bar_slot(i, game.item_prototypes[serialized_player.hotbar[i]]) + -- end + end + + -- Load crafting queue + load_crafting_queue(serialized_player.crafting_queue, player) + + -- Misc + player.cheat_mode = serialized_player.cheat_mode + player.force = serialized_player.force -- Force by name as string + player.tag = serialized_player.tag + player.color = serialized_player.color + player.chat_color = serialized_player.chat_color + + -- Player stats + for _,v in pairs(player_stat_keys) do + character[v] = serialized_player[v] + end + + if serialized_player.flashlight then + player.enable_flashlight() + else + player.disable_flashlight() + end + + local startTick = global.inventory_sync.download_start_tick[player.name] + local log_line = "Imported inventory for "..player_name.." in "..game.tick - startTick.." ticks" + player.print(log_line) + log("[inventory_sync] "..log_line) + end +end + +return download_inventory diff --git a/plugins/inventory_sync/module/ensure_character.lua b/plugins/inventory_sync/module/ensure_character.lua new file mode 100644 index 00000000..bb962f96 --- /dev/null +++ b/plugins/inventory_sync/module/ensure_character.lua @@ -0,0 +1,10 @@ +local function ensure_character(player) + if player.character == nil then + player.set_controller { + type = defines.controllers.god, + } + player.create_character() + end +end + +return ensure_character diff --git a/plugins/inventory_sync/module/gui/dialog_failed_download.lua b/plugins/inventory_sync/module/gui/dialog_failed_download.lua new file mode 100644 index 00000000..4730de6a --- /dev/null +++ b/plugins/inventory_sync/module/gui/dialog_failed_download.lua @@ -0,0 +1,60 @@ +-- Show inventory download progressbar +function dialog_failed_download(player) + if player.gui.screen.dialog_failed_download then + player.gui.screen.dialog_failed_download.destroy() + end + + local frame = player.gui.screen.add { + name = "dialog_failed_download", + type = "frame", + direction = "vertical", + } + + local titlebar = frame.add { + type = "flow", + } + titlebar.drag_target = frame + + local title = titlebar.add { + type = "label", + style = "frame_title", + caption = "Inventory sync download failed", + } + title.drag_target = frame + + local filler = titlebar.add { + type = "empty-widget", + style = "draggable_space_header", + } + filler.style.horizontally_stretchable = "on" + filler.style.right_margin = 4 + filler.style.height = 24 + filler.drag_target = frame + + local content = frame.add { + name = "inventory_sync_failed_download_content", + type = "frame", + direction = "vertical", + style = "inside_shallow_frame_with_padding", + } + content.style.width = 400 + + local p1 = content.add { + type = "label", + caption = "The inventory download couldn't complete. You can choose to keep playing with an empty inventory until the connection is reestablished and the inventory download completes. At that point your temporary inventory will be put in a chest.", + } + p1.style.single_line = false + p1.style.bottom_margin = 8 + + local abort = content.add { + name = "inventory_sync_failed_download_abort", + type = "button", + caption = "Continue with new inventory", + tooltip = "Create a new empty temporary inventory. Items you pick up will be placed in a chest when the sync completes.", + } + + frame.force_auto_center() + player.opened = frame +end + +return dialog_failed_download diff --git a/plugins/inventory_sync/module/gui/handle_gui_events.lua b/plugins/inventory_sync/module/gui/handle_gui_events.lua new file mode 100644 index 00000000..b335c2d3 --- /dev/null +++ b/plugins/inventory_sync/module/gui/handle_gui_events.lua @@ -0,0 +1,25 @@ +local ensure_character = require("modules/inventory_sync/ensure_character") + +-- on_gui_click +local handle_gui_events = function(event) + if not event.element.valid then + return + end + local player = game.get_player(event.player_index) + if event.element.name == "inventory_sync_failed_download_abort" then + -- Give the player a new inventory and mark it as dirty + -- Show dirty status and an explanation in a GUI + + -- Remove GUI element + player.gui.screen.dialog_failed_download.destroy() + -- Handle inventory dirtying + if not global.inventory_sync.players[player.name].dirty_inventory then + global.inventory_sync.players[player.name].dirty_inventory = true + + -- Restore player character + ensure_character(player) + end + end +end + +return handle_gui_events diff --git a/plugins/inventory_sync/module/gui/progress_dialog.lua b/plugins/inventory_sync/module/gui/progress_dialog.lua new file mode 100644 index 00000000..74da1532 --- /dev/null +++ b/plugins/inventory_sync/module/gui/progress_dialog.lua @@ -0,0 +1,71 @@ +-- Show inventory download progressbar +function open_dialog(player, progress, total) + if player.gui.screen.inventory_sync_progress then + player.gui.screen.inventory_sync_progress.destroy() + end + if progress == total then + return -- Hide dialog on completed download + end + + local frame = player.gui.screen.add { + name = "inventory_sync_progress", + type = "frame", + direction = "vertical", + } + + local titlebar = frame.add { + type = "flow", + } + titlebar.drag_target = frame + + local title = titlebar.add { + type = "label", + style = "frame_title", + caption = "Inventory sync", + } + title.drag_target = frame + + local filler = titlebar.add { + type = "empty-widget", + style = "draggable_space_header", + } + filler.style.horizontally_stretchable = "on" + filler.style.right_margin = 4 + filler.style.height = 24 + filler.drag_target = frame + + titlebar.add { + name = "inventory_sync_progress_close_button", + type = "sprite-button", + sprite = "utility/close_white", + hovered_sprite = "utility/close_black", + clicked_sprite = "utility/close_black", + style = "frame_action_button", + } + + local content = frame.add { + name = "inventory_sync_progress_dialog_content", + type = "frame", + direction = "vertical", + style = "inside_shallow_frame_with_padding", + } + content.style.width = 400 + + local p1 = content.add { + type = "label", + caption = "Downloading inventory chunk"..progress.."/"..total, + } + p1.style.single_line = false + p1.style.bottom_margin = 8 + + local progressbar = content.add { + type = "progressbar", + value = progress / total, + } + progressbar.style.width = 376 -- inside_shallow_frame_with_padding has 12px padding on each side + + frame.force_auto_center() + player.opened = frame +end + +return open_dialog diff --git a/plugins/inventory_sync/module/gui/welcome_new_player.lua b/plugins/inventory_sync/module/gui/welcome_new_player.lua new file mode 100644 index 00000000..ba88c49f --- /dev/null +++ b/plugins/inventory_sync/module/gui/welcome_new_player.lua @@ -0,0 +1,9 @@ +local function welcome_new_player(player_name) + local player = game.get_player(player_name) + if player == nil then return end + player.print("Welcome to the server!") + player.print("Your inventory will be automatically synchronized between servers in this cluster.") + player.print("To improve sync performance, please avoid storing blueprints in your inventory.") +end + +return welcome_new_player diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua new file mode 100644 index 00000000..9fb659c5 --- /dev/null +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -0,0 +1,174 @@ +--[[ + +When a player leaves the game, serialize their inventory and upload it to the master. + +When a player joins the game, wait until they have a character, then send a request to +the master server for the players inventory. + +When the master sends an inventory, parse it and give it to the player. +If the master responds that its a new player, show a welcome message. + +]] + +local clusterio_api = require("modules/clusterio/api") +local add_commands = require("modules/inventory_sync/commands") +local upload_inventory = require("modules/inventory_sync/upload_inventory") +local download_inventory = require("modules/inventory_sync/download_inventory") +local welcome_new_player = require("modules/inventory_sync/gui/welcome_new_player") +local handle_gui_events = require("modules/inventory_sync/gui/handle_gui_events") +local dialog_failed_download = require("modules/inventory_sync/gui/dialog_failed_download") +local clean_dirty_inventory = require("modules/inventory_sync/script/clean_dirty_inventory") + + +local function create_player(player, dirty) + global.inventory_sync.players[player.name] = { + dirty_inventory = dirty, -- Player has a temporary non-synced inventory that should be persisted + sync_start_tick = 0, -- To track download failure timeout + } +end + +local function remove_player(player) + -- Remove inventory download performance counter + global.inventory_sync.download_start_tick[player.name] = nil + -- Remove stored crafting queue + global.inventory_sync.saved_crafting_queue[player.name] = nil + -- Remove other player data + global.inventory_sync.players[player.name] = nil +end + + +inventory_sync = {} +inventory_sync.welcome_new_player = welcome_new_player +-- This function is called by instance.js as /sc inventory_sync.download_inventory("danielv", Escaped JSON string, package_number, total_packages_count) with data from the master +inventory_sync.download_inventory = download_inventory +-- This function is called internally when a player leaves the game for their inventory to upload to the master +inventory_sync.upload_inventory = upload_inventory + +inventory_sync.add_commands = add_commands +inventory_sync.events = {} +inventory_sync.events[clusterio_api.events.on_server_startup] = function(event) + -- Set up global table + if not global.inventory_sync then + global.inventory_sync = {} + end + if not global.inventory_sync.download_start_tick then + global.inventory_sync.download_start_tick = {} + end + if not global.inventory_sync.saved_crafting_queue then + global.inventory_sync.saved_crafting_queue = {} + end + if not global.inventory_sync.download_cache then + global.inventory_sync.download_cache = {} + end + if not global.inventory_sync.players_waiting_for_download then + global.inventory_sync.players_waiting_for_download = {} + end + if not global.inventory_sync.players then + global.inventory_sync.players = {} + end + + for _, player in pairs(game.players) do + if not global.inventory_sync.players[player.name] then + create_player(player, true) + end + end +end + +-- Cleanup +inventory_sync.events[defines.events.on_pre_player_removed] = function(event) + remove_player(game.get_player(event.player_index)) +end + +inventory_sync.events[defines.events.on_player_created] = function(event) + create_player(game.get_player(event.player_index), false) +end + +inventory_sync.events[defines.events.on_gui_click] = handle_gui_events + +-- Download inventory from master +inventory_sync.events[defines.events.on_player_joined_game] = function(event) + -- Add player to download queue. Inventory won't be downloaded before the player has a character + global.inventory_sync.players_waiting_for_download[game.get_player(event.player_index).name] = true + -- Don't wait for on_nth_tick if not required + inventory_sync.check_player_character_before_download() +end + +inventory_sync.on_nth_tick = {} +inventory_sync.on_nth_tick[33] = function(event) + -- Periodically check if player has a character to download to. False in cutscenes. + inventory_sync.check_player_character_before_download() + + -- Check if download has failed and we should create a dirty inventory + inventory_sync.check_inventory_download_failed() +end + +-- Upload inventory when a player leaves the game. Triggers on restart after crash if player was online during crash. +inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) + -- for some reason, on_pre_player_left_game gets called before on_server_startup so global isn't ready yet + if not global.inventory_sync then + log("ERROR: Global inventory sync not defined") + return + end + + local player_index = event.player_index + local player = game.get_player(player_index) + -- Avoid uploading for unsynced players (due to not having a character, ex during freeplay cutscene) + if global.inventory_sync.players_waiting_for_download[player.name] == nil then + inventory_sync.upload_inventory(player_index) + global.inventory_sync.download_cache[player_index] = "" -- Clear cache to avoid leaking memory to people quitting during download + end + global.inventory_sync.players_waiting_for_download[player.name] = nil +end + +function inventory_sync.check_player_character_before_download() + -- Check if players waiting for download have a valid character + for name in pairs(global.inventory_sync.players_waiting_for_download) do + local player = game.get_player(name) + if player.character ~= nil then + global.inventory_sync.players_waiting_for_download[name] = nil + inventory_sync.initiate_inventory_download(player) + end + end +end + +function inventory_sync.initiate_inventory_download(player) + player.print("Initiating inventory download...") + global.inventory_sync.download_start_tick[player.name] = game.tick + global.inventory_sync.download_cache[player.name] = "" + clusterio_api.send_json("inventory_sync_download", { + player_name = player.name + }) + + -- Handle dirty inventories. A dirty inventory is a temporary inventory that has been used while waiting + -- for sync. It should be taken care of in some way, like putting it in a corpse or a chest. + if global.inventory_sync.players[player.name].dirty_inventory then + clean_dirty_inventory(player) + end + + -- Set player into ghost mode for duration of download + local character = player.character + player.set_controller { + type = defines.controllers.ghost, + } + if character ~= nil then character.destroy() end + + -- Start timeout for detecting master connection failure + global.inventory_sync.players[player.name].sync_start_tick = game.tick +end +function inventory_sync.check_inventory_download_failed() + -- Used for letting the master report the inventory download failing, like when the master is offline or an + -- error occurs somewhere in the server code. The player will be returned to a playable state. + for name, data in pairs(global.inventory_sync.players) do + if data.sync_start_tick ~= 0 and data.sync_start_tick <= game.tick - 600 then + -- Stop refreshing timer + data.sync_start_tick = 0 + + local player = game.get_player(name) + player.print("Inventory download failed due to master connection") + -- Show GUI with option to retry or abort download + dialog_failed_download(player) + end + end +end + +return inventory_sync diff --git a/plugins/inventory_sync/module/load_crafting_queue.lua b/plugins/inventory_sync/module/load_crafting_queue.lua new file mode 100644 index 00000000..74762cb8 --- /dev/null +++ b/plugins/inventory_sync/module/load_crafting_queue.lua @@ -0,0 +1,27 @@ +-- Load crafting queue from a table +function load_crafting_queue(crafting_queue, player) + local inventory = player.get_main_inventory() + + -- Give player some more inventory space to avoid duplicating items + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus + 1000 + + -- Add items to inventory + for _, item in pairs(crafting_queue.ingredients) do + inventory.insert(item) + end + + -- Load crafting queue + for _, queueItem in pairs(crafting_queue.crafting_queue) do + -- Start crafting (consume items) + player.begin_crafting { + count = queueItem.count, + recipe = queueItem.recipe, + -- silent = true, -- Fail silently if items are missing + } + end + + -- Remove extra inventory slots + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000 +end + +return load_crafting_queue diff --git a/plugins/inventory_sync/module/module.json b/plugins/inventory_sync/module/module.json new file mode 100644 index 00000000..96c3db30 --- /dev/null +++ b/plugins/inventory_sync/module/module.json @@ -0,0 +1,4 @@ +{ + "name": "inventory_sync", + "load": ["inventory_sync.lua"] +} diff --git a/plugins/inventory_sync/module/restore_crafts.lua b/plugins/inventory_sync/module/restore_crafts.lua new file mode 100644 index 00000000..1779018d --- /dev/null +++ b/plugins/inventory_sync/module/restore_crafts.lua @@ -0,0 +1,19 @@ +local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue") + +function restore_crafts(event) + if event.player_index then + local player = game.get_player(event.player_index) + local crafting_queue = global.inventory_sync.saved_crafting_queue[player.name] + + if crafting_queue == nil then + player.print("No saved crafting queue") + return + end + + load_crafting_queue(crafting_queue, player) + + global.inventory_sync.saved_crafting_queue[player.name] = nil + end +end + +return restore_crafts diff --git a/plugins/inventory_sync/module/save_crafts.lua b/plugins/inventory_sync/module/save_crafts.lua new file mode 100644 index 00000000..6916be48 --- /dev/null +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -0,0 +1,96 @@ +function save_crafts(event) + if event.player_index then + local player = game.get_player(event.player_index) + local crafting_queue = {} + + -- Avoid overwriting old save + if global.inventory_sync.saved_crafting_queue[player.name] ~= nil then + player.print("You already have a saved crafting queue, restore it with /restore-crafts") + return + end + + -- Give player some more inventory space to avoid duplicating items + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus + 1000 + + -- Save current items + local inventory = player.get_main_inventory() + local old_items = inventory.get_contents() + + -- Cancel old crafts to get the items back + while player.crafting_queue_size > 0 do + local old_queue = player.crafting_queue + local queueItem = player.crafting_queue[1] + + -- Cancel craft + player.cancel_crafting { + index = 1, + count = 1, + } + + local rightmost_right_index = 0 -- 0 indexed since it is subtractive in a 1 indexed language + local new_queue = player.crafting_queue + while + new_queue ~= nil and + new_queue[#new_queue - rightmost_right_index] ~= nil and + new_queue[#new_queue - rightmost_right_index].count >= old_queue[#old_queue - rightmost_right_index].count + do + rightmost_right_index = rightmost_right_index + 1 + end + local oldItem = old_queue[#old_queue - rightmost_right_index] + local newItem = nil + if new_queue ~= nil then + newItem = new_queue[#new_queue - rightmost_right_index] + end + + -- Figure out how many items to add to queue + local added = oldItem.count + if newItem ~= nil then + added = oldItem.count - newItem.count + if oldItem.recipe ~= newItem.recipe then + log("ERROR: Old item "..oldItem.recipe.." is not equal "..newItem.recipe) + end + end + + -- If the last item we added was of the same type, merge them in the queue + if #crafting_queue > 0 and crafting_queue[#crafting_queue].recipe == oldItem.recipe then + crafting_queue[#crafting_queue].count = crafting_queue[#crafting_queue].count + added + else + -- If the last item was of a different type, add a new item to the queue + table.insert(crafting_queue, { + recipe = oldItem.recipe, + count = added, + }) + end + -- game.print("Saved craft "..oldItem.recipe) + end + + -- Find amount of items added and remove from inventory + local new_items = inventory.get_contents() + local difference = {} + for k,v in pairs(new_items) do + local old_count = old_items[k] + local diff = v + if old_count ~= nil then + diff = diff - old_count + end + if diff > 0 then + local ingredient = { + name = k, + count = diff, + } + inventory.remove(ingredient) + table.insert(difference, ingredient) + end + end + + -- Remove extra inventory slots + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000 + + global.inventory_sync.saved_crafting_queue[player.name] = { + crafting_queue = crafting_queue, + ingredients = difference, + } + end +end + +return save_crafts diff --git a/plugins/inventory_sync/module/script/clean_dirty_inventory.lua b/plugins/inventory_sync/module/script/clean_dirty_inventory.lua new file mode 100644 index 00000000..085050d8 --- /dev/null +++ b/plugins/inventory_sync/module/script/clean_dirty_inventory.lua @@ -0,0 +1,29 @@ +local inventories = require("modules/inventory_sync/define_player_inventories") + +--[[ + +Take a player with a dirty inventory, create chests with all the items, clear the player, +put them in ghost mode and mark the inventory as clean. + +]] + +function clean_dirty_inventory(player) + global.inventory_sync.players[player.name].dirty_inventory = false + + local surface = player.surface + local position = player.position + local character = player.character + player.disassociate_character(character) + character.die() + player.ticks_to_respawn = nil + player.teleport(position, surface) + + -- Remove player character + local character = player.character + player.set_controller { + type = defines.controllers.ghost, + } + if character ~= nil then character.destroy() end +end + +return clean_dirty_inventory diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua new file mode 100644 index 00000000..43812dbc --- /dev/null +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -0,0 +1,81 @@ +local clusterio_api = require("modules/clusterio/api") +local serialize = require("modules/clusterio/serialize") +local inventories = require("modules/inventory_sync/define_player_inventories") +local save_crafts = require("modules/inventory_sync/save_crafts") +local player_stat_keys = require("modules/inventory_sync/define_player_stat_keys") + +function upload_inventory(playerIndex) + local player = game.get_player(playerIndex) + + if player.character == nil then + -- Panic! + log("ERROR: Player "..player.name.." quit without a character. Inventory not uploaded") + return false + end + + local serialized_player = { + name = player.name, + inventories = {}, + hotbar = {}, + color = player.color, + chat_color = player.chat_color, + tag = player.tag, + -- admin = player.admin, -- This is handled by other parts of clusterio + personal_logistic_slots = {}, + force = player.force.name, + cheat_mode = player.cheat_mode, + flashlight = player.is_flashlight_enabled(), + } + + -- Transfer player stats + for _,v in pairs(player_stat_keys) do + serialized_player[v] = player[v] + end + + -- Serialize hotbar + for i = 1, 100 do + serialized_player.hotbar[i] = {} + local slot = player.get_quick_bar_slot(i) + if slot ~= nil and slot.name ~= nil then + serialized_player.hotbar[i] = slot.name + end + end + + -- Serialize personal logistics slots + for i = 1, 200 do -- Nobody will have more than 200 logistics slots, right? + local slot = player.get_personal_logistic_slot(i) + if slot.name ~= nil then + -- We leave [1] empty to force the JSON function to parse it as an object. + -- When its parsed as an array we get inconsistent key datatypes depending + -- on whether or not it is sparse. + serialized_player.personal_logistic_slots[i+1] = { + name = slot.name, + min = slot.min, + max = slot.max, + } + end + end + + -- Serialize inventories + for _, inv in pairs(inventories) do + local inventory = player.get_inventory(inv) + if inventory ~= nil then + local serialized_inventory = serialize.serialize_inventory(inventory) + serialized_player.inventories[inv] = serialized_inventory + end + end + + -- Serialize crafting queue saved by /csc or /save-crafts OR automatically + save_crafts({player_index = player.name}) + serialized_player.crafting_queue = global.inventory_sync.saved_crafting_queue[player.name] + if serialized_player.crafting_queue == nil then + serialized_player.crafting_queue = {} + end + + clusterio_api.send_json("inventory_sync_upload", serialized_player) + + -- Clear saved crafting queue + global.inventory_sync.saved_crafting_queue[player.name] = nil +end + +return upload_inventory diff --git a/plugins/inventory_sync/package.json b/plugins/inventory_sync/package.json new file mode 100644 index 00000000..fe371d35 --- /dev/null +++ b/plugins/inventory_sync/package.json @@ -0,0 +1,38 @@ +{ + "name": "@clusterio/plugin-inventory_sync", + "version": "2.0.0-alpha.6", + "description": "Clusterio plugin forwarding between Factorio servers", + "author": "Danielv ", + "homepage": "https://github.com/clusterio/factorioClusterio#readme", + "license": "MIT", + "repository": "clusterio/factorioClusterio", + "scripts": { + "build": "webpack-cli --env production", + "test": "echo \"Error: run tests from root\" && exit 1" + }, + "bugs": { + "url": "https://github.com/clusterio/factorioClusterio/issues" + }, + "engines": { + "node": ">=12" + }, + "peerDependencies": { + "@clusterio/lib": "^2.0.0-alpha.0" + }, + "dependencies": { + "fs-extra": "^8.1.0" + }, + "devDependencies": { + "@clusterio/lib": "^2.0.0-alpha.6", + "@clusterio/web_ui": "^2.0.0-alpha.6", + "antd": "^4.16.0", + "react": "^16.13.1", + "react-dom": "^16.13.1", + "webpack": "^5.36.2", + "webpack-cli": "^4.7.0", + "webpack-merge": "^5.2.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/plugins/inventory_sync/web/index.css b/plugins/inventory_sync/web/index.css new file mode 100644 index 00000000..e69de29b diff --git a/plugins/inventory_sync/web/index.jsx b/plugins/inventory_sync/web/index.jsx new file mode 100644 index 00000000..38328bc5 --- /dev/null +++ b/plugins/inventory_sync/web/index.jsx @@ -0,0 +1,43 @@ +import React, { useContext, useEffect, useState } from "react"; + +import libPlugin from "@clusterio/lib/plugin"; +import { PageLayout, ControlContext } from "@clusterio/web_ui"; +import info from "../info"; + +import "./index.css"; + +function InventoryPage() { + let control = useContext(ControlContext); + + let [statsData, updateStatsData] = useState(); + + useEffect(() => { + (async () => { + // Get statistics + updateStatsData(await info.messages.databaseStats.send(control, {})); + })(); + }, []); + + return +

Inventory sync

+ {statsData && <> +

Database size: {Math.round(statsData.database_size / 1000)}kB

+

Database entries: {statsData.database_entries}

+

Largest entry is {statsData.largest_entry.name} with {(statsData.largest_entry.size / 1000)}kB

+ } +
; +} + +export class WebPlugin extends libPlugin.BaseWebPlugin { + async init() { + this.pages = [ + { path: "/inventory", sidebarName: "Inventory sync", content: }, + ]; + } + + onMasterConnectionEvent(event) { + if (event === "connect") { + + } + } +} diff --git a/plugins/inventory_sync/webpack.config.js b/plugins/inventory_sync/webpack.config.js new file mode 100644 index 00000000..32df7fbf --- /dev/null +++ b/plugins/inventory_sync/webpack.config.js @@ -0,0 +1,37 @@ +"use strict"; +const path = require("path"); +const webpack = require("webpack"); +const { merge } = require("webpack-merge"); + +const common = require("@clusterio/web_ui/webpack.common"); + +module.exports = (env = {}) => merge(common(env), { + context: __dirname, + entry: "./web/index.jsx", + output: { + publicPath: "auto", + filename: "bundle.js", + path: path.resolve(__dirname, "dist", "web"), + }, + plugins: [ + new webpack.container.ModuleFederationPlugin({ + name: "inventory_sync", + library: { type: "var", name: "plugin_inventory_sync" }, + filename: "remoteEntry.js", + exposes: { + "./info": "./info.js", + "./package.json": "./package.json", + "./web": "./web/index.jsx", + }, + shared: { + "@clusterio/lib/config": { import: false }, + "@clusterio/lib/link": { import: false }, + "@clusterio/web_ui": { import: false }, + "ajv": { import: false }, + "antd": { import: false }, + "react": { import: false }, + "react-dom": { import: false }, + }, + }), + ], +});