2019-04-11 10:37:40 +02:00
|
|
|
import { setTimeoutRef } from "./utils/SetTimeoutRef";
|
2019-02-20 09:42:27 +01:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { isString } from "../utils/helpers/isString";
|
2021-03-11 09:02:05 +01:00
|
|
|
import { AllServers } from "./Server/AllServers";
|
2021-09-24 23:07:53 +02:00
|
|
|
import { WorkerScript } from "./Netscript/WorkerScript";
|
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> {
|
2021-09-05 01:09:30 +02:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
workerScript.delay = setTimeoutRef(() => {
|
|
|
|
workerScript.delay = null;
|
|
|
|
resolve();
|
|
|
|
}, time);
|
|
|
|
workerScript.delayResolve = resolve;
|
|
|
|
});
|
2017-06-05 06:48:37 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:07:53 +02:00
|
|
|
export function makeRuntimeRejectMsg(workerScript: WorkerScript, msg: string, exp: any = null) {
|
2021-09-25 07:26:03 +02:00
|
|
|
let lineNum = "";
|
2021-09-05 01:09:30 +02:00
|
|
|
if (exp != null) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const num = getErrorLineNumber(exp, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
lineNum = " (Line " + num + ")";
|
|
|
|
}
|
|
|
|
const server = AllServers[workerScript.serverIp];
|
|
|
|
if (server == null) {
|
2021-09-24 23:07:53 +02:00
|
|
|
throw new Error(`WorkerScript constructed with invalid server ip: ${workerScript.serverIp}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return "|" + server.hostname + "|" + workerScript.name + "|" + msg + lineNum;
|
2017-05-15 08:35:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:07:53 +02:00
|
|
|
export function resolveNetscriptRequestedThreads(
|
|
|
|
workerScript: WorkerScript,
|
|
|
|
functionName: string,
|
|
|
|
requestedThreads: number,
|
|
|
|
) {
|
2021-09-05 01:09:30 +02:00
|
|
|
const threads = workerScript.scriptRef.threads;
|
|
|
|
if (!requestedThreads) {
|
|
|
|
return isNaN(threads) || threads < 1 ? 1 : threads;
|
|
|
|
}
|
|
|
|
const requestedThreadsAsInt = requestedThreads | 0;
|
|
|
|
if (isNaN(requestedThreads) || requestedThreadsAsInt < 1) {
|
|
|
|
throw makeRuntimeRejectMsg(
|
|
|
|
workerScript,
|
|
|
|
`Invalid thread count passed to ${functionName}: ${requestedThreads}. Threads must be a positive number.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (requestedThreads > threads) {
|
|
|
|
throw makeRuntimeRejectMsg(
|
|
|
|
workerScript,
|
|
|
|
`Too many threads requested by ${functionName}. Requested: ${requestedThreads}. Has: ${threads}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return requestedThreadsAsInt;
|
2019-05-03 10:01:43 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 05:09:18 +02:00
|
|
|
function getErrorLineNumber(exp: any, workerScript: WorkerScript): number {
|
2021-09-24 23:07:53 +02:00
|
|
|
return -1;
|
|
|
|
// TODO wtf is codeCode?
|
2017-09-05 03:03:29 +02:00
|
|
|
|
2021-09-24 23:07:53 +02:00
|
|
|
// var code = workerScript.scriptRef.codeCode();
|
|
|
|
|
|
|
|
// //Split code up to the start of the node
|
|
|
|
// try {
|
|
|
|
// code = code.substring(0, exp.start);
|
|
|
|
// return (code.match(/\n/g) || []).length + 1;
|
|
|
|
// } catch (e) {
|
|
|
|
// return -1;
|
|
|
|
// }
|
2017-09-05 03:03:29 +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-09-25 07:26:03 +02:00
|
|
|
const splitMsg = msg.split("|");
|
2021-09-05 01:09:30 +02:00
|
|
|
if (splitMsg.length != 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|