Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETSCRIPT: Add .script deprecation notice & migration guide #1007

Merged
merged 20 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Documentation/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@

- [v1.0.0 script migration guide](migrations/v1.md)
- [v2.0.0 script migration guide](migrations/v2.md)
- [Netscript 2 migration guide](migrations/ns2.md)
57 changes: 57 additions & 0 deletions src/Documentation/doc/migrations/ns2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Netscript 2 Migration Guide

With the deprecation of NS1 (`.script`) there are required changes to migrate scripts over to NS2 (`.js`).

## Basic Steps
1. You need to wrap the entire script inside of an exported main function, like so:
```js
/** @param {NS} ns */
export async function main(ns) {
// your code here
}
```
2. Add `ns.` as a prefix to all game functions (such as `ns.hack()` or `ns.nuke()`).
3. If a function returns a `Promise`, you need to put the `await` keyword before it (With the JSDoc comment you can hover over the function to see the return type).
4. Long-running loops (like `while(true)`) need to `await` a function (usually `ns.sleep()`) at least once or they will crash the game.

## A demonstration of migration

Original (`early-hacking-template.script`):
```js
var target = "n00dles";
var moneyThresh = getServerMaxMoney(target) * 0.9;
var securityThresh = getServerMinSecurityLevel(target) + 5;

while(true) {
if (getServerSecurityLevel(target) > securityThresh) {
weaken(target);
} else if (getServerMoneyAvailable(target) < moneyThresh) {
grow(target);
} else {
hack(target);
}
}
```
Migrated (`early-hacking-template.js`):
```js
/** @param {NS} ns */
export async function main(ns) {
const target = "n00dles";
const moneyThresh = ns.getServerMaxMoney(target) * 0.9;
const securityThresh = ns.getServerMinSecurityLevel(target) + 5;

while(true) {
if (ns.getServerSecurityLevel(target) > securityThresh) {
await ns.weaken(target);
} else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
await ns.grow(target):
} else {
await ns.hack(target);
}
}
}
```

## Additional problems or edge cases

To get additional help with the migration join the [official Discord server](https://discord.gg/TFc3hKD).
2 changes: 2 additions & 0 deletions src/Documentation/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import file37 from "!!raw-loader!./doc/programming/go_algorithms.md";
import file38 from "!!raw-loader!./doc/programming/hackingalgorithms.md";
import file39 from "!!raw-loader!./doc/programming/learn.md";
import file40 from "!!raw-loader!./doc/programming/remote_api.md";
import file41 from "!!raw-loader!./doc/migrations/ns2.md"

interface Document {
default: string;
Expand Down Expand Up @@ -86,3 +87,4 @@ AllPages["programming/go_algorithms.md"] = file37;
AllPages["programming/hackingalgorithms.md"] = file38;
AllPages["programming/learn.md"] = file39;
AllPages["programming/remote_api.md"] = file40;
AllPages["migrations/ns2.md"] = file41;
8 changes: 8 additions & 0 deletions src/Terminal/commands/common/deprecation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Terminal } from "../../../Terminal";

export function sendDeprecationNotice() {
return Terminal.warn(
"NOTICE: NS1 (.script) scripts are deprecated and will be removed in a future update." +
" Migrate to NS2 (.js) scripts instead."
);
}
7 changes: 7 additions & 0 deletions src/Terminal/commands/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CursorPositions } from "../../../ScriptEditor/CursorPositions";
import { ScriptFilePath, hasScriptExtension } from "../../../Paths/ScriptFilePath";
import { TextFilePath, hasTextExtension } from "../../../Paths/TextFilePath";
import { getGlobbedFileMap } from "../../../Paths/GlobbedFiles";
import { sendDeprecationNotice } from "./deprecation";

// 2.3: Globbing implementation was removed from this file. Globbing will be reintroduced as broader functionality and integrated here.

Expand All @@ -30,12 +31,14 @@ export function commonEditor(
): void {
if (args.length < 1) return Terminal.error(`Incorrect usage of ${command} command. Usage: ${command} [scriptname]`);
const files = new Map<ScriptFilePath | TextFilePath, string>();
let hasNs1 = false;
for (const arg of args) {
const pattern = String(arg);

// Glob of existing files
if (pattern.includes("*") || pattern.includes("?")) {
for (const [path, file] of getGlobbedFileMap(pattern, server, Terminal.currDir)) {
if (path.endsWith(".script")) hasNs1 = true;
files.set(path, file.content);
}
continue;
Expand All @@ -47,10 +50,14 @@ export function commonEditor(
if (!hasScriptExtension(path) && !hasTextExtension(path)) {
return Terminal.error(`${command}: Only scripts or text files can be edited. Invalid file type: ${arg}`);
}
if (path.endsWith(".script")) hasNs1 = true;
const file = server.getContentFile(path);
const content = file ? file.content : isNs2(path) ? newNs2Template : "";
files.set(path, content);
if (content === newNs2Template) CursorPositions.saveCursor(path, { row: 3, column: 5 });
}
if (hasNs1) {
sendDeprecationNotice();
}
Router.toPage(Page.ScriptEditor, { files, options });
}
4 changes: 4 additions & 0 deletions src/Terminal/commands/runScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { formatRam } from "../../ui/formatNumber";
import { ScriptArg } from "@nsdefs";
import { isPositiveInteger } from "../../types";
import { ScriptFilePath } from "../../Paths/ScriptFilePath";
import { sendDeprecationNotice } from "./common/deprecation";

export function runScript(path: ScriptFilePath, commandArgs: (string | number | boolean)[], server: BaseServer): void {
// This takes in the absolute filepath, see "run.ts"
Expand Down Expand Up @@ -52,6 +53,9 @@ export function runScript(path: ScriptFilePath, commandArgs: (string | number |
const success = startWorkerScript(runningScript, server);
if (!success) return Terminal.error(`Failed to start script`);

if (path.endsWith(".script")) {
sendDeprecationNotice();
}
Terminal.print(
`Running script with ${numThreads} thread(s), pid ${runningScript.pid} and args: ${JSON.stringify(args)}.`,
);
Expand Down
Loading