Preload images for non-custom games. Added ability to restore images to default if game is non-custom

This commit is contained in:
Moyasee
2025-09-19 21:06:15 +03:00
parent 9f4fd0ce61
commit de70beb01e
7 changed files with 137 additions and 48 deletions

View File

@@ -9,27 +9,27 @@ const getCustomGamesAssetsPath = () => {
const getAllCustomGameAssets = async (): Promise<string[]> => {
const assetsPath = getCustomGamesAssetsPath();
if (!fs.existsSync(assetsPath)) {
return [];
}
const files = await fs.promises.readdir(assetsPath);
return files.map(file => path.join(assetsPath, file));
return files.map((file) => path.join(assetsPath, file));
};
const getUsedAssetPaths = async (): Promise<Set<string>> => {
// Get all custom games from the level database
const { gamesSublevel } = await import("@main/level");
const allGames = await gamesSublevel.iterator().all();
const customGames = allGames
.map(([_key, game]) => game)
.filter(game => game.shop === "custom" && !game.isDeleted);
.filter((game) => game.shop === "custom" && !game.isDeleted);
const usedPaths = new Set<string>();
customGames.forEach(game => {
customGames.forEach((game) => {
// Extract file paths from local URLs
if (game.iconUrl?.startsWith("local:")) {
usedPaths.add(game.iconUrl.replace("local:", ""));
@@ -45,11 +45,14 @@ const getUsedAssetPaths = async (): Promise<Set<string>> => {
return usedPaths;
};
export const cleanupUnusedAssets = async (): Promise<{ deletedCount: number; errors: string[] }> => {
export const cleanupUnusedAssets = async (): Promise<{
deletedCount: number;
errors: string[];
}> => {
try {
const allAssets = await getAllCustomGameAssets();
const usedAssets = await getUsedAssetPaths();
const errors: string[] = [];
let deletedCount = 0;
@@ -70,4 +73,4 @@ export const cleanupUnusedAssets = async (): Promise<{ deletedCount: number; err
}
};
ipcMain.handle("cleanupUnusedAssets", cleanupUnusedAssets);
ipcMain.handle("cleanupUnusedAssets", cleanupUnusedAssets);

View File

@@ -26,7 +26,7 @@ const copyCustomGameAsset = async (
// Get file extension
const fileExtension = path.extname(sourcePath);
// Generate unique filename
const uniqueId = randomUUID();
const fileName = `${assetType}-${uniqueId}${fileExtension}`;
@@ -39,4 +39,4 @@ const copyCustomGameAsset = async (
return `local:${destinationPath}`;
};
registerEvent("copyCustomGameAsset", copyCustomGameAsset);
registerEvent("copyCustomGameAsset", copyCustomGameAsset);

View File

@@ -21,9 +21,9 @@ const updateGameCustomAssets = async (
const updatedGame = {
...existingGame,
title,
customIconUrl: customIconUrl ?? existingGame.customIconUrl,
customLogoImageUrl: customLogoImageUrl ?? existingGame.customLogoImageUrl,
customHeroImageUrl: customHeroImageUrl ?? existingGame.customHeroImageUrl,
customIconUrl: customIconUrl !== undefined ? customIconUrl : existingGame.customIconUrl,
customLogoImageUrl: customLogoImageUrl !== undefined ? customLogoImageUrl : existingGame.customLogoImageUrl,
customHeroImageUrl: customHeroImageUrl !== undefined ? customHeroImageUrl : existingGame.customHeroImageUrl,
};
await gamesSublevel.put(gameKey, updatedGame);