mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-17 23:08:36 +02:00
API: Allow ns.read to read .msg and .lit files (#2455)
This commit is contained in:
@@ -58,7 +58,9 @@ Data in the specified text file.
|
|||||||
|
|
||||||
RAM cost: 0 GB
|
RAM cost: 0 GB
|
||||||
|
|
||||||
This function is used to read data from a text file (.txt, .json, .css) or script (.js, .jsx, .ts, .tsx).
|
This function is used to read data from a text file (.txt, .json, .css), a script (.js, .jsx, .ts, .tsx), a literature file (.lit), or a message (.msg).
|
||||||
|
|
||||||
This function will return the data in the specified file. If the file does not exist, an empty string will be returned.
|
This function will return the data in the specified file. If the file does not exist, an empty string will be returned.
|
||||||
|
|
||||||
|
With literature files, the returned data is a raw HTML string.
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,9 @@ import { compile } from "./NetscriptJSEvaluator";
|
|||||||
import { Script } from "./Script/Script";
|
import { Script } from "./Script/Script";
|
||||||
import { NetscriptFormat } from "./NetscriptFunctions/Format";
|
import { NetscriptFormat } from "./NetscriptFunctions/Format";
|
||||||
import { FragmentTypeEnum } from "./CotMG/FragmentType";
|
import { FragmentTypeEnum } from "./CotMG/FragmentType";
|
||||||
|
import { renderToStaticMarkup } from "react-dom/server";
|
||||||
|
import { Literatures } from "./Literature/Literatures";
|
||||||
|
import { Messages } from "./Message/MessageHelpers";
|
||||||
|
|
||||||
export const enums: NSEnums = {
|
export const enums: NSEnums = {
|
||||||
CityName,
|
CityName,
|
||||||
@@ -1083,8 +1086,23 @@ export const ns: InternalAPI<NSFull> = {
|
|||||||
},
|
},
|
||||||
read: (ctx) => (_filename) => {
|
read: (ctx) => (_filename) => {
|
||||||
const path = helpers.filePath(ctx, "filename", _filename);
|
const path = helpers.filePath(ctx, "filename", _filename);
|
||||||
if (!hasScriptExtension(path) && !hasTextExtension(path)) return "";
|
|
||||||
const server = ctx.workerScript.getServer();
|
const server = ctx.workerScript.getServer();
|
||||||
|
const isLiterature = path.endsWith(".lit");
|
||||||
|
const isMessage = path.endsWith(".msg");
|
||||||
|
if (isLiterature || isMessage) {
|
||||||
|
if (!server.messages.includes(path as LiteratureName | MessageFilename)) {
|
||||||
|
helpers.log(ctx, () => `${path} does not exist on ${server.hostname}.`);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return isLiterature
|
||||||
|
? renderToStaticMarkup(Literatures[path as LiteratureName].text)
|
||||||
|
: Messages[path as MessageFilename].msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasScriptExtension(path) && !hasTextExtension(path)) {
|
||||||
|
helpers.log(ctx, () => `${path} does not exist on ${server.hostname}.`);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
return server.getContentFile(path)?.content ?? "";
|
return server.getContentFile(path)?.content ?? "";
|
||||||
},
|
},
|
||||||
getFileMetadata: (ctx) => (_filename) => {
|
getFileMetadata: (ctx) => (_filename) => {
|
||||||
|
|||||||
9
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
9
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@@ -7862,10 +7862,13 @@ export interface NS {
|
|||||||
* @remarks
|
* @remarks
|
||||||
* RAM cost: 0 GB
|
* RAM cost: 0 GB
|
||||||
*
|
*
|
||||||
* This function is used to read data from a text file (.txt, .json, .css) or script (.js, .jsx, .ts, .tsx).
|
* This function is used to read data from a text file (.txt, .json, .css), a script (.js, .jsx, .ts, .tsx), a
|
||||||
|
* literature file (.lit), or a message (.msg).
|
||||||
*
|
*
|
||||||
* This function will return the data in the specified file.
|
* This function will return the data in the specified file. If the file does not exist, an empty string will be
|
||||||
* If the file does not exist, an empty string will be returned.
|
* returned.
|
||||||
|
*
|
||||||
|
* With literature files, the returned data is a raw HTML string.
|
||||||
*
|
*
|
||||||
* @param filename - Name of the file to be read.
|
* @param filename - Name of the file to be read.
|
||||||
* @returns Data in the specified text file.
|
* @returns Data in the specified text file.
|
||||||
|
|||||||
Reference in New Issue
Block a user