corrected new lines in file

ran dos2unix on wrapper.js
This commit is contained in:
Michael (Parker) Parker
2024-10-31 21:58:06 -04:00
parent 8331bdfa42
commit d5d2194f6b

View File

@@ -1,140 +1,140 @@
#!/usr/bin/env node #!/usr/bin/env node
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);
for (var i = 0; i < args.length; i++) { for (var i = 0; i < args.length; i++) {
if (i === args.length - 1) { if (i === args.length - 1) {
startupCmd += args[i]; startupCmd += args[i];
} else { } else {
startupCmd += args[i] + " "; startupCmd += args[i] + " ";
} }
} }
if (startupCmd.length < 1) { if (startupCmd.length < 1) {
console.log("Error: Please specify a startup command."); console.log("Error: Please specify a startup command.");
process.exit(); process.exit();
} }
const seenPercentage = {}; const seenPercentage = {};
function filter(data) { function filter(data) {
const str = data.toString(); const str = data.toString();
if (str.startsWith("Loading Prefab Bundle ")) { // Rust seems to spam the same percentage, so filter out any duplicates. 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); const percentage = str.substr("Loading Prefab Bundle ".length);
if (seenPercentage[percentage]) return; if (seenPercentage[percentage]) return;
seenPercentage[percentage] = true; seenPercentage[percentage] = true;
} }
console.log(str); console.log(str);
} }
var exec = require("child_process").exec; var exec = require("child_process").exec;
console.log("Starting Rust..."); console.log("Starting Rust...");
var exited = false; var exited = false;
const gameProcess = exec(startupCmd); const gameProcess = exec(startupCmd);
gameProcess.stdout.on('data', filter); gameProcess.stdout.on('data', filter);
gameProcess.stderr.on('data', filter); gameProcess.stderr.on('data', filter);
gameProcess.on('exit', function (code, signal) { gameProcess.on('exit', function (code, signal) {
exited = true; exited = true;
if (code) { if (code) {
console.log("Main game process exited with code " + code); console.log("Main game process exited with code " + code);
// process.exit(code); // process.exit(code);
} }
}); });
function initialListener(data) { function initialListener(data) {
const command = data.toString().trim(); const command = data.toString().trim();
if (command === 'quit') { if (command === 'quit') {
gameProcess.kill('SIGTERM'); gameProcess.kill('SIGTERM');
} else { } else {
console.log('Unable to run "' + command + '" due to RCON not being connected yet.'); console.log('Unable to run "' + command + '" due to RCON not being connected yet.');
} }
} }
process.stdin.resume(); process.stdin.resume();
process.stdin.setEncoding("utf8"); process.stdin.setEncoding("utf8");
process.stdin.on('data', initialListener); process.stdin.on('data', initialListener);
process.on('exit', function (code) { process.on('exit', function (code) {
if (exited) return; if (exited) return;
console.log("Received request to stop the process, stopping the game..."); console.log("Received request to stop the process, stopping the game...");
gameProcess.kill('SIGTERM'); 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 = {
Identifier: -1, Identifier: -1,
Message: command, Message: command,
Name: "WebRcon" Name: "WebRcon"
}; };
return JSON.stringify(packet); return JSON.stringify(packet);
} }
var serverHostname = process.env.RCON_IP ? process.env.RCON_IP : "localhost"; var serverHostname = process.env.RCON_IP ? process.env.RCON_IP : "localhost";
var serverPort = process.env.RCON_PORT; var serverPort = process.env.RCON_PORT;
var serverPassword = process.env.RCON_PASS; var serverPassword = process.env.RCON_PASS;
var WebSocket = require("ws"); var WebSocket = require("ws");
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. Generating the map now. Please wait until the server status switches to \"Running\"."); console.log("Connected to RCON. Generating the map now. Please wait until the server status switches to \"Running\".");
waiting = false; waiting = false;
// 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); process.stdin.removeListener('data', initialListener);
gameProcess.stdout.removeListener('data', filter); gameProcess.stdout.removeListener('data', filter);
gameProcess.stderr.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));
}); });
}); });
ws.on("message", function (data, flags) { ws.on("message", function (data, flags) {
try { try {
var json = JSON.parse(data); var json = JSON.parse(data);
if (json !== undefined) { if (json !== undefined) {
if (json.Message !== undefined && json.Message.length > 0) { if (json.Message !== undefined && json.Message.length > 0) {
console.log(json.Message); console.log(json.Message);
const fs = require("fs"); const fs = require("fs");
fs.appendFile("latest.log", "\n" + json.Message, (err) => { fs.appendFile("latest.log", "\n" + json.Message, (err) => {
if (err) console.log("Callback error in appendFile:" + err); if (err) console.log("Callback error in appendFile:" + err);
}); });
} }
} else { } else {
console.log("Error: Invalid JSON received"); console.log("Error: Invalid JSON received");
} }
} catch (e) { } catch (e) {
if (e) { if (e) {
console.log(e); console.log(e);
} }
} }
}); });
ws.on("error", function (err) { ws.on("error", function (err) {
waiting = true; waiting = true;
console.log("Waiting for RCON to come up..."); console.log("Waiting for RCON to come up...");
setTimeout(poll, 5000); setTimeout(poll, 5000);
}); });
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; exited = true;
process.exit(); process.exit();
} }
}); });
} }
poll(); poll();