bitburner-src/src/ui/LoadingScreen.tsx
Martin Fournier 855a4e622d Add Electron preload script to allow communication
Adds a channel to communicate between the main process & the renderer
process, so that the game can easily ship data back to the main process.

It uses the Electron contextBridge & ipcRenderer/ipcMain.

Connects those events to various save functions. Adds triggered events
on game save, game load, and imported game. Adds way for the Electron
app to ask for certain actions or data.

Hook handlers to disable automatic restore

Allows to temporarily disable restore when the game just did an import
or deleted a save game. Prevents looping screens.
2022-01-26 03:56:19 -05:00

96 lines
2.7 KiB
TypeScript

import React, { useState, useEffect } from "react";
import CircularProgress from "@mui/material/CircularProgress";
import Typography from "@mui/material/Typography";
import Grid from "@mui/material/Grid";
import Box from "@mui/material/Box";
import { Theme } from "@mui/material/styles";
import makeStyles from "@mui/styles/makeStyles";
import createStyles from "@mui/styles/createStyles";
import { Terminal } from "../Terminal";
import { load } from "../db";
import { Player } from "../Player";
import { Engine } from "../engine";
import { GameRoot } from "./GameRoot";
import { CONSTANTS } from "../Constants";
import { ActivateRecoveryMode } from "./React/RecoveryRoot";
import { hash } from "../hash/hash";
import { pushGameReady } from "../Electron";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
backgroundColor: theme.colors.backgroundprimary,
},
}),
);
export function LoadingScreen(): React.ReactElement {
const classes = useStyles();
const [show, setShow] = useState(false);
const [loaded, setLoaded] = useState(false);
const version = `v${CONSTANTS.VersionString} (${hash()})`;
if (process.env.NODE_ENV === "development") {
document.title = `[dev] Bitburner ${version}`;
} else {
document.title = `Bitburner ${version}`;
}
useEffect(() => {
const id = setTimeout(() => {
if (!loaded) setShow(true);
}, 2000);
return () => clearTimeout(id);
});
useEffect(() => {
async function doLoad(): Promise<void> {
await load()
.then((saveString) => {
try {
Engine.load(saveString);
} catch (err: any) {
ActivateRecoveryMode();
setLoaded(true);
throw err;
}
pushGameReady();
setLoaded(true);
})
.catch((reason) => {
console.error(reason);
Engine.load("");
setLoaded(true);
});
}
doLoad();
}, []);
return (
<Box className={classes.root}>
{loaded ? (
<GameRoot terminal={Terminal} engine={Engine} player={Player} />
) : (
<Grid container direction="column" justifyContent="center" alignItems="center" style={{ minHeight: "100vh" }}>
<Grid item>
<CircularProgress size={150} color="primary" />
</Grid>
<Grid item>
<Typography variant="h3">Loading Bitburner {version}</Typography>
</Grid>
{show && (
<Grid item>
<Typography>
If the game fails to load, consider <a href="?noScripts">killing all scripts</a>
</Typography>
</Grid>
)}
</Grid>
)}
</Box>
);
}