2019-05-05 06:03:40 +02:00
|
|
|
/**
|
|
|
|
* Functions for handling WorkerScripts, which are the underlying mechanism
|
|
|
|
* that allows for scripts to run
|
|
|
|
*/
|
2019-05-17 08:44:59 +02:00
|
|
|
import { killWorkerScript } from "./Netscript/killWorkerScript";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { WorkerScript } from "./Netscript/WorkerScript";
|
2019-05-16 08:05:36 +02:00
|
|
|
import { workerScripts } from "./Netscript/WorkerScripts";
|
2019-05-17 22:41:16 +02:00
|
|
|
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter";
|
2021-03-07 19:08:12 +01:00
|
|
|
import { generateNextPid } from "./Netscript/Pid";
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { CONSTANTS } from "./Constants";
|
2021-09-25 00:42:13 +02:00
|
|
|
import { Interpreter } from "./ThirdParty/JSInterpreter";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { isScriptErrorMessage, makeRuntimeRejectMsg } from "./NetscriptEvaluator";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { NetscriptFunctions } from "./NetscriptFunctions";
|
|
|
|
import { executeJSScript } from "./NetscriptJSEvaluator";
|
2021-09-24 23:56:30 +02:00
|
|
|
import { NetscriptPort, IPort } from "./NetscriptPort";
|
2019-05-07 03:01:06 +02:00
|
|
|
import { RunningScript } from "./Script/RunningScript";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers";
|
2021-05-01 09:17:31 +02:00
|
|
|
import { scriptCalculateOfflineProduction } from "./Script/ScriptHelpers";
|
2021-09-24 23:56:30 +02:00
|
|
|
import { Script } from "./Script/Script";
|
2021-10-07 22:04:04 +02:00
|
|
|
import { GetAllServers } from "./Server/AllServers";
|
2021-09-24 23:56:30 +02:00
|
|
|
import { BaseServer } from "./Server/BaseServer";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Settings } from "./Settings/Settings";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { generate } from "escodegen";
|
|
|
|
|
2021-09-25 20:42:57 +02:00
|
|
|
import { dialogBoxCreate } from "./ui/React/DialogBox";
|
|
|
|
import { arrayToString } from "./utils/helpers/arrayToString";
|
|
|
|
import { roundToTwo } from "./utils/helpers/roundToTwo";
|
|
|
|
import { isString } from "./utils/helpers/isString";
|
2021-09-24 23:56:30 +02:00
|
|
|
import { sprintf } from "sprintf-js";
|
2018-07-20 16:28:03 +02:00
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
import { parse } from "acorn";
|
2021-05-29 20:48:56 +02:00
|
|
|
import { simple as walksimple } from "acorn-walk";
|
2021-11-06 02:01:23 +01:00
|
|
|
import { areFilesEqual } from "./Terminal/DirectoryHelpers";
|
2021-11-16 05:49:33 +01:00
|
|
|
import { Player } from "./Player";
|
2018-07-20 16:28:03 +02:00
|
|
|
|
2019-05-16 08:05:36 +02:00
|
|
|
// Netscript Ports are instantiated here
|
2021-09-24 23:56:30 +02:00
|
|
|
export const NetscriptPorts: IPort[] = [];
|
2021-09-25 07:26:03 +02:00
|
|
|
for (let i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) {
|
2021-09-22 02:39:25 +02:00
|
|
|
NetscriptPorts.push(NetscriptPort());
|
2017-07-22 00:54:55 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
export function prestigeWorkerScripts(): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const ws of workerScripts.values()) {
|
|
|
|
ws.env.stopFlag = true;
|
|
|
|
killWorkerScript(ws);
|
|
|
|
}
|
2019-06-19 10:51:25 +02:00
|
|
|
|
2021-09-18 21:44:39 +02:00
|
|
|
WorkerScriptStartStopEventEmitter.emit();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScripts.clear();
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 00:13:35 +02:00
|
|
|
// JS script promises need a little massaging to have the same guarantees as netscript
|
|
|
|
// promises. This does said massaging and kicks the script off. It returns a promise
|
|
|
|
// that resolves or rejects when the corresponding worker script is done.
|
2021-09-24 23:56:30 +02:00
|
|
|
function startNetscript2Script(workerScript: WorkerScript): Promise<WorkerScript> {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.running = true;
|
|
|
|
|
|
|
|
// The name of the currently running netscript function, to prevent concurrent
|
|
|
|
// calls to hack, grow, etc.
|
2021-09-24 23:56:30 +02:00
|
|
|
let runningFn: string | null = null;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// We need to go through the environment and wrap each function in such a way that it
|
|
|
|
// can be called at most once at a time. This will prevent situations where multiple
|
|
|
|
// hack promises are outstanding, for example.
|
2021-09-25 08:36:49 +02:00
|
|
|
function wrap(propName: string, f: (...args: any[]) => Promise<void>): (...args: any[]) => Promise<void> {
|
2021-09-05 01:09:30 +02:00
|
|
|
// This function unfortunately cannot be an async function, because we don't
|
|
|
|
// know if the original one was, and there's no way to tell.
|
2021-09-24 23:56:30 +02:00
|
|
|
return function (...args: any[]) {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Wrap every netscript function with a check for the stop flag.
|
|
|
|
// This prevents cases where we never stop because we are only calling
|
|
|
|
// netscript functions that don't check this.
|
|
|
|
// This is not a problem for legacy Netscript because it also checks the
|
|
|
|
// stop flag in the evaluator.
|
|
|
|
if (workerScript.env.stopFlag) {
|
|
|
|
throw workerScript;
|
|
|
|
}
|
|
|
|
|
2021-11-03 03:11:22 +01:00
|
|
|
if (propName === "asleep") return f(...args); // OK for multiple simultaneous calls to sleep.
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
const msg =
|
|
|
|
"Concurrent calls to Netscript functions not allowed! " +
|
|
|
|
"Did you forget to await hack(), grow(), or some other " +
|
|
|
|
"promise-returning function? (Currently running: %s tried to run: %s)";
|
|
|
|
if (runningFn) {
|
2021-09-25 08:36:49 +02:00
|
|
|
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, sprintf(msg, runningFn, propName));
|
2021-09-05 01:09:30 +02:00
|
|
|
throw workerScript;
|
|
|
|
}
|
|
|
|
runningFn = propName;
|
|
|
|
|
|
|
|
// If the function throws an error, clear the runningFn flag first, and then re-throw it
|
|
|
|
// This allows people to properly catch errors thrown by NS functions without getting
|
|
|
|
// the concurrent call error above
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = f(...args);
|
2021-10-13 08:27:55 +02:00
|
|
|
} catch (e: any) {
|
2021-09-05 01:09:30 +02:00
|
|
|
runningFn = null;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result && result.finally !== undefined) {
|
|
|
|
return result.finally(function () {
|
|
|
|
runningFn = null;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
runningFn = null;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
for (const prop in workerScript.env.vars) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (typeof workerScript.env.vars[prop] !== "function") continue;
|
|
|
|
workerScript.env.vars[prop] = wrap(prop, workerScript.env.vars[prop]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: the environment that we pass to the JS script only needs to contain the functions visible
|
|
|
|
// to that script, which env.vars does at this point.
|
2021-09-25 01:09:19 +02:00
|
|
|
return new Promise<WorkerScript>((resolve, reject) => {
|
|
|
|
executeJSScript(workerScript.getServer().scripts, workerScript)
|
|
|
|
.then(() => {
|
|
|
|
resolve(workerScript);
|
|
|
|
})
|
|
|
|
.catch((e) => reject(e));
|
|
|
|
}).catch((e) => {
|
2021-09-24 23:56:30 +02:00
|
|
|
if (e instanceof Error) {
|
2021-10-12 00:47:05 +02:00
|
|
|
if (e instanceof SyntaxError) {
|
|
|
|
workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, e.message + " (sorry we can't be more helpful)");
|
|
|
|
} else {
|
|
|
|
workerScript.errorMessage = makeRuntimeRejectMsg(
|
|
|
|
workerScript,
|
|
|
|
e.message + ((e.stack && "\nstack:\n" + e.stack.toString()) || ""),
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 23:56:30 +02:00
|
|
|
throw workerScript;
|
|
|
|
} else if (isScriptErrorMessage(e)) {
|
|
|
|
workerScript.errorMessage = e;
|
|
|
|
throw workerScript;
|
|
|
|
}
|
2021-11-14 02:39:16 +01:00
|
|
|
throw e; // Don't know what to do with it, let's rethrow.
|
2021-09-24 23:56:30 +02:00
|
|
|
});
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:56:30 +02:00
|
|
|
function startNetscript1Script(workerScript: WorkerScript): Promise<WorkerScript> {
|
2021-09-05 01:09:30 +02:00
|
|
|
const code = workerScript.code;
|
|
|
|
workerScript.running = true;
|
|
|
|
|
|
|
|
//Process imports
|
2021-09-25 07:26:03 +02:00
|
|
|
let codeWithImports, codeLineOffset;
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 07:26:03 +02:00
|
|
|
const importProcessingRes = processNetscript1Imports(code, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
codeWithImports = importProcessingRes.code;
|
|
|
|
codeLineOffset = importProcessingRes.lineOffset;
|
2021-10-13 08:27:55 +02:00
|
|
|
} catch (e: any) {
|
2021-09-09 05:47:34 +02:00
|
|
|
dialogBoxCreate("Error processing Imports in " + workerScript.name + ":<br>" + e);
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.env.stopFlag = true;
|
|
|
|
workerScript.running = false;
|
|
|
|
killWorkerScript(workerScript);
|
2021-09-24 23:56:30 +02:00
|
|
|
return Promise.resolve(workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const interpreterInitialization = function (int: any, scope: any): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
//Add the Netscript environment
|
2021-09-25 07:26:03 +02:00
|
|
|
const ns = NetscriptFunctions(workerScript);
|
|
|
|
for (const name in ns) {
|
|
|
|
const entry = ns[name];
|
2021-09-05 01:09:30 +02:00
|
|
|
if (typeof entry === "function") {
|
|
|
|
//Async functions need to be wrapped. See JS-Interpreter documentation
|
2021-11-14 01:33:37 +01:00
|
|
|
if (["hack", "grow", "weaken", "sleep", "prompt", "manualHack", "scp", "write"].includes(name)) {
|
2021-09-25 08:36:49 +02:00
|
|
|
const tempWrapper = function (...args: any[]): void {
|
2021-09-25 07:26:03 +02:00
|
|
|
const fnArgs = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
//All of the Object/array elements are in JSInterpreter format, so
|
|
|
|
//we have to convert them back to native format to pass them to these fns
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < args.length - 1; ++i) {
|
|
|
|
if (typeof args[i] === "object" || args[i].constructor === Array) {
|
|
|
|
fnArgs.push(int.pseudoToNative(args[i]));
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
2021-09-25 08:36:49 +02:00
|
|
|
fnArgs.push(args[i]);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-05-06 00:13:35 +02:00
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
const cb = args[args.length - 1];
|
|
|
|
const fnPromise = entry(...fnArgs);
|
2021-09-05 01:09:30 +02:00
|
|
|
fnPromise
|
2021-09-24 23:56:30 +02:00
|
|
|
.then(function (res: any) {
|
2021-09-05 01:09:30 +02:00
|
|
|
cb(res);
|
|
|
|
})
|
2021-09-24 23:56:30 +02:00
|
|
|
.catch(function (err: any) {
|
2021-10-09 20:31:06 +02:00
|
|
|
// workerscript is when you cancel a delay
|
|
|
|
if (!(err instanceof WorkerScript)) console.error(err);
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
int.setProperty(scope, name, int.createAsyncFunction(tempWrapper));
|
|
|
|
} else if (
|
|
|
|
name === "sprintf" ||
|
|
|
|
name === "vsprintf" ||
|
|
|
|
name === "scp" ||
|
|
|
|
name == "write" ||
|
|
|
|
name === "tryWrite" ||
|
|
|
|
name === "run" ||
|
|
|
|
name === "exec"
|
|
|
|
) {
|
2021-09-25 08:36:49 +02:00
|
|
|
const tempWrapper = function (...args: any[]): void {
|
2021-09-25 07:26:03 +02:00
|
|
|
const fnArgs = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
//All of the Object/array elements are in JSInterpreter format, so
|
|
|
|
//we have to convert them back to native format to pass them to these fns
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < args.length; ++i) {
|
|
|
|
if (typeof args[i] === "object" || args[i].constructor === Array) {
|
|
|
|
fnArgs.push(int.pseudoToNative(args[i]));
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
2021-09-25 08:36:49 +02:00
|
|
|
fnArgs.push(args[i]);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-10-23 20:55:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
return entry(...fnArgs);
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
|
|
|
int.setProperty(scope, name, int.createNativeFunction(tempWrapper));
|
|
|
|
} else {
|
2021-09-25 08:36:49 +02:00
|
|
|
const tempWrapper = function (...args: any[]): any {
|
|
|
|
const res = entry(...args);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
if (res == null) {
|
|
|
|
return res;
|
|
|
|
} else if (res.constructor === Array || res === Object(res)) {
|
|
|
|
//Objects and Arrays must be converted to the interpreter's format
|
|
|
|
return int.nativeToPseudo(res);
|
2018-05-06 00:13:35 +02:00
|
|
|
} else {
|
2021-09-05 01:09:30 +02:00
|
|
|
return res;
|
2018-05-06 00:13:35 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
|
|
|
int.setProperty(scope, name, int.createNativeFunction(tempWrapper));
|
2018-05-06 00:13:35 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
//bladeburner, or anything else
|
|
|
|
int.setProperty(scope, name, int.nativeToPseudo(entry));
|
|
|
|
}
|
2021-04-30 05:52:56 +02:00
|
|
|
}
|
2018-05-09 03:40:07 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
//Add the arguments
|
|
|
|
int.setProperty(scope, "args", int.nativeToPseudo(workerScript.args));
|
|
|
|
};
|
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
let interpreter: any;
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-09 05:47:34 +02:00
|
|
|
interpreter = new Interpreter(codeWithImports, interpreterInitialization, codeLineOffset);
|
2021-10-13 08:27:55 +02:00
|
|
|
} catch (e: any) {
|
2021-09-05 01:09:30 +02:00
|
|
|
dialogBoxCreate("Syntax ERROR in " + workerScript.name + ":<br>" + e);
|
|
|
|
workerScript.env.stopFlag = true;
|
|
|
|
workerScript.running = false;
|
|
|
|
killWorkerScript(workerScript);
|
2021-09-24 23:56:30 +02:00
|
|
|
return Promise.resolve(workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2021-09-25 08:36:49 +02:00
|
|
|
function runInterpreter(): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
|
|
|
if (workerScript.env.stopFlag) {
|
|
|
|
return reject(workerScript);
|
2018-05-06 00:13:35 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 19:32:21 +01:00
|
|
|
let more = true;
|
|
|
|
let i = 0;
|
|
|
|
while (i < 3 && more) {
|
|
|
|
more = more && interpreter.step();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (more) {
|
2021-10-05 01:58:34 +02:00
|
|
|
setTimeout(runInterpreter, Settings.CodeInstructionRunTime);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
resolve(workerScript);
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
2021-10-13 08:27:55 +02:00
|
|
|
} catch (e: any) {
|
2021-09-05 01:09:30 +02:00
|
|
|
e = e.toString();
|
|
|
|
if (!isScriptErrorMessage(e)) {
|
|
|
|
e = makeRuntimeRejectMsg(workerScript, e);
|
|
|
|
}
|
|
|
|
workerScript.errorMessage = e;
|
|
|
|
return reject(workerScript);
|
|
|
|
}
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
2018-07-11 06:41:26 +02:00
|
|
|
|
|
|
|
try {
|
2021-09-05 01:09:30 +02:00
|
|
|
runInterpreter();
|
2021-10-13 08:27:55 +02:00
|
|
|
} catch (e: any) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (isString(e)) {
|
|
|
|
workerScript.errorMessage = e;
|
|
|
|
return reject(workerScript);
|
|
|
|
} else if (e instanceof WorkerScript) {
|
|
|
|
return reject(e);
|
|
|
|
} else {
|
|
|
|
return reject(workerScript);
|
|
|
|
}
|
2018-07-11 06:41:26 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
2018-07-20 16:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Since the JS Interpreter used for Netscript 1.0 only supports ES5, the keyword
|
|
|
|
'import' throws an error. However, since we want to support import funtionality
|
|
|
|
we'll implement it ourselves by parsing the Nodes in the AST out.
|
|
|
|
|
|
|
|
@param code - The script's code
|
2018-07-25 21:53:54 +02:00
|
|
|
@returns {Object} {
|
|
|
|
code: Newly-generated code with imported functions
|
|
|
|
lineOffset: Net number of lines of code added/removed due to imported functions
|
|
|
|
Should typically be positive
|
|
|
|
}
|
2018-07-20 16:28:03 +02:00
|
|
|
*/
|
2021-09-24 23:56:30 +02:00
|
|
|
function processNetscript1Imports(code: string, workerScript: WorkerScript): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
//allowReserved prevents 'import' from throwing error in ES5
|
2021-09-24 23:56:30 +02:00
|
|
|
const ast: any = parse(code, {
|
2021-09-05 01:09:30 +02:00
|
|
|
ecmaVersion: 9,
|
|
|
|
allowReserved: true,
|
|
|
|
sourceType: "module",
|
|
|
|
});
|
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const server = workerScript.getServer();
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw new Error("Failed to find underlying Server object for script");
|
|
|
|
}
|
|
|
|
|
2021-09-24 23:56:30 +02:00
|
|
|
function getScript(scriptName: string): Script | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
|
|
|
if (server.scripts[i].filename === scriptName) {
|
|
|
|
return server.scripts[i];
|
|
|
|
}
|
2018-07-20 16:28:03 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let generatedCode = ""; // Generated Javascript Code
|
|
|
|
let hasImports = false;
|
|
|
|
|
|
|
|
// Walk over the tree and process ImportDeclaration nodes
|
|
|
|
walksimple(ast, {
|
2021-09-24 23:56:30 +02:00
|
|
|
ImportDeclaration: (node: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
hasImports = true;
|
|
|
|
let scriptName = node.source.value;
|
|
|
|
if (scriptName.startsWith("./")) {
|
|
|
|
scriptName = scriptName.slice(2);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const script = getScript(scriptName);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (script == null) {
|
|
|
|
throw new Error("'Import' failed due to invalid script: " + scriptName);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const scriptAst = parse(script.code, {
|
2021-09-05 01:09:30 +02:00
|
|
|
ecmaVersion: 9,
|
|
|
|
allowReserved: true,
|
|
|
|
sourceType: "module",
|
|
|
|
});
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
if (node.specifiers.length === 1 && node.specifiers[0].type === "ImportNamespaceSpecifier") {
|
2021-09-05 01:09:30 +02:00
|
|
|
// import * as namespace from script
|
2021-09-25 07:26:03 +02:00
|
|
|
const namespace = node.specifiers[0].local.name;
|
|
|
|
const fnNames: string[] = []; //Names only
|
|
|
|
const fnDeclarations: any[] = []; //FunctionDeclaration Node objects
|
2021-09-05 01:09:30 +02:00
|
|
|
walksimple(scriptAst, {
|
2021-09-24 23:56:30 +02:00
|
|
|
FunctionDeclaration: (node: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
fnNames.push(node.id.name);
|
|
|
|
fnDeclarations.push(node);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//Now we have to generate the code that would create the namespace
|
2021-09-09 05:47:34 +02:00
|
|
|
generatedCode += "var " + namespace + ";\n" + "(function (namespace) {\n";
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
//Add the function declarations
|
2021-09-24 23:56:30 +02:00
|
|
|
fnDeclarations.forEach((fn: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
generatedCode += generate(fn);
|
|
|
|
generatedCode += "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add functions to namespace
|
|
|
|
fnNames.forEach((fnName) => {
|
|
|
|
generatedCode += "namespace." + fnName + " = " + fnName;
|
|
|
|
generatedCode += "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
//Finish
|
2021-09-09 05:47:34 +02:00
|
|
|
generatedCode += "})(" + namespace + " || " + "(" + namespace + " = {}));\n";
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
//import {...} from script
|
|
|
|
|
|
|
|
//Get array of all fns to import
|
2021-09-25 07:26:03 +02:00
|
|
|
const fnsToImport: string[] = [];
|
2021-09-24 23:56:30 +02:00
|
|
|
node.specifiers.forEach((e: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
fnsToImport.push(e.local.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
//Walk through script and get FunctionDeclaration code for all specified fns
|
2021-09-25 07:26:03 +02:00
|
|
|
const fnDeclarations: any[] = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
walksimple(scriptAst, {
|
2021-09-24 23:56:30 +02:00
|
|
|
FunctionDeclaration: (node: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (fnsToImport.includes(node.id.name)) {
|
|
|
|
fnDeclarations.push(node);
|
2018-07-20 16:28:03 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//Convert FunctionDeclarations into code
|
2021-09-24 23:56:30 +02:00
|
|
|
fnDeclarations.forEach((fn: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
generatedCode += generate(fn);
|
|
|
|
generatedCode += "\n";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//If there are no imports, just return the original code
|
|
|
|
if (!hasImports) {
|
|
|
|
return { code: code, lineOffset: 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove ImportDeclarations from AST. These ImportDeclarations must be in top-level
|
2021-09-25 07:26:03 +02:00
|
|
|
let linesRemoved = 0;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ast.type !== "Program" || ast.body == null) {
|
|
|
|
throw new Error("Code could not be properly parsed");
|
|
|
|
}
|
|
|
|
for (let i = ast.body.length - 1; i >= 0; --i) {
|
|
|
|
if (ast.body[i].type === "ImportDeclaration") {
|
|
|
|
ast.body.splice(i, 1);
|
|
|
|
++linesRemoved;
|
2018-07-20 16:28:03 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-07-09 01:53:24 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
//Calculated line offset
|
2021-09-25 07:26:03 +02:00
|
|
|
const lineOffset = (generatedCode.match(/\n/g) || []).length - linesRemoved;
|
2018-07-25 21:53:54 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
//Convert the AST back into code
|
|
|
|
code = generate(ast);
|
2018-07-09 01:53:24 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
//Add the imported code and re-generate in ES5 (JS Interpreter for NS1 only supports ES5);
|
|
|
|
code = generatedCode + code;
|
2019-05-12 04:20:20 +02:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const res = {
|
2021-09-05 01:09:30 +02:00
|
|
|
code: code,
|
|
|
|
lineOffset: lineOffset,
|
|
|
|
};
|
|
|
|
return res;
|
2018-07-09 01:53:24 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
/**
|
2019-07-14 05:55:58 +02:00
|
|
|
* Used to start a RunningScript (by creating and starting its
|
|
|
|
* corresponding WorkerScript), and add the RunningScript to the server on which
|
|
|
|
* it is active
|
|
|
|
* @param {RunningScript} runningScriptObj - Script that's being run
|
|
|
|
* @param {Server} server - Server on which the script is to be run
|
|
|
|
* @returns {number} pid of started script
|
|
|
|
*/
|
2021-09-24 23:56:30 +02:00
|
|
|
export function startWorkerScript(runningScript: RunningScript, server: BaseServer, parent?: WorkerScript): number {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (createAndAddWorkerScript(runningScript, server, parent)) {
|
|
|
|
// Push onto runningScripts.
|
|
|
|
// This has to come after createAndAddWorkerScript() because that fn updates RAM usage
|
2021-09-24 23:56:30 +02:00
|
|
|
server.runScript(runningScript);
|
2019-07-14 05:55:58 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Once the WorkerScript is constructed in createAndAddWorkerScript(), the RunningScript
|
|
|
|
// object should have a PID assigned to it, so we return that
|
|
|
|
return runningScript.pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-07-14 05:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a RunningScript object, constructs its corresponding WorkerScript,
|
2019-05-17 08:44:59 +02:00
|
|
|
* adds it to the global 'workerScripts' pool, and begins executing it.
|
|
|
|
* @param {RunningScript} runningScriptObj - Script that's being run
|
|
|
|
* @param {Server} server - Server on which the script is to be run
|
2019-07-14 05:55:58 +02:00
|
|
|
* returns {boolean} indicating whether or not the workerScript was successfully added
|
2019-05-05 06:03:40 +02:00
|
|
|
*/
|
2021-10-15 00:45:50 +02:00
|
|
|
function createAndAddWorkerScript(runningScriptObj: RunningScript, server: BaseServer, parent?: WorkerScript): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Update server's ram usage
|
|
|
|
let threads = 1;
|
|
|
|
if (runningScriptObj.threads && !isNaN(runningScriptObj.threads)) {
|
|
|
|
threads = runningScriptObj.threads;
|
|
|
|
} else {
|
|
|
|
runningScriptObj.threads = 1;
|
|
|
|
}
|
2021-10-28 00:55:06 +02:00
|
|
|
const oneRamUsage = getRamUsageFromRunningScript(runningScriptObj);
|
|
|
|
const ramUsage = roundToTwo(oneRamUsage * threads);
|
2021-09-05 01:09:30 +02:00
|
|
|
const ramAvailable = server.maxRam - server.ramUsed;
|
|
|
|
if (ramUsage > ramAvailable) {
|
|
|
|
dialogBoxCreate(
|
|
|
|
`Not enough RAM to run script ${runningScriptObj.filename} with args ` +
|
2021-09-09 05:47:34 +02:00
|
|
|
`${arrayToString(runningScriptObj.args)}. This likely occurred because you re-loaded ` +
|
2021-09-05 01:09:30 +02:00
|
|
|
`the game and the script's RAM usage increased (either because of an update to the game or ` +
|
|
|
|
`your changes to the script.)`,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-16 05:49:33 +01:00
|
|
|
|
|
|
|
server.updateRamUsed(roundToTwo(server.ramUsed + ramUsage), Player);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Get the pid
|
|
|
|
const pid = generateNextPid();
|
|
|
|
if (pid === -1) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to start script because could not find available PID. This is most ` +
|
|
|
|
`because you have too many scripts running.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the WorkerScript. NOTE: WorkerScript ctor will set the underlying
|
|
|
|
// RunningScript's PID as well
|
|
|
|
const s = new WorkerScript(runningScriptObj, pid, NetscriptFunctions);
|
2021-10-28 00:55:06 +02:00
|
|
|
s.ramUsage = oneRamUsage;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Add the WorkerScript to the global pool
|
|
|
|
workerScripts.set(pid, s);
|
2021-09-18 21:44:39 +02:00
|
|
|
WorkerScriptStartStopEventEmitter.emit();
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Start the script's execution
|
2021-09-24 23:56:30 +02:00
|
|
|
let p: Promise<WorkerScript> | null = null; // Script's resulting promise
|
2021-09-05 01:09:30 +02:00
|
|
|
if (s.name.endsWith(".js") || s.name.endsWith(".ns")) {
|
|
|
|
p = startNetscript2Script(s);
|
|
|
|
} else {
|
|
|
|
p = startNetscript1Script(s);
|
|
|
|
if (!(p instanceof Promise)) {
|
|
|
|
return false;
|
2017-09-29 17:02:33 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once the code finishes (either resolved or rejected, doesnt matter), set its
|
|
|
|
// running status to false
|
2021-09-25 01:09:19 +02:00
|
|
|
p.then(function (w: WorkerScript) {
|
2021-09-05 01:09:30 +02:00
|
|
|
// On natural death, the earnings are transfered to the parent if it still exists.
|
2021-09-24 23:56:30 +02:00
|
|
|
if (parent !== undefined) {
|
|
|
|
if (parent.running) {
|
|
|
|
parent.scriptRef.onlineExpGained += runningScriptObj.onlineExpGained;
|
|
|
|
parent.scriptRef.onlineMoneyMade += runningScriptObj.onlineMoneyMade;
|
|
|
|
}
|
2019-06-19 10:03:08 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
killWorkerScript(s);
|
|
|
|
w.log("", "Script finished running");
|
|
|
|
}).catch(function (w) {
|
|
|
|
if (w instanceof Error) {
|
2021-09-09 05:47:34 +02:00
|
|
|
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
|
|
|
|
console.error("Evaluating workerscript returns an Error. THIS SHOULDN'T HAPPEN: " + w.toString());
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
|
|
|
} else if (w instanceof WorkerScript) {
|
|
|
|
if (isScriptErrorMessage(w.errorMessage)) {
|
2021-10-22 21:21:10 +02:00
|
|
|
const errorTextArray = w.errorMessage.split("|DELIMITER|");
|
2021-09-05 01:09:30 +02:00
|
|
|
if (errorTextArray.length != 4) {
|
2021-09-09 05:47:34 +02:00
|
|
|
console.error("ERROR: Something wrong with Error text in evaluator...");
|
2021-09-24 23:56:30 +02:00
|
|
|
console.error("Error text: " + w.errorMessage);
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
2021-05-08 03:59:19 +02:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
const hostname = errorTextArray[1];
|
2021-09-05 01:09:30 +02:00
|
|
|
const scriptName = errorTextArray[2];
|
|
|
|
const errorMsg = errorTextArray[3];
|
2021-05-08 03:59:19 +02:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
let msg = `RUNTIME ERROR<br>${scriptName}@${hostname}<br>`;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (w.args.length > 0) {
|
|
|
|
msg += `Args: ${arrayToString(w.args)}<br>`;
|
2019-05-17 08:44:59 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
msg += "<br>";
|
|
|
|
msg += errorMsg;
|
|
|
|
|
|
|
|
dialogBoxCreate(msg);
|
|
|
|
w.log("", "Script crashed with runtime error");
|
|
|
|
} else {
|
|
|
|
w.log("", "Script killed");
|
|
|
|
return; // Already killed, so stop here
|
|
|
|
}
|
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
|
|
|
} else if (isScriptErrorMessage(w)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
|
2021-09-05 01:09:30 +02:00
|
|
|
console.error(
|
|
|
|
"ERROR: Evaluating workerscript returns only error message rather than WorkerScript object. THIS SHOULDN'T HAPPEN: " +
|
|
|
|
w.toString(),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
dialogBoxCreate("An unknown script died for an unknown reason. This is a bug please contact game dev");
|
2021-09-05 01:09:30 +02:00
|
|
|
console.error(w);
|
|
|
|
}
|
2019-05-17 08:44:59 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
killWorkerScript(s);
|
|
|
|
});
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
/**
|
|
|
|
* Updates the online running time stat of all running scripts
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
export function updateOnlineScriptTimes(numCycles = 1): void {
|
2021-09-25 07:26:03 +02:00
|
|
|
const time = (numCycles * CONSTANTS._idleSpeed) / 1000; //seconds
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const ws of workerScripts.values()) {
|
|
|
|
ws.scriptRef.onlineRunningTime += time;
|
|
|
|
}
|
2016-12-19 19:20:19 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
/**
|
|
|
|
* Called when the game is loaded. Loads all running scripts (from all servers)
|
|
|
|
* into worker scripts so that they will start running
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
export function loadAllRunningScripts(): void {
|
2021-09-25 07:26:03 +02:00
|
|
|
const skipScriptLoad = window.location.href.toLowerCase().indexOf("?noscripts") !== -1;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (skipScriptLoad) {
|
|
|
|
console.info("Skipping the load of any scripts during startup");
|
|
|
|
}
|
2021-10-07 22:04:04 +02:00
|
|
|
for (const server of GetAllServers()) {
|
|
|
|
// Reset each server's RAM usage to 0
|
|
|
|
server.ramUsed = 0;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2021-10-07 22:04:04 +02:00
|
|
|
// Reset modules on all scripts
|
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
|
|
|
server.scripts[i].markUpdated();
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2021-10-07 22:04:04 +02:00
|
|
|
if (skipScriptLoad) {
|
|
|
|
// Start game with no scripts
|
|
|
|
server.runningScripts.length = 0;
|
|
|
|
} else {
|
|
|
|
for (let j = 0; j < server.runningScripts.length; ++j) {
|
|
|
|
createAndAddWorkerScript(server.runningScripts[j], server);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2021-10-07 22:04:04 +02:00
|
|
|
// Offline production
|
|
|
|
scriptCalculateOfflineProduction(server.runningScripts[j]);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a script from inside another script (run(), exec(), spawn(), etc.)
|
|
|
|
*/
|
2021-09-24 23:56:30 +02:00
|
|
|
export function runScriptFromScript(
|
|
|
|
caller: string,
|
|
|
|
server: BaseServer,
|
|
|
|
scriptname: string,
|
|
|
|
args: any[],
|
|
|
|
workerScript: WorkerScript,
|
|
|
|
threads = 1,
|
|
|
|
): number {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Sanitize arguments
|
|
|
|
if (!(workerScript instanceof WorkerScript)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof scriptname !== "string" || !Array.isArray(args)) {
|
2021-09-24 23:56:30 +02:00
|
|
|
workerScript.log(caller, `Invalid arguments: scriptname='${scriptname} args='${args}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
console.error(`runScriptFromScript() failed due to invalid arguments`);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:29:43 +02:00
|
|
|
args.forEach((arg) => {
|
|
|
|
if (typeof arg !== "string" && typeof arg !== "number")
|
|
|
|
throw new Error("Only strings and numbers can be passed as arguments to otherscripts.");
|
2021-10-16 01:13:05 +02:00
|
|
|
});
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Check if the script is already running
|
2021-09-25 07:26:03 +02:00
|
|
|
const runningScriptObj = server.getRunningScript(scriptname, args);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (runningScriptObj != null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log(caller, `'${scriptname}' is already running on '${server.hostname}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'null/undefined' arguments are not allowed
|
|
|
|
for (let i = 0; i < args.length; ++i) {
|
|
|
|
if (args[i] == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log(caller, "Cannot execute a script with null/undefined as an argument");
|
2021-09-05 01:09:30 +02:00
|
|
|
return 0;
|
2019-07-12 04:37:17 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the script exists and if it does run it
|
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
2021-11-06 02:01:23 +01:00
|
|
|
if (!areFilesEqual(server.scripts[i].filename, scriptname)) continue;
|
|
|
|
// Check for admin rights and that there is enough RAM availble to run
|
|
|
|
const script = server.scripts[i];
|
|
|
|
let ramUsage = script.ramUsage;
|
|
|
|
threads = Math.round(Number(threads));
|
|
|
|
if (threads === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ramUsage = ramUsage * threads;
|
|
|
|
const ramAvailable = server.maxRam - server.ramUsed;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2021-11-06 02:01:23 +01:00
|
|
|
if (server.hasAdminRights == false) {
|
|
|
|
workerScript.log(caller, `You do not have root access on '${server.hostname}'`);
|
|
|
|
return 0;
|
|
|
|
} else if (ramUsage > ramAvailable) {
|
|
|
|
workerScript.log(
|
|
|
|
caller,
|
|
|
|
`Cannot run script '${scriptname}' (t=${threads}) on '${server.hostname}' because there is not enough available RAM!`,
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// Able to run script
|
|
|
|
workerScript.log(
|
|
|
|
caller,
|
|
|
|
`'${scriptname}' on '${server.hostname}' with ${threads} threads and args: ${arrayToString(args)}.`,
|
|
|
|
);
|
|
|
|
const runningScriptObj = new RunningScript(script, args);
|
|
|
|
runningScriptObj.threads = threads;
|
|
|
|
|
|
|
|
return startWorkerScript(runningScriptObj, server, workerScript);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2021-11-06 02:01:23 +01:00
|
|
|
break;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-07-14 05:55:58 +02:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log(caller, `Could not find script '${scriptname}' on '${server.hostname}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return 0;
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|