2021-09-05 01:09:30 +02:00
import { vsprintf , sprintf } from "sprintf-js" ;
2017-10-12 22:10:34 +02:00
2019-05-05 06:03:40 +02:00
import { getRamCost } from "./Netscript/RamCostGenerator" ;
2019-06-22 10:12:17 +02:00
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter" ;
2019-05-05 06:03:40 +02:00
2019-04-11 10:37:40 +02:00
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers" ;
2021-11-03 23:16:10 +01:00
2019-04-11 10:37:40 +02:00
import { CONSTANTS } from "./Constants" ;
2021-11-03 23:16:10 +01:00
2019-04-11 10:37:40 +02:00
import {
2021-09-05 01:09:30 +02:00
calculateHackingChance ,
calculateHackingExpGain ,
calculatePercentMoneyHacked ,
calculateHackingTime ,
calculateGrowTime ,
calculateWeakenTime ,
2019-04-11 10:37:40 +02:00
} from "./Hacking" ;
2021-11-03 23:16:10 +01:00
2021-09-09 05:47:34 +02:00
import { netscriptCanGrow , netscriptCanHack , netscriptCanWeaken } from "./Hacking/netscriptCanHack" ;
2019-03-30 04:01:34 +01:00
2021-05-01 09:17:31 +02:00
import { HacknetServer } from "./Hacknet/HacknetServer" ;
2021-11-03 23:16:10 +01:00
2021-03-23 21:02:34 +01:00
import { Terminal } from "./Terminal" ;
2019-04-04 02:12:11 +02:00
2019-04-11 10:37:40 +02:00
import { Player } from "./Player" ;
import { Programs } from "./Programs/Programs" ;
import { Script } from "./Script/Script" ;
2021-09-09 05:47:34 +02:00
import { findRunningScript , findRunningScriptByPid } from "./Script/ScriptHelpers" ;
2021-09-24 22:34:21 +02:00
import { isScriptFilename } from "./Script/isScriptFilename" ;
2021-10-02 04:53:23 +02:00
import { PromptEvent } from "./ui/React/PromptManager" ;
2021-09-24 22:34:21 +02:00
2021-10-07 22:56:01 +02:00
import { GetServer , GetAllServers , DeleteServer , AddToAllServers , createUniqueRandomIp } from "./Server/AllServers" ;
2021-03-12 02:37:58 +01:00
import { RunningScript } from "./Script/RunningScript" ;
2019-04-11 10:37:40 +02:00
import {
2021-09-05 01:09:30 +02:00
getServerOnNetwork ,
numCycleForGrowth ,
processSingleServerGrowth ,
safetlyCreateUniqueServer ,
2019-04-11 10:37:40 +02:00
} from "./Server/ServerHelpers" ;
2021-09-09 05:47:34 +02:00
import { getPurchaseServerCost , getPurchaseServerLimit , getPurchaseServerMaxRam } from "./Server/ServerPurchases" ;
2021-09-25 03:49:49 +02:00
import { Server } from "./Server/Server" ;
2019-05-05 06:03:40 +02:00
import { SourceFileFlags } from "./SourceFile/SourceFileFlags" ;
2021-09-09 05:47:34 +02:00
import { influenceStockThroughServerHack , influenceStockThroughServerGrow } from "./StockMarket/PlayerInfluencing" ;
2021-10-14 09:22:02 +02:00
2021-09-09 05:47:34 +02:00
import { isValidFilePath , removeLeadingSlash } from "./Terminal/DirectoryHelpers" ;
2019-04-11 10:37:40 +02:00
import { TextFile , getTextFile , createTextFile } from "./TextFile" ;
2017-08-30 19:44:29 +02:00
2021-10-14 09:22:02 +02:00
import { NetscriptPorts , runScriptFromScript } from "./NetscriptWorker" ;
2019-05-16 08:05:36 +02:00
import { killWorkerScript } from "./Netscript/killWorkerScript" ;
import { workerScripts } from "./Netscript/WorkerScripts" ;
2021-09-25 03:49:49 +02:00
import { WorkerScript } from "./Netscript/WorkerScript" ;
2021-09-09 05:47:34 +02:00
import { makeRuntimeRejectMsg , netscriptDelay , resolveNetscriptRequestedThreads } from "./NetscriptEvaluator" ;
2017-08-30 19:44:29 +02:00
2019-04-11 10:37:40 +02:00
import { numeralWrapper } from "./ui/numeralFormat" ;
2021-09-25 20:42:57 +02:00
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions" ;
2019-02-27 03:26:29 +01:00
2021-10-01 07:00:50 +02:00
import { LogBoxEvents } from "./ui/React/LogBoxManager" ;
2021-09-25 20:42:57 +02:00
import { arrayToString } from "./utils/helpers/arrayToString" ;
import { isString } from "./utils/helpers/isString" ;
2017-08-21 18:59:06 +02:00
2021-09-25 08:36:49 +02:00
import { BaseServer } from "./Server/BaseServer" ;
2021-11-03 23:16:10 +01:00
import { NetscriptGang } from "./NetscriptFunctions/Gang" ;
import { NetscriptSleeve } from "./NetscriptFunctions/Sleeve" ;
2021-11-04 01:19:52 +01:00
import { NetscriptExtra } from "./NetscriptFunctions/Extra" ;
2021-11-03 23:16:10 +01:00
import { NetscriptHacknet } from "./NetscriptFunctions/Hacknet" ;
2021-11-14 04:44:17 +01:00
import { NetscriptStanek } from "./NetscriptFunctions/Stanek" ;
2021-12-29 08:04:24 +01:00
import { NetscriptUserInterface } from "./NetscriptFunctions/UserInterface" ;
2021-10-28 05:58:07 +02:00
import { NetscriptBladeburner } from "./NetscriptFunctions/Bladeburner" ;
2021-11-03 23:16:10 +01:00
import { NetscriptCodingContract } from "./NetscriptFunctions/CodingContract" ;
2021-11-04 01:19:52 +01:00
import { NetscriptCorporation } from "./NetscriptFunctions/Corporation" ;
2021-11-03 23:16:10 +01:00
import { NetscriptFormulas } from "./NetscriptFunctions/Formulas" ;
2021-10-30 22:03:34 +02:00
import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket" ;
2021-12-27 19:06:38 +01:00
import { IPort } from "./NetscriptPort" ;
2021-10-14 09:22:02 +02:00
2021-11-14 05:45:26 +01:00
import {
NS as INS ,
Player as INetscriptPlayer ,
Gang as IGang ,
Bladeburner as IBladeburner ,
Stanek as IStanek ,
2021-12-03 21:35:45 +01:00
SourceFileLvl ,
2021-11-14 05:45:26 +01:00
} from "./ScriptEditor/NetscriptDefinitions" ;
2021-11-14 04:44:17 +01:00
import { NetscriptSingularity } from "./NetscriptFunctions/Singularity" ;
2021-09-25 23:21:50 +02:00
2021-10-15 18:47:43 +02:00
import { toNative } from "./NetscriptFunctions/toNative" ;
2021-09-05 01:09:30 +02:00
2021-10-13 02:02:37 +02:00
import { dialogBoxCreate } from "./ui/React/DialogBox" ;
2021-10-13 23:25:58 +02:00
import { SnackbarEvents } from "./ui/React/Snackbar" ;
2021-11-03 23:16:10 +01:00
2021-10-15 18:47:43 +02:00
import { Flags } from "./NetscriptFunctions/Flags" ;
2022-01-18 20:02:12 +01:00
import { calculateIntelligenceBonus } from "./PersonObjects/formulas/intelligence" ;
import { CalculateShareMult , StartSharing } from "./NetworkShare/Share" ;
2021-04-21 14:20:26 +02:00
2021-11-04 01:19:52 +01:00
interface NS extends INS {
2021-09-25 03:49:49 +02:00
[ key : string ] : any ;
2021-11-14 05:45:26 +01:00
gang : IGang ;
bladeburner : IBladeburner ;
stanek : IStanek ;
2021-09-25 03:49:49 +02:00
}
2021-11-04 01:19:52 +01:00
export function NetscriptFunctions ( workerScript : WorkerScript ) : NS {
2021-09-25 08:36:49 +02:00
const updateDynamicRam = function ( fnName : string , ramCost : number ) : void {
2021-09-05 01:09:30 +02:00
if ( workerScript . dynamicLoadedFns [ fnName ] ) {
return ;
}
workerScript . dynamicLoadedFns [ fnName ] = true ;
let threads = workerScript . scriptRef . threads ;
if ( typeof threads !== "number" ) {
2021-10-07 23:55:49 +02:00
console . warn ( ` WorkerScript detected NaN for threadcount for ${ workerScript . name } on ${ workerScript . hostname } ` ) ;
2021-09-05 01:09:30 +02:00
threads = 1 ;
2018-09-23 02:25:48 +02:00
}
2021-10-28 00:55:06 +02:00
workerScript . dynamicRamUsage += ramCost ;
2021-09-05 01:09:30 +02:00
if ( workerScript . dynamicRamUsage > 1.01 * workerScript . ramUsage ) {
throw makeRuntimeRejectMsg (
workerScript ,
2021-09-05 01:27:16 +02:00
` Dynamic RAM usage calculated to be greater than initial RAM usage on fn: ${ fnName } .
This is probably because you somehow circumvented the static RAM calculation .
2021-10-28 00:55:06 +02:00
Threads : $ { threads }
2021-09-09 05:47:34 +02:00
Dynamic RAM Usage : $ { numeralWrapper . formatRAM ( workerScript . dynamicRamUsage ) }
2021-09-05 01:27:16 +02:00
Static RAM Usage : $ { numeralWrapper . formatRAM ( workerScript . ramUsage ) }
One of these could be the reason :
* Using eval ( ) to get a reference to a ns function
2021-10-19 16:13:19 +02:00
& nbsp ; & nbsp ; const myScan = eval ( 'ns.scan' ) ;
2021-09-05 01:27:16 +02:00
* Using map access to do the same
2021-10-19 16:13:19 +02:00
& nbsp ; & nbsp ; const myScan = ns [ 'scan' ] ;
2021-09-05 01:27:16 +02:00
Sorry : ( ` ,
2021-09-05 01:09:30 +02:00
) ;
}
} ;
/ * *
* Gets the Server for a specific hostname / ip , throwing an error
* if the server doesn ' t exist .
2021-10-19 16:13:19 +02:00
* @param { string } hostname - Hostname or IP of the server
2021-09-05 01:09:30 +02:00
* @param { string } callingFnName - Name of calling function . For logging purposes
2021-10-19 16:13:19 +02:00
* @returns { BaseServer } The specified server as a BaseServer
2021-09-05 01:09:30 +02:00
* /
2021-10-19 16:13:19 +02:00
const safeGetServer = function ( hostname : string , callingFnName : string ) : BaseServer {
const server = GetServer ( hostname ) ;
2021-09-05 01:09:30 +02:00
if ( server == null ) {
2021-10-19 16:13:19 +02:00
throw makeRuntimeErrorMsg ( callingFnName , ` Invalid hostname or IP: ${ hostname } ` ) ;
2021-09-05 01:09:30 +02:00
}
return server ;
} ;
/ * *
* Searches for and returns the RunningScript object for the specified script .
* If the 'fn' argument is not specified , this returns the current RunningScript .
* @param { string } fn - Filename of script
2021-10-19 16:13:19 +02:00
* @param { string } hostname - Hostname / ip of the server on which the script resides
2021-09-05 01:09:30 +02:00
* @param { any [ ] } scriptArgs - Running script ' s arguments
* @returns { RunningScript }
* Running script identified by the parameters , or null if no such script
* exists , or the current running script if the first argument 'fn'
* is not specified .
* /
2021-10-23 16:09:43 +02:00
const getRunningScript = function (
fn : any ,
hostname : any ,
callingFnName : any ,
scriptArgs : any ,
) : RunningScript | null {
2021-09-05 01:09:30 +02:00
if ( typeof callingFnName !== "string" || callingFnName === "" ) {
callingFnName = "getRunningScript" ;
2019-07-05 03:01:09 +02:00
}
2021-09-05 01:09:30 +02:00
if ( ! Array . isArray ( scriptArgs ) ) {
throw makeRuntimeRejectMsg (
workerScript ,
` Invalid scriptArgs argument passed into getRunningScript() from ${ callingFnName } (). ` +
2022-02-13 10:08:55 +01:00
` This is probably a bug. Please report to game developer ` ,
2021-09-05 01:09:30 +02:00
) ;
2021-04-21 14:20:26 +02:00
}
2021-09-05 01:09:30 +02:00
if ( fn != null && typeof fn === "string" ) {
// Get Logs of another script
2021-10-19 16:13:19 +02:00
if ( hostname == null ) {
hostname = workerScript . hostname ;
2021-09-05 01:09:30 +02:00
}
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , callingFnName ) ;
2019-07-05 03:01:09 +02:00
2021-09-05 01:09:30 +02:00
return findRunningScript ( fn , scriptArgs , server ) ;
2019-07-05 03:01:09 +02:00
}
2021-09-05 01:09:30 +02:00
// If no arguments are specified, return the current RunningScript
return workerScript . scriptRef ;
} ;
2021-09-25 08:36:49 +02:00
const getRunningScriptByPid = function ( pid : any , callingFnName : any ) : RunningScript | null {
2021-09-05 01:09:30 +02:00
if ( typeof callingFnName !== "string" || callingFnName === "" ) {
callingFnName = "getRunningScriptgetRunningScriptByPid" ;
2019-04-29 08:20:27 +02:00
}
2021-10-07 22:04:04 +02:00
for ( const server of GetAllServers ( ) ) {
2021-09-05 01:09:30 +02:00
const runningScript = findRunningScriptByPid ( pid , server ) ;
if ( runningScript ) return runningScript ;
}
return null ;
} ;
/ * *
* Helper function for getting the error log message when the user specifies
* a nonexistent running script
* @param { string } fn - Filename of script
2021-10-19 16:13:19 +02:00
* @param { string } hostname - Hostname / ip of the server on which the script resides
2021-09-05 01:09:30 +02:00
* @param { any [ ] } scriptArgs - Running script ' s arguments
* @returns { string } Error message to print to logs
* /
2021-10-19 16:13:19 +02:00
const getCannotFindRunningScriptErrorMessage = function ( fn : any , hostname : any , scriptArgs : any ) : string {
2021-09-05 01:09:30 +02:00
if ( ! Array . isArray ( scriptArgs ) ) {
scriptArgs = [ ] ;
}
2019-04-29 08:20:27 +02:00
2021-10-19 16:13:19 +02:00
return ` Cannot find running script ${ fn } on server ${ hostname } with args: ${ arrayToString ( scriptArgs ) } ` ;
2021-09-05 01:09:30 +02:00
} ;
/ * *
* 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 { 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
* /
2021-09-25 08:36:49 +02:00
const failOnHacknetServer = function ( server : any , callingFn : any = "" ) : boolean {
2021-09-05 01:09:30 +02:00
if ( server instanceof HacknetServer ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( callingFn , ( ) = > ` Does not work on Hacknet Servers ` ) ;
2021-09-05 01:09:30 +02:00
return true ;
} else {
return false ;
}
} ;
2021-09-25 08:36:49 +02:00
const makeRuntimeErrorMsg = function ( caller : string , msg : string ) : string {
2021-09-25 03:49:49 +02:00
const errstack = new Error ( ) . stack ;
if ( errstack === undefined ) throw new Error ( "how did we not throw an error?" ) ;
const stack = errstack . split ( "\n" ) . slice ( 1 ) ;
2021-09-05 01:09:30 +02:00
const scripts = workerScript . getServer ( ) . scripts ;
const userstack = [ ] ;
for ( const stackline of stack ) {
let filename ;
for ( const script of scripts ) {
if ( script . url && stackline . includes ( script . url ) ) {
filename = script . filename ;
2018-07-15 02:25:50 +02:00
}
2021-09-05 01:09:30 +02:00
for ( const dependency of script . dependencies ) {
if ( stackline . includes ( dependency . url ) ) {
filename = dependency . filename ;
}
2018-07-15 02:25:50 +02:00
}
2021-09-05 01:09:30 +02:00
}
if ( ! filename ) continue ;
2019-03-30 00:14:32 +01:00
2021-09-25 08:36:49 +02:00
interface ILine {
line : string ;
func : string ;
}
function parseChromeStackline ( line : string ) : ILine | null {
2021-09-05 01:09:30 +02:00
const lineRe = /.*:(\d+):\d+.*/ ;
const funcRe = /.*at (.+) \(.*/ ;
2019-03-30 00:14:32 +01:00
2021-09-05 01:09:30 +02:00
const lineMatch = line . match ( lineRe ) ;
const funcMatch = line . match ( funcRe ) ;
if ( lineMatch && funcMatch ) {
return { line : lineMatch [ 1 ] , func : funcMatch [ 1 ] } ;
}
return null ;
}
let call = { line : "-1" , func : "unknown" } ;
const chromeCall = parseChromeStackline ( stackline ) ;
if ( chromeCall ) {
call = chromeCall ;
}
2021-03-25 07:48:15 +01:00
2021-09-25 08:36:49 +02:00
function parseFirefoxStackline ( line : string ) : ILine | null {
2021-09-05 01:09:30 +02:00
const lineRe = /.*:(\d+):\d+$/ ;
const lineMatch = line . match ( lineRe ) ;
2021-03-10 07:27:14 +01:00
2021-09-05 01:09:30 +02:00
const lio = line . lastIndexOf ( "@" ) ;
2021-03-11 09:02:05 +01:00
2021-09-05 01:09:30 +02:00
if ( lineMatch && lio !== - 1 ) {
return { line : lineMatch [ 1 ] , func : line.slice ( 0 , lio ) } ;
}
return null ;
}
2021-03-25 07:48:15 +01:00
2021-09-25 07:26:03 +02:00
const firefoxCall = parseFirefoxStackline ( stackline ) ;
2021-09-05 01:09:30 +02:00
if ( firefoxCall ) {
call = firefoxCall ;
}
2021-03-11 09:02:05 +01:00
2021-09-05 01:09:30 +02:00
userstack . push ( ` ${ filename } :L ${ call . line } @ ${ call . func } ` ) ;
}
2021-03-11 09:02:05 +01:00
2021-11-27 00:30:58 +01:00
workerScript . log ( caller , ( ) = > msg ) ;
2021-09-05 01:09:30 +02:00
let rejectMsg = ` ${ caller } : ${ msg } ` ;
2021-09-09 05:47:34 +02:00
if ( userstack . length !== 0 ) rejectMsg += ` <br><br>Stack:<br> ${ userstack . join ( "<br>" ) } ` ;
2021-09-05 01:09:30 +02:00
return makeRuntimeRejectMsg ( workerScript , rejectMsg ) ;
} ;
2022-01-05 04:21:44 +01:00
const checkSingularityAccess = function ( func : string ) : void {
2021-09-05 01:09:30 +02:00
if ( Player . bitNodeN !== 4 ) {
2022-01-05 04:21:44 +01:00
if ( Player . sourceFileLvl ( 4 ) === 0 ) {
2021-12-19 00:13:49 +01:00
throw makeRuntimeErrorMsg (
func ,
2022-01-05 04:21:44 +01:00
` This singularity function requires Source-File 4 to run. A power up you obtain later in the game. It will be very obvious when and how you can obtain it. ` ,
2021-12-19 00:13:49 +01:00
) ;
2021-09-05 01:09:30 +02:00
}
}
} ;
2021-10-19 16:13:19 +02:00
const hack = function ( hostname : any , manual : any , { threads : requestedThreads , stock } : any = { } ) : Promise < number > {
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "hack" , "Takes 1 argument." ) ;
}
2021-09-09 05:47:34 +02:00
const threads = resolveNetscriptRequestedThreads ( workerScript , "hack" , requestedThreads ) ;
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "hack" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-10-19 16:13:19 +02:00
throw makeRuntimeErrorMsg ( "hack" , "Cannot be executed on this server." ) ;
2021-03-11 09:02:05 +01:00
}
2021-09-05 01:09:30 +02:00
// Calculate the hacking time
2021-09-25 03:49:49 +02:00
const hackingTime = calculateHackingTime ( server , Player ) ; // This is in seconds
2021-09-05 01:09:30 +02:00
// No root access or skill level too low
const canHack = netscriptCanHack ( server , Player ) ;
if ( ! canHack . res ) {
2021-09-25 03:49:49 +02:00
throw makeRuntimeErrorMsg ( "hack" , canHack . msg || "" ) ;
2021-03-11 09:02:05 +01:00
}
2021-09-05 01:09:30 +02:00
workerScript . log (
"hack" ,
2021-11-27 00:30:58 +01:00
( ) = >
2022-01-12 14:58:12 +01:00
` Executing on ' ${ server . hostname } ' in ${ convertTimeMsToTimeElapsedString (
2021-11-27 00:30:58 +01:00
hackingTime * 1000 ,
true ,
) } ( t = $ { numeralWrapper . formatThreads ( threads ) } ) ` ,
2021-09-05 01:09:30 +02:00
) ;
return netscriptDelay ( hackingTime * 1000 , workerScript ) . then ( function ( ) {
2021-09-25 07:26:03 +02:00
const hackChance = calculateHackingChance ( server , Player ) ;
const rand = Math . random ( ) ;
let expGainedOnSuccess = calculateHackingExpGain ( server , Player ) * threads ;
const expGainedOnFailure = expGainedOnSuccess / 4 ;
2021-09-05 01:09:30 +02:00
if ( rand < hackChance ) {
// Success!
const percentHacked = calculatePercentMoneyHacked ( server , Player ) ;
2022-01-15 20:33:46 +01:00
let maxThreadNeeded = Math . ceil ( 1 / percentHacked ) ;
2021-09-05 01:09:30 +02:00
if ( isNaN ( maxThreadNeeded ) ) {
// Server has a 'max money' of 0 (probably). We'll set this to an arbitrarily large value
maxThreadNeeded = 1 e6 ;
2021-03-11 09:02:05 +01:00
}
2021-09-09 05:47:34 +02:00
let moneyDrained = Math . floor ( server . moneyAvailable * percentHacked ) * threads ;
2021-03-11 09:02:05 +01:00
2021-09-05 01:09:30 +02:00
// Over-the-top safety checks
if ( moneyDrained <= 0 ) {
moneyDrained = 0 ;
expGainedOnSuccess = expGainedOnFailure ;
2021-03-11 09:02:05 +01:00
}
2021-09-05 01:09:30 +02:00
if ( moneyDrained > server . moneyAvailable ) {
moneyDrained = server . moneyAvailable ;
}
server . moneyAvailable -= moneyDrained ;
if ( server . moneyAvailable < 0 ) {
server . moneyAvailable = 0 ;
2021-03-11 09:02:05 +01:00
}
2021-09-09 05:47:34 +02:00
const moneyGained = moneyDrained * BitNodeMultipliers . ScriptHackMoneyGain ;
2021-09-05 01:09:30 +02:00
2021-10-27 20:18:33 +02:00
Player . gainMoney ( moneyGained , "hacking" ) ;
2021-09-05 01:09:30 +02:00
workerScript . scriptRef . onlineMoneyMade += moneyGained ;
Player . scriptProdSinceLastAug += moneyGained ;
2021-10-07 23:55:49 +02:00
workerScript . scriptRef . recordHack ( server . hostname , moneyGained , threads ) ;
2021-09-05 01:09:30 +02:00
Player . gainHackingExp ( expGainedOnSuccess ) ;
2022-01-09 21:22:23 +01:00
if ( manual ) Player . gainIntelligenceExp ( 0.005 ) ;
2021-09-05 01:09:30 +02:00
workerScript . scriptRef . onlineExpGained += expGainedOnSuccess ;
workerScript . log (
"hack" ,
2021-11-27 00:30:58 +01:00
( ) = >
` Successfully hacked ' ${ server . hostname } ' for ${ numeralWrapper . formatMoney (
moneyGained ,
) } and $ { numeralWrapper . formatExp ( expGainedOnSuccess ) } exp ( t = $ { numeralWrapper . formatThreads ( threads ) } ) ` ,
2021-09-05 01:09:30 +02:00
) ;
2021-09-09 05:47:34 +02:00
server . fortify ( CONSTANTS . ServerFortifyAmount * Math . min ( threads , maxThreadNeeded ) ) ;
2021-09-05 01:09:30 +02:00
if ( stock ) {
2021-11-25 17:47:31 +01:00
influenceStockThroughServerHack ( server , moneyDrained ) ;
2021-03-11 09:02:05 +01:00
}
2021-09-05 01:09:30 +02:00
if ( manual ) {
server . backdoorInstalled = true ;
}
return Promise . resolve ( moneyGained ) ;
} else {
// Player only gains 25% exp for failure?
Player . gainHackingExp ( expGainedOnFailure ) ;
workerScript . scriptRef . onlineExpGained += expGainedOnFailure ;
workerScript . log (
"hack" ,
2021-11-27 00:30:58 +01:00
( ) = >
` Failed to hack ' ${ server . hostname } '. Gained ${ numeralWrapper . formatExp (
expGainedOnFailure ,
) } exp ( t = $ { numeralWrapper . formatThreads ( threads ) } ) ` ,
2021-09-05 01:09:30 +02:00
) ;
return Promise . resolve ( 0 ) ;
}
} ) ;
} ;
2021-09-25 08:36:49 +02:00
const argsToString = function ( args : any [ ] ) : string {
2021-09-05 01:09:30 +02:00
let out = "" ;
for ( let arg of args ) {
arg = toNative ( arg ) ;
2021-10-23 16:09:43 +02:00
out += typeof arg === "object" ? JSON . stringify ( arg ) : ` ${ arg } ` ;
2021-03-11 09:02:05 +01:00
}
2021-09-05 01:09:30 +02:00
return out ;
} ;
2021-10-05 03:06:55 +02:00
const helper = {
updateDynamicRam : updateDynamicRam ,
makeRuntimeErrorMsg : makeRuntimeErrorMsg ,
2021-10-05 04:25:21 +02:00
string : ( funcName : string , argName : string , v : any ) : string = > {
if ( typeof v === "string" ) return v ;
if ( typeof v === "number" ) return v + "" ; // cast to string;
throw makeRuntimeErrorMsg ( funcName , ` ${ argName } should be a string ` ) ;
} ,
number : ( funcName : string , argName : string , v : any ) : number = > {
2022-01-13 01:55:45 +01:00
if ( ! isNaN ( v ) ) {
2022-01-12 07:24:03 +01:00
if ( typeof v === "number" ) return v ;
if ( ! isNaN ( parseFloat ( v ) ) ) return parseFloat ( v ) ;
}
2021-10-05 04:25:21 +02:00
throw makeRuntimeErrorMsg ( funcName , ` ${ argName } should be a number ` ) ;
} ,
boolean : ( v : any ) : boolean = > {
return ! ! v ; // Just convert it to boolean.
} ,
2021-10-14 09:22:02 +02:00
getServer : safeGetServer ,
checkSingularityAccess : checkSingularityAccess ,
2021-11-03 23:16:10 +01:00
hack : hack ,
2022-01-05 01:09:34 +01:00
getValidPort : ( funcName : string , port : any ) : IPort = > {
2021-12-27 19:06:38 +01:00
if ( isNaN ( port ) ) {
throw makeRuntimeErrorMsg (
funcName ,
` Invalid argument. Must be a port number between 1 and ${ CONSTANTS . NumNetscriptPorts } , is ${ port } ` ,
) ;
}
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
throw makeRuntimeErrorMsg (
funcName ,
2021-12-27 20:36:14 +01:00
` Trying to use an invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid. ` ,
2021-12-27 19:06:38 +01:00
) ;
}
const iport = NetscriptPorts [ port - 1 ] ;
if ( iport == null || ! ( iport instanceof Object ) ) {
throw makeRuntimeErrorMsg ( funcName , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
}
return iport ;
2022-01-05 01:09:34 +01:00
} ,
2021-10-05 03:06:55 +02:00
} ;
2021-03-11 09:02:05 +01:00
2021-10-05 03:06:55 +02:00
const gang = NetscriptGang ( Player , workerScript , helper ) ;
2021-10-05 04:25:21 +02:00
const sleeve = NetscriptSleeve ( Player , workerScript , helper ) ;
2021-10-11 22:38:50 +02:00
const extra = NetscriptExtra ( Player , workerScript ) ;
2021-10-05 04:25:21 +02:00
const hacknet = NetscriptHacknet ( Player , workerScript , helper ) ;
2021-10-05 04:31:07 +02:00
const stanek = NetscriptStanek ( Player , workerScript , helper ) ;
2021-10-14 09:22:02 +02:00
const bladeburner = NetscriptBladeburner ( Player , workerScript , helper ) ;
const codingcontract = NetscriptCodingContract ( Player , workerScript , helper ) ;
2021-12-04 05:06:04 +01:00
const corporation = NetscriptCorporation ( Player , workerScript , helper ) ;
2021-10-14 09:22:02 +02:00
const formulas = NetscriptFormulas ( Player , workerScript , helper ) ;
2021-11-04 01:19:52 +01:00
const singularity = NetscriptSingularity ( Player , workerScript , helper ) ;
2021-10-14 09:22:02 +02:00
const stockmarket = NetscriptStockMarket ( Player , workerScript , helper ) ;
2021-12-20 19:38:21 +01:00
const ui = NetscriptUserInterface ( Player , workerScript , helper ) ;
2021-03-11 09:02:05 +01:00
2021-11-04 01:19:52 +01:00
const base : INS = {
. . . singularity ,
gang : gang ,
bladeburner : bladeburner ,
codingcontract : codingcontract ,
sleeve : sleeve ,
2021-12-04 05:06:04 +01:00
corporation : corporation ,
2021-11-14 04:44:17 +01:00
stanek : stanek ,
2021-12-20 19:38:21 +01:00
ui : ui ,
2021-11-04 01:19:52 +01:00
formulas : formulas ,
stock : stockmarket ,
args : workerScript.args ,
2021-10-05 04:25:21 +02:00
hacknet : hacknet ,
2021-09-05 01:09:30 +02:00
sprintf : sprintf ,
vsprintf : vsprintf ,
2021-10-19 16:13:19 +02:00
scan : function ( hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "scan" , getRamCost ( Player , "scan" ) ) ;
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "scan" ) ;
2021-09-25 07:26:03 +02:00
const out = [ ] ;
for ( let i = 0 ; i < server . serversOnNetwork . length ; i ++ ) {
2021-09-25 03:49:49 +02:00
const s = getServerOnNetwork ( server , i ) ;
if ( s === null ) continue ;
2021-10-11 20:26:44 +02:00
const entry = s . hostname ;
2021-10-19 16:13:19 +02:00
if ( entry === null ) continue ;
2021-09-05 01:09:30 +02:00
out . push ( entry ) ;
}
2021-11-27 00:30:58 +01:00
workerScript . log ( "scan" , ( ) = > ` returned ${ server . serversOnNetwork . length } connections for ${ server . hostname } ` ) ;
2021-09-05 01:09:30 +02:00
return out ;
} ,
2021-10-19 16:13:19 +02:00
hack : function ( hostname : any , { threads : requestedThreads , stock } : any = { } ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "hack" , getRamCost ( Player , "hack" ) ) ;
2021-10-19 16:13:19 +02:00
return hack ( hostname , false , { threads : requestedThreads , stock : stock } ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
hackAnalyzeThreads : function ( hostname : any , hackAmount : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "hackAnalyzeThreads" , getRamCost ( Player , "hackAnalyzeThreads" ) ) ;
2021-09-05 01:09:30 +02:00
// Check argument validity
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "hackAnalyzeThreads" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "hackAnalyzeThreads" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return - 1 ;
}
2021-09-05 01:09:30 +02:00
if ( isNaN ( hackAmount ) ) {
throw makeRuntimeErrorMsg (
2021-09-25 03:49:49 +02:00
"hackAnalyzeThreads" ,
2022-01-17 23:53:44 +01:00
` Invalid hackAmount argument passed into hackAnalyzeThreads: ${ hackAmount } . Must be numeric. ` ,
2021-09-05 01:09:30 +02:00
) ;
}
2021-03-10 07:27:14 +01:00
2021-09-05 01:09:30 +02:00
if ( hackAmount < 0 || hackAmount > server . moneyAvailable ) {
return - 1 ;
2021-12-22 02:12:45 +01:00
} else if ( hackAmount === 0 ) {
return 0 ;
2021-09-05 01:09:30 +02:00
}
2021-03-10 07:27:14 +01:00
2021-09-05 01:09:30 +02:00
const percentHacked = calculatePercentMoneyHacked ( server , Player ) ;
2021-09-02 06:36:33 +02:00
2021-09-05 01:09:30 +02:00
return hackAmount / Math . floor ( server . moneyAvailable * percentHacked ) ;
} ,
2021-11-03 03:11:22 +01:00
hackAnalyze : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "hackAnalyze" , getRamCost ( Player , "hackAnalyze" ) ) ;
2021-09-02 06:36:33 +02:00
2021-11-03 03:11:22 +01:00
const server = safeGetServer ( hostname , "hackAnalyze" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "hackAnalyze" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-02 06:36:33 +02:00
2021-11-03 03:11:22 +01:00
return calculatePercentMoneyHacked ( server , Player ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-28 03:07:58 +02:00
hackAnalyzeSecurity : function ( threads : any ) : number {
return CONSTANTS . ServerFortifyAmount * threads ;
2021-09-05 01:09:30 +02:00
} ,
2021-11-03 03:11:22 +01:00
hackAnalyzeChance : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "hackAnalyzeChance" , getRamCost ( Player , "hackAnalyzeChance" ) ) ;
2021-09-02 06:36:33 +02:00
2021-11-03 03:11:22 +01:00
const server = safeGetServer ( hostname , "hackAnalyzeChance" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "hackAnalyzeChance" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
return calculateHackingChance ( server , Player ) ;
} ,
2021-09-25 03:49:49 +02:00
sleep : function ( time : any ) : any {
2021-09-05 01:09:30 +02:00
if ( time === undefined ) {
throw makeRuntimeErrorMsg ( "sleep" , "Takes 1 argument." ) ;
}
2021-11-27 00:30:58 +01:00
workerScript . log ( "sleep" , ( ) = > ` Sleeping for ${ time } milliseconds ` ) ;
2021-09-05 01:09:30 +02:00
return netscriptDelay ( time , workerScript ) . then ( function ( ) {
return Promise . resolve ( true ) ;
} ) ;
} ,
2021-11-03 03:11:22 +01:00
asleep : function ( time : any ) : any {
if ( time === undefined ) {
throw makeRuntimeErrorMsg ( "asleep" , "Takes 1 argument." ) ;
}
2021-11-27 00:30:58 +01:00
workerScript . log ( "asleep" , ( ) = > ` Sleeping for ${ time } milliseconds ` ) ;
2022-01-19 00:57:43 +01:00
return new Promise ( ( resolve ) = > setTimeout ( resolve , time ) ) ;
2021-11-03 03:11:22 +01:00
} ,
2021-10-19 16:13:19 +02:00
grow : function ( hostname : any , { threads : requestedThreads , stock } : any = { } ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "grow" , getRamCost ( Player , "grow" ) ) ;
2021-09-09 05:47:34 +02:00
const threads = resolveNetscriptRequestedThreads ( workerScript , "grow" , requestedThreads ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "grow" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "grow" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "grow" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
2021-10-07 23:55:49 +02:00
const host = GetServer ( workerScript . hostname ) ;
2021-10-05 21:23:30 +02:00
if ( host === null ) {
throw new Error ( "Workerscript host is null" ) ;
2021-09-25 03:49:49 +02:00
}
2021-09-05 01:09:30 +02:00
// No root access or skill level too low
const canHack = netscriptCanGrow ( server ) ;
if ( ! canHack . res ) {
2021-09-25 03:49:49 +02:00
throw makeRuntimeErrorMsg ( "grow" , canHack . msg || "" ) ;
2021-09-05 01:09:30 +02:00
}
2021-03-12 02:37:58 +01:00
2021-09-25 07:26:03 +02:00
const growTime = calculateGrowTime ( server , Player ) ;
2021-09-05 01:09:30 +02:00
workerScript . log (
"grow" ,
2021-11-27 00:30:58 +01:00
( ) = >
` Executing on ' ${ server . hostname } ' in ${ convertTimeMsToTimeElapsedString (
growTime * 1000 ,
true ,
) } ( t = $ { numeralWrapper . formatThreads ( threads ) } ) . ` ,
2021-09-05 01:09:30 +02:00
) ;
return netscriptDelay ( growTime * 1000 , workerScript ) . then ( function ( ) {
2021-09-09 05:47:34 +02:00
const moneyBefore = server . moneyAvailable <= 0 ? 1 : server.moneyAvailable ;
2021-09-05 01:09:30 +02:00
processSingleServerGrowth ( server , threads , Player , host . cpuCores ) ;
const moneyAfter = server . moneyAvailable ;
2021-10-07 23:55:49 +02:00
workerScript . scriptRef . recordGrow ( server . hostname , threads ) ;
2021-09-25 07:26:03 +02:00
const expGain = calculateHackingExpGain ( server , Player ) * threads ;
2021-09-05 01:09:30 +02:00
const logGrowPercent = moneyAfter / moneyBefore - 1 ;
workerScript . log (
"grow" ,
2021-11-27 00:30:58 +01:00
( ) = >
` Available money on ' ${ server . hostname } ' grown by ${ numeralWrapper . formatPercentage (
logGrowPercent ,
6 ,
) } . Gained $ { numeralWrapper . formatExp ( expGain ) } hacking exp ( t = $ { numeralWrapper . formatThreads ( threads ) } ) . ` ,
2021-09-05 01:09:30 +02:00
) ;
workerScript . scriptRef . onlineExpGained += expGain ;
Player . gainHackingExp ( expGain ) ;
if ( stock ) {
influenceStockThroughServerGrow ( server , moneyAfter - moneyBefore ) ;
}
return Promise . resolve ( moneyAfter / moneyBefore ) ;
} ) ;
} ,
2021-10-19 16:13:19 +02:00
growthAnalyze : function ( hostname : any , growth : any , cores : any = 1 ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "growthAnalyze" , getRamCost ( Player , "growthAnalyze" ) ) ;
2021-09-05 01:09:30 +02:00
// Check argument validity
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "growthAnalyze" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "growthAnalyze" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-09 05:47:34 +02:00
if ( typeof growth !== "number" || isNaN ( growth ) || growth < 1 || ! isFinite ( growth ) ) {
throw makeRuntimeErrorMsg ( "growthAnalyze" , ` Invalid argument: growth must be numeric and >= 1, is ${ growth } . ` ) ;
2021-09-05 01:09:30 +02:00
}
2021-03-24 02:30:15 +01:00
2021-10-08 18:09:44 +02:00
return numCycleForGrowth ( server , Number ( growth ) , Player , cores ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-28 03:07:58 +02:00
growthAnalyzeSecurity : function ( threads : any ) : number {
return 2 * CONSTANTS . ServerFortifyAmount * threads ;
} ,
2021-10-19 16:13:19 +02:00
weaken : function ( hostname : any , { threads : requestedThreads } : any = { } ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "weaken" , getRamCost ( Player , "weaken" ) ) ;
2021-09-25 07:26:03 +02:00
const threads = resolveNetscriptRequestedThreads ( workerScript , "weaken" , requestedThreads ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "weaken" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "weaken" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "weaken" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
// No root access or skill level too low
const canHack = netscriptCanWeaken ( server ) ;
if ( ! canHack . res ) {
2021-09-25 03:49:49 +02:00
throw makeRuntimeErrorMsg ( "weaken" , canHack . msg || "" ) ;
2021-09-05 01:09:30 +02:00
}
const weakenTime = calculateWeakenTime ( server , Player ) ;
workerScript . log (
"weaken" ,
2021-11-27 00:30:58 +01:00
( ) = >
` Executing on ' ${ server . hostname } ' in ${ convertTimeMsToTimeElapsedString (
weakenTime * 1000 ,
true ,
) } ( t = $ { numeralWrapper . formatThreads ( threads ) } ) ` ,
2021-09-05 01:09:30 +02:00
) ;
return netscriptDelay ( weakenTime * 1000 , workerScript ) . then ( function ( ) {
2021-10-07 23:55:49 +02:00
const host = GetServer ( workerScript . hostname ) ;
2021-09-25 03:49:49 +02:00
if ( host === null ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "weaken" , ( ) = > "Server is null, did it die?" ) ;
2021-09-25 03:49:49 +02:00
return Promise . resolve ( 0 ) ;
}
2021-09-05 01:09:30 +02:00
const coreBonus = 1 + ( host . cpuCores - 1 ) / 16 ;
server . weaken ( CONSTANTS . ServerWeakenAmount * threads * coreBonus ) ;
2021-10-07 23:55:49 +02:00
workerScript . scriptRef . recordWeaken ( server . hostname , threads ) ;
2021-09-05 01:09:30 +02:00
const expGain = calculateHackingExpGain ( server , Player ) * threads ;
workerScript . log (
"weaken" ,
2021-11-27 00:30:58 +01:00
( ) = >
2022-02-13 10:08:55 +01:00
` ' ${ server . hostname } ' security level weakened to ${ server . hackDifficulty
2021-11-27 00:30:58 +01:00
} . Gained $ { numeralWrapper . formatExp ( expGain ) } hacking exp ( t = $ { numeralWrapper . formatThreads ( threads ) } ) ` ,
2021-09-05 01:09:30 +02:00
) ;
workerScript . scriptRef . onlineExpGained += expGain ;
Player . gainHackingExp ( expGain ) ;
2021-10-28 03:07:58 +02:00
return Promise . resolve ( CONSTANTS . ServerWeakenAmount * threads * coreBonus ) ;
2021-09-05 01:09:30 +02:00
} ) ;
} ,
2021-10-28 03:07:58 +02:00
weakenAnalyze : function ( threads : any , cores : any = 1 ) : number {
const coreBonus = 1 + ( cores - 1 ) / 16 ;
return CONSTANTS . ServerWeakenAmount * threads * coreBonus ;
} ,
2022-01-18 20:02:12 +01:00
share : function ( ) : Promise < void > {
workerScript . log ( "share" , ( ) = > "Sharing this computer." ) ;
const end = StartSharing ( workerScript . scriptRef . threads * calculateIntelligenceBonus ( Player . intelligence , 2 ) ) ;
return netscriptDelay ( 10000 , workerScript ) . finally ( function ( ) {
workerScript . log ( "share" , ( ) = > "Finished sharing this computer." ) ;
end ( ) ;
} ) ;
} ,
2022-01-19 00:57:43 +01:00
getSharePower : function ( ) : number {
2022-01-18 20:02:12 +01:00
return CalculateShareMult ( ) ;
} ,
2021-09-25 08:36:49 +02:00
print : function ( . . . args : any [ ] ) : void {
if ( args . length === 0 ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "print" , "Takes at least 1 argument." ) ;
}
2021-09-25 08:36:49 +02:00
workerScript . print ( argsToString ( args ) ) ;
2021-09-05 01:09:30 +02:00
} ,
2022-01-21 12:34:10 +01:00
printf : function ( format : string , . . . args : any [ ] ) : void {
if ( typeof format !== "string" ) {
throw makeRuntimeErrorMsg ( "printf" , "First argument must be string for the format." ) ;
}
workerScript . print ( vsprintf ( format , args ) ) ;
} ,
2021-09-25 08:36:49 +02:00
tprint : function ( . . . args : any [ ] ) : void {
if ( args . length === 0 ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "tprint" , "Takes at least 1 argument." ) ;
}
2021-11-19 21:44:12 +01:00
const str = argsToString ( args ) ;
if ( str . startsWith ( "ERROR" ) || str . startsWith ( "FAIL" ) ) {
Terminal . error ( ` ${ workerScript . scriptRef . filename } : ${ str } ` ) ;
return ;
}
if ( str . startsWith ( "SUCCESS" ) ) {
Terminal . success ( ` ${ workerScript . scriptRef . filename } : ${ str } ` ) ;
return ;
}
if ( str . startsWith ( "WARN" ) ) {
Terminal . warn ( ` ${ workerScript . scriptRef . filename } : ${ str } ` ) ;
return ;
}
if ( str . startsWith ( "INFO" ) ) {
Terminal . info ( ` ${ workerScript . scriptRef . filename } : ${ str } ` ) ;
return ;
}
Terminal . print ( ` ${ workerScript . scriptRef . filename } : ${ str } ` ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-09-25 03:49:49 +02:00
tprintf : function ( format : any , . . . args : any ) : any {
2021-11-20 04:58:41 +01:00
if ( typeof format !== "string" ) {
throw makeRuntimeErrorMsg ( "tprintf" , "First argument must be string for the format." ) ;
}
2021-11-19 21:44:12 +01:00
const str = vsprintf ( format , args ) ;
if ( str . startsWith ( "ERROR" ) || str . startsWith ( "FAIL" ) ) {
2021-11-20 20:54:31 +01:00
Terminal . error ( ` ${ str } ` ) ;
2021-11-19 21:44:12 +01:00
return ;
}
if ( str . startsWith ( "SUCCESS" ) ) {
2021-11-20 20:54:31 +01:00
Terminal . success ( ` ${ str } ` ) ;
2021-11-19 21:44:12 +01:00
return ;
}
if ( str . startsWith ( "WARN" ) ) {
2021-11-20 20:54:31 +01:00
Terminal . warn ( ` ${ str } ` ) ;
2021-11-19 21:44:12 +01:00
return ;
}
if ( str . startsWith ( "INFO" ) ) {
2021-11-20 20:54:31 +01:00
Terminal . info ( ` ${ str } ` ) ;
2021-11-19 21:44:12 +01:00
return ;
}
2021-11-20 20:54:31 +01:00
Terminal . print ( ` ${ str } ` ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-09-25 03:49:49 +02:00
clearLog : function ( ) : any {
2021-09-05 01:09:30 +02:00
workerScript . scriptRef . clearLog ( ) ;
} ,
2021-09-25 03:49:49 +02:00
disableLog : function ( fn : any ) : any {
2021-09-05 01:09:30 +02:00
if ( fn === "ALL" ) {
2022-01-16 01:45:03 +01:00
for ( fn of Object . keys ( possibleLogs ) ) {
2021-09-05 01:09:30 +02:00
workerScript . disableLogs [ fn ] = true ;
2021-04-18 17:18:56 +02:00
}
2021-11-27 00:30:58 +01:00
workerScript . log ( "disableLog" , ( ) = > ` Disabled logging for all functions ` ) ;
2021-09-05 01:09:30 +02:00
} else if ( possibleLogs [ fn ] === undefined ) {
throw makeRuntimeErrorMsg ( "disableLog" , ` Invalid argument: ${ fn } . ` ) ;
} else {
workerScript . disableLogs [ fn ] = true ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "disableLog" , ( ) = > ` Disabled logging for ${ fn } ` ) ;
2021-09-05 01:09:30 +02:00
}
} ,
2021-09-25 03:49:49 +02:00
enableLog : function ( fn : any ) : any {
2021-12-17 17:33:33 +01:00
if ( fn === "ALL" ) {
2022-01-16 01:45:03 +01:00
for ( fn of Object . keys ( possibleLogs ) ) {
2021-12-17 17:33:33 +01:00
delete workerScript . disableLogs [ fn ] ;
}
workerScript . log ( "enableLog" , ( ) = > ` Enabled logging for all functions ` ) ;
} else if ( possibleLogs [ fn ] === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "enableLog" , ` Invalid argument: ${ fn } . ` ) ;
}
delete workerScript . disableLogs [ fn ] ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "enableLog" , ( ) = > ` Enabled logging for ${ fn } ` ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-09-25 03:49:49 +02:00
isLogEnabled : function ( fn : any ) : any {
2021-09-05 01:09:30 +02:00
if ( possibleLogs [ fn ] === undefined ) {
throw makeRuntimeErrorMsg ( "isLogEnabled" , ` Invalid argument: ${ fn } . ` ) ;
}
return workerScript . disableLogs [ fn ] ? false : true ;
} ,
2021-10-19 16:13:19 +02:00
getScriptLogs : function ( fn : any , hostname : any , . . . scriptArgs : any ) : any {
const runningScriptObj = getRunningScript ( fn , hostname , "getScriptLogs" , scriptArgs ) ;
2021-09-05 01:09:30 +02:00
if ( runningScriptObj == null ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getScriptLogs" , ( ) = > getCannotFindRunningScriptErrorMessage ( fn , hostname , scriptArgs ) ) ;
2021-09-05 01:09:30 +02:00
return "" ;
}
2021-04-18 17:18:56 +02:00
2021-09-05 01:09:30 +02:00
return runningScriptObj . logs . slice ( ) ;
} ,
2021-10-19 16:13:19 +02:00
tail : function ( fn : any , hostname : any = workerScript . hostname , . . . scriptArgs : any ) : any {
2021-09-05 01:09:30 +02:00
let runningScriptObj ;
if ( arguments . length === 0 ) {
runningScriptObj = workerScript . scriptRef ;
} else if ( typeof fn === "number" ) {
runningScriptObj = getRunningScriptByPid ( fn , "tail" ) ;
} else {
2021-10-19 16:13:19 +02:00
runningScriptObj = getRunningScript ( fn , hostname , "tail" , scriptArgs ) ;
2021-09-05 01:09:30 +02:00
}
if ( runningScriptObj == null ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "tail" , ( ) = > getCannotFindRunningScriptErrorMessage ( fn , hostname , scriptArgs ) ) ;
2021-09-05 01:09:30 +02:00
return ;
}
2021-04-18 17:18:56 +02:00
2021-10-01 07:00:50 +02:00
LogBoxEvents . emit ( runningScriptObj ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
nuke : function ( hostname : any ) : boolean {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "nuke" , getRamCost ( Player , "nuke" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "nuke" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "nuke" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "nuke" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2022-01-04 22:40:21 +01:00
if ( server . hasAdminRights ) {
workerScript . log ( "nuke" , ( ) = > ` Already have root access to ' ${ server . hostname } '. ` ) ;
return true ;
}
2021-09-05 01:09:30 +02:00
if ( ! Player . hasProgram ( Programs . NukeProgram . name ) ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "nuke" , "You do not have the NUKE.exe virus!" ) ;
2021-09-05 01:09:30 +02:00
}
if ( server . openPortCount < server . numOpenPortsRequired ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "nuke" , "Not enough ports opened to use NUKE.exe virus." ) ;
2021-09-05 01:09:30 +02:00
}
2022-01-04 22:40:21 +01:00
server . hasAdminRights = true ;
workerScript . log ( "nuke" , ( ) = > ` Executed NUKE.exe virus on ' ${ server . hostname } ' to gain root access. ` ) ;
2021-09-05 01:09:30 +02:00
return true ;
} ,
2021-10-19 16:13:19 +02:00
brutessh : function ( hostname : any ) : boolean {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "brutessh" , getRamCost ( Player , "brutessh" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "brutessh" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "brutessh" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "brutessh" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
if ( ! Player . hasProgram ( Programs . BruteSSHProgram . name ) ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "brutessh" , "You do not have the BruteSSH.exe program!" ) ;
2021-09-05 01:09:30 +02:00
}
if ( ! server . sshPortOpen ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "brutessh" , ( ) = > ` Executed BruteSSH.exe on ' ${ server . hostname } ' to open SSH port (22). ` ) ;
2021-09-05 01:09:30 +02:00
server . sshPortOpen = true ;
++ server . openPortCount ;
} else {
2021-11-27 00:30:58 +01:00
workerScript . log ( "brutessh" , ( ) = > ` SSH Port (22) already opened on ' ${ server . hostname } '. ` ) ;
2021-09-05 01:09:30 +02:00
}
return true ;
} ,
2021-10-19 16:13:19 +02:00
ftpcrack : function ( hostname : any ) : boolean {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "ftpcrack" , getRamCost ( Player , "ftpcrack" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "ftpcrack" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "ftpcrack" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "ftpcrack" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
if ( ! Player . hasProgram ( Programs . FTPCrackProgram . name ) ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "ftpcrack" , "You do not have the FTPCrack.exe program!" ) ;
2021-09-05 01:09:30 +02:00
}
if ( ! server . ftpPortOpen ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "ftpcrack" , ( ) = > ` Executed FTPCrack.exe on ' ${ server . hostname } ' to open FTP port (21). ` ) ;
2021-09-05 01:09:30 +02:00
server . ftpPortOpen = true ;
++ server . openPortCount ;
} else {
2021-11-27 00:30:58 +01:00
workerScript . log ( "ftpcrack" , ( ) = > ` FTP Port (21) already opened on ' ${ server . hostname } '. ` ) ;
2021-09-05 01:09:30 +02:00
}
return true ;
} ,
2021-10-19 16:13:19 +02:00
relaysmtp : function ( hostname : any ) : boolean {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "relaysmtp" , getRamCost ( Player , "relaysmtp" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "relaysmtp" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "relaysmtp" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "relaysmtp" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
if ( ! Player . hasProgram ( Programs . RelaySMTPProgram . name ) ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "relaysmtp" , "You do not have the relaySMTP.exe program!" ) ;
2021-09-05 01:09:30 +02:00
}
if ( ! server . smtpPortOpen ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "relaysmtp" , ( ) = > ` Executed relaySMTP.exe on ' ${ server . hostname } ' to open SMTP port (25). ` ) ;
2021-09-05 01:09:30 +02:00
server . smtpPortOpen = true ;
++ server . openPortCount ;
} else {
2021-11-27 00:30:58 +01:00
workerScript . log ( "relaysmtp" , ( ) = > ` SMTP Port (25) already opened on ' ${ server . hostname } '. ` ) ;
2021-09-05 01:09:30 +02:00
}
return true ;
} ,
2021-10-19 16:13:19 +02:00
httpworm : function ( hostname : any ) : boolean {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "httpworm" , getRamCost ( Player , "httpworm" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "httpworm" , "Takes 1 argument" ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "httpworm" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "httpworm" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
if ( ! Player . hasProgram ( Programs . HTTPWormProgram . name ) ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "httpworm" , "You do not have the HTTPWorm.exe program!" ) ;
2021-09-05 01:09:30 +02:00
}
if ( ! server . httpPortOpen ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "httpworm" , ( ) = > ` Executed HTTPWorm.exe on ' ${ server . hostname } ' to open HTTP port (80). ` ) ;
2021-09-05 01:09:30 +02:00
server . httpPortOpen = true ;
++ server . openPortCount ;
} else {
2021-11-27 00:30:58 +01:00
workerScript . log ( "httpworm" , ( ) = > ` HTTP Port (80) already opened on ' ${ server . hostname } '. ` ) ;
2021-09-05 01:09:30 +02:00
}
return true ;
} ,
2021-10-19 16:13:19 +02:00
sqlinject : function ( hostname : any ) : boolean {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "sqlinject" , getRamCost ( Player , "sqlinject" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "sqlinject" , "Takes 1 argument." ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "sqlinject" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "sqlinject" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2021-09-05 01:09:30 +02:00
if ( ! Player . hasProgram ( Programs . SQLInjectProgram . name ) ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "sqlinject" , "You do not have the SQLInject.exe program!" ) ;
2021-09-05 01:09:30 +02:00
}
if ( ! server . sqlPortOpen ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "sqlinject" , ( ) = > ` Executed SQLInject.exe on ' ${ server . hostname } ' to open SQL port (1433). ` ) ;
2021-09-05 01:09:30 +02:00
server . sqlPortOpen = true ;
++ server . openPortCount ;
} else {
2021-11-27 00:30:58 +01:00
workerScript . log ( "sqlinject" , ( ) = > ` SQL Port (1433) already opened on ' ${ server . hostname } '. ` ) ;
2021-09-05 01:09:30 +02:00
}
return true ;
} ,
2021-09-25 08:36:49 +02:00
run : function ( scriptname : any , threads : any = 1 , . . . args : any [ ] ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "run" , getRamCost ( Player , "run" ) ) ;
2021-09-05 01:09:30 +02:00
if ( scriptname === undefined ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "run" , "Usage: run(scriptname, [numThreads], [arg1], [arg2]...)" ) ;
2021-09-05 01:09:30 +02:00
}
if ( isNaN ( threads ) || threads <= 0 ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "run" , ` Invalid thread count. Must be numeric and > 0, is ${ threads } ` ) ;
2021-09-05 01:09:30 +02:00
}
2021-10-07 23:55:49 +02:00
const scriptServer = GetServer ( workerScript . hostname ) ;
2021-09-05 01:09:30 +02:00
if ( scriptServer == null ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "run" , "Could not find server. This is a bug. Report to dev." ) ;
2021-09-05 01:09:30 +02:00
}
2019-01-03 09:39:32 +01:00
2022-01-05 01:09:34 +01:00
return runScriptFromScript ( Player , "run" , scriptServer , scriptname , args , workerScript , threads ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
exec : function ( scriptname : any , hostname : any , threads : any = 1 , . . . args : any [ ] ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "exec" , getRamCost ( Player , "exec" ) ) ;
2021-10-19 16:13:19 +02:00
if ( scriptname === undefined || hostname === undefined ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "exec" , "Usage: exec(scriptname, server, [numThreads], [arg1], [arg2]...)" ) ;
2021-09-05 01:09:30 +02:00
}
if ( isNaN ( threads ) || threads <= 0 ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "exec" , ` Invalid thread count. Must be numeric and > 0, is ${ threads } ` ) ;
2021-09-05 01:09:30 +02:00
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "exec" ) ;
2022-01-05 01:09:34 +01:00
return runScriptFromScript ( Player , "exec" , server , scriptname , args , workerScript , threads ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-11-19 00:00:20 +01:00
spawn : function ( scriptname : any , threads : any = 1 , . . . args : any [ ] ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "spawn" , getRamCost ( Player , "spawn" ) ) ;
2021-09-05 01:09:30 +02:00
if ( ! scriptname || ! threads ) {
throw makeRuntimeErrorMsg ( "spawn" , "Usage: spawn(scriptname, threads)" ) ;
}
2019-01-03 09:39:32 +01:00
2021-09-05 01:09:30 +02:00
const spawnDelay = 10 ;
2021-10-05 01:58:34 +02:00
setTimeout ( ( ) = > {
2021-09-05 01:09:30 +02:00
if ( isNaN ( threads ) || threads <= 0 ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "spawn" , ` Invalid thread count. Must be numeric and > 0, is ${ threads } ` ) ;
2021-09-05 01:09:30 +02:00
}
2021-10-07 23:55:49 +02:00
const scriptServer = GetServer ( workerScript . hostname ) ;
2021-09-05 01:09:30 +02:00
if ( scriptServer == null ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "spawn" , "Could not find server. This is a bug. Report to dev" ) ;
2021-09-05 01:09:30 +02:00
}
2019-01-03 09:39:32 +01:00
2022-01-05 01:09:34 +01:00
return runScriptFromScript ( Player , "spawn" , scriptServer , scriptname , args , workerScript , threads ) ;
2021-09-05 01:09:30 +02:00
} , spawnDelay * 1 e3 ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "spawn" , ( ) = > ` Will execute ' ${ scriptname } ' in ${ spawnDelay } seconds ` ) ;
2021-09-05 01:09:30 +02:00
workerScript . running = false ; // Prevent workerScript from "finishing execution naturally"
if ( killWorkerScript ( workerScript ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "spawn" , ( ) = > "Exiting..." ) ;
2021-09-05 01:09:30 +02:00
}
} ,
2021-12-31 01:12:43 +01:00
kill : function ( filename : any , hostname? : any , . . . scriptArgs : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "kill" , getRamCost ( Player , "kill" ) ) ;
2021-09-05 01:09:30 +02:00
let res ;
const killByPid = typeof filename === "number" ;
if ( killByPid ) {
// Kill by pid
res = killWorkerScript ( filename ) ;
} else {
2021-10-19 16:13:19 +02:00
// Kill by filename/hostname
if ( filename === undefined || hostname === undefined ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "kill" , "Usage: kill(scriptname, server, [arg1], [arg2]...)" ) ;
2021-09-05 01:09:30 +02:00
}
2019-01-03 09:39:32 +01:00
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "kill" ) ;
const runningScriptObj = getRunningScript ( filename , hostname , "kill" , scriptArgs ) ;
2021-09-05 01:09:30 +02:00
if ( runningScriptObj == null ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "kill" , ( ) = > getCannotFindRunningScriptErrorMessage ( filename , hostname , scriptArgs ) ) ;
2021-09-05 01:09:30 +02:00
return false ;
}
2019-01-03 09:39:32 +01:00
2021-10-07 23:55:49 +02:00
res = killWorkerScript ( runningScriptObj , server . hostname ) ;
2021-09-05 01:09:30 +02:00
}
2019-01-03 09:39:32 +01:00
2021-09-05 01:09:30 +02:00
if ( res ) {
if ( killByPid ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "kill" , ( ) = > ` Killing script with PID ${ filename } ` ) ;
2021-09-05 01:09:30 +02:00
} else {
2021-11-27 00:30:58 +01:00
workerScript . log (
"kill" ,
( ) = > ` Killing ' ${ filename } ' on ' ${ hostname } ' with args: ${ arrayToString ( scriptArgs ) } . ` ,
) ;
2021-09-05 01:09:30 +02:00
}
return true ;
} else {
if ( killByPid ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "kill" , ( ) = > ` No script with PID ${ filename } ` ) ;
2021-09-05 01:09:30 +02:00
} else {
2021-10-23 16:09:43 +02:00
workerScript . log (
"kill" ,
2021-11-27 00:30:58 +01:00
( ) = > ` No such script ' ${ filename } ' on ' ${ hostname } ' with args: ${ arrayToString ( scriptArgs ) } ` ,
2021-10-23 16:09:43 +02:00
) ;
2021-09-05 01:09:30 +02:00
}
return false ;
}
} ,
2021-10-19 16:13:19 +02:00
killall : function ( hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "killall" , getRamCost ( Player , "killall" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "killall" , "Takes 1 argument" ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "killall" ) ;
2021-09-05 01:09:30 +02:00
const scriptsRunning = server . runningScripts . length > 0 ;
for ( let i = server . runningScripts . length - 1 ; i >= 0 ; -- i ) {
2021-10-07 23:55:49 +02:00
killWorkerScript ( server . runningScripts [ i ] , server . hostname , false ) ;
2021-09-05 01:09:30 +02:00
}
2021-09-18 21:44:39 +02:00
WorkerScriptStartStopEventEmitter . emit ( ) ;
2021-09-05 01:09:30 +02:00
workerScript . log (
"killall" ,
2021-11-27 00:30:58 +01:00
( ) = > ` Killing all scripts on ' ${ server . hostname } '. May take a few minutes for the scripts to die. ` ,
2021-09-05 01:09:30 +02:00
) ;
return scriptsRunning ;
} ,
2021-09-25 03:49:49 +02:00
exit : function ( ) : any {
2021-09-05 01:09:30 +02:00
workerScript . running = false ; // Prevent workerScript from "finishing execution naturally"
if ( killWorkerScript ( workerScript ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "exit" , ( ) = > "Exiting..." ) ;
2021-09-05 01:09:30 +02:00
} else {
2021-11-27 00:30:58 +01:00
workerScript . log ( "exit" , ( ) = > "Failed. This is a bug. Report to dev." ) ;
2021-09-05 01:09:30 +02:00
}
} ,
2022-01-15 22:42:04 +01:00
scp : async function ( scriptname : any , hostname1 : any , hostname2? : any ) : Promise < boolean > {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "scp" , getRamCost ( Player , "scp" ) ) ;
2021-09-05 01:09:30 +02:00
if ( arguments . length !== 2 && arguments . length !== 3 ) {
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
}
if ( scriptname && scriptname . constructor === Array ) {
// Recursively call scp on all elements of array
2021-12-13 01:50:41 +01:00
const scripts : Array < string > = scriptname ;
if ( scripts . length === 0 ) {
throw makeRuntimeErrorMsg ( "scp" , "No scripts to copy" ) ;
}
let res = true ;
2021-12-15 01:39:55 +01:00
await Promise . all (
scripts . map ( async function ( script ) {
if ( ! ( await NetscriptFunctions ( workerScript ) . scp ( script , hostname1 , hostname2 ) ) ) {
res = false ;
}
} ) ,
) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( res ) ;
2021-09-05 01:09:30 +02:00
}
2019-01-03 09:39:32 +01:00
2021-09-05 01:09:30 +02:00
// Invalid file type
if ( ! isValidFilePath ( scriptname ) ) {
throw makeRuntimeErrorMsg ( "scp" , ` Invalid filename: ' ${ scriptname } ' ` ) ;
}
2019-01-03 09:39:32 +01:00
2021-09-05 01:09:30 +02:00
// Invalid file name
2021-09-09 05:47:34 +02:00
if ( ! scriptname . endsWith ( ".lit" ) && ! isScriptFilename ( scriptname ) && ! scriptname . endsWith ( "txt" ) ) {
throw makeRuntimeErrorMsg ( "scp" , "Only works for .script, .lit, and .txt files" ) ;
2021-09-05 01:09:30 +02:00
}
2017-07-25 03:06:40 +02:00
2021-11-10 13:52:26 +01:00
let destServer : BaseServer | null ;
let currServ : BaseServer | null ;
2021-08-19 22:37:59 +02:00
2021-10-19 16:13:19 +02:00
if ( hostname2 != null ) {
2021-09-05 01:09:30 +02:00
// 3 Argument version: scriptname, source, destination
2021-10-19 16:13:19 +02:00
if ( scriptname === undefined || hostname1 === undefined || hostname2 === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
}
2021-10-23 16:09:43 +02:00
destServer = safeGetServer ( hostname2 , "scp" ) ;
2021-10-19 16:13:19 +02:00
currServ = safeGetServer ( hostname1 , "scp" ) ;
} else if ( hostname1 != null ) {
2021-09-05 01:09:30 +02:00
// 2 Argument version: scriptname, destination
2021-10-19 16:13:19 +02:00
if ( scriptname === undefined || hostname1 === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
}
2021-10-23 16:09:43 +02:00
destServer = safeGetServer ( hostname1 , "scp" ) ;
2021-10-07 23:55:49 +02:00
currServ = GetServer ( workerScript . hostname ) ;
2021-09-05 01:09:30 +02:00
if ( currServ == null ) {
2021-10-23 16:09:43 +02:00
throw makeRuntimeErrorMsg (
"scp" ,
"Could not find server hostname for this script. This is a bug. Report to dev." ,
) ;
2021-09-05 01:09:30 +02:00
}
} else {
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
}
2018-11-20 06:54:03 +01:00
2021-09-05 01:09:30 +02:00
// Scp for lit files
if ( scriptname . endsWith ( ".lit" ) ) {
2021-09-25 07:26:03 +02:00
let found = false ;
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < currServ . messages . length ; ++ i ) {
2021-10-14 08:07:05 +02:00
if ( currServ . messages [ i ] == scriptname ) {
2021-09-05 01:09:30 +02:00
found = true ;
break ;
}
}
2017-07-25 03:06:40 +02:00
2021-09-05 01:09:30 +02:00
if ( ! found ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' does not exist. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( false ) ;
2021-09-05 01:09:30 +02:00
}
2017-07-25 03:06:40 +02:00
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < destServer . messages . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( destServer . messages [ i ] === scriptname ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' copied over to ' ${ destServer ? . hostname } '. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( true ) ; // Already exists
2021-09-05 01:09:30 +02:00
}
}
destServer . messages . push ( scriptname ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' copied over to ' ${ destServer ? . hostname } '. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( true ) ;
2021-09-05 01:09:30 +02:00
}
2017-09-07 07:45:14 +02:00
2021-09-05 01:09:30 +02:00
// Scp for text files
if ( scriptname . endsWith ( ".txt" ) ) {
2021-09-25 03:49:49 +02:00
let txtFile ;
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < currServ . textFiles . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( currServ . textFiles [ i ] . fn === scriptname ) {
txtFile = currServ . textFiles [ i ] ;
break ;
}
}
2021-09-25 03:49:49 +02:00
if ( txtFile === undefined ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' does not exist. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( false ) ;
2021-09-05 01:09:30 +02:00
}
2017-07-25 03:06:40 +02:00
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < destServer . textFiles . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( destServer . textFiles [ i ] . fn === scriptname ) {
// Overwrite
destServer . textFiles [ i ] . text = txtFile . text ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' copied over to ' ${ destServer ? . hostname } '. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( true ) ;
2021-09-05 01:09:30 +02:00
}
}
2021-09-25 07:26:03 +02:00
const newFile = new TextFile ( txtFile . fn , txtFile . text ) ;
2021-09-05 01:09:30 +02:00
destServer . textFiles . push ( newFile ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' copied over to ' ${ destServer ? . hostname } '. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( true ) ;
2021-09-05 01:09:30 +02:00
}
2018-09-23 02:25:48 +02:00
2021-09-05 01:09:30 +02:00
// Scp for script files
let sourceScript = null ;
for ( let i = 0 ; i < currServ . scripts . length ; ++ i ) {
if ( scriptname == currServ . scripts [ i ] . filename ) {
sourceScript = currServ . scripts [ i ] ;
break ;
}
}
if ( sourceScript == null ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' does not exist. ` ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( false ) ;
2021-09-05 01:09:30 +02:00
}
2019-04-29 08:20:27 +02:00
2021-09-05 01:09:30 +02:00
// Overwrite script if it already exists
for ( let i = 0 ; i < destServer . scripts . length ; ++ i ) {
if ( scriptname == destServer . scripts [ i ] . filename ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` WARNING: File ' ${ scriptname } ' overwritten on ' ${ destServer ? . hostname } ' ` ) ;
2021-09-05 01:09:30 +02:00
const oldScript = destServer . scripts [ i ] ;
// If it's the exact same file don't actually perform the
// copy to avoid recompiling uselessly. Players tend to scp
// liberally.
2021-11-03 03:11:22 +01:00
if ( oldScript . code === sourceScript . code ) return Promise . resolve ( true ) ;
2021-09-05 01:09:30 +02:00
oldScript . code = sourceScript . code ;
oldScript . ramUsage = sourceScript . ramUsage ;
oldScript . markUpdated ( ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( true ) ;
2021-09-05 01:09:30 +02:00
}
}
2019-04-29 08:20:27 +02:00
2021-09-05 01:09:30 +02:00
// Create new script if it does not already exist
2022-01-05 04:21:44 +01:00
const newScript = new Script ( Player , scriptname ) ;
2021-09-05 01:09:30 +02:00
newScript . code = sourceScript . code ;
newScript . ramUsage = sourceScript . ramUsage ;
2021-10-07 23:55:49 +02:00
newScript . server = destServer . hostname ;
2021-09-05 01:09:30 +02:00
destServer . scripts . push ( newScript ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "scp" , ( ) = > ` File ' ${ scriptname } ' copied over to ' ${ destServer ? . hostname } '. ` ) ;
2021-11-10 13:52:26 +01:00
return new Promise ( ( resolve ) = > {
if ( destServer === null ) {
resolve ( false ) ;
return ;
}
2022-01-05 01:09:34 +01:00
newScript . updateRamUsage ( Player , destServer . scripts ) . then ( ( ) = > resolve ( true ) ) ;
2021-11-10 13:52:26 +01:00
} ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
ls : function ( hostname : any , grep : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "ls" , getRamCost ( Player , "ls" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
throw makeRuntimeErrorMsg ( "ls" , "Usage: ls(hostname/ip, [grep filter])" ) ;
2021-09-05 01:09:30 +02:00
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "ls" ) ;
2019-04-29 08:20:27 +02:00
2021-09-05 01:09:30 +02:00
// Get the grep filter, if one exists
2021-09-25 03:49:49 +02:00
let filter = "" ;
2021-09-05 01:09:30 +02:00
if ( arguments . length >= 2 ) {
filter = grep . toString ( ) ;
}
2019-01-09 11:06:49 +01:00
2021-09-05 01:09:30 +02:00
const allFiles = [ ] ;
for ( let i = 0 ; i < server . programs . length ; i ++ ) {
if ( filter ) {
if ( server . programs [ i ] . includes ( filter ) ) {
allFiles . push ( server . programs [ i ] ) ;
}
} else {
allFiles . push ( server . programs [ i ] ) ;
}
}
for ( let i = 0 ; i < server . scripts . length ; i ++ ) {
if ( filter ) {
if ( server . scripts [ i ] . filename . includes ( filter ) ) {
allFiles . push ( server . scripts [ i ] . filename ) ;
}
} else {
allFiles . push ( server . scripts [ i ] . filename ) ;
}
}
for ( let i = 0 ; i < server . messages . length ; i ++ ) {
if ( filter ) {
2021-09-25 03:49:49 +02:00
const msg = server . messages [ i ] ;
2021-10-14 08:07:05 +02:00
if ( msg . includes ( filter ) ) {
2021-09-25 03:49:49 +02:00
allFiles . push ( msg ) ;
2021-09-05 01:09:30 +02:00
}
} else {
2021-10-14 08:07:05 +02:00
allFiles . push ( server . messages [ i ] ) ;
2021-09-05 01:09:30 +02:00
}
}
2019-06-03 05:28:02 +02:00
2021-09-25 03:49:49 +02:00
for ( let i = 0 ; i < server . textFiles . length ; i ++ ) {
2021-09-05 01:09:30 +02:00
if ( filter ) {
if ( server . textFiles [ i ] . fn . includes ( filter ) ) {
allFiles . push ( server . textFiles [ i ] . fn ) ;
}
} else {
allFiles . push ( server . textFiles [ i ] . fn ) ;
}
}
2019-06-03 05:28:02 +02:00
2021-09-25 03:49:49 +02:00
for ( let i = 0 ; i < server . contracts . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( filter ) {
if ( server . contracts [ i ] . fn . includes ( filter ) ) {
allFiles . push ( server . contracts [ i ] . fn ) ;
}
} else {
allFiles . push ( server . contracts [ i ] . fn ) ;
}
}
2019-06-03 05:28:02 +02:00
2021-09-05 01:09:30 +02:00
// Sort the files alphabetically then print each
allFiles . sort ( ) ;
return allFiles ;
} ,
2021-10-19 16:13:19 +02:00
ps : function ( hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "ps" , getRamCost ( Player , "ps" ) ) ;
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "ps" ) ;
2021-09-05 01:09:30 +02:00
const processes = [ ] ;
2022-01-16 01:45:03 +01:00
for ( const script of server . runningScripts ) {
2021-09-05 01:09:30 +02:00
processes . push ( {
filename : script.filename ,
threads : script.threads ,
args : script.args.slice ( ) ,
pid : script.pid ,
} ) ;
}
return processes ;
} ,
2021-10-19 16:13:19 +02:00
hasRootAccess : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "hasRootAccess" , getRamCost ( Player , "hasRootAccess" ) ) ;
2021-10-19 16:13:19 +02:00
if ( hostname === undefined ) {
2021-09-05 01:09:30 +02:00
throw makeRuntimeErrorMsg ( "hasRootAccess" , "Takes 1 argument" ) ;
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "hasRootAccess" ) ;
2021-09-05 01:09:30 +02:00
return server . hasAdminRights ;
} ,
2021-09-25 03:49:49 +02:00
getHostname : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getHostname" , getRamCost ( Player , "getHostname" ) ) ;
2021-10-07 23:55:49 +02:00
const scriptServer = GetServer ( workerScript . hostname ) ;
2021-09-05 01:09:30 +02:00
if ( scriptServer == null ) {
2021-09-25 03:49:49 +02:00
throw makeRuntimeErrorMsg ( "getHostname" , "Could not find server. This is a bug. Report to dev." ) ;
2021-09-05 01:09:30 +02:00
}
return scriptServer . hostname ;
} ,
2021-09-25 03:49:49 +02:00
getHackingLevel : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getHackingLevel" , getRamCost ( Player , "getHackingLevel" ) ) ;
2021-09-05 01:09:30 +02:00
Player . updateSkillLevels ( ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "getHackingLevel" , ( ) = > ` returned ${ Player . hacking } ` ) ;
2021-11-05 22:12:52 +01:00
return Player . hacking ;
2021-09-05 01:09:30 +02:00
} ,
2021-09-25 03:49:49 +02:00
getHackingMultipliers : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getHackingMultipliers" , getRamCost ( Player , "getHackingMultipliers" ) ) ;
2021-09-05 01:09:30 +02:00
return {
chance : Player.hacking_chance_mult ,
speed : Player.hacking_speed_mult ,
money : Player.hacking_money_mult ,
growth : Player.hacking_grow_mult ,
} ;
} ,
2021-09-25 03:49:49 +02:00
getHacknetMultipliers : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getHacknetMultipliers" , getRamCost ( Player , "getHacknetMultipliers" ) ) ;
2021-09-05 01:09:30 +02:00
return {
production : Player.hacknet_node_money_mult ,
purchaseCost : Player.hacknet_node_purchase_cost_mult ,
ramCost : Player.hacknet_node_ram_cost_mult ,
coreCost : Player.hacknet_node_core_cost_mult ,
levelCost : Player.hacknet_node_level_cost_mult ,
} ;
} ,
2021-09-25 03:49:49 +02:00
getBitNodeMultipliers : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getBitNodeMultipliers" , getRamCost ( Player , "getBitNodeMultipliers" ) ) ;
2021-09-05 01:09:30 +02:00
if ( SourceFileFlags [ 5 ] <= 0 && Player . bitNodeN !== 5 ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "getBitNodeMultipliers" , "Requires Source-File 5 to run." ) ;
2021-09-05 01:09:30 +02:00
}
2021-09-25 07:26:03 +02:00
const copy = Object . assign ( { } , BitNodeMultipliers ) ;
2021-09-05 01:09:30 +02:00
return copy ;
} ,
2021-10-19 16:13:19 +02:00
getServer : function ( hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServer" , getRamCost ( Player , "getServer" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServer" ) ;
2021-12-12 19:47:05 +01:00
const copy = Object . assign ( { } , server ) as any ;
2021-09-05 01:09:30 +02:00
// These fields should be hidden.
2021-12-12 19:47:05 +01:00
copy . contracts = undefined ;
copy . messages = undefined ;
copy . runningScripts = undefined ;
copy . scripts = undefined ;
copy . textFiles = undefined ;
copy . programs = undefined ;
copy . serversOnNetwork = undefined ;
if ( ! copy . baseDifficulty ) copy . baseDifficulty = 0 ;
if ( ! copy . hackDifficulty ) copy . hackDifficulty = 0 ;
if ( ! copy . minDifficulty ) copy . minDifficulty = 0 ;
if ( ! copy . moneyAvailable ) copy . moneyAvailable = 0 ;
if ( ! copy . moneyMax ) copy . moneyMax = 0 ;
if ( ! copy . numOpenPortsRequired ) copy . numOpenPortsRequired = 0 ;
if ( ! copy . openPortCount ) copy . openPortCount = 0 ;
if ( ! copy . requiredHackingSkill ) copy . requiredHackingSkill = 0 ;
if ( ! copy . serverGrowth ) copy . serverGrowth = 0 ;
2021-09-05 01:09:30 +02:00
return copy ;
} ,
2021-10-19 16:13:19 +02:00
getServerMoneyAvailable : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerMoneyAvailable" , getRamCost ( Player , "getServerMoneyAvailable" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerMoneyAvailable" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerMoneyAvailable" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 0 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerMoneyAvailable" ) ) {
return 0 ;
}
if ( server . hostname == "home" ) {
// Return player's money
workerScript . log (
"getServerMoneyAvailable" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned player's money: ${ numeralWrapper . formatMoney ( Player . money ) } ` ,
2021-09-05 01:09:30 +02:00
) ;
2021-11-12 02:09:38 +01:00
return Player . money ;
2021-09-05 01:09:30 +02:00
}
workerScript . log (
"getServerMoneyAvailable" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned ${ numeralWrapper . formatMoney ( server . moneyAvailable ) } for ' ${ server . hostname } ' ` ,
2021-09-05 01:09:30 +02:00
) ;
return server . moneyAvailable ;
} ,
2021-10-19 16:13:19 +02:00
getServerSecurityLevel : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerSecurityLevel" , getRamCost ( Player , "getServerSecurityLevel" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerSecurityLevel" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerSecurityLevel" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 1 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerSecurityLevel" ) ) {
return 1 ;
}
workerScript . log (
"getServerSecurityLevel" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned ${ numeralWrapper . formatServerSecurity ( server . hackDifficulty ) } for ' ${ server . hostname } ' ` ,
2021-09-05 01:09:30 +02:00
) ;
return server . hackDifficulty ;
} ,
2021-10-19 16:13:19 +02:00
getServerBaseSecurityLevel : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerBaseSecurityLevel" , getRamCost ( Player , "getServerBaseSecurityLevel" ) ) ;
2021-11-03 03:11:22 +01:00
workerScript . log (
"getServerBaseSecurityLevel" ,
2021-11-27 00:30:58 +01:00
( ) = > ` getServerBaseSecurityLevel is deprecated because it's not useful. ` ,
2021-11-03 03:11:22 +01:00
) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerBaseSecurityLevel" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerBaseSecurityLevel" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 1 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerBaseSecurityLevel" ) ) {
return 1 ;
}
workerScript . log (
"getServerBaseSecurityLevel" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned ${ numeralWrapper . formatServerSecurity ( server . baseDifficulty ) } for ' ${ server . hostname } ' ` ,
2021-09-05 01:09:30 +02:00
) ;
return server . baseDifficulty ;
} ,
2021-10-19 16:13:19 +02:00
getServerMinSecurityLevel : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerMinSecurityLevel" , getRamCost ( Player , "getServerMinSecurityLevel" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerMinSecurityLevel" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerMinSecurityLevel" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 1 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerMinSecurityLevel" ) ) {
return 1 ;
}
workerScript . log (
"getServerMinSecurityLevel" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned ${ numeralWrapper . formatServerSecurity ( server . minDifficulty ) } for ${ server . hostname } ` ,
2021-09-05 01:09:30 +02:00
) ;
return server . minDifficulty ;
} ,
2021-10-19 16:13:19 +02:00
getServerRequiredHackingLevel : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerRequiredHackingLevel" , getRamCost ( Player , "getServerRequiredHackingLevel" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerRequiredHackingLevel" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerRequiredHackingLevel" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 1 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerRequiredHackingLevel" ) ) {
return 1 ;
}
workerScript . log (
"getServerRequiredHackingLevel" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned ${ numeralWrapper . formatSkill ( server . requiredHackingSkill ) } for ' ${ server . hostname } ' ` ,
2021-09-05 01:09:30 +02:00
) ;
return server . requiredHackingSkill ;
} ,
2021-10-19 16:13:19 +02:00
getServerMaxMoney : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerMaxMoney" , getRamCost ( Player , "getServerMaxMoney" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerMaxMoney" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerMaxMoney" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 0 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerMaxMoney" ) ) {
return 0 ;
}
workerScript . log (
"getServerMaxMoney" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned ${ numeralWrapper . formatMoney ( server . moneyMax ) } for ' ${ server . hostname } ' ` ,
2021-09-05 01:09:30 +02:00
) ;
return server . moneyMax ;
} ,
2021-10-19 16:13:19 +02:00
getServerGrowth : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerGrowth" , getRamCost ( Player , "getServerGrowth" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerGrowth" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerGrowth" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 1 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerGrowth" ) ) {
return 1 ;
}
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerGrowth" , ( ) = > ` returned ${ server . serverGrowth } for ' ${ server . hostname } ' ` ) ;
2021-09-05 01:09:30 +02:00
return server . serverGrowth ;
} ,
2021-10-19 16:13:19 +02:00
getServerNumPortsRequired : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerNumPortsRequired" , getRamCost ( Player , "getServerNumPortsRequired" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerNumPortsRequired" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerNumPortsRequired" , ( ) = > "Cannot be executed on this server." ) ;
2021-09-25 03:49:49 +02:00
return 5 ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getServerNumPortsRequired" ) ) {
return 5 ;
}
2021-11-27 00:30:58 +01:00
workerScript . log (
"getServerNumPortsRequired" ,
( ) = > ` returned ${ server . numOpenPortsRequired } for ' ${ server . hostname } ' ` ,
) ;
2021-09-05 01:09:30 +02:00
return server . numOpenPortsRequired ;
} ,
2021-10-19 16:13:19 +02:00
getServerRam : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerRam" , getRamCost ( Player , "getServerRam" ) ) ;
2021-11-27 00:30:58 +01:00
workerScript . log (
"getServerRam" ,
( ) = > ` getServerRam is deprecated in favor of getServerMaxRam / getServerUsedRam ` ,
) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerRam" ) ;
2021-09-05 01:09:30 +02:00
workerScript . log (
"getServerRam" ,
2021-11-27 00:30:58 +01:00
( ) = > ` returned [ ${ numeralWrapper . formatRAM ( server . maxRam ) } , ${ numeralWrapper . formatRAM ( server . ramUsed ) } ] ` ,
2021-09-05 01:09:30 +02:00
) ;
return [ server . maxRam , server . ramUsed ] ;
} ,
2021-10-19 16:13:19 +02:00
getServerMaxRam : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerMaxRam" , getRamCost ( Player , "getServerMaxRam" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerMaxRam" ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerMaxRam" , ( ) = > ` returned ${ numeralWrapper . formatRAM ( server . maxRam ) } ` ) ;
2021-09-05 01:09:30 +02:00
return server . maxRam ;
} ,
2021-10-19 16:13:19 +02:00
getServerUsedRam : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getServerUsedRam" , getRamCost ( Player , "getServerUsedRam" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getServerUsedRam" ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "getServerUsedRam" , ( ) = > ` returned ${ numeralWrapper . formatRAM ( server . ramUsed ) } ` ) ;
2021-09-05 01:09:30 +02:00
return server . ramUsed ;
} ,
2021-10-19 16:13:19 +02:00
serverExists : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "serverExists" , getRamCost ( Player , "serverExists" ) ) ;
2021-10-19 16:13:19 +02:00
return GetServer ( hostname ) !== null ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
fileExists : function ( filename : any , hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "fileExists" , getRamCost ( Player , "fileExists" ) ) ;
2021-09-05 01:09:30 +02:00
if ( filename === undefined ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "fileExists" , "Usage: fileExists(scriptname, [server])" ) ;
2021-09-05 01:09:30 +02:00
}
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "fileExists" ) ;
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < server . scripts . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( filename == server . scripts [ i ] . filename ) {
return true ;
}
}
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < server . programs . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( filename . toLowerCase ( ) == server . programs [ i ] . toLowerCase ( ) ) {
return true ;
}
}
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < server . messages . length ; ++ i ) {
2022-01-08 20:11:42 +01:00
if ( filename . toLowerCase ( ) === server . messages [ i ] . toLowerCase ( ) ) {
2021-09-05 01:09:30 +02:00
return true ;
}
}
2021-09-25 07:26:03 +02:00
const txtFile = getTextFile ( filename , server ) ;
2021-09-05 01:09:30 +02:00
if ( txtFile != null ) {
return true ;
}
return false ;
} ,
2021-10-19 16:13:19 +02:00
isRunning : function ( fn : any , hostname : any = workerScript . hostname , . . . scriptArgs : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "isRunning" , getRamCost ( Player , "isRunning" ) ) ;
2021-10-19 16:13:19 +02:00
if ( fn === undefined || hostname === undefined ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "isRunning" , "Usage: isRunning(scriptname, server, [arg1], [arg2]...)" ) ;
2021-09-05 01:09:30 +02:00
}
if ( typeof fn === "number" ) {
return getRunningScriptByPid ( fn , "isRunning" ) != null ;
} else {
2021-10-19 16:13:19 +02:00
return getRunningScript ( fn , hostname , "isRunning" , scriptArgs ) != null ;
2021-09-05 01:09:30 +02:00
}
} ,
2021-09-25 03:49:49 +02:00
getPurchasedServerLimit : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getPurchasedServerLimit" , getRamCost ( Player , "getPurchasedServerLimit" ) ) ;
2021-09-05 01:09:30 +02:00
return getPurchaseServerLimit ( ) ;
} ,
2021-09-25 03:49:49 +02:00
getPurchasedServerMaxRam : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getPurchasedServerMaxRam" , getRamCost ( Player , "getPurchasedServerMaxRam" ) ) ;
2021-09-05 01:09:30 +02:00
return getPurchaseServerMaxRam ( ) ;
} ,
2021-09-25 03:49:49 +02:00
getPurchasedServerCost : function ( ram : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getPurchasedServerCost" , getRamCost ( Player , "getPurchasedServerCost" ) ) ;
2021-09-05 01:09:30 +02:00
const cost = getPurchaseServerCost ( ram ) ;
if ( cost === Infinity ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getPurchasedServerCost" , ( ) = > ` Invalid argument: ram=' ${ ram } ' ` ) ;
2021-09-05 01:09:30 +02:00
return Infinity ;
}
2018-11-01 19:18:32 +01:00
2021-09-05 01:09:30 +02:00
return cost ;
} ,
2021-12-20 21:48:26 +01:00
purchaseServer : function ( aname : any , aram : any ) : any {
const name = helper . string ( "purchaseServer" , "name" , aname ) ;
const ram = helper . number ( "purchaseServer" , "ram" , aram ) ;
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "purchaseServer" , getRamCost ( Player , "purchaseServer" ) ) ;
2021-10-19 16:13:19 +02:00
let hostnameStr = String ( name ) ;
2021-09-05 01:09:30 +02:00
hostnameStr = hostnameStr . replace ( /\s+/g , "" ) ;
if ( hostnameStr == "" ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "purchaseServer" , ( ) = > ` Invalid argument: hostname=' ${ hostnameStr } ' ` ) ;
2021-09-05 01:09:30 +02:00
return "" ;
}
2018-11-01 19:18:32 +01:00
2021-09-05 01:09:30 +02:00
if ( Player . purchasedServers . length >= getPurchaseServerLimit ( ) ) {
workerScript . log (
"purchaseServer" ,
2021-11-27 00:30:58 +01:00
( ) = >
` You have reached the maximum limit of ${ getPurchaseServerLimit ( ) } servers. You cannot purchase any more. ` ,
2021-09-05 01:09:30 +02:00
) ;
return "" ;
}
2018-11-01 19:18:32 +01:00
2021-09-05 01:09:30 +02:00
const cost = getPurchaseServerCost ( ram ) ;
if ( cost === Infinity ) {
2022-02-13 10:08:55 +01:00
if ( ram > getPurchaseServerMaxRam ( ) ) {
2022-01-19 19:09:33 +01:00
workerScript . log ( "purchaseServer" , ( ) = > ` Invalid argument: ram=' ${ ram } ' must not be greater than getPurchaseServerMaxRam ` ) ;
2022-02-13 10:08:55 +01:00
} else {
2022-01-19 19:09:33 +01:00
workerScript . log ( "purchaseServer" , ( ) = > ` Invalid argument: ram=' ${ ram } ' must be a positive power of 2 ` ) ;
}
2022-02-13 10:08:55 +01:00
2021-09-05 01:09:30 +02:00
return "" ;
}
2018-11-01 19:18:32 +01:00
2021-11-12 02:09:38 +01:00
if ( Player . money < cost ) {
2021-09-05 01:09:30 +02:00
workerScript . log (
"purchaseServer" ,
2021-11-27 00:30:58 +01:00
( ) = > ` Not enough money to purchase server. Need ${ numeralWrapper . formatMoney ( cost ) } ` ,
2021-09-05 01:09:30 +02:00
) ;
return "" ;
}
2021-09-25 03:49:49 +02:00
const newServ = safetlyCreateUniqueServer ( {
2021-09-05 01:09:30 +02:00
ip : createUniqueRandomIp ( ) ,
hostname : hostnameStr ,
organizationName : "" ,
isConnectedTo : false ,
adminRights : true ,
purchasedByPlayer : true ,
maxRam : ram ,
} ) ;
AddToAllServers ( newServ ) ;
2021-10-07 23:55:49 +02:00
Player . purchasedServers . push ( newServ . hostname ) ;
2021-09-25 03:49:49 +02:00
const homeComputer = Player . getHomeComputer ( ) ;
2021-10-07 23:55:49 +02:00
homeComputer . serversOnNetwork . push ( newServ . hostname ) ;
newServ . serversOnNetwork . push ( homeComputer . hostname ) ;
2021-10-27 20:18:33 +02:00
Player . loseMoney ( cost , "servers" ) ;
2021-09-05 01:09:30 +02:00
workerScript . log (
"purchaseServer" ,
2021-11-27 00:30:58 +01:00
( ) = > ` Purchased new server with hostname ' ${ newServ . hostname } ' for ${ numeralWrapper . formatMoney ( cost ) } ` ,
2021-09-05 01:09:30 +02:00
) ;
return newServ . hostname ;
} ,
2021-10-19 16:13:19 +02:00
deleteServer : function ( name : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "deleteServer" , getRamCost ( Player , "deleteServer" ) ) ;
2021-10-19 16:13:19 +02:00
let hostnameStr = String ( name ) ;
2021-09-05 01:09:30 +02:00
hostnameStr = hostnameStr . replace ( /\s\s+/g , "" ) ;
2021-10-07 22:56:01 +02:00
const server = GetServer ( hostnameStr ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "deleteServer" , ( ) = > ` Invalid argument: hostname=' ${ hostnameStr } ' ` ) ;
2021-09-25 03:49:49 +02:00
return false ;
}
2018-07-20 04:21:11 +02:00
2021-09-05 01:09:30 +02:00
if ( ! server . purchasedByPlayer || server . hostname === "home" ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "deleteServer" , ( ) = > "Cannot delete non-purchased server." ) ;
2021-09-05 01:09:30 +02:00
return false ;
}
2018-07-20 04:21:11 +02:00
2021-10-19 16:13:19 +02:00
const hostname = server . hostname ;
2018-06-20 18:43:05 +02:00
2021-09-05 01:09:30 +02:00
// Can't delete server you're currently connected to
if ( server . isConnectedTo ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "deleteServer" , ( ) = > "You are currently connected to the server you are trying to delete." ) ;
2021-09-05 01:09:30 +02:00
return false ;
}
2018-06-20 18:43:05 +02:00
2021-09-05 01:09:30 +02:00
// A server cannot delete itself
2021-10-19 16:13:19 +02:00
if ( hostname === workerScript . hostname ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "deleteServer" , ( ) = > "Cannot delete the server this script is running on." ) ;
2021-09-05 01:09:30 +02:00
return false ;
}
2017-07-25 03:06:40 +02:00
2021-09-05 01:09:30 +02:00
// Delete all scripts running on server
if ( server . runningScripts . length > 0 ) {
2021-11-27 00:30:58 +01:00
workerScript . log (
"deleteServer" ,
( ) = > ` Cannot delete server ' ${ hostname } ' because it still has scripts running. ` ,
) ;
2021-09-05 01:09:30 +02:00
return false ;
}
2017-07-27 04:56:14 +02:00
2021-09-05 01:09:30 +02:00
// Delete from player's purchasedServers array
2021-09-25 07:26:03 +02:00
let found = false ;
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < Player . purchasedServers . length ; ++ i ) {
2021-10-19 16:13:19 +02:00
if ( hostname == Player . purchasedServers [ i ] ) {
2021-09-05 01:09:30 +02:00
found = true ;
Player . purchasedServers . splice ( i , 1 ) ;
break ;
}
}
2017-07-25 03:06:40 +02:00
2021-09-05 01:09:30 +02:00
if ( ! found ) {
workerScript . log (
"deleteServer" ,
2021-11-27 00:30:58 +01:00
( ) = > ` Could not identify server ${ hostname } as a purchased server. This is a bug. Report to dev. ` ,
2021-09-05 01:09:30 +02:00
) ;
return false ;
}
2017-07-27 04:56:14 +02:00
2021-09-05 01:09:30 +02:00
// Delete from all servers
2021-10-19 16:13:19 +02:00
DeleteServer ( hostname ) ;
2021-09-05 01:09:30 +02:00
// Delete from home computer
found = false ;
2021-09-25 07:26:03 +02:00
const homeComputer = Player . getHomeComputer ( ) ;
2021-09-25 08:36:49 +02:00
for ( let i = 0 ; i < homeComputer . serversOnNetwork . length ; ++ i ) {
2021-10-19 16:13:19 +02:00
if ( hostname == homeComputer . serversOnNetwork [ i ] ) {
2021-09-05 01:09:30 +02:00
homeComputer . serversOnNetwork . splice ( i , 1 ) ;
2021-11-27 00:30:58 +01:00
workerScript . log ( "deleteServer" , ( ) = > ` Deleted server ' ${ hostnameStr } ` ) ;
2021-09-05 01:09:30 +02:00
return true ;
}
}
// Wasn't found on home computer
workerScript . log (
"deleteServer" ,
2021-11-27 00:30:58 +01:00
( ) = > ` Could not find server ${ hostname } as a purchased server. This is a bug. Report to dev. ` ,
2021-09-05 01:09:30 +02:00
) ;
return false ;
} ,
2021-11-03 03:11:22 +01:00
getPurchasedServers : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getPurchasedServers" , getRamCost ( Player , "getPurchasedServers" ) ) ;
2021-09-25 07:26:03 +02:00
const res : string [ ] = [ ] ;
2021-10-19 16:13:19 +02:00
Player . purchasedServers . forEach ( function ( hostname ) {
2021-11-03 03:11:22 +01:00
res . push ( hostname ) ;
2021-09-05 01:09:30 +02:00
} ) ;
return res ;
} ,
2021-11-03 03:11:22 +01:00
writePort : function ( port : any , data : any = "" ) : any {
2022-02-13 10:08:55 +01:00
if ( typeof data !== "string" && typeof data !== "number" ) {
2021-11-12 21:42:57 +01:00
throw makeRuntimeErrorMsg (
"writePort" ,
` Trying to write invalid data to a port: only strings and numbers are valid. ` ,
) ;
}
2021-12-27 19:06:38 +01:00
const iport = helper . getValidPort ( "writePort" , port ) ;
2021-11-03 03:11:22 +01:00
return Promise . resolve ( iport . write ( data ) ) ;
} ,
2021-09-25 03:49:49 +02:00
write : function ( port : any , data : any = "" , mode : any = "a" ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "write" , getRamCost ( Player , "write" ) ) ;
2021-11-03 03:11:22 +01:00
if ( isString ( port ) ) {
2021-09-05 01:09:30 +02:00
// Write to script or text file
let fn = port ;
if ( ! isValidFilePath ( fn ) ) {
throw makeRuntimeErrorMsg ( "write" , ` Invalid filepath: ${ fn } ` ) ;
}
2017-08-03 06:21:15 +02:00
2021-09-05 01:09:30 +02:00
if ( fn . lastIndexOf ( "/" ) === 0 ) {
fn = removeLeadingSlash ( fn ) ;
}
2017-07-27 04:56:14 +02:00
2021-09-05 01:09:30 +02:00
// Coerce 'data' to be a string
try {
data = String ( data ) ;
2021-10-13 08:27:55 +02:00
} catch ( e : any ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "write" , ` Invalid data ( ${ e } ). Data being written must be convertible to a string ` ) ;
2021-09-05 01:09:30 +02:00
}
2018-05-23 02:09:04 +02:00
2021-09-05 01:09:30 +02:00
const server = workerScript . getServer ( ) ;
if ( server == null ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "write" , "Error getting Server. This is a bug. Report to dev." ) ;
2021-09-05 01:09:30 +02:00
}
if ( isScriptFilename ( fn ) ) {
// Write to script
2021-09-25 03:49:49 +02:00
let script = workerScript . getScriptOnServer ( fn , server ) ;
2021-09-05 01:09:30 +02:00
if ( script == null ) {
// Create a new script
2022-01-05 01:09:34 +01:00
script = new Script ( Player , fn , data , server . hostname , server . scripts ) ;
2021-09-05 01:09:30 +02:00
server . scripts . push ( script ) ;
2022-01-05 01:09:34 +01:00
return script . updateRamUsage ( Player , server . scripts ) ;
2021-09-05 01:09:30 +02:00
}
mode === "w" ? ( script . code = data ) : ( script . code += data ) ;
2022-01-05 01:09:34 +01:00
return script . updateRamUsage ( Player , server . scripts ) ;
2021-09-05 01:09:30 +02:00
} else {
// Write to text file
2021-09-25 07:26:03 +02:00
const txtFile = getTextFile ( fn , server ) ;
2021-09-05 01:09:30 +02:00
if ( txtFile == null ) {
2021-09-25 03:49:49 +02:00
createTextFile ( fn , data , server ) ;
2021-10-08 20:05:47 +02:00
return Promise . resolve ( ) ;
2021-09-05 01:09:30 +02:00
}
if ( mode === "w" ) {
txtFile . write ( data ) ;
} else {
txtFile . append ( data ) ;
}
}
2021-10-08 20:05:47 +02:00
return Promise . resolve ( ) ;
2021-09-05 01:09:30 +02:00
} else {
throw makeRuntimeErrorMsg ( "write" , ` Invalid argument: ${ port } ` ) ;
}
} ,
2021-11-12 21:42:57 +01:00
tryWritePort : function ( port : any , data : any = "" ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "tryWritePort" , getRamCost ( Player , "tryWritePort" ) ) ;
2022-02-13 10:08:55 +01:00
if ( typeof data !== "string" && typeof data !== "number" ) {
2022-02-07 15:14:48 +01:00
throw makeRuntimeErrorMsg (
"writePort" ,
` Trying to write invalid data to a port: only strings and numbers are valid. ` ,
) ;
}
2021-09-05 01:09:30 +02:00
if ( ! isNaN ( port ) ) {
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
throw makeRuntimeErrorMsg (
2021-11-12 21:42:57 +01:00
"tryWritePort" ,
2021-09-05 01:09:30 +02:00
` Invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid. ` ,
) ;
}
2021-09-25 03:49:49 +02:00
const iport = NetscriptPorts [ port - 1 ] ;
if ( iport == null || ! ( iport instanceof Object ) ) {
2021-11-12 21:42:57 +01:00
throw makeRuntimeErrorMsg ( "tryWritePort" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2021-09-05 01:09:30 +02:00
}
2021-11-04 01:19:52 +01:00
return Promise . resolve ( iport . tryWrite ( data ) ) ;
2021-09-05 01:09:30 +02:00
} else {
2021-11-12 21:42:57 +01:00
throw makeRuntimeErrorMsg ( "tryWritePort" , ` Invalid argument: ${ port } ` ) ;
2021-09-05 01:09:30 +02:00
}
} ,
2021-11-03 03:11:22 +01:00
readPort : function ( port : any ) : any {
// Read from port
2021-12-27 19:06:38 +01:00
const iport = helper . getValidPort ( "readPort" , port ) ;
2021-11-03 03:11:22 +01:00
const x = iport . read ( ) ;
return x ;
} ,
2021-09-25 03:49:49 +02:00
read : function ( port : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "read" , getRamCost ( Player , "read" ) ) ;
2021-11-03 03:11:22 +01:00
if ( isString ( port ) ) {
2021-09-05 01:09:30 +02:00
// Read from script or text file
2021-09-25 07:26:03 +02:00
const fn = port ;
2021-10-07 23:55:49 +02:00
const server = GetServer ( workerScript . hostname ) ;
2021-09-05 01:09:30 +02:00
if ( server == null ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "read" , "Error getting Server. This is a bug. Report to dev." ) ;
2021-09-05 01:09:30 +02:00
}
if ( isScriptFilename ( fn ) ) {
// Read from script
2021-09-25 07:26:03 +02:00
const script = workerScript . getScriptOnServer ( fn , server ) ;
2021-09-05 01:09:30 +02:00
if ( script == null ) {
return "" ;
}
return script . code ;
} else {
// Read from text file
2021-09-25 07:26:03 +02:00
const txtFile = getTextFile ( fn , server ) ;
2021-09-05 01:09:30 +02:00
if ( txtFile !== null ) {
return txtFile . text ;
} else {
return "" ;
}
}
} else {
throw makeRuntimeErrorMsg ( "read" , ` Invalid argument: ${ port } ` ) ;
}
} ,
2021-09-25 03:49:49 +02:00
peek : function ( port : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "peek" , getRamCost ( Player , "peek" ) ) ;
2021-12-27 19:06:38 +01:00
const iport = helper . getValidPort ( "peek" , port ) ;
2021-10-05 22:37:54 +02:00
const x = iport . peek ( ) ;
return x ;
2021-09-05 01:09:30 +02:00
} ,
2021-11-04 01:19:52 +01:00
clear : function ( file : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "clear" , getRamCost ( Player , "clear" ) ) ;
2021-11-04 01:19:52 +01:00
if ( isString ( file ) ) {
2021-09-05 01:09:30 +02:00
// Clear text file
2021-11-04 01:19:52 +01:00
const fn = file ;
2021-10-07 23:55:49 +02:00
const server = GetServer ( workerScript . hostname ) ;
2021-09-05 01:09:30 +02:00
if ( server == null ) {
2021-09-09 05:47:34 +02:00
throw makeRuntimeErrorMsg ( "clear" , "Error getting Server. This is a bug. Report to dev." ) ;
2021-09-05 01:09:30 +02:00
}
2021-09-25 07:26:03 +02:00
const txtFile = getTextFile ( fn , server ) ;
2021-09-05 01:09:30 +02:00
if ( txtFile != null ) {
txtFile . write ( "" ) ;
}
} else {
2021-11-04 01:19:52 +01:00
throw makeRuntimeErrorMsg ( "clear" , ` Invalid argument: ${ file } ` ) ;
2021-09-05 01:09:30 +02:00
}
return 0 ;
} ,
2021-11-04 01:19:52 +01:00
clearPort : function ( port : any ) : any {
// Clear port
2021-12-27 19:06:38 +01:00
const iport = helper . getValidPort ( "clearPort" , port ) ;
2021-11-04 01:19:52 +01:00
return iport . clear ( ) ;
} ,
2022-01-10 22:49:42 +01:00
getPortHandle : function ( port : any ) : IPort {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getPortHandle" , getRamCost ( Player , "getPortHandle" ) ) ;
2021-12-27 19:06:38 +01:00
const iport = helper . getValidPort ( "getPortHandle" , port ) ;
2021-09-25 03:49:49 +02:00
return iport ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
rm : function ( fn : any , hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "rm" , getRamCost ( Player , "rm" ) ) ;
2017-08-03 06:21:15 +02:00
2021-10-19 16:13:19 +02:00
if ( hostname == null || hostname === "" ) {
hostname = workerScript . hostname ;
2021-09-05 01:09:30 +02:00
}
2021-10-19 16:13:19 +02:00
const s = safeGetServer ( hostname , "rm" ) ;
2017-07-27 04:56:14 +02:00
2021-09-05 01:09:30 +02:00
const status = s . removeFile ( fn ) ;
if ( ! status . res ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "rm" , ( ) = > status . msg + "" ) ;
2021-09-05 01:09:30 +02:00
}
2017-07-27 04:56:14 +02:00
2021-09-05 01:09:30 +02:00
return status . res ;
} ,
2021-10-19 16:13:19 +02:00
scriptRunning : function ( scriptname : any , hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "scriptRunning" , getRamCost ( Player , "scriptRunning" ) ) ;
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "scriptRunning" ) ;
2021-09-25 07:26:03 +02:00
for ( let i = 0 ; i < server . runningScripts . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( server . runningScripts [ i ] . filename == scriptname ) {
return true ;
}
}
return false ;
} ,
2021-10-19 16:13:19 +02:00
scriptKill : function ( scriptname : any , hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "scriptKill" , getRamCost ( Player , "scriptKill" ) ) ;
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "scriptKill" ) ;
2021-09-25 07:26:03 +02:00
let suc = false ;
2021-10-23 21:22:58 +02:00
for ( let i = 0 ; i < server . runningScripts . length ; i ++ ) {
2021-09-05 01:09:30 +02:00
if ( server . runningScripts [ i ] . filename == scriptname ) {
2021-10-07 23:55:49 +02:00
killWorkerScript ( server . runningScripts [ i ] , server . hostname ) ;
2021-09-05 01:09:30 +02:00
suc = true ;
2021-10-23 21:22:58 +02:00
i -- ;
2021-09-05 01:09:30 +02:00
}
}
return suc ;
} ,
2021-09-25 03:49:49 +02:00
getScriptName : function ( ) : any {
2021-09-05 01:09:30 +02:00
return workerScript . name ;
} ,
2021-10-19 16:13:19 +02:00
getScriptRam : function ( scriptname : any , hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getScriptRam" , getRamCost ( Player , "getScriptRam" ) ) ;
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "getScriptRam" ) ;
2021-09-25 07:26:03 +02:00
for ( let i = 0 ; i < server . scripts . length ; ++ i ) {
2021-09-05 01:09:30 +02:00
if ( server . scripts [ i ] . filename == scriptname ) {
return server . scripts [ i ] . ramUsage ;
}
}
return 0 ;
} ,
2021-10-19 16:13:19 +02:00
getRunningScript : function ( fn : any , hostname : any , . . . args : any [ ] ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getRunningScript" , getRamCost ( Player , "getRunningScript" ) ) ;
2021-09-05 01:09:30 +02:00
let runningScript ;
2021-11-27 00:37:03 +01:00
if ( fn === undefined && hostname === undefined && args . length === 0 ) {
2021-09-05 01:09:30 +02:00
runningScript = workerScript . scriptRef ;
} else if ( typeof fn === "number" ) {
runningScript = getRunningScriptByPid ( fn , "getRunningScript" ) ;
} else {
2021-11-27 00:37:03 +01:00
runningScript = getRunningScript ( fn , hostname , "getRunningScript" , args ) ;
2021-09-05 01:09:30 +02:00
}
if ( runningScript === null ) return null ;
return {
args : runningScript.args.slice ( ) ,
filename : runningScript.filename ,
logs : runningScript.logs.slice ( ) ,
offlineExpGained : runningScript.offlineExpGained ,
offlineMoneyMade : runningScript.offlineMoneyMade ,
offlineRunningTime : runningScript.offlineRunningTime ,
onlineExpGained : runningScript.onlineExpGained ,
onlineMoneyMade : runningScript.onlineMoneyMade ,
onlineRunningTime : runningScript.onlineRunningTime ,
pid : runningScript.pid ,
ramUsage : runningScript.ramUsage ,
server : runningScript.server ,
threads : runningScript.threads ,
} ;
} ,
2021-10-19 16:13:19 +02:00
getHackTime : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getHackTime" , getRamCost ( Player , "getHackTime" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getHackTime" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getHackTime" , ( ) = > "invalid for this kind of server" ) ;
2021-09-25 03:49:49 +02:00
return Infinity ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getHackTime" ) ) {
return Infinity ;
}
2017-07-27 04:56:14 +02:00
2021-11-03 03:11:22 +01:00
return calculateHackingTime ( server , Player ) * 1000 ;
2021-09-05 01:09:30 +02:00
} ,
2021-10-19 16:13:19 +02:00
getGrowTime : function ( hostname : any ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getGrowTime" , getRamCost ( Player , "getGrowTime" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getGrowTime" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getGrowTime" , ( ) = > "invalid for this kind of server" ) ;
2021-09-25 03:49:49 +02:00
return Infinity ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getGrowTime" ) ) {
return Infinity ;
}
2018-11-14 07:05:51 +01:00
2021-11-03 03:11:22 +01:00
return calculateGrowTime ( server , Player ) * 1000 ;
2021-09-05 01:09:30 +02:00
} ,
2022-01-15 02:14:35 +01:00
getWeakenTime : function ( hostname : any = workerScript . hostname ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getWeakenTime" , getRamCost ( Player , "getWeakenTime" ) ) ;
2021-10-19 16:13:19 +02:00
const server = safeGetServer ( hostname , "getWeakenTime" ) ;
2021-09-25 03:49:49 +02:00
if ( ! ( server instanceof Server ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "getWeakenTime" , ( ) = > "invalid for this kind of server" ) ;
2021-09-25 03:49:49 +02:00
return Infinity ;
}
2021-09-05 01:09:30 +02:00
if ( failOnHacknetServer ( server , "getWeakenTime" ) ) {
return Infinity ;
}
2018-01-27 07:52:39 +01:00
2021-11-03 03:11:22 +01:00
return calculateWeakenTime ( server , Player ) * 1000 ;
2021-09-05 01:09:30 +02:00
} ,
2021-12-31 13:31:46 +01:00
getScriptIncome : function ( scriptname? : any , hostname? : any , . . . args : any [ ] ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getScriptIncome" , getRamCost ( Player , "getScriptIncome" ) ) ;
2021-09-05 01:09:30 +02:00
if ( arguments . length === 0 ) {
2021-09-25 07:26:03 +02:00
const res = [ ] ;
2021-09-05 01:09:30 +02:00
// First element is total income of all currently running scripts
let total = 0 ;
for ( const script of workerScripts . values ( ) ) {
2021-09-09 05:47:34 +02:00
total += script . scriptRef . onlineMoneyMade / script . scriptRef . onlineRunningTime ;
2021-09-05 01:09:30 +02:00
}
res . push ( total ) ;
// Second element is total income you've earned from scripts since you installed Augs
2021-09-09 05:47:34 +02:00
res . push ( Player . scriptProdSinceLastAug / ( Player . playtimeSinceLastAug / 1000 ) ) ;
2021-09-05 01:09:30 +02:00
return res ;
} else {
// Get income for a particular script
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "getScriptIncome" ) ;
2021-09-25 08:36:49 +02:00
const runningScriptObj = findRunningScript ( scriptname , args , server ) ;
2021-09-05 01:09:30 +02:00
if ( runningScriptObj == null ) {
workerScript . log (
"getScriptIncome" ,
2021-11-27 00:30:58 +01:00
( ) = > ` No such script ' ${ scriptname } ' on ' ${ server . hostname } ' with args: ${ arrayToString ( args ) } ` ,
2021-09-05 01:09:30 +02:00
) ;
return - 1 ;
}
2021-09-09 05:47:34 +02:00
return runningScriptObj . onlineMoneyMade / runningScriptObj . onlineRunningTime ;
2021-09-05 01:09:30 +02:00
}
} ,
2021-12-31 13:31:46 +01:00
getScriptExpGain : function ( scriptname? : any , hostname? : any , . . . args : any [ ] ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getScriptExpGain" , getRamCost ( Player , "getScriptExpGain" ) ) ;
2021-09-05 01:09:30 +02:00
if ( arguments . length === 0 ) {
2021-09-25 07:26:03 +02:00
let total = 0 ;
2021-09-05 01:09:30 +02:00
for ( const ws of workerScripts . values ( ) ) {
2021-09-09 05:47:34 +02:00
total += ws . scriptRef . onlineExpGained / ws . scriptRef . onlineRunningTime ;
2021-09-05 01:09:30 +02:00
}
return total ;
} else {
// Get income for a particular script
2021-10-23 16:09:43 +02:00
const server = safeGetServer ( hostname , "getScriptExpGain" ) ;
2021-09-25 08:36:49 +02:00
const runningScriptObj = findRunningScript ( scriptname , args , server ) ;
2021-09-05 01:09:30 +02:00
if ( runningScriptObj == null ) {
workerScript . log (
"getScriptExpGain" ,
2021-11-27 00:30:58 +01:00
( ) = > ` No such script ' ${ scriptname } ' on ' ${ server . hostname } ' with args: ${ arrayToString ( args ) } ` ,
2021-09-05 01:09:30 +02:00
) ;
return - 1 ;
}
2021-09-09 05:47:34 +02:00
return runningScriptObj . onlineExpGained / runningScriptObj . onlineRunningTime ;
2021-09-05 01:09:30 +02:00
}
} ,
2021-09-25 03:49:49 +02:00
nFormat : function ( n : any , format : any ) : any {
2021-09-05 01:09:30 +02:00
if ( isNaN ( n ) || isNaN ( parseFloat ( n ) ) || typeof format !== "string" ) {
return "" ;
}
2019-04-10 08:07:12 +02:00
2021-09-05 01:09:30 +02:00
return numeralWrapper . format ( parseFloat ( n ) , format ) ;
} ,
2021-09-25 03:49:49 +02:00
tFormat : function ( milliseconds : any , milliPrecision : any = false ) : any {
2021-09-05 01:09:30 +02:00
return convertTimeMsToTimeElapsedString ( milliseconds , milliPrecision ) ;
} ,
2021-09-25 03:49:49 +02:00
getTimeSinceLastAug : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getTimeSinceLastAug" , getRamCost ( Player , "getTimeSinceLastAug" ) ) ;
2021-09-05 01:09:30 +02:00
return Player . playtimeSinceLastAug ;
} ,
2021-10-13 02:02:37 +02:00
alert : function ( message : any ) : void {
2021-12-20 01:58:49 +01:00
message = argsToString ( [ message ] ) ;
dialogBoxCreate ( message ) ;
2021-10-13 02:02:37 +02:00
} ,
2021-12-20 19:29:04 +01:00
toast : function ( message : any , variant : any = "success" , duration : any = 2000 ) : void {
2021-10-13 23:25:58 +02:00
if ( ! [ "success" , "info" , "warning" , "error" ] . includes ( variant ) )
throw new Error ( ` variant must be one of "success", "info", "warning", or "error" ` ) ;
2021-12-16 18:40:24 +01:00
2021-12-20 01:58:49 +01:00
message = argsToString ( [ message ] ) ;
2021-12-20 19:29:04 +01:00
SnackbarEvents . emit ( message , variant , duration ) ;
2021-10-13 23:25:58 +02:00
} ,
2021-09-25 03:49:49 +02:00
prompt : function ( txt : any ) : any {
2021-09-05 01:09:30 +02:00
if ( ! isString ( txt ) ) {
txt = JSON . stringify ( txt ) ;
}
2019-05-14 10:35:37 +02:00
2021-09-05 01:09:30 +02:00
return new Promise ( function ( resolve ) {
2021-10-02 04:53:23 +02:00
PromptEvent . emit ( {
txt : txt ,
resolve : resolve ,
2021-09-05 01:09:30 +02:00
} ) ;
} ) ;
} ,
2021-10-19 16:13:19 +02:00
wget : async function ( url : any , target : any , hostname : any = workerScript . hostname ) : Promise < boolean > {
2021-09-05 01:09:30 +02:00
if ( ! isScriptFilename ( target ) && ! target . endsWith ( ".txt" ) ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "wget" , ( ) = > ` Invalid target file: ' ${ target } '. Must be a script or text file. ` ) ;
2021-09-05 01:09:30 +02:00
return Promise . resolve ( false ) ;
}
2021-10-19 16:13:19 +02:00
const s = safeGetServer ( hostname , "wget" ) ;
2021-09-05 01:09:30 +02:00
return new Promise ( function ( resolve ) {
$ . get (
url ,
function ( data ) {
let res ;
if ( isScriptFilename ( target ) ) {
2022-01-05 01:09:34 +01:00
res = s . writeToScriptFile ( Player , target , data ) ;
2017-09-15 16:06:59 +02:00
} else {
2021-09-05 01:09:30 +02:00
res = s . writeToTextFile ( target , data ) ;
}
if ( ! res . success ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "wget" , ( ) = > "Failed." ) ;
2021-09-05 01:09:30 +02:00
return resolve ( false ) ;
}
if ( res . overwritten ) {
2021-11-27 00:30:58 +01:00
workerScript . log (
"wget" ,
( ) = > ` Successfully retrieved content and overwrote ' ${ target } ' on ' ${ hostname } ' ` ,
) ;
2021-09-05 01:09:30 +02:00
return resolve ( true ) ;
}
2021-11-27 00:30:58 +01:00
workerScript . log ( "wget" , ( ) = > ` Successfully retrieved content to new file ' ${ target } ' on ' ${ hostname } ' ` ) ;
2021-09-05 01:09:30 +02:00
return resolve ( true ) ;
} ,
"text" ,
) . fail ( function ( e ) {
2021-11-27 00:30:58 +01:00
workerScript . log ( "wget" , ( ) = > JSON . stringify ( e ) ) ;
2021-09-05 01:09:30 +02:00
return resolve ( false ) ;
} ) ;
} ) ;
} ,
2021-09-25 03:49:49 +02:00
getFavorToDonate : function ( ) : any {
2022-01-05 01:09:34 +01:00
updateDynamicRam ( "getFavorToDonate" , getRamCost ( Player , "getFavorToDonate" ) ) ;
2021-09-09 05:47:34 +02:00
return Math . floor ( CONSTANTS . BaseFavorToDonate * BitNodeMultipliers . RepToDonateToFaction ) ;
2021-09-05 01:09:30 +02:00
} ,
2021-12-02 18:32:10 +01:00
getOwnedSourceFiles : function ( ) : SourceFileLvl [ ] {
2022-01-05 01:09:34 +01:00
helper . updateDynamicRam ( "getOwnedSourceFiles" , getRamCost ( Player , "getOwnedSourceFiles" ) ) ;
2021-12-02 18:32:10 +01:00
const res : SourceFileLvl [ ] = [ ] ;
2021-12-02 18:17:33 +01:00
for ( let i = 0 ; i < Player . sourceFiles . length ; ++ i ) {
res . push ( {
n : Player.sourceFiles [ i ] . n ,
lvl : Player.sourceFiles [ i ] . lvl ,
2021-12-02 18:32:10 +01:00
} as SourceFileLvl ) ;
2021-12-02 18:17:33 +01:00
}
return res ;
} ,
2021-11-12 21:42:57 +01:00
getPlayer : function ( ) : INetscriptPlayer {
2022-01-05 01:09:34 +01:00
helper . updateDynamicRam ( "getPlayer" , getRamCost ( Player , "getPlayer" ) ) ;
2021-09-05 01:09:30 +02:00
const data = {
2021-11-05 22:12:52 +01:00
hacking : Player.hacking ,
2021-09-05 01:09:30 +02:00
hp : Player.hp ,
max_hp : Player.max_hp ,
strength : Player.strength ,
defense : Player.defense ,
dexterity : Player.dexterity ,
agility : Player.agility ,
charisma : Player.charisma ,
intelligence : Player.intelligence ,
hacking_chance_mult : Player.hacking_chance_mult ,
hacking_speed_mult : Player.hacking_speed_mult ,
hacking_money_mult : Player.hacking_money_mult ,
hacking_grow_mult : Player.hacking_grow_mult ,
hacking_exp : Player.hacking_exp ,
strength_exp : Player.strength_exp ,
defense_exp : Player.defense_exp ,
dexterity_exp : Player.dexterity_exp ,
agility_exp : Player.agility_exp ,
charisma_exp : Player.charisma_exp ,
hacking_mult : Player.hacking_mult ,
strength_mult : Player.strength_mult ,
defense_mult : Player.defense_mult ,
dexterity_mult : Player.dexterity_mult ,
agility_mult : Player.agility_mult ,
charisma_mult : Player.charisma_mult ,
hacking_exp_mult : Player.hacking_exp_mult ,
strength_exp_mult : Player.strength_exp_mult ,
defense_exp_mult : Player.defense_exp_mult ,
dexterity_exp_mult : Player.dexterity_exp_mult ,
agility_exp_mult : Player.agility_exp_mult ,
charisma_exp_mult : Player.charisma_exp_mult ,
company_rep_mult : Player.company_rep_mult ,
faction_rep_mult : Player.faction_rep_mult ,
numPeopleKilled : Player.numPeopleKilled ,
2021-11-12 02:09:38 +01:00
money : Player.money ,
2021-09-05 01:09:30 +02:00
city : Player.city ,
location : Player.location ,
2021-09-13 23:11:02 +02:00
companyName : Player.companyName ,
2021-09-05 01:09:30 +02:00
crime_money_mult : Player.crime_money_mult ,
crime_success_mult : Player.crime_success_mult ,
isWorking : Player.isWorking ,
workType : Player.workType ,
currentWorkFactionName : Player.currentWorkFactionName ,
currentWorkFactionDescription : Player.currentWorkFactionDescription ,
workHackExpGainRate : Player.workHackExpGainRate ,
workStrExpGainRate : Player.workStrExpGainRate ,
workDefExpGainRate : Player.workDefExpGainRate ,
workDexExpGainRate : Player.workDexExpGainRate ,
workAgiExpGainRate : Player.workAgiExpGainRate ,
workChaExpGainRate : Player.workChaExpGainRate ,
workRepGainRate : Player.workRepGainRate ,
workMoneyGainRate : Player.workMoneyGainRate ,
workMoneyLossRate : Player.workMoneyLossRate ,
workHackExpGained : Player.workHackExpGained ,
workStrExpGained : Player.workStrExpGained ,
workDefExpGained : Player.workDefExpGained ,
workDexExpGained : Player.workDexExpGained ,
workAgiExpGained : Player.workAgiExpGained ,
workChaExpGained : Player.workChaExpGained ,
workRepGained : Player.workRepGained ,
workMoneyGained : Player.workMoneyGained ,
createProgramName : Player.createProgramName ,
createProgramReqLvl : Player.createProgramReqLvl ,
className : Player.className ,
crimeType : Player.crimeType ,
work_money_mult : Player.work_money_mult ,
hacknet_node_money_mult : Player.hacknet_node_money_mult ,
hacknet_node_purchase_cost_mult : Player.hacknet_node_purchase_cost_mult ,
hacknet_node_ram_cost_mult : Player.hacknet_node_ram_cost_mult ,
hacknet_node_core_cost_mult : Player.hacknet_node_core_cost_mult ,
hacknet_node_level_cost_mult : Player.hacknet_node_level_cost_mult ,
hasWseAccount : Player.hasWseAccount ,
hasTixApiAccess : Player.hasTixApiAccess ,
has4SData : Player.has4SData ,
has4SDataTixApi : Player.has4SDataTixApi ,
bladeburner_max_stamina_mult : Player.bladeburner_max_stamina_mult ,
bladeburner_stamina_gain_mult : Player.bladeburner_stamina_gain_mult ,
bladeburner_analysis_mult : Player.bladeburner_analysis_mult ,
bladeburner_success_chance_mult : Player.bladeburner_success_chance_mult ,
bitNodeN : Player.bitNodeN ,
totalPlaytime : Player.totalPlaytime ,
playtimeSinceLastAug : Player.playtimeSinceLastAug ,
playtimeSinceLastBitnode : Player.playtimeSinceLastBitnode ,
jobs : { } ,
factions : Player.factions.slice ( ) ,
2021-10-07 23:55:49 +02:00
tor : Player.hasTorRouter ( ) ,
2022-01-11 21:08:14 +01:00
hasCorporation : Player.hasCorporation ( ) ,
2021-09-05 01:09:30 +02:00
} ;
Object . assign ( data . jobs , Player . jobs ) ;
return data ;
} ,
2021-10-15 02:13:26 +02:00
atExit : function ( f : any ) : void {
if ( typeof f !== "function" ) {
throw makeRuntimeErrorMsg ( "atExit" , "argument should be function" ) ;
2021-09-05 01:09:30 +02:00
}
2021-10-15 02:13:26 +02:00
workerScript . atExit = f ;
2021-09-05 01:09:30 +02:00
} ,
2022-01-12 18:53:54 +01:00
mv : function ( host : string , source : string , destination : string ) : void {
updateDynamicRam ( "mv" , getRamCost ( Player , "mv" ) ) ;
if ( arguments . length != 3 ) throw makeRuntimeErrorMsg ( "mv" , "Takes 3 argument." ) ;
if ( ! isValidFilePath ( source ) ) throw makeRuntimeErrorMsg ( "mv" , ` Invalid filename: ' ${ source } ' ` ) ;
if ( ! isValidFilePath ( destination ) ) throw makeRuntimeErrorMsg ( "mv" , ` Invalid filename: ' ${ destination } ' ` ) ;
const source_is_txt = source . endsWith ( ".txt" ) ;
const dest_is_txt = destination . endsWith ( ".txt" ) ;
2022-01-18 20:02:12 +01:00
if ( ! isScriptFilename ( source ) && ! source_is_txt )
throw makeRuntimeErrorMsg ( "mv" , ` 'mv' can only be used on scripts and text files (.txt) ` ) ;
if ( source_is_txt != dest_is_txt )
throw makeRuntimeErrorMsg ( "mv" , ` Source and destination files must have the same type ` ) ;
2022-01-12 18:53:54 +01:00
if ( source === destination ) {
return ;
}
// This will throw if the server is not found, we do not need to validate result.
const destServer : BaseServer | null = safeGetServer ( host , "mv" ) ;
2022-01-18 20:02:12 +01:00
if ( ! source_is_txt && destServer . isRunning ( source ) )
throw makeRuntimeErrorMsg ( "mv" , ` Cannot use 'mv' on a script that is running ` ) ;
2022-01-12 18:53:54 +01:00
interface File {
filename : string ;
}
const files = source_is_txt ? destServer.textFiles : destServer.scripts ;
let source_file : File | null = null ;
let dest_file : File | null = null ;
for ( let i = 0 ; i < files . length ; ++ i ) {
const file = files [ i ] ;
if ( file . filename === source ) {
source_file = file ;
} else if ( file . filename === destination ) {
dest_file = file ;
}
}
2022-01-18 20:02:12 +01:00
if ( source_file == null ) throw makeRuntimeErrorMsg ( "mv" , ` Source file ${ source } does not exist ` ) ;
2022-01-12 18:53:54 +01:00
if ( dest_file != null ) {
if ( dest_file instanceof TextFile && source_file instanceof TextFile ) {
dest_file . text = source_file . text ;
} else if ( dest_file instanceof Script && source_file instanceof Script ) {
dest_file . code = source_file . code ;
dest_file . markUpdated ( ) ;
}
destServer . removeFile ( source ) ;
} else {
source_file . filename = destination ;
if ( source_file instanceof Script ) {
source_file . markUpdated ( ) ;
}
}
} ,
2021-10-15 18:47:43 +02:00
flags : Flags ( workerScript . args ) ,
2021-09-05 01:09:30 +02:00
} ;
2021-11-04 01:19:52 +01:00
// add undocumented functions
const ns = {
. . . base ,
. . . extra ,
} ;
2021-12-29 08:04:24 +01:00
function getFunctionNames ( obj : any , prefix : string ) : string [ ] {
2021-09-25 03:49:49 +02:00
const functionNames : string [ ] = [ ] ;
2021-09-05 01:09:30 +02:00
for ( const [ key , value ] of Object . entries ( obj ) ) {
2021-12-29 08:04:24 +01:00
if ( key === "args" ) {
continue ;
} else if ( typeof value == "function" ) {
2021-10-11 19:31:12 +02:00
functionNames . push ( prefix + key ) ;
2021-09-05 01:09:30 +02:00
} else if ( typeof value == "object" ) {
2021-10-11 19:31:12 +02:00
functionNames . push ( . . . getFunctionNames ( value , key + "." ) ) ;
2021-09-05 01:09:30 +02:00
}
2021-05-12 21:10:36 +02:00
}
2021-09-05 01:09:30 +02:00
return functionNames ;
}
2021-05-12 21:10:36 +02:00
2021-11-04 01:19:52 +01:00
const possibleLogs = Object . fromEntries ( [ . . . getFunctionNames ( ns , "" ) ] . map ( ( a ) = > [ a , true ] ) ) ;
2017-08-30 19:44:29 +02:00
2021-11-04 01:19:52 +01:00
return ns ;
}