hydra/src/main/events/library/add-game-to-library.ts
2024-05-24 00:26:21 +01:00

57 lines
1.4 KiB
TypeScript

import { gameRepository } from "@main/repository";
import { registerEvent } from "../register-event";
import type { GameShop } from "@types";
import { getFileBase64, getSteamAppAsset } from "@main/helpers";
import { stateManager } from "@main/state-manager";
const addGameToLibrary = async (
_event: Electron.IpcMainInvokeEvent,
objectID: string,
title: string,
shop: GameShop,
executablePath: string | null
) => {
return gameRepository
.update(
{
objectID,
},
{
shop,
status: null,
executablePath,
isDeleted: false,
}
)
.then(async ({ affected }) => {
if (!affected) {
const steamGame = stateManager
.getValue("steamGames")
.find((game) => game.id === Number(objectID));
const iconUrl = steamGame?.clientIcon
? getSteamAppAsset("icon", objectID, steamGame.clientIcon)
: null;
await gameRepository
.insert({
title,
iconUrl,
objectID,
shop,
executablePath,
})
.then(() => {
if (iconUrl) {
getFileBase64(iconUrl).then((base64) =>
gameRepository.update({ objectID }, { iconUrl: base64 })
);
}
});
}
});
};
registerEvent("addGameToLibrary", addGameToLibrary);