mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Microoptimizations after code review
This commit is contained in:
1 parent
a939e11641
commit
f9ef2ebbe1
9 files changed
+27
-30
No files matched your search
@@ -273,12 +273,6 @@ export class Control extends libLink.Link {
|
|||||||
handler(info);
|
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) {
|
async onInstanceLog(id, handler) {
|
||||||
|
|||||||
@@ -5,6 +5,20 @@
|
|||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const libLuaTools = require("@clusterio/lib/lua_tools");
|
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 {
|
class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
||||||
async init() {
|
async init() {
|
||||||
this.inventoryQueue = [];
|
this.inventoryQueue = [];
|
||||||
@@ -35,19 +49,12 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
|
|||||||
this.logger.verbose(`Sending inventory for ${player.player_name} in ${chunks.length} chunks`);
|
this.logger.verbose(`Sending inventory for ${player.player_name} in ${chunks.length} chunks`);
|
||||||
for (let i = 0; i < chunks.length; i++) {
|
for (let i = 0; i < chunks.length; i++) {
|
||||||
// this.logger.verbose(`Sending chunk ${i+1} of ${chunks.length}`)
|
// 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 = {
|
module.exports = {
|
||||||
InstancePlugin,
|
InstancePlugin,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const fs = require("fs-extra");
|
const fs = require("fs").promises;
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
@@ -40,14 +40,14 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async uploadRequestHandler(message) {
|
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;
|
this.inventories[message.data.player_name] = message.data.inventory;
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadRequestHandler(message) {
|
async downloadRequestHandler(message) {
|
||||||
console.log("Downloading inventory for", message.data.player_name);
|
this.logger.verbose(`Downloading inventory for ${message.data.player_name}`);
|
||||||
return {
|
return {
|
||||||
player_name: message.data.player_name,
|
player_name: message.data.player_name,
|
||||||
inventory: this.inventories[message.data.player_name],
|
inventory: this.inventories[message.data.player_name],
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue"
|
|||||||
local inventories = require("modules/inventory_sync/define_player_inventories")
|
local inventories = require("modules/inventory_sync/define_player_inventories")
|
||||||
|
|
||||||
function download_inventory(playerName, data, number, total)
|
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 player == nil then return end
|
||||||
if number then
|
if number then
|
||||||
if global.download_cache[playerName] == nil then
|
if global.download_cache[playerName] == nil then
|
||||||
@@ -16,7 +16,7 @@ function download_inventory(playerName, data, number, total)
|
|||||||
|
|
||||||
if number ~= total then
|
if number ~= total then
|
||||||
-- Show progress in console
|
-- Show progress in console
|
||||||
-- game.players[playerName].print("Downloaded "..(number + 1).."/"..total.." parts")
|
-- player.print("Downloaded "..(number + 1).."/"..total.." parts")
|
||||||
-- Show progress in GUI
|
-- Show progress in GUI
|
||||||
progress_dialog(player, number+1, total)
|
progress_dialog(player, number+1, total)
|
||||||
|
|
||||||
@@ -31,7 +31,6 @@ function download_inventory(playerName, data, number, total)
|
|||||||
-- Load downloaded inventory
|
-- Load downloaded inventory
|
||||||
local serialized_player = game.json_to_table(global.download_cache[playerName])
|
local serialized_player = game.json_to_table(global.download_cache[playerName])
|
||||||
global.download_cache[playerName] = nil -- remove data to lower mapsize
|
global.download_cache[playerName] = nil -- remove data to lower mapsize
|
||||||
local player = game.players[playerName]
|
|
||||||
-- Load inventories
|
-- Load inventories
|
||||||
for _, inv in pairs(inventories) do
|
for _, inv in pairs(inventories) do
|
||||||
local inventory = player.get_inventory(inv)
|
local inventory = player.get_inventory(inv)
|
||||||
@@ -87,7 +86,7 @@ function download_inventory(playerName, data, number, total)
|
|||||||
player.disable_flashlight()
|
player.disable_flashlight()
|
||||||
end
|
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"
|
local log_line = "Imported inventory for "..playerName.." in "..game.tick - startTick.." ticks"
|
||||||
player.print(log_line)
|
player.print(log_line)
|
||||||
log("[inventory_sync] "..log_line)
|
log("[inventory_sync] "..log_line)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ inventory_sync.add_commands = add_commands
|
|||||||
inventory_sync.events = {}
|
inventory_sync.events = {}
|
||||||
-- Cleanup
|
-- Cleanup
|
||||||
inventory_sync.events[defines.events.on_player_removed] = function(event)
|
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
|
-- Remove inventory download performance counter
|
||||||
global["inv_sync_download_start_tick "..player.name] = nil
|
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
|
end
|
||||||
|
|
||||||
function inventory_sync.initiateInventoryDownload(playerIndex)
|
function inventory_sync.initiateInventoryDownload(playerIndex)
|
||||||
local player = game.players[playerIndex]
|
local player = game.get_player(playerIndex)
|
||||||
player.print("Initiating inventory download...")
|
player.print("Initiating inventory download...")
|
||||||
global["inv_sync_download_start_tick "..player.name] = game.tick
|
global["inv_sync_download_start_tick "..player.name] = game.tick
|
||||||
if global.download_cache == nil then
|
if global.download_cache == nil then
|
||||||
|
|||||||
@@ -12,9 +12,6 @@ function load_crafting_queue(crafting_queue, player)
|
|||||||
|
|
||||||
-- Load crafting queue
|
-- Load crafting queue
|
||||||
for _, queueItem in pairs(crafting_queue.crafting_queue) do
|
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)
|
-- Start crafting (consume items)
|
||||||
player.begin_crafting {
|
player.begin_crafting {
|
||||||
count = queueItem.count,
|
count = queueItem.count,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue"
|
|||||||
|
|
||||||
function restore_crafts(event)
|
function restore_crafts(event)
|
||||||
if event.player_index then
|
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]
|
local crafting_queue = global["saved_crafting_queue_"..player.name]
|
||||||
|
|
||||||
if crafting_queue == nil then
|
if crafting_queue == nil then
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
function save_crafts(event)
|
function save_crafts(event)
|
||||||
if event.player_index then
|
if event.player_index then
|
||||||
local player = game.players[event.player_index]
|
local player = game.get_player(event.player_index)
|
||||||
local crafting_queue = {}
|
local crafting_queue = {}
|
||||||
|
|
||||||
-- Avoid overwriting old save
|
-- Avoid overwriting old save
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local inventories = require("modules/inventory_sync/define_player_inventories")
|
|||||||
local save_crafts = require("modules/inventory_sync/save_crafts")
|
local save_crafts = require("modules/inventory_sync/save_crafts")
|
||||||
|
|
||||||
function upload_inventory(playerIndex)
|
function upload_inventory(playerIndex)
|
||||||
local player = game.players[playerIndex]
|
local player = game.get_player(playerIndex)
|
||||||
|
|
||||||
local serialized_player = {
|
local serialized_player = {
|
||||||
name = player.name,
|
name = player.name,
|
||||||
|
|||||||
Reference in new issue
Block a user