hydra/src/preload/index.ts

166 lines
6.9 KiB
TypeScript
Raw Normal View History

2024-04-21 08:26:29 +03:00
// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
import { contextBridge, ipcRenderer } from "electron";
import type {
GameShop,
2024-05-20 04:21:11 +03:00
DownloadProgress,
2024-04-21 08:26:29 +03:00
UserPreferences,
AppUpdaterEvent,
StartGameDownloadPayload,
2024-06-21 00:09:49 +03:00
GameRunning,
2024-07-11 01:52:57 +03:00
FriendRequestAction,
2024-04-21 08:26:29 +03:00
} from "@types";
contextBridge.exposeInMainWorld("electron", {
/* Torrenting */
startGameDownload: (payload: StartGameDownloadPayload) =>
ipcRenderer.invoke("startGameDownload", payload),
2024-04-21 08:26:29 +03:00
cancelGameDownload: (gameId: number) =>
ipcRenderer.invoke("cancelGameDownload", gameId),
pauseGameDownload: (gameId: number) =>
ipcRenderer.invoke("pauseGameDownload", gameId),
resumeGameDownload: (gameId: number) =>
ipcRenderer.invoke("resumeGameDownload", gameId),
2024-05-20 04:21:11 +03:00
onDownloadProgress: (cb: (value: DownloadProgress) => void) => {
2024-04-21 08:26:29 +03:00
const listener = (
_event: Electron.IpcRendererEvent,
2024-05-20 04:21:11 +03:00
value: DownloadProgress
2024-04-21 08:26:29 +03:00
) => cb(value);
ipcRenderer.on("on-download-progress", listener);
return () => ipcRenderer.removeListener("on-download-progress", listener);
},
/* Catalogue */
searchGames: (query: string) => ipcRenderer.invoke("searchGames", query),
2024-06-03 04:12:05 +03:00
getCatalogue: () => ipcRenderer.invoke("getCatalogue"),
2024-04-21 08:26:29 +03:00
getGameShopDetails: (objectID: string, shop: GameShop, language: string) =>
ipcRenderer.invoke("getGameShopDetails", objectID, shop, language),
getRandomGame: () => ipcRenderer.invoke("getRandomGame"),
getHowLongToBeat: (objectID: string, shop: GameShop, title: string) =>
ipcRenderer.invoke("getHowLongToBeat", objectID, shop, title),
getGames: (take?: number, prevCursor?: number) =>
ipcRenderer.invoke("getGames", take, prevCursor),
2024-05-13 01:43:00 +03:00
searchGameRepacks: (query: string) =>
ipcRenderer.invoke("searchGameRepacks", query),
2024-04-21 08:26:29 +03:00
/* User preferences */
getUserPreferences: () => ipcRenderer.invoke("getUserPreferences"),
updateUserPreferences: (preferences: UserPreferences) =>
ipcRenderer.invoke("updateUserPreferences", preferences),
autoLaunch: (enabled: boolean) => ipcRenderer.invoke("autoLaunch", enabled),
2024-05-28 16:01:28 +03:00
authenticateRealDebrid: (apiToken: string) =>
ipcRenderer.invoke("authenticateRealDebrid", apiToken),
2024-04-21 08:26:29 +03:00
2024-06-03 04:12:05 +03:00
/* Download sources */
getDownloadSources: () => ipcRenderer.invoke("getDownloadSources"),
validateDownloadSource: (url: string) =>
ipcRenderer.invoke("validateDownloadSource", url),
addDownloadSource: (url: string) =>
ipcRenderer.invoke("addDownloadSource", url),
removeDownloadSource: (id: number) =>
ipcRenderer.invoke("removeDownloadSource", id),
2024-06-05 16:18:40 +03:00
syncDownloadSources: () => ipcRenderer.invoke("syncDownloadSources"),
2024-06-03 04:12:05 +03:00
2024-04-21 08:26:29 +03:00
/* Library */
2024-06-06 02:29:42 +03:00
addGameToLibrary: (objectID: string, title: string, shop: GameShop) =>
ipcRenderer.invoke("addGameToLibrary", objectID, title, shop),
2024-06-05 22:42:45 +03:00
createGameShortcut: (id: number) =>
ipcRenderer.invoke("createGameShortcut", id),
updateExecutablePath: (id: number, executablePath: string) =>
ipcRenderer.invoke("updateExecutablePath", id, executablePath),
2024-04-21 08:26:29 +03:00
getLibrary: () => ipcRenderer.invoke("getLibrary"),
openGameInstaller: (gameId: number) =>
ipcRenderer.invoke("openGameInstaller", gameId),
openGameInstallerPath: (gameId: number) =>
ipcRenderer.invoke("openGameInstallerPath", gameId),
openGameExecutablePath: (gameId: number) =>
ipcRenderer.invoke("openGameExecutablePath", gameId),
2024-04-21 08:26:29 +03:00
openGame: (gameId: number, executablePath: string) =>
ipcRenderer.invoke("openGame", gameId, executablePath),
closeGame: (gameId: number) => ipcRenderer.invoke("closeGame", gameId),
2024-04-29 13:01:34 +03:00
removeGameFromLibrary: (gameId: number) =>
ipcRenderer.invoke("removeGameFromLibrary", gameId),
removeGame: (gameId: number) => ipcRenderer.invoke("removeGame", gameId),
2024-04-21 08:26:29 +03:00
deleteGameFolder: (gameId: number) =>
ipcRenderer.invoke("deleteGameFolder", gameId),
getGameByObjectID: (objectID: string) =>
ipcRenderer.invoke("getGameByObjectID", objectID),
2024-06-21 00:09:49 +03:00
onGamesRunning: (
cb: (
gamesRunning: Pick<GameRunning, "id" | "sessionDurationInMillis">[]
) => void
) => {
const listener = (_event: Electron.IpcRendererEvent, gamesRunning) =>
cb(gamesRunning);
ipcRenderer.on("on-games-running", listener);
return () => ipcRenderer.removeListener("on-games-running", listener);
2024-04-21 08:26:29 +03:00
},
onLibraryBatchComplete: (cb: () => void) => {
const listener = (_event: Electron.IpcRendererEvent) => cb();
ipcRenderer.on("on-library-batch-complete", listener);
return () =>
ipcRenderer.removeListener("on-library-batch-complete", listener);
},
2024-04-21 08:26:29 +03:00
/* Hardware */
2024-04-21 07:02:17 +03:00
getDiskFreeSpace: (path: string) =>
ipcRenderer.invoke("getDiskFreeSpace", path),
2024-04-21 08:26:29 +03:00
/* Misc */
ping: () => ipcRenderer.invoke("ping"),
getVersion: () => ipcRenderer.invoke("getVersion"),
getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"),
isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"),
2024-04-21 08:26:29 +03:00
openExternal: (src: string) => ipcRenderer.invoke("openExternal", src),
showOpenDialog: (options: Electron.OpenDialogOptions) =>
ipcRenderer.invoke("showOpenDialog", options),
platform: process.platform,
2024-05-19 07:39:50 +03:00
/* Auto update */
onAutoUpdaterEvent: (cb: (value: AppUpdaterEvent) => void) => {
2024-05-19 20:15:07 +03:00
const listener = (
_event: Electron.IpcRendererEvent,
value: AppUpdaterEvent
2024-05-19 20:15:07 +03:00
) => cb(value);
ipcRenderer.on("autoUpdaterEvent", listener);
return () => {
ipcRenderer.removeListener("autoUpdaterEvent", listener);
};
},
checkForUpdates: () => ipcRenderer.invoke("checkForUpdates"),
restartAndInstallUpdate: () => ipcRenderer.invoke("restartAndInstallUpdate"),
2024-06-12 04:09:24 +03:00
/* Profile */
getMe: () => ipcRenderer.invoke("getMe"),
2024-06-18 03:55:43 +03:00
updateProfile: (displayName: string, newProfileImagePath: string | null) =>
ipcRenderer.invoke("updateProfile", displayName, newProfileImagePath),
2024-07-11 00:52:08 +03:00
getFriendRequests: () => ipcRenderer.invoke("getFriendRequests"),
2024-07-11 01:52:57 +03:00
updateFriendRequest: (userId: string, action: FriendRequestAction) =>
ipcRenderer.invoke("updateFriendRequest", userId, action),
2024-07-11 03:04:45 +03:00
sendFriendRequest: (userId: string) =>
ipcRenderer.invoke("sendFriendRequest", userId),
2024-06-16 19:58:24 +03:00
/* User */
2024-06-17 01:51:09 +03:00
getUser: (userId: string) => ipcRenderer.invoke("getUser", userId),
2024-07-24 02:27:38 +03:00
getUserFriends: (userId: string, take: number, skip: number) =>
ipcRenderer.invoke("getUserFriends", userId, take, skip),
2024-06-16 19:58:24 +03:00
/* Auth */
2024-06-17 04:19:36 +03:00
signOut: () => ipcRenderer.invoke("signOut"),
2024-06-20 15:22:12 +03:00
openAuthWindow: () => ipcRenderer.invoke("openAuthWindow"),
2024-06-21 04:37:49 +03:00
getSessionHash: () => ipcRenderer.invoke("getSessionHash"),
2024-06-16 19:58:24 +03:00
onSignIn: (cb: () => void) => {
const listener = (_event: Electron.IpcRendererEvent) => cb();
ipcRenderer.on("on-signin", listener);
return () => ipcRenderer.removeListener("on-signin", listener);
},
onSignOut: (cb: () => void) => {
const listener = (_event: Electron.IpcRendererEvent) => cb();
ipcRenderer.on("on-signout", listener);
return () => ipcRenderer.removeListener("on-signout", listener);
},
2024-04-21 08:26:29 +03:00
});