Pid resets to 1 when installing or destroying a BitNode.

This commit is contained in:
Olivier Gagnon
2021-03-07 13:08:12 -05:00
parent f9dcefd6ea
commit bbe4f9c286
4 changed files with 49 additions and 36 deletions
+42
View File
@@ -0,0 +1,42 @@
import { workerScripts } from "./WorkerScripts";
let pidCounter = 1;
/**
* Find and return the next availble PID for a script
*/
export function generateNextPid(): number {
let tempCounter = pidCounter;
// Cap the number of search iterations at some arbitrary value to avoid
// infinite loops. We'll assume that players wont have 1mil+ running scripts
let found = false;
for (let i = 0; i < 1e6;) {
if (!workerScripts.has(tempCounter + i)) {
found = true;
tempCounter = tempCounter + i;
break;
}
if (i === Number.MAX_SAFE_INTEGER - 1) {
i = 1;
} else {
++i;
}
}
if (found) {
pidCounter = tempCounter + 1;
if (pidCounter >= Number.MAX_SAFE_INTEGER) {
pidCounter = 1;
}
return tempCounter;
} else {
return -1;
}
}
export function resetPidCounter(): void {
pidCounter = 1;
}