crash now prints ns stack trace, workerscript now does the shouldLog check on its own, many ns function are way simpler.

This commit is contained in:
Olivier Gagnon 2021-03-11 03:02:05 -05:00
parent e2a353fc8a
commit cd972dabe3
12 changed files with 853 additions and 1637 deletions

@ -3827,43 +3827,6 @@ Bladeburner.prototype.setTeamSizeNetscriptFn = function(type, name, size, worker
return sanitizedSize;
}
Bladeburner.prototype.getCityEstimatedPopulationNetscriptFn = function(cityName, workerScript) {
if (!this.cities.hasOwnProperty(cityName)) {
workerScript.log("ERROR: bladeburner.getCityEstimatedPopulation() failed because the specified " +
"city was invalid: " + cityName + ". Note that this city argument is case-sensitive");
return -1;
}
return this.cities[cityName].popEst;
}
Bladeburner.prototype.getCityEstimatedCommunitiesNetscriptFn = function(cityName, workerScript) {
if (!this.cities.hasOwnProperty(cityName)) {
workerScript.log("ERROR: bladeburner.getCityEstimatedCommunities() failed because the specified " +
"city was invalid: " + cityName + ". Note that this city argument is case-sensitive");
return -1;
}
return this.cities[cityName].commsEst;
}
Bladeburner.prototype.getCityChaosNetscriptFn = function(cityName, workerScript) {
if (!this.cities.hasOwnProperty(cityName)) {
workerScript.log("ERROR: bladeburner.getCityChaos() failed because the specified " +
"city was invalid: " + cityName + ". Note that this city argument is case-sensitive");
return -1;
}
return this.cities[cityName].chaos;
}
Bladeburner.prototype.switchCityNetscriptFn = function(cityName, workerScript) {
if (!this.cities.hasOwnProperty(cityName)) {
workerScript.log("ERROR: bladeburner.switchCity() failed because the specified " +
"city was invalid: " + cityName + ". Note that this city argument is case-sensitive");
return false;
}
this.city = cityName;
return true;
}
Bladeburner.prototype.joinBladeburnerFactionNetscriptFn = function(workerScript) {
var bladeburnerFac = Factions["Bladeburners"];
if (bladeburnerFac.isMember) {

@ -188,7 +188,17 @@ export class WorkerScript {
return (this.disableLogs.ALL == null && this.disableLogs[fn] == null);
}
log(txt: string): void {
log(func: string, txt: string): void {
if(this.shouldLog(func)) {
if(func !== "" && txt !== ""){
this.scriptRef.log(`${func}: ${txt}`);
} else {
this.scriptRef.log(func+txt);
}
}
}
print(txt: string): void {
this.scriptRef.log(txt);
}
}

@ -12,15 +12,4 @@ function unknownBladeburnerExceptionMessage(functionName, err) {
return `bladeburner.${functionName}() failed with exception: ` + err;
}
function checkBladeburnerAccess(workerScript, functionName) {
const accessDenied = `${functionName}() failed because you do not ` +
"currently have access to the Bladeburner API. To access the Bladeburner API" +
"you must be employed at the Bladeburner division, AND you must either be in " +
"BitNode-7 or have Source-File 7.";
const hasAccess = Player.bladeburner instanceof Bladeburner && (Player.bitNodeN === 7 || Player.sourceFiles.some(a=>{return a.n === 7}));
if(!hasAccess) {
throw makeRuntimeRejectMsg(workerScript, accessDenied);
}
}
export {unknownBladeburnerActionErrorMessage, unknownBladeburnerExceptionMessage, checkBladeburnerAccess};

@ -2,6 +2,7 @@ import { setTimeoutRef } from "./utils/SetTimeoutRef";
import { isValidIPAddress } from "../utils/helpers/isValidIPAddress";
import { isString } from "../utils/helpers/isString";
import { AllServers } from "./Server/AllServers";
export function netscriptDelay(time, workerScript) {
return new Promise(function(resolve, reject) {
@ -19,7 +20,12 @@ export function makeRuntimeRejectMsg(workerScript, msg, exp=null) {
var num = getErrorLineNumber(exp, workerScript);
lineNum = " (Line " + num + ")"
}
return "|"+workerScript.serverIp+"|"+workerScript.name+"|" + msg + lineNum;
const server = AllServers[workerScript.serverIp];
if (server == null) {
throw new Error(`WorkerScript constructed with invalid server ip: ${this.serverIp}`);
}
return "|"+server.hostname+"|"+workerScript.name+"|" + msg + lineNum;
}
export function resolveNetscriptRequestedThreads(workerScript, functionName, requestedThreads) {
@ -57,8 +63,5 @@ export function isScriptErrorMessage(msg) {
return false;
}
var ip = splitMsg[1];
if (!isValidIPAddress(ip)) {
return false;
}
return true;
}

File diff suppressed because it is too large Load Diff

@ -6,10 +6,3 @@ export function unknownGangApiExceptionMessage(functionName, err) {
return `gang.${functionName}() failed with exception: ` + err;
}
export function checkGangApiAccess(workerScript, functionName) {
const accessDenied = `gang.${functionName}() failed because you do not currently have a Gang`;
const hasAccess = Player.gang instanceof Gang;
if (!hasAccess) {
throw makeRuntimeRejectMsg(workerScript, accessDenied);
}
}

@ -1,21 +1,12 @@
import { makeRuntimeRejectMsg } from "./NetscriptEvaluator";
import { Script } from "./Script/Script";
import { ScriptUrl } from "./Script/ScriptUrl";
// Makes a blob that contains the code of a given script.
export function makeScriptBlob(code) {
return new Blob([code], {type: "text/javascript"});
}
class ScriptUrl {
/**
* @param {string} filename
* @param {string} url
*/
constructor(filename, url) {
this.filename = filename;
this.url = url;
}
}
// Begin executing a user JS script, and return a promise that resolves
// or rejects when the script finishes.
@ -40,7 +31,7 @@ export async function executeJSScript(scripts = [], workerScript) {
urls = _getScriptUrls(script, scripts, []);
script.url = urls[urls.length - 1].url;
script.module = new Promise(resolve => resolve(eval('import(urls[urls.length - 1].url)')));
script.dependencies = urls.map(u => u.filename);
script.dependencies = urls;
}
loadedModule = await script.module;
@ -69,7 +60,7 @@ export async function executeJSScript(scripts = [], workerScript) {
function shouldCompile(script, scripts) {
if (script.module === "") return true;
return script.dependencies.some(dep => {
const depScript = scripts.find(s => s.filename == dep);
const depScript = scripts.find(s => s.filename == dep.url);
// If the script is not present on the server, we should recompile, if only to get any necessary
// compilation errors.

@ -501,7 +501,7 @@ export function createAndAddWorkerScript(runningScriptObj, server) {
if (!w.running) { return; }
killWorkerScript(s);
w.log("Script finished running");
w.log("", "Script finished running");
}).catch(function(w) {
if (w instanceof Error) {
dialogBoxCreate("Script runtime unknown error. This is a bug please contact game developer");
@ -509,22 +509,27 @@ export function createAndAddWorkerScript(runningScriptObj, server) {
return;
} else if (w instanceof WorkerScript) {
if (isScriptErrorMessage(w.errorMessage)) {
var errorTextArray = w.errorMessage.split("|");
const errorTextArray = w.errorMessage.split("|");
if (errorTextArray.length != 4) {
console.error("ERROR: Something wrong with Error text in evaluator...");
console.error("Error text: " + errorText);
return;
}
var serverIp = errorTextArray[1];
var scriptName = errorTextArray[2];
var errorMsg = errorTextArray[3];
const serverIp = errorTextArray[1];
const scriptName = errorTextArray[2];
const errorMsg = errorTextArray[3];
dialogBoxCreate("Script runtime error: <br>Server Ip: " + serverIp +
"<br>Script name: " + scriptName +
"<br>Args:" + arrayToString(w.args) + "<br>" + errorMsg);
w.log("Script crashed with runtime error");
let msg = `RUNTIME ERROR<br>${scriptName}@${serverIp}<br>`
if (w.args.length > 0) {
msg += `Args: ${arrayToString(w.args)}<br>`
}
msg += "<br>";
msg += errorMsg;
dialogBoxCreate(msg);
w.log("", "Script crashed with runtime error");
} else {
w.log("Script killed");
w.log("", "Script killed");
return; // Already killed, so stop here
}
w.running = false;
@ -594,14 +599,14 @@ export function loadAllRunningScripts() {
/**
* Run a script from inside another script (run(), exec(), spawn(), etc.)
*/
export function runScriptFromScript(server, scriptname, args, workerScript, threads=1) {
export function runScriptFromScript(caller, server, scriptname, args, workerScript, threads=1) {
// Sanitize arguments
if (!(workerScript instanceof WorkerScript)) {
return 0;
}
if (typeof scriptname !== "string" || !Array.isArray(args)) {
workerScript.log(`ERROR: runScriptFromScript() failed due to invalid arguments`);
workerScript.log(caller, `Invalid arguments: scriptname='${scriptname} args='${ags}'`);
console.error(`runScriptFromScript() failed due to invalid arguments`);
return 0;
}
@ -609,14 +614,14 @@ export function runScriptFromScript(server, scriptname, args, workerScript, thre
// Check if the script is already running
let runningScriptObj = server.getRunningScript(scriptname, args);
if (runningScriptObj != null) {
workerScript.log(`${scriptname} is already running on ${server.hostname}`);
workerScript.log(caller, `'${scriptname}' is already running on '${server.hostname}'`);
return 0;
}
// 'null/undefined' arguments are not allowed
for (let i = 0; i < args.length; ++i) {
if (args[i] == null) {
workerScript.log("ERROR: Cannot execute a script with null/undefined as an argument");
workerScript.log(caller, "Cannot execute a script with null/undefined as an argument");
return 0;
}
}
@ -633,16 +638,14 @@ export function runScriptFromScript(server, scriptname, args, workerScript, thre
const ramAvailable = server.maxRam - server.ramUsed;
if (server.hasAdminRights == false) {
workerScript.log(`Cannot run script ${scriptname} on ${server.hostname} because you do not have root access!`);
workerScript.log(caller, `You do not have root access on '${server.hostname}'`);
return 0;
} else if (ramUsage > ramAvailable){
workerScript.log(`Cannot run script ${scriptname} (t=${threads}) on ${server.hostname} because there is not enough available RAM!`);
workerScript.log(caller, `Cannot run script '${scriptname}' (t=${threads}) on '${server.hostname}' because there is not enough available RAM!`);
return 0;
} else {
// Able to run script
if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.exec == null && workerScript.disableLogs.run == null && workerScript.disableLogs.spawn == null) {
workerScript.log(`Running script: ${scriptname} on ${server.hostname} with ${threads} threads and args: ${arrayToString(args)}.`);
}
workerScript.log(caller, `'${scriptname}' on '${server.hostname}' with ${threads} threads and args: ${arrayToString(args)}.`);
let runningScriptObj = new RunningScript(script, args);
runningScriptObj.threads = threads;
@ -651,6 +654,6 @@ export function runScriptFromScript(server, scriptname, args, workerScript, thre
}
}
workerScript.log(`Could not find script ${scriptname} on ${server.hostname}`);
workerScript.log(caller, `Could not find script '${scriptname}' on '${server.hostname}'`);
return 0;
}

@ -5,6 +5,7 @@
* being evaluated. See RunningScript for that
*/
import { calculateRamUsage } from "./RamCalculations";
import { ScriptUrl } from "./ScriptUrl";
import { Page, routing } from "../ui/navigationTracking";
import { setTimeoutRef } from "../utils/SetTimeoutRef";
@ -42,7 +43,7 @@ export class Script {
// 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[] = [];
dependencies: ScriptUrl[] = [];
// Amount of RAM this Script requres to run
ramUsage: number = 0;

9
src/Script/ScriptUrl.ts Normal file

@ -0,0 +1,9 @@
export class ScriptUrl {
filename: string;
url: string;
constructor(filename: string, url: string) {
this.filename = filename;
this.url = url;
}
}

@ -43,7 +43,7 @@ export function buyStock(stock: Stock, shares: number, workerScript: WorkerScrip
if (shares <= 0) { return false; }
if (stock == null || isNaN(shares)) {
if (tixApi) {
workerScript!.log(`ERROR: buyStock() failed due to invalid arguments`);
workerScript!.log("buyStock", `Invalid arguments: stock='${stock}' shares='${shares}'`);
} else if (opts.suppressDialog !== true) {
dialogBoxCreate("Failed to buy stock. This may be a bug, contact developer");
}
@ -56,7 +56,7 @@ export function buyStock(stock: Stock, shares: number, workerScript: WorkerScrip
if (totalPrice == null) { return false; }
if (Player.money.lt(totalPrice)) {
if (tixApi) {
workerScript!.log(`ERROR: buyStock() failed because you do not have enough money to purchase this position. You need ${numeralWrapper.formatMoney(totalPrice)}`);
workerScript!.log("buyStock", `You do not have enough money to purchase this position. You need ${numeralWrapper.formatMoney(totalPrice)}.`);
} else if (opts.suppressDialog !== true) {
dialogBoxCreate(`You do not have enough money to purchase this. You need ${numeralWrapper.formatMoney(totalPrice)}`);
}
@ -67,7 +67,7 @@ export function buyStock(stock: Stock, shares: number, workerScript: WorkerScrip
// Would this purchase exceed the maximum number of shares?
if (shares + stock.playerShares + stock.playerShortShares > stock.maxShares) {
if (tixApi) {
workerScript!.log(`ERROR: buyStock() failed because purchasing this many shares would exceed ${stock.symbol}'s maximum number of shares`);
workerScript!.log("buyStock", `Purchasing '${shares + stock.playerShares + stock.playerShortShares}' shares would exceed ${stock.symbol}'s maximum (${stock.maxShares}) number of shares`);
} else if (opts.suppressDialog !== true) {
dialogBoxCreate(`You cannot purchase this many shares. ${stock.symbol} has a maximum of ${numeralWrapper.formatBigNumber(stock.maxShares)} shares.`);
}
@ -88,7 +88,7 @@ export function buyStock(stock: Stock, shares: number, workerScript: WorkerScrip
const resultTxt = `Bought ${numeralWrapper.format(shares, '0,0')} shares of ${stock.symbol} for ${numeralWrapper.formatMoney(totalPrice)}. ` +
`Paid ${numeralWrapper.formatMoney(CONSTANTS.StockMarketCommission)} in commission fees.`
if (tixApi) {
if (workerScript!.shouldLog("buyStock")) { workerScript!.log(resultTxt); }
workerScript!.log("buyStock", resultTxt)
} else if (opts.suppressDialog !== true) {
dialogBoxCreate(resultTxt);
}
@ -110,7 +110,7 @@ export function sellStock(stock: Stock, shares: number, workerScript: WorkerScri
// Sanitize/Validate arguments
if (stock == null || shares < 0 || isNaN(shares)) {
if (tixApi) {
workerScript!.log(`ERROR: sellStock() failed due to invalid arguments`);
workerScript!.log("sellStock", `Invalid arguments: stock='${stock}' shares='${shares}'`);
} else if (opts.suppressDialog !== true) {
dialogBoxCreate("Failed to sell stock. This is probably due to an invalid quantity. Otherwise, this may be a bug, contact developer");
}
@ -146,7 +146,7 @@ export function sellStock(stock: Stock, shares: number, workerScript: WorkerScri
const resultTxt = `Sold ${numeralWrapper.format(shares, '0,0')} shares of ${stock.symbol}. ` +
`After commissions, you gained a total of ${numeralWrapper.formatMoney(gains)}.`;
if (tixApi) {
if (workerScript!.shouldLog("sellStock")) { workerScript!.log(resultTxt); }
workerScript!.log("sellStock", resultTxt)
} else if (opts.suppressDialog !== true) {
dialogBoxCreate(resultTxt);
}
@ -170,7 +170,7 @@ export function shortStock(stock: Stock, shares: number, workerScript: WorkerScr
if (shares <= 0) { return false; }
if (stock == null || isNaN(shares)) {
if (tixApi) {
workerScript!.log("ERROR: shortStock() failed because of invalid arguments.");
workerScript!.log("shortStock", `Invalid arguments: stock='${stock}' shares='${shares}'`);
} else if (opts.suppressDialog !== true) {
dialogBoxCreate("Failed to initiate a short position in a stock. This is probably " +
"due to an invalid quantity. Otherwise, this may be a bug, so contact developer");
@ -183,7 +183,7 @@ export function shortStock(stock: Stock, shares: number, workerScript: WorkerScr
if (totalPrice == null) { return false; }
if (Player.money.lt(totalPrice)) {
if (tixApi) {
workerScript!.log("ERROR: shortStock() failed because you do not have enough " +
workerScript!.log("shortStock", "You do not have enough " +
"money to purchase this short position. You need " +
numeralWrapper.formatMoney(totalPrice));
} else if (opts.suppressDialog !== true) {
@ -197,7 +197,7 @@ export function shortStock(stock: Stock, shares: number, workerScript: WorkerScr
// Would this purchase exceed the maximum number of shares?
if (shares + stock.playerShares + stock.playerShortShares > stock.maxShares) {
if (tixApi) {
workerScript!.log(`ERROR: shortStock() failed because purchasing this many short shares would exceed ${stock.symbol}'s maximum number of shares.`);
workerScript!.log("shortStock", `This '${shares + stock.playerShares + stock.playerShortShares}' short shares would exceed ${stock.symbol}'s maximum (${stock.maxShares}) number of shares.`);
} else if (opts.suppressDialog !== true) {
dialogBoxCreate(`You cannot purchase this many shares. ${stock.symbol} has a maximum of ${stock.maxShares} shares.`);
}
@ -220,7 +220,7 @@ export function shortStock(stock: Stock, shares: number, workerScript: WorkerScr
`for ${numeralWrapper.formatMoney(totalPrice)}. Paid ${numeralWrapper.formatMoney(CONSTANTS.StockMarketCommission)} ` +
`in commission fees.`;
if (tixApi) {
if (workerScript!.shouldLog("shortStock")) { workerScript!.log(resultTxt); }
workerScript!.log("shortStock", resultTxt);
} else if (!opts.suppressDialog) {
dialogBoxCreate(resultTxt);
}
@ -241,7 +241,7 @@ export function sellShort(stock: Stock, shares: number, workerScript: WorkerScri
if (stock == null || isNaN(shares) || shares < 0) {
if (tixApi) {
workerScript!.log("ERROR: sellShort() failed because of invalid arguments.");
workerScript!.log("sellShort", `Invalid arguments: stock='${stock}' shares='${shares}'`);
} else if (!opts.suppressDialog) {
dialogBoxCreate("Failed to sell a short position in a stock. This is probably " +
"due to an invalid quantity. Otherwise, this may be a bug, so contact developer");
@ -257,7 +257,7 @@ export function sellShort(stock: Stock, shares: number, workerScript: WorkerScri
const totalGain = getSellTransactionGain(stock, shares, PositionTypes.Short);
if (totalGain == null || isNaN(totalGain) || origCost == null) {
if (tixApi) {
workerScript!.log(`Failed to sell short position in a stock. This is probably either due to invalid arguments, or a bug`);
workerScript!.log("sellShort", `Failed to sell short position in a stock. This is probably either due to invalid arguments, or a bug`);
} else if (!opts.suppressDialog) {
dialogBoxCreate(`Failed to sell short position in a stock. This is probably either due to invalid arguments, or a bug`);
}
@ -286,7 +286,7 @@ export function sellShort(stock: Stock, shares: number, workerScript: WorkerScri
const resultTxt = `Sold your short position of ${numeralWrapper.format(shares, '0,0')} shares of ${stock.symbol}. ` +
`After commissions, you gained a total of ${numeralWrapper.formatMoney(totalGain)}`;
if (tixApi) {
if (workerScript!.shouldLog("sellShort")) { workerScript!.log(resultTxt); }
workerScript!.log("sellShort", resultTxt);
} else if (!opts.suppressDialog) {
dialogBoxCreate(resultTxt);
}

@ -38,7 +38,7 @@ export function placeOrder(stock: Stock, shares: number, price: number, type: Or
const tixApi = (workerScript instanceof WorkerScript);
if (!(stock instanceof Stock)) {
if (tixApi) {
workerScript!.log(`ERROR: Invalid stock passed to placeOrder() function`);
workerScript!.log("placeOrder", `Invalid stock: '${stock}'`);
} else {
dialogBoxCreate(`ERROR: Invalid stock passed to placeOrder() function`);
}
@ -46,7 +46,7 @@ export function placeOrder(stock: Stock, shares: number, price: number, type: Or
}
if (typeof shares !== "number" || typeof price !== "number") {
if (tixApi) {
workerScript!.log("ERROR: Invalid numeric value provided for either 'shares' or 'price' argument");
workerScript!.log("placeOrder", `Invalid arguments: shares='${shares}' price='${price}'`);
} else {
dialogBoxCreate("ERROR: Invalid numeric value provided for either 'shares' or 'price' argument");
}