2021-09-25 20:42:57 +02:00
|
|
|
import { isString } from "./utils/helpers/isString";
|
2021-10-07 22:56:01 +02:00
|
|
|
import { GetServer } from "./Server/AllServers";
|
2022-02-06 01:15:42 +01:00
|
|
|
import { ScriptDeath } from "./Netscript/ScriptDeath";
|
2021-09-24 23:07:53 +02:00
|
|
|
import { WorkerScript } from "./Netscript/WorkerScript";
|
2022-05-25 02:16:39 +02:00
|
|
|
import { NetscriptContext } from "./Netscript/APIWrapper";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-24 23:07:53 +02:00
|
|
|
export function netscriptDelay(time: number, workerScript: WorkerScript): Promise<void> {
|
2022-02-13 20:53:23 +01:00
|
|
|
// Cancel any pre-existing netscriptDelay'ed function call
|
|
|
|
// TODO: the rejection almost certainly ends up in the uncaught rejection handler.
|
|
|
|
// Maybe reject with a stack-trace'd error message?
|
|
|
|
if (workerScript.delayReject) workerScript.delayReject();
|
2022-02-06 01:15:42 +01:00
|
|
|
|
2022-01-06 01:56:10 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
2021-10-05 01:58:34 +02:00
|
|
|
workerScript.delay = window.setTimeout(() => {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.delay = null;
|
2022-01-06 01:56:10 +01:00
|
|
|
workerScript.delayReject = undefined;
|
2022-02-06 01:15:42 +01:00
|
|
|
if (workerScript.env.stopFlag) reject(new ScriptDeath(workerScript));
|
2022-01-18 20:02:12 +01:00
|
|
|
else resolve();
|
2021-09-05 01:09:30 +02:00
|
|
|
}, time);
|
2022-01-06 01:56:10 +01:00
|
|
|
workerScript.delayReject = reject;
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
2017-06-05 06:48:37 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
export function makeRuntimeRejectMsg(workerScript: WorkerScript, msg: string): string {
|
2021-10-07 23:55:49 +02:00
|
|
|
const server = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-10-07 23:55:49 +02:00
|
|
|
throw new Error(`WorkerScript constructed with invalid server ip: ${workerScript.hostname}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-12-13 01:39:53 +01:00
|
|
|
for (const scriptUrl of workerScript.scriptRef.dependencies) {
|
2022-01-04 00:26:22 +01:00
|
|
|
// Return just the original msg if it's nullish so that we don't get a workerscript error
|
|
|
|
msg = msg?.replace(new RegExp(scriptUrl.url, "g"), scriptUrl.filename) ?? msg;
|
2021-12-12 20:49:02 +01:00
|
|
|
}
|
|
|
|
|
2021-10-22 21:21:10 +02:00
|
|
|
return "|DELIMITER|" + server.hostname + "|DELIMITER|" + workerScript.name + "|DELIMITER|" + msg;
|
2017-05-15 08:35:09 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 02:16:39 +02:00
|
|
|
export function resolveNetscriptRequestedThreads(ctx: NetscriptContext, requestedThreads: number): number {
|
|
|
|
const threads = ctx.workerScript.scriptRef.threads;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!requestedThreads) {
|
|
|
|
return isNaN(threads) || threads < 1 ? 1 : threads;
|
|
|
|
}
|
|
|
|
const requestedThreadsAsInt = requestedThreads | 0;
|
|
|
|
if (isNaN(requestedThreads) || requestedThreadsAsInt < 1) {
|
|
|
|
throw makeRuntimeRejectMsg(
|
2022-05-25 02:16:39 +02:00
|
|
|
ctx.workerScript,
|
|
|
|
`Invalid thread count passed to ${ctx.function}: ${requestedThreads}. Threads must be a positive number.`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
}
|
2022-01-02 18:13:01 +01:00
|
|
|
if (requestedThreadsAsInt > threads) {
|
2021-09-05 01:09:30 +02:00
|
|
|
throw makeRuntimeRejectMsg(
|
2022-05-25 02:16:39 +02:00
|
|
|
ctx.workerScript,
|
|
|
|
`Too many threads requested by ${ctx.function}. Requested: ${requestedThreads}. Has: ${threads}.`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return requestedThreadsAsInt;
|
2019-05-03 10:01:43 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:07:53 +02:00
|
|
|
export function isScriptErrorMessage(msg: string): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!isString(msg)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-22 21:21:10 +02:00
|
|
|
const splitMsg = msg.split("|DELIMITER|");
|
2022-03-01 20:37:47 +01:00
|
|
|
return splitMsg.length == 4;
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|