From 5d9d3981ff9c9128011db67d6b84f8893bc2bf67 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 24 Oct 2019 17:09:32 +0200 Subject: [PATCH] Remove getLua and getCommand from clusterTools Remove the unused and badly behaved getCommand and getLua functions from clusterTools. The main issue with these is that encourage the use of large commands that have negative impact on the performance of the servers, as such large commands have to be transmitted to all players that are connected. These functions also have poor handling of lua comments and will fail on strings containing for example "--". --- CHANGELOG.md | 3 +++ lib/clusterTools/getCommand.js | 16 ----------- lib/clusterTools/getLua.js | 40 ---------------------------- lib/clusterTools/index.js | 19 +------------ sharedPlugins/serverManager/index.js | 1 - 5 files changed, 4 insertions(+), 75 deletions(-) delete mode 100644 lib/clusterTools/getCommand.js delete mode 100644 lib/clusterTools/getLua.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cc793b4..5af51bc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,9 @@ Version 2.0.0 directory inside the instance folder. - Hotpatch scenarios and code loading is no longer compatible with Clusterio. Breaks playerManager, serverSelect, and tranTeleports. +- Removed getLua and getCommand from lib/clusterTools. If you need to run + more than the most trivial of code in commands use the save patcher and + add in a remote interface. Version 1.2.2 diff --git a/lib/clusterTools/getCommand.js b/lib/clusterTools/getCommand.js deleted file mode 100644 index 8d5afc11..00000000 --- a/lib/clusterTools/getCommand.js +++ /dev/null @@ -1,16 +0,0 @@ -const fs = require("fs-extra"); - -const getLua = require("./getLua.js"); - -let commandCache; -module.exports = async function getCommand(file, COMPRESS = true){ - commandCache = commandCache || {}; - if(!commandCache[file]){ - commandCache[file] = await getLua(file); - return commandCache[file]; - } else if(typeof commandCache[file] == "string"){ - return commandCache[file]; - } else { - throw new Error("Command not found"); - } -} diff --git a/lib/clusterTools/getLua.js b/lib/clusterTools/getLua.js deleted file mode 100644 index a54c8dac..00000000 --- a/lib/clusterTools/getLua.js +++ /dev/null @@ -1,40 +0,0 @@ -const fs = require("fs-extra"); - -module.exports = function getLua(filePath, COMPRESS_LUA = true){ - return new Promise((resolve, reject) => { - fs.readFile(filePath, "utf8", (err, contents) => { - if(err){ - reject(err); - } else { - // split content into lines - contents = contents.split(/\r?\n/); - - // join those lines after making them safe again - contents = contents.reduce((acc, val) => { - val = val.replace(/\\/g ,'\\\\'); - // remove leading and trailing spaces - val = val.trim(); - // escape single quotes - val = val.replace(/'/g ,'\\\''); - - // remove single line comments - let singleLineCommentPosition = val.indexOf("--"); - let multiLineCommentPosition = val.indexOf("--[["); - - if(multiLineCommentPosition === -1 && singleLineCommentPosition !== -1) { - val = val.substr(0, singleLineCommentPosition); - } - - return acc + val + '\\n'; - }, ""); // need the "" or it will not process the first row, potentially leaving a single line comment in that disables the whole code - - // console.log(contents); - - // this takes about 46 ms to minify train_stop_tracking.lua in my tests on an i3 6100u - if(COMPRESS_LUA) contents = require("luamin").minify(contents); - - resolve(contents); - } - }); - }); -} diff --git a/lib/clusterTools/index.js b/lib/clusterTools/index.js index b0dd54ef..eee7cb1b 100644 --- a/lib/clusterTools/index.js +++ b/lib/clusterTools/index.js @@ -1,20 +1,11 @@ /** Functions frequently needed from different places in the cluster. Ex: - * Loading Lua scripts * Getting an instance's displayname from their ID */ module.exports = ({config} = {}) => ({ - /** - async function to get Lua from a file, remove comments, minify and return. - - arg1 is the file path - arg2 is whether or not to compress the Lua. Makes debugging harder, but reduces bandwidth when many players are online. Recommended for polling commands. Defaults to true. - Promise resolves with a string of Lua. - */ - getLua: require("./getLua.js"), /** async function to get an instances displayname from their instanceID @@ -22,12 +13,4 @@ module.exports = ({config} = {}) => ({ Promise resolves with a string, either the displayName or instanceID if not found. */ getInstanceName: require("./getInstanceName.js")(config), - /** - async function same as getLua, but using an in memory cache to lower FS operations. - - arg1 is the file path - arg2 is whether or not to compress, default true - Promise resolves with a string of Lua. - */ - getCommand: require("./getCommand.js"), -}) \ No newline at end of file +}) diff --git a/sharedPlugins/serverManager/index.js b/sharedPlugins/serverManager/index.js index a175630f..aafa32a7 100644 --- a/sharedPlugins/serverManager/index.js +++ b/sharedPlugins/serverManager/index.js @@ -2,7 +2,6 @@ const needle = require("needle"); const fs = require("fs-extra"); const pluginConfig = require("./config"); -const COMPRESS_LUA = false; module.exports = class remoteCommands { constructor(mergedConfig, messageInterface, extras){