bitburner-src/src/Electron.tsx

109 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-11-27 21:07:25 +01:00
import { Player } from "./Player";
2021-12-23 01:54:59 +01:00
import { removeLeadingSlash } from "./Terminal/DirectoryHelpers";
2022-01-05 01:09:34 +01:00
import { Terminal } from "./Terminal";
import { SnackbarEvents } from "./ui/React/Snackbar";
import { IMap, IReturnStatus } from "./types";
import { GetServer } from "./Server/AllServers";
2021-11-27 21:07:25 +01:00
export function initElectron(): void {
const userAgent = navigator.userAgent.toLowerCase();
2022-01-05 01:09:34 +01:00
if (userAgent.indexOf(" electron/") > -1) {
// Electron-specific code
(document as any).achievements = [];
initWebserver();
initAppNotifier();
}
2021-11-27 21:07:25 +01:00
}
2021-12-21 19:41:11 +01:00
function initWebserver(): void {
interface IReturnWebStatus extends IReturnStatus {
data?: {
[propName: string]: any;
};
}
function normalizeFileName(filename: string): string {
2022-01-06 17:43:42 +01:00
filename = filename.replace(/\/\/+/g, "/");
filename = removeLeadingSlash(filename);
if (filename.includes("/")) {
2021-12-23 21:34:01 +01:00
filename = "/" + removeLeadingSlash(filename);
}
return filename;
}
(document as any).getFiles = function (): IReturnWebStatus {
const home = GetServer("home");
if (home === null) {
return {
res: false,
msg: "Home server does not exist."
}
}
return {
res: true,
data: {
files: home.scripts.map((script) => ({
filename: script.filename,
code: script.code
}))
}
}
};
(document as any).deleteFile = function (filename: string): IReturnWebStatus {
filename = normalizeFileName(filename);
2021-12-21 19:41:11 +01:00
const home = GetServer("home");
if (home === null) {
return {
res: false,
msg: "Home server does not exist."
2021-12-21 19:41:11 +01:00
}
}
return home.removeFile(filename);
};
(document as any).saveFile = function (filename: string, code: string): IReturnWebStatus {
filename = normalizeFileName(filename);
2021-12-21 19:41:11 +01:00
code = Buffer.from(code, "base64").toString();
const home = GetServer("home");
if (home === null) {
return {
res: false,
msg: "Home server does not exist."
}
2021-12-21 19:41:11 +01:00
}
const result = home.writeToScriptFile(Player, filename, code);
2021-12-21 19:41:11 +01:00
return {
res: result.success,
data: {
overwritten: result.overwritten
}
};
2021-12-21 19:41:11 +01:00
};
}
// Expose certain alert functions to allow the wrapper to sends message to the game
function initAppNotifier(): void {
const funcs = {
terminal: (message: string, type?: string) => {
const typesFn: IMap<(s: string) => void> = {
info: Terminal.info,
warn: Terminal.warn,
error: Terminal.error,
2022-01-05 01:09:34 +01:00
success: Terminal.success,
};
let fn;
if (type) fn = typesFn[type];
if (!fn) fn = Terminal.print;
fn.bind(Terminal)(message);
},
2022-01-05 01:09:34 +01:00
toast: (message: string, type: "info" | "success" | "warning" | "error", duration = 2000) =>
SnackbarEvents.emit(message, type, duration),
2022-01-05 01:09:34 +01:00
};
// Will be consumud by the electron wrapper.
// @ts-ignore
window.appNotifier = funcs;
}