UI: Add option to enable/disable syncing Steam achievements (#2117)

This commit is contained in:
catloversg
2025-05-10 16:13:41 +07:00
committed by GitHub
parent 290557332c
commit 494ef0dff3
13 changed files with 197 additions and 64 deletions

View File

@@ -1,25 +1,27 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { ipcMain } = require("electron");
const { steamworksClient } = require("./steamworksUtils");
const log = require("electron-log");
function enableAchievementsInterval(window) {
function enableSyncingAchievements() {
// If the Steam API could not be initialized on game start, we'll abort this.
if (!steamworksClient) {
return;
}
// This is backward but the game fills in an array called `document.achievements` and we retrieve it from
// here. Hey if it works it works.
const allSteamAchievements = steamworksClient.achievement.names();
log.silly(`All Steam achievements ${JSON.stringify(allSteamAchievements)}`);
const steamAchievements = allSteamAchievements.filter((achievement) =>
steamworksClient.achievement.isActivated(achievement),
);
log.debug(`Player has Steam achievements ${JSON.stringify(steamAchievements)}`);
const intervalID = setInterval(async () => {
ipcMain.on("activate-achievements", async (_event, data) => {
if (!data || !Array.isArray(data.achievements)) {
log.info("Achievement list is invalid. Data:", data);
return;
}
try {
const playerAchievements = await window.webContents.executeJavaScript("document.achievements");
for (const achievement of playerAchievements) {
for (const achievement of data.achievements) {
// Don't try activating achievements that don't exist Steam-side
if (!allSteamAchievements.includes(achievement)) {
continue;
@@ -37,22 +39,10 @@ function enableAchievementsInterval(window) {
}
} catch (error) {
log.error(error);
// The interval probably did not get cleared after a window kill
log.warn("Clearing achievements timer");
clearInterval(intervalID);
}
}, 1000);
window.achievementsIntervalID = intervalID;
}
function disableAchievementsInterval(window) {
if (window.achievementsIntervalID) {
clearInterval(window.achievementsIntervalID);
}
});
}
module.exports = {
enableAchievementsInterval,
disableAchievementsInterval,
enableSyncingAchievements,
};

View File

@@ -59,7 +59,7 @@ async function createWindow(killall) {
window.webContents.backgroundThrottling = false;
achievements.enableAchievementsInterval(window);
achievements.enableSyncingAchievements();
utils.attachUnresponsiveAppHandler(window);
menu.refreshMenu(window);

View File

@@ -21,7 +21,6 @@ app.on("window-all-closed", () => {
require("./steamworksUtils");
const gameWindow = require("./gameWindow");
const achievements = require("./achievements");
const utils = require("./utils");
const storage = require("./storage");
const debounce = require("lodash/debounce");
@@ -43,9 +42,6 @@ function setStopProcessHandler(window) {
// We need to prevent the default closing event to add custom logic
e.preventDefault();
// First we clear the achievement timer
achievements.disableAchievementsInterval(window);
// Trigger debounced saves right now before closing
try {
await saveToDisk.flush();

View File

@@ -11,6 +11,7 @@ contextBridge.exposeInMainWorld("electronBridge", {
"push-game-ready",
"push-import-result",
"push-disable-restore",
"activate-achievements",
];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);