This commit is contained in:
Olivier Gagnon 2021-10-23 10:09:43 -04:00
parent eec75b0840
commit 9493768c0b

@ -166,7 +166,12 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
* exists, or the current running script if the first argument 'fn' * exists, or the current running script if the first argument 'fn'
* is not specified. * is not specified.
*/ */
const getRunningScript = function (fn: any, hostname: any, callingFnName: any, scriptArgs: any): RunningScript | null { const getRunningScript = function (
fn: any,
hostname: any,
callingFnName: any,
scriptArgs: any,
): RunningScript | null {
if (typeof callingFnName !== "string" || callingFnName === "") { if (typeof callingFnName !== "string" || callingFnName === "") {
callingFnName = "getRunningScript"; callingFnName = "getRunningScript";
} }
@ -334,8 +339,8 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
throw makeRuntimeErrorMsg("hack", "Takes 1 argument."); throw makeRuntimeErrorMsg("hack", "Takes 1 argument.");
} }
const threads = resolveNetscriptRequestedThreads(workerScript, "hack", requestedThreads); const threads = resolveNetscriptRequestedThreads(workerScript, "hack", requestedThreads);
const server = safeGetServer(hostname,"hack"); const server = safeGetServer(hostname, "hack");
if (!(server instanceof Server)){ if (!(server instanceof Server)) {
throw makeRuntimeErrorMsg("hack", "Cannot be executed on this server."); throw makeRuntimeErrorMsg("hack", "Cannot be executed on this server.");
} }
@ -430,7 +435,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
let out = ""; let out = "";
for (let arg of args) { for (let arg of args) {
arg = toNative(arg); arg = toNative(arg);
out += (typeof arg === "object") ? JSON.stringify(arg) : `${arg}`; out += typeof arg === "object" ? JSON.stringify(arg) : `${arg}`;
} }
return out; return out;
@ -474,7 +479,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
vsprintf: vsprintf, vsprintf: vsprintf,
scan: function (hostname: any = workerScript.hostname): any { scan: function (hostname: any = workerScript.hostname): any {
updateDynamicRam("scan", getRamCost("scan")); updateDynamicRam("scan", getRamCost("scan"));
const server = safeGetServer(hostname,"scan"); const server = safeGetServer(hostname, "scan");
const out = []; const out = [];
for (let i = 0; i < server.serversOnNetwork.length; i++) { for (let i = 0; i < server.serversOnNetwork.length; i++) {
const s = getServerOnNetwork(server, i); const s = getServerOnNetwork(server, i);
@ -551,7 +556,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("grow", "Takes 1 argument."); throw makeRuntimeErrorMsg("grow", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"grow"); const server = safeGetServer(hostname, "grow");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("grow", "Cannot be executed on this server."); workerScript.log("grow", "Cannot be executed on this server.");
return false; return false;
@ -622,7 +627,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("weaken", "Takes 1 argument."); throw makeRuntimeErrorMsg("weaken", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"weaken"); const server = safeGetServer(hostname, "weaken");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("weaken", "Cannot be executed on this server."); workerScript.log("weaken", "Cannot be executed on this server.");
return false; return false;
@ -738,7 +743,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("nuke", "Takes 1 argument."); throw makeRuntimeErrorMsg("nuke", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"nuke"); const server = safeGetServer(hostname, "nuke");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("nuke", "Cannot be executed on this server."); workerScript.log("nuke", "Cannot be executed on this server.");
return false; return false;
@ -762,7 +767,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("brutessh", "Takes 1 argument."); throw makeRuntimeErrorMsg("brutessh", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"brutessh"); const server = safeGetServer(hostname, "brutessh");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("brutessh", "Cannot be executed on this server."); workerScript.log("brutessh", "Cannot be executed on this server.");
return false; return false;
@ -784,7 +789,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("ftpcrack", "Takes 1 argument."); throw makeRuntimeErrorMsg("ftpcrack", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"ftpcrack"); const server = safeGetServer(hostname, "ftpcrack");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("ftpcrack", "Cannot be executed on this server."); workerScript.log("ftpcrack", "Cannot be executed on this server.");
return false; return false;
@ -806,7 +811,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("relaysmtp", "Takes 1 argument."); throw makeRuntimeErrorMsg("relaysmtp", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"relaysmtp"); const server = safeGetServer(hostname, "relaysmtp");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("relaysmtp", "Cannot be executed on this server."); workerScript.log("relaysmtp", "Cannot be executed on this server.");
return false; return false;
@ -828,7 +833,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("httpworm", "Takes 1 argument"); throw makeRuntimeErrorMsg("httpworm", "Takes 1 argument");
} }
const server = safeGetServer(hostname,"httpworm"); const server = safeGetServer(hostname, "httpworm");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("httpworm", "Cannot be executed on this server."); workerScript.log("httpworm", "Cannot be executed on this server.");
return false; return false;
@ -850,7 +855,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("sqlinject", "Takes 1 argument."); throw makeRuntimeErrorMsg("sqlinject", "Takes 1 argument.");
} }
const server = safeGetServer(hostname,"sqlinject"); const server = safeGetServer(hostname, "sqlinject");
if (!(server instanceof Server)) { if (!(server instanceof Server)) {
workerScript.log("sqlinject", "Cannot be executed on this server."); workerScript.log("sqlinject", "Cannot be executed on this server.");
return false; return false;
@ -890,7 +895,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (isNaN(threads) || threads <= 0) { if (isNaN(threads) || threads <= 0) {
throw makeRuntimeErrorMsg("exec", `Invalid thread count. Must be numeric and > 0, is ${threads}`); throw makeRuntimeErrorMsg("exec", `Invalid thread count. Must be numeric and > 0, is ${threads}`);
} }
const server = safeGetServer(hostname,"exec"); const server = safeGetServer(hostname, "exec");
return runScriptFromScript("exec", server, scriptname, args, workerScript, threads); return runScriptFromScript("exec", server, scriptname, args, workerScript, threads);
}, },
spawn: function (scriptname: any, threads: any, ...args: any[]): any { spawn: function (scriptname: any, threads: any, ...args: any[]): any {
@ -954,7 +959,10 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (killByPid) { if (killByPid) {
workerScript.log("kill", `No script with PID ${filename}`); workerScript.log("kill", `No script with PID ${filename}`);
} else { } else {
workerScript.log("kill", `No such script '${filename}' on '${hostname}' with args: ${arrayToString(scriptArgs)}`); workerScript.log(
"kill",
`No such script '${filename}' on '${hostname}' with args: ${arrayToString(scriptArgs)}`,
);
} }
return false; return false;
} }
@ -964,7 +972,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("killall", "Takes 1 argument"); throw makeRuntimeErrorMsg("killall", "Takes 1 argument");
} }
const server = safeGetServer(hostname,"killall"); const server = safeGetServer(hostname, "killall");
const scriptsRunning = server.runningScripts.length > 0; const scriptsRunning = server.runningScripts.length > 0;
for (let i = server.runningScripts.length - 1; i >= 0; --i) { for (let i = server.runningScripts.length - 1; i >= 0; --i) {
killWorkerScript(server.runningScripts[i], server.hostname, false); killWorkerScript(server.runningScripts[i], server.hostname, false);
@ -1018,18 +1026,20 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (scriptname === undefined || hostname1 === undefined || hostname2 === undefined) { if (scriptname === undefined || hostname1 === undefined || hostname2 === undefined) {
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments"); throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
} }
destServer = safeGetServer(hostname2,"scp"); destServer = safeGetServer(hostname2, "scp");
currServ = safeGetServer(hostname1, "scp"); currServ = safeGetServer(hostname1, "scp");
} else if (hostname1 != null) { } else if (hostname1 != null) {
// 2 Argument version: scriptname, destination // 2 Argument version: scriptname, destination
if (scriptname === undefined || hostname1 === undefined) { if (scriptname === undefined || hostname1 === undefined) {
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments"); throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
} }
destServer = safeGetServer(hostname1,"scp"); destServer = safeGetServer(hostname1, "scp");
currServ = GetServer(workerScript.hostname); currServ = GetServer(workerScript.hostname);
if (currServ == null) { if (currServ == null) {
throw makeRuntimeErrorMsg("scp", "Could not find server hostname for this script. This is a bug. Report to dev."); throw makeRuntimeErrorMsg(
"scp",
"Could not find server hostname for this script. This is a bug. Report to dev.",
);
} }
} else { } else {
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments"); throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
@ -1132,7 +1142,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("ls", "Usage: ls(hostname/ip, [grep filter])"); throw makeRuntimeErrorMsg("ls", "Usage: ls(hostname/ip, [grep filter])");
} }
const server = safeGetServer(hostname,"ls"); const server = safeGetServer(hostname, "ls");
// Get the grep filter, if one exists // Get the grep filter, if one exists
let filter = ""; let filter = "";
@ -1196,7 +1206,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
}, },
ps: function (hostname: any = workerScript.hostname): any { ps: function (hostname: any = workerScript.hostname): any {
updateDynamicRam("ps", getRamCost("ps")); updateDynamicRam("ps", getRamCost("ps"));
const server = safeGetServer(hostname,"ps"); const server = safeGetServer(hostname, "ps");
const processes = []; const processes = [];
for (const i in server.runningScripts) { for (const i in server.runningScripts) {
const script = server.runningScripts[i]; const script = server.runningScripts[i];
@ -1214,7 +1224,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (hostname === undefined) { if (hostname === undefined) {
throw makeRuntimeErrorMsg("hasRootAccess", "Takes 1 argument"); throw makeRuntimeErrorMsg("hasRootAccess", "Takes 1 argument");
} }
const server = safeGetServer(hostname,"hasRootAccess"); const server = safeGetServer(hostname, "hasRootAccess");
return server.hasAdminRights; return server.hasAdminRights;
}, },
getIp: function (): any { getIp: function (): any {
@ -1440,7 +1450,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
if (filename === undefined) { if (filename === undefined) {
throw makeRuntimeErrorMsg("fileExists", "Usage: fileExists(scriptname, [server])"); throw makeRuntimeErrorMsg("fileExists", "Usage: fileExists(scriptname, [server])");
} }
const server = safeGetServer(hostname,"fileExists"); const server = safeGetServer(hostname, "fileExists");
for (let i = 0; i < server.scripts.length; ++i) { for (let i = 0; i < server.scripts.length; ++i) {
if (filename == server.scripts[i].filename) { if (filename == server.scripts[i].filename) {
return true; return true;
@ -1578,10 +1588,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
// Delete all scripts running on server // Delete all scripts running on server
if (server.runningScripts.length > 0) { if (server.runningScripts.length > 0) {
workerScript.log( workerScript.log("deleteServer", `Cannot delete server '${hostname}' because it still has scripts running.`);
"deleteServer",
`Cannot delete server '${hostname}' because it still has scripts running.`,
);
return false; return false;
} }
@ -1863,7 +1870,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
}, },
scriptRunning: function (scriptname: any, hostname: any): any { scriptRunning: function (scriptname: any, hostname: any): any {
updateDynamicRam("scriptRunning", getRamCost("scriptRunning")); updateDynamicRam("scriptRunning", getRamCost("scriptRunning"));
const server = safeGetServer(hostname,"scriptRunning"); const server = safeGetServer(hostname, "scriptRunning");
for (let i = 0; i < server.runningScripts.length; ++i) { for (let i = 0; i < server.runningScripts.length; ++i) {
if (server.runningScripts[i].filename == scriptname) { if (server.runningScripts[i].filename == scriptname) {
return true; return true;
@ -1873,7 +1880,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
}, },
scriptKill: function (scriptname: any, hostname: any): any { scriptKill: function (scriptname: any, hostname: any): any {
updateDynamicRam("scriptKill", getRamCost("scriptKill")); updateDynamicRam("scriptKill", getRamCost("scriptKill"));
const server = safeGetServer(hostname,"scriptKill"); const server = safeGetServer(hostname, "scriptKill");
let suc = false; let suc = false;
for (let i = 0; i < server.runningScripts.length; ++i) { for (let i = 0; i < server.runningScripts.length; ++i) {
if (server.runningScripts[i].filename == scriptname) { if (server.runningScripts[i].filename == scriptname) {
@ -1888,7 +1895,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
}, },
getScriptRam: function (scriptname: any, hostname: any = workerScript.hostname): any { getScriptRam: function (scriptname: any, hostname: any = workerScript.hostname): any {
updateDynamicRam("getScriptRam", getRamCost("getScriptRam")); updateDynamicRam("getScriptRam", getRamCost("getScriptRam"));
const server = safeGetServer(hostname,"getScriptRam"); const server = safeGetServer(hostname, "getScriptRam");
for (let i = 0; i < server.scripts.length; ++i) { for (let i = 0; i < server.scripts.length; ++i) {
if (server.scripts[i].filename == scriptname) { if (server.scripts[i].filename == scriptname) {
return server.scripts[i].ramUsage; return server.scripts[i].ramUsage;
@ -1984,7 +1991,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
return res; return res;
} else { } else {
// Get income for a particular script // Get income for a particular script
const server = safeGetServer(hostname,"getScriptIncome"); const server = safeGetServer(hostname, "getScriptIncome");
const runningScriptObj = findRunningScript(scriptname, args, server); const runningScriptObj = findRunningScript(scriptname, args, server);
if (runningScriptObj == null) { if (runningScriptObj == null) {
workerScript.log( workerScript.log(
@ -2006,7 +2013,7 @@ function NetscriptFunctions(workerScript: WorkerScript): NS {
return total; return total;
} else { } else {
// Get income for a particular script // Get income for a particular script
const server = safeGetServer(hostname,"getScriptExpGain"); const server = safeGetServer(hostname, "getScriptExpGain");
const runningScriptObj = findRunningScript(scriptname, args, server); const runningScriptObj = findRunningScript(scriptname, args, server);
if (runningScriptObj == null) { if (runningScriptObj == null) {
workerScript.log( workerScript.log(