diff --git a/src/NetscriptJSEvaluator.ts b/src/NetscriptJSEvaluator.ts index 408dba167..3267c4e24 100644 --- a/src/NetscriptJSEvaluator.ts +++ b/src/NetscriptJSEvaluator.ts @@ -9,9 +9,6 @@ import { makeRuntimeRejectMsg } from "./NetscriptEvaluator"; import { ScriptUrl } from "./Script/ScriptUrl"; import { WorkerScript } from "./Netscript/WorkerScript"; import { Script } from "./Script/Script"; -import { computeHash } from "./utils/helpers/computeHash"; -import { BlobCache } from "./utils/BlobCache"; -import { ImportCache } from "./utils/ImportCache"; import { areImportsEquals } from "./Terminal/DirectoryHelpers"; import { IPlayer } from "./PersonObjects/IPlayer"; @@ -190,31 +187,12 @@ function _getScriptUrls(script: Script, scripts: Script[], seen: Script[]): Scri if (matchingScripts.length === 0) continue; const [importedScript] = matchingScripts; - // Check to see if the urls for this script are stored in the cache by the hash value. - let urls = ImportCache.get(importedScript.hash()); - // If we don't have it in the cache, then we need to generate the urls for it. - if (urls) { - // Verify that these urls are valid and have not been updated. - for (const url of urls) { - if (isDependencyOutOfDate(url.filename, scripts, url.moduleSequenceNumber)) { - // Revoke these URLs from the browser. We will be unable to use them again. - for (const url of urls) URL.revokeObjectURL(url.url); - // Clear the cache and prepare for new blobs. - urls = null; - ImportCache.remove(importedScript.hash()); - break; - } - } - } - if (!urls) { - // Try to get a URL for the requested script and its dependencies. - urls = _getScriptUrls(importedScript, scripts, seen); - } + + const urls = _getScriptUrls(importedScript, scripts, seen); // The top url in the stack is the replacement import file for this script. urlStack.push(...urls); const blob = urls[urls.length - 1].url; - ImportCache.store(importedScript.hash(), urls); // Replace the blob inside the import statement. transformedCode = transformedCode.substring(0, node.start) + blob + transformedCode.substring(node.end); @@ -224,17 +202,7 @@ function _getScriptUrls(script: Script, scripts: Script[], seen: Script[]): Scri // accidental calls to window.print() do not bring up the "print screen" dialog transformedCode += `\n\nfunction print() {throw new Error("Invalid call to window.print(). Did you mean to use Netscript's print()?");}`; - // If we successfully transformed the code, create a blob url for it - // Compute the hash for the transformed code - const transformedHash = computeHash(transformedCode); - // Check to see if this transformed hash is in our cache - let blob = BlobCache.get(transformedHash); - if (!blob) { - blob = URL.createObjectURL(makeScriptBlob(transformedCode)); - } - // Store this blob in the cache. Any script that transforms the same - // (e.g. same scripts on server, same hash value, etc) can use this blob url. - BlobCache.store(transformedHash, blob); + const blob = URL.createObjectURL(makeScriptBlob(transformedCode)); // Push the blob URL onto the top of the stack. urlStack.push(new ScriptUrl(script.filename, blob, script.moduleSequenceNumber)); return urlStack; diff --git a/src/Script/Script.ts b/src/Script/Script.ts index 393e08091..458b40c3f 100644 --- a/src/Script/Script.ts +++ b/src/Script/Script.ts @@ -9,13 +9,14 @@ import { ScriptUrl } from "./ScriptUrl"; import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver"; import { roundToTwo } from "../utils/helpers/roundToTwo"; -import { computeHash } from "../utils/helpers/computeHash"; -import { ImportCache } from "../utils/ImportCache"; import { IPlayer } from "../PersonObjects/IPlayer"; let globalModuleSequenceNumber = 0; -interface ScriptReference { filename: string; server: string } +interface ScriptReference { + filename: string; + server: string; +} export class Script { // Code for this script @@ -47,9 +48,6 @@ export class Script { // hostname of server that this script is on. server = ""; - // sha256 hash of the code in the Script. Do not access directly. - _hash = ""; - constructor(player: IPlayer | null = null, fn = "", code = "", server = "", otherScripts: Script[] = []) { this.filename = fn; this.code = code; @@ -57,10 +55,8 @@ export class Script { this.server = server; // hostname of server this script is on this.module = ""; this.moduleSequenceNumber = ++globalModuleSequenceNumber; - this._hash = ""; if (this.code !== "" && player !== null) { this.updateRamUsage(player, otherScripts); - this.rehash(); } } @@ -96,27 +92,6 @@ export class Script { markUpdated(): void { this.module = ""; this.moduleSequenceNumber = ++globalModuleSequenceNumber; - this.rehash(); - } - - /** - * Force update of the computed hash based on the source code. - */ - rehash(): void { - const oldHash = this._hash; - this._hash = computeHash(this.code); - if (oldHash !== this._hash) { - ImportCache.remove(oldHash); - } - } - - /** - * If the hash is not computed, computes the hash. Otherwise return the computed hash. - * @returns the computed hash of the script - */ - hash(): string { - if (!this._hash) this.rehash(); - return this._hash; } /** @@ -133,7 +108,9 @@ export class Script { this.updateRamUsage(player, otherScripts); this.markUpdated(); for (const dependent of this.dependents) { - const [dependentScript] = otherScripts.filter(s => s.filename === dependent.filename && s.server == dependent.server); + const [dependentScript] = otherScripts.filter( + (s) => s.filename === dependent.filename && s.server == dependent.server, + ); if (dependentScript !== null) dependentScript.markUpdated(); } } @@ -166,8 +143,6 @@ export class Script { const s = Generic_fromJSON(Script, value.data); // Force the url to blank from the save data. Urls are not valid outside the current browser page load. s.url = ""; - // Rehash the code to ensure that hash is set properly. - s.rehash(); s.dependents = []; return s; } @@ -177,7 +152,7 @@ export class Script { * @param {string} code - The code to format * @returns The formatted code */ - static formatCode(code: string): string { + static formatCode(code: string): string { return code.replace(/^\s+|\s+$/g, ""); } } diff --git a/src/ui/React/CharacterOverview.tsx b/src/ui/React/CharacterOverview.tsx index 9057e04e8..e5beed047 100644 --- a/src/ui/React/CharacterOverview.tsx +++ b/src/ui/React/CharacterOverview.tsx @@ -453,14 +453,14 @@ export function CharacterOverview({ save, killScripts }: IProps): React.ReactEle - + - setKillOpen(true)}> + setKillOpen(true)}> diff --git a/src/ui/React/Overview.tsx b/src/ui/React/Overview.tsx index 8e865f90e..d1bdf2a56 100644 --- a/src/ui/React/Overview.tsx +++ b/src/ui/React/Overview.tsx @@ -124,7 +124,12 @@ export function Overview({ children, mode }: IProps): React.ReactElement { {header} - diff --git a/src/utils/BlobCache.ts b/src/utils/BlobCache.ts deleted file mode 100644 index e58a9abb6..000000000 --- a/src/utils/BlobCache.ts +++ /dev/null @@ -1,18 +0,0 @@ - -const blobCache: { [hash: string]: string } = {}; - -export class BlobCache { - static get(hash: string): string { - return blobCache[hash]; - } - - static store(hash: string, value: string): void { - if (blobCache[hash]) return; - blobCache[hash] = value; - } - - static removeByValue(value: string): void { - const keys = Object.keys(blobCache).filter((key) => blobCache[key] === value); - keys.forEach((key) => delete blobCache[key]); - } -} diff --git a/src/utils/ImportCache.ts b/src/utils/ImportCache.ts deleted file mode 100644 index ffeb845e8..000000000 --- a/src/utils/ImportCache.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { ScriptUrl } from "../Script/ScriptUrl"; - -const importCache: { [hash: string]: ScriptUrl[] } = {}; - -export class ImportCache { - static get(hash: string): ScriptUrl[] | null { - return importCache[hash] || null; - } - - static store(hash: string, value: ScriptUrl[]): void { - if (importCache[hash]) return; - importCache[hash] = value; - } - - static remove(hash: string): void { - delete importCache[hash]; - } -} diff --git a/src/utils/helpers/computeHash.ts b/src/utils/helpers/computeHash.ts deleted file mode 100644 index f7c1435bb..000000000 --- a/src/utils/helpers/computeHash.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { sha256 } from "js-sha256"; - -/** - * Computes a SHA-256 hash of a string synchronously - * @param message The input string - * @returns The SHA-256 hash in hex - */ -export function computeHash(message: string): string { - const hash = sha256.create(); - hash.update(message); - return hash.hex(); -}