2021-11-27 21:07:25 +01:00
|
|
|
import { Player } from "./Player";
|
2021-12-21 19:41:11 +01:00
|
|
|
import { isScriptFilename } from "./Script/isScriptFilename";
|
|
|
|
import { Script } from "./Script/Script";
|
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";
|
2022-01-03 16:32:01 +01:00
|
|
|
import { SnackbarEvents } from "./ui/React/Snackbar";
|
|
|
|
import { IMap } from "./types";
|
2022-01-06 13:04:03 +01:00
|
|
|
import { GetServer } from "./Server/AllServers";
|
2021-11-27 21:07:25 +01:00
|
|
|
|
|
|
|
export function initElectron(): void {
|
2022-01-03 16:33:58 +01:00
|
|
|
const userAgent = navigator.userAgent.toLowerCase();
|
2022-01-05 01:09:34 +01:00
|
|
|
if (userAgent.indexOf(" electron/") > -1) {
|
2022-01-03 16:33:58 +01:00
|
|
|
// Electron-specific code
|
2022-01-06 13:04:03 +01:00
|
|
|
(document as any).achievements = [];
|
2022-01-03 16:33:58 +01:00
|
|
|
initWebserver();
|
|
|
|
initAppNotifier();
|
|
|
|
}
|
2021-11-27 21:07:25 +01:00
|
|
|
}
|
2021-12-21 19:41:11 +01:00
|
|
|
|
|
|
|
function initWebserver(): void {
|
|
|
|
(document as any).saveFile = function (filename: string, code: 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);
|
|
|
|
}
|
2021-12-23 01:54:59 +01:00
|
|
|
code = Buffer.from(code, "base64").toString();
|
2021-12-21 19:41:11 +01:00
|
|
|
const home = GetServer("home");
|
|
|
|
if (home === null) return "'home' server not found.";
|
|
|
|
if (isScriptFilename(filename)) {
|
|
|
|
//If the current script already exists on the server, overwrite it
|
|
|
|
for (let i = 0; i < home.scripts.length; i++) {
|
|
|
|
if (filename == home.scripts[i].filename) {
|
2022-01-05 01:09:34 +01:00
|
|
|
home.scripts[i].saveScript(Player, filename, code, "home", home.scripts);
|
2021-12-21 21:39:04 +01:00
|
|
|
return "written";
|
2021-12-21 19:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//If the current script does NOT exist, create a new one
|
|
|
|
const script = new Script();
|
2022-01-05 01:09:34 +01:00
|
|
|
script.saveScript(Player, filename, code, "home", home.scripts);
|
2021-12-21 19:41:11 +01:00
|
|
|
home.scripts.push(script);
|
|
|
|
return "written";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "not a script file";
|
|
|
|
};
|
|
|
|
}
|
2022-01-03 16:32:01 +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,
|
2022-01-03 16:32:01 +01:00
|
|
|
};
|
|
|
|
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) =>
|
2022-01-03 16:32:01 +01:00
|
|
|
SnackbarEvents.emit(message, type, duration),
|
2022-01-05 01:09:34 +01:00
|
|
|
};
|
2022-01-03 16:32:01 +01:00
|
|
|
|
|
|
|
// Will be consumud by the electron wrapper.
|
|
|
|
// @ts-ignore
|
|
|
|
window.appNotifier = funcs;
|
|
|
|
}
|