From d72405f642db7d3fe5bac6505b3e5201e586a740 Mon Sep 17 00:00:00 2001 From: Stepan Fedotov Date: Mon, 13 Jan 2020 19:34:39 +0200 Subject: [PATCH 1/3] Redirect rust's stdout and stderr to the console --- wrapper.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wrapper.js b/wrapper.js index 28c8f7b..d54ba48 100644 --- a/wrapper.js +++ b/wrapper.js @@ -22,7 +22,13 @@ if (startupCmd.length < 1) { var exec = require("child_process").exec; console.log("Starting Rust..."); -exec(startupCmd); + +const gameProcess = exec(startupCmd); +gameProcess.stdout.on('data', console.log); +gameProcess.stderr.on('data', console.log); +gameProcess.on('exit', function (code, signal) { + process.exit(code); +}); var waiting = true; From b971077432fe8e6b97bb385305ac8d697a9119f2 Mon Sep 17 00:00:00 2001 From: Stepan Fedotov Date: Tue, 14 Jan 2020 10:57:51 +0200 Subject: [PATCH 2/3] Support for graceful shutdown, filter out some spammy lines --- wrapper.js | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/wrapper.js b/wrapper.js index d54ba48..612929b 100644 --- a/wrapper.js +++ b/wrapper.js @@ -3,7 +3,7 @@ var startupCmd = ""; const fs = require("fs"); fs.writeFile("latest.log", "", (err) => { - if (err) console.log("Callback error in appendFile:"+err); + if (err) console.log("Callback error in appendFile:" + err); }); var args = process.argv.splice(process.execArgv.length + 2); @@ -20,18 +20,53 @@ if (startupCmd.length < 1) { process.exit(); } +const seenPercentage = {}; +function filter(data) { + const str = data.toString(); + if (str.startsWith("Loading Prefab Bundle ")) { // Rust seems to spam the same percentage, so filter out any duplicates. + const percentage = str.substr("Loading Prefab Bundle ".length); + if (seenPercentage[percentage]) return; + + seenPercentage[percentage] = true; + } + + console.log(str); +} + var exec = require("child_process").exec; console.log("Starting Rust..."); +var exited = false; const gameProcess = exec(startupCmd); -gameProcess.stdout.on('data', console.log); +gameProcess.stdout.on('data', filter); gameProcess.stderr.on('data', console.log); gameProcess.on('exit', function (code, signal) { + exited = true; + + console.log("Game process exited with code " + code + "."); process.exit(code); }); -var waiting = true; +function initialListener(data) { + const command = data.toString().trim(); + if (command === 'quit') { + gameProcess.kill('SIGTERM'); + } else { + console.log('Unable to run "' + command + '" due to RCON not being connected yet.'); + } +} +process.stdin.resume(); +process.stdin.setEncoding("utf8"); +process.stdin.on('data', initialListener); +process.on('exit', function(code) { + if (exited) return; + + console.log("Received request to stop the process, stopping the game..."); + gameProcess.kill('SIGTERM'); +}); + +var waiting = true; var poll = function( ) { function createPacket(command) { var packet = { @@ -49,14 +84,13 @@ var poll = function( ) { var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword); ws.on("open", function open() { + console.log("Connected to RCON."); waiting = false; - process.stdin.resume(); - process.stdin.setEncoding("utf8"); - var util = require("util"); // Hack to fix broken console output ws.send(createPacket('status')); + process.stdin.removeListener('data', initialListener); process.stdin.on('data', function (text) { ws.send(createPacket(text)); }); @@ -92,6 +126,8 @@ var poll = function( ) { ws.on("close", function() { if (!waiting) { console.log("Connection to server closed."); + + exited = true; process.exit(); } }); From 7b6869070ab7a16881f8447614a8828f39be0a3a Mon Sep 17 00:00:00 2001 From: Stepan Fedotov Date: Fri, 17 Jan 2020 10:08:46 +0200 Subject: [PATCH 3/3] Fix duplicate console output after rcon is connected --- wrapper.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wrapper.js b/wrapper.js index 612929b..d871af1 100644 --- a/wrapper.js +++ b/wrapper.js @@ -39,7 +39,7 @@ console.log("Starting Rust..."); var exited = false; const gameProcess = exec(startupCmd); gameProcess.stdout.on('data', filter); -gameProcess.stderr.on('data', console.log); +gameProcess.stderr.on('data', filter); gameProcess.on('exit', function (code, signal) { exited = true; @@ -91,6 +91,8 @@ var poll = function( ) { ws.send(createPacket('status')); process.stdin.removeListener('data', initialListener); + gameProcess.stdout.removeListener('data', filter); + gameProcess.stderr.removeListener('data', filter); process.stdin.on('data', function (text) { ws.send(createPacket(text)); });