From 95a1c18139fbb48a68b117aa3f35d38ccc047ab5 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Wed, 17 Aug 2022 17:11:59 -0400 Subject: [PATCH] synchronize write and scp --- src/NetscriptFunctions.ts | 53 +++++++++++++------------------ src/Script/RamCalculations.ts | 59 +++++++++-------------------------- src/Script/Script.ts | 4 +-- 3 files changed, 38 insertions(+), 78 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 2c617b4b6..df60d64e8 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -829,11 +829,7 @@ const base: InternalAPI = { }, scp: (ctx: NetscriptContext) => - async ( - _scriptname: unknown, - _destination: unknown, - _source: unknown = ctx.workerScript.hostname, - ): Promise => { + (_scriptname: unknown, _destination: unknown, _source: unknown = ctx.workerScript.hostname): boolean => { const destination = helpers.string(ctx, "destination", _destination); const source = helpers.string(ctx, "source", _source); if (Array.isArray(_scriptname)) { @@ -843,14 +839,12 @@ const base: InternalAPI = { throw helpers.makeRuntimeErrorMsg(ctx, "No scripts to copy"); } let res = true; - await Promise.all( - scripts.map(async function (script) { - if (!(await NetscriptFunctions(ctx.workerScript).scp(script, destination, source))) { - res = false; - } - }), - ); - return Promise.resolve(res); + scripts.map(function (script) { + if (!NetscriptFunctions(ctx.workerScript).scp(script, destination, source)) { + res = false; + } + }); + return res; } const scriptName = helpers.string(ctx, "scriptName", _scriptname); @@ -880,18 +874,18 @@ const base: InternalAPI = { if (!found) { helpers.log(ctx, () => `File '${scriptName}' does not exist.`); - return Promise.resolve(false); + return false; } for (let i = 0; i < destServer.messages.length; ++i) { if (destServer.messages[i] === scriptName) { helpers.log(ctx, () => `File '${scriptName}' copied over to '${destServer?.hostname}'.`); - return Promise.resolve(true); // Already exists + return true; // Already exists } } destServer.messages.push(scriptName); helpers.log(ctx, () => `File '${scriptName}' copied over to '${destServer?.hostname}'.`); - return Promise.resolve(true); + return true; } // Scp for text files @@ -905,7 +899,7 @@ const base: InternalAPI = { } if (txtFile === undefined) { helpers.log(ctx, () => `File '${scriptName}' does not exist.`); - return Promise.resolve(false); + return false; } for (let i = 0; i < destServer.textFiles.length; ++i) { @@ -913,13 +907,13 @@ const base: InternalAPI = { // Overwrite destServer.textFiles[i].text = txtFile.text; helpers.log(ctx, () => `File '${scriptName}' copied over to '${destServer?.hostname}'.`); - return Promise.resolve(true); + return true; } } const newFile = new TextFile(txtFile.fn, txtFile.text); destServer.textFiles.push(newFile); helpers.log(ctx, () => `File '${scriptName}' copied over to '${destServer?.hostname}'.`); - return Promise.resolve(true); + return true; } // Scp for script files @@ -932,7 +926,7 @@ const base: InternalAPI = { } if (sourceScript == null) { helpers.log(ctx, () => `File '${scriptName}' does not exist.`); - return Promise.resolve(false); + return false; } // Overwrite script if it already exists @@ -943,11 +937,11 @@ const base: InternalAPI = { // If it's the exact same file don't actually perform the // copy to avoid recompiling uselessly. Players tend to scp // liberally. - if (oldScript.code === sourceScript.code) return Promise.resolve(true); + if (oldScript.code === sourceScript.code) return true; oldScript.code = sourceScript.code; oldScript.ramUsage = sourceScript.ramUsage; oldScript.markUpdated(); - return Promise.resolve(true); + return true; } } @@ -958,13 +952,8 @@ const base: InternalAPI = { newScript.server = destServer.hostname; destServer.scripts.push(newScript); helpers.log(ctx, () => `File '${scriptName}' copied over to '${destServer?.hostname}'.`); - return new Promise((resolve) => { - if (destServer === null) { - resolve(false); - return; - } - newScript.updateRamUsage(Player, destServer.scripts).then(() => resolve(true)); - }); + newScript.updateRamUsage(Player, destServer.scripts); + return true; }, ls: (ctx: NetscriptContext) => @@ -1501,7 +1490,7 @@ const base: InternalAPI = { }, write: (ctx: NetscriptContext) => - (_port: unknown, data: unknown = "", _mode: unknown = "a"): Promise => { + (_port: unknown, data: unknown = "", _mode: unknown = "a"): void => { const port = helpers.string(ctx, "port", _port); const mode = helpers.string(ctx, "mode", _mode); if (isString(port)) { @@ -1545,7 +1534,7 @@ const base: InternalAPI = { const txtFile = getTextFile(fn, server); if (txtFile == null) { createTextFile(fn, String(data), server); - return Promise.resolve(); + return; } if (mode === "w") { txtFile.write(String(data)); @@ -1553,7 +1542,7 @@ const base: InternalAPI = { txtFile.append(String(data)); } } - return Promise.resolve(); + return; } else { throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${port}`); } diff --git a/src/Script/RamCalculations.ts b/src/Script/RamCalculations.ts index 7c69aff5b..61538bf4b 100644 --- a/src/Script/RamCalculations.ts +++ b/src/Script/RamCalculations.ts @@ -44,7 +44,7 @@ const memCheckGlobalKey = ".__GLOBAL__"; * @param {WorkerScript} workerScript - Object containing RAM costs of Netscript functions. Also used to * keep track of what functions have/havent been accounted for */ -async function parseOnlyRamCalculate(player: IPlayer, otherScripts: Script[], code: string): Promise { +function parseOnlyRamCalculate(player: IPlayer, otherScripts: Script[], code: string): RamCalculation { try { /** * Maps dependent identifiers to their dependencies. @@ -88,47 +88,22 @@ async function parseOnlyRamCalculate(player: IPlayer, otherScripts: Script[], co while (parseQueue.length > 0) { const nextModule = parseQueue.shift(); if (nextModule === undefined) throw new Error("nextModule should not be undefined"); + if (nextModule.startsWith("https://") || nextModule.startsWith("http://")) continue; - // Additional modules can either be imported from the web (in which case we use - // a dynamic import), or from other in-game scripts - let code; - if (nextModule.startsWith("https://") || nextModule.startsWith("http://")) { - try { - // eslint-disable-next-line no-await-in-loop - const module = await eval("import(nextModule)"); - code = ""; - for (const prop in module) { - if (typeof module[prop] === "function") { - code += module[prop].toString() + ";\n"; - } - } - } catch (e) { - console.error(`Error dynamically importing module from ${nextModule} for RAM calculations: ${e}`); - return { cost: RamCalculationErrorCode.URLImportError }; + let script = null; + const fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule; + for (const s of otherScripts) { + if (areImportsEquals(s.filename, fn)) { + script = s; + break; } - } else { - if (!Array.isArray(otherScripts)) { - console.warn(`parseOnlyRamCalculate() not called with array of scripts`); - return { cost: RamCalculationErrorCode.ImportError }; - } - - let script = null; - const fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule; - for (const s of otherScripts) { - if (areImportsEquals(s.filename, fn)) { - script = s; - break; - } - } - - if (script == null) { - return { cost: RamCalculationErrorCode.ImportError }; // No such script on the server - } - - code = script.code; } - parseCode(code, nextModule); + if (script == null) { + return { cost: RamCalculationErrorCode.ImportError }; // No such script on the server + } + + parseCode(script.code, nextModule); } // Finally, walk the reference map and generate a ram cost. The initial set of keys to scan @@ -406,13 +381,9 @@ function parseOnlyCalculateDeps(code: string, currentModule: string): ParseDepsR * @param {Script[]} otherScripts - All other scripts on the server. * Used to account for imported scripts */ -export async function calculateRamUsage( - player: IPlayer, - codeCopy: string, - otherScripts: Script[], -): Promise { +export function calculateRamUsage(player: IPlayer, codeCopy: string, otherScripts: Script[]): RamCalculation { try { - return await parseOnlyRamCalculate(player, otherScripts, codeCopy); + return parseOnlyRamCalculate(player, otherScripts, codeCopy); } catch (e) { console.error(`Failed to parse script for RAM calculations:`); console.error(e); diff --git a/src/Script/Script.ts b/src/Script/Script.ts index e4ea95c3d..ba0079c8c 100644 --- a/src/Script/Script.ts +++ b/src/Script/Script.ts @@ -113,8 +113,8 @@ export class Script { * Calculates and updates the script's RAM usage based on its code * @param {Script[]} otherScripts - Other scripts on the server. Used to process imports */ - async updateRamUsage(player: IPlayer, otherScripts: Script[]): Promise { - const res = await calculateRamUsage(player, this.code, otherScripts); + updateRamUsage(player: IPlayer, otherScripts: Script[]): void { + const res = calculateRamUsage(player, this.code, otherScripts); if (res.cost > 0) { this.ramUsage = roundToTwo(res.cost); this.ramUsageEntries = res.entries;