mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-07 08:07:57 +02:00
API: Make some APIs throw error when server is invalid (#2261)
This commit is contained in:
@@ -104,6 +104,7 @@ export const helpers = {
|
||||
createPublicRunningScript,
|
||||
failOnHacknetServer,
|
||||
validateBitNodeOptions,
|
||||
getNormalServer,
|
||||
};
|
||||
|
||||
/** RunOptions with non-optional, type-validated members, for passing between internal functions. */
|
||||
@@ -478,7 +479,7 @@ function scriptIdentifier(
|
||||
* @param {string} hostname - Hostname of the server
|
||||
* @returns {BaseServer} The specified server as a BaseServer
|
||||
*/
|
||||
function getServer(ctx: NetscriptContext, hostname: string) {
|
||||
function getServer(ctx: NetscriptContext, hostname: string): BaseServer {
|
||||
const server = GetServer(hostname);
|
||||
if (server == null || (server.serversOnNetwork.length == 0 && server.hostname != "home")) {
|
||||
const str = hostname === "" ? "'' (empty string)" : "'" + hostname + "'";
|
||||
@@ -487,6 +488,21 @@ function getServer(ctx: NetscriptContext, hostname: string) {
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* A "normal server" is an instance of the Server class in src/Server/Server.ts.
|
||||
*/
|
||||
function getNormalServer(ctx: NetscriptContext, host: string): Server {
|
||||
const server = getServer(ctx, host);
|
||||
if (!(server instanceof Server)) {
|
||||
let errorMessage = `Cannot be executed on ${host}.`;
|
||||
if (server instanceof HacknetServer) {
|
||||
errorMessage += " The server must not be a hacknet server.";
|
||||
}
|
||||
throw helpers.errorMessage(ctx, errorMessage);
|
||||
}
|
||||
return server;
|
||||
}
|
||||
|
||||
function isScriptArgs(args: unknown): args is ScriptArg[] {
|
||||
const isScriptArg = (arg: unknown) => typeof arg === "string" || typeof arg === "number" || typeof arg === "boolean";
|
||||
return Array.isArray(args) && args.every(isScriptArg);
|
||||
@@ -495,10 +511,7 @@ function isScriptArgs(args: unknown): args is ScriptArg[] {
|
||||
function hack(ctx: NetscriptContext, hostname: string, manual: boolean, opts: unknown): Promise<number> {
|
||||
const ws = ctx.workerScript;
|
||||
const { threads, stock, additionalMsec } = validateHGWOptions(ctx, opts);
|
||||
const server = getServer(ctx, hostname);
|
||||
if (!(server instanceof Server)) {
|
||||
throw errorMessage(ctx, "Cannot be executed on this server.");
|
||||
}
|
||||
const server = getNormalServer(ctx, hostname);
|
||||
|
||||
// Calculate the hacking time
|
||||
// This is in seconds
|
||||
@@ -785,8 +798,8 @@ function createPublicRunningScript(runningScript: RunningScript, workerScript?:
|
||||
/**
|
||||
* Used to fail a function if the function's target is a Hacknet Server.
|
||||
* This is used for functions that should run on normal Servers, but not Hacknet Servers
|
||||
* @param {NetscriptContext} ctx - Netscript context
|
||||
* @param {Server} server - Target server
|
||||
* @param {string} callingFn - Name of calling function. For logging purposes
|
||||
* @returns {boolean} True if the server is a Hacknet Server, false otherwise
|
||||
*/
|
||||
function failOnHacknetServer(ctx: NetscriptContext, server: BaseServer): boolean {
|
||||
|
||||
Reference in New Issue
Block a user