prettify, sorry for the big ass commit

This commit is contained in:
Olivier Gagnon
2021-09-04 19:09:30 -04:00
parent 3d7cdb4ef9
commit a18bdd6afc
554 changed files with 91615 additions and 66138 deletions
+3 -3
View File
@@ -1,5 +1,5 @@
export enum RamCalculationErrorCode {
SyntaxError = -1,
ImportError = -2,
URLImportError = -3,
SyntaxError = -1,
ImportError = -2,
URLImportError = -3,
}
+4 -1
View File
@@ -1,3 +1,6 @@
import { Script } from "./Script";
export declare function calculateRamUsage(codeCopy: string, otherScripts: Script[]): number;
export declare function calculateRamUsage(
codeCopy: string,
otherScripts: Script[],
): number;
+286 -262
View File
@@ -30,183 +30,195 @@ const memCheckGlobalKey = ".__GLOBAL__";
* keep track of what functions have/havent been accounted for
*/
async function parseOnlyRamCalculate(otherScripts, code, workerScript) {
try {
/**
* Maps dependent identifiers to their dependencies.
*
* The initial identifier is __SPECIAL_INITIAL_MODULE__.__GLOBAL__.
* It depends on all the functions declared in the module, all the global scopes
* of its imports, and any identifiers referenced in this global scope. Each
* function depends on all the identifiers referenced internally.
* We walk the dependency graph to calculate RAM usage, given that some identifiers
* reference Netscript functions which have a RAM cost.
*/
let dependencyMap = {};
try {
/**
* Maps dependent identifiers to their dependencies.
*
* The initial identifier is __SPECIAL_INITIAL_MODULE__.__GLOBAL__.
* It depends on all the functions declared in the module, all the global scopes
* of its imports, and any identifiers referenced in this global scope. Each
* function depends on all the identifiers referenced internally.
* We walk the dependency graph to calculate RAM usage, given that some identifiers
* reference Netscript functions which have a RAM cost.
*/
let dependencyMap = {};
// Scripts we've parsed.
const completedParses = new Set();
// Scripts we've parsed.
const completedParses = new Set();
// Scripts we've discovered that need to be parsed.
const parseQueue = [];
// Scripts we've discovered that need to be parsed.
const parseQueue = [];
// Parses a chunk of code with a given module name, and updates parseQueue and dependencyMap.
function parseCode(code, moduleName) {
const result = parseOnlyCalculateDeps(code, moduleName);
completedParses.add(moduleName);
// Parses a chunk of code with a given module name, and updates parseQueue and dependencyMap.
function parseCode(code, moduleName) {
const result = parseOnlyCalculateDeps(code, moduleName);
completedParses.add(moduleName);
// Add any additional modules to the parse queue;
for (let i = 0; i < result.additionalModules.length; ++i) {
if (!completedParses.has(result.additionalModules[i])) {
parseQueue.push(result.additionalModules[i]);
}
}
// Splice all the references in
dependencyMap = Object.assign(dependencyMap, result.dependencyMap);
// Add any additional modules to the parse queue;
for (let i = 0; i < result.additionalModules.length; ++i) {
if (!completedParses.has(result.additionalModules[i])) {
parseQueue.push(result.additionalModules[i]);
}
}
// Parse the initial module, which is the "main" script that is being run
const initialModule = "__SPECIAL_INITIAL_MODULE__";
parseCode(code, initialModule);
// Process additional modules, which occurs if the "main" script has any imports
while (parseQueue.length > 0) {
const nextModule = parseQueue.shift();
// 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 RamCalculationErrorCode.URLImportError;
}
} else {
if (!Array.isArray(otherScripts)) {
console.warn(`parseOnlyRamCalculate() not called with array of scripts`);
return RamCalculationErrorCode.ImportError;
}
let script = null;
let fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule;
for (const s of otherScripts) {
if (s.filename === fn) {
script = s;
break;
}
}
if (script == null) {
return RamCalculationErrorCode.ImportError; // No such script on the server
}
code = script.code;
}
parseCode(code, nextModule);
}
// Finally, walk the reference map and generate a ram cost. The initial set of keys to scan
// are those that start with __SPECIAL_INITIAL_MODULE__.
let ram = RamCostConstants.ScriptBaseRamCost;
const unresolvedRefs = Object.keys(dependencyMap).filter(s => s.startsWith(initialModule));
const resolvedRefs = new Set();
while (unresolvedRefs.length > 0) {
const ref = unresolvedRefs.shift();
// Check if this is one of the special keys, and add the appropriate ram cost if so.
if (ref === "hacknet" && !resolvedRefs.has("hacknet")) {
ram += RamCostConstants.ScriptHacknetNodesRamCost;
}
if (ref === "document" && !resolvedRefs.has("document")) {
ram += RamCostConstants.ScriptDomRamCost;
}
if (ref === "window" && !resolvedRefs.has("window")) {
ram += RamCostConstants.ScriptDomRamCost;
}
resolvedRefs.add(ref);
if (ref.endsWith(".*")) {
// A prefix reference. We need to find all matching identifiers.
const prefix = ref.slice(0, ref.length - 2);
for (let ident of Object.keys(dependencyMap).filter(k => k.startsWith(prefix))) {
for (let dep of dependencyMap[ident] || []) {
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
}
}
} else {
// An exact reference. Add all dependencies of this ref.
for (let dep of dependencyMap[ref] || []) {
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
}
}
// Check if this identifier is a function in the workerScript environment.
// If it is, then we need to get its RAM cost.
try {
function applyFuncRam(func) {
if (typeof func === "function") {
try {
let res;
if (func.constructor.name === "AsyncFunction") {
res = 0; // Async functions will always be 0 RAM
} else {
res = func.apply(null, []);
}
if (typeof res === "number") {
return res;
}
return 0;
} catch(e) {
console.error(`Error applying function: ${e}`);
return 0;
}
} else {
return 0;
}
}
// Only count each function once
if (workerScript.loadedFns[ref]) {
continue;
} else {
workerScript.loadedFns[ref] = true;
}
// This accounts for namespaces (Bladeburner, CodingCpntract, etc.)
let func;
if (ref in workerScript.env.vars.bladeburner) {
func = workerScript.env.vars.bladeburner[ref];
} else if (ref in workerScript.env.vars.codingcontract) {
func = workerScript.env.vars.codingcontract[ref];
} else if (ref in workerScript.env.vars.gang) {
func = workerScript.env.vars.gang[ref];
} else if (ref in workerScript.env.vars.sleeve) {
func = workerScript.env.vars.sleeve[ref];
} else {
func = workerScript.env.vars[ref];
}
ram += applyFuncRam(func);
} catch (error) {continue;}
}
return ram;
} catch (error) {
// console.info("parse or eval error: ", error);
// This is not unexpected. The user may be editing a script, and it may be in
// a transitory invalid state.
return RamCalculationErrorCode.SyntaxError;
// Splice all the references in
dependencyMap = Object.assign(dependencyMap, result.dependencyMap);
}
// Parse the initial module, which is the "main" script that is being run
const initialModule = "__SPECIAL_INITIAL_MODULE__";
parseCode(code, initialModule);
// Process additional modules, which occurs if the "main" script has any imports
while (parseQueue.length > 0) {
const nextModule = parseQueue.shift();
// 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 RamCalculationErrorCode.URLImportError;
}
} else {
if (!Array.isArray(otherScripts)) {
console.warn(
`parseOnlyRamCalculate() not called with array of scripts`,
);
return RamCalculationErrorCode.ImportError;
}
let script = null;
let fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule;
for (const s of otherScripts) {
if (s.filename === fn) {
script = s;
break;
}
}
if (script == null) {
return RamCalculationErrorCode.ImportError; // No such script on the server
}
code = script.code;
}
parseCode(code, nextModule);
}
// Finally, walk the reference map and generate a ram cost. The initial set of keys to scan
// are those that start with __SPECIAL_INITIAL_MODULE__.
let ram = RamCostConstants.ScriptBaseRamCost;
const unresolvedRefs = Object.keys(dependencyMap).filter((s) =>
s.startsWith(initialModule),
);
const resolvedRefs = new Set();
while (unresolvedRefs.length > 0) {
const ref = unresolvedRefs.shift();
// Check if this is one of the special keys, and add the appropriate ram cost if so.
if (ref === "hacknet" && !resolvedRefs.has("hacknet")) {
ram += RamCostConstants.ScriptHacknetNodesRamCost;
}
if (ref === "document" && !resolvedRefs.has("document")) {
ram += RamCostConstants.ScriptDomRamCost;
}
if (ref === "window" && !resolvedRefs.has("window")) {
ram += RamCostConstants.ScriptDomRamCost;
}
resolvedRefs.add(ref);
if (ref.endsWith(".*")) {
// A prefix reference. We need to find all matching identifiers.
const prefix = ref.slice(0, ref.length - 2);
for (let ident of Object.keys(dependencyMap).filter((k) =>
k.startsWith(prefix),
)) {
for (let dep of dependencyMap[ident] || []) {
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
}
}
} else {
// An exact reference. Add all dependencies of this ref.
for (let dep of dependencyMap[ref] || []) {
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
}
}
// Check if this identifier is a function in the workerScript environment.
// If it is, then we need to get its RAM cost.
try {
function applyFuncRam(func) {
if (typeof func === "function") {
try {
let res;
if (func.constructor.name === "AsyncFunction") {
res = 0; // Async functions will always be 0 RAM
} else {
res = func.apply(null, []);
}
if (typeof res === "number") {
return res;
}
return 0;
} catch (e) {
console.error(`Error applying function: ${e}`);
return 0;
}
} else {
return 0;
}
}
// Only count each function once
if (workerScript.loadedFns[ref]) {
continue;
} else {
workerScript.loadedFns[ref] = true;
}
// This accounts for namespaces (Bladeburner, CodingCpntract, etc.)
let func;
if (ref in workerScript.env.vars.bladeburner) {
func = workerScript.env.vars.bladeburner[ref];
} else if (ref in workerScript.env.vars.codingcontract) {
func = workerScript.env.vars.codingcontract[ref];
} else if (ref in workerScript.env.vars.gang) {
func = workerScript.env.vars.gang[ref];
} else if (ref in workerScript.env.vars.sleeve) {
func = workerScript.env.vars.sleeve[ref];
} else {
func = workerScript.env.vars[ref];
}
ram += applyFuncRam(func);
} catch (error) {
continue;
}
}
return ram;
} catch (error) {
// console.info("parse or eval error: ", error);
// This is not unexpected. The user may be editing a script, and it may be in
// a transitory invalid state.
return RamCalculationErrorCode.SyntaxError;
}
}
/**
@@ -216,99 +228,111 @@ async function parseOnlyRamCalculate(otherScripts, code, workerScript) {
* that need to be parsed (i.e. are 'import'ed scripts).
*/
function parseOnlyCalculateDeps(code, currentModule) {
const ast = parse(code, {sourceType:"module", ecmaVersion: 'latest' });
const ast = parse(code, { sourceType: "module", ecmaVersion: "latest" });
// Everything from the global scope goes in ".". Everything else goes in ".function", where only
// the outermost layer of functions counts.
const globalKey = currentModule + memCheckGlobalKey;
const dependencyMap = {};
dependencyMap[globalKey] = new Set();
// Everything from the global scope goes in ".". Everything else goes in ".function", where only
// the outermost layer of functions counts.
const globalKey = currentModule + memCheckGlobalKey;
const dependencyMap = {};
dependencyMap[globalKey] = new Set();
// If we reference this internal name, we're really referencing that external name.
// Filled when we import names from other modules.
let internalToExternal = {};
// If we reference this internal name, we're really referencing that external name.
// Filled when we import names from other modules.
let internalToExternal = {};
var additionalModules = [];
var additionalModules = [];
// References get added pessimistically. They are added for thisModule.name, name, and for
// any aliases.
function addRef(key, name) {
const s = dependencyMap[key] || (dependencyMap[key] = new Set());
if (name in internalToExternal) {
s.add(internalToExternal[name]);
}
s.add(currentModule + "." + name);
s.add(name); // For builtins like hack.
// References get added pessimistically. They are added for thisModule.name, name, and for
// any aliases.
function addRef(key, name) {
const s = dependencyMap[key] || (dependencyMap[key] = new Set());
if (name in internalToExternal) {
s.add(internalToExternal[name]);
}
s.add(currentModule + "." + name);
s.add(name); // For builtins like hack.
}
//A list of identifiers that resolve to "native Javascript code"
const objectPrototypeProperties = Object.getOwnPropertyNames(Object.prototype);
//A list of identifiers that resolve to "native Javascript code"
const objectPrototypeProperties = Object.getOwnPropertyNames(
Object.prototype,
);
// If we discover a dependency identifier, state.key is the dependent identifier.
// walkDeeper is for doing recursive walks of expressions in composites that we handle.
function commonVisitors() {
return {
Identifier: (node, st) => {
if (objectPrototypeProperties.includes(node.name)) {return;}
addRef(st.key, node.name);
},
WhileStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceWHILE);
node.test && walkDeeper(node.test, st);
node.body && walkDeeper(node.body, st);
},
DoWhileStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceWHILE);
node.test && walkDeeper(node.test, st);
node.body && walkDeeper(node.body, st);
},
ForStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceFOR);
node.init && walkDeeper(node.init, st);
node.test && walkDeeper(node.test, st);
node.update && walkDeeper(node.update, st);
node.body && walkDeeper(node.body, st);
},
IfStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceIF);
node.test && walkDeeper(node.test, st);
node.consequent && walkDeeper(node.consequent, st);
node.alternate && walkDeeper(node.alternate, st);
},
MemberExpression: (node, st, walkDeeper) => {
node.object && walkDeeper(node.object, st);
node.property && walkDeeper(node.property, st);
},
// If we discover a dependency identifier, state.key is the dependent identifier.
// walkDeeper is for doing recursive walks of expressions in composites that we handle.
function commonVisitors() {
return {
Identifier: (node, st) => {
if (objectPrototypeProperties.includes(node.name)) {
return;
}
}
addRef(st.key, node.name);
},
WhileStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceWHILE);
node.test && walkDeeper(node.test, st);
node.body && walkDeeper(node.body, st);
},
DoWhileStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceWHILE);
node.test && walkDeeper(node.test, st);
node.body && walkDeeper(node.body, st);
},
ForStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceFOR);
node.init && walkDeeper(node.init, st);
node.test && walkDeeper(node.test, st);
node.update && walkDeeper(node.update, st);
node.body && walkDeeper(node.body, st);
},
IfStatement: (node, st, walkDeeper) => {
addRef(st.key, specialReferenceIF);
node.test && walkDeeper(node.test, st);
node.consequent && walkDeeper(node.consequent, st);
node.alternate && walkDeeper(node.alternate, st);
},
MemberExpression: (node, st, walkDeeper) => {
node.object && walkDeeper(node.object, st);
node.property && walkDeeper(node.property, st);
},
};
}
walk.recursive(ast, {key: globalKey}, Object.assign({
walk.recursive(
ast,
{ key: globalKey },
Object.assign(
{
ImportDeclaration: (node, st) => {
const importModuleName = node.source.value;
additionalModules.push(importModuleName);
const importModuleName = node.source.value;
additionalModules.push(importModuleName);
// This module's global scope refers to that module's global scope, no matter how we
// import it.
dependencyMap[st.key].add(importModuleName + memCheckGlobalKey);
// This module's global scope refers to that module's global scope, no matter how we
// import it.
dependencyMap[st.key].add(importModuleName + memCheckGlobalKey);
for (let i = 0; i < node.specifiers.length; ++i) {
const spec = node.specifiers[i];
if (spec.imported !== undefined && spec.local !== undefined) {
// We depend on specific things.
internalToExternal[spec.local.name] = importModuleName + "." + spec.imported.name;
} else {
// We depend on everything.
dependencyMap[st.key].add(importModuleName + ".*");
}
for (let i = 0; i < node.specifiers.length; ++i) {
const spec = node.specifiers[i];
if (spec.imported !== undefined && spec.local !== undefined) {
// We depend on specific things.
internalToExternal[spec.local.name] =
importModuleName + "." + spec.imported.name;
} else {
// We depend on everything.
dependencyMap[st.key].add(importModuleName + ".*");
}
}
},
FunctionDeclaration: (node) => {
const key = currentModule + "." + node.id.name;
walk.recursive(node, {key: key}, commonVisitors());
const key = currentModule + "." + node.id.name;
walk.recursive(node, { key: key }, commonVisitors());
},
}, commonVisitors()));
},
commonVisitors(),
),
);
return {dependencyMap: dependencyMap, additionalModules: additionalModules};
return { dependencyMap: dependencyMap, additionalModules: additionalModules };
}
/**
@@ -318,22 +342,22 @@ function parseOnlyCalculateDeps(code, currentModule) {
* Used to account for imported scripts
*/
export async function calculateRamUsage(codeCopy, otherScripts) {
// We don't need a real WorkerScript for this. Just an object that keeps
// track of whatever's needed for RAM calculations
const workerScript = {
loadedFns: {},
env: {
vars: RamCosts,
},
}
try {
return await parseOnlyRamCalculate(otherScripts, codeCopy, workerScript);
} catch (e) {
console.error(`Failed to parse script for RAM calculations:`);
console.error(e);
return RamCalculationErrorCode.SyntaxError;
}
// We don't need a real WorkerScript for this. Just an object that keeps
// track of whatever's needed for RAM calculations
const workerScript = {
loadedFns: {},
env: {
vars: RamCosts,
},
};
try {
return await parseOnlyRamCalculate(otherScripts, codeCopy, workerScript);
} catch (e) {
console.error(`Failed to parse script for RAM calculations:`);
console.error(e);
return RamCalculationErrorCode.SyntaxError;
}
return RamCalculationErrorCode.SyntaxError;
}
+98 -88
View File
@@ -9,128 +9,138 @@ import { IMap } from "../types";
import { post } from "../ui/postToTerminal";
import {
Generic_fromJSON,
Generic_toJSON,
Reviver,
Generic_fromJSON,
Generic_toJSON,
Reviver,
} from "../../utils/JSONReviver";
import { getTimestamp } from "../../utils/helpers/getTimestamp";
export class RunningScript {
// Script arguments
args: any[] = [];
// Script arguments
args: any[] = [];
// Map of [key: server ip] -> Hacking data. Used for offline progress calculations.
// Hacking data format: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
dataMap: IMap<number[]> = {};
// Map of [key: server ip] -> Hacking data. Used for offline progress calculations.
// Hacking data format: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
dataMap: IMap<number[]> = {};
// Script filename
filename = "";
// Script filename
filename = "";
// This script's logs. An array of log entries
logs: string[] = [];
// This script's logs. An array of log entries
logs: string[] = [];
// Flag indicating whether the logs have been updated since
// the last time the UI was updated
logUpd = false;
// Flag indicating whether the logs have been updated since
// the last time the UI was updated
logUpd = false;
// Total amount of hacking experience earned from this script when offline
offlineExpGained = 0;
// Total amount of hacking experience earned from this script when offline
offlineExpGained = 0;
// Total amount of money made by this script when offline
offlineMoneyMade = 0;
// Total amount of money made by this script when offline
offlineMoneyMade = 0;
// Number of seconds that the script has been running offline
offlineRunningTime = 0.01;
// Number of seconds that the script has been running offline
offlineRunningTime = 0.01;
// Total amount of hacking experience earned from this script when online
onlineExpGained = 0;
// Total amount of hacking experience earned from this script when online
onlineExpGained = 0;
// Total amount of money made by this script when online
onlineMoneyMade = 0;
// Total amount of money made by this script when online
onlineMoneyMade = 0;
// Number of seconds that this script has been running online
onlineRunningTime = 0.01;
// Number of seconds that this script has been running online
onlineRunningTime = 0.01;
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript
pid = -1;
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript
pid = -1;
// How much RAM this script uses for ONE thread
ramUsage = 0;
// How much RAM this script uses for ONE thread
ramUsage = 0;
// IP of the server on which this script is running
server = "";
// IP of the server on which this script is running
server = "";
// Number of threads that this script is running with
threads = 1;
// Number of threads that this script is running with
threads = 1;
constructor(script: Script | null = null, args: any[] = []) {
if (script == null) {
return;
}
this.filename = script.filename;
this.args = args;
this.server = script.server;
this.ramUsage = script.ramUsage;
}
constructor(script: Script | null = null, args: any[] = []) {
if (script == null) { return; }
this.filename = script.filename;
this.args = args;
this.server = script.server;
this.ramUsage = script.ramUsage;
log(txt: string): void {
if (this.logs.length > Settings.MaxLogCapacity) {
this.logs.shift();
}
log(txt: string): void {
if (this.logs.length > Settings.MaxLogCapacity) {
this.logs.shift();
}
let logEntry = txt;
if (FconfSettings.ENABLE_TIMESTAMPS) {
logEntry = "[" + getTimestamp() + "] " + logEntry;
}
this.logs.push(logEntry);
this.logUpd = true;
let logEntry = txt;
if (FconfSettings.ENABLE_TIMESTAMPS) {
logEntry = "[" + getTimestamp() + "] " + logEntry;
}
displayLog(): void {
for (let i = 0; i < this.logs.length; ++i) {
post(this.logs[i]);
}
}
this.logs.push(logEntry);
this.logUpd = true;
}
clearLog(): void {
this.logs.length = 0;
displayLog(): void {
for (let i = 0; i < this.logs.length; ++i) {
post(this.logs[i]);
}
}
// Update the moneyStolen and numTimesHack maps when hacking
recordHack(serverIp: string, moneyGained: number, n=1): void {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][0] += moneyGained;
this.dataMap[serverIp][1] += n;
}
clearLog(): void {
this.logs.length = 0;
}
// Update the grow map when calling grow()
recordGrow(serverIp: string, n=1): void {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][2] += n;
// Update the moneyStolen and numTimesHack maps when hacking
recordHack(serverIp: string, moneyGained: number, n = 1): void {
if (
this.dataMap[serverIp] == null ||
this.dataMap[serverIp].constructor !== Array
) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][0] += moneyGained;
this.dataMap[serverIp][1] += n;
}
// Update the weaken map when calling weaken() {
recordWeaken(serverIp: string, n=1): void {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][3] += n;
// Update the grow map when calling grow()
recordGrow(serverIp: string, n = 1): void {
if (
this.dataMap[serverIp] == null ||
this.dataMap[serverIp].constructor !== Array
) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][2] += n;
}
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("RunningScript", this);
// Update the weaken map when calling weaken() {
recordWeaken(serverIp: string, n = 1): void {
if (
this.dataMap[serverIp] == null ||
this.dataMap[serverIp].constructor !== Array
) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][3] += n;
}
// Initializes a RunningScript Object from a JSON save state
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): RunningScript {
return Generic_fromJSON(RunningScript, value.data);
}
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("RunningScript", this);
}
// Initializes a RunningScript Object from a JSON save state
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): RunningScript {
return Generic_fromJSON(RunningScript, value.data);
}
}
Reviver.constructors.RunningScript = RunningScript;
+15 -13
View File
@@ -2,19 +2,21 @@ import { AllServers } from "../Server/AllServers";
import { RunningScript } from "./RunningScript";
export function getRamUsageFromRunningScript(script: RunningScript): number {
if (script.ramUsage != null && script.ramUsage > 0) {
return script.ramUsage; // Use cached value
}
const server = AllServers[script.server];
if (server == null) { return 0; }
for (let i = 0; i < server.scripts.length; ++i) {
if (server.scripts[i].filename === script.filename) {
// Cache the ram usage for the next call
script.ramUsage = server.scripts[i].ramUsage;
return script.ramUsage;
}
}
if (script.ramUsage != null && script.ramUsage > 0) {
return script.ramUsage; // Use cached value
}
const server = AllServers[script.server];
if (server == null) {
return 0;
}
for (let i = 0; i < server.scripts.length; ++i) {
if (server.scripts[i].filename === script.filename) {
// Cache the ram usage for the next call
script.ramUsage = server.scripts[i].ramUsage;
return script.ramUsage;
}
}
return 0;
}
+103 -98
View File
@@ -10,127 +10,132 @@ import { Page, routing } from "../ui/navigationTracking";
import { setTimeoutRef } from "../utils/SetTimeoutRef";
import {
Generic_fromJSON,
Generic_toJSON,
Reviver,
Generic_fromJSON,
Generic_toJSON,
Reviver,
} from "../../utils/JSONReviver";
import { roundToTwo } from "../../utils/helpers/roundToTwo";
let globalModuleSequenceNumber = 0;
export class Script {
// Code for this script
code = "";
// Code for this script
code = "";
// Filename for the script file
filename = "";
// Filename for the script file
filename = "";
// url of the script if any, only for NS2.
url = "";
// 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 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;
// 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[] = [];
// 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;
// Amount of RAM this Script requres to run
ramUsage = 0;
// IP of server that this script is on.
server = "";
// IP of server that this script is on.
server = "";
constructor(fn="", code="", server="", otherScripts: Script[]=[]) {
this.filename = fn;
this.code = code;
this.ramUsage = 0;
this.server = server; // IP of server this script is on
this.module = "";
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
if (this.code !== "") { this.updateRamUsage(otherScripts); }
constructor(fn = "", code = "", server = "", otherScripts: Script[] = []) {
this.filename = fn;
this.code = code;
this.ramUsage = 0;
this.server = server; // IP of server this script is on
this.module = "";
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
if (this.code !== "") {
this.updateRamUsage(otherScripts);
}
}
/**
* Download the script as a file
*/
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
const 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);
}
/**
* Download the script as a file
*/
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
const 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);
}
}
/**
* 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;
/**
* 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;
}
/**
* 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
*/
saveScript(code: string, serverIp: string, otherScripts: Script[]): void {
if (routing.isOn(Page.ScriptEditor)) {
// Update code and filename
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;
this.server = serverIp;
this.updateRamUsage(otherScripts);
this.markUpdated();
}
}
/**
* 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
*/
saveScript(code: string, serverIp: string, otherScripts: Script[]): void {
if (routing.isOn(Page.ScriptEditor)) {
// Update code and filename
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;
this.server = serverIp;
this.updateRamUsage(otherScripts);
this.markUpdated();
}
/**
* 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);
if (res > 0) {
this.ramUsage = roundToTwo(res);
}
}
/**
* 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);
if (res > 0) {
this.ramUsage = roundToTwo(res);
}
}
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("Script", this);
}
// 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 {
return Generic_fromJSON(Script, value.data);
}
// Initializes a Script Object from a JSON save state
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): Script {
return Generic_fromJSON(Script, value.data);
}
}
Reviver.constructors.Script = Script;
+100 -57
View File
@@ -4,13 +4,13 @@ import { RamCalculationErrorCode } from "./RamCalculationErrorCodes";
import { calculateRamUsage } from "./RamCalculations";
import { isScriptFilename } from "./ScriptHelpersTS";
import {CONSTANTS} from "../Constants";
import {Engine} from "../engine";
import { CONSTANTS } from "../Constants";
import { Engine } from "../engine";
import { parseFconfSettings } from "../Fconf/Fconf";
import {
iTutorialSteps,
iTutorialNextStep,
ITutorial,
iTutorialSteps,
iTutorialNextStep,
ITutorial,
} from "../InteractiveTutorial";
import { Player } from "../Player";
import { CursorPositions } from "../ScriptEditor/CursorPositions";
@@ -29,76 +29,119 @@ import { compareArrays } from "../../utils/helpers/compareArrays";
import { createElement } from "../../utils/uiHelpers/createElement";
export function scriptCalculateOfflineProduction(runningScriptObj) {
//The Player object stores the last update time from when we were online
const thisUpdate = new Date().getTime();
const lastUpdate = Player.lastUpdate;
const timePassed = (thisUpdate - lastUpdate) / 1000; //Seconds
//The Player object stores the last update time from when we were online
const thisUpdate = new Date().getTime();
const lastUpdate = Player.lastUpdate;
const timePassed = (thisUpdate - lastUpdate) / 1000; //Seconds
//Calculate the "confidence" rating of the script's true production. This is based
//entirely off of time. We will arbitrarily say that if a script has been running for
//4 hours (14400 sec) then we are completely confident in its ability
let confidence = (runningScriptObj.onlineRunningTime) / 14400;
if (confidence >= 1) {confidence = 1;}
//Calculate the "confidence" rating of the script's true production. This is based
//entirely off of time. We will arbitrarily say that if a script has been running for
//4 hours (14400 sec) then we are completely confident in its ability
let confidence = runningScriptObj.onlineRunningTime / 14400;
if (confidence >= 1) {
confidence = 1;
}
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
// Grow
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (runningScriptObj.dataMap[ip][2] == 0 || runningScriptObj.dataMap[ip][2] == null) {continue;}
const serv = AllServers[ip];
if (serv == null) {continue;}
const timesGrown = Math.round(0.5 * runningScriptObj.dataMap[ip][2] / runningScriptObj.onlineRunningTime * timePassed);
runningScriptObj.log(`Called on ${serv.hostname} ${timesGrown} times while offline`);
const host = AllServers[runningScriptObj.server];
const growth = processSingleServerGrowth(serv, timesGrown, Player, host.cpuCores);
runningScriptObj.log(`'${serv.hostname}' grown by ${numeralWrapper.format(growth * 100 - 100, '0.000000%')} while offline`);
}
// Grow
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (
runningScriptObj.dataMap[ip][2] == 0 ||
runningScriptObj.dataMap[ip][2] == null
) {
continue;
}
const serv = AllServers[ip];
if (serv == null) {
continue;
}
const timesGrown = Math.round(
((0.5 * runningScriptObj.dataMap[ip][2]) /
runningScriptObj.onlineRunningTime) *
timePassed,
);
runningScriptObj.log(
`Called on ${serv.hostname} ${timesGrown} times while offline`,
);
const host = AllServers[runningScriptObj.server];
const growth = processSingleServerGrowth(
serv,
timesGrown,
Player,
host.cpuCores,
);
runningScriptObj.log(
`'${serv.hostname}' grown by ${numeralWrapper.format(
growth * 100 - 100,
"0.000000%",
)} while offline`,
);
}
}
// Offline EXP gain
// A script's offline production will always be at most half of its online production.
const expGain = confidence * (runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) * timePassed;
Player.gainHackingExp(expGain);
// Offline EXP gain
// A script's offline production will always be at most half of its online production.
const expGain =
confidence *
(runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) *
timePassed;
Player.gainHackingExp(expGain);
// Update script stats
runningScriptObj.offlineRunningTime += timePassed;
runningScriptObj.offlineExpGained += expGain;
// Update script stats
runningScriptObj.offlineRunningTime += timePassed;
runningScriptObj.offlineExpGained += expGain;
// Weaken
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (runningScriptObj.dataMap[ip][3] == 0 || runningScriptObj.dataMap[ip][3] == null) {continue;}
const serv = AllServers[ip];
if (serv == null) {continue;}
const host = AllServers[runningScriptObj.server];
const timesWeakened = Math.round(0.5 * runningScriptObj.dataMap[ip][3] / runningScriptObj.onlineRunningTime * timePassed);
runningScriptObj.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
const coreBonus = 1+(host.cpuCores-1)/16;
serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened * coreBonus);
}
// Weaken
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (
runningScriptObj.dataMap[ip][3] == 0 ||
runningScriptObj.dataMap[ip][3] == null
) {
continue;
}
const serv = AllServers[ip];
if (serv == null) {
continue;
}
const host = AllServers[runningScriptObj.server];
const timesWeakened = Math.round(
((0.5 * runningScriptObj.dataMap[ip][3]) /
runningScriptObj.onlineRunningTime) *
timePassed,
);
runningScriptObj.log(
`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`,
);
const coreBonus = 1 + (host.cpuCores - 1) / 16;
serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened * coreBonus);
}
}
}
//Returns a RunningScript object matching the filename and arguments on the
//designated server, and false otherwise
export function findRunningScript(filename, args, server) {
for (var i = 0; i < server.runningScripts.length; ++i) {
if (server.runningScripts[i].filename === filename &&
compareArrays(server.runningScripts[i].args, args)) {
return server.runningScripts[i];
}
for (var i = 0; i < server.runningScripts.length; ++i) {
if (
server.runningScripts[i].filename === filename &&
compareArrays(server.runningScripts[i].args, args)
) {
return server.runningScripts[i];
}
return null;
}
return null;
}
//Returns a RunningScript object matching the pid on the
//designated server, and false otherwise
export function findRunningScriptByPid(pid, server) {
for (var i = 0; i < server.runningScripts.length; ++i) {
if (server.runningScripts[i].pid === pid) {
return server.runningScripts[i];
}
for (var i = 0; i < server.runningScripts.length; ++i) {
if (server.runningScripts[i].pid === pid) {
return server.runningScripts[i];
}
return null;
}
return null;
}
+1 -1
View File
@@ -1,4 +1,4 @@
// Script helper functions
export function isScriptFilename(f: string): boolean {
return f.endsWith(".js") || f.endsWith(".script") || f.endsWith(".ns");
return f.endsWith(".js") || f.endsWith(".script") || f.endsWith(".ns");
}
+6 -6
View File
@@ -1,9 +1,9 @@
export class ScriptUrl {
filename: string;
url: string;
filename: string;
url: string;
constructor(filename: string, url: string) {
this.filename = filename;
this.url = url;
}
constructor(filename: string, url: string) {
this.filename = filename;
this.url = url;
}
}