import React, { useState } from "react"; import { Box, InputAdornment, TextField, Tooltip, Typography } from "@mui/material"; import { Settings } from "../../Settings/Settings"; import { parseCommand } from "../../Terminal/Parser"; import { resolveScriptFilePath } from "../../Paths/ScriptFilePath"; import { formatRam } from "../../ui/formatNumber"; import CheckCircleIcon from "@mui/icons-material/CheckCircle"; import WarningIcon from "@mui/icons-material/Warning"; import ErrorIcon from "@mui/icons-material/Error"; import { Player } from "@player"; interface IProps { tooltip: React.ReactElement; label: string; } export const AutoexecInput = (props: IProps): React.ReactElement => { const [autoexec, setAutoexec] = useState(Settings.AutoexecScript); function handleAutoexecChange(event: React.ChangeEvent): void { Settings.AutoexecScript = event.target.value; setAutoexec(event.target.value); } // None of these errors block the saving of the setting; what is invalid now // could become valid later. function createTooltip() { const args = parseCommand(autoexec); if (args.length === 0) { return ( No script will be auto-launched}> ); } const cmd = String(args[0]); const scriptPath = resolveScriptFilePath(cmd); if (!scriptPath) { return ( "{cmd}" is invalid for a script name (maybe missing suffix?)}> ); } const home = Player.getHomeComputer(); const script = home.scripts.get(scriptPath); if (!script) { return ( {cmd} does not exist!}> ); } const ramUsage = script.getRamUsage(home.scripts); if (ramUsage === null) { return ( {cmd} has errors!}> ); } // Stolen from Prestige.ts const minRam = Player.sourceFileLvl(9) >= 2 ? 128 : Player.sourceFileLvl(1) > 0 ? 32 : 8; if (ramUsage <= minRam) { return ( {cmd} costs {formatRam(ramUsage)} } > ); } else { return ( {cmd} costs {formatRam(ramUsage)}, you might only have {formatRam(minRam)} on home! } > ); } } return ( {props.tooltip}}> {props.label} {createTooltip()}, }} value={autoexec} onChange={handleAutoexecChange} /> ); };