change sf4

This commit is contained in:
Olivier Gagnon
2022-01-04 19:09:34 -05:00
parent c06087c634
commit c59a267437
32 changed files with 431 additions and 455 deletions
+6 -1
View File
@@ -14,6 +14,7 @@ import { RamCosts, RamCostConstants } from "../Netscript/RamCostGenerator";
import { Script } from "../Script/Script";
import { WorkerScript } from "../Netscript/WorkerScript";
import { areImportsEquals } from "../Terminal/DirectoryHelpers";
import { IPlayer } from "../PersonObjects/IPlayer";
// These special strings are used to reference the presence of a given logical
// construct within a user script.
@@ -33,6 +34,7 @@ const memCheckGlobalKey = ".__GLOBAL__";
* keep track of what functions have/havent been accounted for
*/
async function parseOnlyRamCalculate(
player: IPlayer,
otherScripts: Script[],
code: string,
workerScript: WorkerScript,
@@ -169,6 +171,8 @@ async function parseOnlyRamCalculate(
function applyFuncRam(cost: any): number {
if (typeof cost === "number") {
return cost;
} else if (typeof cost === "function") {
return cost(player);
} else {
return 0;
}
@@ -375,6 +379,7 @@ function parseOnlyCalculateDeps(code: string, currentModule: string): any {
* Used to account for imported scripts
*/
export async function calculateRamUsage(
player: IPlayer,
codeCopy: string,
otherScripts: Script[],
): Promise<RamCalculationErrorCode | number> {
@@ -388,7 +393,7 @@ export async function calculateRamUsage(
} as WorkerScript;
try {
return await parseOnlyRamCalculate(otherScripts, codeCopy, workerScript);
return await parseOnlyRamCalculate(player, otherScripts, codeCopy, workerScript);
} catch (e) {
console.error(`Failed to parse script for RAM calculations:`);
console.error(e);
+9 -9
View File
@@ -10,6 +10,7 @@ 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 { IPlayer } from "../PersonObjects/IPlayer";
let globalModuleSequenceNumber = 0;
@@ -44,7 +45,7 @@ export class Script {
// sha256 hash of the code in the Script. Do not access directly.
_hash = "";
constructor(fn = "", code = "", server = "", otherScripts: Script[] = []) {
constructor(player: IPlayer | null = null, fn = "", code = "", server = "", otherScripts: Script[] = []) {
this.filename = fn;
this.code = code;
this.ramUsage = 0;
@@ -52,8 +53,8 @@ export class Script {
this.module = "";
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
this._hash = "";
if (this.code !== "") {
this.updateRamUsage(otherScripts);
if (this.code !== "" && player !== null) {
this.updateRamUsage(player, otherScripts);
this.rehash();
}
}
@@ -105,8 +106,7 @@ export class Script {
* @returns the computed hash of the script
*/
hash(): string {
if (!this._hash)
this.rehash();
if (!this._hash) this.rehash();
return this._hash;
}
@@ -115,13 +115,13 @@ export class Script {
* @param {string} code - The new contents of the script
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
*/
saveScript(filename: string, code: string, hostname: string, otherScripts: Script[]): void {
saveScript(player: IPlayer, filename: string, code: string, hostname: string, otherScripts: Script[]): void {
// Update code and filename
this.code = code.replace(/^\s+|\s+$/g, "");
this.filename = filename;
this.server = hostname;
this.updateRamUsage(otherScripts);
this.updateRamUsage(player, otherScripts);
this.markUpdated();
}
@@ -129,8 +129,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(otherScripts: Script[]): Promise<void> {
const res = await calculateRamUsage(this.code, otherScripts);
async updateRamUsage(player: IPlayer, otherScripts: Script[]): Promise<void> {
const res = await calculateRamUsage(player, this.code, otherScripts);
if (res > 0) {
this.ramUsage = roundToTwo(res);
}