mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-28 21:31:03 +00:00
Feat: Custom Games
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import {
|
||||
gamesSublevel,
|
||||
gamesShopAssetsSublevel,
|
||||
levelKeys,
|
||||
} from "@main/level";
|
||||
import { gamesSublevel, gamesShopAssetsSublevel, levelKeys } from "@main/level";
|
||||
import { randomUUID } from "crypto";
|
||||
import type { GameShop } from "@types";
|
||||
|
||||
|
||||
const addCustomGameToLibrary = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
title: string,
|
||||
@@ -21,12 +16,14 @@ const addCustomGameToLibrary = async (
|
||||
const gameKey = levelKeys.game(shop, objectId);
|
||||
|
||||
const existingGames = await gamesSublevel.iterator().all();
|
||||
const existingGame = existingGames.find(([_key, game]) =>
|
||||
game.executablePath === executablePath && !game.isDeleted
|
||||
const existingGame = existingGames.find(
|
||||
([_key, game]) => game.executablePath === executablePath && !game.isDeleted
|
||||
);
|
||||
|
||||
if (existingGame) {
|
||||
throw new Error("A game with this executable path already exists in your library");
|
||||
throw new Error(
|
||||
"A game with this executable path already exists in your library"
|
||||
);
|
||||
}
|
||||
|
||||
const assets = {
|
||||
@@ -65,4 +62,4 @@ const addCustomGameToLibrary = async (
|
||||
return game;
|
||||
};
|
||||
|
||||
registerEvent("addCustomGameToLibrary", addCustomGameToLibrary);
|
||||
registerEvent("addCustomGameToLibrary", addCustomGameToLibrary);
|
||||
|
||||
@@ -30,6 +30,8 @@ const addGameToLibrary = async (
|
||||
game = {
|
||||
title,
|
||||
iconUrl: gameAssets?.iconUrl ?? null,
|
||||
libraryHeroImageUrl: gameAssets?.libraryHeroImageUrl ?? null,
|
||||
logoImageUrl: gameAssets?.logoImageUrl ?? null,
|
||||
objectId,
|
||||
shop,
|
||||
remoteId: null,
|
||||
|
||||
@@ -13,13 +13,13 @@ const changeGamePlaytime = async (
|
||||
const gameKey = levelKeys.game(shop, objectId);
|
||||
const game = await gamesSublevel.get(gameKey);
|
||||
if (!game) return;
|
||||
|
||||
|
||||
if (game.remoteId) {
|
||||
await HydraApi.put(`/profile/games/${shop}/${objectId}/playtime`, {
|
||||
playTimeInSeconds,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
await gamesSublevel.put(gameKey, {
|
||||
...game,
|
||||
playTimeInMilliseconds: playTimeInSeconds * 1000,
|
||||
|
||||
@@ -18,11 +18,17 @@ const getLibrary = async (): Promise<LibraryGame[]> => {
|
||||
const download = await downloadsSublevel.get(key);
|
||||
const gameAssets = await gamesShopAssetsSublevel.get(key);
|
||||
|
||||
// 确保返回的对象符合 LibraryGame 类型
|
||||
return {
|
||||
id: key,
|
||||
...game,
|
||||
download: download ?? null,
|
||||
...gameAssets,
|
||||
// 确保 gameAssets 中的可能为 null 的字段转换为 undefined
|
||||
libraryHeroImageUrl: gameAssets?.libraryHeroImageUrl ?? undefined,
|
||||
libraryImageUrl: gameAssets?.libraryImageUrl ?? undefined,
|
||||
logoImageUrl: gameAssets?.logoImageUrl ?? undefined,
|
||||
logoPosition: gameAssets?.logoPosition ?? undefined,
|
||||
coverImageUrl: gameAssets?.coverImageUrl ?? undefined,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import {
|
||||
gamesSublevel,
|
||||
gamesShopAssetsSublevel,
|
||||
levelKeys,
|
||||
} from "@main/level";
|
||||
import { gamesSublevel, gamesShopAssetsSublevel, levelKeys } from "@main/level";
|
||||
import type { GameShop } from "@types";
|
||||
|
||||
const updateCustomGame = async (
|
||||
@@ -16,7 +12,7 @@ const updateCustomGame = async (
|
||||
libraryHeroImageUrl?: string
|
||||
) => {
|
||||
const gameKey = levelKeys.game(shop, objectId);
|
||||
|
||||
|
||||
const existingGame = await gamesSublevel.get(gameKey);
|
||||
if (!existingGame) {
|
||||
throw new Error("Game not found");
|
||||
@@ -50,4 +46,4 @@ const updateCustomGame = async (
|
||||
return updatedGame;
|
||||
};
|
||||
|
||||
registerEvent("updateCustomGame", updateCustomGame);
|
||||
registerEvent("updateCustomGame", updateCustomGame);
|
||||
|
||||
@@ -53,6 +53,8 @@ const startGameDownload = async (
|
||||
await gamesSublevel.put(gameKey, {
|
||||
title,
|
||||
iconUrl: gameAssets?.iconUrl ?? null,
|
||||
libraryHeroImageUrl: gameAssets?.libraryHeroImageUrl ?? null,
|
||||
logoImageUrl: gameAssets?.logoImageUrl ?? null,
|
||||
objectId,
|
||||
shop,
|
||||
remoteId: null,
|
||||
|
||||
Reference in New Issue
Block a user