This commit is contained in:
lilezek
2024-05-04 19:35:49 +02:00
31 changed files with 1447 additions and 1484 deletions

View File

@@ -18,7 +18,6 @@ import "./library/open-game";
import "./library/open-game-installer";
import "./library/remove-game";
import "./library/remove-game-from-library";
import "./misc/get-or-cache-image";
import "./misc/open-external";
import "./misc/show-open-dialog";
import "./torrenting/cancel-game-download";

View File

@@ -3,7 +3,7 @@ import { gameRepository } from "@main/repository";
import { registerEvent } from "../register-event";
import type { GameShop } from "@types";
import { getImageBase64 } from "@main/helpers";
import { getFileBase64 } from "@main/helpers";
import { getSteamGameIconUrl } from "@main/services";
const addGameToLibrary = async (
@@ -11,7 +11,7 @@ const addGameToLibrary = async (
objectID: string,
title: string,
gameShop: GameShop,
executablePath: string
executablePath: string | null
) => {
const game = await gameRepository.findOne({
where: {
@@ -31,7 +31,7 @@ const addGameToLibrary = async (
}
);
} else {
const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID));
const iconUrl = await getFileBase64(await getSteamGameIconUrl(objectID));
return gameRepository.insert({
title,

View File

@@ -1,40 +0,0 @@
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { registerEvent } from "../register-event";
import { getFileBuffer } from "@main/helpers";
import { logger } from "@main/services";
import { imageCachePath } from "@main/constants";
const getOrCacheImage = async (
_event: Electron.IpcMainInvokeEvent,
url: string
) => {
if (!fs.existsSync(imageCachePath)) fs.mkdirSync(imageCachePath);
const extname = path.extname(url);
const checksum = crypto.createHash("sha256").update(url).digest("hex");
const cachePath = path.join(imageCachePath, `${checksum}${extname}`);
const cache = fs.existsSync(cachePath);
if (cache) return `hydra://${cachePath}`;
getFileBuffer(url).then((buffer) =>
fs.writeFile(cachePath, buffer, (err) => {
if (err) {
logger.error(`Failed to cache image`, err, {
method: "getOrCacheImage",
});
}
})
);
return url;
};
registerEvent(getOrCacheImage, {
name: "getOrCacheImage",
});

View File

@@ -5,7 +5,11 @@ import { registerEvent } from "../register-event";
const showOpenDialog = async (
_event: Electron.IpcMainInvokeEvent,
options: Electron.OpenDialogOptions
) => dialog.showOpenDialog(WindowManager.mainWindow, options);
) => {
if (WindowManager.mainWindow) {
dialog.showOpenDialog(WindowManager.mainWindow, options);
}
};
registerEvent(showOpenDialog, {
name: "showOpenDialog",

View File

@@ -4,7 +4,7 @@ import { gameRepository, repackRepository } from "@main/repository";
import { registerEvent } from "../register-event";
import type { GameShop } from "@types";
import { getImageBase64 } from "@main/helpers";
import { getFileBase64 } from "@main/helpers";
import { In } from "typeorm";
import { Downloader } from "@main/services/downloaders/downloader";
import { GameStatus } from "@globals";
@@ -68,7 +68,7 @@ const startGameDownload = async (
return game;
} else {
const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID));
const iconUrl = await getFileBase64(await getSteamGameIconUrl(objectID));
const createdGame = await gameRepository.save({
title,

View File

@@ -79,10 +79,24 @@ export const getFileBuffer = async (url: string) =>
response.arrayBuffer().then((buffer) => Buffer.from(buffer))
);
export const getImageBase64 = async (url: string) =>
getFileBuffer(url).then((buffer) => {
return `data:image/jpeg;base64,${Buffer.from(buffer).toString("base64")}`;
});
export const getFileBase64 = async (url: string) =>
fetch(url, { method: "GET" }).then((response) =>
response.arrayBuffer().then((buffer) => {
const base64 = Buffer.from(buffer).toString("base64");
const contentType = response.headers.get("content-type");
return `data:${contentType};base64,${base64}`;
})
);
export const steamUrlBuilder = {
library: (objectID: string) =>
`https://steamcdn-a.akamaihd.net/steam/apps/${objectID}/header.jpg`,
libraryHero: (objectID: string) =>
`https://steamcdn-a.akamaihd.net/steam/apps/${objectID}/library_hero.jpg`,
logo: (objectID: string) =>
`https://cdn.cloudflare.steamstatic.com/steam/apps/${objectID}/logo.png`,
};
export * from "./formatters";
export * from "./ps";

View File

@@ -85,7 +85,7 @@ app.on("second-instance", (_event, commandLine) => {
WindowManager.createMainWindow();
}
const [, path] = commandLine.pop()!.split("://");
const [, path] = commandLine.pop()?.split("://") ?? [];
if (path) WindowManager.redirect(path);
});

View File

@@ -41,7 +41,8 @@ export const getSteam250List = async () => {
).flat();
const gamesMap: Map<string, Steam250Game> = gamesList.reduce((map, item) => {
map.set(item.objectID, item);
if (item) map.set(item.objectID, item);
return map;
}, new Map());

View File

@@ -50,11 +50,15 @@ export class WindowManager {
this.loadURL();
this.mainWindow.removeMenu();
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
this.mainWindow.on("ready-to-show", () => {
if (!app.isPackaged) WindowManager.mainWindow?.webContents.openDevTools();
});
this.mainWindow.on("close", () => {
this.mainWindow.on("close", async () => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
if (userPreferences?.preferQuitInsteadOfHiding) {
app.quit();
}

View File

@@ -1,13 +1,16 @@
import { parentPort } from "worker_threads";
import parseTorrent from "parse-torrent";
import { getFileBuffer } from "@main/helpers";
const port = parentPort;
if (!port) throw new Error("IllegalState");
export const getFileBuffer = async (url: string) =>
fetch(url, { method: "GET" }).then((response) =>
response.arrayBuffer().then((buffer) => Buffer.from(buffer))
);
port.on("message", async (url: string) => {
const buffer = await getFileBuffer(url);
const torrent = await parseTorrent(buffer);
port.postMessage(torrent);
});