2021-12-29 14:46:56 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
const greenworks = require("./greenworks");
|
2022-01-05 20:17:54 +01:00
|
|
|
const log = require("electron-log");
|
2021-12-29 14:46:56 +01:00
|
|
|
|
2022-01-12 19:34:17 +01:00
|
|
|
async function enableAchievementsInterval(window) {
|
2022-01-14 12:06:59 +01:00
|
|
|
// If the Steam API could not be initialized on game start, we'll abort this.
|
|
|
|
if (global.greenworksError) return;
|
|
|
|
|
2022-04-07 01:30:08 +02:00
|
|
|
// This is backward but the game fills in an array called `document.achievements` and we retrieve it from
|
2021-12-29 14:46:56 +01:00
|
|
|
// here. Hey if it works it works.
|
|
|
|
const steamAchievements = greenworks.getAchievementNames();
|
Add support for Steam Cloud & local filesystem
Adds actions to save the game's data directly in the filesystem and/or
into Steam Cloud. Them game will push an event with the save data
whenever the game is saved. Electron will use that data to persist it
into the User Data folder, until the folder reaches a certain size.
Once over the quota, it's going to remove previous saves.
Files are grouped according the the player's identifier, ensuring
backups off different playthroughs if importing.
Optionally, the file will be gzipped before saving to disk, largely
reducing file size.
Adds a way to save & load from Steam Cloud, currently manually only.
When loading a save, it'll trigger the new "Import Data Comparison" page
to accept or cancel the import.
When saving the game, it will save to Steam Cloud & to filesystem if the
options are enabled.
Add automatic game restore
Detects when the player has access to a newer game that has been saved
more recently than the one being loaded. It checks both in the Steam
Cloud and on the local filesystem. Adds an option to disable the
feature.
- Adds a "Save Game" menu item that triggers the game's save.
- Adds a "Export Game" menu item that triggers the download file popup.
- Adds a "Export Scripts" menu item that triggers the "download *"
terminal command.
- Adds a "Load Last Save" menu item that loads the latest file modified
in the user data folder.
- Adds a "Load from Steam Cloud" menu item.
- Adds a "Load From File" menu item that popups a file selector & loads
the file.
- Adds settings for "Saves Compression (.gz)", "Auto-save Backups" &
"Auto-save to Steam", toggleable through the menu.
- Adds a "Open Game Data","Open Saves", "Open Logs" & "Open User Data" menu items.
- Adds a "Quit" menu item.
2022-01-23 19:51:17 +01:00
|
|
|
log.silly(`All Steam achievements ${JSON.stringify(steamAchievements)}`);
|
2022-04-07 01:30:08 +02:00
|
|
|
const playerAchieved = (await Promise.all(steamAchievements.map(checkSteamAchievement))).filter((name) => !!name);
|
2022-01-13 00:59:38 +01:00
|
|
|
log.debug(`Player has Steam achievements ${JSON.stringify(playerAchieved)}`);
|
2021-12-29 14:46:56 +01:00
|
|
|
const intervalID = setInterval(async () => {
|
|
|
|
try {
|
|
|
|
const playerAchievements = await window.webContents.executeJavaScript("document.achievements");
|
|
|
|
for (const ach of playerAchievements) {
|
2022-01-12 19:34:17 +01:00
|
|
|
if (!steamAchievements.includes(ach)) continue; // Don't try activating achievements that don't exist Steam-side
|
2022-04-07 01:30:08 +02:00
|
|
|
if (playerAchieved.includes(ach)) continue; // Don't spam achievements that have already been recorded
|
2022-01-12 19:34:17 +01:00
|
|
|
log.info(`Granting Steam achievement ${ach}`);
|
2021-12-29 14:46:56 +01:00
|
|
|
greenworks.activateAchievement(ach, () => undefined);
|
2022-01-12 19:34:17 +01:00
|
|
|
playerAchieved.push(ach);
|
2021-12-29 14:46:56 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
log.error(error);
|
|
|
|
|
|
|
|
// The interval probably did not get cleared after a window kill
|
2022-04-07 01:30:08 +02:00
|
|
|
log.warn("Clearing achievements timer");
|
2021-12-29 14:46:56 +01:00
|
|
|
clearInterval(intervalID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
window.achievementsIntervalID = intervalID;
|
|
|
|
}
|
|
|
|
|
2022-01-12 19:57:45 +01:00
|
|
|
function checkSteamAchievement(name) {
|
|
|
|
return new Promise((resolve) => {
|
2022-04-07 01:30:08 +02:00
|
|
|
greenworks.getAchievement(
|
|
|
|
name,
|
|
|
|
(playerHas) => resolve(playerHas ? name : ""),
|
|
|
|
(err) => {
|
|
|
|
log.warn(`Failed to get Steam achievement ${name} status: ${err}`);
|
|
|
|
resolve("");
|
|
|
|
},
|
|
|
|
);
|
2022-01-12 19:57:45 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-29 14:46:56 +01:00
|
|
|
function disableAchievementsInterval(window) {
|
|
|
|
if (window.achievementsIntervalID) {
|
|
|
|
clearInterval(window.achievementsIntervalID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2022-04-07 01:30:08 +02:00
|
|
|
enableAchievementsInterval,
|
|
|
|
disableAchievementsInterval,
|
|
|
|
};
|