chore: update dependencies and refactor game launch logic

This commit is contained in:
Moyasee
2026-01-26 10:42:43 +02:00
parent a54dacb2da
commit edd94ba7c4
7 changed files with 92 additions and 62 deletions

View File

@@ -4,6 +4,7 @@ import path from "node:path";
import fs from "node:fs";
import { app } from "electron";
import axios from "axios";
import pngToIco from "png-to-ico";
import { removeSymbolsFromName } from "@shared";
import { GameShop, ShortcutLocation } from "@types";
import { gamesSublevel, levelKeys } from "@main/level";
@@ -17,25 +18,29 @@ const downloadIcon = async (
objectId: string,
iconUrl?: string | null
): Promise<string | null> => {
const iconPath = path.join(ASSETS_PATH, `${shop}-${objectId}`, "icon.ico");
if (!iconUrl) {
return null;
}
const iconDir = path.join(ASSETS_PATH, `${shop}-${objectId}`);
const iconPath = path.join(iconDir, "icon.ico");
try {
if (fs.existsSync(iconPath)) {
return iconPath;
}
if (!iconUrl) {
return null;
}
fs.mkdirSync(path.dirname(iconPath), { recursive: true });
fs.mkdirSync(iconDir, { recursive: true });
const response = await axios.get(iconUrl, { responseType: "arraybuffer" });
fs.writeFileSync(iconPath, response.data);
const imageBuffer = Buffer.from(response.data);
const icoBuffer = await pngToIco(imageBuffer);
fs.writeFileSync(iconPath, icoBuffer);
return iconPath;
} catch (error) {
logger.error("Failed to download game icon", error);
logger.error("Failed to download/convert game icon", error);
return null;
}
};

View File

@@ -1,11 +1,6 @@
import { registerEvent } from "../register-event";
import { shell } from "electron";
import { spawn } from "node:child_process";
import { parseExecutablePath } from "../helpers/parse-executable-path";
import { gamesSublevel, levelKeys } from "@main/level";
import { GameShop } from "@types";
import { parseLaunchOptions } from "../helpers/parse-launch-options";
import { WindowManager } from "@main/services";
import { launchGame } from "@main/helpers";
const openGame = async (
_event: Electron.IpcMainInvokeEvent,
@@ -14,32 +9,7 @@ const openGame = async (
executablePath: string,
launchOptions?: string | null
) => {
const parsedPath = parseExecutablePath(executablePath);
const parsedParams = parseLaunchOptions(launchOptions);
const gameKey = levelKeys.game(shop, objectId);
const game = await gamesSublevel.get(gameKey);
if (!game) return;
await gamesSublevel.put(gameKey, {
...game,
executablePath: parsedPath,
launchOptions,
});
// Always show the launcher window when launching a game
await WindowManager.createGameLauncherWindow(shop, objectId);
await new Promise((resolve) => setTimeout(resolve, 2000));
if (parsedParams.length === 0) {
shell.openPath(parsedPath);
return;
}
spawn(parsedPath, parsedParams, { shell: false, detached: true });
await launchGame({ shop, objectId, executablePath, launchOptions });
};
registerEvent("openGame", openGame);