mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 00:32:51 +02:00
MISC: Add file metadata (timestamps) (#2199)
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import { SaveData } from "../types";
|
||||
import type { SaveData } from "../types";
|
||||
import type { BaseServer } from "../Server/BaseServer";
|
||||
|
||||
export class RFAMessage {
|
||||
jsonrpc = "2.0"; // Transmits version of JSON-RPC. Compliance maybe allows some funky interaction with external tools?
|
||||
public method?: string; // Is defined when it's a request/notification, otherwise undefined
|
||||
public result?: ResultType; // Is defined when it's a response, otherwise undefined
|
||||
public params?: FileMetadata; // Optional parameters to method
|
||||
public params?: FileDescription; // Optional parameters to method
|
||||
public error?: string; // Only defined on error
|
||||
public id?: number; // ID to keep track of request -> response interaction, undefined with notifications, defined with request/response
|
||||
|
||||
constructor(obj: { method?: string; result?: ResultType; params?: FileMetadata; error?: string; id?: number } = {}) {
|
||||
constructor(
|
||||
obj: { method?: string; result?: ResultType; params?: FileDescription; error?: string; id?: number } = {},
|
||||
) {
|
||||
this.method = obj.method;
|
||||
this.result = obj.result;
|
||||
this.params = obj.params;
|
||||
@@ -28,8 +30,10 @@ type ResultType =
|
||||
identifier: string;
|
||||
binary: boolean;
|
||||
save: SaveData;
|
||||
};
|
||||
type FileMetadata = FileData | FileContent | FileLocation | FileServer;
|
||||
}
|
||||
| FileMetadata
|
||||
| FileMetadata[];
|
||||
type FileDescription = FileData | FileContent | FileLocation | FileServer;
|
||||
|
||||
export interface FileData {
|
||||
filename: string;
|
||||
@@ -51,6 +55,13 @@ export interface FileServer {
|
||||
server: string;
|
||||
}
|
||||
|
||||
export interface FileMetadata {
|
||||
filename: string;
|
||||
atime: number;
|
||||
mtime: number;
|
||||
btime: number;
|
||||
}
|
||||
|
||||
export type RFAServerData = Pick<BaseServer, "hostname" | "hasAdminRights" | "purchasedByPlayer">;
|
||||
|
||||
export function isFileData(p: unknown): p is FileData {
|
||||
|
||||
@@ -4,12 +4,13 @@ import { hasScriptExtension } from "../Paths/ScriptFilePath";
|
||||
import { GetServer, GetAllServers } from "../Server/AllServers";
|
||||
import {
|
||||
RFAMessage,
|
||||
FileData,
|
||||
FileContent,
|
||||
type FileData,
|
||||
type FileContent,
|
||||
isFileServer,
|
||||
isFileLocation,
|
||||
FileLocation,
|
||||
type FileLocation,
|
||||
isFileData,
|
||||
type FileMetadata,
|
||||
} from "./MessageDefinitions";
|
||||
|
||||
import libSource from "../ScriptEditor/NetscriptDefinitions.d.ts?raw";
|
||||
@@ -37,7 +38,6 @@ export const RFARequestHandler: Record<string, (message: RFAMessage) => RFAMessa
|
||||
}
|
||||
return error("Invalid file extension", msg);
|
||||
},
|
||||
|
||||
getFile: function (msg: RFAMessage): RFAMessage {
|
||||
if (!isFileLocation(msg.params)) return error("Message misses parameters", msg);
|
||||
|
||||
@@ -54,6 +54,27 @@ export const RFARequestHandler: Record<string, (message: RFAMessage) => RFAMessa
|
||||
return new RFAMessage({ result: file.content, id: msg.id });
|
||||
},
|
||||
|
||||
getFileMetadata: function (msg: RFAMessage): RFAMessage {
|
||||
if (!isFileLocation(msg.params)) return error("Message misses parameters", msg);
|
||||
|
||||
const fileData: FileLocation = msg.params;
|
||||
const filePath = resolveFilePath(fileData.filename);
|
||||
if (!filePath) return error("Invalid file path", msg);
|
||||
|
||||
const server = GetServer(fileData.server);
|
||||
if (!server) return error("Server hostname invalid", msg);
|
||||
|
||||
if (!hasTextExtension(filePath) && !hasScriptExtension(filePath)) return error("Invalid file extension", msg);
|
||||
const file = server.getContentFile(filePath);
|
||||
if (!file) return error("File doesn't exist", msg);
|
||||
|
||||
const result: FileMetadata = {
|
||||
filename: file.filename,
|
||||
...file.metadata.plain(),
|
||||
};
|
||||
return new RFAMessage({ result: result, id: msg.id });
|
||||
},
|
||||
|
||||
deleteFile: function (msg: RFAMessage): RFAMessage {
|
||||
if (!isFileLocation(msg.params)) return error("Message misses parameters", msg);
|
||||
|
||||
@@ -93,6 +114,19 @@ export const RFARequestHandler: Record<string, (message: RFAMessage) => RFAMessa
|
||||
return new RFAMessage({ result: fileList, id: msg.id });
|
||||
},
|
||||
|
||||
getAllFileMetadata: function (msg: RFAMessage): RFAMessage {
|
||||
if (!isFileServer(msg.params)) return error("Message misses parameters", msg);
|
||||
|
||||
const server = GetServer(msg.params.server);
|
||||
if (!server) return error("Server hostname invalid", msg);
|
||||
|
||||
const fileList: FileMetadata[] = [...server.scripts, ...server.textFiles].map(([filename, file]) => ({
|
||||
filename: filename,
|
||||
...file.metadata.plain(),
|
||||
}));
|
||||
return new RFAMessage({ result: fileList, id: msg.id });
|
||||
},
|
||||
|
||||
calculateRam: function (msg: RFAMessage): RFAMessage {
|
||||
if (!isFileLocation(msg.params)) return error("Message misses parameters", msg);
|
||||
const fileData: FileLocation = msg.params;
|
||||
|
||||
Reference in New Issue
Block a user