2017-04-13 19:33:34 +02:00
|
|
|
/* Worker code, contains Netscript scripts that are actually running */
|
2016-11-28 23:02:06 +01:00
|
|
|
|
2016-11-30 23:08:21 +01:00
|
|
|
//TODO Tested For and while and generic call statements. Have not tested if statements
|
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
/* Actual Worker Code */
|
2017-06-17 04:53:57 +02:00
|
|
|
function WorkerScript(runningScriptObj) {
|
|
|
|
this.name = runningScriptObj.filename;
|
2016-11-30 00:38:50 +01:00
|
|
|
this.running = false;
|
2016-12-02 22:57:20 +01:00
|
|
|
this.serverIp = null;
|
2017-06-17 04:53:57 +02:00
|
|
|
this.code = runningScriptObj.scriptRef.code;
|
2017-06-28 11:47:42 +02:00
|
|
|
this.env = new Environment(this);
|
|
|
|
this.env.set("args", runningScriptObj.args);
|
2016-12-02 22:57:20 +01:00
|
|
|
this.output = "";
|
2016-12-19 19:20:19 +01:00
|
|
|
this.ramUsage = 0;
|
2017-06-17 04:53:57 +02:00
|
|
|
this.scriptRef = runningScriptObj;
|
2017-05-01 07:39:48 +02:00
|
|
|
this.errorMessage = "";
|
2017-06-17 04:53:57 +02:00
|
|
|
this.args = runningScriptObj.args;
|
2016-12-19 19:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Returns the server on which the workerScript is running
|
|
|
|
WorkerScript.prototype.getServer = function() {
|
|
|
|
return AllServers[this.serverIp];
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
//Array containing all scripts that are running across all servers, to easily run them all
|
2016-11-28 23:02:06 +01:00
|
|
|
var workerScripts = [];
|
|
|
|
|
|
|
|
//Loop through workerScripts and run every script that is not currently running
|
|
|
|
function runScriptsLoop() {
|
2016-12-05 23:31:46 +01:00
|
|
|
//Run any scripts that haven't been started
|
2016-11-28 23:02:06 +01:00
|
|
|
for (var i = 0; i < workerScripts.length; i++) {
|
2016-12-05 23:31:46 +01:00
|
|
|
//If it isn't running, start the script
|
2016-12-06 17:59:20 +01:00
|
|
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
|
2016-12-15 23:22:42 +01:00
|
|
|
try {
|
2017-06-28 11:47:42 +02:00
|
|
|
var ast = acorn.parse(workerScripts[i].code);
|
2017-06-17 04:53:57 +02:00
|
|
|
//console.log(ast);
|
2016-12-15 23:22:42 +01:00
|
|
|
} catch (e) {
|
2017-06-02 21:46:14 +02:00
|
|
|
console.log("Error parsing script: " + workerScripts[i].name);
|
2017-06-01 15:36:49 +02:00
|
|
|
dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":<br>" + e);
|
2017-04-13 21:36:03 +02:00
|
|
|
workerScripts[i].env.stopFlag = true;
|
2016-12-19 19:20:19 +01:00
|
|
|
continue;
|
2016-12-15 23:22:42 +01:00
|
|
|
}
|
2017-04-23 04:32:51 +02:00
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
workerScripts[i].running = true;
|
2016-12-05 23:31:46 +01:00
|
|
|
var p = evaluate(ast, workerScripts[i]);
|
|
|
|
//Once the code finishes (either resolved or rejected, doesnt matter), set its
|
|
|
|
//running status to false
|
2016-12-06 17:59:20 +01:00
|
|
|
p.then(function(w) {
|
2016-12-14 21:29:40 +01:00
|
|
|
console.log("Stopping script " + w.name + " because it finished running naturally");
|
2016-12-06 17:59:20 +01:00
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
2017-06-17 04:53:57 +02:00
|
|
|
w.scriptRef.log("Script finished running");
|
2016-12-06 17:59:20 +01:00
|
|
|
}, function(w) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (w instanceof Error) {
|
2017-06-17 04:53:57 +02:00
|
|
|
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
|
|
|
|
console.log("ERROR: Evaluating workerscript returns an Error. THIS SHOULDN'T HAPPEN");
|
|
|
|
return;
|
2017-05-01 07:39:48 +02:00
|
|
|
} else if (w instanceof WorkerScript) {
|
|
|
|
if (isScriptErrorMessage(w.errorMessage)) {
|
|
|
|
var errorTextArray = w.errorMessage.split("|");
|
|
|
|
if (errorTextArray.length != 4) {
|
|
|
|
console.log("ERROR: Something wrong with Error text in evaluator...");
|
|
|
|
console.log("Error text: " + errorText);
|
2017-05-31 02:00:24 +02:00
|
|
|
return;
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|
|
|
|
var serverIp = errorTextArray[1];
|
|
|
|
var scriptName = errorTextArray[2];
|
|
|
|
var errorMsg = errorTextArray[3];
|
2016-12-19 19:20:19 +01:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
dialogBoxCreate("Script runtime error: <br>Server Ip: " + serverIp +
|
|
|
|
"<br>Script name: " + scriptName +
|
|
|
|
"<br>Args:" + printArray(w.args) + "<br>" + errorMsg);
|
|
|
|
w.scriptRef.log("Script crashed with runtime error");
|
|
|
|
} else {
|
|
|
|
w.scriptRef.log("Script killed");
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|
2016-12-15 23:22:42 +01:00
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
2017-06-17 04:53:57 +02:00
|
|
|
|
2017-05-31 02:00:24 +02:00
|
|
|
} else if (isScriptErrorMessage(w)) {
|
2017-06-17 04:53:57 +02:00
|
|
|
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
|
|
|
|
console.log("ERROR: Evaluating workerscript returns only error message rather than WorkerScript object. THIS SHOULDN'T HAPPEN");
|
|
|
|
return;
|
2017-06-07 06:09:53 +02:00
|
|
|
} else {
|
|
|
|
dialogBoxCreate("An unknown script died for an unknown reason. This is a bug please contact game dev");
|
2017-05-31 02:00:24 +02:00
|
|
|
}
|
2016-12-05 23:31:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete any scripts that finished or have been killed. Loop backwards bc removing
|
|
|
|
//items fucks up the indexing
|
|
|
|
for (var i = workerScripts.length - 1; i >= 0; i--) {
|
|
|
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == true) {
|
2016-12-14 00:52:32 +01:00
|
|
|
console.log("Deleting script: " + workerScripts[i].name);
|
2016-12-19 19:20:19 +01:00
|
|
|
//Delete script from the runningScripts array on its host serverIp
|
2016-12-05 23:31:46 +01:00
|
|
|
var ip = workerScripts[i].serverIp;
|
|
|
|
var name = workerScripts[i].name;
|
|
|
|
for (var j = 0; j < AllServers[ip].runningScripts.length; j++) {
|
2017-06-17 04:53:57 +02:00
|
|
|
if (AllServers[ip].runningScripts[j].filename == name &&
|
|
|
|
compareArrays(AllServers[ip].runningScripts[j].args, workerScripts[i].args)) {
|
2016-12-06 17:59:20 +01:00
|
|
|
AllServers[ip].runningScripts.splice(j, 1);
|
2016-12-05 23:31:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 07:34:45 +02:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Free RAM
|
|
|
|
AllServers[ip].ramUsed -= workerScripts[i].ramUsage;
|
2017-06-06 23:22:57 +02:00
|
|
|
|
|
|
|
//Delete script from Active Scripts
|
|
|
|
deleteActiveScriptsItem(workerScripts[i]);
|
2016-12-05 23:31:46 +01:00
|
|
|
|
|
|
|
//Delete script from workerScripts
|
|
|
|
workerScripts.splice(i, 1);
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(runScriptsLoop, 10000);
|
|
|
|
}
|
|
|
|
|
2016-12-06 19:09:23 +01:00
|
|
|
//Queues a script to be killed by settings its stop flag to true. Then, the code will reject
|
|
|
|
//all of its promises recursively, and when it does so it will no longer be running.
|
|
|
|
//The runScriptsLoop() will then delete the script from worker scripts
|
2017-06-17 04:53:57 +02:00
|
|
|
function killWorkerScript(runningScriptObj, serverIp) {
|
2016-12-06 19:09:23 +01:00
|
|
|
for (var i = 0; i < workerScripts.length; i++) {
|
2017-06-17 04:53:57 +02:00
|
|
|
if (workerScripts[i].name == runningScriptObj.filename && workerScripts[i].serverIp == serverIp &&
|
|
|
|
compareArrays(workerScripts[i].args, runningScriptObj.args)) {
|
2016-12-06 19:09:23 +01:00
|
|
|
workerScripts[i].env.stopFlag = true;
|
2017-06-07 06:09:53 +02:00
|
|
|
return true;
|
2016-12-06 19:09:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 06:09:53 +02:00
|
|
|
return false;
|
2016-12-06 19:09:23 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 21:29:40 +01:00
|
|
|
//Queues a script to be run
|
2017-06-17 04:53:57 +02:00
|
|
|
function addWorkerScript(runningScriptObj, server) {
|
|
|
|
var filename = runningScriptObj.filename;
|
2016-12-14 21:29:40 +01:00
|
|
|
|
|
|
|
//Update server's ram usage
|
2017-06-11 23:07:38 +02:00
|
|
|
var threads = 1;
|
2017-06-17 04:53:57 +02:00
|
|
|
if (runningScriptObj.threads && !isNaN(runningScriptObj.threads)) {
|
|
|
|
threads = runningScriptObj.threads;
|
2017-06-11 23:07:38 +02:00
|
|
|
} else {
|
2017-06-17 05:32:58 +02:00
|
|
|
runningScriptObj.threads = 1;
|
2017-06-11 23:07:38 +02:00
|
|
|
}
|
2017-06-20 18:11:33 +02:00
|
|
|
var ramUsage = runningScriptObj.scriptRef.ramUsage * threads
|
|
|
|
* Math.pow(CONSTANTS.MultithreadingRAMCost, threads-1);
|
2017-06-11 03:46:02 +02:00
|
|
|
server.ramUsed += ramUsage;
|
2016-12-14 21:29:40 +01:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Create the WorkerScript
|
2017-06-17 04:53:57 +02:00
|
|
|
var s = new WorkerScript(runningScriptObj);
|
2016-12-14 21:29:40 +01:00
|
|
|
s.serverIp = server.ip;
|
2017-06-11 03:46:02 +02:00
|
|
|
s.ramUsage = ramUsage;
|
2016-12-19 19:20:19 +01:00
|
|
|
|
|
|
|
//Add the WorkerScript to the Active Scripts list
|
2017-06-06 23:22:57 +02:00
|
|
|
addActiveScriptsItem(s);
|
2016-12-19 19:20:19 +01:00
|
|
|
|
|
|
|
//Add the WorkerScript
|
2016-12-14 21:29:40 +01:00
|
|
|
workerScripts.push(s);
|
2016-12-15 18:51:23 +01:00
|
|
|
return;
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Updates the online running time stat of all running scripts
|
|
|
|
function updateOnlineScriptTimes(numCycles = 1) {
|
|
|
|
var time = (numCycles * Engine._idleSpeed) / 1000; //seconds
|
|
|
|
for (var i = 0; i < workerScripts.length; ++i) {
|
|
|
|
workerScripts[i].scriptRef.onlineRunningTime += time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
runScriptsLoop();
|