mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-22 01:03:01 +02:00
MISC: Make spawn able to have 0 delay (#1333)
This eliminates a hole where spawn was unrelaible, because other scripts could jump in and steal the RAM. It's not an API break, because 0 used to be an invalid value.
This commit is contained in:
@@ -737,23 +737,36 @@ export const ns: InternalAPI<NSFull> = {
|
||||
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
|
||||
const runOpts = helpers.spawnOptions(ctx, _thread_or_opt);
|
||||
const args = helpers.scriptArgs(ctx, _args);
|
||||
setTimeout(() => {
|
||||
const spawnCb = () => {
|
||||
if (Router.page() === Page.BitVerse) {
|
||||
helpers.log(ctx, () => `Script execution is canceled because you are in Bitverse.`);
|
||||
return;
|
||||
}
|
||||
const scriptServer = GetServer(ctx.workerScript.hostname);
|
||||
if (scriptServer === null) {
|
||||
if (scriptServer == null) {
|
||||
throw helpers.errorMessage(ctx, `Cannot find server ${ctx.workerScript.hostname}`);
|
||||
}
|
||||
|
||||
runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts);
|
||||
}, runOpts.spawnDelay);
|
||||
return runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts);
|
||||
};
|
||||
|
||||
helpers.log(ctx, () => `Will execute '${path}' in ${runOpts.spawnDelay} milliseconds`);
|
||||
if (runOpts.spawnDelay !== 0) {
|
||||
setTimeout(spawnCb, runOpts.spawnDelay);
|
||||
helpers.log(ctx, () => `Will execute '${path}' in ${runOpts.spawnDelay} milliseconds`);
|
||||
}
|
||||
|
||||
if (killWorkerScript(ctx.workerScript)) {
|
||||
helpers.log(ctx, () => "Exiting...");
|
||||
helpers.log(ctx, () => "About to exit...");
|
||||
const killed = killWorkerScript(ctx.workerScript);
|
||||
|
||||
if (runOpts.spawnDelay === 0) {
|
||||
helpers.log(ctx, () => `Executing '${path}' immediately`);
|
||||
spawnCb();
|
||||
}
|
||||
|
||||
if (killed) {
|
||||
// This prevents error messages about statements after the spawn()
|
||||
// trying to be executed when the script is dead.
|
||||
throw new ScriptDeath(ctx.workerScript);
|
||||
}
|
||||
},
|
||||
kill:
|
||||
|
||||
Reference in New Issue
Block a user