mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-05 23:27:55 +02:00
API: dynamic imports with ns.dynamicImport() (#2036)
This commit is contained in:
@@ -628,6 +628,7 @@ export const RamCosts: RamCostTree<NSFull> = {
|
||||
heart: { break: 0 },
|
||||
tprintRaw: 0,
|
||||
printRaw: 0,
|
||||
dynamicImport: 0,
|
||||
|
||||
formulas: {
|
||||
mockServer: 0,
|
||||
|
||||
@@ -112,6 +112,8 @@ import { assertFunctionWithNSContext } from "./Netscript/TypeAssertion";
|
||||
import { Router } from "./ui/GameRoot";
|
||||
import { Page } from "./ui/Router";
|
||||
import { canAccessBitNodeFeature, validBitNodes } from "./BitNode/BitNodeUtils";
|
||||
import { compile } from "./NetscriptJSEvaluator";
|
||||
import { Script } from "./Script/Script";
|
||||
|
||||
export const enums: NSEnums = {
|
||||
CityName,
|
||||
@@ -1836,6 +1838,17 @@ export const ns: InternalAPI<NSFull> = {
|
||||
printRaw: (ctx) => (value) => {
|
||||
ctx.workerScript.print(wrapUserNode(value));
|
||||
},
|
||||
dynamicImport: (ctx) => async (value) => {
|
||||
const path = helpers.scriptPath(ctx, "path", value);
|
||||
const server = helpers.getServer(ctx, ctx.workerScript.hostname);
|
||||
const script = server.getContentFile(path);
|
||||
|
||||
if (!script) throw helpers.errorMessage(ctx, `Script was not found\nPath: ${path}`);
|
||||
|
||||
//We validated the path as ScriptFilePath and made sure script is not null
|
||||
//Script **must** be a script at this point
|
||||
return compile(script as Script, server.scripts);
|
||||
},
|
||||
flags: Flags,
|
||||
heart: { break: () => () => Player.karma },
|
||||
...NetscriptExtra(),
|
||||
|
||||
+28
@@ -8370,6 +8370,34 @@ export interface NS {
|
||||
*/
|
||||
getSharePower(): number;
|
||||
|
||||
/**
|
||||
* Dynamically import a script.
|
||||
* Only scripts located on the same server can be imported.
|
||||
* A dynamic import will not adjust RAM usage. This must be done manually with {@link NS.ramOverride|ramOverride}.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* File: script.js
|
||||
* ```js
|
||||
* export async function main(ns){
|
||||
* const script = await ns.dynamicImport("./scriptToImport.js");
|
||||
* script.log(ns, "Message from an imported script!")
|
||||
* }
|
||||
*
|
||||
* ```
|
||||
*
|
||||
* File: scriptToImport.js
|
||||
* ```js
|
||||
* export async function log(ns, message){
|
||||
* ns.tprint(message);
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
* RAM cost: 0 GB
|
||||
*/
|
||||
dynamicImport(path: string): Promise<any>;
|
||||
|
||||
enums: NSEnums;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user