From 3a040192412c182f183c39441f946455bb554093 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 2 Mar 2017 11:50:18 +0100 Subject: [PATCH 1/5] Reduce logs --- client.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client.js b/client.js index 255fa4e3..abfa5d5d 100644 --- a/client.js +++ b/client.js @@ -188,10 +188,10 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ console.log(`child process exited with code ${code}`); process.exit(); }); - +/* serverprocess.stdout.on('data', (chunk) => { console.log('OUT: ' + chunk); - }) + })*/ serverprocess.stderr.on('data', (chunk) => { console.log('ERR: ' + chunk); @@ -353,7 +353,7 @@ function instanceManagement() { require('getmac').getMac(function (err, mac) { if (err) throw err payload.mac = mac - console.log(payload) + // console.log(payload) needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function (err, response, body) { if (response && response.body) { // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. @@ -502,7 +502,7 @@ function instanceManagement() { inventoryFrame[inventory[i].name] = Number(inventory[i].count); } inventoryFrame["signal-unixtime"] = Math.floor(Date.now()/1000); - console.log("RCONing inventory! " + JSON.stringify(inventoryFrame)); + // console.log("RCONing inventory! " + JSON.stringify(inventoryFrame)); client.exec("/silent-command remote.call('clusterio', 'receiveInventory', '" + JSON.stringify(inventoryFrame) + "')"); } }); From e21332591fb197716385280986b6bd64761c9feb Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 2 Mar 2017 12:37:34 +0100 Subject: [PATCH 2/5] Load autosaves but bug with factorio-current.log resoves #44 --- client.js | 224 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 132 insertions(+), 92 deletions(-) diff --git a/client.js b/client.js index abfa5d5d..873a58b0 100644 --- a/client.js +++ b/client.js @@ -99,29 +99,38 @@ if (!command || command == "help" || command == "--help") { } } else if (command == "start" && typeof instance == "string" && instance != "/" && !fs.existsSync(instancedirectory)) { // if instance does not exist, create it - console.log("Creating instance...") + console.log("Creating instance..."); fs.mkdirSync(instancedirectory); fs.mkdirSync(instancedirectory + "/script-output/"); - fs.writeFileSync(instancedirectory + "/script-output/output.txt", "") - fs.writeFileSync(instancedirectory + "/script-output/orders.txt", "") - fs.writeFileSync(instancedirectory + "/script-output/txbuffer.txt", "") - fs.mkdirSync(instancedirectory + "/mods/") + fs.mkdirSync(instancedirectory + "/saves/"); + fs.writeFileSync(instancedirectory + "/script-output/output.txt", ""); + fs.writeFileSync(instancedirectory + "/script-output/orders.txt", ""); + fs.writeFileSync(instancedirectory + "/script-output/txbuffer.txt", ""); + fs.mkdirSync(instancedirectory + "/mods/"); // fs.symlinkSync('../../../sharedMods', instancedirectory + "/mods", 'junction') // This is broken because it can only take a file as first argument, not a folder fs.writeFileSync(instancedirectory + "/config.ini", "[path]\r\n\ read-data=__PATH__executable__/../../data\r\n\ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ "); - var instconf = { + fs.copySync('sharedMods', instancedirectory + "/mods") + let instconf = { "factorioPort": Math.floor(Math.random() * 65535), "clientPort": Math.floor(Math.random() * 65535), "clientPassword": Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8), } + console.log("Clusterio | Moving shared mods...") console.log(instconf) + + // create instance config fs.writeFileSync(instancedirectory + "/config.json", JSON.stringify(instconf, null, 4)); - - var serversettings = { - "name": config.username + "'s clusterio " + instance, + + let name = "Clusterio instance: " + instance; + if (config.username) { + name = config.username + "'s clusterio " + instance; + } + let serversettings = { + "name": name, "description": config.description, "tags": ["clusterio"], "max_players": "20", @@ -137,14 +146,12 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ "autosave_slots": 5, "afk_autokick_interval": 0, "auto_pause": config.auto_pause, - } fs.writeFileSync(instancedirectory + "/server-settings.json", JSON.stringify(serversettings, null, 4)); - - var createSave = child_process.spawnSync( + let createSave = child_process.spawnSync( './' + config.factorioDirectory + '/bin/x64/factorio', [ '-c', instancedirectory + '/config.ini', - '--create', instancedirectory + '/save.zip', + '--create', instancedirectory + '/saves/save.zip', ] ) console.log("Instance created!") @@ -156,58 +163,10 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ process.exit(1) } - // move mods from ./sharedMods to the instances mod directory - console.log("Clusterio | Moving shared mods...") - //ncp("./sharedMods/", instancedirectory + "/mods", function (err) { - - - fs.copySync('sharedMods', instancedirectory + "/mods") - - - process.on('SIGINT', function () { - console.log("Caught interrupt signal"); - //serverprocess.stdin.write('/quit') - }); - - // Spawn factorio server - //var serverprocess = child_process.exec(commandline) - var serverprocess = child_process.spawn( - './' + config.factorioDirectory + '/bin/x64/factorio', [ - '-c', instancedirectory + '/config.ini', - '--start-server', instancedirectory + '/save.zip', - '--rcon-port', instanceconfig.clientPort, - '--rcon-password', instanceconfig.clientPassword, - '--server-settings', instancedirectory + '/server-settings.json', - '--port', instanceconfig.factorioPort - ], { - 'stdio': ['pipe', 'pipe', 'pipe'] - } - ) - - serverprocess.on('close', (code) => { - console.log(`child process exited with code ${code}`); - process.exit(); - }); -/* - serverprocess.stdout.on('data', (chunk) => { - console.log('OUT: ' + chunk); - })*/ - - serverprocess.stderr.on('data', (chunk) => { - console.log('ERR: ' + chunk); - }) - - // connect to the server with rcon - // IP, port, password - var client = new Rcon({ - host: 'localhost', - port: instanceconfig.clientPort, - password: instanceconfig.clientPassword, - timeout: 0 - }); - + console.log("Deleting logs"); // clean old log file to avoid crash // file exists, delete so we don't get in trouble + try { fs.unlinkSync(instancedirectory+'/factorio-current.log') } catch (err){ @@ -218,37 +177,85 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ } } - // check the logfile to see if the RCON interface is running as there is no way to continue without it - // we read the log every 2 seconds and stop looping when we start connecting to factorio - function checkRcon() { - fs.readFile(instancedirectory+"/factorio-current.log", function (err, data) { - // if (err) console.log(err); - if(data && data.indexOf('Starting RCON interface') > 0){ - client.connect(); - } else { - setTimeout(function(){ - checkRcon(); - },2000); - } - }); - } - checkRcon(); - - client.on('authenticated', function () { - console.log('Clusterio | Authenticated!'); - instanceManagement(); // start using rcons - }).on('connected', function () { - console.log('Clusterio | Connected!'); - // getID(); - }).on('disconnected', function () { - console.log('Clusterio | Disconnected!'); - // now reconnect - client.connect(); + // move mods from ./sharedMods to the instances mod directory + console.log("Clusterio | Moving shared mods..."); + fs.copySync('sharedMods', instancedirectory + "/mods"); + + process.on('SIGINT', function () { + console.log("Caught interrupt signal"); + messageInterface("/quit"); }); - // set some globals - confirmedOrders = []; - lastSignalCheck = Date.now(); + // Spawn factorio server + //var serverprocess = child_process.exec(commandline) + getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { + var serverprocess = child_process.spawn( + './' + config.factorioDirectory + '/bin/x64/factorio', [ + '-c', instancedirectory + '/config.ini', + '--start-server', instancedirectory + '/saves/' + latestSave, + '--rcon-port', instanceconfig.clientPort, + '--rcon-password', instanceconfig.clientPassword, + '--server-settings', instancedirectory + '/server-settings.json', + '--port', instanceconfig.factorioPort + ], { + 'stdio': ['pipe', 'pipe', 'pipe'] + } + ) + + serverprocess.on('close', (code) => { + console.log(`child process exited with code ${code}`); + process.exit(); + }); + /* + serverprocess.stdout.on('data', (chunk) => { + console.log('OUT: ' + chunk); + })*/ + + serverprocess.stderr.on('data', (chunk) => { + console.log('ERR: ' + chunk); + }) + + // connect to the server with rcon + // IP, port, password + client = new Rcon({ + host: 'localhost', + port: instanceconfig.clientPort, + password: instanceconfig.clientPassword, + timeout: 0 + }); + + // check the logfile to see if the RCON interface is running as there is no way to continue without it + // we read the log every 2 seconds and stop looping when we start connecting to factorio + function checkRcon() { + fs.readFile(instancedirectory+"/factorio-current.log", function (err, data) { + // if (err) console.log(err); + if(data && data.indexOf('Starting RCON interface') > 0){ + client.connect(); + } else { + setTimeout(function(){ + checkRcon(); + },2000); + } + }); + } + setTimeout(checkRcon, 5000); + + client.on('authenticated', function () { + console.log('Clusterio | Authenticated!'); + instanceManagement(); // start using rcons + }).on('connected', function () { + console.log('Clusterio | Connected!'); + // getID(); + }).on('disconnected', function () { + console.log('Clusterio | Disconnected!'); + // now reconnect + client.connect(); + }); + + // set some globals + confirmedOrders = []; + lastSignalCheck = Date.now(); + }) } function instanceManagement() { @@ -619,3 +626,36 @@ function hashMods(instanceName, callback) { }); } } + +// gets newest file in a directory +// dir is directory +// files is array of filenames +// callback(filename string) +function getNewestFile(dir, files, callback) { + if (!callback) return; + if (!files || (files && files.length === 0)) { + callback(); + } + if (files.length === 1) { + callback(files[0]); + } + var newest = { file: files[0] }; + var checked = 0; + fs.stat(dir + newest.file, function(err, stats) { + newest.mtime = stats.mtime; + for (var i = 0; i < files.length; i++) { + var file = files[i]; + (function(file) { + fs.stat(file, function(err, stats) { + ++checked; + if (stats.mtime.getTime() > newest.mtime.getTime()) { + newest = { file : file, mtime : stats.mtime }; + } + if (checked == files.length) { + callback(newest); + } + }); + })(dir + file); + } + }); + } From 36bfd6450ba1dd701e812c0ec5e17b974234238e Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 2 Mar 2017 13:31:51 +0100 Subject: [PATCH 3/5] Autosave loading fixed again --- client.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/client.js b/client.js index 873a58b0..61f92931 100644 --- a/client.js +++ b/client.js @@ -192,7 +192,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ var serverprocess = child_process.spawn( './' + config.factorioDirectory + '/bin/x64/factorio', [ '-c', instancedirectory + '/config.ini', - '--start-server', instancedirectory + '/saves/' + latestSave, + '--start-server', latestSave.file, '--rcon-port', instanceconfig.clientPort, '--rcon-password', instanceconfig.clientPassword, '--server-settings', instancedirectory + '/server-settings.json', @@ -636,9 +636,6 @@ function getNewestFile(dir, files, callback) { if (!files || (files && files.length === 0)) { callback(); } - if (files.length === 1) { - callback(files[0]); - } var newest = { file: files[0] }; var checked = 0; fs.stat(dir + newest.file, function(err, stats) { From d4968e0894d5ab9fa0ba8d1b6f649d011fa53116 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 2 Mar 2017 13:47:02 +0100 Subject: [PATCH 4/5] Fix getting trolled by partial autosaves (.tmp.zip) --- client.js | 8 +++++--- package.json | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index 61f92931..ab42d4fd 100644 --- a/client.js +++ b/client.js @@ -8,6 +8,7 @@ var request = require("request") var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); +var findRemoveSync = require('find-remove'); // require config.json var config = require('./config'); @@ -162,11 +163,12 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ } else { process.exit(1) } + console.log("Deleting .tmp.zip files"); + findRemoveSync(instancedirectory + "/saves", {extensions: ['.tmp.zip']}) console.log("Deleting logs"); // clean old log file to avoid crash // file exists, delete so we don't get in trouble - try { fs.unlinkSync(instancedirectory+'/factorio-current.log') } catch (err){ @@ -493,7 +495,7 @@ function instanceManagement() { sadas = JSON.stringify(confirmedOrders) confirmedOrders = []; // send our RCON command with whatever we got - client.exec("/silent-command remote.call('clusterio', 'importMany', '" + sadas + "')"); + messageInterface("/silent-command remote.call('clusterio', 'importMany', '" + sadas + "')"); } }, 3000) // COMBINATOR SIGNALS --------------------------------------------------------- @@ -510,7 +512,7 @@ function instanceManagement() { } inventoryFrame["signal-unixtime"] = Math.floor(Date.now()/1000); // console.log("RCONing inventory! " + JSON.stringify(inventoryFrame)); - client.exec("/silent-command remote.call('clusterio', 'receiveInventory', '" + JSON.stringify(inventoryFrame) + "')"); + messageInterface("/silent-command remote.call('clusterio', 'receiveInventory', '" + JSON.stringify(inventoryFrame) + "')"); } }); }, 1000) diff --git a/package.json b/package.json index a1917639..2db79739 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "child_process": "^1.0.2", "express": "^4.14.0", "express-fileupload": "0.0.6", + "find-remove": "^1.0.0", "follow-redirects": "^1.2.1", "fs": "0.0.2", "fs-extra": "^1.0.0", From 1f5c2a1977e63f207fadac5ec52dfa183fad6b21 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 2 Mar 2017 14:19:27 +0100 Subject: [PATCH 5/5] Refix because last fix was moot --- client.js | 12 +++++++++--- package.json | 3 +-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/client.js b/client.js index ab42d4fd..f97e11f6 100644 --- a/client.js +++ b/client.js @@ -8,7 +8,6 @@ var request = require("request") var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); -var findRemoveSync = require('find-remove'); // require config.json var config = require('./config'); @@ -55,7 +54,9 @@ function messageInterface(command, callback) { client.exec(command, callback); } catch (err) { console.log(err); - callback(err); + if(typeof callback == "function"){ + callback(err); + } } } } @@ -164,7 +165,12 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ process.exit(1) } console.log("Deleting .tmp.zip files"); - findRemoveSync(instancedirectory + "/saves", {extensions: ['.tmp.zip']}) + let savefiles = fs.readdirSync(instancedirectory + "/saves/"); + for(i = 0; i < savefiles.length; i++){ + if(savefiles[i].substr(savefiles[i].length - 8, 8) == ".tmp.zip") { + fs.unlinkSync(instancedirectory + "/saves/" + savefiles[i]); + } + } console.log("Deleting logs"); // clean old log file to avoid crash diff --git a/package.json b/package.json index 2db79739..238662cf 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "needle": "^1.1.0", "portfinder": "^1.0.9", "request": "^2.79.0", - "simple-rcon": "^0.3.1", - "sync-request": "^3.0.1" + "simple-rcon": "^0.3.1" } }