From 41127abee807f2c86e6381020a0f7029375dedb1 Mon Sep 17 00:00:00 2001 From: aef123 Date: Sat, 6 May 2023 15:04:06 -0700 Subject: [PATCH] Adding ability to download log files from within the game to the local filesystem. --- .gitignore | 1 + filesync.json | 21 +++++++++++++++++++++ src/config.ts | 26 ++++++++++++++++++++++++++ src/fileWatch.ts | 11 +++++++++++ src/index.ts | 19 +++++++++++++++++-- src/networking/messageGenerators.ts | 11 +++++++++++ src/networking/messageHandler.ts | 29 +++++++++++++++++++++++++++-- 7 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 filesync.json diff --git a/.gitignore b/.gitignore index 6704566..6d14920 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* +NetScriptDefinitions.d.ts # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json diff --git a/filesync.json b/filesync.json new file mode 100644 index 0000000..b107c82 --- /dev/null +++ b/filesync.json @@ -0,0 +1,21 @@ +{ + "allowedFiletypes": [ + ".js" + ], + "allowDeletingFiles": true, + "port": 12525, + "scriptsFolder": "c:\\git\\bitburner\\out", + "quiet": false, + "dry": false, + "definitionFile": { + "update": true, + "location": "NetScriptDefinitions.d.ts" + }, + "logFiles": { + "update": true, + "interval": 1, + "remoteLocation": "/logs/", + "localLocation": "D:\\bitburner_game_logs" + }, + "pushAllOnConnection": true + } \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index 46e9b1b..52406e6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -42,6 +42,32 @@ export const config = convict({ default: false, arg: "dry", }, + logFiles: { + update: { + doc: "Automatically pull the log files from the game.", + format: "Boolean", + env: "BB_UPDATE_LOG", + default: false + }, + interval: { + doc: "Interval in seconds to pull log files from the game.", + format: "Number", + env: "BB_UPDATE_LOG_INTERVAL", + default: 5 + }, + remoteLocation: { + doc: "Folder where log files are stored in the game.", + format: "String", + env: "BB_LOCATION_LOG", + default: "/logs" + }, + localLocation: { + doc: "Folder where log files are stored locally.", + format: "String", + env: "BB_LOCATION_LOG_LOCAL", + default: "./logs" + } + }, definitionFile: { update: { doc: "Automatically pull the definition file from the game.", diff --git a/src/fileWatch.ts b/src/fileWatch.ts index a505560..8eaea54 100644 --- a/src/fileWatch.ts +++ b/src/fileWatch.ts @@ -12,6 +12,17 @@ function fileFilter(file: File) { return false; } +export async function setupLogsFolder() { + try { + await mkdir(resolve(config.get("logFiles").localLocation)); + } catch (err) { + if (isError(err) && err.code !== "EEXIST") { + console.log(`Failed to create folder '${config.get("logFiles").localLocation}' (${err.code})`); + process.exit(); + } + } +} + function isError(err: unknown): err is NodeJS.ErrnoException { return (err as NodeJS.ErrnoException).code !== undefined; } diff --git a/src/index.ts b/src/index.ts index c256948..1f8839f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { setupWatch } from "./fileWatch"; +import { setupWatch, setupLogsFolder } from "./fileWatch"; import { config, loadConfig } from "./config"; import { setupSocket } from "./networking/webSocket"; import signal from "signal-js"; @@ -8,21 +8,26 @@ import { fileRemovalEventToMsg, requestFilenames, requestDefinitionFile, + getAllFiles } from "./networking/messageGenerators"; import { EventType } from "./eventTypes"; import { messageHandler } from "./networking/messageHandler"; import { FileEvent } from "./interfaces"; +import { TIMEOUT } from "dns"; export async function start() { loadConfig(); const watch = await setupWatch(signal); const socket = setupSocket(signal); + let isConnected: boolean = false; + await setupLogsFolder(); // Add a handler for received messages. - signal.on(EventType.MessageReceived, (msg: RawData) => messageHandler(signal, msg, watch.paths)); + signal.on(EventType.MessageReceived, (msg: RawData) => messageHandler(signal, msg, watch.paths, config.get("logFiles").update, config.get("logFiles").remoteLocation, config.get("logFiles").localLocation)); // Add a handler for when a connection to a game is made. signal.on(EventType.ConnectionMade, () => { + isConnected = true; console.log("Connection made!"); if (config.get("definitionFile").update) { @@ -62,4 +67,14 @@ export async function start() { socket.close(); process.exit(); }); + + if (config.get("logFiles").update) { + while(true) { + if ((isConnected as boolean) === true) { + signal.emit(EventType.MessageSend, getAllFiles()); + } + + await new Promise((resolve) => setTimeout(resolve, config.get("logFiles").interval * 1000)); + } + } } diff --git a/src/networking/messageGenerators.ts b/src/networking/messageGenerators.ts index e8db805..0c1daad 100644 --- a/src/networking/messageGenerators.ts +++ b/src/networking/messageGenerators.ts @@ -49,6 +49,17 @@ export function requestFilenames(): Message { }; } +export function getAllFiles(): Message { + return { + jsonrpc: "2.0", + method: "getAllFiles", + params: { + server: "home", + }, + id: messageCounter++, + }; +} + function addLeadingSlash(path: string): string { const slashes = path.match("/"); if (slashes) return `/${path}`; diff --git a/src/networking/messageHandler.ts b/src/networking/messageHandler.ts index 48d122d..c4ce667 100644 --- a/src/networking/messageHandler.ts +++ b/src/networking/messageHandler.ts @@ -5,7 +5,7 @@ import { config } from "../config"; import { EventType } from "../eventTypes"; import { fileChangeEventToMsg } from "./messageGenerators"; import type { Signal } from "signal-js"; -import { Message } from "../interfaces"; +import { Message, FileContent } from "../interfaces"; function deserialize(data: RawData): Message { const msg = JSON.parse(data.toString()); @@ -32,7 +32,12 @@ export function isStringArray(s: Array): s is string[] { return s.every((s) => typeof s === "string"); } -export function messageHandler(signaller: Signal, data: RawData, paths: Map) { +export function messageHandler(signaller: Signal, + data: RawData, + paths: Map, + updateLogs: boolean, + remoteLogFolder: string, + localLogFolder: string) { let incoming; try { @@ -62,6 +67,26 @@ export function messageHandler(signaller: Signal, data: RawData, paths: Map { + if (err) return console.log(err); + }); + } + } + } + + break; } }