2019-05-17 22:41:16 +02:00
|
|
|
/**
|
|
|
|
* Class representing a script file.
|
|
|
|
*
|
|
|
|
* This does NOT represent a script that is actively running and
|
|
|
|
* being evaluated. See RunningScript for that
|
|
|
|
*/
|
2019-03-05 02:40:28 +01:00
|
|
|
import { calculateRamUsage } from "./RamCalculations";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { Page, routing } from "../ui/navigationTracking";
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
import { setTimeoutRef } from "../utils/SetTimeoutRef";
|
2019-05-05 06:03:40 +02:00
|
|
|
import {
|
|
|
|
Generic_fromJSON,
|
|
|
|
Generic_toJSON,
|
|
|
|
Reviver
|
|
|
|
} from "../../utils/JSONReviver";
|
2019-03-03 04:15:10 +01:00
|
|
|
import { roundToTwo } from "../../utils/helpers/roundToTwo";
|
|
|
|
|
2019-06-03 02:17:27 +02:00
|
|
|
let globalModuleSequenceNumber = 0;
|
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
export class Script {
|
|
|
|
// Initializes a Script Object from a JSON save state
|
|
|
|
static fromJSON(value: any): Script {
|
|
|
|
return Generic_fromJSON(Script, value.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code for this script
|
|
|
|
code: string = "";
|
|
|
|
|
|
|
|
// Filename for the script file
|
|
|
|
filename: string = "";
|
|
|
|
|
|
|
|
// The dynamic module generated for this script when it is run.
|
|
|
|
// This is only applicable for NetscriptJS
|
|
|
|
module: any = "";
|
|
|
|
|
2019-06-02 21:21:08 +02:00
|
|
|
// The timestamp when when the script was last updated.
|
2019-06-03 02:17:27 +02:00
|
|
|
moduleSequenceNumber: number;
|
2019-06-02 21:21:08 +02:00
|
|
|
|
|
|
|
// 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: string[] = [];
|
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
// Amount of RAM this Script requres to run
|
|
|
|
ramUsage: number = 0;
|
|
|
|
|
|
|
|
// IP of server that this script is on.
|
|
|
|
server: string = "";
|
|
|
|
|
2019-05-07 03:01:06 +02:00
|
|
|
constructor(fn: string="", code: string="", server: string="", otherScripts: Script[]=[]) {
|
2019-03-03 04:15:10 +01:00
|
|
|
this.filename = fn;
|
|
|
|
this.code = code;
|
|
|
|
this.ramUsage = 0;
|
|
|
|
this.server = server; // IP of server this script is on
|
|
|
|
this.module = "";
|
2019-06-03 02:17:27 +02:00
|
|
|
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
|
2019-05-07 03:01:06 +02:00
|
|
|
if (this.code !== "") { this.updateRamUsage(otherScripts); }
|
2019-03-03 04:15:10 +01:00
|
|
|
};
|
|
|
|
|
2019-05-17 22:41:16 +02:00
|
|
|
/**
|
|
|
|
* Download the script as a file
|
|
|
|
*/
|
2019-03-03 04:15:10 +01:00
|
|
|
download(): void {
|
|
|
|
const filename = this.filename + ".js";
|
|
|
|
const file = new Blob([this.code], {type: 'text/plain'});
|
|
|
|
if (window.navigator.msSaveOrOpenBlob) {// IE10+
|
|
|
|
window.navigator.msSaveOrOpenBlob(file, filename);
|
|
|
|
} else { // Others
|
|
|
|
var a = document.createElement("a"),
|
|
|
|
url = URL.createObjectURL(file);
|
|
|
|
a.href = url;
|
|
|
|
a.download = filename;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
setTimeoutRef(function() {
|
|
|
|
document.body.removeChild(a);
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:32:35 +02:00
|
|
|
/**
|
|
|
|
* Marks this script as having been updated. It will be recompiled next time something tries
|
|
|
|
* to exec it.
|
|
|
|
*/
|
|
|
|
markUpdated() {
|
|
|
|
this.module = "";
|
2019-06-03 08:29:56 +02:00
|
|
|
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
|
2019-06-02 21:32:35 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 22:41:16 +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
|
|
|
|
*/
|
2019-05-07 03:01:06 +02:00
|
|
|
saveScript(code: string, serverIp: string, otherScripts: Script[]): void {
|
2019-03-03 04:15:10 +01:00
|
|
|
if (routing.isOn(Page.ScriptEditor)) {
|
2019-05-17 22:41:16 +02:00
|
|
|
// Update code and filename
|
2019-03-03 04:15:10 +01:00
|
|
|
this.code = code.replace(/^\s+|\s+$/g, '');
|
|
|
|
|
|
|
|
const filenameElem: HTMLInputElement | null = document.getElementById("script-editor-filename") as HTMLInputElement;
|
|
|
|
if (filenameElem == null) {
|
|
|
|
console.error(`Failed to get Script filename DOM element`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.filename = filenameElem!.value;
|
2019-05-07 03:01:06 +02:00
|
|
|
this.server = serverIp;
|
|
|
|
this.updateRamUsage(otherScripts);
|
2019-06-03 05:53:07 +02:00
|
|
|
this.markUpdated();
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 22:41:16 +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
|
|
|
|
*/
|
2019-05-07 03:01:06 +02:00
|
|
|
async updateRamUsage(otherScripts: Script[]) {
|
|
|
|
var res = await calculateRamUsage(this.code, otherScripts);
|
2019-05-12 04:20:20 +02:00
|
|
|
if (res > 0) {
|
2019-03-03 04:15:10 +01:00
|
|
|
this.ramUsage = roundToTwo(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the current object to a JSON save state
|
|
|
|
toJSON(): any {
|
|
|
|
return Generic_toJSON("Script", this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.Script = Script;
|