API: Move tail-related APIs to another namespace (#1935)

* API: Move tail-related APIs to another namespace

* Add missing RAM cost config

* Remove setTailFontSize from NS namespace
This commit is contained in:
catloversg
2025-02-02 12:43:56 +07:00
committed by GitHub
parent 67aff2a6a0
commit d6ee16cdb3
18 changed files with 493 additions and 115 deletions

View File

@@ -7,14 +7,103 @@ import { CONSTANTS } from "../Constants";
import { commitHash } from "../utils/helpers/commitHash";
import { InternalAPI } from "../Netscript/APIWrapper";
import { Terminal } from "../../src/Terminal";
import { helpers } from "../Netscript/NetscriptHelpers";
import { helpers, wrapUserNode } from "../Netscript/NetscriptHelpers";
import { assertAndSanitizeMainTheme, assertAndSanitizeStyles } from "../JsonSchema/JSONSchemaAssertion";
import { LogBoxCloserEvents, LogBoxEvents } from "../ui/React/LogBoxManager";
export function NetscriptUserInterface(): InternalAPI<IUserInterface> {
return {
openTail:
(ctx) =>
(scriptID, hostname, ...scriptArgs) => {
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
const runningScriptObj = helpers.getRunningScript(ctx, ident);
if (runningScriptObj == null) {
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return;
}
LogBoxEvents.emit(runningScriptObj);
},
renderTail:
(ctx) =>
(_pid = ctx.workerScript.scriptRef.pid) => {
const pid = helpers.number(ctx, "pid", _pid);
const runningScriptObj = helpers.getRunningScript(ctx, pid);
if (runningScriptObj == null) {
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(pid));
return;
}
runningScriptObj.tailProps?.rerender();
},
moveTail:
(ctx) =>
(_x, _y, _pid = ctx.workerScript.scriptRef.pid) => {
const x = helpers.number(ctx, "x", _x);
const y = helpers.number(ctx, "y", _y);
const pid = helpers.number(ctx, "pid", _pid);
const runningScriptObj = helpers.getRunningScript(ctx, pid);
if (runningScriptObj == null) {
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(pid));
return;
}
runningScriptObj.tailProps?.setPosition(x, y);
},
resizeTail:
(ctx) =>
(_w, _h, _pid = ctx.workerScript.scriptRef.pid) => {
const w = helpers.number(ctx, "w", _w);
const h = helpers.number(ctx, "h", _h);
const pid = helpers.number(ctx, "pid", _pid);
const runningScriptObj = helpers.getRunningScript(ctx, pid);
if (runningScriptObj == null) {
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(pid));
return;
}
runningScriptObj.tailProps?.setSize(w, h);
},
closeTail:
(ctx) =>
(_pid = ctx.workerScript.scriptRef.pid) => {
const pid = helpers.number(ctx, "pid", _pid);
//Emit an event to tell the game to close the tail window if it exists
LogBoxCloserEvents.emit(pid);
},
setTailTitle:
(ctx) =>
(title, _pid = ctx.workerScript.scriptRef.pid) => {
const pid = helpers.number(ctx, "pid", _pid);
const runningScriptObj = helpers.getRunningScript(ctx, pid);
if (runningScriptObj == null) {
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(pid));
return;
}
runningScriptObj.title = typeof title === "string" ? title : wrapUserNode(title);
runningScriptObj.tailProps?.rerender();
},
setTailFontSize:
(ctx) =>
(_pixel, scriptID, hostname, ...scriptArgs) => {
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
const runningScriptObj = helpers.getRunningScript(ctx, ident);
if (runningScriptObj == null) {
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return;
}
if (_pixel === undefined) runningScriptObj.tailProps?.setFontSize(undefined);
else runningScriptObj.tailProps?.setFontSize(helpers.number(ctx, "pixel", _pixel));
},
windowSize: () => () => {
return [window.innerWidth, window.innerHeight];
},
getTheme: () => () => {
return { ...Settings.theme };
},