API: Allow ns.read to read .msg and .lit files (#2455)

This commit is contained in:
catloversg
2026-01-26 11:32:44 +07:00
committed by GitHub
parent 2edad3ed37
commit 5f519991a6
3 changed files with 28 additions and 5 deletions
+19 -1
View File
@@ -109,6 +109,9 @@ import { compile } from "./NetscriptJSEvaluator";
import { Script } from "./Script/Script";
import { NetscriptFormat } from "./NetscriptFunctions/Format";
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 = {
CityName,
@@ -1083,8 +1086,23 @@ export const ns: InternalAPI<NSFull> = {
},
read: (ctx) => (_filename) => {
const path = helpers.filePath(ctx, "filename", _filename);
if (!hasScriptExtension(path) && !hasTextExtension(path)) return "";
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 ?? "";
},
getFileMetadata: (ctx) => (_filename) => {