2021-09-05 01:09:30 +02:00
|
|
|
import { vsprintf, sprintf } from "sprintf-js";
|
2023-06-16 23:52:42 +02:00
|
|
|
import { currentNodeMults } from "./BitNode/BitNodeMultipliers";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { CONSTANTS } from "./Constants";
|
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
calculateHackingChance,
|
|
|
|
calculateHackingExpGain,
|
|
|
|
calculatePercentMoneyHacked,
|
|
|
|
calculateHackingTime,
|
|
|
|
calculateGrowTime,
|
|
|
|
calculateWeakenTime,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Hacking";
|
2022-08-08 19:43:41 +02:00
|
|
|
import { netscriptCanGrow, netscriptCanWeaken } from "./Hacking/netscriptCanHack";
|
2021-03-23 21:02:34 +01:00
|
|
|
import { Terminal } from "./Terminal";
|
2022-10-10 00:42:14 +02:00
|
|
|
import { Player } from "@player";
|
2023-06-12 06:34:20 +02:00
|
|
|
import {
|
|
|
|
CityName,
|
|
|
|
CompletedProgramName,
|
|
|
|
CrimeType,
|
|
|
|
FactionWorkType,
|
|
|
|
GymType,
|
|
|
|
JobName,
|
2023-10-17 11:33:16 +02:00
|
|
|
JobField,
|
2023-06-12 06:34:20 +02:00
|
|
|
LiteratureName,
|
|
|
|
LocationName,
|
|
|
|
ToastVariant,
|
|
|
|
UniversityClassType,
|
2023-08-16 23:31:41 +02:00
|
|
|
CompanyName,
|
2023-06-12 06:34:20 +02:00
|
|
|
} from "@enums";
|
2021-10-02 04:53:23 +02:00
|
|
|
import { PromptEvent } from "./ui/React/PromptManager";
|
2022-08-10 00:08:04 +02:00
|
|
|
import { GetServer, DeleteServer, AddToAllServers, createUniqueRandomIp } from "./Server/AllServers";
|
2019-04-11 10:37:40 +02:00
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
getServerOnNetwork,
|
|
|
|
numCycleForGrowth,
|
2022-04-13 15:54:48 +02:00
|
|
|
numCycleForGrowthCorrected,
|
2021-09-05 01:09:30 +02:00
|
|
|
processSingleServerGrowth,
|
2022-10-09 07:25:31 +02:00
|
|
|
safelyCreateUniqueServer,
|
2023-12-08 02:10:21 +01:00
|
|
|
getCoreBonus,
|
2024-02-17 02:18:16 +01:00
|
|
|
getWeakenEffect,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Server/ServerHelpers";
|
2022-10-09 08:32:13 +02:00
|
|
|
import {
|
|
|
|
getPurchasedServerUpgradeCost,
|
|
|
|
getPurchaseServerCost,
|
|
|
|
getPurchaseServerLimit,
|
|
|
|
getPurchaseServerMaxRam,
|
|
|
|
renamePurchasedServer,
|
|
|
|
upgradePurchasedServer,
|
|
|
|
} from "./Server/ServerPurchases";
|
2021-09-25 03:49:49 +02:00
|
|
|
import { Server } from "./Server/Server";
|
2022-08-08 19:43:41 +02:00
|
|
|
import { influenceStockThroughServerGrow } from "./StockMarket/PlayerInfluencing";
|
2022-09-02 19:19:00 +02:00
|
|
|
import { runScriptFromScript } from "./NetscriptWorker";
|
2023-04-28 00:21:06 +02:00
|
|
|
import { killWorkerScript, killWorkerScriptByPid } from "./Netscript/killWorkerScript";
|
2019-05-16 08:05:36 +02:00
|
|
|
import { workerScripts } from "./Netscript/WorkerScripts";
|
2021-09-25 03:49:49 +02:00
|
|
|
import { WorkerScript } from "./Netscript/WorkerScript";
|
2023-12-08 02:34:49 +01:00
|
|
|
import { helpers, wrapUserNode } from "./Netscript/NetscriptHelpers";
|
2023-02-11 19:18:50 +01:00
|
|
|
import {
|
|
|
|
formatExp,
|
|
|
|
formatNumberNoSuffix,
|
|
|
|
formatMoney,
|
|
|
|
formatPercent,
|
|
|
|
formatRam,
|
|
|
|
formatSecurity,
|
|
|
|
formatThreads,
|
|
|
|
formatNumber,
|
|
|
|
} from "./ui/formatNumber";
|
|
|
|
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
|
2024-06-29 03:42:20 +02:00
|
|
|
import { roundToTwo } from "./utils/helpers/roundToTwo";
|
2023-05-26 14:07:37 +02:00
|
|
|
import { LogBoxEvents, LogBoxCloserEvents } from "./ui/React/LogBoxManager";
|
2023-05-05 09:55:59 +02:00
|
|
|
import { arrayToString } from "./utils/helpers/ArrayHelpers";
|
2021-11-03 23:16:10 +01:00
|
|
|
import { NetscriptGang } from "./NetscriptFunctions/Gang";
|
2023-12-26 17:45:27 +01:00
|
|
|
import { NetscriptGo } from "./NetscriptFunctions/Go";
|
2021-11-03 23:16:10 +01:00
|
|
|
import { NetscriptSleeve } from "./NetscriptFunctions/Sleeve";
|
2022-05-25 02:16:39 +02:00
|
|
|
import { NetscriptExtra } from "./NetscriptFunctions/Extra";
|
2021-11-03 23:16:10 +01:00
|
|
|
import { NetscriptHacknet } from "./NetscriptFunctions/Hacknet";
|
2021-11-14 04:44:17 +01:00
|
|
|
import { NetscriptStanek } from "./NetscriptFunctions/Stanek";
|
2022-03-27 09:53:13 +02:00
|
|
|
import { NetscriptInfiltration } from "./NetscriptFunctions/Infiltration";
|
2021-12-29 08:04:24 +01:00
|
|
|
import { NetscriptUserInterface } from "./NetscriptFunctions/UserInterface";
|
2021-10-28 05:58:07 +02:00
|
|
|
import { NetscriptBladeburner } from "./NetscriptFunctions/Bladeburner";
|
2021-11-03 23:16:10 +01:00
|
|
|
import { NetscriptCodingContract } from "./NetscriptFunctions/CodingContract";
|
2021-11-04 01:19:52 +01:00
|
|
|
import { NetscriptCorporation } from "./NetscriptFunctions/Corporation";
|
2021-11-03 23:16:10 +01:00
|
|
|
import { NetscriptFormulas } from "./NetscriptFunctions/Formulas";
|
2021-10-30 22:03:34 +02:00
|
|
|
import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket";
|
2022-03-23 18:35:15 +01:00
|
|
|
import { NetscriptGrafting } from "./NetscriptFunctions/Grafting";
|
2023-12-08 02:34:49 +01:00
|
|
|
import { NS, RecentScript, ProcessInfo, NSEnums } from "@nsdefs";
|
2021-11-14 04:44:17 +01:00
|
|
|
import { NetscriptSingularity } from "./NetscriptFunctions/Singularity";
|
2021-09-25 23:21:50 +02:00
|
|
|
|
2021-10-13 02:02:37 +02:00
|
|
|
import { dialogBoxCreate } from "./ui/React/DialogBox";
|
2023-06-12 06:34:20 +02:00
|
|
|
import { SnackbarEvents } from "./ui/React/Snackbar";
|
2023-04-28 00:21:06 +02:00
|
|
|
import { matchScriptPathExact } from "./utils/helpers/scriptKey";
|
2021-11-03 23:16:10 +01:00
|
|
|
|
2021-10-15 18:47:43 +02:00
|
|
|
import { Flags } from "./NetscriptFunctions/Flags";
|
2022-01-18 20:02:12 +01:00
|
|
|
import { calculateIntelligenceBonus } from "./PersonObjects/formulas/intelligence";
|
|
|
|
import { CalculateShareMult, StartSharing } from "./NetworkShare/Share";
|
2022-01-19 08:04:48 +01:00
|
|
|
import { recentScripts } from "./Netscript/RecentScripts";
|
2023-08-07 08:38:38 +02:00
|
|
|
import { InternalAPI, setRemovedFunctions, NSProxy } from "./Netscript/APIWrapper";
|
2022-08-10 01:02:33 +02:00
|
|
|
import { INetscriptExtra } from "./NetscriptFunctions/Extra";
|
2022-09-01 14:23:31 +02:00
|
|
|
import { ScriptDeath } from "./Netscript/ScriptDeath";
|
2022-10-13 06:51:53 +02:00
|
|
|
import { getBitNodeMultipliers } from "./BitNode/BitNode";
|
2022-10-14 17:00:29 +02:00
|
|
|
import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers/typeAssertion";
|
2024-02-10 10:10:19 +01:00
|
|
|
import { escapeRegExp } from "lodash";
|
2023-02-11 19:18:50 +01:00
|
|
|
import numeral from "numeral";
|
2024-01-15 12:10:39 +01:00
|
|
|
import { clearPort, peekPort, portHandle, readPort, tryWritePort, writePort, nextPortWrite } from "./NetscriptPort";
|
2023-05-16 00:06:57 +02:00
|
|
|
import { FilePath, resolveFilePath } from "./Paths/FilePath";
|
2023-04-28 00:21:06 +02:00
|
|
|
import { hasScriptExtension } from "./Paths/ScriptFilePath";
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
import { hasTextExtension } from "./Paths/TextFilePath";
|
|
|
|
import { ContentFilePath } from "./Paths/ContentFile";
|
2023-05-16 00:06:57 +02:00
|
|
|
import { hasContractExtension } from "./Paths/ContractFilePath";
|
2023-05-26 13:46:08 +02:00
|
|
|
import { getRamCost } from "./Netscript/RamCostGenerator";
|
2023-06-12 06:34:20 +02:00
|
|
|
import { getEnumHelper } from "./utils/EnumHelper";
|
2023-10-23 10:48:06 +02:00
|
|
|
import { setDeprecatedProperties, deprecationWarning } from "./utils/DeprecationHelper";
|
2024-02-10 10:13:42 +01:00
|
|
|
import { ServerConstants } from "./Server/data/Constants";
|
2024-03-06 01:42:55 +01:00
|
|
|
import { assertFunction } from "./Netscript/TypeAssertion";
|
2024-06-25 07:20:08 +02:00
|
|
|
import { Router } from "./ui/GameRoot";
|
|
|
|
import { Page } from "./ui/Router";
|
2024-07-14 23:30:30 +02:00
|
|
|
import { canAccessBitNodeFeature, validBitNodes } from "./BitNode/BitNodeUtils";
|
2022-10-03 18:12:16 +02:00
|
|
|
|
2022-11-09 19:46:21 +01:00
|
|
|
export const enums: NSEnums = {
|
2022-12-30 02:28:53 +01:00
|
|
|
CityName,
|
2022-10-24 14:44:01 +02:00
|
|
|
CrimeType,
|
2022-11-09 19:46:21 +01:00
|
|
|
FactionWorkType,
|
2022-11-20 14:37:11 +01:00
|
|
|
GymType,
|
2022-12-30 02:28:53 +01:00
|
|
|
JobName,
|
2023-10-17 11:33:16 +02:00
|
|
|
JobField,
|
2023-06-12 06:34:20 +02:00
|
|
|
LocationName,
|
2022-12-30 02:28:53 +01:00
|
|
|
ToastVariant,
|
|
|
|
UniversityClassType,
|
2023-08-16 23:31:41 +02:00
|
|
|
CompanyName,
|
2022-10-21 17:57:37 +02:00
|
|
|
};
|
2023-01-04 15:23:20 +01:00
|
|
|
for (const val of Object.values(enums)) Object.freeze(val);
|
|
|
|
Object.freeze(enums);
|
2022-10-24 14:44:01 +02:00
|
|
|
|
2023-01-04 15:23:20 +01:00
|
|
|
export type NSFull = Readonly<Omit<NS & INetscriptExtra, "pid" | "args" | "enums">>;
|
2022-08-10 01:02:33 +02:00
|
|
|
|
2022-12-13 15:08:23 +01:00
|
|
|
export const ns: InternalAPI<NSFull> = {
|
2022-08-09 21:41:47 +02:00
|
|
|
singularity: NetscriptSingularity(),
|
|
|
|
gang: NetscriptGang(),
|
2023-12-26 17:45:27 +01:00
|
|
|
go: NetscriptGo(),
|
2022-08-09 21:41:47 +02:00
|
|
|
bladeburner: NetscriptBladeburner(),
|
|
|
|
codingcontract: NetscriptCodingContract(),
|
|
|
|
sleeve: NetscriptSleeve(),
|
|
|
|
corporation: NetscriptCorporation(),
|
|
|
|
stanek: NetscriptStanek(),
|
|
|
|
infiltration: NetscriptInfiltration(),
|
|
|
|
ui: NetscriptUserInterface(),
|
|
|
|
formulas: NetscriptFormulas(),
|
|
|
|
stock: NetscriptStockMarket(),
|
|
|
|
grafting: NetscriptGrafting(),
|
|
|
|
hacknet: NetscriptHacknet(),
|
|
|
|
sprintf: () => sprintf,
|
|
|
|
vsprintf: () => vsprintf,
|
2022-10-12 14:49:27 +02:00
|
|
|
scan: (ctx) => (_hostname) => {
|
|
|
|
const hostname = _hostname ? helpers.string(ctx, "hostname", _hostname) : ctx.workerScript.hostname;
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
const out: string[] = [];
|
|
|
|
for (let i = 0; i < server.serversOnNetwork.length; i++) {
|
|
|
|
const s = getServerOnNetwork(server, i);
|
|
|
|
if (s === null) continue;
|
|
|
|
const entry = s.hostname;
|
|
|
|
if (entry === null) continue;
|
|
|
|
out.push(entry);
|
|
|
|
}
|
|
|
|
helpers.log(ctx, () => `returned ${server.serversOnNetwork.length} connections for ${server.hostname}`);
|
|
|
|
return out;
|
|
|
|
},
|
2022-11-09 13:26:26 +01:00
|
|
|
hasTorRouter: () => () => Player.hasTorRouter(),
|
2023-12-08 02:34:49 +01:00
|
|
|
hack: (ctx) => (_hostname, opts?) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
return helpers.hack(ctx, hostname, false, opts);
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
hackAnalyzeThreads: (ctx) => (_hostname, _hackAmount) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const hackAmount = helpers.number(ctx, "hackAmount", _hackAmount);
|
|
|
|
|
|
|
|
// Check argument validity
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (isNaN(hackAmount)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(
|
2022-10-12 14:49:27 +02:00
|
|
|
ctx,
|
|
|
|
`Invalid hackAmount argument passed into hackAnalyzeThreads: ${hackAmount}. Must be numeric.`,
|
|
|
|
);
|
|
|
|
}
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
if (hackAmount < 0 || hackAmount > server.moneyAvailable) {
|
|
|
|
return -1;
|
|
|
|
} else if (hackAmount === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const percentHacked = calculatePercentMoneyHacked(server, Player);
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
if (percentHacked === 0 || server.moneyAvailable === 0) {
|
2023-04-24 17:04:19 +02:00
|
|
|
return -1; // To prevent returning infinity below
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
2021-03-10 07:27:14 +01:00
|
|
|
|
2023-03-26 08:56:01 +02:00
|
|
|
return hackAmount / (server.moneyAvailable * percentHacked);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
|
|
|
hackAnalyze: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-30 23:14:10 +01:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
return calculatePercentMoneyHacked(server, Player);
|
|
|
|
},
|
|
|
|
hackAnalyzeSecurity: (ctx) => (_threads, _hostname?) => {
|
|
|
|
let threads = helpers.number(ctx, "threads", _threads);
|
|
|
|
if (_hostname) {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2022-08-09 21:41:47 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const percentHacked = calculatePercentMoneyHacked(server, Player);
|
2022-04-12 17:42:49 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
if (percentHacked > 0) {
|
|
|
|
// thread count is limited to the maximum number of threads needed
|
|
|
|
threads = Math.min(threads, Math.ceil(1 / percentHacked));
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
2022-04-12 17:42:49 +02:00
|
|
|
|
2024-02-10 10:13:42 +01:00
|
|
|
return ServerConstants.ServerFortifyAmount * threads;
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
|
|
|
hackAnalyzeChance: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
return calculateHackingChance(server, Player);
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
sleep:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2022-10-12 15:54:13 +02:00
|
|
|
(_time = 0) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const time = helpers.number(ctx, "time", _time);
|
2023-09-17 21:40:25 +02:00
|
|
|
helpers.log(ctx, () => `Sleeping for ${convertTimeMsToTimeElapsedString(time, true)}.`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return helpers.netscriptDelay(ctx, time).then(function () {
|
|
|
|
return Promise.resolve(true);
|
|
|
|
});
|
|
|
|
},
|
2022-10-12 15:54:13 +02:00
|
|
|
asleep:
|
|
|
|
(ctx) =>
|
|
|
|
(_time = 0) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const time = helpers.number(ctx, "time", _time);
|
2023-09-17 21:40:25 +02:00
|
|
|
helpers.log(ctx, () => `Sleeping for ${convertTimeMsToTimeElapsedString(time, true)}.`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return new Promise((resolve) => setTimeout(() => resolve(true), time));
|
|
|
|
},
|
2023-12-08 02:34:49 +01:00
|
|
|
grow: (ctx) => (_hostname, opts?) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const { threads, stock, additionalMsec } = helpers.validateHGWOptions(ctx, opts);
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2023-12-08 02:34:49 +01:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
2024-08-17 23:23:08 +02:00
|
|
|
throw helpers.errorMessage(ctx, "Cannot be executed on this server.");
|
2023-12-08 02:34:49 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2023-12-08 02:34:49 +01:00
|
|
|
// No root access or skill level too low
|
|
|
|
const canHack = netscriptCanGrow(server);
|
|
|
|
if (!canHack.res) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, canHack.msg || "");
|
2023-12-08 02:34:49 +01:00
|
|
|
}
|
2021-03-12 02:37:58 +01:00
|
|
|
|
2023-12-08 02:34:49 +01:00
|
|
|
const growTime = calculateGrowTime(server, Player) + additionalMsec / 1000.0;
|
|
|
|
helpers.log(
|
|
|
|
ctx,
|
|
|
|
() =>
|
|
|
|
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
|
|
|
|
growTime * 1000,
|
|
|
|
true,
|
|
|
|
)} (t=${formatThreads(threads)}).`,
|
|
|
|
);
|
|
|
|
return helpers.netscriptDelay(ctx, growTime * 1000).then(function () {
|
2024-08-17 23:23:08 +02:00
|
|
|
const host = GetServer(ctx.workerScript.hostname);
|
|
|
|
if (host === null) {
|
|
|
|
throw helpers.errorMessage(ctx, `Cannot find host of WorkerScript. Hostname: ${ctx.workerScript.hostname}.`);
|
|
|
|
}
|
2023-12-08 02:34:49 +01:00
|
|
|
const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable;
|
|
|
|
processSingleServerGrowth(server, threads, host.cpuCores);
|
|
|
|
const moneyAfter = server.moneyAvailable;
|
|
|
|
ctx.workerScript.scriptRef.recordGrow(server.hostname, threads);
|
|
|
|
const expGain = calculateHackingExpGain(server, Player) * threads;
|
|
|
|
const logGrowPercent = moneyAfter / moneyBefore - 1;
|
2022-08-09 21:41:47 +02:00
|
|
|
helpers.log(
|
|
|
|
ctx,
|
|
|
|
() =>
|
2023-12-08 02:34:49 +01:00
|
|
|
`Available money on '${server.hostname}' grown by ${formatPercent(logGrowPercent, 6)}. Gained ${formatExp(
|
|
|
|
expGain,
|
|
|
|
)} hacking exp (t=${formatThreads(threads)}).`,
|
2022-08-09 21:41:47 +02:00
|
|
|
);
|
2023-12-08 02:34:49 +01:00
|
|
|
ctx.workerScript.scriptRef.onlineExpGained += expGain;
|
|
|
|
Player.gainHackingExp(expGain);
|
|
|
|
if (stock) {
|
|
|
|
influenceStockThroughServerGrow(server, moneyAfter - moneyBefore);
|
|
|
|
}
|
|
|
|
return Promise.resolve(moneyAfter / moneyBefore);
|
|
|
|
});
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
growthAnalyze:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2023-02-14 23:38:51 +01:00
|
|
|
(_host, _multiplier, _cores = 1) => {
|
|
|
|
const host = helpers.string(ctx, "hostname", _host);
|
|
|
|
const mult = helpers.number(ctx, "multiplier", _multiplier);
|
2022-08-09 21:41:47 +02:00
|
|
|
const cores = helpers.number(ctx, "cores", _cores);
|
|
|
|
|
|
|
|
// Check argument validity
|
2023-02-14 23:38:51 +01:00
|
|
|
const server = helpers.getServer(ctx, host);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (!(server instanceof Server)) {
|
2023-02-14 23:38:51 +01:00
|
|
|
// Todo 2.3: Make this throw instead of returning 0?
|
|
|
|
helpers.log(ctx, () => `${host} is not a hackable server. Returning 0.`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2023-02-14 23:38:51 +01:00
|
|
|
if (mult < 1 || !isFinite(mult)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `Invalid argument: multiplier must be finite and >= 1, is ${mult}.`);
|
2023-02-14 23:38:51 +01:00
|
|
|
}
|
|
|
|
// TODO 2.3: Add assertion function for positive integer, there are a lot of places everywhere that can use this
|
|
|
|
if (!Number.isInteger(cores) || cores < 1) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `Cores should be a positive integer. Cores provided: ${cores}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2023-02-14 23:38:51 +01:00
|
|
|
return numCycleForGrowth(server, mult, cores);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
|
|
|
growthAnalyzeSecurity:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_threads, _hostname?, _cores = 1) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
let threads = helpers.number(ctx, "threads", _threads);
|
|
|
|
if (_hostname) {
|
2022-09-29 06:02:33 +02:00
|
|
|
const cores = helpers.number(ctx, "cores", _cores);
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2022-08-08 19:43:41 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2022-08-09 21:41:47 +02:00
|
|
|
|
2022-04-13 09:16:59 +02:00
|
|
|
if (!(server instanceof Server)) {
|
2022-08-08 21:51:50 +02:00
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
2022-04-13 09:16:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
const maxThreadsNeeded = Math.ceil(
|
2022-09-18 03:09:15 +02:00
|
|
|
numCycleForGrowthCorrected(server, server.moneyMax, server.moneyAvailable, cores),
|
2022-08-09 21:41:47 +02:00
|
|
|
);
|
2022-04-13 09:16:59 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
threads = Math.min(threads, maxThreadsNeeded);
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2024-02-10 10:13:42 +01:00
|
|
|
return 2 * ServerConstants.ServerFortifyAmount * threads;
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
2023-12-08 02:34:49 +01:00
|
|
|
weaken: (ctx) => async (_hostname, opts?) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const { threads, additionalMsec } = helpers.validateHGWOptions(ctx, opts);
|
2022-10-12 14:49:27 +02:00
|
|
|
|
2023-12-08 02:34:49 +01:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
2024-08-17 23:23:08 +02:00
|
|
|
throw helpers.errorMessage(ctx, "Cannot be executed on this server.");
|
2023-12-08 02:34:49 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2023-12-08 02:34:49 +01:00
|
|
|
// No root access or skill level too low
|
|
|
|
const canHack = netscriptCanWeaken(server);
|
|
|
|
if (!canHack.res) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, canHack.msg || "");
|
2023-12-08 02:34:49 +01:00
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2023-12-08 02:34:49 +01:00
|
|
|
const weakenTime = calculateWeakenTime(server, Player) + additionalMsec / 1000.0;
|
|
|
|
helpers.log(
|
|
|
|
ctx,
|
|
|
|
() =>
|
|
|
|
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
|
|
|
|
weakenTime * 1000,
|
|
|
|
true,
|
|
|
|
)} (t=${formatThreads(threads)})`,
|
|
|
|
);
|
|
|
|
return helpers.netscriptDelay(ctx, weakenTime * 1000).then(function () {
|
|
|
|
const host = GetServer(ctx.workerScript.hostname);
|
|
|
|
if (host === null) {
|
2024-08-17 23:23:08 +02:00
|
|
|
throw helpers.errorMessage(ctx, `Cannot find host of WorkerScript. Hostname: ${ctx.workerScript.hostname}.`);
|
2023-12-08 02:34:49 +01:00
|
|
|
}
|
2024-02-17 02:18:16 +01:00
|
|
|
const weakenAmt = getWeakenEffect(threads, host.cpuCores);
|
2023-12-08 02:34:49 +01:00
|
|
|
server.weaken(weakenAmt);
|
|
|
|
ctx.workerScript.scriptRef.recordWeaken(server.hostname, threads);
|
|
|
|
const expGain = calculateHackingExpGain(server, Player) * threads;
|
2022-08-09 21:41:47 +02:00
|
|
|
helpers.log(
|
|
|
|
ctx,
|
|
|
|
() =>
|
2023-12-08 02:34:49 +01:00
|
|
|
`'${server.hostname}' security level weakened to ${server.hackDifficulty}. Gained ${formatExp(
|
|
|
|
expGain,
|
|
|
|
)} hacking exp (t=${formatThreads(threads)})`,
|
2022-08-09 21:41:47 +02:00
|
|
|
);
|
2023-12-08 02:34:49 +01:00
|
|
|
ctx.workerScript.scriptRef.onlineExpGained += expGain;
|
|
|
|
Player.gainHackingExp(expGain);
|
|
|
|
// Account for hidden multiplier in Server.weaken()
|
2024-02-17 02:18:16 +01:00
|
|
|
return Promise.resolve(weakenAmt);
|
2023-12-08 02:34:49 +01:00
|
|
|
});
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
weakenAnalyze:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_threads, _cores = 1) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const threads = helpers.number(ctx, "threads", _threads);
|
|
|
|
const cores = helpers.number(ctx, "cores", _cores);
|
2024-02-17 02:18:16 +01:00
|
|
|
return getWeakenEffect(threads, cores);
|
2022-01-18 20:02:12 +01:00
|
|
|
},
|
2022-10-12 15:54:13 +02:00
|
|
|
share: (ctx) => () => {
|
2023-12-08 02:10:21 +01:00
|
|
|
const cores = helpers.getServer(ctx, ctx.workerScript.hostname).cpuCores;
|
|
|
|
const coreBonus = getCoreBonus(cores);
|
2022-08-09 21:41:47 +02:00
|
|
|
helpers.log(ctx, () => "Sharing this computer.");
|
|
|
|
const end = StartSharing(
|
2023-12-08 02:10:21 +01:00
|
|
|
ctx.workerScript.scriptRef.threads * calculateIntelligenceBonus(Player.skills.intelligence, 2) * coreBonus,
|
2022-08-09 21:41:47 +02:00
|
|
|
);
|
|
|
|
return helpers.netscriptDelay(ctx, 10000).finally(function () {
|
|
|
|
helpers.log(ctx, () => "Finished sharing this computer.");
|
|
|
|
end();
|
|
|
|
});
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getSharePower: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
return CalculateShareMult();
|
|
|
|
},
|
|
|
|
print:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
if (args.length === 0) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "Takes at least 1 argument.");
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-08-10 00:08:04 +02:00
|
|
|
ctx.workerScript.print(helpers.argsToString(args));
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
|
|
|
printf:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_format, ...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const format = helpers.string(ctx, "format", _format);
|
|
|
|
if (typeof format !== "string") {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "First argument must be string for the format.");
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
|
|
|
ctx.workerScript.print(vsprintf(format, args));
|
|
|
|
},
|
|
|
|
tprint:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
if (args.length === 0) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "Takes at least 1 argument.");
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-08-10 00:08:04 +02:00
|
|
|
const str = helpers.argsToString(args);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (str.startsWith("ERROR") || str.startsWith("FAIL")) {
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
Terminal.error(`${ctx.workerScript.name}: ${str}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str.startsWith("SUCCESS")) {
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
Terminal.success(`${ctx.workerScript.name}: ${str}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str.startsWith("WARN")) {
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
Terminal.warn(`${ctx.workerScript.name}: ${str}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str.startsWith("INFO")) {
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
Terminal.info(`${ctx.workerScript.name}: ${str}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
Terminal.print(`${ctx.workerScript.name}: ${str}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
|
|
|
tprintf:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_format, ...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const format = helpers.string(ctx, "format", _format);
|
|
|
|
const str = vsprintf(format, args);
|
|
|
|
|
|
|
|
if (str.startsWith("ERROR") || str.startsWith("FAIL")) {
|
|
|
|
Terminal.error(`${str}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str.startsWith("SUCCESS")) {
|
|
|
|
Terminal.success(`${str}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str.startsWith("WARN")) {
|
|
|
|
Terminal.warn(`${str}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str.startsWith("INFO")) {
|
|
|
|
Terminal.info(`${str}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Terminal.print(`${str}`);
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
clearLog: (ctx) => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
ctx.workerScript.scriptRef.clearLog();
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
disableLog: (ctx) => (_fn) => {
|
|
|
|
const fn = helpers.string(ctx, "fn", _fn);
|
2024-08-17 04:10:20 +02:00
|
|
|
if (possibleLogs[fn] === undefined) {
|
|
|
|
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
|
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
if (fn === "ALL") {
|
2024-08-17 04:10:20 +02:00
|
|
|
ctx.workerScript.disableLogs = allDisabled;
|
|
|
|
// No need to log here, it's been disabled.
|
|
|
|
} else {
|
|
|
|
// We don't track individual log entries when all are disabled.
|
|
|
|
if (!ctx.workerScript.disableLogs["ALL"]) {
|
2022-08-09 21:41:47 +02:00
|
|
|
ctx.workerScript.disableLogs[fn] = true;
|
2024-08-17 04:10:20 +02:00
|
|
|
helpers.log(ctx, () => `Disabled logging for ${fn}`);
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
enableLog: (ctx) => (_fn) => {
|
|
|
|
const fn = helpers.string(ctx, "fn", _fn);
|
2024-08-17 04:10:20 +02:00
|
|
|
if (possibleLogs[fn] === undefined) {
|
|
|
|
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
|
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
if (fn === "ALL") {
|
2024-08-17 04:10:20 +02:00
|
|
|
ctx.workerScript.disableLogs = {};
|
2022-10-12 14:49:27 +02:00
|
|
|
helpers.log(ctx, () => `Enabled logging for all functions`);
|
2024-08-17 04:10:20 +02:00
|
|
|
} else {
|
|
|
|
if (ctx.workerScript.disableLogs["ALL"]) {
|
|
|
|
// As an optimization, we normally store only that key, but we have to
|
|
|
|
// expand it out to all keys at this point.
|
|
|
|
// Conveniently, possibleLogs serves as a model for "all keys disabled."
|
|
|
|
ctx.workerScript.disableLogs = Object.assign({}, possibleLogs, { ALL: false, [fn]: false });
|
|
|
|
} else {
|
|
|
|
ctx.workerScript.disableLogs[fn] = false;
|
|
|
|
}
|
|
|
|
helpers.log(ctx, () => `Enabled logging for ${fn}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
isLogEnabled: (ctx) => (_fn) => {
|
|
|
|
const fn = helpers.string(ctx, "fn", _fn);
|
|
|
|
if (possibleLogs[fn] === undefined) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
2024-08-17 04:10:20 +02:00
|
|
|
return ctx.workerScript.shouldLog(fn);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
getScriptLogs:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(scriptID, hostname, ...scriptArgs) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
|
2022-08-10 00:08:04 +02:00
|
|
|
const runningScriptObj = helpers.getRunningScript(ctx, ident);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (runningScriptObj == null) {
|
2022-08-10 00:08:04 +02:00
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
|
2022-10-12 14:49:27 +02:00
|
|
|
return [] as string[];
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2024-11-04 07:35:14 +01:00
|
|
|
return runningScriptObj.logs.map((x) => String(x));
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
|
|
|
tail:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(scriptID, hostname, ...scriptArgs) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
|
2022-08-10 00:08:04 +02:00
|
|
|
const runningScriptObj = helpers.getRunningScript(ctx, ident);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (runningScriptObj == null) {
|
2022-08-10 00:08:04 +02:00
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
|
2022-08-09 21:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
LogBoxEvents.emit(runningScriptObj);
|
|
|
|
},
|
2022-08-30 00:07:17 +02:00
|
|
|
moveTail:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_x, _y, _pid = ctx.workerScript.scriptRef.pid) => {
|
2022-08-30 00:07:17 +02:00
|
|
|
const x = helpers.number(ctx, "x", _x);
|
|
|
|
const y = helpers.number(ctx, "y", _y);
|
|
|
|
const pid = helpers.number(ctx, "pid", _pid);
|
2023-05-26 14:07:37 +02:00
|
|
|
const runningScriptObj = helpers.getRunningScript(ctx, pid);
|
|
|
|
if (runningScriptObj == null) {
|
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(pid));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runningScriptObj.tailProps?.setPosition(x, y);
|
2022-08-30 00:07:17 +02:00
|
|
|
},
|
|
|
|
resizeTail:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_w, _h, _pid = ctx.workerScript.scriptRef.pid) => {
|
2022-08-30 00:07:17 +02:00
|
|
|
const w = helpers.number(ctx, "w", _w);
|
|
|
|
const h = helpers.number(ctx, "h", _h);
|
|
|
|
const pid = helpers.number(ctx, "pid", _pid);
|
2023-05-26 14:07:37 +02:00
|
|
|
const runningScriptObj = helpers.getRunningScript(ctx, pid);
|
|
|
|
if (runningScriptObj == null) {
|
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(pid));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runningScriptObj.tailProps?.setSize(w, h);
|
2022-08-30 00:07:17 +02:00
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
closeTail:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_pid = ctx.workerScript.scriptRef.pid) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
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);
|
|
|
|
},
|
2023-05-26 14:07:37 +02:00
|
|
|
setTitle:
|
|
|
|
(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();
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
nuke: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2022-08-09 21:41:47 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (server.hasAdminRights) {
|
|
|
|
helpers.log(ctx, () => `Already have root access to '${server.hostname}'.`);
|
2022-08-09 21:41:47 +02:00
|
|
|
return true;
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (!Player.hasProgram(CompletedProgramName.nuke)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "You do not have the NUKE.exe virus!");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
if (server.openPortCount < server.numOpenPortsRequired) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "Not enough ports opened to use NUKE.exe virus.");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
server.hasAdminRights = true;
|
|
|
|
helpers.log(ctx, () => `Executed NUKE.exe virus on '${server.hostname}' to gain root access.`);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
brutessh: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (!Player.hasProgram(CompletedProgramName.bruteSsh)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "You do not have the BruteSSH.exe program!");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
if (!server.sshPortOpen) {
|
|
|
|
helpers.log(ctx, () => `Executed BruteSSH.exe on '${server.hostname}' to open SSH port (22).`);
|
|
|
|
server.sshPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `SSH Port (22) already opened on '${server.hostname}'.`);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
ftpcrack: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (!Player.hasProgram(CompletedProgramName.ftpCrack)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "You do not have the FTPCrack.exe program!");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
if (!server.ftpPortOpen) {
|
|
|
|
helpers.log(ctx, () => `Executed FTPCrack.exe on '${server.hostname}' to open FTP port (21).`);
|
|
|
|
server.ftpPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `FTP Port (21) already opened on '${server.hostname}'.`);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
relaysmtp: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (!Player.hasProgram(CompletedProgramName.relaySmtp)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "You do not have the relaySMTP.exe program!");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
if (!server.smtpPortOpen) {
|
|
|
|
helpers.log(ctx, () => `Executed relaySMTP.exe on '${server.hostname}' to open SMTP port (25).`);
|
|
|
|
server.smtpPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `SMTP Port (25) already opened on '${server.hostname}'.`);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
httpworm: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (!Player.hasProgram(CompletedProgramName.httpWorm)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "You do not have the HTTPWorm.exe program!");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
if (!server.httpPortOpen) {
|
|
|
|
helpers.log(ctx, () => `Executed HTTPWorm.exe on '${server.hostname}' to open HTTP port (80).`);
|
|
|
|
server.httpPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `HTTP Port (80) already opened on '${server.hostname}'.`);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
sqlinject: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (!Player.hasProgram(CompletedProgramName.sqlInject)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "You do not have the SQLInject.exe program!");
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
if (!server.sqlPortOpen) {
|
|
|
|
helpers.log(ctx, () => `Executed SQLInject.exe on '${server.hostname}' to open SQL port (1433).`);
|
|
|
|
server.sqlPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `SQL Port (1433) already opened on '${server.hostname}'.`);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
run:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2023-03-21 23:54:49 +01:00
|
|
|
(_scriptname, _thread_or_opt = 1, ..._args) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
2023-03-21 23:54:49 +01:00
|
|
|
const runOpts = helpers.runOptions(ctx, _thread_or_opt);
|
2022-08-09 21:41:47 +02:00
|
|
|
const args = helpers.scriptArgs(ctx, _args);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const scriptServer = ctx.workerScript.getServer();
|
2022-08-09 21:41:47 +02:00
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
return runScriptFromScript("run", scriptServer, path, args, ctx.workerScript, runOpts);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
|
|
|
exec:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2023-03-21 23:54:49 +01:00
|
|
|
(_scriptname, _hostname, _thread_or_opt = 1, ..._args) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2023-03-21 23:54:49 +01:00
|
|
|
const runOpts = helpers.runOptions(ctx, _thread_or_opt);
|
2022-08-09 21:41:47 +02:00
|
|
|
const args = helpers.scriptArgs(ctx, _args);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
return runScriptFromScript("exec", server, path, args, ctx.workerScript, runOpts);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
|
|
|
spawn:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2023-03-21 23:54:49 +01:00
|
|
|
(_scriptname, _thread_or_opt = 1, ..._args) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
2023-09-27 07:31:47 +02:00
|
|
|
const runOpts = helpers.spawnOptions(ctx, _thread_or_opt);
|
2022-08-09 21:41:47 +02:00
|
|
|
const args = helpers.scriptArgs(ctx, _args);
|
2024-06-29 03:41:41 +02:00
|
|
|
const spawnCb = () => {
|
2024-06-25 07:20:08 +02:00
|
|
|
if (Router.page() === Page.BitVerse) {
|
|
|
|
helpers.log(ctx, () => `Script execution is canceled because you are in Bitverse.`);
|
|
|
|
return;
|
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
const scriptServer = GetServer(ctx.workerScript.hostname);
|
2024-06-29 03:41:41 +02:00
|
|
|
if (scriptServer == null) {
|
2024-06-07 22:23:04 +02:00
|
|
|
throw helpers.errorMessage(ctx, `Cannot find server ${ctx.workerScript.hostname}`);
|
2022-05-25 02:16:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-29 03:41:41 +02:00
|
|
|
return runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts);
|
|
|
|
};
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2024-06-29 03:41:41 +02:00
|
|
|
if (runOpts.spawnDelay !== 0) {
|
|
|
|
setTimeout(spawnCb, runOpts.spawnDelay);
|
|
|
|
helpers.log(ctx, () => `Will execute '${path}' in ${runOpts.spawnDelay} milliseconds`);
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.log(ctx, () => "About to exit...");
|
|
|
|
const killed = killWorkerScript(ctx.workerScript);
|
|
|
|
|
|
|
|
if (runOpts.spawnDelay === 0) {
|
|
|
|
helpers.log(ctx, () => `Executing '${path}' immediately`);
|
|
|
|
spawnCb();
|
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2024-06-29 03:41:41 +02:00
|
|
|
if (killed) {
|
|
|
|
// This prevents error messages about statements after the spawn()
|
|
|
|
// trying to be executed when the script is dead.
|
|
|
|
throw new ScriptDeath(ctx.workerScript);
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
|
|
|
},
|
2024-09-08 02:34:24 +02:00
|
|
|
self: (ctx) => () => {
|
|
|
|
const runningScript = helpers.getRunningScript(ctx, ctx.workerScript.pid);
|
|
|
|
if (runningScript == null) throw helpers.errorMessage(ctx, "Cannot find running script. This is a bug.");
|
|
|
|
return helpers.createPublicRunningScript(runningScript, ctx.workerScript);
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
kill:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2023-02-14 03:50:38 +01:00
|
|
|
(scriptID, hostname = ctx.workerScript.hostname, ...scriptArgs) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
|
|
|
|
let res;
|
|
|
|
const killByPid = typeof ident === "number";
|
|
|
|
if (killByPid) {
|
|
|
|
// Kill by pid
|
2023-04-28 00:21:06 +02:00
|
|
|
res = killWorkerScriptByPid(ident);
|
2022-08-09 21:41:47 +02:00
|
|
|
} else {
|
|
|
|
// Kill by filename/hostname
|
2023-02-14 03:50:38 +01:00
|
|
|
if (scriptID === undefined) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "Usage: kill(scriptname, server, [arg1], [arg2]...)");
|
2022-05-25 02:16:39 +02:00
|
|
|
}
|
2021-08-19 22:37:59 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
const byPid = helpers.getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
|
|
|
|
if (byPid === null) {
|
2022-08-10 00:08:04 +02:00
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
|
2022-05-25 02:16:39 +02:00
|
|
|
return false;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
res = true;
|
|
|
|
for (const pid of byPid.keys()) {
|
|
|
|
res &&= killWorkerScriptByPid(pid);
|
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
if (res) {
|
|
|
|
if (killByPid) {
|
|
|
|
helpers.log(ctx, () => `Killing script with PID ${ident}`);
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `Killing '${scriptID}' on '${hostname}' with args: ${arrayToString(scriptArgs)}.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
return true;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
2022-08-09 21:41:47 +02:00
|
|
|
if (killByPid) {
|
|
|
|
helpers.log(ctx, () => `No script with PID ${ident}`);
|
|
|
|
} else {
|
|
|
|
helpers.log(
|
|
|
|
ctx,
|
2023-04-28 00:21:06 +02:00
|
|
|
() => `Internal error killing '${scriptID}' on '${hostname}' with args: ${arrayToString(scriptArgs)}`,
|
2022-05-25 02:16:39 +02:00
|
|
|
);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
killall:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
2024-08-04 22:09:50 +02:00
|
|
|
(_hostname = ctx.workerScript.hostname, _safetyGuard = true) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2024-08-04 22:09:50 +02:00
|
|
|
const safetyGuard = !!_safetyGuard;
|
2022-08-09 21:41:47 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
let scriptsKilled = 0;
|
2022-07-20 01:04:06 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
for (const byPid of server.runningScriptMap.values()) {
|
|
|
|
for (const pid of byPid.keys()) {
|
2024-08-04 22:09:50 +02:00
|
|
|
if (safetyGuard && pid == ctx.workerScript.pid) continue;
|
2023-04-28 00:21:06 +02:00
|
|
|
killWorkerScriptByPid(pid);
|
|
|
|
++scriptsKilled;
|
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2023-08-06 02:26:37 +02:00
|
|
|
helpers.log(ctx, () => `Killing all scripts on '${server.hostname}'.`);
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
return scriptsKilled > 0;
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
exit: (ctx) => () => {
|
2022-09-01 14:23:31 +02:00
|
|
|
helpers.log(ctx, () => "Exiting...");
|
2022-09-01 14:37:59 +02:00
|
|
|
killWorkerScript(ctx.workerScript);
|
2022-09-01 14:23:31 +02:00
|
|
|
throw new ScriptDeath(ctx.workerScript);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
scp: (ctx) => (_files, _destination, _source) => {
|
|
|
|
const destination = helpers.string(ctx, "destination", _destination);
|
|
|
|
const source = helpers.string(ctx, "source", _source ?? ctx.workerScript.hostname);
|
|
|
|
const destServer = helpers.getServer(ctx, destination);
|
|
|
|
const sourceServer = helpers.getServer(ctx, source);
|
|
|
|
const files = Array.isArray(_files) ? _files : [_files];
|
2023-04-28 00:21:06 +02:00
|
|
|
const lits: FilePath[] = [];
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const contentFiles: ContentFilePath[] = [];
|
|
|
|
//First loop through filenames to find all errors before moving anything.
|
|
|
|
for (const file of files) {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.filePath(ctx, "files", file);
|
|
|
|
if (hasScriptExtension(path) || hasTextExtension(path)) {
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
contentFiles.push(path);
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
if (!path.endsWith(".lit")) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "Only works for scripts, .lit and .txt files.");
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
lits.push(path);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
}
|
2018-09-23 02:25:48 +02:00
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
let noFailures = true;
|
|
|
|
// --- Scripts and Text Files---
|
|
|
|
for (const contentFilePath of contentFiles) {
|
|
|
|
const sourceContentFile = sourceServer.getContentFile(contentFilePath);
|
|
|
|
if (!sourceContentFile) {
|
|
|
|
helpers.log(ctx, () => `File '${contentFilePath}' does not exist.`);
|
|
|
|
noFailures = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Overwrite script if it already exists
|
|
|
|
const result = destServer.writeToContentFile(contentFilePath, sourceContentFile.content);
|
2023-05-26 10:04:17 +02:00
|
|
|
helpers.log(ctx, () => `Copied file ${contentFilePath} from ${sourceServer.hostname} to ${destServer.hostname}`);
|
|
|
|
if (result.overwritten) {
|
|
|
|
helpers.log(ctx, () => `Warning: ${contentFilePath} was overwritten on ${destServer.hostname}`);
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Literature Files ---
|
|
|
|
for (const litFilePath of lits) {
|
|
|
|
const sourceMessage = sourceServer.messages.find((message) => message === litFilePath);
|
|
|
|
if (!sourceMessage) {
|
|
|
|
helpers.log(ctx, () => `File '${litFilePath}' does not exist.`);
|
|
|
|
noFailures = false;
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const destMessage = destServer.messages.find((message) => message === litFilePath);
|
|
|
|
if (destMessage) {
|
2023-05-05 09:55:59 +02:00
|
|
|
helpers.log(ctx, () => `File '${litFilePath}' was already on '${destServer.hostname}'.`);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
continue;
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
// It exists in sourceServer.messages, so it's a valid name.
|
|
|
|
destServer.messages.push(litFilePath as LiteratureName);
|
2023-05-05 09:55:59 +02:00
|
|
|
helpers.log(ctx, () => `File '${litFilePath}' copied over to '${destServer.hostname}'.`);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return noFailures;
|
|
|
|
},
|
2023-03-07 14:04:08 +01:00
|
|
|
ls: (ctx) => (_hostname, _substring) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const substring = helpers.string(ctx, "substring", _substring ?? "");
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2019-06-03 05:28:02 +02:00
|
|
|
|
2023-03-07 14:04:08 +01:00
|
|
|
const allFilenames = [
|
|
|
|
...server.contracts.map((contract) => contract.fn),
|
|
|
|
...server.messages,
|
|
|
|
...server.programs,
|
2023-04-18 09:19:45 +02:00
|
|
|
...server.scripts.keys(),
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
...server.textFiles.keys(),
|
2023-03-07 14:04:08 +01:00
|
|
|
];
|
2019-06-03 05:28:02 +02:00
|
|
|
|
2023-03-07 14:04:08 +01:00
|
|
|
if (!substring) return allFilenames.sort();
|
2023-06-06 14:46:07 +02:00
|
|
|
return allFilenames.filter((filename) => ("/" + filename).includes(substring)).sort();
|
2023-03-07 14:04:08 +01:00
|
|
|
},
|
2022-11-09 19:46:21 +01:00
|
|
|
getRecentScripts: () => (): RecentScript[] => {
|
2022-08-09 21:41:47 +02:00
|
|
|
return recentScripts.map((rs) => ({
|
|
|
|
timeOfDeath: rs.timeOfDeath,
|
2022-08-10 00:08:04 +02:00
|
|
|
...helpers.createPublicRunningScript(rs.runningScript),
|
2022-08-09 21:41:47 +02:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
ps:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_hostname = ctx.workerScript.hostname) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2022-10-12 14:49:27 +02:00
|
|
|
const processes: ProcessInfo[] = [];
|
2023-04-28 00:21:06 +02:00
|
|
|
for (const byPid of server.runningScriptMap.values()) {
|
|
|
|
for (const script of byPid.values()) {
|
|
|
|
processes.push({
|
|
|
|
filename: script.filename,
|
|
|
|
threads: script.threads,
|
|
|
|
args: script.args.slice(),
|
|
|
|
pid: script.pid,
|
|
|
|
temporary: script.temporary,
|
|
|
|
});
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
return processes;
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
hasRootAccess: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
return server.hasAdminRights;
|
|
|
|
},
|
|
|
|
getHostname: (ctx) => () => ctx.workerScript.hostname,
|
|
|
|
getHackingLevel: (ctx) => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
Player.updateSkillLevels();
|
|
|
|
helpers.log(ctx, () => `returned ${Player.skills.hacking}`);
|
|
|
|
return Player.skills.hacking;
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getHackingMultipliers: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
return {
|
|
|
|
chance: Player.mults.hacking_chance,
|
|
|
|
speed: Player.mults.hacking_speed,
|
|
|
|
money: Player.mults.hacking_money,
|
|
|
|
growth: Player.mults.hacking_grow,
|
|
|
|
};
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getHacknetMultipliers: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
return {
|
|
|
|
production: Player.mults.hacknet_node_money,
|
|
|
|
purchaseCost: Player.mults.hacknet_node_purchase_cost,
|
|
|
|
ramCost: Player.mults.hacknet_node_ram_cost,
|
|
|
|
coreCost: Player.mults.hacknet_node_core_cost,
|
|
|
|
levelCost: Player.mults.hacknet_node_level_cost,
|
|
|
|
};
|
|
|
|
},
|
2022-10-13 06:51:53 +02:00
|
|
|
getBitNodeMultipliers:
|
|
|
|
(ctx) =>
|
2024-07-14 23:30:30 +02:00
|
|
|
(_n = Player.bitNodeN, _lvl = Player.activeSourceFileLvl(Player.bitNodeN) + 1) => {
|
|
|
|
if (!canAccessBitNodeFeature(5)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, "Requires Source-File 5 to run.");
|
2024-07-14 23:30:30 +02:00
|
|
|
}
|
|
|
|
// TODO v3.0: check n and lvl with helpers.number() and Number.isInteger().
|
2022-10-13 06:51:53 +02:00
|
|
|
const n = Math.round(helpers.number(ctx, "n", _n));
|
|
|
|
const lvl = Math.round(helpers.number(ctx, "lvl", _lvl));
|
2024-07-14 23:30:30 +02:00
|
|
|
if (!validBitNodes.includes(n)) {
|
|
|
|
throw new Error(`Invalid BitNode: ${n}.`);
|
|
|
|
}
|
|
|
|
if (lvl < 1) {
|
|
|
|
throw new Error("SF level must be greater than or equal to 1.");
|
|
|
|
}
|
2022-10-13 06:51:53 +02:00
|
|
|
|
|
|
|
return Object.assign({}, getBitNodeMultipliers(n, lvl));
|
|
|
|
},
|
2023-04-11 21:12:55 +02:00
|
|
|
getServer: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname ?? ctx.workerScript.hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
return {
|
|
|
|
hostname: server.hostname,
|
|
|
|
ip: server.ip,
|
|
|
|
sshPortOpen: server.sshPortOpen,
|
|
|
|
ftpPortOpen: server.ftpPortOpen,
|
|
|
|
smtpPortOpen: server.smtpPortOpen,
|
|
|
|
httpPortOpen: server.httpPortOpen,
|
|
|
|
sqlPortOpen: server.sqlPortOpen,
|
|
|
|
hasAdminRights: server.hasAdminRights,
|
|
|
|
cpuCores: server.cpuCores,
|
|
|
|
isConnectedTo: server.isConnectedTo,
|
|
|
|
ramUsed: server.ramUsed,
|
|
|
|
maxRam: server.maxRam,
|
|
|
|
organizationName: server.organizationName,
|
|
|
|
purchasedByPlayer: server.purchasedByPlayer,
|
|
|
|
backdoorInstalled: server.backdoorInstalled,
|
|
|
|
baseDifficulty: server.baseDifficulty,
|
|
|
|
hackDifficulty: server.hackDifficulty,
|
|
|
|
minDifficulty: server.minDifficulty,
|
|
|
|
moneyAvailable: server.moneyAvailable,
|
|
|
|
moneyMax: server.moneyMax,
|
|
|
|
numOpenPortsRequired: server.numOpenPortsRequired,
|
|
|
|
openPortCount: server.openPortCount,
|
|
|
|
requiredHackingSkill: server.requiredHackingSkill,
|
|
|
|
serverGrowth: server.serverGrowth,
|
|
|
|
};
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getServerMoneyAvailable: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (server.hostname == "home") {
|
|
|
|
// Return player's money
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned player's money: ${formatMoney(Player.money)}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return Player.money;
|
|
|
|
}
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatMoney(server.moneyAvailable)} for '${server.hostname}'`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.moneyAvailable;
|
|
|
|
},
|
|
|
|
getServerSecurityLevel: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatSecurity(server.hackDifficulty)} for '${server.hostname}'`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.hackDifficulty;
|
|
|
|
},
|
|
|
|
getServerBaseSecurityLevel: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatSecurity(server.baseDifficulty)} for '${server.hostname}'`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.baseDifficulty;
|
|
|
|
},
|
|
|
|
getServerMinSecurityLevel: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatSecurity(server.minDifficulty)} for ${server.hostname}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.minDifficulty;
|
|
|
|
},
|
|
|
|
getServerRequiredHackingLevel: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatNumberNoSuffix(server.requiredHackingSkill, 0)} for '${server.hostname}'`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.requiredHackingSkill;
|
|
|
|
},
|
|
|
|
getServerMaxMoney: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatMoney(server.moneyMax)} for '${server.hostname}'`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.moneyMax;
|
|
|
|
},
|
|
|
|
getServerGrowth: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
helpers.log(ctx, () => `returned ${server.serverGrowth} for '${server.hostname}'`);
|
|
|
|
return server.serverGrowth;
|
|
|
|
},
|
|
|
|
getServerNumPortsRequired: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "Cannot be executed on this server.");
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
helpers.log(ctx, () => `returned ${server.numOpenPortsRequired} for '${server.hostname}'`);
|
|
|
|
return server.numOpenPortsRequired;
|
|
|
|
},
|
|
|
|
getServerMaxRam: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatRam(server.maxRam)}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.maxRam;
|
|
|
|
},
|
|
|
|
getServerUsedRam: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `returned ${formatRam(server.ramUsed)}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return server.ramUsed;
|
|
|
|
},
|
|
|
|
serverExists: (ctx) => (_hostname) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
2024-01-07 17:40:07 +01:00
|
|
|
const server = GetServer(hostname);
|
|
|
|
return server !== null && (server.serversOnNetwork.length > 0 || server.hostname === "home");
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2023-05-27 14:34:14 +02:00
|
|
|
fileExists: (ctx) => (_filename, _hostname) => {
|
|
|
|
const filename = helpers.string(ctx, "filename", _filename);
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname ?? ctx.workerScript.hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
const path = resolveFilePath(filename, ctx.workerScript.name);
|
|
|
|
if (!path) return false;
|
|
|
|
if (hasScriptExtension(path)) return server.scripts.has(path);
|
|
|
|
if (hasTextExtension(path)) return server.textFiles.has(path);
|
|
|
|
if (path.endsWith(".lit") || path.endsWith(".msg")) return server.messages.includes(path as any);
|
|
|
|
if (hasContractExtension(path)) return !!server.contracts.find(({ fn }) => fn === path);
|
|
|
|
const lowerPath = path.toLowerCase();
|
|
|
|
return server.programs.map((programName) => programName.toLowerCase()).includes(lowerPath);
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
isRunning:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(fn, hostname, ...scriptArgs) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, fn, hostname, scriptArgs);
|
2022-08-10 00:08:04 +02:00
|
|
|
return helpers.getRunningScript(ctx, ident) !== null;
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getPurchasedServerLimit: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
return getPurchaseServerLimit();
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getPurchasedServerMaxRam: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
return getPurchaseServerMaxRam();
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getPurchasedServerCost: (ctx) => (_ram) => {
|
|
|
|
const ram = helpers.number(ctx, "ram", _ram);
|
2022-08-09 21:41:47 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const cost = getPurchaseServerCost(ram);
|
|
|
|
if (cost === Infinity) {
|
2023-08-28 21:03:16 +02:00
|
|
|
if (ram > getPurchaseServerMaxRam()) {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: ram='${ram}' must not be greater than getPurchaseServerMaxRam`);
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: ram='${ram}' must be a positive power of 2`);
|
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
return Infinity;
|
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
return cost;
|
|
|
|
},
|
|
|
|
purchaseServer: (ctx) => (_name, _ram) => {
|
|
|
|
const name = helpers.string(ctx, "name", _name);
|
|
|
|
const ram = helpers.number(ctx, "ram", _ram);
|
|
|
|
let hostnameStr = String(name);
|
|
|
|
hostnameStr = hostnameStr.replace(/\s+/g, "");
|
|
|
|
if (hostnameStr == "") {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: hostname='${hostnameStr}'`);
|
|
|
|
return "";
|
|
|
|
}
|
2024-02-21 03:22:24 +01:00
|
|
|
if (hostnameStr.startsWith("hacknet-node-") || hostnameStr.startsWith("hacknet-server-")) {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: hostname='${hostnameStr}' is a reserved hostname.`);
|
|
|
|
return "";
|
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
if (Player.purchasedServers.length >= getPurchaseServerLimit()) {
|
2022-08-09 21:41:47 +02:00
|
|
|
helpers.log(
|
|
|
|
ctx,
|
2022-10-12 14:49:27 +02:00
|
|
|
() =>
|
|
|
|
`You have reached the maximum limit of ${getPurchaseServerLimit()} servers. You cannot purchase any more.`,
|
2022-08-09 21:41:47 +02:00
|
|
|
);
|
2022-10-12 14:49:27 +02:00
|
|
|
return "";
|
|
|
|
}
|
2022-10-09 08:32:13 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const cost = getPurchaseServerCost(ram);
|
|
|
|
if (cost === Infinity) {
|
|
|
|
if (ram > getPurchaseServerMaxRam()) {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: ram='${ram}' must not be greater than getPurchaseServerMaxRam`);
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: ram='${ram}' must be a positive power of 2`);
|
2022-10-09 08:32:13 +02:00
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Player.money < cost) {
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `Not enough money to purchase server. Need ${formatMoney(cost)}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
const newServ = safelyCreateUniqueServer({
|
|
|
|
ip: createUniqueRandomIp(),
|
|
|
|
hostname: hostnameStr,
|
|
|
|
organizationName: "",
|
|
|
|
isConnectedTo: false,
|
|
|
|
adminRights: true,
|
|
|
|
purchasedByPlayer: true,
|
|
|
|
maxRam: ram,
|
|
|
|
});
|
|
|
|
AddToAllServers(newServ);
|
|
|
|
|
|
|
|
Player.purchasedServers.push(newServ.hostname);
|
|
|
|
const homeComputer = Player.getHomeComputer();
|
|
|
|
homeComputer.serversOnNetwork.push(newServ.hostname);
|
|
|
|
newServ.serversOnNetwork.push(homeComputer.hostname);
|
|
|
|
Player.loseMoney(cost, "servers");
|
2023-02-11 19:18:50 +01:00
|
|
|
helpers.log(ctx, () => `Purchased new server with hostname '${newServ.hostname}' for ${formatMoney(cost)}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
return newServ.hostname;
|
|
|
|
},
|
|
|
|
|
|
|
|
getPurchasedServerUpgradeCost: (ctx) => (_hostname, _ram) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const ram = helpers.number(ctx, "ram", _ram);
|
|
|
|
try {
|
|
|
|
return getPurchasedServerUpgradeCost(hostname, ram);
|
|
|
|
} catch (err) {
|
|
|
|
helpers.log(ctx, () => String(err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
},
|
2023-03-28 22:01:09 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
upgradePurchasedServer: (ctx) => (_hostname, _ram) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const ram = helpers.number(ctx, "ram", _ram);
|
|
|
|
try {
|
|
|
|
upgradePurchasedServer(hostname, ram);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
helpers.log(ctx, () => String(err));
|
2022-10-09 08:32:13 +02:00
|
|
|
return false;
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
},
|
2023-03-28 22:01:09 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
renamePurchasedServer: (ctx) => (_hostname, _newName) => {
|
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const newName = helpers.string(ctx, "newName", _newName);
|
2023-03-28 22:10:24 +02:00
|
|
|
try {
|
|
|
|
renamePurchasedServer(hostname, newName);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
helpers.log(ctx, () => String(err));
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2022-10-09 08:32:13 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
deleteServer: (ctx) => (_name) => {
|
|
|
|
const name = helpers.string(ctx, "name", _name);
|
|
|
|
let hostnameStr = String(name);
|
|
|
|
hostnameStr = hostnameStr.replace(/\s\s+/g, "");
|
|
|
|
const server = GetServer(hostnameStr);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => `Invalid argument: hostname='${hostnameStr}'`);
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
if (!server.purchasedByPlayer || server.hostname === "home") {
|
|
|
|
helpers.log(ctx, () => "Cannot delete non-purchased server.");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-20 04:21:11 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
const hostname = server.hostname;
|
2018-07-20 04:21:11 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
// Can't delete server you're currently connected to
|
|
|
|
if (server.isConnectedTo) {
|
|
|
|
helpers.log(ctx, () => "You are currently connected to the server you are trying to delete.");
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-20 18:43:05 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
// A server cannot delete itself
|
|
|
|
if (hostname === ctx.workerScript.hostname) {
|
|
|
|
helpers.log(ctx, () => "Cannot delete the server this script is running on.");
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-20 18:43:05 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
// Delete all scripts running on server
|
2023-04-28 00:21:06 +02:00
|
|
|
if (server.runningScriptMap.size > 0) {
|
2022-10-12 14:49:27 +02:00
|
|
|
helpers.log(ctx, () => `Cannot delete server '${hostname}' because it still has scripts running.`);
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
// Delete from player's purchasedServers array
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < Player.purchasedServers.length; ++i) {
|
|
|
|
if (hostname == Player.purchasedServers[i]) {
|
|
|
|
found = true;
|
|
|
|
Player.purchasedServers.splice(i, 1);
|
|
|
|
break;
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
if (!found) {
|
|
|
|
helpers.log(
|
|
|
|
ctx,
|
|
|
|
() => `Could not identify server ${hostname} as a purchased server. This is a bug. Report to dev.`,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
// Delete from all servers
|
|
|
|
DeleteServer(hostname);
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
// Delete from home computer
|
|
|
|
found = false;
|
|
|
|
const homeComputer = Player.getHomeComputer();
|
|
|
|
for (let i = 0; i < homeComputer.serversOnNetwork.length; ++i) {
|
|
|
|
if (hostname == homeComputer.serversOnNetwork[i]) {
|
|
|
|
homeComputer.serversOnNetwork.splice(i, 1);
|
|
|
|
helpers.log(ctx, () => `Deleted server '${hostnameStr}`);
|
|
|
|
return true;
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
// Wasn't found on home computer
|
|
|
|
helpers.log(ctx, () => `Could not find server ${hostname} as a purchased server. This is a bug. Report to dev.`);
|
|
|
|
return false;
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
getPurchasedServers: () => (): string[] => {
|
|
|
|
const res: string[] = [];
|
|
|
|
Player.purchasedServers.forEach(function (hostname) {
|
|
|
|
res.push(hostname);
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
},
|
2023-02-28 01:54:04 +01:00
|
|
|
writePort: (ctx) => (_portNumber, data) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return writePort(portNumber, data);
|
|
|
|
},
|
2023-04-18 09:19:45 +02:00
|
|
|
write: (ctx) => (_filename, _data, _mode) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const filepath = helpers.filePath(ctx, "filename", _filename);
|
2023-04-18 09:19:45 +02:00
|
|
|
const data = helpers.string(ctx, "data", _data ?? "");
|
|
|
|
const mode = helpers.string(ctx, "mode", _mode ?? "a");
|
|
|
|
|
|
|
|
const server = helpers.getServer(ctx, ctx.workerScript.hostname);
|
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
if (hasScriptExtension(filepath)) {
|
|
|
|
if (mode === "w") {
|
|
|
|
server.writeToScriptFile(filepath, data);
|
2023-03-06 04:39:42 +01:00
|
|
|
return;
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const existingScript = server.scripts.get(filepath);
|
|
|
|
const existingCode = existingScript ? existingScript.code : "";
|
|
|
|
server.writeToScriptFile(filepath, existingCode + data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!hasTextExtension(filepath)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `File path should be a text file or script. ${filepath} is invalid.`);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
}
|
|
|
|
if (mode === "w") {
|
|
|
|
server.writeToTextFile(filepath, data);
|
2022-08-24 04:06:05 +02:00
|
|
|
return;
|
2023-04-18 09:19:45 +02:00
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const existingTextFile = server.textFiles.get(filepath);
|
|
|
|
const existingText = existingTextFile?.text ?? "";
|
|
|
|
server.writeToTextFile(filepath, mode === "w" ? data : existingText + data);
|
2023-04-18 09:19:45 +02:00
|
|
|
},
|
2023-02-28 01:54:04 +01:00
|
|
|
tryWritePort: (ctx) => (_portNumber, data) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return tryWritePort(portNumber, data);
|
|
|
|
},
|
2024-01-15 12:10:39 +01:00
|
|
|
nextPortWrite: (ctx) => (_portNumber) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return nextPortWrite(portNumber);
|
|
|
|
},
|
2023-02-28 01:54:04 +01:00
|
|
|
readPort: (ctx) => (_portNumber) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return readPort(portNumber);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
|
|
|
read: (ctx) => (_filename) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.filePath(ctx, "filename", _filename);
|
|
|
|
if (!hasScriptExtension(path) && !hasTextExtension(path)) return "";
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const server = ctx.workerScript.getServer();
|
|
|
|
return server.getContentFile(path)?.content ?? "";
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2023-02-28 01:54:04 +01:00
|
|
|
peek: (ctx) => (_portNumber) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return peekPort(portNumber);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
|
|
|
clear: (ctx) => (_file) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.filePath(ctx, "file", _file);
|
|
|
|
if (!hasScriptExtension(path) && !hasTextExtension(path)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `Invalid file path or extension: ${_file}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const server = ctx.workerScript.getServer();
|
|
|
|
const file = server.getContentFile(path);
|
2024-02-26 14:05:10 +01:00
|
|
|
if (!file) throw helpers.errorMessage(ctx, `${path} does not exist on ${server.hostname}`);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
// The content setter handles invalidating script modules where applicable.
|
|
|
|
file.content = "";
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2023-02-28 01:54:04 +01:00
|
|
|
clearPort: (ctx) => (_portNumber) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return clearPort(portNumber);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2023-02-28 01:54:04 +01:00
|
|
|
getPortHandle: (ctx) => (_portNumber) => {
|
|
|
|
const portNumber = helpers.portNumber(ctx, _portNumber);
|
|
|
|
return portHandle(portNumber);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
rm: (ctx) => (_fn, _hostname) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const filepath = helpers.filePath(ctx, "fn", _fn);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname ?? ctx.workerScript.hostname);
|
|
|
|
const s = helpers.getServer(ctx, hostname);
|
|
|
|
if (!filepath) {
|
|
|
|
helpers.log(ctx, () => `Error while parsing filepath ${filepath}`);
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-09 21:41:47 +02:00
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const status = s.removeFile(filepath);
|
|
|
|
if (!status.res) {
|
|
|
|
helpers.log(ctx, () => status.msg + "");
|
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
return status.res;
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
scriptRunning: (ctx) => (_scriptname, _hostname) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const scriptname = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
2022-10-12 14:49:27 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2023-04-28 00:21:06 +02:00
|
|
|
return server.isRunning(scriptname);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
|
|
|
scriptKill: (ctx) => (_scriptname, _hostname) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
2022-10-12 14:49:27 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
let suc = false;
|
2023-04-28 00:21:06 +02:00
|
|
|
|
|
|
|
const pattern = matchScriptPathExact(escapeRegExp(path));
|
|
|
|
for (const [key, byPid] of server.runningScriptMap) {
|
|
|
|
if (!pattern.test(key)) continue;
|
|
|
|
suc = true;
|
|
|
|
for (const pid of byPid.keys()) {
|
|
|
|
killWorkerScriptByPid(pid);
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
|
|
|
return suc;
|
|
|
|
},
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
getScriptName: (ctx) => () => ctx.workerScript.name,
|
2023-04-18 09:19:45 +02:00
|
|
|
getScriptRam: (ctx) => (_scriptname, _hostname) => {
|
2023-04-28 00:21:06 +02:00
|
|
|
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
2023-04-18 09:19:45 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname ?? ctx.workerScript.hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const script = server.scripts.get(path);
|
2023-04-18 09:19:45 +02:00
|
|
|
if (!script) return 0;
|
|
|
|
const ramUsage = script.getRamUsage(server.scripts);
|
|
|
|
if (!ramUsage) {
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
helpers.log(ctx, () => `Could not calculate ram usage for ${path} on ${hostname}.`);
|
2023-04-18 09:19:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ramUsage;
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
getRunningScript:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(fn, hostname, ...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
|
2022-08-10 00:08:04 +02:00
|
|
|
const runningScript = helpers.getRunningScript(ctx, ident);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (runningScript === null) return null;
|
2024-10-20 09:52:57 +02:00
|
|
|
// Need to look this up again, because we only have ident-based lookup
|
|
|
|
// for RunningScript.
|
|
|
|
const ws = workerScripts.get(runningScript.pid);
|
|
|
|
// We don't check for null, since it's fine to pass null as the 2nd arg.
|
|
|
|
return helpers.createPublicRunningScript(runningScript, ws);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
2024-06-29 03:42:20 +02:00
|
|
|
ramOverride: (ctx) => (_ram) => {
|
|
|
|
const newRam = roundToTwo(helpers.number(ctx, "ram", _ram || 0));
|
|
|
|
const rs = ctx.workerScript.scriptRef;
|
|
|
|
const server = ctx.workerScript.getServer();
|
|
|
|
if (newRam < roundToTwo(ctx.workerScript.dynamicRamUsage)) {
|
|
|
|
// Impossibly small, return immediately.
|
|
|
|
return rs.ramUsage;
|
|
|
|
}
|
|
|
|
const newServerRamUsed = roundToTwo(server.ramUsed + (newRam - rs.ramUsage) * rs.threads);
|
|
|
|
if (newServerRamUsed >= server.maxRam) {
|
|
|
|
// Can't allocate more RAM.
|
|
|
|
return rs.ramUsage;
|
|
|
|
}
|
|
|
|
if (newServerRamUsed <= 0) {
|
|
|
|
throw helpers.errorMessage(
|
|
|
|
ctx,
|
|
|
|
`Game error: Calculated impossible new server ramUsed ${newServerRamUsed} from new limit of ${_ram}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
server.updateRamUsed(newServerRamUsed);
|
|
|
|
rs.ramUsage = newRam;
|
|
|
|
return rs.ramUsage;
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
getHackTime:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_hostname = ctx.workerScript.hostname) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "invalid for this kind of server");
|
|
|
|
return Infinity;
|
|
|
|
}
|
2022-09-05 16:35:12 +02:00
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
2022-08-09 21:41:47 +02:00
|
|
|
return Infinity;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
return calculateHackingTime(server, Player) * 1000;
|
|
|
|
},
|
|
|
|
getGrowTime:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_hostname = ctx.workerScript.hostname) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "invalid for this kind of server");
|
|
|
|
return Infinity;
|
|
|
|
}
|
2022-09-05 16:35:12 +02:00
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
2022-08-09 21:41:47 +02:00
|
|
|
return Infinity;
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
return calculateGrowTime(server, Player) * 1000;
|
|
|
|
},
|
|
|
|
getWeakenTime:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_hostname = ctx.workerScript.hostname) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const hostname = helpers.string(ctx, "hostname", _hostname);
|
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
helpers.log(ctx, () => "invalid for this kind of server");
|
|
|
|
return Infinity;
|
|
|
|
}
|
2022-09-05 16:35:12 +02:00
|
|
|
if (helpers.failOnHacknetServer(ctx, server)) {
|
2022-08-09 21:41:47 +02:00
|
|
|
return Infinity;
|
2022-07-20 01:04:06 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
return calculateWeakenTime(server, Player) * 1000;
|
2022-07-20 01:04:06 +02:00
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getTotalScriptIncome: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
// First element is total income of all currently running scripts
|
|
|
|
let total = 0;
|
|
|
|
for (const script of workerScripts.values()) {
|
|
|
|
total += script.scriptRef.onlineMoneyMade / script.scriptRef.onlineRunningTime;
|
|
|
|
}
|
2022-05-25 02:16:39 +02:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
return [total, Player.scriptProdSinceLastAug / (Player.playtimeSinceLastAug / 1000)];
|
|
|
|
},
|
|
|
|
getScriptIncome:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(fn, hostname, ...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
|
2022-08-10 00:08:04 +02:00
|
|
|
const runningScript = helpers.getRunningScript(ctx, ident);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (runningScript == null) {
|
2022-08-10 00:08:04 +02:00
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
|
2022-08-09 21:41:47 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return runningScript.onlineMoneyMade / runningScript.onlineRunningTime;
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getTotalScriptExpGain: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
let total = 0;
|
|
|
|
for (const ws of workerScripts.values()) {
|
|
|
|
total += ws.scriptRef.onlineExpGained / ws.scriptRef.onlineRunningTime;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
},
|
|
|
|
getScriptExpGain:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(fn, hostname, ...args) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
|
2022-08-10 00:08:04 +02:00
|
|
|
const runningScript = helpers.getRunningScript(ctx, ident);
|
2022-08-09 21:41:47 +02:00
|
|
|
if (runningScript == null) {
|
2022-08-10 00:08:04 +02:00
|
|
|
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
|
2022-08-09 21:41:47 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return runningScript.onlineExpGained / runningScript.onlineRunningTime;
|
|
|
|
},
|
2023-02-11 19:18:50 +01:00
|
|
|
formatNumber:
|
|
|
|
(ctx) =>
|
|
|
|
(_n, _fractionalDigits = 3, _suffixStart = 1000, isInteger) => {
|
|
|
|
const n = helpers.number(ctx, "n", _n);
|
|
|
|
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
|
|
|
|
const suffixStart = helpers.number(ctx, "suffixStart", _suffixStart);
|
|
|
|
return formatNumber(n, fractionalDigits, suffixStart, !!isInteger);
|
|
|
|
},
|
|
|
|
formatRam:
|
|
|
|
(ctx) =>
|
|
|
|
(_n, _fractionalDigits = 2) => {
|
|
|
|
const n = helpers.number(ctx, "n", _n);
|
|
|
|
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
|
|
|
|
return formatRam(n, fractionalDigits);
|
|
|
|
},
|
|
|
|
formatPercent:
|
|
|
|
(ctx) =>
|
2023-02-19 21:24:27 +01:00
|
|
|
(_n, _fractionalDigits = 2, _multStart = 1e6) => {
|
2023-02-11 19:18:50 +01:00
|
|
|
const n = helpers.number(ctx, "n", _n);
|
|
|
|
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
|
2023-02-19 21:24:27 +01:00
|
|
|
const multStart = helpers.number(ctx, "multStart", _multStart);
|
|
|
|
return formatPercent(n, fractionalDigits, multStart);
|
2023-02-11 19:18:50 +01:00
|
|
|
},
|
2023-04-25 11:22:36 +02:00
|
|
|
// Todo: Remove function for real though in 2.4. Until then it just directly wraps numeral.
|
2022-10-12 14:49:27 +02:00
|
|
|
nFormat: (ctx) => (_n, _format) => {
|
2023-04-25 11:22:36 +02:00
|
|
|
deprecationWarning(
|
|
|
|
"ns.nFormat",
|
2024-11-11 01:27:35 +01:00
|
|
|
"Use ns.formatNumber, ns.formatRam, ns.formatPercent, or JS built-in objects/functions (e.g., Intl namespace) instead. " +
|
|
|
|
"Check the NS API documentation for details.",
|
2023-04-25 11:22:36 +02:00
|
|
|
);
|
2022-10-12 14:49:27 +02:00
|
|
|
const n = helpers.number(ctx, "n", _n);
|
|
|
|
const format = helpers.string(ctx, "format", _format);
|
2023-02-11 19:18:50 +01:00
|
|
|
return numeral(n).format(format);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2022-10-25 02:09:10 +02:00
|
|
|
tFormat: (ctx) => (_milliseconds, _milliPrecision) => {
|
|
|
|
const milliseconds = helpers.number(ctx, "milliseconds", _milliseconds);
|
|
|
|
const milliPrecision = !!_milliPrecision;
|
|
|
|
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getTimeSinceLastAug: () => () => {
|
2024-08-04 09:01:34 +02:00
|
|
|
deprecationWarning(
|
|
|
|
"ns.getTimeSinceLastAug()",
|
|
|
|
"Use `Date.now() - ns.getResetInfo().lastAugReset` instead. Please note that ns.getResetInfo().lastAugReset does NOT return the " +
|
|
|
|
"same value as ns.getTimeSinceLastAug(). Check the NS API documentation for details.",
|
|
|
|
);
|
2022-08-09 21:41:47 +02:00
|
|
|
return Player.playtimeSinceLastAug;
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
alert: (ctx) => (_message) => {
|
|
|
|
const message = helpers.string(ctx, "message", _message);
|
|
|
|
dialogBoxCreate(message, true);
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
toast:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_message, _variant = ToastVariant.SUCCESS, _duration = 2000) => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const message = helpers.string(ctx, "message", _message);
|
2023-06-12 06:34:20 +02:00
|
|
|
const variant = getEnumHelper("ToastVariant").nsGetMember(ctx, _variant);
|
2022-08-19 13:38:46 +02:00
|
|
|
const duration = _duration === null ? null : helpers.number(ctx, "duration", _duration);
|
2022-10-04 12:40:10 +02:00
|
|
|
SnackbarEvents.emit(message, variant as ToastVariant, duration);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
2022-10-14 17:00:29 +02:00
|
|
|
prompt: (ctx) => (_txt, _options) => {
|
|
|
|
const options: { type?: string; choices?: string[] } = {};
|
|
|
|
_options ??= options;
|
|
|
|
const txt = helpers.string(ctx, "txt", _txt);
|
|
|
|
assert(_options, objectAssert, (type) =>
|
2024-02-26 14:05:10 +01:00
|
|
|
helpers.errorMessage(ctx, `Invalid type for options: ${type}. Should be object.`, "TYPE"),
|
2022-10-14 17:00:29 +02:00
|
|
|
);
|
|
|
|
if (_options.type !== undefined) {
|
|
|
|
assert(_options.type, stringAssert, (type) =>
|
2024-02-26 14:05:10 +01:00
|
|
|
helpers.errorMessage(ctx, `Invalid type for options.type: ${type}. Should be string.`, "TYPE"),
|
2022-10-14 17:00:29 +02:00
|
|
|
);
|
|
|
|
options.type = _options.type;
|
|
|
|
const validTypes = ["boolean", "text", "select"];
|
|
|
|
if (!["boolean", "text", "select"].includes(options.type)) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(
|
2022-10-14 17:00:29 +02:00
|
|
|
ctx,
|
|
|
|
`Invalid value for options.type: ${options.type}. Must be one of ${validTypes.join(", ")}.`,
|
|
|
|
);
|
2022-08-09 21:41:47 +02:00
|
|
|
}
|
2022-10-14 17:00:29 +02:00
|
|
|
if (options.type === "select") {
|
|
|
|
assert(_options.choices, arrayAssert, (type) =>
|
2024-02-26 14:05:10 +01:00
|
|
|
helpers.errorMessage(
|
2022-10-14 17:00:29 +02:00
|
|
|
ctx,
|
|
|
|
`Invalid type for options.choices: ${type}. If options.type is "select", options.choices must be an array.`,
|
|
|
|
"TYPE",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
options.choices = _options.choices.map((choice, i) => helpers.string(ctx, `options.choices[${i}]`, choice));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
PromptEvent.emit({
|
|
|
|
txt: txt,
|
|
|
|
options,
|
|
|
|
resolve: resolve,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2024-03-20 22:20:29 +01:00
|
|
|
wget: (ctx) => async (_url, _target, _hostname) => {
|
2022-10-14 17:00:29 +02:00
|
|
|
const url = helpers.string(ctx, "url", _url);
|
2023-04-28 17:14:29 +02:00
|
|
|
const target = helpers.filePath(ctx, "target", _target);
|
2022-10-14 17:00:29 +02:00
|
|
|
const hostname = _hostname ? helpers.string(ctx, "hostname", _hostname) : ctx.workerScript.hostname;
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
|
|
|
if (!target || (!hasTextExtension(target) && !hasScriptExtension(target))) {
|
2022-10-14 17:00:29 +02:00
|
|
|
helpers.log(ctx, () => `Invalid target file: '${target}'. Must be a script or text file.`);
|
2024-03-20 22:20:29 +01:00
|
|
|
return false;
|
2022-10-14 17:00:29 +02:00
|
|
|
}
|
2024-03-20 22:20:29 +01:00
|
|
|
let response: Response;
|
|
|
|
try {
|
|
|
|
response = await fetch(url);
|
|
|
|
} catch (error) {
|
|
|
|
/**
|
|
|
|
* Properties in error are not enumerable, so JSON.stringify(error) returns "{}". We need to explicitly specify
|
|
|
|
* the properties in the "replacer" parameter of JSON.stringify. We can do it by using Object.getOwnPropertyNames.
|
|
|
|
*
|
|
|
|
* Ref:
|
|
|
|
* - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
|
|
|
|
* - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
|
|
|
|
* - https://stackoverflow.com/q/18391212
|
|
|
|
*/
|
|
|
|
helpers.log(ctx, () => JSON.stringify(error, Object.getOwnPropertyNames(error)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (response.status !== 200) {
|
|
|
|
helpers.log(ctx, () => `wget failed. HTTP code: ${response.status}.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const writeResult = server.writeToContentFile(target, await response.text());
|
|
|
|
if (writeResult.overwritten) {
|
|
|
|
helpers.log(ctx, () => `Successfully retrieved content and overwrote '${target}' on '${hostname}'`);
|
|
|
|
} else {
|
|
|
|
helpers.log(ctx, () => `Successfully retrieved content to new file '${target}' on '${hostname}'`);
|
|
|
|
}
|
|
|
|
return true;
|
2022-10-14 17:00:29 +02:00
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
getFavorToDonate: () => () => {
|
2023-06-16 23:52:42 +02:00
|
|
|
return Math.floor(CONSTANTS.BaseFavorToDonate * currentNodeMults.RepToDonateToFaction);
|
2022-08-09 21:41:47 +02:00
|
|
|
},
|
2023-04-25 11:22:36 +02:00
|
|
|
getPlayer: () => () => {
|
2022-08-09 21:41:47 +02:00
|
|
|
const data = {
|
2023-04-25 11:22:36 +02:00
|
|
|
// Person
|
2024-02-10 10:10:19 +01:00
|
|
|
hp: structuredClone(Player.hp),
|
|
|
|
skills: structuredClone(Player.skills),
|
|
|
|
exp: structuredClone(Player.exp),
|
|
|
|
mults: structuredClone(Player.mults),
|
2023-04-25 11:22:36 +02:00
|
|
|
city: Player.city,
|
|
|
|
// Player-specific
|
2022-08-09 21:41:47 +02:00
|
|
|
numPeopleKilled: Player.numPeopleKilled,
|
|
|
|
money: Player.money,
|
|
|
|
location: Player.location,
|
|
|
|
totalPlaytime: Player.totalPlaytime,
|
2024-02-10 10:10:19 +01:00
|
|
|
jobs: structuredClone(Player.jobs),
|
2022-08-09 21:41:47 +02:00
|
|
|
factions: Player.factions.slice(),
|
|
|
|
entropy: Player.entropy,
|
2024-03-04 15:35:54 +01:00
|
|
|
karma: Player.karma,
|
2022-08-09 21:41:47 +02:00
|
|
|
};
|
2023-04-25 11:22:36 +02:00
|
|
|
setDeprecatedProperties(data, {
|
|
|
|
playtimeSinceLastAug: {
|
|
|
|
identifier: "ns.getPlayer().playtimeSinceLastAug",
|
|
|
|
message: "Use ns.getResetInfo().lastAugReset instead. This is a static timestamp instead of an elapsed time.",
|
|
|
|
value: Player.playtimeSinceLastAug,
|
|
|
|
},
|
|
|
|
playtimeSinceLastBitnode: {
|
|
|
|
identifier: "ns.getPlayer().playtimeSinceLastBitnode",
|
|
|
|
message: "Use ns.getResetInfo().lastNodeReset instead. This is a static timestamp instead of an elapsed time.",
|
|
|
|
value: Player.playtimeSinceLastBitnode,
|
|
|
|
},
|
|
|
|
bitNodeN: {
|
|
|
|
identifier: "ns.getPlayer().bitNodeN",
|
2023-05-16 05:12:17 +02:00
|
|
|
message: "Use ns.getResetInfo().currentNode instead",
|
2023-04-25 11:22:36 +02:00
|
|
|
value: Player.bitNodeN,
|
|
|
|
},
|
|
|
|
});
|
2022-08-09 21:41:47 +02:00
|
|
|
return data;
|
|
|
|
},
|
2022-10-13 07:07:10 +02:00
|
|
|
getMoneySources: () => () => ({
|
2022-10-13 06:55:57 +02:00
|
|
|
sinceInstall: Object.assign({}, Player.moneySourceA),
|
|
|
|
sinceStart: Object.assign({}, Player.moneySourceB),
|
|
|
|
}),
|
2024-03-06 01:42:55 +01:00
|
|
|
atExit: (ctx) => (callback, _id) => {
|
|
|
|
const id = _id ? helpers.string(ctx, "id", _id) : "default";
|
|
|
|
assertFunction(ctx, "callback", callback);
|
|
|
|
ctx.workerScript.atExit.set(id, callback);
|
|
|
|
},
|
2022-10-12 14:49:27 +02:00
|
|
|
mv: (ctx) => (_host, _source, _destination) => {
|
2023-04-18 09:19:45 +02:00
|
|
|
const hostname = helpers.string(ctx, "host", _host);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const server = helpers.getServer(ctx, hostname);
|
2023-04-28 00:21:06 +02:00
|
|
|
const sourcePath = helpers.filePath(ctx, "source", _source);
|
|
|
|
const destinationPath = helpers.filePath(ctx, "destination", _destination);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
(!hasTextExtension(sourcePath) && !hasScriptExtension(sourcePath)) ||
|
|
|
|
(!hasTextExtension(destinationPath) && !hasScriptExtension(destinationPath))
|
|
|
|
) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `'mv' can only be used on scripts and text files (.txt)`);
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
}
|
|
|
|
if (sourcePath === destinationPath) {
|
|
|
|
helpers.log(ctx, () => "WARNING: Did nothing, source and destination paths were the same.");
|
2022-10-12 14:49:27 +02:00
|
|
|
return;
|
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const sourceContentFile = server.getContentFile(sourcePath);
|
|
|
|
if (!sourceContentFile) {
|
2024-02-26 14:05:10 +01:00
|
|
|
throw helpers.errorMessage(ctx, `Source text file ${sourcePath} does not exist on ${hostname}`);
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
const success = sourceContentFile.deleteFromServer(server);
|
|
|
|
if (success) {
|
|
|
|
const { overwritten } = server.writeToContentFile(destinationPath, sourceContentFile.content);
|
|
|
|
if (overwritten) helpers.log(ctx, () => `WARNING: Overwriting file ${destinationPath} on ${hostname}`);
|
|
|
|
helpers.log(ctx, () => `Moved ${sourcePath} to ${destinationPath} on ${hostname}`);
|
|
|
|
return;
|
2022-10-12 14:49:27 +02:00
|
|
|
}
|
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
|
|
|
helpers.log(ctx, () => `ERROR: Failed. Was unable to remove file ${sourcePath} from its original location.`);
|
2022-10-12 14:49:27 +02:00
|
|
|
},
|
2023-04-25 11:22:36 +02:00
|
|
|
getResetInfo: () => () => ({
|
|
|
|
lastAugReset: Player.lastAugReset,
|
|
|
|
lastNodeReset: Player.lastNodeReset,
|
|
|
|
currentNode: Player.bitNodeN,
|
2023-08-01 07:40:30 +02:00
|
|
|
ownedAugs: new Map(Player.augmentations.map((aug) => [aug.name, aug.level])),
|
2024-11-11 07:56:57 +01:00
|
|
|
ownedSF: new Map(
|
|
|
|
[...Player.activeSourceFiles].filter(([__, activeLevel]) => {
|
|
|
|
return activeLevel > 0;
|
|
|
|
}),
|
|
|
|
),
|
2024-07-14 23:30:30 +02:00
|
|
|
bitNodeOptions: {
|
|
|
|
...Player.bitNodeOptions,
|
|
|
|
sourceFileOverrides: new Map(Player.bitNodeOptions.sourceFileOverrides),
|
|
|
|
},
|
2023-04-25 11:22:36 +02:00
|
|
|
}),
|
2023-05-26 13:46:08 +02:00
|
|
|
getFunctionRamCost: (ctx) => (_name) => {
|
|
|
|
const name = helpers.string(ctx, "name", _name);
|
2023-08-07 08:38:38 +02:00
|
|
|
return getRamCost(name.split("."), true);
|
2023-05-26 13:46:08 +02:00
|
|
|
},
|
2023-07-08 00:23:26 +02:00
|
|
|
tprintRaw: () => (value) => {
|
|
|
|
Terminal.printRaw(wrapUserNode(value));
|
|
|
|
},
|
|
|
|
printRaw: (ctx) => (value) => {
|
|
|
|
ctx.workerScript.print(wrapUserNode(value));
|
|
|
|
},
|
2022-08-09 21:41:47 +02:00
|
|
|
flags: Flags,
|
2024-03-05 00:36:14 +01:00
|
|
|
heart: { break: () => () => Player.karma },
|
2022-08-09 21:41:47 +02:00
|
|
|
...NetscriptExtra(),
|
|
|
|
};
|
2023-08-07 08:38:38 +02:00
|
|
|
|
|
|
|
// Removed functions
|
|
|
|
setRemovedFunctions(ns, {
|
|
|
|
getServerRam: { version: "2.2.0", replacement: "getServerMaxRam and getServerUsedRam" },
|
2023-01-02 19:18:02 +01:00
|
|
|
});
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2023-01-06 02:41:24 +01:00
|
|
|
export function NetscriptFunctions(ws: WorkerScript): NSFull {
|
2023-01-04 15:23:20 +01:00
|
|
|
return NSProxy(ws, ns, [], { args: ws.args.slice(), pid: ws.pid, enums });
|
2022-11-28 15:11:55 +01:00
|
|
|
}
|
|
|
|
|
2024-08-17 04:10:20 +02:00
|
|
|
const possibleLogs = Object.fromEntries(getFunctionNames(ns, "").map((a) => [a, true]));
|
|
|
|
possibleLogs.ALL = true;
|
|
|
|
|
|
|
|
// We reuse this object for *all* scripts that disable all keys, to prevent memory growth.
|
|
|
|
// Any script that needs a custom set of values will use a fresh object.
|
|
|
|
const allDisabled = { ALL: true } as const;
|
|
|
|
|
2022-08-29 08:41:17 +02:00
|
|
|
/** Provides an array of all function names on a nested object */
|
|
|
|
function getFunctionNames(obj: object, prefix: string): string[] {
|
|
|
|
const functionNames: string[] = [];
|
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
|
|
if (key === "args") {
|
|
|
|
continue;
|
2024-04-10 03:24:57 +02:00
|
|
|
} else if (typeof value === "function") {
|
2022-08-29 08:41:17 +02:00
|
|
|
functionNames.push(prefix + key);
|
2024-04-10 03:24:57 +02:00
|
|
|
} else if (typeof value === "object") {
|
2024-11-04 07:35:14 +01:00
|
|
|
functionNames.push(...getFunctionNames(value as object, `${prefix}${key}.`));
|
2022-08-29 08:41:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return functionNames;
|
|
|
|
}
|