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(); } });