From 86bd07666fccc81a80bc13c668085a33ec052ead Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 9 Jun 2021 17:34:48 +0200 Subject: [PATCH 01/24] First working draft of inventory sync --- plugins/inventory_sync/.npmignore | 1 + plugins/inventory_sync/README.md | 12 ++ plugins/inventory_sync/control.js | 30 +++++ plugins/inventory_sync/info.js | 120 ++++++++++++++++++ plugins/inventory_sync/instance.js | 87 +++++++++++++ plugins/inventory_sync/master.js | 78 ++++++++++++ .../inventory_sync/module/inventory_sync.lua | 82 ++++++++++++ plugins/inventory_sync/module/module.json | 4 + plugins/inventory_sync/package.json | 32 +++++ plugins/inventory_sync/web/index.css | 0 plugins/inventory_sync/web/index.jsx | 32 +++++ plugins/inventory_sync/webpack.config.js | 34 +++++ 12 files changed, 512 insertions(+) create mode 100644 plugins/inventory_sync/.npmignore create mode 100644 plugins/inventory_sync/README.md create mode 100644 plugins/inventory_sync/control.js create mode 100644 plugins/inventory_sync/info.js create mode 100644 plugins/inventory_sync/instance.js create mode 100644 plugins/inventory_sync/master.js create mode 100644 plugins/inventory_sync/module/inventory_sync.lua create mode 100644 plugins/inventory_sync/module/module.json create mode 100644 plugins/inventory_sync/package.json create mode 100644 plugins/inventory_sync/web/index.css create mode 100644 plugins/inventory_sync/web/index.jsx create mode 100644 plugins/inventory_sync/webpack.config.js 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..8c2067a2 --- /dev/null +++ b/plugins/inventory_sync/README.md @@ -0,0 +1,12 @@ +# Clusterio inventory sync plugin + +Carry over player inventory between servers + +## 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 dedicate slave or ctl installation respectively. diff --git a/plugins/inventory_sync/control.js b/plugins/inventory_sync/control.js new file mode 100644 index 00000000..680e79af --- /dev/null +++ b/plugins/inventory_sync/control.js @@ -0,0 +1,30 @@ +"use strict"; +const libPlugin = require("@clusterio/lib/plugin"); +const info = require("./info"); +const libCommand = require("@clusterio/lib/command"); + + +const globalChatCommands = new libCommand.CommandTree({ + name: "global-chat", description: "Global Chat plugin commands", +}); +globalChatCommands.add(new libCommand.Command({ + definition: ["shout ", "Send message to all instances", (yargs) => { + yargs.positional("message", { describe: "message to send", type: "string" }); + }], + handler: async function(args, control) { + await info.messages.chat.send(control, { + instance_name: "Console", + content: args.message, + }); + }, +})); + +class ControlPlugin extends libPlugin.BaseControlPlugin { + async addCommands(rootCommand) { + rootCommand.add(globalChatCommands); + } +} + +module.exports = { + ControlPlugin, +}; diff --git a/plugins/inventory_sync/info.js b/plugins/inventory_sync/info.js new file mode 100644 index 00000000..03fecb81 --- /dev/null +++ b/plugins/inventory_sync/info.js @@ -0,0 +1,120 @@ +"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: 60, +}); +MasterConfigGroup.define({ + name: "spectator_until_ready", + title: "Spectator mode until ready", + description: "Set the player into spectator mode until the inventory is fully synchronized.", + type: "boolean", + initial_value: true, +}); +MasterConfigGroup.define({ + name: "clear_old_inventory", + title: "Clear old inventory on join", + description: "Clear old inventory on join to prevent duplication of items. Can cause greater disruption on failed or slow sync.", + type: "boolean", + initial_value: true, +}); +MasterConfigGroup.finalize(); + +class InstanceConfigGroup extends libConfig.PluginConfigGroup { } +InstanceConfigGroup.defaultAccess = ["master", "slave", "control"]; +InstanceConfigGroup.groupName = "inventory_sync"; +InstanceConfigGroup.define({ + name: "enable_inventory_sync", + title: "Syncronize inventories", + description: "Syncronize inventories. Disable if this instance is running incompatible mods or otherwise shouldn't sync.", + type: "boolean", + initial_value: true, +}); +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: { + chat: new libLink.Event({ + type: "inventory_sync:chat", + links: ["instance-slave", "slave-master", "master-slave", "slave-instance"], + forwardTo: "master", + broadcastTo: "instance", + eventProperties: { + "instance_name": { type: "string" }, + "content": { type: "string" }, + }, + }), + 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: "string" }, + }, + responseProperties: { + "success": { type: "boolean" }, + }, + }), + 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: "string" }, + }, + }), + databaseStats: new libLink.Request({ + type: "inventory_sync:databaseStats", + links: ["control-master"], + permission: "inventory_sync.inventory.view", + requestProperties: {}, + 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..159c2c12 --- /dev/null +++ b/plugins/inventory_sync/instance.js @@ -0,0 +1,87 @@ +/** + * @module + */ +"use strict"; +const libPlugin = require("@clusterio/lib/plugin"); +const libLuaTools = require("@clusterio/lib/lua_tools"); + +class InstancePlugin extends libPlugin.BaseInstancePlugin { + async init() { + this.inventoryQueue = []; + + // setInterval(() => { + + // console.log("Connected") + // this.info.messages.upload.send(this.instance, { + // instance_id: this.instance.id, + // player: "danielv", + // inventory: "stuff", + // }) + // },5000) + + + 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}` + )) + ) + } + + onMasterConnectionEvent(event) { + console.log("Connectionevent", event) + if (event === "connect") { + } + } + + async handleUpload(player) { + console.log("Uploading", player) + this.info.messages.upload.send(this.instance, { + instance_id: this.instance.id, + instance_name: this.instance.name, + player_name: player.name, + inventory: JSON.stringify(player), + }) + } + async handleDownload(player) { + console.log("Downloading", player.player_name) + let response = await this.info.messages.download.send(this.instance, { + player_name: player.player_name + }) + if (response.inventory) { + console.log("Sending command") + await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}')`) + } + } + + async chatEventHandler(message) { + // TODO check if cross server chat is enabled + let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`; + await this.sendRcon(`/sc game.print('${libLuaTools.escapeString(content)}')`, true); + } + + sendChat(message) { + this.info.messages.chat.send(this.instance, { + instance_name: this.instance.name, + content: message, + }); + } + + async onOutput(output) { + if (output.type === "action" && output.action === "INVENTORY") { + if (this.slave.connector.connected) { + this.sendChat(output.message); + } else { + this.inventoryQueue.push(output.message); + } + } + } +} + +module.exports = { + InstancePlugin, +}; diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js new file mode 100644 index 00000000..71889dcc --- /dev/null +++ b/plugins/inventory_sync/master.js @@ -0,0 +1,78 @@ +"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 JSON.parse(content); + + } catch (err) { + if (err.code === "ENOENT") { + logger.verbose("Creating new inventory database"); + return {}; + } + 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(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) { + console.log("Saving inventory for", message.data.player_name) + this.inventories[message.data.player_name] = message.data.inventory + + return { success: true } + } + async downloadRequestHandler(message) { + console.log("Downloading inventory for", message.data.player_name) + return { + player_name: message.data.player_name, + inventory: this.inventories[message.data.player_name] + } + } + async onShutdown() { + clearInterval(this.autosaveId); + await saveDatabase(this.master.config, this.inventories, this.logger); + } + async databaseStatsRequestHandler(message) { + let inventories = Object.keys(this.inventories) + .map(name => ({ + name, + length: this.inventories[name].length + })) + .sort((a, b) => a.stringified.length - b.stringified.length) + return { + database_size: inventories.reduce((a, b) => b - a, 0), + database_entries: inventories.length, + largest_entry: { + name: inventories[0].name, + size: inventories[0].length, + }, + } + } +} + +module.exports = { + MasterPlugin, +}; diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua new file mode 100644 index 00000000..ebce407e --- /dev/null +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -0,0 +1,82 @@ +--[[ + +When a player leaves the game, serialize their inventory and upload it to the master. + +When a player joins the game, 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. + + +packages/slave/modules/clusterio +]] + +local serialize = require("modules/clusterio/serialize") +local clusterio_api = require("modules/clusterio/api") + +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, +} + +inventory_sync = {} + +inventory_sync.events = {} +-- Download inventory from master +inventory_sync.events[defines.events.on_player_joined_game] = function(event) + local playerIndex = event.player_index + inventory_sync.initiateInventoryDownload(playerIndex) +end + +function inventory_sync.initiateInventoryDownload(playerIndex) + game.players[playerIndex].print("Initiating inventory download...") + global["inv_sync_download_start_tick "..game.players[playerIndex].name] = game.tick + clusterio_api.send_json("inventory_sync_download", { + player_name = game.players[playerIndex].name + }) +end + +-- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string) with data from the master +function inventory_sync.downloadInventory(playerName, data) + local serialized_player = game.json_to_table(data) + local player = game.players[playerName] + -- game.print(serpent.block(serialized_player)) + 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 + local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] + game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") +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_player_left_game] = function(event) + local playerIndex = event.player_index + + inventory_sync.uploadInventory(playerIndex) +end + +function inventory_sync.uploadInventory(playerIndex) + local player = game.players[playerIndex] + + local serialized_player = { + name = player.name, + 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 + clusterio_api.send_json("inventory_sync_upload", serialized_player) +end + +return inventory_sync 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/package.json b/plugins/inventory_sync/package.json new file mode 100644 index 00000000..3237132f --- /dev/null +++ b/plugins/inventory_sync/package.json @@ -0,0 +1,32 @@ +{ + "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" + }, + "devDependencies": { + "@clusterio/lib": "^2.0.0-alpha.6", + "@clusterio/web_ui": "^2.0.0-alpha.6", + "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..3cd384d2 --- /dev/null +++ b/plugins/inventory_sync/web/index.jsx @@ -0,0 +1,32 @@ +import React, { useContext } from "react"; +import { Typography } from "antd"; + +import libPlugin from "@clusterio/lib/plugin"; +import { notifyErrorHandler, PageLayout, ControlContext } from "@clusterio/web_ui"; +import info from "../info"; + +import "./index.css"; + +const { Paragraph } = Typography; + +function InventoryPage() { + // let control = useContext(ControlContext); + + return +

Inventory sync

+
; +} + +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..1dd85e59 --- /dev/null +++ b/plugins/inventory_sync/webpack.config.js @@ -0,0 +1,34 @@ +"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 }, + }, + }), + ], +}); From 8a85cfc8da8913255f6f7eb491bbadfe63f4ff62 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 9 Jun 2021 18:00:35 +0200 Subject: [PATCH 02/24] Added small web interface with the most vital stats --- plugins/inventory_sync/master.js | 2 +- plugins/inventory_sync/web/index.jsx | 23 +++++++++++++++++------ plugins/inventory_sync/webpack.config.js | 3 +++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index 71889dcc..29f080b9 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -63,7 +63,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { })) .sort((a, b) => a.stringified.length - b.stringified.length) return { - database_size: inventories.reduce((a, b) => b - a, 0), + database_size: inventories.map(x => x.length).reduce((a, b) => b - a, 0), database_entries: inventories.length, largest_entry: { name: inventories[0].name, diff --git a/plugins/inventory_sync/web/index.jsx b/plugins/inventory_sync/web/index.jsx index 3cd384d2..2229428e 100644 --- a/plugins/inventory_sync/web/index.jsx +++ b/plugins/inventory_sync/web/index.jsx @@ -1,19 +1,30 @@ -import React, { useContext } from "react"; -import { Typography } from "antd"; +import React, { useContext, useEffect, useState } from "react"; import libPlugin from "@clusterio/lib/plugin"; -import { notifyErrorHandler, PageLayout, ControlContext } from "@clusterio/web_ui"; +import { PageLayout, ControlContext } from "@clusterio/web_ui"; import info from "../info"; import "./index.css"; -const { Paragraph } = Typography; - function InventoryPage() { - // let control = useContext(ControlContext); + 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

+ }
; } diff --git a/plugins/inventory_sync/webpack.config.js b/plugins/inventory_sync/webpack.config.js index 1dd85e59..32df7fbf 100644 --- a/plugins/inventory_sync/webpack.config.js +++ b/plugins/inventory_sync/webpack.config.js @@ -28,6 +28,9 @@ module.exports = (env = {}) => merge(common(env), { "@clusterio/lib/link": { import: false }, "@clusterio/web_ui": { import: false }, "ajv": { import: false }, + "antd": { import: false }, + "react": { import: false }, + "react-dom": { import: false }, }, }), ], From 6bb2c50e5c93f24937d4857e206bc4c963e873ec Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 9 Jun 2021 18:07:34 +0200 Subject: [PATCH 03/24] Implemented changes suggested by linter --- packages/web_ui/src/util/websocket.jsx | 6 ++++ plugins/inventory_sync/instance.js | 35 +++++++++--------- plugins/inventory_sync/master.js | 24 +++++++------ plugins/inventory_sync/web/index.jsx | 50 +++++++++++++------------- 4 files changed, 62 insertions(+), 53 deletions(-) diff --git a/packages/web_ui/src/util/websocket.jsx b/packages/web_ui/src/util/websocket.jsx index 6b3b983f..b08b2fc6 100644 --- a/packages/web_ui/src/util/websocket.jsx +++ b/packages/web_ui/src/util/websocket.jsx @@ -273,6 +273,12 @@ export class Control extends libLink.Link { handler(info); } } + if (info.slave_id !== undefined) { + let slaveHandlers = this.slaveLogHandlers.get(info.slave_id); + for (let handler of slaveHandlers || []) { + handler(info); + } + } } async onInstanceLog(id, handler) { diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index 159c2c12..fadd0198 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -20,41 +20,40 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { // },5000) - 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}` - )) - ) + 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}` + )) + ); } onMasterConnectionEvent(event) { - console.log("Connectionevent", event) + console.log("Connectionevent", event); if (event === "connect") { } } async handleUpload(player) { - console.log("Uploading", player) + console.log("Uploading", player); this.info.messages.upload.send(this.instance, { instance_id: this.instance.id, instance_name: this.instance.name, player_name: player.name, inventory: JSON.stringify(player), - }) + }); } + async handleDownload(player) { - console.log("Downloading", player.player_name) + console.log("Downloading", player.player_name); let response = await this.info.messages.download.send(this.instance, { - player_name: player.player_name - }) + player_name: player.player_name, + }); if (response.inventory) { - console.log("Sending command") - await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}')`) + console.log("Sending command"); + await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}')`); } } diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index 29f080b9..d2ffd5aa 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -38,30 +38,34 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { }); }, this.master.config.get("inventory_sync.autosave_interval") * 1000); } - async uploadRequestHandler(message) { - console.log("Saving inventory for", message.data.player_name) - this.inventories[message.data.player_name] = message.data.inventory - return { success: true } + async uploadRequestHandler(message) { + console.log("Saving inventory for", message.data.player_name); + this.inventories[message.data.player_name] = message.data.inventory; + + return { success: true }; } + async downloadRequestHandler(message) { - console.log("Downloading inventory for", message.data.player_name) + console.log("Downloading inventory for", message.data.player_name); return { player_name: message.data.player_name, - inventory: this.inventories[message.data.player_name] - } + inventory: this.inventories[message.data.player_name], + }; } + async onShutdown() { clearInterval(this.autosaveId); await saveDatabase(this.master.config, this.inventories, this.logger); } + async databaseStatsRequestHandler(message) { let inventories = Object.keys(this.inventories) .map(name => ({ name, - length: this.inventories[name].length + length: this.inventories[name].length, })) - .sort((a, b) => a.stringified.length - b.stringified.length) + .sort((a, b) => a.stringified.length - b.stringified.length); return { database_size: inventories.map(x => x.length).reduce((a, b) => b - a, 0), database_entries: inventories.length, @@ -69,7 +73,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { name: inventories[0].name, size: inventories[0].length, }, - } + }; } } diff --git a/plugins/inventory_sync/web/index.jsx b/plugins/inventory_sync/web/index.jsx index 2229428e..38328bc5 100644 --- a/plugins/inventory_sync/web/index.jsx +++ b/plugins/inventory_sync/web/index.jsx @@ -7,37 +7,37 @@ import info from "../info"; import "./index.css"; function InventoryPage() { - let control = useContext(ControlContext); + let control = useContext(ControlContext); - let [statsData, updateStatsData] = useState() + let [statsData, updateStatsData] = useState(); - useEffect(() => { - (async () => { - // Get statistics - updateStatsData(await info.messages.databaseStats.send(control, {})) - })() - }, []) + 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

- } -
; + 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: }, - ]; - } + async init() { + this.pages = [ + { path: "/inventory", sidebarName: "Inventory sync", content: }, + ]; + } - onMasterConnectionEvent(event) { - if (event === "connect") { + onMasterConnectionEvent(event) { + if (event === "connect") { - } - } + } + } } From c1d0404f236a2424544d4445a2e80370f565b1d0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 9 Jun 2021 19:13:13 +0200 Subject: [PATCH 04/24] Made inventories download in chunks to prevent blocking rcon --- plugins/inventory_sync/README.md | 32 +++++++++ plugins/inventory_sync/info.js | 7 ++ plugins/inventory_sync/instance.js | 72 ++++++------------- .../inventory_sync/module/inventory_sync.lua | 41 ++++++++--- 4 files changed, 90 insertions(+), 62 deletions(-) diff --git a/plugins/inventory_sync/README.md b/plugins/inventory_sync/README.md index 8c2067a2..e01bec8a 100644 --- a/plugins/inventory_sync/README.md +++ b/plugins/inventory_sync/README.md @@ -10,3 +10,35 @@ Run the following commands in the folder Clusterio is installed to: npx clusteriomaster plugin add @clusterio/plugin-inventory_sync Substitute clusteriomaster with clusterioslave or clusterioctl if this a dedicate slave or ctl installation respectively. + +## Method of operation + +This plugin does even 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 index 03fecb81..d0001caa 100644 --- a/plugins/inventory_sync/info.js +++ b/plugins/inventory_sync/info.js @@ -39,6 +39,13 @@ InstanceConfigGroup.define({ type: "boolean", initial_value: true, }); +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({ diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index fadd0198..d1d5c881 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -8,36 +8,14 @@ const libLuaTools = require("@clusterio/lib/lua_tools"); class InstancePlugin extends libPlugin.BaseInstancePlugin { async init() { this.inventoryQueue = []; - - // setInterval(() => { - - // console.log("Connected") - // this.info.messages.upload.send(this.instance, { - // instance_id: this.instance.id, - // player: "danielv", - // inventory: "stuff", - // }) - // },5000) - - - 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}` - )) - ); - } - - onMasterConnectionEvent(event) { - console.log("Connectionevent", event); - if (event === "connect") { - } + 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) { - console.log("Uploading", 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, @@ -47,39 +25,29 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { } async handleDownload(player) { - console.log("Downloading", player.player_name); + this.logger.verbose(`Downloading ${player.player_name}`); let response = await this.info.messages.download.send(this.instance, { player_name: player.player_name, }); if (response.inventory) { - console.log("Sending command"); - await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}')`); - } - } - - async chatEventHandler(message) { - // TODO check if cross server chat is enabled - let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`; - await this.sendRcon(`/sc game.print('${libLuaTools.escapeString(content)}')`, true); - } - - sendChat(message) { - this.info.messages.chat.send(this.instance, { - instance_name: this.instance.name, - content: message, - }); - } - - async onOutput(output) { - if (output.type === "action" && output.action === "INVENTORY") { - if (this.slave.connector.connected) { - this.sendChat(output.message); - } else { - this.inventoryQueue.push(output.message); + const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size"); + const chunks = chunkify(chunkSize, 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.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`); } } } } +function chunkify(chunkSize, string) { + if (string.length <= chunkSize) { + return [string] + } else { + let chunk = string.substr(0, chunkSize) + return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))] + } +} module.exports = { InstancePlugin, diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index ebce407e..d0050cc4 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -34,24 +34,45 @@ end function inventory_sync.initiateInventoryDownload(playerIndex) game.players[playerIndex].print("Initiating inventory download...") global["inv_sync_download_start_tick "..game.players[playerIndex].name] = game.tick + if global.download_cache == nil then + global.download_cache = {} + end + global.download_cache[playerIndex] = "" clusterio_api.send_json("inventory_sync_download", { player_name = game.players[playerIndex].name }) end -- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string) with data from the master -function inventory_sync.downloadInventory(playerName, data) - local serialized_player = game.json_to_table(data) - local player = game.players[playerName] - -- game.print(serpent.block(serialized_player)) - 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)]) +function inventory_sync.downloadInventory(playerName, data, number, total) + if number then + if global.download_cache[playerName] == nil then + global.download_cache[playerName] = "" end + -- Append data to download cache + global.download_cache[playerName] = global.download_cache[playerName] .. data + end + + if number ~= total then + -- Show progress in GUI + game.players[playerName].print("Downloaded "..number.."/"..total.." parts") + + -- Request next segment + rcon.print("Segment downloaded") + else + local serialized_player = game.json_to_table(global.download_cache[playerName]) + global.download_cache[playerName] = nil -- remove data to lower mapsize + local player = game.players[playerName] + -- game.print(serpent.block(serialized_player)) + 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 + local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] + game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") end - local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] - game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") end -- Upload inventory when a player leaves the game. Triggers on restart after crash if player was online during crash. From e4bdf996f7270cfc15fe0cb10630b54f60eeb628 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Jun 2021 08:42:35 +0200 Subject: [PATCH 05/24] Serialize hotbar&logistics&data, show download progess --- plugins/inventory_sync/info.js | 10 --- plugins/inventory_sync/instance.js | 10 +-- .../inventory_sync/module/inventory_sync.lua | 90 +++++++++++++++++-- .../inventory_sync/module/progress_dialog.lua | 71 +++++++++++++++ 4 files changed, 157 insertions(+), 24 deletions(-) create mode 100644 plugins/inventory_sync/module/progress_dialog.lua diff --git a/plugins/inventory_sync/info.js b/plugins/inventory_sync/info.js index d0001caa..8699e3df 100644 --- a/plugins/inventory_sync/info.js +++ b/plugins/inventory_sync/info.js @@ -70,16 +70,6 @@ module.exports = { routes: ["/inventory"], messages: { - chat: new libLink.Event({ - type: "inventory_sync:chat", - links: ["instance-slave", "slave-master", "master-slave", "slave-instance"], - forwardTo: "master", - broadcastTo: "instance", - eventProperties: { - "instance_name": { type: "string" }, - "content": { type: "string" }, - }, - }), upload: new libLink.Request({ type: "inventory_sync:upload", links: ["instance-slave", "slave-master"], diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index d1d5c881..8f400f03 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -31,7 +31,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { }); if (response.inventory) { const chunkSize = this.instance.config.get("inventory_sync.rcon_chunk_size"); - const chunks = chunkify(chunkSize, response.inventory) + const chunks = chunkify(chunkSize, 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}`) @@ -42,11 +42,11 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { } function chunkify(chunkSize, string) { if (string.length <= chunkSize) { - return [string] - } else { - let chunk = string.substr(0, chunkSize) - return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))] + return [string]; } + let chunk = string.substr(0, chunkSize); + return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]; + } module.exports = { diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index d0050cc4..26501a90 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -6,12 +6,11 @@ When a player joins the game, send a request to the master server for the player When the master sends an inventory, parse it and give it to the player. - -packages/slave/modules/clusterio ]] local serialize = require("modules/clusterio/serialize") local clusterio_api = require("modules/clusterio/api") +local progress_dialog = require("modules/inventory_sync/progress_dialog") local inventories = { defines.inventory.character_main, @@ -32,19 +31,29 @@ inventory_sync.events[defines.events.on_player_joined_game] = function(event) end function inventory_sync.initiateInventoryDownload(playerIndex) - game.players[playerIndex].print("Initiating inventory download...") - global["inv_sync_download_start_tick "..game.players[playerIndex].name] = game.tick + local player = game.players[playerIndex] + player.print("Initiating inventory download...") + global["inv_sync_download_start_tick "..player.name] = game.tick if global.download_cache == nil then global.download_cache = {} end global.download_cache[playerIndex] = "" clusterio_api.send_json("inventory_sync_download", { - player_name = game.players[playerIndex].name + player_name = player.name }) + + -- 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 end -- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string) with data from the master function inventory_sync.downloadInventory(playerName, data, number, total) + local player = game.players[playerName] + if player == nil then return end if number then if global.download_cache[playerName] == nil then global.download_cache[playerName] = "" @@ -54,22 +63,52 @@ function inventory_sync.downloadInventory(playerName, data, number, total) end if number ~= total then + -- Show progress in console + -- game.players[playerName].print("Downloaded "..(number + 1).."/"..total.." parts") -- Show progress in GUI - game.players[playerName].print("Downloaded "..number.."/"..total.." parts") + progress_dialog(player, number+1, total) -- Request next segment rcon.print("Segment downloaded") else + -- Recreate player character + player.set_controller { + type = defines.controllers.god, + } + player.create_character() + -- Load downloaded inventory local serialized_player = game.json_to_table(global.download_cache[playerName]) global.download_cache[playerName] = nil -- remove data to lower mapsize local player = game.players[playerName] - -- game.print(serpent.block(serialized_player)) + -- 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)] + 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 + + -- Misc + 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 + local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") end @@ -78,8 +117,8 @@ 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_player_left_game] = function(event) local playerIndex = event.player_index - inventory_sync.uploadInventory(playerIndex) + global.download_cache[playerIndex] = "" -- Clear cache to avoid leaking memory to people quitting during download end function inventory_sync.uploadInventory(playerIndex) @@ -87,9 +126,42 @@ function inventory_sync.uploadInventory(playerIndex) local serialized_player = { name = player.name, - inventories = {} + 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, } + -- 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) + game.print(serpent.block(slot)) + 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 diff --git a/plugins/inventory_sync/module/progress_dialog.lua b/plugins/inventory_sync/module/progress_dialog.lua new file mode 100644 index 00000000..fc8d2d10 --- /dev/null +++ b/plugins/inventory_sync/module/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 From 5db9cad7e5ce942642efda769b5d5c4de285b672 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Jun 2021 08:51:29 +0200 Subject: [PATCH 06/24] Removed unused settings --- plugins/inventory_sync/info.js | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/plugins/inventory_sync/info.js b/plugins/inventory_sync/info.js index 8699e3df..c5e8b31f 100644 --- a/plugins/inventory_sync/info.js +++ b/plugins/inventory_sync/info.js @@ -11,34 +11,13 @@ MasterConfigGroup.define({ title: "Autosave Interval", description: "Interval the player data is autosaved at in seconds.", type: "number", - initial_value: 60, -}); -MasterConfigGroup.define({ - name: "spectator_until_ready", - title: "Spectator mode until ready", - description: "Set the player into spectator mode until the inventory is fully synchronized.", - type: "boolean", - initial_value: true, -}); -MasterConfigGroup.define({ - name: "clear_old_inventory", - title: "Clear old inventory on join", - description: "Clear old inventory on join to prevent duplication of items. Can cause greater disruption on failed or slow sync.", - type: "boolean", - initial_value: true, + initial_value: 600, // 10 minutes }); MasterConfigGroup.finalize(); class InstanceConfigGroup extends libConfig.PluginConfigGroup { } InstanceConfigGroup.defaultAccess = ["master", "slave", "control"]; InstanceConfigGroup.groupName = "inventory_sync"; -InstanceConfigGroup.define({ - name: "enable_inventory_sync", - title: "Syncronize inventories", - description: "Syncronize inventories. Disable if this instance is running incompatible mods or otherwise shouldn't sync.", - type: "boolean", - initial_value: true, -}); InstanceConfigGroup.define({ name: "rcon_chunk_size", title: "Rcon inventory chunk size", From 40301f59b7c20df05182223b1b7a54da1e73790f Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Jun 2021 10:38:09 +0200 Subject: [PATCH 07/24] Transfer all misc properties as well --- .../inventory_sync/module/inventory_sync.lua | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 26501a90..c412c641 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -103,12 +103,44 @@ function inventory_sync.downloadInventory(playerName, data, number, total) -- end end + -- Load crafting queue + for _, queueItem in pairs(serialized_player.crafting_queue) do + player.begin_crafting { + count = queueItem.count, + recipe = queueItem.recipe, + silent = true, -- Fail silently if items are missing + } + end + -- 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.character_crafting_speed_modifier = serialized_player.character_crafting_speed_modifier + player.character_mining_speed_modifier = serialized_player.character_mining_speed_modifier + player.character_additional_mining_categories = serialized_player.character_additional_mining_categories + player.character_running_speed_modifier = serialized_player.character_running_speed_modifier + player.character_build_distance_bonus = serialized_player.character_build_distance_bonus + player.character_item_drop_distance_bonus = serialized_player.character_item_drop_distance_bonus + player.character_reach_distance_bonus = serialized_player.character_reach_distance_bonus + player.character_resource_reach_distance_bonus = serialized_player.character_resource_reach_distance_bonus + player.character_item_pickup_distance_bonus = serialized_player.character_item_pickup_distance_bonus + player.character_loot_pickup_distance_bonus = serialized_player.character_loot_pickup_distance_bonus + player.character_inventory_slots_bonus = serialized_player.character_inventory_slots_bonus + player.character_trash_slot_count_bonus = serialized_player.character_trash_slot_count_bonus + player.character_maximum_following_robot_count_bonus = serialized_player.character_maximum_following_robot_count_bonus + player.character_health_bonus = serialized_player.character_health_bonus + player.character_personal_logistic_requests_enabled = serialized_player.character_personal_logistic_requests_enabled + + if serialized_player.flashlight then + player.enable_flashlight() + else + player.disable_flashlight() + end + local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") end @@ -134,6 +166,25 @@ function inventory_sync.uploadInventory(playerIndex) -- admin = player.admin, -- This is handled by other parts of clusterio personal_logistic_slots = {}, force = player.force.name, + cheat_mode = player.cheat_mode, + crafting_queue = {}, + character_crafting_speed_modifier = player.character_crafting_speed_modifier, + character_mining_speed_modifier = player.character_mining_speed_modifier, + character_additional_mining_categories = player.character_additional_mining_categories, + character_running_speed_modifier = player.character_running_speed_modifier, + character_build_distance_bonus = player.character_build_distance_bonus, + character_item_drop_distance_bonus = player.character_item_drop_distance_bonus, + character_reach_distance_bonus = player.character_reach_distance_bonus, + character_resource_reach_distance_bonus = player.character_resource_reach_distance_bonus, + character_item_pickup_distance_bonus = player.character_item_pickup_distance_bonus, + character_loot_pickup_distance_bonus = player.character_loot_pickup_distance_bonus, + character_inventory_slots_bonus = player.character_inventory_slots_bonus, + character_trash_slot_count_bonus = player.character_trash_slot_count_bonus, + character_maximum_following_robot_count_bonus = player.character_maximum_following_robot_count_bonus, + character_health_bonus = player.character_health_bonus, + character_personal_logistic_requests_enabled = player.character_personal_logistic_requests_enabled, + + flashlight = player.is_flashlight_enabled(), } -- Serialize hotbar @@ -148,7 +199,6 @@ function inventory_sync.uploadInventory(playerIndex) -- 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) - game.print(serpent.block(slot)) 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 From 87218e48037a4ddfd493e35a0c74da44fcd0607d Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Jun 2021 12:16:13 +0200 Subject: [PATCH 08/24] Support for transferring crafting queues --- plugins/inventory_sync/module/commands.lua | 17 ++ .../module/define_player_inventories.lua | 10 + .../module/download_inventory.lua | 95 ++++++++ .../inventory_sync/module/inventory_sync.lua | 207 +++--------------- .../module/load_crafting_queue.lua | 35 +++ .../inventory_sync/module/restore_crafts.lua | 19 ++ plugins/inventory_sync/module/save_crafts.lua | 55 +++++ .../module/upload_inventory.lua | 83 +++++++ 8 files changed, 339 insertions(+), 182 deletions(-) create mode 100644 plugins/inventory_sync/module/commands.lua create mode 100644 plugins/inventory_sync/module/define_player_inventories.lua create mode 100644 plugins/inventory_sync/module/download_inventory.lua create mode 100644 plugins/inventory_sync/module/load_crafting_queue.lua create mode 100644 plugins/inventory_sync/module/restore_crafts.lua create mode 100644 plugins/inventory_sync/module/save_crafts.lua create mode 100644 plugins/inventory_sync/module/upload_inventory.lua diff --git a/plugins/inventory_sync/module/commands.lua b/plugins/inventory_sync/module/commands.lua new file mode 100644 index 00000000..914c961b --- /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..86569406 --- /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/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua new file mode 100644 index 00000000..0110a0af --- /dev/null +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -0,0 +1,95 @@ +local progress_dialog = require("modules/inventory_sync/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") + +function download_inventory(playerName, data, number, total) + local player = game.players[playerName] + if player == nil then return end + if number then + if global.download_cache[playerName] == nil then + global.download_cache[playerName] = "" + end + -- Append data to download cache + global.download_cache[playerName] = global.download_cache[playerName] .. data + end + + if number ~= total then + -- Show progress in console + -- game.players[playerName].print("Downloaded "..(number + 1).."/"..total.." parts") + -- Show progress in GUI + progress_dialog(player, number+1, total) + + -- Request next segment + rcon.print("Segment downloaded") + else + -- Recreate player character + player.set_controller { + type = defines.controllers.god, + } + player.create_character() + -- Load downloaded inventory + local serialized_player = game.json_to_table(global.download_cache[playerName]) + global.download_cache[playerName] = nil -- remove data to lower mapsize + local player = game.players[playerName] + -- 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)] + 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.character_crafting_speed_modifier = serialized_player.character_crafting_speed_modifier + player.character_mining_speed_modifier = serialized_player.character_mining_speed_modifier + player.character_additional_mining_categories = serialized_player.character_additional_mining_categories + player.character_running_speed_modifier = serialized_player.character_running_speed_modifier + player.character_build_distance_bonus = serialized_player.character_build_distance_bonus + player.character_item_drop_distance_bonus = serialized_player.character_item_drop_distance_bonus + player.character_reach_distance_bonus = serialized_player.character_reach_distance_bonus + player.character_resource_reach_distance_bonus = serialized_player.character_resource_reach_distance_bonus + player.character_item_pickup_distance_bonus = serialized_player.character_item_pickup_distance_bonus + player.character_loot_pickup_distance_bonus = serialized_player.character_loot_pickup_distance_bonus + player.character_inventory_slots_bonus = serialized_player.character_inventory_slots_bonus + player.character_trash_slot_count_bonus = serialized_player.character_trash_slot_count_bonus + player.character_maximum_following_robot_count_bonus = serialized_player.character_maximum_following_robot_count_bonus + player.character_health_bonus = serialized_player.character_health_bonus + player.character_personal_logistic_requests_enabled = serialized_player.character_personal_logistic_requests_enabled + + if serialized_player.flashlight then + player.enable_flashlight() + else + player.disable_flashlight() + end + + local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] + game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") + end +end + +return download_inventory diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index c412c641..396c9afe 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -8,28 +8,38 @@ When the master sends an inventory, parse it and give it to the player. ]] -local serialize = require("modules/clusterio/serialize") local clusterio_api = require("modules/clusterio/api") -local progress_dialog = require("modules/inventory_sync/progress_dialog") - -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, -} +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") inventory_sync = {} +inventory_sync.add_commands = add_commands inventory_sync.events = {} +-- Cleanup +inventory_sync.events[defines.events.on_player_removed] = function(event) + local player = game.players[event.player_index] + + -- Remove inventory download performance counter + global["inv_sync_download_start_tick "..player.name] = nil + -- Remove stored crafting queue + global["saved_crafting_queue_"..player.name] = nil +end + -- Download inventory from master inventory_sync.events[defines.events.on_player_joined_game] = function(event) local playerIndex = event.player_index inventory_sync.initiateInventoryDownload(playerIndex) 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_player_left_game] = function(event) + local playerIndex = event.player_index + inventory_sync.uploadInventory(playerIndex) + global.download_cache[playerIndex] = "" -- Clear cache to avoid leaking memory to people quitting during download +end + function inventory_sync.initiateInventoryDownload(playerIndex) local player = game.players[playerIndex] player.print("Initiating inventory download...") @@ -50,176 +60,9 @@ function inventory_sync.initiateInventoryDownload(playerIndex) if character ~= nil then character.destroy() end end --- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string) with data from the master -function inventory_sync.downloadInventory(playerName, data, number, total) - local player = game.players[playerName] - if player == nil then return end - if number then - if global.download_cache[playerName] == nil then - global.download_cache[playerName] = "" - end - -- Append data to download cache - global.download_cache[playerName] = global.download_cache[playerName] .. data - end - - if number ~= total then - -- Show progress in console - -- game.players[playerName].print("Downloaded "..(number + 1).."/"..total.." parts") - -- Show progress in GUI - progress_dialog(player, number+1, total) - - -- Request next segment - rcon.print("Segment downloaded") - else - -- Recreate player character - player.set_controller { - type = defines.controllers.god, - } - player.create_character() - -- Load downloaded inventory - local serialized_player = game.json_to_table(global.download_cache[playerName]) - global.download_cache[playerName] = nil -- remove data to lower mapsize - local player = game.players[playerName] - -- 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)] - 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 - for _, queueItem in pairs(serialized_player.crafting_queue) do - player.begin_crafting { - count = queueItem.count, - recipe = queueItem.recipe, - silent = true, -- Fail silently if items are missing - } - end - - -- 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.character_crafting_speed_modifier = serialized_player.character_crafting_speed_modifier - player.character_mining_speed_modifier = serialized_player.character_mining_speed_modifier - player.character_additional_mining_categories = serialized_player.character_additional_mining_categories - player.character_running_speed_modifier = serialized_player.character_running_speed_modifier - player.character_build_distance_bonus = serialized_player.character_build_distance_bonus - player.character_item_drop_distance_bonus = serialized_player.character_item_drop_distance_bonus - player.character_reach_distance_bonus = serialized_player.character_reach_distance_bonus - player.character_resource_reach_distance_bonus = serialized_player.character_resource_reach_distance_bonus - player.character_item_pickup_distance_bonus = serialized_player.character_item_pickup_distance_bonus - player.character_loot_pickup_distance_bonus = serialized_player.character_loot_pickup_distance_bonus - player.character_inventory_slots_bonus = serialized_player.character_inventory_slots_bonus - player.character_trash_slot_count_bonus = serialized_player.character_trash_slot_count_bonus - player.character_maximum_following_robot_count_bonus = serialized_player.character_maximum_following_robot_count_bonus - player.character_health_bonus = serialized_player.character_health_bonus - player.character_personal_logistic_requests_enabled = serialized_player.character_personal_logistic_requests_enabled - - if serialized_player.flashlight then - player.enable_flashlight() - else - player.disable_flashlight() - end - - local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] - game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") - end -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_player_left_game] = function(event) - local playerIndex = event.player_index - inventory_sync.uploadInventory(playerIndex) - global.download_cache[playerIndex] = "" -- Clear cache to avoid leaking memory to people quitting during download -end - -function inventory_sync.uploadInventory(playerIndex) - local player = game.players[playerIndex] - - 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, - crafting_queue = {}, - character_crafting_speed_modifier = player.character_crafting_speed_modifier, - character_mining_speed_modifier = player.character_mining_speed_modifier, - character_additional_mining_categories = player.character_additional_mining_categories, - character_running_speed_modifier = player.character_running_speed_modifier, - character_build_distance_bonus = player.character_build_distance_bonus, - character_item_drop_distance_bonus = player.character_item_drop_distance_bonus, - character_reach_distance_bonus = player.character_reach_distance_bonus, - character_resource_reach_distance_bonus = player.character_resource_reach_distance_bonus, - character_item_pickup_distance_bonus = player.character_item_pickup_distance_bonus, - character_loot_pickup_distance_bonus = player.character_loot_pickup_distance_bonus, - character_inventory_slots_bonus = player.character_inventory_slots_bonus, - character_trash_slot_count_bonus = player.character_trash_slot_count_bonus, - character_maximum_following_robot_count_bonus = player.character_maximum_following_robot_count_bonus, - character_health_bonus = player.character_health_bonus, - character_personal_logistic_requests_enabled = player.character_personal_logistic_requests_enabled, - - flashlight = player.is_flashlight_enabled(), - } - - -- 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 - clusterio_api.send_json("inventory_sync_upload", serialized_player) -end +-- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string, package_number, total_packages_count) with data from the master +inventory_sync.downloadInventory = download_inventory +-- This function is called internally when a player leaves the game for their inventory to upload to the master +inventory_sync.uploadInventory = upload_inventory 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..edec5cb6 --- /dev/null +++ b/plugins/inventory_sync/module/load_crafting_queue.lua @@ -0,0 +1,35 @@ +-- 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 + 100 + + -- Load crafting queue + for _, queueItem in pairs(crafting_queue) do + local recipe = game.recipe_prototypes[queueItem.recipe] + game.print("Crafting "..queueItem.recipe) + -- Add items to inventory + for _, item in pairs(recipe.ingredients) do + local count = math.floor(item.amount * queueItem.count / math.max(recipe.main_product.amount, 1)) + if count > 0 then + inventory.insert({ + name = item.name, + count = count, + }) + end + end + + -- 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 - 100 +end + +return load_crafting_queue diff --git a/plugins/inventory_sync/module/restore_crafts.lua b/plugins/inventory_sync/module/restore_crafts.lua new file mode 100644 index 00000000..5b7650a9 --- /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.players[event.player_index] + local crafting_queue = global["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["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..b11beab1 --- /dev/null +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -0,0 +1,55 @@ +function save_crafts(event) + if event.player_index then + local player = game.players[event.player_index] + local crafting_queue = {} + + -- Avoid overwriting old save + if global["saved_crafting_queue_"..player.name] ~= nil then + player.print("You already have a saved crafting queue, restore it with /restore-crafts") + return + end + + -- Serialize crafting queue + if player.crafting_queue == nil then + player.print("You can't save nothing, you silly goose") + return + end + for _, queueItem in pairs(player.crafting_queue) do + crafting_queue[queueItem.index] = { + count = queueItem.count, + recipe = queueItem.recipe, + } + end + + -- Give player some more inventory space to avoid duplicating items + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus + 100 + -- Cancel old crafts to get the items back + while player.crafting_queue_size > 0 do + local queueItem = player.crafting_queue[1] + local recipe = game.recipe_prototypes[queueItem.recipe] + + -- Cancel craft + player.cancel_crafting { + index = 1, + count = 1, + } + + -- Remove ingredients from inventory + for _, item in pairs(recipe.ingredients) do + local inventory = player.get_main_inventory() + inventory.remove({ + name = item.name, + count = item.amount, + }) + end + game.print("Canceled craft "..recipe.name) + end + + -- Remove extra inventory slots + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 100 + + global["saved_crafting_queue_"..player.name] = crafting_queue + end +end + +return save_crafts diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua new file mode 100644 index 00000000..0394fc6c --- /dev/null +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -0,0 +1,83 @@ +local clusterio_api = require("modules/clusterio/api") +local serialize = require("modules/clusterio/serialize") +local inventories = require("modules/inventory_sync/define_player_inventories") + +function upload_inventory(playerIndex) + local player = game.players[playerIndex] + + 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, + character_crafting_speed_modifier = player.character_crafting_speed_modifier, + character_mining_speed_modifier = player.character_mining_speed_modifier, + character_additional_mining_categories = player.character_additional_mining_categories, + character_running_speed_modifier = player.character_running_speed_modifier, + character_build_distance_bonus = player.character_build_distance_bonus, + character_item_drop_distance_bonus = player.character_item_drop_distance_bonus, + character_reach_distance_bonus = player.character_reach_distance_bonus, + character_resource_reach_distance_bonus = player.character_resource_reach_distance_bonus, + character_item_pickup_distance_bonus = player.character_item_pickup_distance_bonus, + character_loot_pickup_distance_bonus = player.character_loot_pickup_distance_bonus, + character_inventory_slots_bonus = player.character_inventory_slots_bonus, + character_trash_slot_count_bonus = player.character_trash_slot_count_bonus, + character_maximum_following_robot_count_bonus = player.character_maximum_following_robot_count_bonus, + character_health_bonus = player.character_health_bonus, + character_personal_logistic_requests_enabled = player.character_personal_logistic_requests_enabled, + + flashlight = player.is_flashlight_enabled(), + } + + -- 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 + serialized_player.crafting_queue = global["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["saved_crafting_queue_"..player.name] = nil +end + +return upload_inventory From a4a667ef06220aff63402dec6b1df4e57874fc77 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Jun 2021 22:19:30 +0200 Subject: [PATCH 09/24] Reimplement core method for crafting_queue sync * New method avoids all dataloss and prefectly replicates the queue * Made proper description in readme * Added packages to make lerna build happy * Added reference to main readme --- README.md | 1 + plugins/inventory_sync/README.md | 10 ++- .../inventory_sync/module/inventory_sync.lua | 2 +- .../module/load_crafting_queue.lua | 25 +++--- plugins/inventory_sync/module/save_crafts.lua | 89 ++++++++++++++----- .../module/upload_inventory.lua | 4 +- plugins/inventory_sync/package.json | 3 + 7 files changed, 91 insertions(+), 43 deletions(-) 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/README.md b/plugins/inventory_sync/README.md index e01bec8a..f5d27ca7 100644 --- a/plugins/inventory_sync/README.md +++ b/plugins/inventory_sync/README.md @@ -2,6 +2,12 @@ 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: @@ -9,11 +15,11 @@ 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 dedicate slave or ctl installation respectively. +Substitute clusteriomaster with clusterioslave or clusterioctl if this a dedicated slave or ctl installation respectively. ## Method of operation -This plugin does even based synchronization of inventories. The data flow is as follows: +This plugin does event based synchronization of inventories. The data flow is as follows: 1. Player joins server, gets put in spectator mode diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 396c9afe..97683ce7 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -34,7 +34,7 @@ inventory_sync.events[defines.events.on_player_joined_game] = function(event) 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_player_left_game] = function(event) +inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) local playerIndex = event.player_index inventory_sync.uploadInventory(playerIndex) global.download_cache[playerIndex] = "" -- Clear cache to avoid leaking memory to people quitting during download diff --git a/plugins/inventory_sync/module/load_crafting_queue.lua b/plugins/inventory_sync/module/load_crafting_queue.lua index edec5cb6..f44d060b 100644 --- a/plugins/inventory_sync/module/load_crafting_queue.lua +++ b/plugins/inventory_sync/module/load_crafting_queue.lua @@ -3,33 +3,28 @@ 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 + 100 + 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) do + for _, queueItem in pairs(crafting_queue.crafting_queue) do local recipe = game.recipe_prototypes[queueItem.recipe] - game.print("Crafting "..queueItem.recipe) - -- Add items to inventory - for _, item in pairs(recipe.ingredients) do - local count = math.floor(item.amount * queueItem.count / math.max(recipe.main_product.amount, 1)) - if count > 0 then - inventory.insert({ - name = item.name, - count = count, - }) - end - end + -- game.print("Crafting "..queueItem.recipe) -- Start crafting (consume items) player.begin_crafting { count = queueItem.count, recipe = queueItem.recipe, - silent = true, -- Fail silently if items are missing + -- silent = true, -- Fail silently if items are missing } end -- Remove extra inventory slots - player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 100 + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000 end return load_crafting_queue diff --git a/plugins/inventory_sync/module/save_crafts.lua b/plugins/inventory_sync/module/save_crafts.lua index b11beab1..4e2cc1f2 100644 --- a/plugins/inventory_sync/module/save_crafts.lua +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -9,46 +9,87 @@ function save_crafts(event) return end - -- Serialize crafting queue - if player.crafting_queue == nil then - player.print("You can't save nothing, you silly goose") - return - end - for _, queueItem in pairs(player.crafting_queue) do - crafting_queue[queueItem.index] = { - count = queueItem.count, - recipe = queueItem.recipe, - } - end - -- Give player some more inventory space to avoid duplicating items - player.character_inventory_slots_bonus = player.character_inventory_slots_bonus + 100 + 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] - local recipe = game.recipe_prototypes[queueItem.recipe] - + -- Cancel craft player.cancel_crafting { index = 1, count = 1, } - -- Remove ingredients from inventory - for _, item in pairs(recipe.ingredients) do - local inventory = player.get_main_inventory() - inventory.remove({ - name = item.name, - count = item.amount, + 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("Canceled craft "..recipe.name) + -- 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 - 100 + player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000 - global["saved_crafting_queue_"..player.name] = crafting_queue + global["saved_crafting_queue_"..player.name] = { + crafting_queue = crafting_queue, + ingredients = difference, + } end end diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index 0394fc6c..c3e6cc50 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -1,6 +1,7 @@ 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") function upload_inventory(playerIndex) local player = game.players[playerIndex] @@ -68,7 +69,8 @@ function upload_inventory(playerIndex) end end - -- Serialize crafting queue saved by /csc or /save-crafts + -- Serialize crafting queue saved by /csc or /save-crafts OR automatically + save_crafts({player_index = player.name}) serialized_player.crafting_queue = global["saved_crafting_queue_"..player.name] if serialized_player.crafting_queue == nil then serialized_player.crafting_queue = {} diff --git a/plugins/inventory_sync/package.json b/plugins/inventory_sync/package.json index 3237132f..18a47d61 100644 --- a/plugins/inventory_sync/package.json +++ b/plugins/inventory_sync/package.json @@ -22,6 +22,9 @@ "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" From a939e11641b8d29275f3e7108c95def10482af81 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 11 Jun 2021 15:56:49 +0200 Subject: [PATCH 10/24] Fixed bug with import of logistics slots Slots were shifted 1 slot to the right every time due to off by one issue in the deserializer --- plugins/inventory_sync/module/download_inventory.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua index 0110a0af..e8c511b3 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -42,7 +42,7 @@ function download_inventory(playerName, data, number, total) -- Load personal logistics slots for i = 1, 200 do - local slot = serialized_player.personal_logistic_slots[tostring(i)] + 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 @@ -88,7 +88,9 @@ function download_inventory(playerName, data, number, total) end local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] - game.print("Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks") + local log_line = "Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks" + player.print(log_line) + log("[inventory_sync] "..log_line) end end From f9ef2ebbe172616cd37299e428b94747b5136040 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 15 Jun 2021 23:33:58 +0200 Subject: [PATCH 11/24] Microoptimizations after code review --- packages/web_ui/src/util/websocket.jsx | 6 ----- plugins/inventory_sync/instance.js | 25 ++++++++++++------- plugins/inventory_sync/master.js | 6 ++--- .../module/download_inventory.lua | 7 +++--- .../inventory_sync/module/inventory_sync.lua | 4 +-- .../module/load_crafting_queue.lua | 3 --- .../inventory_sync/module/restore_crafts.lua | 2 +- plugins/inventory_sync/module/save_crafts.lua | 2 +- .../module/upload_inventory.lua | 2 +- 9 files changed, 27 insertions(+), 30 deletions(-) diff --git a/packages/web_ui/src/util/websocket.jsx b/packages/web_ui/src/util/websocket.jsx index b08b2fc6..6b3b983f 100644 --- a/packages/web_ui/src/util/websocket.jsx +++ b/packages/web_ui/src/util/websocket.jsx @@ -273,12 +273,6 @@ export class Control extends libLink.Link { handler(info); } } - if (info.slave_id !== undefined) { - let slaveHandlers = this.slaveLogHandlers.get(info.slave_id); - for (let handler of slaveHandlers || []) { - handler(info); - } - } } async onInstanceLog(id, handler) { diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index 8f400f03..79c96da3 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -5,6 +5,20 @@ 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) { + if (string.length <= chunkSize) { + return [string]; + } + let chunk = string.substr(0, chunkSize); + return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]; +} + class InstancePlugin extends libPlugin.BaseInstancePlugin { async init() { this.inventoryQueue = []; @@ -35,19 +49,12 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { 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.downloadInventory('${response.player_name}', '${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`); + await this.sendRcon(`/sc inventory_sync.downloadInventory('${response.player_name}', ' + ${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`); } } } } -function chunkify(chunkSize, string) { - if (string.length <= chunkSize) { - return [string]; - } - let chunk = string.substr(0, chunkSize); - return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]; - -} module.exports = { InstancePlugin, diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index d2ffd5aa..bfd9e8f1 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -1,5 +1,5 @@ "use strict"; -const fs = require("fs-extra"); +const fs = require("fs").promises; const path = require("path"); const libPlugin = require("@clusterio/lib/plugin"); @@ -40,14 +40,14 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { } async uploadRequestHandler(message) { - console.log("Saving inventory for", message.data.player_name); + this.logger.verbose(`Saving inventory for ${message.data.player_name}`); this.inventories[message.data.player_name] = message.data.inventory; return { success: true }; } async downloadRequestHandler(message) { - console.log("Downloading inventory for", message.data.player_name); + this.logger.verbose(`Downloading inventory for ${message.data.player_name}`); return { player_name: message.data.player_name, inventory: this.inventories[message.data.player_name], diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua index e8c511b3..7dba1625 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -4,7 +4,7 @@ local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue" local inventories = require("modules/inventory_sync/define_player_inventories") function download_inventory(playerName, data, number, total) - local player = game.players[playerName] + local player = game.get_player(playerName) if player == nil then return end if number then if global.download_cache[playerName] == nil then @@ -16,7 +16,7 @@ function download_inventory(playerName, data, number, total) if number ~= total then -- Show progress in console - -- game.players[playerName].print("Downloaded "..(number + 1).."/"..total.." parts") + -- player.print("Downloaded "..(number + 1).."/"..total.." parts") -- Show progress in GUI progress_dialog(player, number+1, total) @@ -31,7 +31,6 @@ function download_inventory(playerName, data, number, total) -- Load downloaded inventory local serialized_player = game.json_to_table(global.download_cache[playerName]) global.download_cache[playerName] = nil -- remove data to lower mapsize - local player = game.players[playerName] -- Load inventories for _, inv in pairs(inventories) do local inventory = player.get_inventory(inv) @@ -87,7 +86,7 @@ function download_inventory(playerName, data, number, total) player.disable_flashlight() end - local startTick = global["inv_sync_download_start_tick "..game.players[playerName].name] + local startTick = global["inv_sync_download_start_tick "..player.name] local log_line = "Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks" player.print(log_line) log("[inventory_sync] "..log_line) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 97683ce7..0c5e65f3 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -19,7 +19,7 @@ inventory_sync.add_commands = add_commands inventory_sync.events = {} -- Cleanup inventory_sync.events[defines.events.on_player_removed] = function(event) - local player = game.players[event.player_index] + local player = game.get_player(event.player_index) -- Remove inventory download performance counter global["inv_sync_download_start_tick "..player.name] = nil @@ -41,7 +41,7 @@ inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) end function inventory_sync.initiateInventoryDownload(playerIndex) - local player = game.players[playerIndex] + local player = game.get_player(playerIndex) player.print("Initiating inventory download...") global["inv_sync_download_start_tick "..player.name] = game.tick if global.download_cache == nil then diff --git a/plugins/inventory_sync/module/load_crafting_queue.lua b/plugins/inventory_sync/module/load_crafting_queue.lua index f44d060b..91f43626 100644 --- a/plugins/inventory_sync/module/load_crafting_queue.lua +++ b/plugins/inventory_sync/module/load_crafting_queue.lua @@ -12,9 +12,6 @@ function load_crafting_queue(crafting_queue, player) -- Load crafting queue for _, queueItem in pairs(crafting_queue.crafting_queue) do - local recipe = game.recipe_prototypes[queueItem.recipe] - -- game.print("Crafting "..queueItem.recipe) - -- Start crafting (consume items) player.begin_crafting { count = queueItem.count, diff --git a/plugins/inventory_sync/module/restore_crafts.lua b/plugins/inventory_sync/module/restore_crafts.lua index 5b7650a9..09abb7f7 100644 --- a/plugins/inventory_sync/module/restore_crafts.lua +++ b/plugins/inventory_sync/module/restore_crafts.lua @@ -2,7 +2,7 @@ local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue" function restore_crafts(event) if event.player_index then - local player = game.players[event.player_index] + local player = game.get_player(event.player_index) local crafting_queue = global["saved_crafting_queue_"..player.name] if crafting_queue == nil then diff --git a/plugins/inventory_sync/module/save_crafts.lua b/plugins/inventory_sync/module/save_crafts.lua index 4e2cc1f2..87612616 100644 --- a/plugins/inventory_sync/module/save_crafts.lua +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -1,6 +1,6 @@ function save_crafts(event) if event.player_index then - local player = game.players[event.player_index] + local player = game.get_player(event.player_index) local crafting_queue = {} -- Avoid overwriting old save diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index c3e6cc50..14c6bdae 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -4,7 +4,7 @@ local inventories = require("modules/inventory_sync/define_player_inventories") local save_crafts = require("modules/inventory_sync/save_crafts") function upload_inventory(playerIndex) - local player = game.players[playerIndex] + local player = game.get_player(playerIndex) local serialized_player = { name = player.name, From 98f801e24430dd361a95a72c45d636de795c44e5 Mon Sep 17 00:00:00 2001 From: danielv Date: Sat, 19 Jun 2021 23:34:47 +0200 Subject: [PATCH 12/24] Changed usage of globals, worked around edge cases --- plugins/inventory_sync/info.js | 5 +- plugins/inventory_sync/instance.js | 11 +-- plugins/inventory_sync/master.js | 7 +- .../module/download_inventory.lua | 68 +++++++++++-------- .../module/ensure_character.lua | 13 ++++ .../inventory_sync/module/inventory_sync.lua | 50 +++++++++----- .../inventory_sync/module/restore_crafts.lua | 4 +- plugins/inventory_sync/module/save_crafts.lua | 4 +- .../module/upload_inventory.lua | 8 ++- .../module/welcome_new_player.lua | 13 ++++ plugins/inventory_sync/package.json | 3 + 11 files changed, 124 insertions(+), 62 deletions(-) create mode 100644 plugins/inventory_sync/module/ensure_character.lua create mode 100644 plugins/inventory_sync/module/welcome_new_player.lua diff --git a/plugins/inventory_sync/info.js b/plugins/inventory_sync/info.js index c5e8b31f..30de703a 100644 --- a/plugins/inventory_sync/info.js +++ b/plugins/inventory_sync/info.js @@ -57,7 +57,7 @@ module.exports = { "instance_id": { type: "integer" }, "instance_name": { type: "string" }, "player_name": { type: "string" }, - "inventory": { type: "string" }, + "inventory": { type: "object" }, }, responseProperties: { "success": { type: "boolean" }, @@ -72,7 +72,8 @@ module.exports = { }, responseProperties: { "player_name": { type: "string" }, - "inventory": { type: "string" }, + "inventory": { type: "object" }, + "new_player": { type: "boolean" }, }, }), databaseStats: new libLink.Request({ diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index 79c96da3..482343ae 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -34,7 +34,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { instance_id: this.instance.id, instance_name: this.instance.name, player_name: player.name, - inventory: JSON.stringify(player), + inventory: player, }); } @@ -43,14 +43,15 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { let response = await this.info.messages.download.send(this.instance, { player_name: player.player_name, }); - if (response.inventory) { + 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, response.inventory); + 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.downloadInventory('${response.player_name}', ' - ${libLuaTools.escapeString(response.inventory)}', ${i + 1}, ${chunks.length})`); + await this.sendRcon(`/sc inventory_sync.download_inventory('${response.player_name}', '${libLuaTools.escapeString(chunks[i])}', ${i + 1}, ${chunks.length})`); } } } diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index bfd9e8f1..91618d3b 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -1,5 +1,5 @@ "use strict"; -const fs = require("fs").promises; +const fs = require("fs-extra"); const path = require("path"); const libPlugin = require("@clusterio/lib/plugin"); @@ -50,7 +50,8 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { this.logger.verbose(`Downloading inventory for ${message.data.player_name}`); return { player_name: message.data.player_name, - inventory: this.inventories[message.data.player_name], + inventory: this.inventories[message.data.player_name] || {}, + new_player: !this.inventories[message.data.player_name], }; } @@ -63,7 +64,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { let inventories = Object.keys(this.inventories) .map(name => ({ name, - length: this.inventories[name].length, + length: JSON.stringify(this.inventories[name]).length, })) .sort((a, b) => a.stringified.length - b.stringified.length); return { diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua index 7dba1625..09bfc31f 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -2,16 +2,20 @@ local progress_dialog = require("modules/inventory_sync/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") -function download_inventory(playerName, data, number, total) - local player = game.get_player(playerName) - if player == nil then return end +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.download_cache[playerName] == nil then - global.download_cache[playerName] = "" + if global.inventory_sync.download_cache[player_name] == nil then + global.inventory_sync.download_cache[player_name] = "" end -- Append data to download cache - global.download_cache[playerName] = global.download_cache[playerName] .. data + global.inventory_sync.download_cache[player_name] = global.inventory_sync.download_cache[player_name] .. data end if number ~= total then @@ -24,13 +28,19 @@ function download_inventory(playerName, data, number, total) rcon.print("Segment downloaded") else -- Recreate player character - player.set_controller { - type = defines.controllers.god, - } - player.create_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.download_cache[playerName]) - global.download_cache[playerName] = nil -- remove data to lower mapsize + 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) @@ -64,21 +74,21 @@ function download_inventory(playerName, data, number, total) player.color = serialized_player.color player.chat_color = serialized_player.chat_color - player.character_crafting_speed_modifier = serialized_player.character_crafting_speed_modifier - player.character_mining_speed_modifier = serialized_player.character_mining_speed_modifier - player.character_additional_mining_categories = serialized_player.character_additional_mining_categories - player.character_running_speed_modifier = serialized_player.character_running_speed_modifier - player.character_build_distance_bonus = serialized_player.character_build_distance_bonus - player.character_item_drop_distance_bonus = serialized_player.character_item_drop_distance_bonus - player.character_reach_distance_bonus = serialized_player.character_reach_distance_bonus - player.character_resource_reach_distance_bonus = serialized_player.character_resource_reach_distance_bonus - player.character_item_pickup_distance_bonus = serialized_player.character_item_pickup_distance_bonus - player.character_loot_pickup_distance_bonus = serialized_player.character_loot_pickup_distance_bonus - player.character_inventory_slots_bonus = serialized_player.character_inventory_slots_bonus - player.character_trash_slot_count_bonus = serialized_player.character_trash_slot_count_bonus - player.character_maximum_following_robot_count_bonus = serialized_player.character_maximum_following_robot_count_bonus - player.character_health_bonus = serialized_player.character_health_bonus - player.character_personal_logistic_requests_enabled = serialized_player.character_personal_logistic_requests_enabled + character.character_crafting_speed_modifier = serialized_player.character_crafting_speed_modifier + character.character_mining_speed_modifier = serialized_player.character_mining_speed_modifier + character.character_additional_mining_categories = serialized_player.character_additional_mining_categories + character.character_running_speed_modifier = serialized_player.character_running_speed_modifier + character.character_build_distance_bonus = serialized_player.character_build_distance_bonus + character.character_item_drop_distance_bonus = serialized_player.character_item_drop_distance_bonus + character.character_reach_distance_bonus = serialized_player.character_reach_distance_bonus + character.character_resource_reach_distance_bonus = serialized_player.character_resource_reach_distance_bonus + character.character_item_pickup_distance_bonus = serialized_player.character_item_pickup_distance_bonus + character.character_loot_pickup_distance_bonus = serialized_player.character_loot_pickup_distance_bonus + character.character_inventory_slots_bonus = serialized_player.character_inventory_slots_bonus + character.character_trash_slot_count_bonus = serialized_player.character_trash_slot_count_bonus + character.character_maximum_following_robot_count_bonus = serialized_player.character_maximum_following_robot_count_bonus + character.character_health_bonus = serialized_player.character_health_bonus + character.character_personal_logistic_requests_enabled = serialized_player.character_personal_logistic_requests_enabled if serialized_player.flashlight then player.enable_flashlight() @@ -86,8 +96,8 @@ function download_inventory(playerName, data, number, total) player.disable_flashlight() end - local startTick = global["inv_sync_download_start_tick "..player.name] - local log_line = "Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks" + 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 diff --git a/plugins/inventory_sync/module/ensure_character.lua b/plugins/inventory_sync/module/ensure_character.lua new file mode 100644 index 00000000..50ea74b6 --- /dev/null +++ b/plugins/inventory_sync/module/ensure_character.lua @@ -0,0 +1,13 @@ +local function ensure_character(player) + if player.character == nil then + log("Character define:") + log(player.controller_type) + log(serpent.block(defines.controllers)) + player.set_controller { + type = defines.controllers.god, + } + player.create_character() + end +end + +return ensure_character diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 0c5e65f3..915192ce 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -5,6 +5,7 @@ When a player leaves the game, serialize their inventory and upload it to the ma When a player joins the game, 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. ]] @@ -12,42 +13,62 @@ 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/welcome_new_player") 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 = { + download_start_tick = {}, + saved_crafting_queue = {}, + download_cache = {}, + } + end +end + -- Cleanup inventory_sync.events[defines.events.on_player_removed] = function(event) local player = game.get_player(event.player_index) -- Remove inventory download performance counter - global["inv_sync_download_start_tick "..player.name] = nil + global.inventory_sync.download_start_tick[player.name] = nil -- Remove stored crafting queue - global["saved_crafting_queue_"..player.name] = nil + global.inventory_sync.saved_crafting_queue[player.name] = nil end -- Download inventory from master inventory_sync.events[defines.events.on_player_joined_game] = function(event) local playerIndex = event.player_index - inventory_sync.initiateInventoryDownload(playerIndex) + inventory_sync.initiate_inventory_download(playerIndex) 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) - local playerIndex = event.player_index - inventory_sync.uploadInventory(playerIndex) - global.download_cache[playerIndex] = "" -- Clear cache to avoid leaking memory to people quitting during download + -- 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 + 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 -function inventory_sync.initiateInventoryDownload(playerIndex) +function inventory_sync.initiate_inventory_download(playerIndex) local player = game.get_player(playerIndex) player.print("Initiating inventory download...") - global["inv_sync_download_start_tick "..player.name] = game.tick - if global.download_cache == nil then - global.download_cache = {} - end - global.download_cache[playerIndex] = "" + global.inventory_sync.download_start_tick[player.name] = game.tick + global.inventory_sync.download_cache[playerIndex] = "" clusterio_api.send_json("inventory_sync_download", { player_name = player.name }) @@ -60,9 +81,4 @@ function inventory_sync.initiateInventoryDownload(playerIndex) if character ~= nil then character.destroy() end end --- This function is called by instance.js as /sc inventory_sync.downloadInventory("danielv", Escaped JSON string, package_number, total_packages_count) with data from the master -inventory_sync.downloadInventory = download_inventory --- This function is called internally when a player leaves the game for their inventory to upload to the master -inventory_sync.uploadInventory = upload_inventory - return inventory_sync diff --git a/plugins/inventory_sync/module/restore_crafts.lua b/plugins/inventory_sync/module/restore_crafts.lua index 09abb7f7..b8188ed5 100644 --- a/plugins/inventory_sync/module/restore_crafts.lua +++ b/plugins/inventory_sync/module/restore_crafts.lua @@ -3,7 +3,7 @@ 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["saved_crafting_queue_"..player.name] + local crafting_queue = global.inventory_sync.saved_crafting_queue[player.name] if crafting_queue == nil then player.print("No saved crafting queue") @@ -12,7 +12,7 @@ function restore_crafts(event) load_crafting_queue(crafting_queue, player) - global["saved_crafting_queue_"..player.name] = nil + global.inventory_sync.saved_crafting_queue[player.name] = nil end end diff --git a/plugins/inventory_sync/module/save_crafts.lua b/plugins/inventory_sync/module/save_crafts.lua index 87612616..b6e7854c 100644 --- a/plugins/inventory_sync/module/save_crafts.lua +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -4,7 +4,7 @@ function save_crafts(event) local crafting_queue = {} -- Avoid overwriting old save - if global["saved_crafting_queue_"..player.name] ~= nil then + 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 @@ -86,7 +86,7 @@ function save_crafts(event) -- Remove extra inventory slots player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000 - global["saved_crafting_queue_"..player.name] = { + global.inventory_sync.saved_crafting_queue[player.name] = { crafting_queue = crafting_queue, ingredients = difference, } diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index 14c6bdae..c0deb814 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -2,10 +2,14 @@ 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 ensure_character = require("modules/inventory_sync/ensure_character") function upload_inventory(playerIndex) local player = game.get_player(playerIndex) + -- Force player to have a character to prevent access errors + ensure_character(player) + local serialized_player = { name = player.name, inventories = {}, @@ -71,7 +75,7 @@ function upload_inventory(playerIndex) -- Serialize crafting queue saved by /csc or /save-crafts OR automatically save_crafts({player_index = player.name}) - serialized_player.crafting_queue = global["saved_crafting_queue_"..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 @@ -79,7 +83,7 @@ function upload_inventory(playerIndex) clusterio_api.send_json("inventory_sync_upload", serialized_player) -- Clear saved crafting queue - global["saved_crafting_queue_"..player.name] = nil + global.inventory_sync.saved_crafting_queue[player.name] = nil end return upload_inventory diff --git a/plugins/inventory_sync/module/welcome_new_player.lua b/plugins/inventory_sync/module/welcome_new_player.lua new file mode 100644 index 00000000..11090a72 --- /dev/null +++ b/plugins/inventory_sync/module/welcome_new_player.lua @@ -0,0 +1,13 @@ +local ensure_character = require("modules/inventory_sync/ensure_character") + +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.") + + ensure_character(player) +end + +return welcome_new_player diff --git a/plugins/inventory_sync/package.json b/plugins/inventory_sync/package.json index 18a47d61..fe371d35 100644 --- a/plugins/inventory_sync/package.json +++ b/plugins/inventory_sync/package.json @@ -19,6 +19,9 @@ "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", From 37052e1f681145a70606242c6b67112f079fb940 Mon Sep 17 00:00:00 2001 From: danielv Date: Sun, 20 Jun 2021 00:42:14 +0200 Subject: [PATCH 13/24] Changed inventory storage to use Map --- plugins/inventory_sync/info.js | 4 ---- plugins/inventory_sync/instance.js | 12 ++++-------- plugins/inventory_sync/master.js | 23 +++++++++++------------ 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/plugins/inventory_sync/info.js b/plugins/inventory_sync/info.js index 30de703a..5c80e838 100644 --- a/plugins/inventory_sync/info.js +++ b/plugins/inventory_sync/info.js @@ -59,9 +59,6 @@ module.exports = { "player_name": { type: "string" }, "inventory": { type: "object" }, }, - responseProperties: { - "success": { type: "boolean" }, - }, }), download: new libLink.Request({ type: "inventory_sync:download", @@ -80,7 +77,6 @@ module.exports = { type: "inventory_sync:databaseStats", links: ["control-master"], permission: "inventory_sync.inventory.view", - requestProperties: {}, responseProperties: { "database_size": { type: "integer" }, "database_entries": { type: "integer" }, diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index 482343ae..e3a40c5d 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -12,16 +12,11 @@ const libLuaTools = require("@clusterio/lib/lua_tools"); * @returns {String[]} Chunks */ function chunkify(chunkSize, string) { - if (string.length <= chunkSize) { - return [string]; - } - let chunk = string.substr(0, chunkSize); - return [chunk, ...chunkify(chunkSize, string.replace(chunk, ""))]; + return string.match(new RegExp(`.{1,${chunkSize}}`, "g")); } class InstancePlugin extends libPlugin.BaseInstancePlugin { async init() { - this.inventoryQueue = []; 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) @@ -44,14 +39,15 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { player_name: player.player_name, }); if (response.new_player) { - await this.sendRcon(`/sc inventory_sync.welcome_new_player('${response.player_name}')`) + 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})`); + await this.sendRcon(`/sc inventory_sync.download_inventory('${response.player_name}',` + + `'${libLuaTools.escapeString(chunks[i])}', ${i + 1}, ${chunks.length})`, true); } } } diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index 91618d3b..31f2f262 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -9,12 +9,12 @@ async function loadDatabase(config, logger) { logger.verbose(`Loading ${itemsPath}`); try { let content = await fs.readFile(itemsPath); - return JSON.parse(content); + return new Map(JSON.parse(content)); } catch (err) { if (err.code === "ENOENT") { logger.verbose("Creating new inventory database"); - return {}; + return new Map(); } throw err; } @@ -24,7 +24,7 @@ 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(inventories); + let content = JSON.stringify(Array.from(inventories)); await fs.outputFile(file, content); } } @@ -41,17 +41,16 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { async uploadRequestHandler(message) { this.logger.verbose(`Saving inventory for ${message.data.player_name}`); - this.inventories[message.data.player_name] = message.data.inventory; - - return { success: true }; + 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: this.inventories[message.data.player_name] || {}, - new_player: !this.inventories[message.data.player_name], + inventory: inventory || {}, + new_player: !inventory, }; } @@ -61,18 +60,18 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { } async databaseStatsRequestHandler(message) { - let inventories = Object.keys(this.inventories) + let inventories = Array.from(this.inventories.keys()) .map(name => ({ name, - length: JSON.stringify(this.inventories[name]).length, + length: JSON.stringify(this.inventories.get(name)).length, })) .sort((a, b) => a.stringified.length - b.stringified.length); return { database_size: inventories.map(x => x.length).reduce((a, b) => b - a, 0), database_entries: inventories.length, largest_entry: { - name: inventories[0].name, - size: inventories[0].length, + name: inventories[0]?.name || "-", + size: inventories[0]?.length || 0, }, }; } From 4027a4f310831084c6e6fd7d3482a98c09840859 Mon Sep 17 00:00:00 2001 From: danielv Date: Mon, 21 Jun 2021 13:19:32 +0200 Subject: [PATCH 14/24] Cleanup, sync more player properties --- plugins/inventory_sync/instance.js | 2 +- .../module/define_player_stat_keys.lua | 23 +++++++++++++++++++ .../module/download_inventory.lua | 20 ++++------------ .../module/ensure_character.lua | 3 --- .../module/upload_inventory.lua | 22 +++++------------- 5 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 plugins/inventory_sync/module/define_player_stat_keys.lua diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index e3a40c5d..450eba69 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -47,7 +47,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { 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})`, true); + `'${libLuaTools.escapeString(chunks[i])}', ${i + 1}, ${chunks.length})`); } } } 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..026c5a88 --- /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 index 09bfc31f..310126b8 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -3,6 +3,7 @@ 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) @@ -74,21 +75,10 @@ function download_inventory(player_name, data, number, total) player.color = serialized_player.color player.chat_color = serialized_player.chat_color - character.character_crafting_speed_modifier = serialized_player.character_crafting_speed_modifier - character.character_mining_speed_modifier = serialized_player.character_mining_speed_modifier - character.character_additional_mining_categories = serialized_player.character_additional_mining_categories - character.character_running_speed_modifier = serialized_player.character_running_speed_modifier - character.character_build_distance_bonus = serialized_player.character_build_distance_bonus - character.character_item_drop_distance_bonus = serialized_player.character_item_drop_distance_bonus - character.character_reach_distance_bonus = serialized_player.character_reach_distance_bonus - character.character_resource_reach_distance_bonus = serialized_player.character_resource_reach_distance_bonus - character.character_item_pickup_distance_bonus = serialized_player.character_item_pickup_distance_bonus - character.character_loot_pickup_distance_bonus = serialized_player.character_loot_pickup_distance_bonus - character.character_inventory_slots_bonus = serialized_player.character_inventory_slots_bonus - character.character_trash_slot_count_bonus = serialized_player.character_trash_slot_count_bonus - character.character_maximum_following_robot_count_bonus = serialized_player.character_maximum_following_robot_count_bonus - character.character_health_bonus = serialized_player.character_health_bonus - character.character_personal_logistic_requests_enabled = serialized_player.character_personal_logistic_requests_enabled + -- Player stats + for _,v in pairs(player_stat_keys) do + character[v] = serialized_player[v] + end if serialized_player.flashlight then player.enable_flashlight() diff --git a/plugins/inventory_sync/module/ensure_character.lua b/plugins/inventory_sync/module/ensure_character.lua index 50ea74b6..464fbe11 100644 --- a/plugins/inventory_sync/module/ensure_character.lua +++ b/plugins/inventory_sync/module/ensure_character.lua @@ -1,8 +1,5 @@ local function ensure_character(player) if player.character == nil then - log("Character define:") - log(player.controller_type) - log(serpent.block(defines.controllers)) player.set_controller { type = defines.controllers.god, } diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index c0deb814..60476ac9 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -3,6 +3,7 @@ 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 ensure_character = require("modules/inventory_sync/ensure_character") +local player_stat_keys = require("modules/inventory_sync/define_player_stat_keys") function upload_inventory(playerIndex) local player = game.get_player(playerIndex) @@ -21,25 +22,14 @@ function upload_inventory(playerIndex) personal_logistic_slots = {}, force = player.force.name, cheat_mode = player.cheat_mode, - character_crafting_speed_modifier = player.character_crafting_speed_modifier, - character_mining_speed_modifier = player.character_mining_speed_modifier, - character_additional_mining_categories = player.character_additional_mining_categories, - character_running_speed_modifier = player.character_running_speed_modifier, - character_build_distance_bonus = player.character_build_distance_bonus, - character_item_drop_distance_bonus = player.character_item_drop_distance_bonus, - character_reach_distance_bonus = player.character_reach_distance_bonus, - character_resource_reach_distance_bonus = player.character_resource_reach_distance_bonus, - character_item_pickup_distance_bonus = player.character_item_pickup_distance_bonus, - character_loot_pickup_distance_bonus = player.character_loot_pickup_distance_bonus, - character_inventory_slots_bonus = player.character_inventory_slots_bonus, - character_trash_slot_count_bonus = player.character_trash_slot_count_bonus, - character_maximum_following_robot_count_bonus = player.character_maximum_following_robot_count_bonus, - character_health_bonus = player.character_health_bonus, - character_personal_logistic_requests_enabled = player.character_personal_logistic_requests_enabled, - 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] = {} From 1a1b4fb0d2e967d6bf5c2b73a1a374485efaa235 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 12 Jul 2021 23:28:16 +0200 Subject: [PATCH 15/24] Fix invincible ghost character creation on first join Inventory sync will now wait intil the player has a character. This means sync won't happen ex during respawn animations or freeplay intro cutscenes. --- .../inventory_sync/module/inventory_sync.lua | 40 +++++++++++++++---- .../module/upload_inventory.lua | 8 ++-- .../module/welcome_new_player.lua | 4 -- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 915192ce..23c77f1c 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -2,7 +2,8 @@ When a player leaves the game, serialize their inventory and upload it to the master. -When a player joins the game, send a request to the master server for the players inventory. +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. @@ -31,6 +32,7 @@ inventory_sync.events[clusterio_api.events.on_server_startup] = function(event) download_start_tick = {}, saved_crafting_queue = {}, download_cache = {}, + players_waiting_for_download = {}, } end end @@ -47,8 +49,15 @@ end -- Download inventory from master inventory_sync.events[defines.events.on_player_joined_game] = function(event) - local playerIndex = event.player_index - inventory_sync.initiate_inventory_download(playerIndex) + -- 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) + inventory_sync.check_player_character_before_download() end -- Upload inventory when a player leaves the game. Triggers on restart after crash if player was online during crash. @@ -60,15 +69,30 @@ inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) end local player_index = event.player_index - inventory_sync.upload_inventory(player_index) - global.inventory_sync.download_cache[player_index] = "" -- Clear cache to avoid leaking memory to people quitting during download + 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.initiate_inventory_download(playerIndex) - local player = game.get_player(playerIndex) +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[playerIndex] = "" + global.inventory_sync.download_cache[player.name] = "" clusterio_api.send_json("inventory_sync_download", { player_name = player.name }) diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index 60476ac9..e6368850 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -2,14 +2,16 @@ 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 ensure_character = require("modules/inventory_sync/ensure_character") local player_stat_keys = require("modules/inventory_sync/define_player_stat_keys") function upload_inventory(playerIndex) local player = game.get_player(playerIndex) - -- Force player to have a character to prevent access errors - ensure_character(player) + 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, diff --git a/plugins/inventory_sync/module/welcome_new_player.lua b/plugins/inventory_sync/module/welcome_new_player.lua index 11090a72..f0d3976f 100644 --- a/plugins/inventory_sync/module/welcome_new_player.lua +++ b/plugins/inventory_sync/module/welcome_new_player.lua @@ -1,13 +1,9 @@ -local ensure_character = require("modules/inventory_sync/ensure_character") - 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.") - - ensure_character(player) end return welcome_new_player From 29917fb85abadb6a4aa2b678d217d817d01f7a3c Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 12 Jul 2021 23:30:41 +0200 Subject: [PATCH 16/24] Maintain nodejs v12 support --- plugins/inventory_sync/master.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index 31f2f262..ed60320a 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -70,8 +70,8 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { database_size: inventories.map(x => x.length).reduce((a, b) => b - a, 0), database_entries: inventories.length, largest_entry: { - name: inventories[0]?.name || "-", - size: inventories[0]?.length || 0, + name: inventories[0] && inventories[0].name || "-", + size: inventories[0] && inventories[0].length || 0, }, }; } From e63f1246da5b11ec657efd581f98220c27b1c865 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 29 Jul 2021 23:55:48 +0200 Subject: [PATCH 17/24] Offer the player a temporary inventory while waiting for master --- plugins/inventory_sync/instance.js | 3 + .../module/download_inventory.lua | 13 +++- .../module/gui/dialog_failed_download.lua | 60 +++++++++++++++++++ .../module/gui/handle_gui_events.lua | 23 +++++++ .../module/{ => gui}/progress_dialog.lua | 0 .../module/{ => gui}/welcome_new_player.lua | 0 .../inventory_sync/module/inventory_sync.lua | 46 +++++++++++++- .../module/script/clean_dirty_inventory.lua | 29 +++++++++ 8 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 plugins/inventory_sync/module/gui/dialog_failed_download.lua create mode 100644 plugins/inventory_sync/module/gui/handle_gui_events.lua rename plugins/inventory_sync/module/{ => gui}/progress_dialog.lua (100%) rename plugins/inventory_sync/module/{ => gui}/welcome_new_player.lua (100%) create mode 100644 plugins/inventory_sync/module/script/clean_dirty_inventory.lua diff --git a/plugins/inventory_sync/instance.js b/plugins/inventory_sync/instance.js index 450eba69..7dbc83ff 100644 --- a/plugins/inventory_sync/instance.js +++ b/plugins/inventory_sync/instance.js @@ -17,6 +17,7 @@ function chunkify(chunkSize, string) { 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) @@ -35,6 +36,8 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { 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, }); diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua index 310126b8..590b8156 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -1,4 +1,4 @@ -local progress_dialog = require("modules/inventory_sync/progress_dialog") +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") @@ -27,7 +27,18 @@ function download_inventory(player_name, data, number, 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) 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..50074622 --- /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..259cc0a7 --- /dev/null +++ b/plugins/inventory_sync/module/gui/handle_gui_events.lua @@ -0,0 +1,23 @@ +local ensure_character = require("modules/inventory_sync/ensure_character") + +-- on_gui_click +local handle_gui_events = function(event) + local player = game.get_player(event.player_index) + local element = event.element + if 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/progress_dialog.lua b/plugins/inventory_sync/module/gui/progress_dialog.lua similarity index 100% rename from plugins/inventory_sync/module/progress_dialog.lua rename to plugins/inventory_sync/module/gui/progress_dialog.lua diff --git a/plugins/inventory_sync/module/welcome_new_player.lua b/plugins/inventory_sync/module/gui/welcome_new_player.lua similarity index 100% rename from plugins/inventory_sync/module/welcome_new_player.lua rename to plugins/inventory_sync/module/gui/welcome_new_player.lua diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 23c77f1c..7a7019d3 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -14,7 +14,10 @@ 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/welcome_new_player") +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") inventory_sync = {} inventory_sync.welcome_new_player = welcome_new_player @@ -33,6 +36,7 @@ inventory_sync.events[clusterio_api.events.on_server_startup] = function(event) saved_crafting_queue = {}, download_cache = {}, players_waiting_for_download = {}, + players = {}, } end end @@ -45,8 +49,20 @@ inventory_sync.events[defines.events.on_player_removed] = function(event) 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.events[defines.events.on_player_created] = function(event) + local player = game.get_player(event.player_index) + global.inventory_sync.players[player.name] = { + dirty_inventory = false, -- Player has a temporary non-synced inventory that should be persisted + sync_start_tick = 0, -- To track download failure timeout + } +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 @@ -57,7 +73,11 @@ 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. @@ -97,12 +117,36 @@ function inventory_sync.initiate_inventory_download(player) 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/script/clean_dirty_inventory.lua b/plugins/inventory_sync/module/script/clean_dirty_inventory.lua new file mode 100644 index 00000000..9a7b7c72 --- /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 From 2ffb848fe69a356410b311529c20f2465e446be1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 29 Jul 2021 23:59:37 +0200 Subject: [PATCH 18/24] Replace spaces with tabs --- plugins/inventory_sync/module/commands.lua | 18 +- .../module/define_player_inventories.lua | 12 +- .../module/define_player_stat_keys.lua | 42 ++--- .../module/download_inventory.lua | 160 ++++++++--------- .../module/ensure_character.lua | 12 +- .../module/gui/dialog_failed_download.lua | 60 +++---- .../module/gui/handle_gui_events.lua | 30 ++-- .../module/gui/progress_dialog.lua | 64 +++---- .../module/gui/welcome_new_player.lua | 10 +- .../inventory_sync/module/inventory_sync.lua | 162 ++++++++--------- .../module/load_crafting_queue.lua | 36 ++-- .../inventory_sync/module/restore_crafts.lua | 20 +-- plugins/inventory_sync/module/save_crafts.lua | 166 +++++++++--------- .../module/script/clean_dirty_inventory.lua | 28 +-- .../module/upload_inventory.lua | 126 ++++++------- 15 files changed, 473 insertions(+), 473 deletions(-) diff --git a/plugins/inventory_sync/module/commands.lua b/plugins/inventory_sync/module/commands.lua index 914c961b..bce7a58a 100644 --- a/plugins/inventory_sync/module/commands.lua +++ b/plugins/inventory_sync/module/commands.lua @@ -2,16 +2,16 @@ 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. - ]] + --[[ + 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) + 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 index 86569406..df13de05 100644 --- a/plugins/inventory_sync/module/define_player_inventories.lua +++ b/plugins/inventory_sync/module/define_player_inventories.lua @@ -1,10 +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, + 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 index 026c5a88..729d4c66 100644 --- a/plugins/inventory_sync/module/define_player_stat_keys.lua +++ b/plugins/inventory_sync/module/define_player_stat_keys.lua @@ -1,23 +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", + "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 index 590b8156..c669e0c6 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -6,102 +6,102 @@ 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 + 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) + 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") + -- 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 + -- 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 + -- Handle dirty inventory + if global.inventory_sync.players[player_name].dirty_inventory then + clean_dirty_inventory(player) + end - -- Recreate player character - ensure_character(player) + -- 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 + -- 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 + 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 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 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 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 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) + -- 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 + -- 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 + -- 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 + 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 + 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 index 464fbe11..bb962f96 100644 --- a/plugins/inventory_sync/module/ensure_character.lua +++ b/plugins/inventory_sync/module/ensure_character.lua @@ -1,10 +1,10 @@ local function ensure_character(player) - if player.character == nil then - player.set_controller { - type = defines.controllers.god, - } - player.create_character() - end + 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 index 50074622..4730de6a 100644 --- a/plugins/inventory_sync/module/gui/dialog_failed_download.lua +++ b/plugins/inventory_sync/module/gui/dialog_failed_download.lua @@ -1,31 +1,31 @@ -- 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 + 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 frame = player.gui.screen.add { + name = "dialog_failed_download", + type = "frame", + direction = "vertical", + } - local titlebar = frame.add { - type = "flow", - } - titlebar.drag_target = frame + 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 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", - } + 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 @@ -46,15 +46,15 @@ function dialog_failed_download(player) 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.", - } + 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 + 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 index 259cc0a7..c2ed1dfb 100644 --- a/plugins/inventory_sync/module/gui/handle_gui_events.lua +++ b/plugins/inventory_sync/module/gui/handle_gui_events.lua @@ -2,22 +2,22 @@ local ensure_character = require("modules/inventory_sync/ensure_character") -- on_gui_click local handle_gui_events = function(event) - local player = game.get_player(event.player_index) - local element = event.element - if 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 + local player = game.get_player(event.player_index) + local element = event.element + if 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 + -- 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 index fc8d2d10..74da1532 100644 --- a/plugins/inventory_sync/module/gui/progress_dialog.lua +++ b/plugins/inventory_sync/module/gui/progress_dialog.lua @@ -1,34 +1,34 @@ -- 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 + 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 frame = player.gui.screen.add { + name = "inventory_sync_progress", + type = "frame", + direction = "vertical", + } - local titlebar = frame.add { - type = "flow", - } - titlebar.drag_target = frame + 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 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", - } + 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 @@ -58,14 +58,14 @@ function open_dialog(player, 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 + 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 + 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 index f0d3976f..ba88c49f 100644 --- a/plugins/inventory_sync/module/gui/welcome_new_player.lua +++ b/plugins/inventory_sync/module/gui/welcome_new_player.lua @@ -1,9 +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.") + 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 index 7a7019d3..7c2f59ec 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -29,124 +29,124 @@ 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 = { - download_start_tick = {}, - saved_crafting_queue = {}, - download_cache = {}, - players_waiting_for_download = {}, - players = {}, - } - end + -- set up global table + if not global.inventory_sync then + global.inventory_sync = { + download_start_tick = {}, + saved_crafting_queue = {}, + download_cache = {}, + players_waiting_for_download = {}, + players = {}, + } + end end -- Cleanup inventory_sync.events[defines.events.on_player_removed] = function(event) - local player = game.get_player(event.player_index) + local player = game.get_player(event.player_index) - -- 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 + -- 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.events[defines.events.on_player_created] = function(event) - local player = game.get_player(event.player_index) - global.inventory_sync.players[player.name] = { - dirty_inventory = false, -- Player has a temporary non-synced inventory that should be persisted - sync_start_tick = 0, -- To track download failure timeout - } + local player = game.get_player(event.player_index) + global.inventory_sync.players[player.name] = { + dirty_inventory = false, -- Player has a temporary non-synced inventory that should be persisted + sync_start_tick = 0, -- To track download failure timeout + } 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() + -- 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() + -- 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() + -- 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 + -- 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 + 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 + -- 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 + 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 - }) + 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 + -- 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 + -- 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 + -- 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 + -- 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 + 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 index 91f43626..74762cb8 100644 --- a/plugins/inventory_sync/module/load_crafting_queue.lua +++ b/plugins/inventory_sync/module/load_crafting_queue.lua @@ -1,27 +1,27 @@ -- Load crafting queue from a table function load_crafting_queue(crafting_queue, player) - local inventory = player.get_main_inventory() + 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 + -- 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 + -- 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 + -- 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 + -- 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/restore_crafts.lua b/plugins/inventory_sync/module/restore_crafts.lua index b8188ed5..1779018d 100644 --- a/plugins/inventory_sync/module/restore_crafts.lua +++ b/plugins/inventory_sync/module/restore_crafts.lua @@ -1,19 +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 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 + if crafting_queue == nil then + player.print("No saved crafting queue") + return + end - load_crafting_queue(crafting_queue, player) + load_crafting_queue(crafting_queue, player) - global.inventory_sync.saved_crafting_queue[player.name] = nil - end + 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 index b6e7854c..42ebbd93 100644 --- a/plugins/inventory_sync/module/save_crafts.lua +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -1,96 +1,96 @@ function save_crafts(event) - if event.player_index then - local player = game.get_player(event.player_index) - local crafting_queue = {} + 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 + -- 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 + -- 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() + -- 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, - } + -- 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 + 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 + -- 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 + -- 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 + -- 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 + 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 index 9a7b7c72..085050d8 100644 --- a/plugins/inventory_sync/module/script/clean_dirty_inventory.lua +++ b/plugins/inventory_sync/module/script/clean_dirty_inventory.lua @@ -8,22 +8,22 @@ 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 + 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) + 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 + -- 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 index e6368850..acb1de27 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -5,77 +5,77 @@ 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) + 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 + 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(), - } + 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 + -- 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 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 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 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 + -- 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 + 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 From f72995ec8b091c590d996a67403918838267a7a9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 1 Aug 2021 21:54:46 +0200 Subject: [PATCH 19/24] Remove control plugin --- plugins/inventory_sync/control.js | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 plugins/inventory_sync/control.js diff --git a/plugins/inventory_sync/control.js b/plugins/inventory_sync/control.js deleted file mode 100644 index 680e79af..00000000 --- a/plugins/inventory_sync/control.js +++ /dev/null @@ -1,30 +0,0 @@ -"use strict"; -const libPlugin = require("@clusterio/lib/plugin"); -const info = require("./info"); -const libCommand = require("@clusterio/lib/command"); - - -const globalChatCommands = new libCommand.CommandTree({ - name: "global-chat", description: "Global Chat plugin commands", -}); -globalChatCommands.add(new libCommand.Command({ - definition: ["shout ", "Send message to all instances", (yargs) => { - yargs.positional("message", { describe: "message to send", type: "string" }); - }], - handler: async function(args, control) { - await info.messages.chat.send(control, { - instance_name: "Console", - content: args.message, - }); - }, -})); - -class ControlPlugin extends libPlugin.BaseControlPlugin { - async addCommands(rootCommand) { - rootCommand.add(globalChatCommands); - } -} - -module.exports = { - ControlPlugin, -}; From ecbce16c99533bed6c1bdd52e38feabe10531637 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 10 Aug 2021 17:41:40 +0200 Subject: [PATCH 20/24] Be more defensive with global table initialization When upgrading or downgrading the inventory_sync plugin it's possible an different structure of the global.inventory_sync table is left behind which could cause the code to crash when certain sub tables are not present. Be more defensive in the initialization of the table to make sure all sub-tables are present on startup. --- .../inventory_sync/module/inventory_sync.lua | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 7c2f59ec..72b7c1f7 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -29,15 +29,24 @@ 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 + -- Set up global table if not global.inventory_sync then - global.inventory_sync = { - download_start_tick = {}, - saved_crafting_queue = {}, - download_cache = {}, - players_waiting_for_download = {}, - players = {}, - } + 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 end From d126e7f25a6baf740244398c307f134d1d3567e9 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 10 Aug 2021 18:19:18 +0200 Subject: [PATCH 21/24] Validate GUI element in on_gui_click Make sure the GUI element in the on_gui_click event is valid before attempting to access this, as another module may have destroyed it during its event handler. --- plugins/inventory_sync/module/gui/handle_gui_events.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/inventory_sync/module/gui/handle_gui_events.lua b/plugins/inventory_sync/module/gui/handle_gui_events.lua index c2ed1dfb..5935c058 100644 --- a/plugins/inventory_sync/module/gui/handle_gui_events.lua +++ b/plugins/inventory_sync/module/gui/handle_gui_events.lua @@ -2,9 +2,11 @@ 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) - local element = event.element - if element.name == "inventory_sync_failed_download_abort" then + 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 From 60e434e4ac4ea2274e9203b78509106440056105 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 13 Aug 2021 06:17:59 +0200 Subject: [PATCH 22/24] Fix inventory_sync player created/removed Create entries for players that are present in the save on server startup to preserving their inventory and fix attempting to reference the player object after it had been removed in on_player_removed. --- .../inventory_sync/module/inventory_sync.lua | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 72b7c1f7..a8c69ed0 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -19,6 +19,24 @@ 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 @@ -48,26 +66,21 @@ inventory_sync.events[clusterio_api.events.on_server_startup] = function(event) 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_player_removed] = function(event) - local player = game.get_player(event.player_index) - - -- 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 +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) - local player = game.get_player(event.player_index) - global.inventory_sync.players[player.name] = { - dirty_inventory = false, -- Player has a temporary non-synced inventory that should be persisted - sync_start_tick = 0, -- To track download failure timeout - } + create_player(game.get_player(event.player_index), false) end inventory_sync.events[defines.events.on_gui_click] = handle_gui_events From 4a29c13acb50979842e6ff5d3ebbbe3073fc35d1 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 13 Aug 2021 06:21:32 +0200 Subject: [PATCH 23/24] Fix bad reference in inventory_sync stat request --- plugins/inventory_sync/master.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory_sync/master.js b/plugins/inventory_sync/master.js index ed60320a..789a130c 100644 --- a/plugins/inventory_sync/master.js +++ b/plugins/inventory_sync/master.js @@ -65,7 +65,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { name, length: JSON.stringify(this.inventories.get(name)).length, })) - .sort((a, b) => a.stringified.length - b.stringified.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, From cbfc0adf3de65d094031f0c83cc6b1a9b25eed1f Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 13 Aug 2021 06:22:14 +0200 Subject: [PATCH 24/24] Remove extra whitespace in inventory_sync plugin --- plugins/inventory_sync/README.md | 2 +- .../module/define_player_inventories.lua | 2 +- plugins/inventory_sync/module/download_inventory.lua | 2 +- .../inventory_sync/module/gui/handle_gui_events.lua | 2 +- plugins/inventory_sync/module/inventory_sync.lua | 6 +++--- plugins/inventory_sync/module/save_crafts.lua | 10 +++++----- plugins/inventory_sync/module/upload_inventory.lua | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/plugins/inventory_sync/README.md b/plugins/inventory_sync/README.md index f5d27ca7..0ef56ab1 100644 --- a/plugins/inventory_sync/README.md +++ b/plugins/inventory_sync/README.md @@ -45,6 +45,6 @@ This plugin does event based synchronization of inventories. The data flow is as 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 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/module/define_player_inventories.lua b/plugins/inventory_sync/module/define_player_inventories.lua index df13de05..fe5e0921 100644 --- a/plugins/inventory_sync/module/define_player_inventories.lua +++ b/plugins/inventory_sync/module/define_player_inventories.lua @@ -1,5 +1,5 @@ local inventories = { - defines.inventory.character_main, + defines.inventory.character_main, defines.inventory.character_guns, defines.inventory.character_ammo, defines.inventory.character_armor, diff --git a/plugins/inventory_sync/module/download_inventory.lua b/plugins/inventory_sync/module/download_inventory.lua index c669e0c6..c1f20934 100644 --- a/plugins/inventory_sync/module/download_inventory.lua +++ b/plugins/inventory_sync/module/download_inventory.lua @@ -77,7 +77,7 @@ function download_inventory(player_name, data, number, total) end -- Load crafting queue - load_crafting_queue(serialized_player.crafting_queue, player) + load_crafting_queue(serialized_player.crafting_queue, player) -- Misc player.cheat_mode = serialized_player.cheat_mode diff --git a/plugins/inventory_sync/module/gui/handle_gui_events.lua b/plugins/inventory_sync/module/gui/handle_gui_events.lua index 5935c058..b335c2d3 100644 --- a/plugins/inventory_sync/module/gui/handle_gui_events.lua +++ b/plugins/inventory_sync/module/gui/handle_gui_events.lua @@ -15,7 +15,7 @@ local handle_gui_events = function(event) -- 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 diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index a8c69ed0..9fb659c5 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -2,7 +2,7 @@ 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 +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. @@ -105,7 +105,7 @@ 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 + if not global.inventory_sync then log("ERROR: Global inventory sync not defined") return end @@ -156,7 +156,7 @@ function inventory_sync.initiate_inventory_download(player) 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 + -- 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 diff --git a/plugins/inventory_sync/module/save_crafts.lua b/plugins/inventory_sync/module/save_crafts.lua index 42ebbd93..6916be48 100644 --- a/plugins/inventory_sync/module/save_crafts.lua +++ b/plugins/inventory_sync/module/save_crafts.lua @@ -20,7 +20,7 @@ function save_crafts(event) 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, @@ -29,10 +29,10 @@ function save_crafts(event) local rightmost_right_index = 0 -- 0 indexed since it is subtractive in a 1 indexed language local new_queue = player.crafting_queue - while + 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 + 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 @@ -48,7 +48,7 @@ function save_crafts(event) added = oldItem.count - newItem.count if oldItem.recipe ~= newItem.recipe then log("ERROR: Old item "..oldItem.recipe.." is not equal "..newItem.recipe) - end + end end -- If the last item we added was of the same type, merge them in the queue @@ -63,7 +63,7 @@ function save_crafts(event) 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 = {} diff --git a/plugins/inventory_sync/module/upload_inventory.lua b/plugins/inventory_sync/module/upload_inventory.lua index acb1de27..43812dbc 100644 --- a/plugins/inventory_sync/module/upload_inventory.lua +++ b/plugins/inventory_sync/module/upload_inventory.lua @@ -73,7 +73,7 @@ function upload_inventory(playerIndex) end clusterio_api.send_json("inventory_sync_upload", serialized_player) - + -- Clear saved crafting queue global.inventory_sync.saved_crafting_queue[player.name] = nil end