MISC: Clear recent scripts when installing augmentations (#2670)

This commit is contained in:
Mathekatze
2026-04-17 23:20:07 +02:00
committed by GitHub
parent f8ec7f4294
commit f5bbc26495
2 changed files with 26 additions and 24 deletions
+24 -24
View File
@@ -4,37 +4,37 @@ let pidCounter = 1;
/** Find and return the next available PID for a script */
export function generateNextPid(): number {
let tempCounter = pidCounter;
let pidCandidate = 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;
// infinite loops. We'll assume that players won't have a million running scripts.
for (let attemptCounter = 0; attemptCounter < 1e6; ++attemptCounter, ++pidCandidate) {
// ensure the candidate PID is a safe integer
if (pidCandidate >= Number.MAX_SAFE_INTEGER) {
pidCandidate = 1;
}
if (i === Number.MAX_SAFE_INTEGER - 1) {
i = 1;
} else {
++i;
// ensure the PID is not in use
if (workerScripts.has(pidCandidate)) {
continue;
}
// found a PID that's not in use
pidCounter = pidCandidate + 1;
return pidCandidate;
}
if (found) {
pidCounter = tempCounter + 1;
if (pidCounter >= Number.MAX_SAFE_INTEGER) {
pidCounter = 1;
}
return tempCounter;
} else {
return -1;
}
// ran out of attempts without finding an unused PID :-(
return -1;
}
/**
* Reset the PID counter to 1.
*
* Note that the list of recently finished scripts has to be
* cleared (`recentScripts.splice(0)`) when resetting the PID counter.
* Otherwise scripts which re-use a PID that is still in the
* list of recent scripts do not show up there when they finish
* (if the previous script with that PID is still in the list at that point),
* because the function `AddRecentScript` de-duplicates scripts by PID.
*/
export function resetPidCounter(): void {
pidCounter = 1;
}