2021-11-27 21:07:25 +01:00
|
|
|
import { Player } from "./Player";
|
2022-01-18 18:21:53 +01:00
|
|
|
import { Router } from "./ui/GameRoot";
|
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";
|
2022-01-20 09:03:07 +01:00
|
|
|
import { IMap, IReturnStatus } from "./types";
|
2022-01-06 13:04:03 +01:00
|
|
|
import { GetServer } from "./Server/AllServers";
|
2022-01-18 18:21:53 +01:00
|
|
|
import { ImportPlayerData, SaveData, saveObject } from "./SaveObject";
|
|
|
|
import { Settings } from "./Settings/Settings";
|
|
|
|
import { exportScripts } from "./Terminal/commands/download";
|
|
|
|
import { CONSTANTS } from "./Constants";
|
|
|
|
import { hash } from "./hash/hash";
|
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();
|
2022-01-18 18:21:53 +01:00
|
|
|
initSaveFunctions();
|
|
|
|
initElectronBridge();
|
2022-01-03 16:33:58 +01:00
|
|
|
}
|
2021-11-27 21:07:25 +01:00
|
|
|
}
|
2021-12-21 19:41:11 +01:00
|
|
|
|
|
|
|
function initWebserver(): void {
|
2022-01-20 09:03:07 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-01-20 09:03:07 +01:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
(document as any).getFiles = function (): IReturnWebStatus {
|
|
|
|
const home = GetServer("home");
|
|
|
|
if (home === null) {
|
|
|
|
return {
|
|
|
|
res: false,
|
2022-01-26 06:58:02 +01:00
|
|
|
msg: "Home server does not exist.",
|
|
|
|
};
|
2022-01-20 09:03:07 +01:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
res: true,
|
|
|
|
data: {
|
|
|
|
files: home.scripts.map((script) => ({
|
|
|
|
filename: script.filename,
|
2022-01-22 05:49:12 +01:00
|
|
|
code: script.code,
|
2022-01-26 06:58:02 +01:00
|
|
|
ramUsage: script.ramUsage,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
};
|
2022-01-20 09:03:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
(document as any).deleteFile = function (filename: string): IReturnWebStatus {
|
|
|
|
filename = normalizeFileName(filename);
|
2021-12-21 19:41:11 +01:00
|
|
|
const home = GetServer("home");
|
2022-01-20 09:03:07 +01:00
|
|
|
if (home === null) {
|
|
|
|
return {
|
|
|
|
res: false,
|
2022-01-26 06:58:02 +01:00
|
|
|
msg: "Home server does not exist.",
|
|
|
|
};
|
2022-01-20 09:03:07 +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
|
|
|
|
2022-01-20 09:03:07 +01:00
|
|
|
code = Buffer.from(code, "base64").toString();
|
|
|
|
const home = GetServer("home");
|
|
|
|
if (home === null) {
|
|
|
|
return {
|
|
|
|
res: false,
|
2022-01-26 06:58:02 +01:00
|
|
|
msg: "Home server does not exist.",
|
|
|
|
};
|
2021-12-21 19:41:11 +01:00
|
|
|
}
|
2022-01-26 06:58:02 +01:00
|
|
|
const { success, overwritten } = home.writeToScriptFile(Player, filename, code);
|
2022-01-22 05:49:12 +01:00
|
|
|
let script;
|
|
|
|
if (success) {
|
|
|
|
script = home.getScript(filename);
|
|
|
|
}
|
2022-01-20 09:03:07 +01:00
|
|
|
return {
|
2022-01-22 05:49:12 +01:00
|
|
|
res: success,
|
2022-01-20 09:03:07 +01:00
|
|
|
data: {
|
2022-01-22 05:49:12 +01:00
|
|
|
overwritten,
|
2022-01-26 06:58:02 +01:00
|
|
|
ramUsage: script?.ramUsage,
|
|
|
|
},
|
2022-01-20 09:03:07 +01:00
|
|
|
};
|
2021-12-21 19:41:11 +01:00
|
|
|
};
|
|
|
|
}
|
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.
|
2022-01-18 18:21:53 +01:00
|
|
|
(window as any).appNotifier = funcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initSaveFunctions(): void {
|
|
|
|
const funcs = {
|
|
|
|
triggerSave: (): Promise<void> => saveObject.saveGame(true),
|
|
|
|
triggerGameExport: (): void => {
|
|
|
|
try {
|
|
|
|
saveObject.exportGame();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
SnackbarEvents.emit("Could not export game.", "error", 2000);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
triggerScriptsExport: (): void => exportScripts("*", Player.getHomeComputer()),
|
|
|
|
getSaveData: (): { save: string; fileName: string } => {
|
|
|
|
return {
|
|
|
|
save: saveObject.getSaveString(Settings.ExcludeRunningScriptsFromSave),
|
|
|
|
fileName: saveObject.getSaveFileName(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
getSaveInfo: async (base64save: string): Promise<ImportPlayerData | undefined> => {
|
|
|
|
try {
|
|
|
|
const data = await saveObject.getImportDataFromString(base64save);
|
|
|
|
return data.playerData;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pushSaveData: (base64save: string, automatic = false): void => Router.toImportSave(base64save, automatic),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Will be consumud by the electron wrapper.
|
|
|
|
(window as any).appSaveFns = funcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initElectronBridge(): void {
|
|
|
|
const bridge = (window as any).electronBridge as any;
|
|
|
|
if (!bridge) return;
|
|
|
|
|
|
|
|
bridge.receive("get-save-data-request", () => {
|
|
|
|
const data = (window as any).appSaveFns.getSaveData();
|
|
|
|
bridge.send("get-save-data-response", data);
|
|
|
|
});
|
|
|
|
bridge.receive("get-save-info-request", async (save: string) => {
|
|
|
|
const data = await (window as any).appSaveFns.getSaveInfo(save);
|
|
|
|
bridge.send("get-save-info-response", data);
|
|
|
|
});
|
|
|
|
bridge.receive("push-save-request", ({ save, automatic = false }: { save: string; automatic: boolean }) => {
|
|
|
|
(window as any).appSaveFns.pushSaveData(save, automatic);
|
|
|
|
});
|
|
|
|
bridge.receive("trigger-save", () => {
|
|
|
|
return (window as any).appSaveFns
|
|
|
|
.triggerSave()
|
|
|
|
.then(() => {
|
|
|
|
bridge.send("save-completed");
|
|
|
|
})
|
|
|
|
.catch((error: any) => {
|
|
|
|
console.log(error);
|
|
|
|
SnackbarEvents.emit("Could not save game.", "error", 2000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
bridge.receive("trigger-game-export", () => {
|
|
|
|
try {
|
|
|
|
(window as any).appSaveFns.triggerGameExport();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
SnackbarEvents.emit("Could not export game.", "error", 2000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
bridge.receive("trigger-scripts-export", () => {
|
|
|
|
try {
|
|
|
|
(window as any).appSaveFns.triggerScriptsExport();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
SnackbarEvents.emit("Could not export scripts.", "error", 2000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pushGameSaved(data: SaveData): void {
|
|
|
|
const bridge = (window as any).electronBridge as any;
|
|
|
|
if (!bridge) return;
|
|
|
|
|
|
|
|
bridge.send("push-game-saved", data);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pushGameReady(): void {
|
|
|
|
const bridge = (window as any).electronBridge as any;
|
|
|
|
if (!bridge) return;
|
|
|
|
|
|
|
|
// Send basic information to the electron wrapper
|
|
|
|
bridge.send("push-game-ready", {
|
|
|
|
player: {
|
|
|
|
identifier: Player.identifier,
|
|
|
|
playtime: Player.totalPlaytime,
|
|
|
|
lastSave: Player.lastSave,
|
|
|
|
},
|
|
|
|
game: {
|
|
|
|
version: CONSTANTS.VersionString,
|
|
|
|
hash: hash(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pushImportResult(wasImported: boolean): void {
|
|
|
|
const bridge = (window as any).electronBridge as any;
|
|
|
|
if (!bridge) return;
|
|
|
|
|
|
|
|
bridge.send("push-import-result", { wasImported });
|
|
|
|
pushDisableRestore();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pushDisableRestore(): void {
|
|
|
|
const bridge = (window as any).electronBridge as any;
|
|
|
|
if (!bridge) return;
|
|
|
|
|
|
|
|
bridge.send("push-disable-restore", { duration: 1000 * 60 });
|
2022-01-03 16:32:01 +01:00
|
|
|
}
|