GO: Various changes before 2.6.0 (#1120)

This commit is contained in:
Snarling
2024-02-26 08:05:10 -05:00
committed by GitHub
parent f6871f0911
commit 373ced2efe
62 changed files with 1626 additions and 2135 deletions
+32 -32
View File
@@ -173,7 +173,7 @@ export const ns: InternalAPI<NSFull> = {
return -1;
}
if (isNaN(hackAmount)) {
throw helpers.makeRuntimeErrorMsg(
throw helpers.errorMessage(
ctx,
`Invalid hackAmount argument passed into hackAnalyzeThreads: ${hackAmount}. Must be numeric.`,
);
@@ -269,7 +269,7 @@ export const ns: InternalAPI<NSFull> = {
// No root access or skill level too low
const canHack = netscriptCanGrow(server);
if (!canHack.res) {
throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || "");
throw helpers.errorMessage(ctx, canHack.msg || "");
}
const growTime = calculateGrowTime(server, Player) + additionalMsec / 1000.0;
@@ -318,11 +318,11 @@ export const ns: InternalAPI<NSFull> = {
return 0;
}
if (mult < 1 || !isFinite(mult)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: multiplier must be finite and >= 1, is ${mult}.`);
throw helpers.errorMessage(ctx, `Invalid argument: multiplier must be finite and >= 1, is ${mult}.`);
}
// 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) {
throw helpers.makeRuntimeErrorMsg(ctx, `Cores should be a positive integer. Cores provided: ${cores}`);
throw helpers.errorMessage(ctx, `Cores should be a positive integer. Cores provided: ${cores}`);
}
return numCycleForGrowth(server, mult, cores);
@@ -363,7 +363,7 @@ export const ns: InternalAPI<NSFull> = {
// No root access or skill level too low
const canHack = netscriptCanWeaken(server);
if (!canHack.res) {
throw helpers.makeRuntimeErrorMsg(ctx, canHack.msg || "");
throw helpers.errorMessage(ctx, canHack.msg || "");
}
const weakenTime = calculateWeakenTime(server, Player) + additionalMsec / 1000.0;
@@ -424,7 +424,7 @@ export const ns: InternalAPI<NSFull> = {
(ctx) =>
(...args) => {
if (args.length === 0) {
throw helpers.makeRuntimeErrorMsg(ctx, "Takes at least 1 argument.");
throw helpers.errorMessage(ctx, "Takes at least 1 argument.");
}
ctx.workerScript.print(helpers.argsToString(args));
},
@@ -433,7 +433,7 @@ export const ns: InternalAPI<NSFull> = {
(_format, ...args) => {
const format = helpers.string(ctx, "format", _format);
if (typeof format !== "string") {
throw helpers.makeRuntimeErrorMsg(ctx, "First argument must be string for the format.");
throw helpers.errorMessage(ctx, "First argument must be string for the format.");
}
ctx.workerScript.print(vsprintf(format, args));
},
@@ -441,7 +441,7 @@ export const ns: InternalAPI<NSFull> = {
(ctx) =>
(...args) => {
if (args.length === 0) {
throw helpers.makeRuntimeErrorMsg(ctx, "Takes at least 1 argument.");
throw helpers.errorMessage(ctx, "Takes at least 1 argument.");
}
const str = helpers.argsToString(args);
if (str.startsWith("ERROR") || str.startsWith("FAIL")) {
@@ -497,7 +497,7 @@ export const ns: InternalAPI<NSFull> = {
}
helpers.log(ctx, () => `Disabled logging for all functions`);
} else if (possibleLogs[fn] === undefined) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${fn}.`);
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
} else {
ctx.workerScript.disableLogs[fn] = true;
helpers.log(ctx, () => `Disabled logging for ${fn}`);
@@ -511,7 +511,7 @@ export const ns: InternalAPI<NSFull> = {
}
helpers.log(ctx, () => `Enabled logging for all functions`);
} else if (possibleLogs[fn] === undefined) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${fn}.`);
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
}
delete ctx.workerScript.disableLogs[fn];
helpers.log(ctx, () => `Enabled logging for ${fn}`);
@@ -519,7 +519,7 @@ export const ns: InternalAPI<NSFull> = {
isLogEnabled: (ctx) => (_fn) => {
const fn = helpers.string(ctx, "fn", _fn);
if (possibleLogs[fn] === undefined) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${fn}.`);
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
}
return !ctx.workerScript.disableLogs[fn];
},
@@ -605,10 +605,10 @@ export const ns: InternalAPI<NSFull> = {
return true;
}
if (!Player.hasProgram(CompletedProgramName.nuke)) {
throw helpers.makeRuntimeErrorMsg(ctx, "You do not have the NUKE.exe virus!");
throw helpers.errorMessage(ctx, "You do not have the NUKE.exe virus!");
}
if (server.openPortCount < server.numOpenPortsRequired) {
throw helpers.makeRuntimeErrorMsg(ctx, "Not enough ports opened to use NUKE.exe virus.");
throw helpers.errorMessage(ctx, "Not enough ports opened to use NUKE.exe virus.");
}
server.hasAdminRights = true;
helpers.log(ctx, () => `Executed NUKE.exe virus on '${server.hostname}' to gain root access.`);
@@ -622,7 +622,7 @@ export const ns: InternalAPI<NSFull> = {
return false;
}
if (!Player.hasProgram(CompletedProgramName.bruteSsh)) {
throw helpers.makeRuntimeErrorMsg(ctx, "You do not have the BruteSSH.exe program!");
throw helpers.errorMessage(ctx, "You do not have the BruteSSH.exe program!");
}
if (!server.sshPortOpen) {
helpers.log(ctx, () => `Executed BruteSSH.exe on '${server.hostname}' to open SSH port (22).`);
@@ -641,7 +641,7 @@ export const ns: InternalAPI<NSFull> = {
return false;
}
if (!Player.hasProgram(CompletedProgramName.ftpCrack)) {
throw helpers.makeRuntimeErrorMsg(ctx, "You do not have the FTPCrack.exe program!");
throw helpers.errorMessage(ctx, "You do not have the FTPCrack.exe program!");
}
if (!server.ftpPortOpen) {
helpers.log(ctx, () => `Executed FTPCrack.exe on '${server.hostname}' to open FTP port (21).`);
@@ -660,7 +660,7 @@ export const ns: InternalAPI<NSFull> = {
return false;
}
if (!Player.hasProgram(CompletedProgramName.relaySmtp)) {
throw helpers.makeRuntimeErrorMsg(ctx, "You do not have the relaySMTP.exe program!");
throw helpers.errorMessage(ctx, "You do not have the relaySMTP.exe program!");
}
if (!server.smtpPortOpen) {
helpers.log(ctx, () => `Executed relaySMTP.exe on '${server.hostname}' to open SMTP port (25).`);
@@ -679,7 +679,7 @@ export const ns: InternalAPI<NSFull> = {
return false;
}
if (!Player.hasProgram(CompletedProgramName.httpWorm)) {
throw helpers.makeRuntimeErrorMsg(ctx, "You do not have the HTTPWorm.exe program!");
throw helpers.errorMessage(ctx, "You do not have the HTTPWorm.exe program!");
}
if (!server.httpPortOpen) {
helpers.log(ctx, () => `Executed HTTPWorm.exe on '${server.hostname}' to open HTTP port (80).`);
@@ -698,7 +698,7 @@ export const ns: InternalAPI<NSFull> = {
return false;
}
if (!Player.hasProgram(CompletedProgramName.sqlInject)) {
throw helpers.makeRuntimeErrorMsg(ctx, "You do not have the SQLInject.exe program!");
throw helpers.errorMessage(ctx, "You do not have the SQLInject.exe program!");
}
if (!server.sqlPortOpen) {
helpers.log(ctx, () => `Executed SQLInject.exe on '${server.hostname}' to open SQL port (1433).`);
@@ -738,7 +738,7 @@ export const ns: InternalAPI<NSFull> = {
setTimeout(() => {
const scriptServer = GetServer(ctx.workerScript.hostname);
if (scriptServer == null) {
throw helpers.makeRuntimeErrorMsg(ctx, "Could not find server. This is a bug. Report to dev");
throw helpers.errorMessage(ctx, "Could not find server. This is a bug. Report to dev");
}
return runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts);
@@ -762,7 +762,7 @@ export const ns: InternalAPI<NSFull> = {
} else {
// Kill by filename/hostname
if (scriptID === undefined) {
throw helpers.makeRuntimeErrorMsg(ctx, "Usage: kill(scriptname, server, [arg1], [arg2]...)");
throw helpers.errorMessage(ctx, "Usage: kill(scriptname, server, [arg1], [arg2]...)");
}
const byPid = helpers.getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
@@ -837,7 +837,7 @@ export const ns: InternalAPI<NSFull> = {
continue;
}
if (!path.endsWith(".lit")) {
throw helpers.makeRuntimeErrorMsg(ctx, "Only works for scripts, .lit and .txt files.");
throw helpers.errorMessage(ctx, "Only works for scripts, .lit and .txt files.");
}
lits.push(path);
}
@@ -954,7 +954,7 @@ export const ns: InternalAPI<NSFull> = {
(ctx) =>
(_n = Player.bitNodeN, _lvl = Player.sourceFileLvl(Player.bitNodeN) + 1) => {
if (Player.sourceFileLvl(5) <= 0 && Player.bitNodeN !== 5)
throw helpers.makeRuntimeErrorMsg(ctx, "Requires Source-File 5 to run.");
throw helpers.errorMessage(ctx, "Requires Source-File 5 to run.");
const n = Math.round(helpers.number(ctx, "n", _n));
const lvl = Math.round(helpers.number(ctx, "lvl", _lvl));
if (n < 1 || n > 14) throw new Error("n must be between 1 and 14");
@@ -1350,7 +1350,7 @@ export const ns: InternalAPI<NSFull> = {
return;
}
if (!hasTextExtension(filepath)) {
throw helpers.makeRuntimeErrorMsg(ctx, `File path should be a text file or script. ${filepath} is invalid.`);
throw helpers.errorMessage(ctx, `File path should be a text file or script. ${filepath} is invalid.`);
}
if (mode === "w") {
server.writeToTextFile(filepath, data);
@@ -1385,11 +1385,11 @@ export const ns: InternalAPI<NSFull> = {
clear: (ctx) => (_file) => {
const path = helpers.filePath(ctx, "file", _file);
if (!hasScriptExtension(path) && !hasTextExtension(path)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid file path or extension: ${_file}`);
throw helpers.errorMessage(ctx, `Invalid file path or extension: ${_file}`);
}
const server = ctx.workerScript.getServer();
const file = server.getContentFile(path);
if (!file) throw helpers.makeRuntimeErrorMsg(ctx, `${path} does not exist on ${server.hostname}`);
if (!file) throw helpers.errorMessage(ctx, `${path} does not exist on ${server.hostname}`);
// The content setter handles invalidating script modules where applicable.
file.content = "";
},
@@ -1603,23 +1603,23 @@ export const ns: InternalAPI<NSFull> = {
_options ??= options;
const txt = helpers.string(ctx, "txt", _txt);
assert(_options, objectAssert, (type) =>
helpers.makeRuntimeErrorMsg(ctx, `Invalid type for options: ${type}. Should be object.`, "TYPE"),
helpers.errorMessage(ctx, `Invalid type for options: ${type}. Should be object.`, "TYPE"),
);
if (_options.type !== undefined) {
assert(_options.type, stringAssert, (type) =>
helpers.makeRuntimeErrorMsg(ctx, `Invalid type for options.type: ${type}. Should be string.`, "TYPE"),
helpers.errorMessage(ctx, `Invalid type for options.type: ${type}. Should be string.`, "TYPE"),
);
options.type = _options.type;
const validTypes = ["boolean", "text", "select"];
if (!["boolean", "text", "select"].includes(options.type)) {
throw helpers.makeRuntimeErrorMsg(
throw helpers.errorMessage(
ctx,
`Invalid value for options.type: ${options.type}. Must be one of ${validTypes.join(", ")}.`,
);
}
if (options.type === "select") {
assert(_options.choices, arrayAssert, (type) =>
helpers.makeRuntimeErrorMsg(
helpers.errorMessage(
ctx,
`Invalid type for options.choices: ${type}. If options.type is "select", options.choices must be an array.`,
"TYPE",
@@ -1709,7 +1709,7 @@ export const ns: InternalAPI<NSFull> = {
}),
atExit: (ctx) => (f) => {
if (typeof f !== "function") {
throw helpers.makeRuntimeErrorMsg(ctx, "argument should be function");
throw helpers.errorMessage(ctx, "argument should be function");
}
ctx.workerScript.atExit = () => {
f();
@@ -1725,7 +1725,7 @@ export const ns: InternalAPI<NSFull> = {
(!hasTextExtension(sourcePath) && !hasScriptExtension(sourcePath)) ||
(!hasTextExtension(destinationPath) && !hasScriptExtension(destinationPath))
) {
throw helpers.makeRuntimeErrorMsg(ctx, `'mv' can only be used on scripts and text files (.txt)`);
throw helpers.errorMessage(ctx, `'mv' can only be used on scripts and text files (.txt)`);
}
if (sourcePath === destinationPath) {
helpers.log(ctx, () => "WARNING: Did nothing, source and destination paths were the same.");
@@ -1733,7 +1733,7 @@ export const ns: InternalAPI<NSFull> = {
}
const sourceContentFile = server.getContentFile(sourcePath);
if (!sourceContentFile) {
throw helpers.makeRuntimeErrorMsg(ctx, `Source text file ${sourcePath} does not exist on ${hostname}`);
throw helpers.errorMessage(ctx, `Source text file ${sourcePath} does not exist on ${hostname}`);
}
const success = sourceContentFile.deleteFromServer(server);
if (success) {