Feat: Custom Games

This commit is contained in:
Moyasee
2025-09-19 16:22:12 +03:00
parent 7e59e02d03
commit 3409b53268
17 changed files with 158 additions and 111 deletions

View File

@@ -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);

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,
};
})
);

View File

@@ -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);

View File

@@ -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,

View File

@@ -65,36 +65,61 @@ app.whenReady().then(async () => {
});
protocol.handle("gradient", (request) => {
const gradientCss = decodeURIComponent(request.url.slice("gradient:".length));
const match = gradientCss.match(/linear-gradient\(([^,]+),\s*([^,]+),\s*([^)]+)\)/);
const gradientCss = decodeURIComponent(
request.url.slice("gradient:".length)
);
const match = gradientCss.match(
/linear-gradient\(([^,]+),\s*([^,]+),\s*([^)]+)\)/
);
let direction = "45deg";
let color1 = '#4a90e2';
let color2 = '#7b68ee';
let color1 = "#4a90e2";
let color2 = "#7b68ee";
if (match) {
direction = match[1].trim();
color1 = match[2].trim();
color2 = match[3].trim();
}
let x1 = "0%", y1 = "0%", x2 = "100%", y2 = "100%";
let x1 = "0%",
y1 = "0%",
x2 = "100%",
y2 = "100%";
if (direction === "to right") {
x1 = "0%"; y1 = "0%"; x2 = "100%"; y2 = "0%";
x1 = "0%";
y1 = "0%";
x2 = "100%";
y2 = "0%";
} else if (direction === "to bottom") {
x1 = "0%"; y1 = "0%"; x2 = "0%"; y2 = "100%";
x1 = "0%";
y1 = "0%";
x2 = "0%";
y2 = "100%";
} else if (direction === "45deg") {
x1 = "0%"; y1 = "100%"; x2 = "100%"; y2 = "0%";
x1 = "0%";
y1 = "100%";
x2 = "100%";
y2 = "0%";
} else if (direction === "135deg") {
x1 = "0%"; y1 = "0%"; x2 = "100%"; y2 = "100%";
x1 = "0%";
y1 = "0%";
x2 = "100%";
y2 = "100%";
} else if (direction === "225deg") {
x1 = "100%"; y1 = "0%"; x2 = "0%"; y2 = "100%";
x1 = "100%";
y1 = "0%";
x2 = "0%";
y2 = "100%";
} else if (direction === "315deg") {
x1 = "100%"; y1 = "100%"; x2 = "0%"; y2 = "0%";
x1 = "100%";
y1 = "100%";
x2 = "0%";
y2 = "0%";
}
const svgContent = `
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300" viewBox="0 0 400 300">
<defs>
@@ -106,9 +131,9 @@ app.whenReady().then(async () => {
<rect width="100%" height="100%" fill="url(#grad)" />
</svg>
`;
return new Response(svgContent, {
headers: { 'Content-Type': 'image/svg+xml' }
headers: { "Content-Type": "image/svg+xml" },
});
});

View File

@@ -11,7 +11,8 @@ export const uploadGamesBatch = async () => {
.all()
.then((results) => {
return results.filter(
(game) => !game.isDeleted && game.remoteId === null && game.shop !== "custom"
(game) =>
!game.isDeleted && game.remoteId === null && game.shop !== "custom"
);
});