feat: move leveldb read out of find achievement files loop

This commit is contained in:
Zamitto
2026-05-04 09:24:55 -03:00
parent a1f3c6b711
commit 697cb75f11
2 changed files with 62 additions and 23 deletions
@@ -36,6 +36,15 @@ const watchAchievementsWindows = async () => {
const achievementFiles = findAllAchievementFiles();
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
{
valueEncoding: "json",
}
);
const enableSteamAchievements =
userPreferences?.enableSteamAchievements ?? false;
for (const game of games) {
const gameAchievementFiles: AchievementFile[] = [];
@@ -46,9 +55,9 @@ const watchAchievementsWindows = async () => {
...findAchievementFileInExecutableDirectory(game)
);
gameAchievementFiles.push(
...(await findAchievementFileInSteamPath(game))
);
if (enableSteamAchievements) {
gameAchievementFiles.push(...findAchievementFileInSteamPath(game));
}
}
for (const file of gameAchievementFiles) {
@@ -69,10 +78,23 @@ const watchAchievementsWithWine = async () => {
)
);
if (games.length === 0) return;
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
{
valueEncoding: "json",
}
);
const enableSteamAchievements =
userPreferences?.enableSteamAchievements ?? false;
for (const game of games) {
const gameAchievementFiles = findAchievementFiles(game);
gameAchievementFiles.push(...(await findAchievementFileInSteamPath(game)));
if (enableSteamAchievements) {
gameAchievementFiles.push(...findAchievementFileInSteamPath(game));
}
for (const file of gameAchievementFiles) {
await compareFile(game, file);
@@ -184,7 +206,16 @@ export class AchievementWatcherManager {
const gameAchievementFiles = findAchievementFiles(game);
gameAchievementFiles.push(...(await findAchievementFileInSteamPath(game)));
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
{
valueEncoding: "json",
}
);
if (userPreferences?.enableSteamAchievements) {
gameAchievementFiles.push(...findAchievementFileInSteamPath(game));
}
const unlockedAchievements: UnlockedAchievement[] = [];
@@ -265,6 +296,15 @@ export class AchievementWatcherManager {
const gameAchievementFilesMap = findAllAchievementFiles();
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
{
valueEncoding: "json",
}
);
const enableSteamAchievements =
userPreferences?.enableSteamAchievements ?? false;
return Promise.all(
games.map(async (game) => {
const achievementFiles: AchievementFile[] = [];
@@ -278,9 +318,9 @@ export class AchievementWatcherManager {
...findAchievementFileInExecutableDirectory(game)
);
achievementFiles.push(
...(await findAchievementFileInSteamPath(game))
);
if (enableSteamAchievements) {
achievementFiles.push(...findAchievementFileInSteamPath(game));
}
}
return { game, achievementFiles };
@@ -294,11 +334,22 @@ export class AchievementWatcherManager {
.all()
.then((games) => games.filter((game) => !game.isDeleted));
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
{
valueEncoding: "json",
}
);
const enableSteamAchievements =
userPreferences?.enableSteamAchievements ?? false;
return Promise.all(
games.map(async (game) => {
const achievementFiles = findAchievementFiles(game);
achievementFiles.push(...(await findAchievementFileInSteamPath(game)));
if (enableSteamAchievements) {
achievementFiles.push(...findAchievementFileInSteamPath(game));
}
return { game, achievementFiles };
})
@@ -1,12 +1,11 @@
import path from "node:path";
import fs from "node:fs";
import type { Game, AchievementFile, UserPreferences } from "@types";
import type { Game, AchievementFile } from "@types";
import { Cracker } from "@shared";
import { achievementsLogger } from "../logger";
import { SystemPath } from "../system-path";
import { getSteamLocation, getSteamUsersIds } from "../steam";
import { Wine } from "../wine";
import { db, levelKeys } from "@main/level";
const getAppDataPath = () => {
if (process.platform === "win32") {
@@ -284,7 +283,7 @@ export const findAchievementFiles = (game: Game) => {
const steamUserIds = await getSteamUsersIds();
const steamPath = await getSteamLocation().catch(() => null);
export const findAchievementFileInSteamPath = async (game: Game) => {
export const findAchievementFileInSteamPath = (game: Game) => {
if (!steamUserIds.length) {
return [];
}
@@ -293,17 +292,6 @@ export const findAchievementFileInSteamPath = async (game: Game) => {
return [];
}
const userPreferences = await db.get<string, UserPreferences | null>(
levelKeys.userPreferences,
{
valueEncoding: "json",
}
);
if (!userPreferences?.enableSteamAchievements) {
return [];
}
const achievementFiles: AchievementFile[] = [];
for (const steamUserId of steamUserIds) {