Merge pull request #55 from TrixterTheTux/rust

Improve the rust image's wrapper
This commit is contained in:
Michael (Parker) Parker 2020-02-06 10:52:02 -05:00 committed by GitHub
commit 061f20e200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@
var startupCmd = ""; var startupCmd = "";
const fs = require("fs"); const fs = require("fs");
fs.writeFile("latest.log", "", (err) => { 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); var args = process.argv.splice(process.execArgv.length + 2);
@ -20,12 +20,53 @@ if (startupCmd.length < 1) {
process.exit(); 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; var exec = require("child_process").exec;
console.log("Starting Rust..."); 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 waiting = true;
var poll = function( ) { var poll = function( ) {
function createPacket(command) { function createPacket(command) {
var packet = { var packet = {
@ -43,14 +84,15 @@ var poll = function( ) {
var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword); var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword);
ws.on("open", function open() { ws.on("open", function open() {
console.log("Connected to RCON.");
waiting = false; waiting = false;
process.stdin.resume();
process.stdin.setEncoding("utf8");
var util = require("util");
// Hack to fix broken console output // Hack to fix broken console output
ws.send(createPacket('status')); 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) { process.stdin.on('data', function (text) {
ws.send(createPacket(text)); ws.send(createPacket(text));
}); });
@ -86,6 +128,8 @@ var poll = function( ) {
ws.on("close", function() { ws.on("close", function() {
if (!waiting) { if (!waiting) {
console.log("Connection to server closed."); console.log("Connection to server closed.");
exited = true;
process.exit(); process.exit();
} }
}); });