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 the 'cat' method to 'ns.singularity' #1999

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

NagaOuroboros
Copy link
Contributor

@NagaOuroboros NagaOuroboros commented Mar 6, 2025

Motivation

While .lit files are easy to copy onto the 'home' server, doing so clutters up the root directory, and will only create more clutter as new .lit files get added in the future. As a first step to managing this, I've implemented the functionality of the cat terminal command as a method available on the ns.singularity namespace.

This addition should let players write their own 'remote file viewer' scripts once they've gotten SF4.

Scope

This PR only adds the new method itself, which is simply a 1:1 feature-for-feature implementation of the terminal equivalent. No new behaviors are added.

New API Method

ns.singularity.cat(filename) - Displays the content of a file on the currently connected server

Considerations

  • The RAM cost of the method is currently set at 0.5 before the SF4 multipliers. I would argue for lower, perhaps even 0.1, but I feel 0.5 is a reasonable upper limit. There is no gameplay advantage to this method—it is purely a quality of life addition—so I do not feel it deserves to be any higher, especially as it will often be used in conjunction with ns.singularity.connect.

cat: (ctx) => (_filename) => {
const path = helpers.filePath(ctx, "filename", _filename);
const server = helpers.getServer(ctx, Player.currentServer);
if (hasScriptExtension(path) || hasTextExtension(path)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can reuse a large part of src\Terminal\commands\cat.ts instead of duplicating them here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean directly use the exported cat function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or pull common code out to a new function, depending on how different these functions are. For example:

// src/Terminal/commands/cat.ts

export function commonCode(something) {}

export function cat(args: (string | number | boolean)[], server: BaseServer): void {
  someChecks1();
  commonCode(something);
}

// src/NetscriptFunctions/Singularity.ts
import { commonCode } from "/path/cat.ts";
    cat: (ctx) => (_filename) => {
      someChecks2();
      commonCode(something);
    }

It's okay if you reuse cat in cat.ts (if it's possible).

@NagaOuroboros
Copy link
Contributor Author

Refactor

Changed the implementation to simply hook directly into the terminal command's logic. Also added the missing check for the player owning SF4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants