Skip to content

Commit

Permalink
update log path with old version
Browse files Browse the repository at this point in the history
  • Loading branch information
SturdyStubs authored Aug 10, 2024
1 parent 8e47f3a commit 0f81b05
Showing 1 changed file with 157 additions and 151 deletions.
308 changes: 157 additions & 151 deletions games/carbon/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,52 @@
#!/usr/bin/env node

var startupCmd = "";
const fs = require("fs");
const path = require("path");
var startupCmd = "";

// Function to find the most recent log file matching the desired format
function findMostRecentLog(directory) {
const logPattern = /^\d{4}-\d{2}-\d{2}_T\d{4}\.log$/;
const files = fs.readdirSync(directory);
let latestFile;
let latestTime = 0;

files.forEach(file => {
if (logPattern.test(file)) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isFile() && stat.mtimeMs > latestTime) {
latestTime = stat.mtimeMs;
latestFile = filePath;
}
}
});

return latestFile;
}

// Async function to monitor file changes and log the updates
function monitorLogFile(logFilePath) {
let fileSize = fs.statSync(logFilePath).size;

fs.watchFile(logFilePath, { interval: 100 }, (curr, prev) => {
if (curr.mtimeMs > prev.mtimeMs) {
fs.open(logFilePath, 'r', (err, fd) => {
if (err) throw err;
fs.read(fd, Buffer.alloc(curr.size - fileSize), fileSize, curr.size - fileSize, fileSize, (err, bytesRead, buffer) => {
if (err) throw err;
console.log(buffer.toString().trim());
fileSize = curr.size;
});
});
}
});
}

if (process.env.LOG_FILE === "true") {
const logDirectory = path.join(__dirname, 'logs'); // Adjusting to use /logs/ directory
const latestLogFile = findMostRecentLog(logDirectory);
if (latestLogFile) {
console.log(`Monitoring log file: ${latestLogFile}`);
monitorLogFile(latestLogFile);
} else {
console.log("No log files found to monitor.");
}
// Set up the log file path based on the current date and time
const logFile = path.join(process.cwd(), "logs", `${new Date().toISOString().replace(/:/g, "-").replace("T", "_").slice(0, 16)}.log`);

// Check if the log file exists
let logFileExists = false;
if (fs.existsSync(logFile)) {
logFileExists = true;
// Initialize the log file (if needed)
fs.writeFileSync(logFile, "");
} else {
console.log("Log file does not exist. Skipping log file handling.");
}

fs.writeFile("latest.log", "", (err) => {
if (err) console.log("Callback error in appendFile:" + err);
});

var args = process.argv.splice(process.execArgv.length + 2);
for (var i = 0; i < args.length; i++) {
if (i === args.length - 1) {
startupCmd += args[i];
} else {
startupCmd += args[i] + " ";
}
if (i === args.length - 1) {
startupCmd += args[i];
} else {
startupCmd += args[i] + " ";
}
}

if (startupCmd.length < 1) {
console.log("Error: Please specify a startup command.");
process.exit();
console.log("Error: Please specify a startup command.");
process.exit();
}

const seenPercentage = {};
let rconConnected = false;
let lastSize = 0;
let watcher;

function filter(data) {
const str = data.toString();
if(str.startsWith("Fallback handler could not load library")) return; // Remove fallback
if(str.includes("Filename:")) return; //Remove bindings.h
if(str.includes("ERROR: Shader ")) return; //Remove shader errors
if(str.includes("WARNING: Shader ")) return; //Remove shader errors
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;
function filterAndOutput(data) {
const str = data.toString().trim();

seenPercentage[percentage] = true;
// Filtering logic
if (str.startsWith("Fallback handler could not load library")) return;
if (str.includes("Filename:")) return;
if (str.includes("ERROR: Shader ")) return;
if (str.includes("WARNING: Shader ")) return;
if (str.startsWith("Loading Prefab Bundle ")) {
const percentage = str.substr("Loading Prefab Bundle ".length);
console.log(percentage);
return;
}

console.log(str);
Expand All @@ -95,101 +57,145 @@ console.log("Starting Rust...");

var exited = false;
const gameProcess = exec(startupCmd);
gameProcess.stdout.on('data', filter);
gameProcess.stderr.on('data', filter);
gameProcess.stdout.on('data', filterAndOutput);
gameProcess.stderr.on('data', filterAndOutput);
gameProcess.on('exit', function (code, signal) {
exited = true;
exited = true;

if (code) {
console.log("Main game process exited with code " + code);
// process.exit(code);
}
if (code) {
console.log("Main 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.');
}
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;
if (exited) return;

console.log("Received request to stop the process, stopping the game...");
gameProcess.kill('SIGTERM');
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 = {
Identifier: -1,
Message: command,
Name: "WebRcon"
};
return JSON.stringify(packet);
}

var serverHostname = process.env.RCON_IP ? process.env.RCON_IP : "localhost";
var serverPort = process.env.RCON_PORT;
var serverPassword = process.env.RCON_PASS;
var WebSocket = require("ws");
var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword);

ws.on("open", function open() {
console.log("Connected to RCON. Generating the map now. Please wait until the server status switches to \"Running\".");
waiting = false;

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

ws.on("message", function (data, flags) {
try {
var json = JSON.parse(data);
if (json !== undefined) {
if (json.Message !== undefined && json.Message.length > 0) {
console.log(json.Message);
fs.appendFile("latest.log", "\n" + json.Message, (err) => {
if (err) console.log("Callback error in appendFile:" + err);
});
}
} else {
console.log("Error: Invalid JSON received");
}
} catch (e) {
if (e) {
console.log(e);
}
}
});

ws.on("error", function (err) {
waiting = true;
console.log("Waiting for RCON to come up...");
setTimeout(poll, 5000);
});

ws.on("close", function () {
if (!waiting) {
console.log("Connection to server closed.");

exited = true;
process.exit();
}
});
function createPacket(command) {
var packet = {
Identifier: -1,
Message: command,
Name: "WebRcon"
};
return JSON.stringify(packet);
}

var serverHostname = process.env.RCON_IP ? process.env.RCON_IP : "localhost";
var serverPort = process.env.RCON_PORT;
var serverPassword = process.env.RCON_PASS;
var WebSocket = require("ws");
var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword);

ws.on("open", function open() {
console.log("Connected to RCON. Generating the map now. Please wait until the server status switches to \"Running\".");
rconConnected = true;
waiting = false;

// Stop file watching when RCON is connected
if (watcher) {
watcher.close();
}

// Hack to fix broken console output
ws.send(createPacket('status'));

process.stdin.removeListener('data', initialListener);
gameProcess.stdout.removeListener('data', filterAndOutput);
gameProcess.stderr.removeListener('data', filterAndOutput);
process.stdin.on('data', function (text) {
ws.send(createPacket(text));
});
});

ws.on("message", function (data, flags) {
try {
var json = JSON.parse(data);
if (json !== undefined) {
if (json.Message !== undefined && json.Message.length > 0) {
if (logFileExists) {
fs.appendFile(logFile, "\n" + json.Message, (err) => {
if (err) console.log("Callback error in appendFile: " + err);
});
}
filterAndOutput(json.Message); // Apply filtering to WebSocket messages
}
} else {
console.log("Error: Invalid JSON received");
}
} catch (e) {
if (e) {
console.log(e);
}
}
});

ws.on("error", function (err) {
waiting = true;
console.log("Waiting for RCON to come up...");
setTimeout(poll, 5000);
});

ws.on("close", function () {
if (!waiting) {
console.log("Connection to server closed.");

exited = true;
process.exit();
}
});
}
poll();

// Function to handle new log data
function handleNewLogData(chunk) {
if (!rconConnected) {
filterAndOutput(chunk); // Apply filtering to log data from the file
lastSize += Buffer.byteLength(chunk, 'utf8'); // Update lastSize to reflect the latest read position
}
}

// Set up the initial file watcher
if (logFileExists && !rconConnected) {
// First read any existing data in the file
fs.stat(logFile, (err, stats) => {
if (err) return console.error(err);

if (stats.size > lastSize) {
const logStream = fs.createReadStream(logFile, { encoding: 'utf8', start: lastSize });
logStream.on('data', handleNewLogData);
logStream.on('end', () => {
// Set up file watcher after reading initial data
watcher = fs.watch(logFile, (event, filename) => {
if (filename && event === 'change' && !rconConnected) {
fs.stat(logFile, (err, stats) => {
if (err) return console.error(err);

if (stats.size > lastSize) {
const newLogStream = fs.createReadStream(logFile, { encoding: 'utf8', start: lastSize });
newLogStream.on('data', handleNewLogData);
}
});
}
});
});
}
});
}

0 comments on commit 0f81b05

Please sign in to comment.