bitburner-src/src/Script/Script.ts

173 lines
5.0 KiB
TypeScript
Raw Normal View History

/**
* Class representing a script file.
*
* This does NOT represent a script that is actively running and
* being evaluated. See RunningScript for that
*/
2022-01-05 22:41:48 +01:00
import { calculateRamUsage, RamUsageEntry } from "./RamCalculations";
import { ScriptUrl } from "./ScriptUrl";
2021-09-25 20:42:57 +02:00
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
import { roundToTwo } from "../utils/helpers/roundToTwo";
2021-12-13 01:39:53 +01:00
import { computeHash } from "../utils/helpers/computeHash";
2022-01-05 01:09:34 +01:00
import { IPlayer } from "../PersonObjects/IPlayer";
2019-03-03 04:15:10 +01:00
let globalModuleSequenceNumber = 0;
2019-03-03 04:15:10 +01:00
export class Script {
2021-09-05 01:09:30 +02:00
// Code for this script
code = "";
// Filename for the script file
filename = "";
// url of the script if any, only for NS2.
url = "";
// The dynamic module generated for this script when it is run.
// This is only applicable for NetscriptJS
module: any = "";
// The timestamp when when the script was last updated.
moduleSequenceNumber: number;
// Only used with NS2 scripts; the list of dependency script filenames. This is constructed
// whenever the script is first evaluated, and therefore may be out of date if the script
// has been updated since it was last run.
dependencies: ScriptUrl[] = [];
// Amount of RAM this Script requres to run
ramUsage = 0;
2022-01-05 22:41:48 +01:00
ramUsageEntries?: RamUsageEntry[];
2021-09-05 01:09:30 +02:00
2021-10-07 23:55:49 +02:00
// hostname of server that this script is on.
2021-09-05 01:09:30 +02:00
server = "";
2021-12-13 01:39:53 +01:00
// sha256 hash of the code in the Script. Do not access directly.
_hash = "";
2022-01-05 01:09:34 +01:00
constructor(player: IPlayer | null = null, fn = "", code = "", server = "", otherScripts: Script[] = []) {
2021-09-05 01:09:30 +02:00
this.filename = fn;
this.code = code;
this.ramUsage = 0;
2021-10-07 23:55:49 +02:00
this.server = server; // hostname of server this script is on
2021-09-05 01:09:30 +02:00
this.module = "";
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
2021-12-13 01:39:53 +01:00
this._hash = "";
2022-01-05 01:09:34 +01:00
if (this.code !== "" && player !== null) {
this.updateRamUsage(player, otherScripts);
2021-12-13 01:39:53 +01:00
this.rehash();
2021-04-30 05:52:56 +02:00
}
2021-09-05 01:09:30 +02:00
}
/**
* Download the script as a file
*/
download(): void {
const filename = this.filename;
2021-09-05 01:09:30 +02:00
const file = new Blob([this.code], { type: "text/plain" });
2021-10-13 01:23:36 +02:00
const navigator = window.navigator as any;
if (navigator.msSaveOrOpenBlob) {
2021-09-05 01:09:30 +02:00
// IE10+
2021-10-13 01:23:36 +02:00
navigator.msSaveOrOpenBlob(file, filename);
2021-09-05 01:09:30 +02:00
} else {
// Others
const a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
2021-09-05 01:09:30 +02:00
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
2019-03-03 04:15:10 +01:00
}
2021-09-05 01:09:30 +02:00
}
/**
* Marks this script as having been updated. It will be recompiled next time something tries
* to exec it.
*/
markUpdated(): void {
this.module = "";
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
2021-12-13 01:39:53 +01:00
this.rehash();
}
/**
* Force update of the computed hash based on the source code.
*/
rehash(): void {
this._hash = computeHash(this.code);
}
/**
* If the hash is not computed, computes the hash. Otherwise return the computed hash.
* @returns the computed hash of the script
*/
hash(): string {
2022-01-05 01:09:34 +01:00
if (!this._hash) this.rehash();
2021-12-13 01:39:53 +01:00
return this._hash;
2021-09-05 01:09:30 +02:00
}
/**
* Save a script from the script editor
* @param {string} code - The new contents of the script
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
*/
2022-01-05 01:09:34 +01:00
saveScript(player: IPlayer, filename: string, code: string, hostname: string, otherScripts: Script[]): void {
2021-09-17 08:58:02 +02:00
// Update code and filename
this.code = Script.formatCode(this.code);
2021-09-17 08:58:02 +02:00
2021-09-25 07:06:17 +02:00
this.filename = filename;
2021-10-07 23:55:49 +02:00
this.server = hostname;
2022-01-05 01:09:34 +01:00
this.updateRamUsage(player, otherScripts);
2021-09-17 08:58:02 +02:00
this.markUpdated();
2021-09-05 01:09:30 +02:00
}
/**
* Calculates and updates the script's RAM usage based on its code
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
*/
2022-01-05 01:09:34 +01:00
async updateRamUsage(player: IPlayer, otherScripts: Script[]): Promise<void> {
const res = await calculateRamUsage(player, this.code, otherScripts);
2022-01-05 22:41:48 +01:00
if (res.cost > 0) {
this.ramUsage = roundToTwo(res.cost);
this.ramUsageEntries = res.entries;
2021-05-01 09:17:31 +02:00
}
this.markUpdated();
2021-09-05 01:09:30 +02:00
}
2021-10-13 01:23:36 +02:00
imports(): string[] {
return [];
}
2021-09-05 01:09:30 +02:00
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("Script", this);
}
// Initializes a Script Object from a JSON save state
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): Script {
2021-12-13 01:39:53 +01:00
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();
return s;
2021-09-05 01:09:30 +02:00
}
/**
* Formats code: Removes the starting & trailing whitespace
* @param {string} code - The code to format
* @returns The formatted code
*/
static formatCode(code: string): string {
return code.replace(/^\s+|\s+$/g, "");
}
2019-03-03 04:15:10 +01:00
}
Reviver.constructors.Script = Script;