From 113af6e711b2e04a003717b40ddc775af8fd3a44 Mon Sep 17 00:00:00 2001
From: G4mingJon4s <40526179+G4mingJon4s@users.noreply.github.com>
Date: Fri, 26 May 2023 13:46:08 +0200
Subject: [PATCH] Add ns.getFunctionRamCost (#526)
---
markdown/bitburner.ns.getfunctionramcost.md | 28 +++++++++++++++++++++
markdown/bitburner.ns.md | 1 +
src/Netscript/RamCostGenerator.ts | 10 +++-----
src/NetscriptFunctions.ts | 5 ++++
src/ScriptEditor/NetscriptDefinitions.d.ts | 10 ++++++++
5 files changed, 48 insertions(+), 6 deletions(-)
create mode 100644 markdown/bitburner.ns.getfunctionramcost.md
diff --git a/markdown/bitburner.ns.getfunctionramcost.md b/markdown/bitburner.ns.getfunctionramcost.md
new file mode 100644
index 000000000..72d2cc2d2
--- /dev/null
+++ b/markdown/bitburner.ns.getfunctionramcost.md
@@ -0,0 +1,28 @@
+
+
+[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [getFunctionRamCost](./bitburner.ns.getfunctionramcost.md)
+
+## NS.getFunctionRamCost() method
+
+Get the ram cost of a netscript function.
+
+**Signature:**
+
+```typescript
+getFunctionRamCost(name: string): number;
+```
+
+## Parameters
+
+| Parameter | Type | Description |
+| --- | --- | --- |
+| name | string | The fully-qualified function name, without the leading ns
. Example inputs: hack
, tprint
, stock.getPosition
. |
+
+**Returns:**
+
+number
+
+## Remarks
+
+RAM cost: 0 GB
+
diff --git a/markdown/bitburner.ns.md b/markdown/bitburner.ns.md
index 5e01c39af..7e27708d5 100644
--- a/markdown/bitburner.ns.md
+++ b/markdown/bitburner.ns.md
@@ -74,6 +74,7 @@ export async function main(ns) {
| [ftpcrack(host)](./bitburner.ns.ftpcrack.md) | Runs FTPCrack.exe on a server. |
| [getBitNodeMultipliers(n, lvl)](./bitburner.ns.getbitnodemultipliers.md) | Get the current Bitnode multipliers. |
| [getFavorToDonate()](./bitburner.ns.getfavortodonate.md) | Returns the amount of Faction favor required to be able to donate to a faction. |
+| [getFunctionRamCost(name)](./bitburner.ns.getfunctionramcost.md) | Get the ram cost of a netscript function. |
| [getGrowTime(host)](./bitburner.ns.getgrowtime.md) | Get the execution time of a grow() call. |
| [getHackingLevel()](./bitburner.ns.gethackinglevel.md) | Returns the player’s current hacking level. |
| [getHackingMultipliers()](./bitburner.ns.gethackingmultipliers.md) | Get hacking related multipliers. |
diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts
index 45656a51e..1c92dbc21 100644
--- a/src/Netscript/RamCostGenerator.ts
+++ b/src/Netscript/RamCostGenerator.ts
@@ -534,6 +534,7 @@ export const RamCosts: RamCostTree = {
getMoneySources: RamCostConstants.GetMoneySourcesCost,
mv: 0,
getResetInfo: 1,
+ getFunctionRamCost: 0,
tail: 0,
toast: 0,
moveTail: 0,
@@ -613,15 +614,13 @@ export const RamCosts: RamCostTree = {
export function getRamCost(...args: string[]): number {
if (args.length === 0) {
- console.warn(`No arguments passed to getRamCost()`);
- return 0;
+ throw new Error(`No arguments passed to getRamCost()`);
}
let curr = RamCosts[args[0] as keyof typeof RamCosts];
for (let i = 1; i < args.length; ++i) {
if (curr == null) {
- console.warn(`Invalid function passed to getRamCost: ${args}`);
- return 0;
+ throw new Error(`Invalid function passed to getRamCost: ${args.join(".")}`);
}
const currType = typeof curr;
@@ -640,6 +639,5 @@ export function getRamCost(...args: string[]): number {
return curr();
}
- console.warn(`Unexpected type (${curr}) for value [${args}]`);
- return 0;
+ throw new Error(`Invalid function passed to getRamCost: ${args.join(".")}`);
}
diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts
index b00fcb588..732042b2a 100644
--- a/src/NetscriptFunctions.ts
+++ b/src/NetscriptFunctions.ts
@@ -93,6 +93,7 @@ import { ContentFilePath } from "./Paths/ContentFile";
import { LiteratureName } from "./Literature/data/LiteratureNames";
import { hasProgramExtension } from "./Paths/ProgramFilePath";
import { hasContractExtension } from "./Paths/ContractFilePath";
+import { getRamCost } from "./Netscript/RamCostGenerator";
export const enums: NSEnums = {
CityName,
@@ -1737,6 +1738,10 @@ export const ns: InternalAPI = {
lastNodeReset: Player.lastNodeReset,
currentNode: Player.bitNodeN,
}),
+ getFunctionRamCost: (ctx) => (_name) => {
+ const name = helpers.string(ctx, "name", _name);
+ return getRamCost(...name.split("."));
+ },
flags: Flags,
...NetscriptExtra(),
};
diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts
index fe14887d6..503d9eeab 100644
--- a/src/ScriptEditor/NetscriptDefinitions.d.ts
+++ b/src/ScriptEditor/NetscriptDefinitions.d.ts
@@ -6510,6 +6510,16 @@ export interface NS {
* */
getResetInfo(): ResetInfo;
+ /**
+ * Get the ram cost of a netscript function.
+ *
+ * @remarks
+ * RAM cost: 0 GB
+ *
+ * @param name - The fully-qualified function name, without the leading `ns`. Example inputs: `hack`, `tprint`, `stock.getPosition`.
+ */
+ getFunctionRamCost(name: string): number;
+
/**
* Parse command line flags.
* @remarks