diff --git a/wrapper.js b/wrapper.js index 28c8f7b..d871af1 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,12 +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..."); -exec(startupCmd); + +var exited = false; +const gameProcess = exec(startupCmd); +gameProcess.stdout.on('data', filter); +gameProcess.stderr.on('data', filter); +gameProcess.on('exit', function (code, signal) { + exited = true; + + console.log("Game process exited with code " + code + "."); + process.exit(code); +}); + +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 = { @@ -43,14 +84,15 @@ 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); + gameProcess.stdout.removeListener('data', filter); + gameProcess.stderr.removeListener('data', filter); process.stdin.on('data', function (text) { ws.send(createPacket(text)); }); @@ -86,6 +128,8 @@ var poll = function( ) { ws.on("close", function() { if (!waiting) { console.log("Connection to server closed."); + + exited = true; process.exit(); } });