feat: only downloading files once

This commit is contained in:
Chubby Granny Chaser
2025-05-14 10:11:29 +01:00
parent 521d9faa0c
commit e8e524182a
2 changed files with 65 additions and 28 deletions

View File

@@ -40,4 +40,6 @@ export const backupsPath = path.join(SystemPath.getPath("userData"), "Backups");
export const appVersion = app.getVersion() + (isStaging ? "-staging" : "");
export const ASSETS_PATH = path.join(SystemPath.getPath("userData"), "Assets");
export const MAIN_LOOP_INTERVAL = 1500;

View File

@@ -14,6 +14,7 @@ import {
import fs from "node:fs";
import axios from "axios";
import path from "node:path";
import { ASSETS_PATH } from "@main/constants";
const downloadAsset = async (downloadPath: string, url?: string | null) => {
try {
@@ -37,6 +38,40 @@ const downloadAsset = async (downloadPath: string, url?: string | null) => {
}
};
const downloadAssetsFromSteam = async (
shop: GameShop,
objectId: string,
assets: GameStats["assets"]
) => {
const gameAssetsPath = path.join(ASSETS_PATH, `${shop}-${objectId}`);
return await Promise.all([
downloadAsset(path.join(gameAssetsPath, "icon.ico"), assets?.iconUrl),
downloadAsset(
path.join(gameAssetsPath, "hero.jpg"),
assets?.libraryHeroImageUrl
),
downloadAsset(path.join(gameAssetsPath, "logo.png"), assets?.logoImageUrl),
downloadAsset(
path.join(gameAssetsPath, "cover.jpg"),
assets?.coverImageUrl
),
downloadAsset(
path.join(gameAssetsPath, "library.jpg"),
assets?.libraryImageUrl
),
]);
};
const copyAssetIfExists = async (
sourcePath: string | null,
destinationPath: string
) => {
if (sourcePath && fs.existsSync(sourcePath)) {
await fs.promises.cp(sourcePath, destinationPath);
}
};
const createSteamShortcut = async (
_event: Electron.IpcMainInvokeEvent,
shop: GameShop,
@@ -61,19 +96,13 @@ const createSteamShortcut = async (
return;
}
const icon = await downloadAsset(
path.join(
SystemPath.getPath("userData"),
"Icons",
`${game.shop}-${game.objectId}.ico`
),
assets?.iconUrl
);
const [iconImage, heroImage, logoImage, coverImage, libraryImage] =
await downloadAssetsFromSteam(game.shop, game.objectId, assets);
const newShortcut = composeSteamShortcut(
game.title,
game.executablePath,
icon
iconImage
);
for (const steamUserId of steamUserIds) {
@@ -93,27 +122,29 @@ const createSteamShortcut = async (
"grid"
);
fs.mkdirSync(gridPath, { recursive: true });
await fs.promises.mkdir(gridPath, { recursive: true });
await Promise.allSettled([
downloadAsset(
path.join(gridPath, `${newShortcut.appid}_hero.jpg`),
assets?.libraryHeroImageUrl
await Promise.all([
copyAssetIfExists(
heroImage,
path.join(gridPath, `${newShortcut.appid}_hero.jpg`)
),
downloadAsset(
path.join(gridPath, `${newShortcut.appid}_logo.png`),
assets?.logoImageUrl
copyAssetIfExists(
logoImage,
path.join(gridPath, `${newShortcut.appid}_logo.png`)
),
downloadAsset(
path.join(gridPath, `${newShortcut.appid}p.jpg`),
assets?.coverImageUrl
copyAssetIfExists(
coverImage,
path.join(gridPath, `${newShortcut.appid}p.jpg`)
),
downloadAsset(
path.join(gridPath, `${newShortcut.appid}.jpg`),
assets?.libraryImageUrl
copyAssetIfExists(
libraryImage,
path.join(gridPath, `${newShortcut.appid}.jpg`)
),
]);
fs.mkdirSync(gridPath, { recursive: true });
steamShortcuts.push(newShortcut);
logger.info(newShortcut);
@@ -132,13 +163,17 @@ const createSteamShortcut = async (
"compatdata"
);
const winePrefixPath = path.join(
steamWinePrefixes,
newShortcut.appid.toString(),
"pfx"
);
await fs.promises.mkdir(winePrefixPath, { recursive: true });
await gamesSublevel.put(gameKey, {
...game,
winePrefixPath: path.join(
steamWinePrefixes,
newShortcut.appid.toString(),
"pfx"
),
winePrefixPath,
});
}
}