fix sleeve memory bug

This commit is contained in:
Olivier Gagnon
2021-09-08 23:47:34 -04:00
parent bada8a5f39
commit 2a13db39c7
360 changed files with 5424 additions and 15764 deletions
+1 -4
View File
@@ -1,6 +1,3 @@
import { Script } from "./Script";
export declare function calculateRamUsage(
codeCopy: string,
otherScripts: Script[],
): number;
export declare function calculateRamUsage(codeCopy: string, otherScripts: Script[]): number;
+7 -19
View File
@@ -76,10 +76,7 @@ async function parseOnlyRamCalculate(otherScripts, code, workerScript) {
// 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://")
) {
if (nextModule.startsWith("https://") || nextModule.startsWith("http://")) {
try {
// eslint-disable-next-line no-await-in-loop
const module = await eval("import(nextModule)");
@@ -90,16 +87,12 @@ async function parseOnlyRamCalculate(otherScripts, code, workerScript) {
}
}
} catch (e) {
console.error(
`Error dynamically importing module from ${nextModule} for RAM calculations: ${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`,
);
console.warn(`parseOnlyRamCalculate() not called with array of scripts`);
return RamCalculationErrorCode.ImportError;
}
@@ -125,8 +118,7 @@ async function parseOnlyRamCalculate(otherScripts, code, workerScript) {
// 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 unresolvedRefs = Object.keys(dependencyMap).filter((s) => s.startsWith(initialModule));
const resolvedRefs = new Set();
while (unresolvedRefs.length > 0) {
const ref = unresolvedRefs.shift();
@@ -147,8 +139,7 @@ async function parseOnlyRamCalculate(otherScripts, code, workerScript) {
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 ident of Object.keys(dependencyMap).filter((k) => k.startsWith(prefix))) {
for (let dep of dependencyMap[ident] || []) {
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
}
@@ -252,9 +243,7 @@ function parseOnlyCalculateDeps(code, currentModule) {
}
//A list of identifiers that resolve to "native Javascript code"
const objectPrototypeProperties = Object.getOwnPropertyNames(
Object.prototype,
);
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.
@@ -313,8 +302,7 @@ function parseOnlyCalculateDeps(code, currentModule) {
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;
internalToExternal[spec.local.name] = importModuleName + "." + spec.imported.name;
} else {
// We depend on everything.
dependencyMap[st.key].add(importModuleName + ".*");
+4 -17
View File
@@ -8,11 +8,7 @@ import { Settings } from "../Settings/Settings";
import { IMap } from "../types";
import { post } from "../ui/postToTerminal";
import {
Generic_fromJSON,
Generic_toJSON,
Reviver,
} from "../../utils/JSONReviver";
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../../utils/JSONReviver";
import { getTimestamp } from "../../utils/helpers/getTimestamp";
export class RunningScript {
@@ -99,10 +95,7 @@ export class RunningScript {
// 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
) {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][0] += moneyGained;
@@ -111,10 +104,7 @@ export class RunningScript {
// Update the grow map when calling grow()
recordGrow(serverIp: string, n = 1): void {
if (
this.dataMap[serverIp] == null ||
this.dataMap[serverIp].constructor !== Array
) {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][2] += n;
@@ -122,10 +112,7 @@ export class RunningScript {
// Update the weaken map when calling weaken() {
recordWeaken(serverIp: string, n = 1): void {
if (
this.dataMap[serverIp] == null ||
this.dataMap[serverIp].constructor !== Array
) {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][3] += n;
+1 -5
View File
@@ -9,11 +9,7 @@ import { ScriptUrl } from "./ScriptUrl";
import { Page, routing } from "../ui/navigationTracking";
import { setTimeoutRef } from "../utils/SetTimeoutRef";
import {
Generic_fromJSON,
Generic_toJSON,
Reviver,
} from "../../utils/JSONReviver";
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../../utils/JSONReviver";
import { roundToTwo } from "../../utils/helpers/roundToTwo";
let globalModuleSequenceNumber = 0;
+10 -38
View File
@@ -26,10 +26,7 @@ export function scriptCalculateOfflineProduction(runningScriptObj) {
// Grow
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (
runningScriptObj.dataMap[ip][2] == 0 ||
runningScriptObj.dataMap[ip][2] == null
) {
if (runningScriptObj.dataMap[ip][2] == 0 || runningScriptObj.dataMap[ip][2] == null) {
continue;
}
const serv = AllServers[ip];
@@ -37,35 +34,20 @@ export function scriptCalculateOfflineProduction(runningScriptObj) {
continue;
}
const timesGrown = Math.round(
((0.5 * runningScriptObj.dataMap[ip][2]) /
runningScriptObj.onlineRunningTime) *
timePassed,
);
runningScriptObj.log(
`Called on ${serv.hostname} ${timesGrown} times while offline`,
((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,
);
const growth = processSingleServerGrowth(serv, timesGrown, Player, host.cpuCores);
runningScriptObj.log(
`'${serv.hostname}' grown by ${numeralWrapper.format(
growth * 100 - 100,
"0.000000%",
)} while offline`,
`'${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;
const expGain = confidence * (runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) * timePassed;
Player.gainHackingExp(expGain);
// Update script stats
@@ -75,10 +57,7 @@ export function scriptCalculateOfflineProduction(runningScriptObj) {
// Weaken
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (
runningScriptObj.dataMap[ip][3] == 0 ||
runningScriptObj.dataMap[ip][3] == null
) {
if (runningScriptObj.dataMap[ip][3] == 0 || runningScriptObj.dataMap[ip][3] == null) {
continue;
}
const serv = AllServers[ip];
@@ -87,13 +66,9 @@ export function scriptCalculateOfflineProduction(runningScriptObj) {
}
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`,
((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);
}
@@ -104,10 +79,7 @@ export function scriptCalculateOfflineProduction(runningScriptObj) {
//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)
) {
if (server.runningScripts[i].filename === filename && compareArrays(server.runningScripts[i].args, args)) {
return server.runningScripts[i];
}
}