mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-29 20:37:05 +02:00
8553bcb8fc
* Use Compression Streams API instead of jszip or other libraries. * Remove usage of base64 in the new binary format. * Do not convert binary data to string and back. The type of save data is SaveData, it's either string (old base64 format) or Uint8Array (new binary format). * Proper support for interacting with electron-related code. Electron-related code assumes that save data is in the base64 format. * Proper support for other tools (DevMenu, pretty-save.js). Full support for DevMenu will be added in a follow-up PR. Check the comments in src\DevMenu\ui\SaveFileDev.tsx for details.
94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
import React, { useState, useRef } from "react";
|
|
|
|
import Accordion from "@mui/material/Accordion";
|
|
import AccordionSummary from "@mui/material/AccordionSummary";
|
|
import AccordionDetails from "@mui/material/AccordionDetails";
|
|
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
|
|
|
|
import Typography from "@mui/material/Typography";
|
|
import { saveObject } from "../../SaveObject";
|
|
import { SnackbarEvents } from "../../ui/React/Snackbar";
|
|
import { ToastVariant } from "@enums";
|
|
import { Upload } from "@mui/icons-material";
|
|
import { Button } from "@mui/material";
|
|
import { OptionSwitch } from "../../ui/React/OptionSwitch";
|
|
import { isBinaryFormat } from "../../../electron/saveDataBinaryFormat";
|
|
|
|
export function SaveFileDev(): React.ReactElement {
|
|
const importInput = useRef<HTMLInputElement>(null);
|
|
const [saveFile, setSaveFile] = useState("");
|
|
const [restoreScripts, setRestoreScripts] = useState(true);
|
|
const [restoreAugs, setRestoreAugs] = useState(true);
|
|
const [restoreSFs, setRestoreSFs] = useState(true);
|
|
|
|
async function onImport(event: React.ChangeEvent<HTMLInputElement>): Promise<void> {
|
|
try {
|
|
const saveData = await saveObject.getSaveDataFromFile(event.target.files);
|
|
// TODO Support binary format. This is low priority because this entire feature (SaveFileDev) is not fully
|
|
// implemented. "doRestore" does nothing.
|
|
if (isBinaryFormat(saveData)) {
|
|
SnackbarEvents.emit("We currently do not support binary format", ToastVariant.ERROR, 5000);
|
|
return;
|
|
}
|
|
const save = decodeURIComponent(escape(atob(saveData as string)));
|
|
setSaveFile(save);
|
|
} catch (e: unknown) {
|
|
SnackbarEvents.emit(String(e), ToastVariant.ERROR, 5000);
|
|
}
|
|
}
|
|
|
|
function startImport(): void {
|
|
if (!window.File || !window.FileReader || !window.FileList || !window.Blob) return;
|
|
const ii = importInput.current;
|
|
if (ii === null) throw new Error("import input should not be null");
|
|
ii.click();
|
|
}
|
|
|
|
function doRestore(): void {
|
|
const save = JSON.parse(saveFile);
|
|
// TODO unplanned: "Continue here". Unknown what this todo is for.
|
|
console.error(save);
|
|
}
|
|
|
|
return (
|
|
<Accordion TransitionProps={{ unmountOnExit: true }}>
|
|
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
|
|
<Typography>Save file</Typography>
|
|
</AccordionSummary>
|
|
<AccordionDetails>
|
|
<Button onClick={startImport} startIcon={<Upload />} sx={{ gridArea: "import" }}>
|
|
Select save file
|
|
<input ref={importInput} type="file" hidden onChange={onImport} />
|
|
</Button>
|
|
<br />
|
|
{saveFile !== "" && (
|
|
<>
|
|
<OptionSwitch
|
|
checked={restoreScripts}
|
|
onChange={(v) => setRestoreScripts(v)}
|
|
text="Restore scripts"
|
|
tooltip={<>Restore the save file home computer scripts.</>}
|
|
/>
|
|
<br />
|
|
<OptionSwitch
|
|
checked={restoreAugs}
|
|
onChange={(v) => setRestoreAugs(v)}
|
|
text="Restore Augmentations"
|
|
tooltip={<>Restore the save file installed augmentations.</>}
|
|
/>
|
|
<br />
|
|
<OptionSwitch
|
|
checked={restoreSFs}
|
|
onChange={(v) => setRestoreSFs(v)}
|
|
text="Restore Source Files"
|
|
tooltip={<>Restore the save file acquired source files.</>}
|
|
/>
|
|
<br />
|
|
<Button onClick={doRestore}>Restore</Button>
|
|
</>
|
|
)}
|
|
</AccordionDetails>
|
|
</Accordion>
|
|
);
|
|
}
|