From c5cae30d8318c4cf2bd01987629bda14ea533584 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 6 Sep 2019 04:31:42 +0200 Subject: [PATCH 1/2] Remove duplicated clusterTools code Remove getCommand, getInstanceName, getSafeLua, and checkHotpatchInstallation definitions from plugins and use the implementation in lib/clusterTools for these functions instead. --- sharedPlugins/clusterioMod/index.js | 96 ++------------------------ sharedPlugins/globalChat/index.js | 27 +------- sharedPlugins/scenario_loader/index.js | 11 +-- 3 files changed, 6 insertions(+), 128 deletions(-) diff --git a/sharedPlugins/clusterioMod/index.js b/sharedPlugins/clusterioMod/index.js index 01cd9be0..7c2cc492 100644 --- a/sharedPlugins/clusterioMod/index.js +++ b/sharedPlugins/clusterioMod/index.js @@ -4,6 +4,8 @@ const fs = require("fs-extra"); const objectOps = require("lib/objectOps.js"); const fileOps = require("lib/fileOps"); const stringUtils = require("lib/stringUtils.js"); +const checkHotpatchInstallation = require("lib/clusterTools/checkHotpatchInstallation"); +const getLua = require("lib/clusterTools/getLua"); const pluginConfig = require("./config"); const COMPRESS_LUA = false; @@ -27,10 +29,10 @@ module.exports = class remoteCommands { }; (async ()=>{ - let hotpatchInstallStatus = await this.checkHotpatchInstallation(); + let hotpatchInstallStatus = await checkHotpatchInstallation(this.messageInterface); this.messageInterface("Hotpach installation status: "+hotpatchInstallStatus); if(hotpatchInstallStatus){ - // let mainCode = await this.getSafeLua("sharedPlugins/playerManager/lua/playerTracking.lua"); + // let mainCode = await getLua("sharedPlugins/playerManager/lua/playerTracking.lua"); // if(mainCode) var returnValue = await messageInterface("/silent-command remote.call('hotpatch', 'update', '"+pluginConfig.name+"', '"+pluginConfig.version+"', '"+mainCode+"')"); } })().catch(e => console.log(e)); @@ -294,99 +296,9 @@ module.exports = class remoteCommands { } }); } - async getCommand(file){ - this.commandCache = this.commandCache || {}; - if(!this.commandCache[file]){ - try{ - let command = (await fs.readFile(file)).toString(); - this.commandCache[file] = command.replace(/(\r\n\t|\n|\r\t)/gm, " "); // remove newlines - return this.commandCache[file]; - } catch(e){ - console.log("Unable to get command from file!"); - console.log(e) - } - } else if(typeof this.commandCache[file] == "string"){ - return this.commandCache[file]; - } else { - throw new Error("Command not found"); - } - } async factorioOutput(data){ } - getInstanceName(instanceID){ - return new Promise((resolve, reject) => { - let instance = this.instances[instanceID]; - if(!instance){ - needle.get(this.config.masterURL + '/api/slaves', { compressed: true }, (err, response) => { - if(err || response.statusCode != 200) { - console.log("Unable to get JSON master/api/slaves, master might be unaccessible"); - } else if (response && response.body) { - if(Buffer.isBuffer(response.body)) {console.log(response.body.toString("utf-8")); throw new Error();} - try { - for (let index in response.body) - this.instances[index] = response.body[index].instanceName; - } catch (e){ - console.log(e); - return null; - } - instance = this.instances[instanceID] - if (!instance) instance = instanceID; //somehow the master doesn't know the instance - resolve(instance); - } - }); - } else { - resolve(instance); - } - }); - } - async getSafeLua(filePath){ - 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 - if(COMPRESS_LUA) contents = require("luamin").minify(contents); - - resolve(contents); - } - }); - }); - } - async checkHotpatchInstallation(){ - let yn = await this.messageInterface("/silent-command if remote.interfaces['hotpatch'] then rcon.print('true') else rcon.print('false') end"); - yn = yn.replace(/(\r\n\t|\n|\r\t)/gm, ""); - if(yn == "true"){ - return true; - } else if(yn == "false"){ - return false; - } - } } async function sleep(s){ return new Promise((resolve, reject) => { diff --git a/sharedPlugins/globalChat/index.js b/sharedPlugins/globalChat/index.js index 7386c74d..f520ec85 100644 --- a/sharedPlugins/globalChat/index.js +++ b/sharedPlugins/globalChat/index.js @@ -6,6 +6,7 @@ module.exports = class remoteCommands { this.config = mergedConfig; this.socket = extras.socket; this.instances = {}; + this.getInstanceName = require("lib/clusterTools/getInstanceName")(mergedConfig); this.socket.on("hello", () => this.socket.emit("registerChatReciever")); @@ -51,30 +52,4 @@ module.exports = class remoteCommands { } }catch(e){console.log(e)} } - getInstanceName(instanceID){ - return new Promise((resolve, reject) => { - let instance = this.instances[instanceID]; - if(!instance){ - needle.get(this.config.masterURL + '/api/slaves', { compressed: true }, (err, response) => { - if(err || response.statusCode != 200) { - console.log("Unable to get JSON master/api/slaves, master might be unaccessible"); - } else if (response && response.body) { - if(Buffer.isBuffer(response.body)) {console.log(response.body.toString("utf-8")); throw new Error();} - try { - for (let index in response.body) - this.instances[index] = response.body[index].instanceName; - } catch (e){ - console.log(e); - return null; - } - instance = this.instances[instanceID] - if (!instance) instance = instanceID; //somehow the master doesn't know the instance - resolve(instance); - } - }); - } else { - resolve(instance); - } - }); - } } diff --git a/sharedPlugins/scenario_loader/index.js b/sharedPlugins/scenario_loader/index.js index afe0cb1f..eb575593 100644 --- a/sharedPlugins/scenario_loader/index.js +++ b/sharedPlugins/scenario_loader/index.js @@ -8,7 +8,7 @@ module.exports = class remoteCommands { this.config = mergedConfig; (async () => { - let hotpatchInstallStatus = await this.checkHotpatchInstallation(); + let hotpatchInstallStatus = await clusterTools.checkHotpatchInstallation(messageInterface); messageInterface("Hotpach installation status: "+hotpatchInstallStatus); if(hotpatchInstallStatus){ await this.loadScenariosFromFolder("scenarios") // located in project root folder (with sharedMods, sharedPlugins etc) @@ -66,15 +66,6 @@ module.exports = class remoteCommands { } } } - async checkHotpatchInstallation(){ - let yn = await this.messageInterface("/silent-command if remote.interfaces['hotpatch'] then rcon.print('true') else rcon.print('false') end"); - yn = yn.replace(/(\r\n\t|\n|\r\t)/gm, ""); - if(yn == "true"){ - return true; - } else if(yn == "false"){ - return false; - } - } } // this function is bad in terms of scope since it relies on a global for return values, should be refactored. From aaf43143df3790d8935edcf26d3fa2d1e47b1f6e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 6 Sep 2019 04:32:00 +0200 Subject: [PATCH 2/2] Add compression to getInstanceName Set the compression header when doing requests in getInstanceName. --- lib/clusterTools/getInstanceName.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clusterTools/getInstanceName.js b/lib/clusterTools/getInstanceName.js index 9632c4e5..c0ba7e4e 100644 --- a/lib/clusterTools/getInstanceName.js +++ b/lib/clusterTools/getInstanceName.js @@ -8,7 +8,7 @@ module.exports = function(config){ return new Promise((resolve, reject) => { let instance = instances[instanceID]; if(!instance){ - needle.get(config.masterURL + '/api/slaves', (err, response) => { + needle.get(config.masterURL + '/api/slaves', { compressed: true }, (err, response) => { if(err || response.statusCode != 200) { console.log("Unable to get JSON master/api/slaves, master might be unaccessible"); } else if (response && response.body) {