mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
UI: Do not close scripts in editor when their servers are deleted (#2049)
This commit is contained in:
@@ -2,4 +2,5 @@
|
||||
export enum RamCalculationErrorCode {
|
||||
SyntaxError = -1,
|
||||
ImportError = -2,
|
||||
InvalidServer = -3,
|
||||
}
|
||||
|
||||
@@ -45,6 +45,9 @@ export function ScriptEditorContextProvider({ children }: { children: React.Reac
|
||||
case RamCalculationErrorCode.ImportError:
|
||||
errorType = "Import Error";
|
||||
break;
|
||||
case RamCalculationErrorCode.InvalidServer:
|
||||
errorType = "Invalid server";
|
||||
break;
|
||||
default:
|
||||
errorType = "Unknown Error";
|
||||
break;
|
||||
|
||||
@@ -180,14 +180,6 @@ function Root(props: IProps): React.ReactElement {
|
||||
|
||||
let decorations: monaco.editor.IEditorDecorationsCollection | undefined;
|
||||
|
||||
// Prevent Crash if script is open on deleted server
|
||||
for (let i = openScripts.length - 1; i >= 0; i--) {
|
||||
GetServer(openScripts[i].hostname) === null && openScripts.splice(i, 1);
|
||||
}
|
||||
if (currentScript && GetServer(currentScript.hostname) === null) {
|
||||
currentScript = openScripts[0] ?? null;
|
||||
}
|
||||
|
||||
const save = useCallback(() => {
|
||||
if (currentScript === null) {
|
||||
console.error("currentScript is null when it shouldn't be. Unable to save script");
|
||||
@@ -304,10 +296,17 @@ function Root(props: IProps): React.ReactElement {
|
||||
|
||||
const debouncedCodeParsing = debounce((newCode: string) => {
|
||||
let server;
|
||||
if (!currentScript || !hasScriptExtension(currentScript.path) || !(server = GetServer(currentScript.hostname))) {
|
||||
if (!currentScript || !hasScriptExtension(currentScript.path)) {
|
||||
showRAMError();
|
||||
return;
|
||||
}
|
||||
if (!(server = GetServer(currentScript.hostname))) {
|
||||
showRAMError({
|
||||
errorCode: RamCalculationErrorCode.InvalidServer,
|
||||
errorMessage: `Server ${currentScript.hostname} does not exist`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
let ast;
|
||||
try {
|
||||
ast = parseAST(currentScript.path, currentScript.hostname, newCode, getFileType(currentScript.path));
|
||||
@@ -398,7 +397,8 @@ function Root(props: IProps): React.ReactElement {
|
||||
function saveScript(scriptToSave: OpenScript): void {
|
||||
const server = GetServer(scriptToSave.hostname);
|
||||
if (!server) {
|
||||
throw new Error("Server should not be null but it is.");
|
||||
dialogBoxCreate(`Server ${scriptToSave.hostname} does not exist.`);
|
||||
return;
|
||||
}
|
||||
// Show a warning message if the file is on a non-home server.
|
||||
if (scriptToSave.hostname !== SpecialServers.Home) {
|
||||
@@ -478,7 +478,7 @@ function Root(props: IProps): React.ReactElement {
|
||||
const indexOffset = openScripts.length === index ? -1 : 0;
|
||||
currentScript = openScripts[index + indexOffset];
|
||||
if (editorRef.current !== null) {
|
||||
if (currentScript.model.isDisposed() || !currentScript.model) {
|
||||
if (!currentScript.model || currentScript.model.isDisposed()) {
|
||||
currentScript.regenerateModel();
|
||||
}
|
||||
editorRef.current.setModel(currentScript.model);
|
||||
|
||||
@@ -7,7 +7,9 @@ import { throwIfReachable } from "../../utils/helpers/throwIfReachable";
|
||||
function getServerCode(scripts: OpenScript[], index: number): string | null {
|
||||
const openScript = scripts[index];
|
||||
const server = GetServer(openScript.hostname);
|
||||
if (server === null) throw new Error(`Server '${openScript.hostname}' should not be null, but it is.`);
|
||||
if (server === null) {
|
||||
return null;
|
||||
}
|
||||
const data = server.getContentFile(openScript.path)?.content ?? null;
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user