mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-19 09:13:57 +00:00
Merge branch 'main' into Fix/Datanodes
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { parseAchievementFile } from "./parse-achievement-file";
|
||||
import { Game } from "@main/entity";
|
||||
import { mergeAchievements } from "./merge-achievements";
|
||||
import fs, { readdirSync } from "node:fs";
|
||||
import {
|
||||
@@ -9,21 +7,20 @@ import {
|
||||
findAllAchievementFiles,
|
||||
getAlternativeObjectIds,
|
||||
} from "./find-achivement-files";
|
||||
import type { AchievementFile, UnlockedAchievement } from "@types";
|
||||
import type { AchievementFile, Game, UnlockedAchievement } from "@types";
|
||||
import { achievementsLogger } from "../logger";
|
||||
import { Cracker } from "@shared";
|
||||
import { IsNull, Not } from "typeorm";
|
||||
import { publishCombinedNewAchievementNotification } from "../notifications";
|
||||
import { gamesSublevel } from "@main/level";
|
||||
|
||||
const fileStats: Map<string, number> = new Map();
|
||||
const fltFiles: Map<string, Set<string>> = new Map();
|
||||
|
||||
const watchAchievementsWindows = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
},
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((games) => games.filter((game) => !game.isDeleted));
|
||||
|
||||
if (games.length === 0) return;
|
||||
|
||||
@@ -32,7 +29,7 @@ const watchAchievementsWindows = async () => {
|
||||
for (const game of games) {
|
||||
const gameAchievementFiles: AchievementFile[] = [];
|
||||
|
||||
for (const objectId of getAlternativeObjectIds(game.objectID)) {
|
||||
for (const objectId of getAlternativeObjectIds(game.objectId)) {
|
||||
gameAchievementFiles.push(...(achievementFiles.get(objectId) || []));
|
||||
|
||||
gameAchievementFiles.push(
|
||||
@@ -47,12 +44,12 @@ const watchAchievementsWindows = async () => {
|
||||
};
|
||||
|
||||
const watchAchievementsWithWine = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
winePrefixPath: Not(IsNull()),
|
||||
},
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((games) =>
|
||||
games.filter((game) => !game.isDeleted && game.winePrefixPath)
|
||||
);
|
||||
|
||||
for (const game of games) {
|
||||
const gameAchievementFiles = findAchievementFiles(game);
|
||||
@@ -144,7 +141,7 @@ const processAchievementFileDiff = async (
|
||||
export class AchievementWatcherManager {
|
||||
private static hasFinishedMergingWithRemote = false;
|
||||
|
||||
public static watchAchievements = () => {
|
||||
public static watchAchievements() {
|
||||
if (!this.hasFinishedMergingWithRemote) return;
|
||||
|
||||
if (process.platform === "win32") {
|
||||
@@ -152,12 +149,12 @@ export class AchievementWatcherManager {
|
||||
}
|
||||
|
||||
return watchAchievementsWithWine();
|
||||
};
|
||||
}
|
||||
|
||||
private static preProcessGameAchievementFiles = (
|
||||
private static preProcessGameAchievementFiles(
|
||||
game: Game,
|
||||
gameAchievementFiles: AchievementFile[]
|
||||
) => {
|
||||
) {
|
||||
const unlockedAchievements: UnlockedAchievement[] = [];
|
||||
for (const achievementFile of gameAchievementFiles) {
|
||||
const parsedAchievements = parseAchievementFile(
|
||||
@@ -185,14 +182,13 @@ export class AchievementWatcherManager {
|
||||
}
|
||||
|
||||
return mergeAchievements(game, unlockedAchievements, false);
|
||||
};
|
||||
}
|
||||
|
||||
private static preSearchAchievementsWindows = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
},
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((games) => games.filter((game) => !game.isDeleted));
|
||||
|
||||
const gameAchievementFilesMap = findAllAchievementFiles();
|
||||
|
||||
@@ -200,7 +196,7 @@ export class AchievementWatcherManager {
|
||||
games.map((game) => {
|
||||
const gameAchievementFiles: AchievementFile[] = [];
|
||||
|
||||
for (const objectId of getAlternativeObjectIds(game.objectID)) {
|
||||
for (const objectId of getAlternativeObjectIds(game.objectId)) {
|
||||
gameAchievementFiles.push(
|
||||
...(gameAchievementFilesMap.get(objectId) || [])
|
||||
);
|
||||
@@ -216,11 +212,10 @@ export class AchievementWatcherManager {
|
||||
};
|
||||
|
||||
private static preSearchAchievementsWithWine = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
},
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((games) => games.filter((game) => !game.isDeleted));
|
||||
|
||||
return Promise.all(
|
||||
games.map((game) => {
|
||||
@@ -235,7 +230,7 @@ export class AchievementWatcherManager {
|
||||
);
|
||||
};
|
||||
|
||||
public static preSearchAchievements = async () => {
|
||||
public static async preSearchAchievements() {
|
||||
try {
|
||||
const newAchievementsCount =
|
||||
process.platform === "win32"
|
||||
@@ -261,5 +256,5 @@ export class AchievementWatcherManager {
|
||||
}
|
||||
|
||||
this.hasFinishedMergingWithRemote = true;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import { app } from "electron";
|
||||
import type { AchievementFile } from "@types";
|
||||
import type { Game, AchievementFile } from "@types";
|
||||
import { Cracker } from "@shared";
|
||||
import { Game } from "@main/entity";
|
||||
import { achievementsLogger } from "../logger";
|
||||
|
||||
const getAppDataPath = () => {
|
||||
@@ -254,7 +253,7 @@ export const findAchievementFiles = (game: Game) => {
|
||||
|
||||
for (const cracker of crackers) {
|
||||
for (const { folderPath, fileLocation } of getPathFromCracker(cracker)) {
|
||||
for (const objectId of getAlternativeObjectIds(game.objectID)) {
|
||||
for (const objectId of getAlternativeObjectIds(game.objectId)) {
|
||||
const filePath = path.join(
|
||||
game.winePrefixPath ?? "",
|
||||
folderPath,
|
||||
|
||||
@@ -1,40 +1,37 @@
|
||||
import {
|
||||
gameAchievementRepository,
|
||||
userPreferencesRepository,
|
||||
} from "@main/repository";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import type { AchievementData, GameShop } from "@types";
|
||||
import type { GameShop, SteamAchievement } from "@types";
|
||||
import { UserNotLoggedInError } from "@shared";
|
||||
import { logger } from "../logger";
|
||||
import { GameAchievement } from "@main/entity";
|
||||
import { db, gameAchievementsSublevel, levelKeys } from "@main/level";
|
||||
|
||||
export const getGameAchievementData = async (
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
cachedAchievements: GameAchievement | null
|
||||
useCachedData: boolean
|
||||
) => {
|
||||
if (cachedAchievements && cachedAchievements.achievements) {
|
||||
return JSON.parse(cachedAchievements.achievements) as AchievementData[];
|
||||
}
|
||||
const cachedAchievements = await gameAchievementsSublevel.get(
|
||||
levelKeys.game(shop, objectId)
|
||||
);
|
||||
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
if (cachedAchievements && useCachedData)
|
||||
return cachedAchievements.achievements;
|
||||
|
||||
return HydraApi.get<AchievementData[]>("/games/achievements", {
|
||||
const language = await db
|
||||
.get<string, string>(levelKeys.language, {
|
||||
valueEncoding: "utf-8",
|
||||
})
|
||||
.then((language) => language || "en");
|
||||
|
||||
return HydraApi.get<SteamAchievement[]>("/games/achievements", {
|
||||
shop,
|
||||
objectId,
|
||||
language: userPreferences?.language || "en",
|
||||
language,
|
||||
})
|
||||
.then((achievements) => {
|
||||
gameAchievementRepository.upsert(
|
||||
{
|
||||
objectId,
|
||||
shop,
|
||||
achievements: JSON.stringify(achievements),
|
||||
},
|
||||
["objectId", "shop"]
|
||||
);
|
||||
.then(async (achievements) => {
|
||||
await gameAchievementsSublevel.put(levelKeys.game(shop, objectId), {
|
||||
unlockedAchievements: cachedAchievements?.unlockedAchievements ?? [],
|
||||
achievements,
|
||||
});
|
||||
|
||||
return achievements;
|
||||
})
|
||||
@@ -42,15 +39,9 @@ export const getGameAchievementData = async (
|
||||
if (err instanceof UserNotLoggedInError) {
|
||||
throw err;
|
||||
}
|
||||
logger.error("Failed to get game achievements", err);
|
||||
return gameAchievementRepository
|
||||
.findOne({
|
||||
where: { objectId, shop },
|
||||
})
|
||||
.then((gameAchievements) => {
|
||||
return JSON.parse(
|
||||
gameAchievements?.achievements || "[]"
|
||||
) as AchievementData[];
|
||||
});
|
||||
|
||||
logger.error("Failed to get game achievements for", objectId, err);
|
||||
|
||||
return [];
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,32 +1,33 @@
|
||||
import {
|
||||
gameAchievementRepository,
|
||||
userPreferencesRepository,
|
||||
} from "@main/repository";
|
||||
import type { AchievementData, GameShop, UnlockedAchievement } from "@types";
|
||||
import type {
|
||||
Game,
|
||||
GameShop,
|
||||
UnlockedAchievement,
|
||||
UserPreferences,
|
||||
} from "@types";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
|
||||
import { Game } from "@main/entity";
|
||||
import { publishNewAchievementNotification } from "../notifications";
|
||||
import { SubscriptionRequiredError } from "@shared";
|
||||
import { achievementsLogger } from "../logger";
|
||||
import { db, gameAchievementsSublevel, levelKeys } from "@main/level";
|
||||
|
||||
const saveAchievementsOnLocal = async (
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
achievements: UnlockedAchievement[],
|
||||
unlockedAchievements: UnlockedAchievement[],
|
||||
sendUpdateEvent: boolean
|
||||
) => {
|
||||
return gameAchievementRepository
|
||||
.upsert(
|
||||
{
|
||||
objectId,
|
||||
shop,
|
||||
unlockedAchievements: JSON.stringify(achievements),
|
||||
},
|
||||
["objectId", "shop"]
|
||||
)
|
||||
.then(() => {
|
||||
const levelKey = levelKeys.game(shop, objectId);
|
||||
|
||||
return gameAchievementsSublevel
|
||||
.get(levelKey)
|
||||
.then(async (gameAchievement) => {
|
||||
await gameAchievementsSublevel.put(levelKey, {
|
||||
achievements: gameAchievement?.achievements ?? [],
|
||||
unlockedAchievements: unlockedAchievements,
|
||||
});
|
||||
|
||||
if (!sendUpdateEvent) return;
|
||||
|
||||
return getUnlockedAchievements(objectId, shop, true)
|
||||
@@ -46,25 +47,17 @@ export const mergeAchievements = async (
|
||||
publishNotification: boolean
|
||||
) => {
|
||||
const [localGameAchievement, userPreferences] = await Promise.all([
|
||||
gameAchievementRepository.findOne({
|
||||
where: {
|
||||
objectId: game.objectID,
|
||||
shop: game.shop,
|
||||
},
|
||||
gameAchievementsSublevel.get(levelKeys.game(game.shop, game.objectId)),
|
||||
db.get<string, UserPreferences>(levelKeys.userPreferences, {
|
||||
valueEncoding: "json",
|
||||
}),
|
||||
userPreferencesRepository.findOne({ where: { id: 1 } }),
|
||||
]);
|
||||
|
||||
const achievementsData = JSON.parse(
|
||||
localGameAchievement?.achievements || "[]"
|
||||
) as AchievementData[];
|
||||
|
||||
const unlockedAchievements = JSON.parse(
|
||||
localGameAchievement?.unlockedAchievements || "[]"
|
||||
).filter((achievement) => achievement.name) as UnlockedAchievement[];
|
||||
const achievementsData = localGameAchievement?.achievements ?? [];
|
||||
const unlockedAchievements = localGameAchievement?.unlockedAchievements ?? [];
|
||||
|
||||
const newAchievementsMap = new Map(
|
||||
achievements.reverse().map((achievement) => {
|
||||
achievements.toReversed().map((achievement) => {
|
||||
return [achievement.name.toUpperCase(), achievement];
|
||||
})
|
||||
);
|
||||
@@ -92,7 +85,7 @@ export const mergeAchievements = async (
|
||||
userPreferences?.achievementNotificationsEnabled
|
||||
) {
|
||||
const achievementsInfo = newAchievements
|
||||
.sort((a, b) => {
|
||||
.toSorted((a, b) => {
|
||||
return a.unlockTime - b.unlockTime;
|
||||
})
|
||||
.map((achievement) => {
|
||||
@@ -138,16 +131,16 @@ export const mergeAchievements = async (
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err! instanceof SubscriptionRequiredError) {
|
||||
if (err instanceof SubscriptionRequiredError) {
|
||||
achievementsLogger.log(
|
||||
"Achievements not synchronized on API due to lack of subscription",
|
||||
game.objectID,
|
||||
game.objectId,
|
||||
game.title
|
||||
);
|
||||
}
|
||||
|
||||
return saveAchievementsOnLocal(
|
||||
game.objectID,
|
||||
game.objectId,
|
||||
game.shop,
|
||||
mergedLocalAchievements,
|
||||
publishNotification
|
||||
@@ -155,7 +148,7 @@ export const mergeAchievements = async (
|
||||
});
|
||||
} else {
|
||||
await saveAchievementsOnLocal(
|
||||
game.objectID,
|
||||
game.objectId,
|
||||
game.shop,
|
||||
mergedLocalAchievements,
|
||||
publishNotification
|
||||
|
||||
@@ -4,10 +4,9 @@ import {
|
||||
} from "./find-achivement-files";
|
||||
import { parseAchievementFile } from "./parse-achievement-file";
|
||||
import { mergeAchievements } from "./merge-achievements";
|
||||
import type { UnlockedAchievement } from "@types";
|
||||
import { Game } from "@main/entity";
|
||||
import type { Game, UnlockedAchievement } from "@types";
|
||||
|
||||
export const updateLocalUnlockedAchivements = async (game: Game) => {
|
||||
export const updateLocalUnlockedAchievements = async (game: Game) => {
|
||||
const gameAchievementFiles = findAchievementFiles(game);
|
||||
|
||||
const achievementFileInsideDirectory =
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
import { Game } from "@main/entity";
|
||||
import { Downloader } from "@shared";
|
||||
import { Downloader, DownloadError } from "@shared";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import {
|
||||
downloadQueueRepository,
|
||||
gameRepository,
|
||||
userPreferencesRepository,
|
||||
} from "@main/repository";
|
||||
import { publishDownloadCompleteNotification } from "../notifications";
|
||||
import type { DownloadProgress } from "@types";
|
||||
import { GofileApi, QiwiApi, DatanodesApi } from "../hosters";
|
||||
import type { Download, DownloadProgress, UserPreferences } from "@types";
|
||||
import { GofileApi, QiwiApi, DatanodesApi, MediafireApi } from "../hosters";
|
||||
import { PythonRPC } from "../python-rpc";
|
||||
import {
|
||||
LibtorrentPayload,
|
||||
@@ -16,37 +10,45 @@ import {
|
||||
PauseDownloadPayload,
|
||||
} from "./types";
|
||||
import { calculateETA, getDirSize } from "./helpers";
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
||||
import { RealDebridClient } from "./real-debrid";
|
||||
import path from "path";
|
||||
import { logger } from "../logger";
|
||||
import { db, downloadsSublevel, gamesSublevel, levelKeys } from "@main/level";
|
||||
import { sortBy } from "lodash-es";
|
||||
import { TorBoxClient } from "./torbox";
|
||||
|
||||
export class DownloadManager {
|
||||
private static downloadingGameId: number | null = null;
|
||||
private static downloadingGameId: string | null = null;
|
||||
|
||||
public static async startRPC(game?: Game, initialSeeding?: Game[]) {
|
||||
public static async startRPC(
|
||||
download?: Download,
|
||||
downloadsToSeed?: Download[]
|
||||
) {
|
||||
PythonRPC.spawn(
|
||||
game?.status === "active"
|
||||
? await this.getDownloadPayload(game).catch(() => undefined)
|
||||
download?.status === "active"
|
||||
? await this.getDownloadPayload(download).catch((err) => {
|
||||
logger.error("Error getting download payload", err);
|
||||
return undefined;
|
||||
})
|
||||
: undefined,
|
||||
initialSeeding?.map((game) => ({
|
||||
game_id: game.id,
|
||||
url: game.uri!,
|
||||
save_path: game.downloadPath!,
|
||||
downloadsToSeed?.map((download) => ({
|
||||
game_id: levelKeys.game(download.shop, download.objectId),
|
||||
url: download.uri,
|
||||
save_path: download.downloadPath,
|
||||
}))
|
||||
);
|
||||
|
||||
this.downloadingGameId = game?.id ?? null;
|
||||
if (download) {
|
||||
this.downloadingGameId = levelKeys.game(download.shop, download.objectId);
|
||||
}
|
||||
}
|
||||
|
||||
private static async getDownloadStatus() {
|
||||
const response = await PythonRPC.rpc.get<LibtorrentPayload | null>(
|
||||
"/status"
|
||||
);
|
||||
|
||||
if (response.data === null || !this.downloadingGameId) return null;
|
||||
|
||||
const gameId = this.downloadingGameId;
|
||||
const downloadId = this.downloadingGameId;
|
||||
|
||||
try {
|
||||
const {
|
||||
@@ -62,24 +64,21 @@ export class DownloadManager {
|
||||
|
||||
const isDownloadingMetadata =
|
||||
status === LibtorrentStatus.DownloadingMetadata;
|
||||
|
||||
const isCheckingFiles = status === LibtorrentStatus.CheckingFiles;
|
||||
|
||||
const download = await downloadsSublevel.get(downloadId);
|
||||
|
||||
if (!isDownloadingMetadata && !isCheckingFiles) {
|
||||
const update: QueryDeepPartialEntity<Game> = {
|
||||
if (!download) return null;
|
||||
|
||||
await downloadsSublevel.put(downloadId, {
|
||||
...download,
|
||||
bytesDownloaded,
|
||||
fileSize,
|
||||
progress,
|
||||
folderName,
|
||||
status: "active",
|
||||
};
|
||||
|
||||
await gameRepository.update(
|
||||
{ id: gameId },
|
||||
{
|
||||
...update,
|
||||
folderName,
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -90,7 +89,8 @@ export class DownloadManager {
|
||||
isDownloadingMetadata,
|
||||
isCheckingFiles,
|
||||
progress,
|
||||
gameId,
|
||||
gameId: downloadId,
|
||||
download,
|
||||
} as DownloadProgress;
|
||||
} catch (err) {
|
||||
return null;
|
||||
@@ -102,14 +102,22 @@ export class DownloadManager {
|
||||
|
||||
if (status) {
|
||||
const { gameId, progress } = status;
|
||||
const game = await gameRepository.findOne({
|
||||
where: { id: gameId, isDeleted: false },
|
||||
});
|
||||
const userPreferences = await userPreferencesRepository.findOneBy({
|
||||
id: 1,
|
||||
});
|
||||
|
||||
if (WindowManager.mainWindow && game) {
|
||||
const [download, game] = await Promise.all([
|
||||
downloadsSublevel.get(gameId),
|
||||
gamesSublevel.get(gameId),
|
||||
]);
|
||||
|
||||
if (!download || !game) return;
|
||||
|
||||
const userPreferences = await db.get<string, UserPreferences | null>(
|
||||
levelKeys.userPreferences,
|
||||
{
|
||||
valueEncoding: "json",
|
||||
}
|
||||
);
|
||||
|
||||
if (WindowManager.mainWindow && download) {
|
||||
WindowManager.mainWindow.setProgressBar(progress === 1 ? -1 : progress);
|
||||
WindowManager.mainWindow.webContents.send(
|
||||
"on-download-progress",
|
||||
@@ -121,39 +129,48 @@ export class DownloadManager {
|
||||
)
|
||||
);
|
||||
}
|
||||
if (progress === 1 && game) {
|
||||
|
||||
if (progress === 1 && download) {
|
||||
publishDownloadCompleteNotification(game);
|
||||
|
||||
if (
|
||||
userPreferences?.seedAfterDownloadComplete &&
|
||||
game.downloader === Downloader.Torrent
|
||||
download.downloader === Downloader.Torrent
|
||||
) {
|
||||
gameRepository.update(
|
||||
{ id: gameId },
|
||||
{ status: "seeding", shouldSeed: true }
|
||||
);
|
||||
downloadsSublevel.put(gameId, {
|
||||
...download,
|
||||
status: "seeding",
|
||||
shouldSeed: true,
|
||||
queued: false,
|
||||
});
|
||||
} else {
|
||||
gameRepository.update(
|
||||
{ id: gameId },
|
||||
{ status: "complete", shouldSeed: false }
|
||||
);
|
||||
downloadsSublevel.put(gameId, {
|
||||
...download,
|
||||
status: "complete",
|
||||
shouldSeed: false,
|
||||
queued: false,
|
||||
});
|
||||
|
||||
this.cancelDownload(gameId);
|
||||
}
|
||||
|
||||
await downloadQueueRepository.delete({ game });
|
||||
const [nextQueueItem] = await downloadQueueRepository.find({
|
||||
order: {
|
||||
id: "DESC",
|
||||
},
|
||||
relations: {
|
||||
game: true,
|
||||
},
|
||||
});
|
||||
if (nextQueueItem) {
|
||||
this.resumeDownload(nextQueueItem.game);
|
||||
const downloads = await downloadsSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((games) => {
|
||||
return sortBy(
|
||||
games.filter((game) => game.status === "paused" && game.queued),
|
||||
"timestamp",
|
||||
"DESC"
|
||||
);
|
||||
});
|
||||
|
||||
const [nextItemOnQueue] = downloads;
|
||||
|
||||
if (nextItemOnQueue) {
|
||||
this.resumeDownload(nextItemOnQueue);
|
||||
} else {
|
||||
this.downloadingGameId = -1;
|
||||
this.downloadingGameId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,20 +186,19 @@ export class DownloadManager {
|
||||
logger.log(seedStatus);
|
||||
|
||||
seedStatus.forEach(async (status) => {
|
||||
const game = await gameRepository.findOne({
|
||||
where: { id: status.gameId },
|
||||
});
|
||||
const download = await downloadsSublevel.get(status.gameId);
|
||||
|
||||
if (!game) return;
|
||||
if (!download) return;
|
||||
|
||||
const totalSize = await getDirSize(
|
||||
path.join(game.downloadPath!, status.folderName)
|
||||
path.join(download.downloadPath, status.folderName)
|
||||
);
|
||||
|
||||
if (totalSize < status.fileSize) {
|
||||
await this.cancelDownload(game.id);
|
||||
await this.cancelDownload(status.gameId);
|
||||
|
||||
await gameRepository.update(game.id, {
|
||||
await downloadsSublevel.put(status.gameId, {
|
||||
...download,
|
||||
status: "paused",
|
||||
shouldSeed: false,
|
||||
progress: totalSize / status.fileSize,
|
||||
@@ -195,123 +211,151 @@ export class DownloadManager {
|
||||
WindowManager.mainWindow?.webContents.send("on-seeding-status", seedStatus);
|
||||
}
|
||||
|
||||
static async pauseDownload() {
|
||||
static async pauseDownload(downloadKey = this.downloadingGameId) {
|
||||
await PythonRPC.rpc
|
||||
.post("/action", {
|
||||
action: "pause",
|
||||
game_id: this.downloadingGameId,
|
||||
game_id: downloadKey,
|
||||
} as PauseDownloadPayload)
|
||||
.catch(() => {});
|
||||
|
||||
WindowManager.mainWindow?.setProgressBar(-1);
|
||||
|
||||
this.downloadingGameId = null;
|
||||
}
|
||||
|
||||
static async resumeDownload(game: Game) {
|
||||
return this.startDownload(game);
|
||||
}
|
||||
|
||||
static async cancelDownload(gameId = this.downloadingGameId!) {
|
||||
await PythonRPC.rpc.post("/action", {
|
||||
action: "cancel",
|
||||
game_id: gameId,
|
||||
});
|
||||
|
||||
WindowManager.mainWindow?.setProgressBar(-1);
|
||||
|
||||
if (gameId === this.downloadingGameId) {
|
||||
if (downloadKey === this.downloadingGameId) {
|
||||
WindowManager.mainWindow?.setProgressBar(-1);
|
||||
this.downloadingGameId = null;
|
||||
}
|
||||
}
|
||||
|
||||
static async resumeSeeding(game: Game) {
|
||||
static async resumeDownload(download: Download) {
|
||||
return this.startDownload(download);
|
||||
}
|
||||
|
||||
static async cancelDownload(downloadKey = this.downloadingGameId) {
|
||||
await PythonRPC.rpc
|
||||
.post("/action", {
|
||||
action: "cancel",
|
||||
game_id: downloadKey,
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error("Failed to cancel game download", err);
|
||||
});
|
||||
|
||||
if (downloadKey === this.downloadingGameId) {
|
||||
WindowManager.mainWindow?.setProgressBar(-1);
|
||||
WindowManager.mainWindow?.webContents.send("on-download-progress", null);
|
||||
this.downloadingGameId = null;
|
||||
}
|
||||
}
|
||||
|
||||
static async resumeSeeding(download: Download) {
|
||||
await PythonRPC.rpc.post("/action", {
|
||||
action: "resume_seeding",
|
||||
game_id: game.id,
|
||||
url: game.uri,
|
||||
save_path: game.downloadPath,
|
||||
game_id: levelKeys.game(download.shop, download.objectId),
|
||||
url: download.uri,
|
||||
save_path: download.downloadPath,
|
||||
});
|
||||
}
|
||||
|
||||
static async pauseSeeding(gameId: number) {
|
||||
static async pauseSeeding(downloadKey: string) {
|
||||
await PythonRPC.rpc.post("/action", {
|
||||
action: "pause_seeding",
|
||||
game_id: gameId,
|
||||
game_id: downloadKey,
|
||||
});
|
||||
}
|
||||
|
||||
private static async getDownloadPayload(game: Game) {
|
||||
switch (game.downloader) {
|
||||
case Downloader.Gofile: {
|
||||
const id = game.uri!.split("/").pop();
|
||||
private static async getDownloadPayload(download: Download) {
|
||||
const downloadId = levelKeys.game(download.shop, download.objectId);
|
||||
|
||||
switch (download.downloader) {
|
||||
case Downloader.Gofile: {
|
||||
const id = download.uri.split("/").pop();
|
||||
const token = await GofileApi.authorize();
|
||||
const downloadLink = await GofileApi.getDownloadLink(id!);
|
||||
|
||||
await GofileApi.checkDownloadUrl(downloadLink);
|
||||
|
||||
return {
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
game_id: downloadId,
|
||||
url: downloadLink,
|
||||
save_path: game.downloadPath!,
|
||||
save_path: download.downloadPath,
|
||||
header: `Cookie: accountToken=${token}`,
|
||||
};
|
||||
}
|
||||
case Downloader.PixelDrain: {
|
||||
const id = game.uri!.split("/").pop();
|
||||
const id = download.uri.split("/").pop();
|
||||
|
||||
return {
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
url: `https://pixeldrain.com/api/file/${id}?download`,
|
||||
save_path: game.downloadPath!,
|
||||
game_id: downloadId,
|
||||
url: `https://cdn.pd5-gamedriveorg.workers.dev/api/file/${id}`,
|
||||
save_path: download.downloadPath,
|
||||
};
|
||||
}
|
||||
case Downloader.Qiwi: {
|
||||
const downloadUrl = await QiwiApi.getDownloadUrl(game.uri!);
|
||||
|
||||
const downloadUrl = await QiwiApi.getDownloadUrl(download.uri);
|
||||
return {
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
game_id: downloadId,
|
||||
url: downloadUrl,
|
||||
save_path: game.downloadPath!,
|
||||
save_path: download.downloadPath,
|
||||
};
|
||||
}
|
||||
case Downloader.Datanodes: {
|
||||
const downloadUrl = await DatanodesApi.getDownloadUrl(game.uri!);
|
||||
const downloadUrl = await DatanodesApi.getDownloadUrl(download.uri);
|
||||
return {
|
||||
action: "start",
|
||||
game_id: downloadId,
|
||||
url: downloadUrl,
|
||||
save_path: download.downloadPath,
|
||||
};
|
||||
}
|
||||
case Downloader.Mediafire: {
|
||||
const downloadUrl = await MediafireApi.getDownloadUrl(download.uri);
|
||||
|
||||
return {
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
game_id: downloadId,
|
||||
url: downloadUrl,
|
||||
save_path: game.downloadPath!,
|
||||
save_path: download.downloadPath,
|
||||
};
|
||||
}
|
||||
case Downloader.Torrent:
|
||||
return {
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
url: game.uri!,
|
||||
save_path: game.downloadPath!,
|
||||
game_id: downloadId,
|
||||
url: download.uri,
|
||||
save_path: download.downloadPath,
|
||||
};
|
||||
case Downloader.RealDebrid: {
|
||||
const downloadUrl = await RealDebridClient.getDownloadUrl(game.uri!);
|
||||
const downloadUrl = await RealDebridClient.getDownloadUrl(download.uri);
|
||||
|
||||
if (!downloadUrl) throw new Error(DownloadError.NotCachedInRealDebrid);
|
||||
|
||||
return {
|
||||
action: "start",
|
||||
game_id: game.id,
|
||||
url: downloadUrl!,
|
||||
save_path: game.downloadPath!,
|
||||
game_id: downloadId,
|
||||
url: downloadUrl,
|
||||
save_path: download.downloadPath,
|
||||
};
|
||||
}
|
||||
case Downloader.TorBox: {
|
||||
const { name, url } = await TorBoxClient.getDownloadInfo(download.uri);
|
||||
|
||||
if (!url) return;
|
||||
return {
|
||||
action: "start",
|
||||
game_id: downloadId,
|
||||
url,
|
||||
save_path: download.downloadPath,
|
||||
out: name,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async startDownload(game: Game) {
|
||||
const payload = await this.getDownloadPayload(game);
|
||||
|
||||
static async startDownload(download: Download) {
|
||||
const payload = await this.getDownloadPayload(download);
|
||||
await PythonRPC.rpc.post("/action", payload);
|
||||
|
||||
this.downloadingGameId = game.id;
|
||||
this.downloadingGameId = levelKeys.game(download.shop, download.objectId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,24 +6,23 @@ import type {
|
||||
TorBoxAddTorrentRequest,
|
||||
TorBoxRequestLinkRequest,
|
||||
} from "@types";
|
||||
import { logger } from "../logger";
|
||||
|
||||
export class TorBoxClient {
|
||||
private static instance: AxiosInstance;
|
||||
private static readonly baseURL = "https://api.torbox.app/v1/api";
|
||||
public static apiToken: string;
|
||||
private static apiToken: string;
|
||||
|
||||
static authorize(apiToken: string) {
|
||||
this.apiToken = apiToken;
|
||||
this.instance = axios.create({
|
||||
baseURL: this.baseURL,
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiToken}`,
|
||||
},
|
||||
});
|
||||
this.apiToken = apiToken;
|
||||
}
|
||||
|
||||
static async addMagnet(magnet: string) {
|
||||
private static async addMagnet(magnet: string) {
|
||||
const form = new FormData();
|
||||
form.append("magnet", magnet);
|
||||
|
||||
@@ -32,6 +31,10 @@ export class TorBoxClient {
|
||||
form
|
||||
);
|
||||
|
||||
if (!response.data.success) {
|
||||
throw new Error(response.data.detail);
|
||||
}
|
||||
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
@@ -55,22 +58,16 @@ export class TorBoxClient {
|
||||
}
|
||||
|
||||
static async requestLink(id: number) {
|
||||
const searchParams = new URLSearchParams({});
|
||||
|
||||
searchParams.set("token", this.apiToken);
|
||||
searchParams.set("torrent_id", id.toString());
|
||||
searchParams.set("zip_link", "true");
|
||||
const searchParams = new URLSearchParams({
|
||||
token: this.apiToken,
|
||||
torrent_id: id.toString(),
|
||||
zip_link: "true",
|
||||
});
|
||||
|
||||
const response = await this.instance.get<TorBoxRequestLinkRequest>(
|
||||
"/torrents/requestdl?" + searchParams.toString()
|
||||
);
|
||||
|
||||
if (response.status !== 200) {
|
||||
logger.error(response.data.error);
|
||||
logger.error(response.data.detail);
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
@@ -81,7 +78,7 @@ export class TorBoxClient {
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
static async getTorrentId(magnetUri: string) {
|
||||
private static async getTorrentIdAndName(magnetUri: string) {
|
||||
const userTorrents = await this.getAllTorrentsFromUser();
|
||||
|
||||
const { infoHash } = await parseTorrent(magnetUri);
|
||||
@@ -89,9 +86,18 @@ export class TorBoxClient {
|
||||
(userTorrent) => userTorrent.hash === infoHash
|
||||
);
|
||||
|
||||
if (userTorrent) return userTorrent.id;
|
||||
if (userTorrent) return { id: userTorrent.id, name: userTorrent.name };
|
||||
|
||||
const torrent = await this.addMagnet(magnetUri);
|
||||
return torrent.torrent_id;
|
||||
return { id: torrent.torrent_id, name: torrent.name };
|
||||
}
|
||||
|
||||
static async getDownloadInfo(uri: string) {
|
||||
const torrentData = await this.getTorrentIdAndName(uri);
|
||||
const url = await this.requestLink(torrentData.id);
|
||||
|
||||
const name = torrentData.name ? `${torrentData.name}.zip` : undefined;
|
||||
|
||||
return { url, name };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export interface PauseDownloadPayload {
|
||||
game_id: number;
|
||||
game_id: string;
|
||||
}
|
||||
|
||||
export interface CancelDownloadPayload {
|
||||
game_id: number;
|
||||
game_id: string;
|
||||
}
|
||||
|
||||
export enum LibtorrentStatus {
|
||||
@@ -24,7 +24,7 @@ export interface LibtorrentPayload {
|
||||
fileSize: number;
|
||||
folderName: string;
|
||||
status: LibtorrentStatus;
|
||||
gameId: number;
|
||||
gameId: string;
|
||||
}
|
||||
|
||||
export interface ProcessPayload {
|
||||
|
||||
@@ -60,4 +60,12 @@ export class GofileApi {
|
||||
|
||||
throw new Error("Failed to get download link");
|
||||
}
|
||||
|
||||
public static async checkDownloadUrl(url: string) {
|
||||
return axios.head(url, {
|
||||
headers: {
|
||||
Cookie: `accountToken=${this.token}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./gofile";
|
||||
export * from "./qiwi";
|
||||
export * from "./datanodes";
|
||||
export * from "./mediafire";
|
||||
|
||||
54
src/main/services/hosters/mediafire.ts
Normal file
54
src/main/services/hosters/mediafire.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import fetch from "node-fetch";
|
||||
|
||||
export class MediafireApi {
|
||||
private static readonly validMediafireIdentifierDL = /^[a-zA-Z0-9]+$/m;
|
||||
private static readonly validMediafirePreDL =
|
||||
/(?<=['"])(https?:)?(\/\/)?(www\.)?mediafire\.com\/(file|view|download)\/[^'"?]+\?dkey=[^'"]+(?=['"])/;
|
||||
private static readonly validDynamicDL =
|
||||
/(?<=['"])https?:\/\/download\d+\.mediafire\.com\/[^'"]+(?=['"])/;
|
||||
private static readonly checkHTTP = /^https?:\/\//m;
|
||||
|
||||
public static async getDownloadUrl(mediafireUrl: string): Promise<string> {
|
||||
try {
|
||||
const processedUrl = this.processUrl(mediafireUrl);
|
||||
const response = await fetch(processedUrl);
|
||||
|
||||
if (!response.ok) throw new Error("Failed to fetch Mediafire page");
|
||||
|
||||
const html = await response.text();
|
||||
return this.extractDirectUrl(html);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to get download URL`);
|
||||
}
|
||||
}
|
||||
|
||||
private static processUrl(url: string): string {
|
||||
let processed = url.replace("http://", "https://");
|
||||
|
||||
if (this.validMediafireIdentifierDL.test(processed)) {
|
||||
processed = `https://mediafire.com/?${processed}`;
|
||||
}
|
||||
|
||||
if (!this.checkHTTP.test(processed)) {
|
||||
processed = processed.startsWith("//")
|
||||
? `https:${processed}`
|
||||
: `https://${processed}`;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
private static extractDirectUrl(html: string): string {
|
||||
const preMatch = this.validMediafirePreDL.exec(html);
|
||||
if (preMatch?.[0]) {
|
||||
return preMatch[0];
|
||||
}
|
||||
|
||||
const dlMatch = this.validDynamicDL.exec(html);
|
||||
if (dlMatch?.[0]) {
|
||||
return dlMatch[0];
|
||||
}
|
||||
|
||||
throw new Error("No valid download links found");
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
import {
|
||||
userAuthRepository,
|
||||
userSubscriptionRepository,
|
||||
} from "@main/repository";
|
||||
import axios, { AxiosError, AxiosInstance } from "axios";
|
||||
import { WindowManager } from "./window-manager";
|
||||
import url from "url";
|
||||
import { uploadGamesBatch } from "./library-sync";
|
||||
import { clearGamesRemoteIds } from "./library-sync/clear-games-remote-id";
|
||||
import { logger } from "./logger";
|
||||
import { networkLogger as logger } from "./logger";
|
||||
import { UserNotLoggedInError, SubscriptionRequiredError } from "@shared";
|
||||
import { omit } from "lodash-es";
|
||||
import { appVersion } from "@main/constants";
|
||||
import { getUserData } from "./user/get-user-data";
|
||||
import { isFuture, isToday } from "date-fns";
|
||||
import { db } from "@main/level";
|
||||
import { levelKeys } from "@main/level/sublevels";
|
||||
import type { Auth, User } from "@types";
|
||||
|
||||
interface HydraApiOptions {
|
||||
needsAuth?: boolean;
|
||||
@@ -32,7 +31,9 @@ export class HydraApi {
|
||||
private static readonly EXPIRATION_OFFSET_IN_MS = 1000 * 60 * 5; // 5 minutes
|
||||
private static readonly ADD_LOG_INTERCEPTOR = true;
|
||||
|
||||
private static secondsToMilliseconds = (seconds: number) => seconds * 1000;
|
||||
private static secondsToMilliseconds(seconds: number) {
|
||||
return seconds * 1000;
|
||||
}
|
||||
|
||||
private static userAuth: HydraApiUserAuth = {
|
||||
authToken: "",
|
||||
@@ -77,14 +78,14 @@ export class HydraApi {
|
||||
tokenExpirationTimestamp
|
||||
);
|
||||
|
||||
await userAuthRepository.upsert(
|
||||
db.put<string, Auth>(
|
||||
levelKeys.auth,
|
||||
{
|
||||
id: 1,
|
||||
accessToken,
|
||||
tokenExpirationTimestamp,
|
||||
refreshToken,
|
||||
tokenExpirationTimestamp,
|
||||
},
|
||||
["id"]
|
||||
{ valueEncoding: "json" }
|
||||
);
|
||||
|
||||
await getUserData().then((userDetails) => {
|
||||
@@ -153,7 +154,8 @@ export class HydraApi {
|
||||
(error) => {
|
||||
logger.error(" ---- RESPONSE ERROR -----");
|
||||
const { config } = error;
|
||||
const data = JSON.parse(config.data);
|
||||
|
||||
const data = JSON.parse(config.data ?? null);
|
||||
|
||||
logger.error(
|
||||
config.method,
|
||||
@@ -174,29 +176,39 @@ export class HydraApi {
|
||||
error.response.status,
|
||||
error.response.data
|
||||
);
|
||||
} else if (error.request) {
|
||||
const errorData = error.toJSON();
|
||||
logger.error("Request error:", errorData.message);
|
||||
} else {
|
||||
logger.error("Error", error.message);
|
||||
|
||||
return Promise.reject(error as Error);
|
||||
}
|
||||
logger.error(" ----- END RESPONSE ERROR -------");
|
||||
return Promise.reject(error);
|
||||
|
||||
if (error.request) {
|
||||
const errorData = error.toJSON();
|
||||
logger.error("Request error:", errorData.code, errorData.message);
|
||||
return Promise.reject(
|
||||
new Error(
|
||||
`Request failed with ${errorData.code} ${errorData.message}`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
logger.error("Error", error.message);
|
||||
return Promise.reject(error as Error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const userAuth = await userAuthRepository.findOne({
|
||||
where: { id: 1 },
|
||||
relations: { subscription: true },
|
||||
const result = await db.getMany<string>([levelKeys.auth, levelKeys.user], {
|
||||
valueEncoding: "json",
|
||||
});
|
||||
|
||||
const userAuth = result.at(0) as Auth | undefined;
|
||||
const user = result.at(1) as User | undefined;
|
||||
|
||||
this.userAuth = {
|
||||
authToken: userAuth?.accessToken ?? "",
|
||||
refreshToken: userAuth?.refreshToken ?? "",
|
||||
expirationTimestamp: userAuth?.tokenExpirationTimestamp ?? 0,
|
||||
subscription: userAuth?.subscription
|
||||
? { expiresAt: userAuth.subscription?.expiresAt }
|
||||
subscription: user?.subscription
|
||||
? { expiresAt: user.subscription?.expiresAt }
|
||||
: null,
|
||||
};
|
||||
|
||||
@@ -215,38 +227,47 @@ export class HydraApi {
|
||||
}
|
||||
}
|
||||
|
||||
private static async revalidateAccessTokenIfExpired() {
|
||||
const now = new Date();
|
||||
public static async refreshToken() {
|
||||
const response = await this.instance.post(`/auth/refresh`, {
|
||||
refreshToken: this.userAuth.refreshToken,
|
||||
});
|
||||
|
||||
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
||||
try {
|
||||
const response = await this.instance.post(`/auth/refresh`, {
|
||||
refreshToken: this.userAuth.refreshToken,
|
||||
});
|
||||
const { accessToken, expiresIn } = response.data;
|
||||
|
||||
const { accessToken, expiresIn } = response.data;
|
||||
const tokenExpirationTimestamp =
|
||||
Date.now() +
|
||||
this.secondsToMilliseconds(expiresIn) -
|
||||
this.EXPIRATION_OFFSET_IN_MS;
|
||||
|
||||
const tokenExpirationTimestamp =
|
||||
now.getTime() +
|
||||
this.secondsToMilliseconds(expiresIn) -
|
||||
this.EXPIRATION_OFFSET_IN_MS;
|
||||
this.userAuth.authToken = accessToken;
|
||||
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
|
||||
|
||||
this.userAuth.authToken = accessToken;
|
||||
this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
|
||||
logger.log(
|
||||
"Token refreshed. New expiration:",
|
||||
this.userAuth.expirationTimestamp
|
||||
);
|
||||
|
||||
logger.log(
|
||||
"Token refreshed. New expiration:",
|
||||
this.userAuth.expirationTimestamp
|
||||
);
|
||||
|
||||
userAuthRepository.upsert(
|
||||
await db
|
||||
.get<string, Auth>(levelKeys.auth, { valueEncoding: "json" })
|
||||
.then((auth) => {
|
||||
return db.put<string, Auth>(
|
||||
levelKeys.auth,
|
||||
{
|
||||
id: 1,
|
||||
...auth,
|
||||
accessToken,
|
||||
tokenExpirationTimestamp,
|
||||
},
|
||||
["id"]
|
||||
{ valueEncoding: "json" }
|
||||
);
|
||||
});
|
||||
|
||||
return { accessToken, expiresIn };
|
||||
}
|
||||
|
||||
private static async revalidateAccessTokenIfExpired() {
|
||||
if (this.userAuth.expirationTimestamp < Date.now()) {
|
||||
try {
|
||||
await this.refreshToken();
|
||||
} catch (err) {
|
||||
this.handleUnauthorizedError(err);
|
||||
}
|
||||
@@ -261,7 +282,7 @@ export class HydraApi {
|
||||
};
|
||||
}
|
||||
|
||||
private static handleUnauthorizedError = (err) => {
|
||||
private static readonly handleUnauthorizedError = (err) => {
|
||||
if (err instanceof AxiosError && err.response?.status === 401) {
|
||||
logger.error(
|
||||
"401 - Current credentials:",
|
||||
@@ -276,8 +297,16 @@ export class HydraApi {
|
||||
subscription: null,
|
||||
};
|
||||
|
||||
userAuthRepository.delete({ id: 1 });
|
||||
userSubscriptionRepository.delete({ id: 1 });
|
||||
db.batch([
|
||||
{
|
||||
type: "del",
|
||||
key: levelKeys.auth,
|
||||
},
|
||||
{
|
||||
type: "del",
|
||||
key: levelKeys.user,
|
||||
},
|
||||
]);
|
||||
|
||||
this.sendSignOutEvent();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { gamesSublevel, levelKeys } from "@main/level";
|
||||
|
||||
export const clearGamesRemoteIds = () => {
|
||||
return gameRepository.update({}, { remoteId: null });
|
||||
export const clearGamesRemoteIds = async () => {
|
||||
const games = await gamesSublevel.values().all();
|
||||
|
||||
await gamesSublevel.batch(
|
||||
games.map((game) => ({
|
||||
type: "put",
|
||||
key: levelKeys.game(game.shop, game.objectId),
|
||||
value: {
|
||||
...game,
|
||||
remoteId: null,
|
||||
},
|
||||
}))
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import { Game } from "@main/entity";
|
||||
import type { Game } from "@types";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { gamesSublevel, levelKeys } from "@main/level";
|
||||
|
||||
export const createGame = async (game: Game) => {
|
||||
return HydraApi.post(`/profile/games`, {
|
||||
objectId: game.objectID,
|
||||
objectId: game.objectId,
|
||||
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds),
|
||||
shop: game.shop,
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
}).then((response) => {
|
||||
const { id: remoteId, playTimeInMilliseconds, lastTimePlayed } = response;
|
||||
|
||||
gameRepository.update(
|
||||
{ objectID: game.objectID },
|
||||
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
|
||||
);
|
||||
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
|
||||
...game,
|
||||
remoteId,
|
||||
playTimeInMilliseconds,
|
||||
lastTimePlayed,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { steamGamesWorker } from "@main/workers";
|
||||
import { steamUrlBuilder } from "@shared";
|
||||
import { gamesSublevel, levelKeys } from "@main/level";
|
||||
|
||||
export const mergeWithRemoteGames = async () => {
|
||||
return HydraApi.get("/profile/games")
|
||||
.then(async (response) => {
|
||||
for (const game of response) {
|
||||
const localGame = await gameRepository.findOne({
|
||||
where: {
|
||||
objectID: game.objectId,
|
||||
},
|
||||
});
|
||||
const localGame = await gamesSublevel.get(
|
||||
levelKeys.game(game.shop, game.objectId)
|
||||
);
|
||||
|
||||
if (localGame) {
|
||||
const updatedLastTimePlayed =
|
||||
@@ -26,37 +24,31 @@ export const mergeWithRemoteGames = async () => {
|
||||
? game.playTimeInMilliseconds
|
||||
: localGame.playTimeInMilliseconds;
|
||||
|
||||
gameRepository.update(
|
||||
{
|
||||
objectID: game.objectId,
|
||||
shop: "steam",
|
||||
},
|
||||
{
|
||||
remoteId: game.id,
|
||||
lastTimePlayed: updatedLastTimePlayed,
|
||||
playTimeInMilliseconds: updatedPlayTime,
|
||||
}
|
||||
);
|
||||
await gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
|
||||
...localGame,
|
||||
remoteId: game.id,
|
||||
lastTimePlayed: updatedLastTimePlayed,
|
||||
playTimeInMilliseconds: updatedPlayTime,
|
||||
});
|
||||
} else {
|
||||
const steamGame = await steamGamesWorker.run(Number(game.objectId), {
|
||||
name: "getById",
|
||||
});
|
||||
|
||||
if (steamGame) {
|
||||
const iconUrl = steamGame?.clientIcon
|
||||
? steamUrlBuilder.icon(game.objectId, steamGame.clientIcon)
|
||||
: null;
|
||||
const iconUrl = steamGame?.clientIcon
|
||||
? steamUrlBuilder.icon(game.objectId, steamGame.clientIcon)
|
||||
: null;
|
||||
|
||||
gameRepository.insert({
|
||||
objectID: game.objectId,
|
||||
title: steamGame?.name,
|
||||
remoteId: game.id,
|
||||
shop: game.shop,
|
||||
iconUrl,
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
playTimeInMilliseconds: game.playTimeInMilliseconds,
|
||||
});
|
||||
}
|
||||
await gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
|
||||
objectId: game.objectId,
|
||||
title: steamGame?.name,
|
||||
remoteId: game.id,
|
||||
shop: game.shop,
|
||||
iconUrl,
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
playTimeInMilliseconds: game.playTimeInMilliseconds,
|
||||
isDeleted: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Game } from "@main/entity";
|
||||
import type { Game } from "@types";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
|
||||
export const updateGamePlaytime = async (
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { chunk } from "lodash-es";
|
||||
import { IsNull } from "typeorm";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { mergeWithRemoteGames } from "./merge-with-remote-games";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import { AchievementWatcherManager } from "../achievements/achievement-watcher-manager";
|
||||
import { gamesSublevel } from "@main/level";
|
||||
|
||||
export const uploadGamesBatch = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: { remoteId: IsNull(), isDeleted: false },
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((results) => {
|
||||
return results.filter(
|
||||
(game) => !game.isDeleted && game.remoteId === null
|
||||
);
|
||||
});
|
||||
|
||||
const gamesChunks = chunk(games, 200);
|
||||
const gamesChunks = chunk(games, 50);
|
||||
|
||||
for (const chunk of gamesChunks) {
|
||||
await HydraApi.post(
|
||||
"/profile/games/batch",
|
||||
chunk.map((game) => {
|
||||
return {
|
||||
objectId: game.objectID,
|
||||
objectId: game.objectId,
|
||||
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds),
|
||||
shop: game.shop,
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
|
||||
@@ -6,8 +6,12 @@ log.transports.file.resolvePathFn = (
|
||||
_: log.PathVariables,
|
||||
message?: log.LogMessage | undefined
|
||||
) => {
|
||||
if (message?.scope === "python-instance") {
|
||||
return path.join(logsPath, "pythoninstance.txt");
|
||||
if (message?.scope === "python-rpc") {
|
||||
return path.join(logsPath, "pythonrpc.txt");
|
||||
}
|
||||
|
||||
if (message?.scope === "network") {
|
||||
return path.join(logsPath, "network.txt");
|
||||
}
|
||||
|
||||
if (message?.scope == "achievements") {
|
||||
@@ -34,3 +38,4 @@ log.initialize();
|
||||
export const pythonRpcLogger = log.scope("python-rpc");
|
||||
export const logger = log.scope("main");
|
||||
export const achievementsLogger = log.scope("achievements");
|
||||
export const networkLogger = log.scope("network");
|
||||
|
||||
@@ -2,6 +2,7 @@ import { sleep } from "@main/helpers";
|
||||
import { DownloadManager } from "./download";
|
||||
import { watchProcesses } from "./process-watcher";
|
||||
import { AchievementWatcherManager } from "./achievements/achievement-watcher-manager";
|
||||
import { UpdateManager } from "./update-manager";
|
||||
|
||||
export const startMainLoop = async () => {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
@@ -11,6 +12,7 @@ export const startMainLoop = async () => {
|
||||
DownloadManager.watchDownloads(),
|
||||
AchievementWatcherManager.watchAchievements(),
|
||||
DownloadManager.getSeedStatus(),
|
||||
UpdateManager.checkForUpdatePeriodically(),
|
||||
]);
|
||||
|
||||
await sleep(1500);
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Notification, app } from "electron";
|
||||
import { t } from "i18next";
|
||||
import trayIcon from "@resources/tray-icon.png?asset";
|
||||
import { Game } from "@main/entity";
|
||||
import { userPreferencesRepository } from "@main/repository";
|
||||
import fs from "node:fs";
|
||||
import axios from "axios";
|
||||
import path from "node:path";
|
||||
@@ -11,6 +9,9 @@ import { achievementSoundPath } from "@main/constants";
|
||||
import icon from "@resources/icon.png?asset";
|
||||
import { NotificationOptions, toXmlString } from "./xml";
|
||||
import { logger } from "../logger";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import type { Game, UserPreferences } from "@types";
|
||||
import { db, levelKeys } from "@main/level";
|
||||
|
||||
async function downloadImage(url: string | null) {
|
||||
if (!url) return undefined;
|
||||
@@ -38,9 +39,12 @@ async function downloadImage(url: string | null) {
|
||||
}
|
||||
|
||||
export const publishDownloadCompleteNotification = async (game: Game) => {
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
const userPreferences = await db.get<string, UserPreferences>(
|
||||
levelKeys.userPreferences,
|
||||
{
|
||||
valueEncoding: "json",
|
||||
}
|
||||
);
|
||||
|
||||
if (userPreferences?.downloadNotificationsEnabled) {
|
||||
new Notification({
|
||||
@@ -93,7 +97,9 @@ export const publishCombinedNewAchievementNotification = async (
|
||||
toastXml: toXmlString(options),
|
||||
}).show();
|
||||
|
||||
if (process.platform !== "linux") {
|
||||
if (WindowManager.mainWindow) {
|
||||
WindowManager.mainWindow.webContents.send("on-achievement-unlocked");
|
||||
} else if (process.platform !== "linux") {
|
||||
sound.play(achievementSoundPath);
|
||||
}
|
||||
};
|
||||
@@ -140,7 +146,9 @@ export const publishNewAchievementNotification = async (info: {
|
||||
toastXml: toXmlString(options),
|
||||
}).show();
|
||||
|
||||
if (process.platform !== "linux") {
|
||||
if (WindowManager.mainWindow) {
|
||||
WindowManager.mainWindow.webContents.send("on-achievement-unlocked");
|
||||
} else if (process.platform !== "linux") {
|
||||
sound.play(achievementSoundPath);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { WindowManager } from "./window-manager";
|
||||
import { createGame, updateGamePlaytime } from "./library-sync";
|
||||
import type { GameRunning } from "@types";
|
||||
import type { Game, GameRunning } from "@types";
|
||||
import { PythonRPC } from "./python-rpc";
|
||||
import { Game } from "@main/entity";
|
||||
import axios from "axios";
|
||||
import { exec } from "child_process";
|
||||
import { ProcessPayload } from "./download/types";
|
||||
import { gamesSublevel, levelKeys } from "@main/level";
|
||||
|
||||
const commands = {
|
||||
findWineDir: `lsof -c wine 2>/dev/null | grep '/drive_c/windows$' | head -n 1 | awk '{for(i=9;i<=NF;i++) printf "%s ", $i; print ""}'`,
|
||||
@@ -14,7 +13,7 @@ const commands = {
|
||||
};
|
||||
|
||||
export const gamesPlaytime = new Map<
|
||||
number,
|
||||
string,
|
||||
{ lastTick: number; firstTick: number; lastSyncTick: number }
|
||||
>();
|
||||
|
||||
@@ -82,23 +81,28 @@ const findGamePathByProcess = (
|
||||
const pathSet = processMap.get(executable.exe);
|
||||
|
||||
if (pathSet) {
|
||||
pathSet.forEach((path) => {
|
||||
pathSet.forEach(async (path) => {
|
||||
if (path.toLowerCase().endsWith(executable.name)) {
|
||||
gameRepository.update(
|
||||
{ objectID: gameId, shop: "steam" },
|
||||
{ executablePath: path }
|
||||
);
|
||||
const gameKey = levelKeys.game("steam", gameId);
|
||||
const game = await gamesSublevel.get(gameKey);
|
||||
|
||||
if (game) {
|
||||
gamesSublevel.put(gameKey, {
|
||||
...game,
|
||||
executablePath: path,
|
||||
});
|
||||
}
|
||||
|
||||
if (isLinuxPlatform) {
|
||||
exec(commands.findWineDir, (err, out) => {
|
||||
if (err) return;
|
||||
|
||||
gameRepository.update(
|
||||
{ objectID: gameId, shop: "steam" },
|
||||
{
|
||||
if (game) {
|
||||
gamesSublevel.put(gameKey, {
|
||||
...game,
|
||||
winePrefixPath: out.trim().replace("/drive_c/windows", ""),
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -159,11 +163,12 @@ const getSystemProcessMap = async () => {
|
||||
};
|
||||
|
||||
export const watchProcesses = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
},
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((results) => {
|
||||
return results.filter((game) => game.isDeleted === false);
|
||||
});
|
||||
|
||||
if (!games.length) return;
|
||||
|
||||
@@ -172,8 +177,8 @@ export const watchProcesses = async () => {
|
||||
for (const game of games) {
|
||||
const executablePath = game.executablePath;
|
||||
if (!executablePath) {
|
||||
if (gameExecutables[game.objectID]) {
|
||||
findGamePathByProcess(processMap, game.objectID);
|
||||
if (gameExecutables[game.objectId]) {
|
||||
findGamePathByProcess(processMap, game.objectId);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -185,12 +190,12 @@ export const watchProcesses = async () => {
|
||||
const hasProcess = processMap.get(executable)?.has(executablePath);
|
||||
|
||||
if (hasProcess) {
|
||||
if (gamesPlaytime.has(game.id)) {
|
||||
if (gamesPlaytime.has(levelKeys.game(game.shop, game.objectId))) {
|
||||
onTickGame(game);
|
||||
} else {
|
||||
onOpenGame(game);
|
||||
}
|
||||
} else if (gamesPlaytime.has(game.id)) {
|
||||
} else if (gamesPlaytime.has(levelKeys.game(game.shop, game.objectId))) {
|
||||
onCloseGame(game);
|
||||
}
|
||||
}
|
||||
@@ -202,20 +207,17 @@ export const watchProcesses = async () => {
|
||||
return {
|
||||
id: entry[0],
|
||||
sessionDurationInMillis: performance.now() - entry[1].firstTick,
|
||||
};
|
||||
} as Pick<GameRunning, "id" | "sessionDurationInMillis">;
|
||||
});
|
||||
|
||||
WindowManager.mainWindow.webContents.send(
|
||||
"on-games-running",
|
||||
gamesRunning as Pick<GameRunning, "id" | "sessionDurationInMillis">[]
|
||||
);
|
||||
WindowManager.mainWindow.webContents.send("on-games-running", gamesRunning);
|
||||
}
|
||||
};
|
||||
|
||||
function onOpenGame(game: Game) {
|
||||
const now = performance.now();
|
||||
|
||||
gamesPlaytime.set(game.id, {
|
||||
gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), {
|
||||
lastTick: now,
|
||||
firstTick: now,
|
||||
lastSyncTick: now,
|
||||
@@ -230,16 +232,25 @@ function onOpenGame(game: Game) {
|
||||
|
||||
function onTickGame(game: Game) {
|
||||
const now = performance.now();
|
||||
const gamePlaytime = gamesPlaytime.get(game.id)!;
|
||||
const gamePlaytime = gamesPlaytime.get(
|
||||
levelKeys.game(game.shop, game.objectId)
|
||||
)!;
|
||||
|
||||
const delta = now - gamePlaytime.lastTick;
|
||||
|
||||
gameRepository.update(game.id, {
|
||||
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
|
||||
...game,
|
||||
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
|
||||
lastTimePlayed: new Date(),
|
||||
});
|
||||
|
||||
gamesPlaytime.set(game.id, {
|
||||
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
|
||||
...game,
|
||||
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
|
||||
lastTimePlayed: new Date(),
|
||||
});
|
||||
|
||||
gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), {
|
||||
...gamePlaytime,
|
||||
lastTick: now,
|
||||
});
|
||||
@@ -255,7 +266,7 @@ function onTickGame(game: Game) {
|
||||
|
||||
gamePromise
|
||||
.then(() => {
|
||||
gamesPlaytime.set(game.id, {
|
||||
gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), {
|
||||
...gamePlaytime,
|
||||
lastSyncTick: now,
|
||||
});
|
||||
@@ -265,8 +276,10 @@ function onTickGame(game: Game) {
|
||||
}
|
||||
|
||||
const onCloseGame = (game: Game) => {
|
||||
const gamePlaytime = gamesPlaytime.get(game.id)!;
|
||||
gamesPlaytime.delete(game.id);
|
||||
const gamePlaytime = gamesPlaytime.get(
|
||||
levelKeys.game(game.shop, game.objectId)
|
||||
)!;
|
||||
gamesPlaytime.delete(levelKeys.game(game.shop, game.objectId));
|
||||
|
||||
if (game.remoteId) {
|
||||
updateGamePlaytime(
|
||||
|
||||
@@ -10,7 +10,7 @@ import { Readable } from "node:stream";
|
||||
import { app, dialog } from "electron";
|
||||
|
||||
interface GamePayload {
|
||||
game_id: number;
|
||||
game_id: string;
|
||||
url: string;
|
||||
save_path: string;
|
||||
}
|
||||
|
||||
@@ -21,11 +21,18 @@ export const getSteamAppDetails = async (
|
||||
});
|
||||
|
||||
return axios
|
||||
.get(
|
||||
.get<SteamAppDetailsResponse>(
|
||||
`http://store.steampowered.com/api/appdetails?${searchParams.toString()}`
|
||||
)
|
||||
.then((response) => {
|
||||
if (response.data[objectId].success) return response.data[objectId].data;
|
||||
if (response.data[objectId].success) {
|
||||
const data = response.data[objectId].data;
|
||||
return {
|
||||
...data,
|
||||
objectId,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
60
src/main/services/update-manager.ts
Normal file
60
src/main/services/update-manager.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import updater, { UpdateInfo } from "electron-updater";
|
||||
import { logger, WindowManager } from "@main/services";
|
||||
import { AppUpdaterEvent } from "@types";
|
||||
import { app } from "electron";
|
||||
import { publishNotificationUpdateReadyToInstall } from "@main/services/notifications";
|
||||
|
||||
const isAutoInstallAvailable =
|
||||
process.platform !== "darwin" && process.env.PORTABLE_EXECUTABLE_FILE == null;
|
||||
|
||||
const { autoUpdater } = updater;
|
||||
const sendEventsForDebug = false;
|
||||
|
||||
export class UpdateManager {
|
||||
private static hasNotified = false;
|
||||
private static newVersion = "";
|
||||
private static checkTick = 0;
|
||||
|
||||
private static mockValuesForDebug() {
|
||||
this.sendEvent({ type: "update-available", info: { version: "1.3.0" } });
|
||||
this.sendEvent({ type: "update-downloaded" });
|
||||
}
|
||||
|
||||
private static sendEvent(event: AppUpdaterEvent) {
|
||||
WindowManager.mainWindow?.webContents.send("autoUpdaterEvent", event);
|
||||
}
|
||||
|
||||
public static checkForUpdates() {
|
||||
autoUpdater
|
||||
.once("update-available", (info: UpdateInfo) => {
|
||||
this.sendEvent({ type: "update-available", info });
|
||||
this.newVersion = info.version;
|
||||
})
|
||||
.once("update-downloaded", () => {
|
||||
this.sendEvent({ type: "update-downloaded" });
|
||||
|
||||
if (!this.hasNotified) {
|
||||
this.hasNotified = true;
|
||||
publishNotificationUpdateReadyToInstall(this.newVersion);
|
||||
}
|
||||
});
|
||||
|
||||
if (app.isPackaged) {
|
||||
autoUpdater.autoDownload = isAutoInstallAvailable;
|
||||
autoUpdater.checkForUpdates().then((result) => {
|
||||
logger.log(`Check for updates result: ${result}`);
|
||||
});
|
||||
} else if (sendEventsForDebug) {
|
||||
this.mockValuesForDebug();
|
||||
}
|
||||
|
||||
return isAutoInstallAvailable;
|
||||
}
|
||||
|
||||
public static checkForUpdatePeriodically() {
|
||||
if (this.checkTick % 2000 == 0) {
|
||||
this.checkForUpdates();
|
||||
}
|
||||
this.checkTick++;
|
||||
}
|
||||
}
|
||||
@@ -1,60 +1,45 @@
|
||||
import type { ProfileVisibility, UserDetails } from "@types";
|
||||
import { User, type ProfileVisibility, type UserDetails } from "@types";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import {
|
||||
userAuthRepository,
|
||||
userSubscriptionRepository,
|
||||
} from "@main/repository";
|
||||
import { UserNotLoggedInError } from "@shared";
|
||||
import { logger } from "../logger";
|
||||
import { db } from "@main/level";
|
||||
import { levelKeys } from "@main/level/sublevels";
|
||||
|
||||
export const getUserData = () => {
|
||||
export const getUserData = async () => {
|
||||
return HydraApi.get<UserDetails>(`/profile/me`)
|
||||
.then(async (me) => {
|
||||
userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
displayName: me.displayName,
|
||||
profileImageUrl: me.profileImageUrl,
|
||||
backgroundImageUrl: me.backgroundImageUrl,
|
||||
userId: me.id,
|
||||
},
|
||||
["id"]
|
||||
db.get<string, User>(levelKeys.user, { valueEncoding: "json" }).then(
|
||||
(user) => {
|
||||
return db.put<string, User>(
|
||||
levelKeys.user,
|
||||
{
|
||||
...user,
|
||||
id: me.id,
|
||||
displayName: me.displayName,
|
||||
profileImageUrl: me.profileImageUrl,
|
||||
backgroundImageUrl: me.backgroundImageUrl,
|
||||
subscription: me.subscription,
|
||||
},
|
||||
{ valueEncoding: "json" }
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
if (me.subscription) {
|
||||
await userSubscriptionRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
subscriptionId: me.subscription?.id || "",
|
||||
status: me.subscription?.status || "",
|
||||
planId: me.subscription?.plan.id || "",
|
||||
planName: me.subscription?.plan.name || "",
|
||||
expiresAt: me.subscription?.expiresAt || null,
|
||||
user: { id: 1 },
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
} else {
|
||||
await userSubscriptionRepository.delete({ id: 1 });
|
||||
}
|
||||
|
||||
return me;
|
||||
})
|
||||
.catch(async (err) => {
|
||||
if (err instanceof UserNotLoggedInError) {
|
||||
logger.info("User is not logged in", err);
|
||||
return null;
|
||||
}
|
||||
logger.error("Failed to get logged user");
|
||||
const loggedUser = await userAuthRepository.findOne({
|
||||
where: { id: 1 },
|
||||
relations: { subscription: true },
|
||||
|
||||
const loggedUser = await db.get<string, User>(levelKeys.user, {
|
||||
valueEncoding: "json",
|
||||
});
|
||||
|
||||
if (loggedUser) {
|
||||
return {
|
||||
...loggedUser,
|
||||
id: loggedUser.userId,
|
||||
username: "",
|
||||
bio: "",
|
||||
email: null,
|
||||
@@ -64,15 +49,16 @@ export const getUserData = () => {
|
||||
},
|
||||
subscription: loggedUser.subscription
|
||||
? {
|
||||
id: loggedUser.subscription.subscriptionId,
|
||||
id: loggedUser.subscription.id,
|
||||
status: loggedUser.subscription.status,
|
||||
plan: {
|
||||
id: loggedUser.subscription.planId,
|
||||
name: loggedUser.subscription.planName,
|
||||
id: loggedUser.subscription.plan.id,
|
||||
name: loggedUser.subscription.plan.name,
|
||||
},
|
||||
expiresAt: loggedUser.subscription.expiresAt,
|
||||
}
|
||||
: null,
|
||||
featurebaseJwt: "",
|
||||
} as UserDetails;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,18 +9,23 @@ import {
|
||||
shell,
|
||||
} from "electron";
|
||||
import { is } from "@electron-toolkit/utils";
|
||||
import i18next, { t } from "i18next";
|
||||
import { t } from "i18next";
|
||||
import path from "node:path";
|
||||
import icon from "@resources/icon.png?asset";
|
||||
import trayIcon from "@resources/tray-icon.png?asset";
|
||||
import { gameRepository, userPreferencesRepository } from "@main/repository";
|
||||
import { IsNull, Not } from "typeorm";
|
||||
import { HydraApi } from "./hydra-api";
|
||||
import UserAgent from "user-agents";
|
||||
import { db, gamesSublevel, levelKeys } from "@main/level";
|
||||
import { slice, sortBy } from "lodash-es";
|
||||
import type { UserPreferences } from "@types";
|
||||
import { AuthPage } from "@shared";
|
||||
import { isStaging } from "@main/constants";
|
||||
|
||||
export class WindowManager {
|
||||
public static mainWindow: Electron.BrowserWindow | null = null;
|
||||
|
||||
private static readonly editorWindows: Map<string, BrowserWindow> = new Map();
|
||||
|
||||
private static loadMainWindowURL(hash = "") {
|
||||
// HMR for renderer base on electron-vite cli.
|
||||
// Load the remote URL for development or the local html file for production.
|
||||
@@ -48,11 +53,11 @@ export class WindowManager {
|
||||
minHeight: 540,
|
||||
backgroundColor: "#1c1c1c",
|
||||
titleBarStyle: process.platform === "linux" ? "default" : "hidden",
|
||||
...(process.platform === "linux" ? { icon } : {}),
|
||||
icon,
|
||||
trafficLightPosition: { x: 16, y: 16 },
|
||||
titleBarOverlay: {
|
||||
symbolColor: "#DADBE1",
|
||||
color: "#151515",
|
||||
color: "#00000000",
|
||||
height: 34,
|
||||
},
|
||||
webPreferences: {
|
||||
@@ -125,14 +130,18 @@ export class WindowManager {
|
||||
this.mainWindow.removeMenu();
|
||||
|
||||
this.mainWindow.on("ready-to-show", () => {
|
||||
if (!app.isPackaged) WindowManager.mainWindow?.webContents.openDevTools();
|
||||
if (!app.isPackaged || isStaging)
|
||||
WindowManager.mainWindow?.webContents.openDevTools();
|
||||
WindowManager.mainWindow?.show();
|
||||
});
|
||||
|
||||
this.mainWindow.on("close", async () => {
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
const userPreferences = await db.get<string, UserPreferences>(
|
||||
levelKeys.userPreferences,
|
||||
{
|
||||
valueEncoding: "json",
|
||||
}
|
||||
);
|
||||
|
||||
if (userPreferences?.preferQuitInsteadOfHiding) {
|
||||
app.quit();
|
||||
@@ -140,9 +149,14 @@ export class WindowManager {
|
||||
WindowManager.mainWindow?.setProgressBar(-1);
|
||||
WindowManager.mainWindow = null;
|
||||
});
|
||||
|
||||
this.mainWindow.webContents.setWindowOpenHandler((handler) => {
|
||||
shell.openExternal(handler.url);
|
||||
return { action: "deny" };
|
||||
});
|
||||
}
|
||||
|
||||
public static openAuthWindow() {
|
||||
public static openAuthWindow(page: AuthPage, searchParams: URLSearchParams) {
|
||||
if (this.mainWindow) {
|
||||
const authWindow = new BrowserWindow({
|
||||
width: 600,
|
||||
@@ -164,12 +178,8 @@ export class WindowManager {
|
||||
|
||||
if (!app.isPackaged) authWindow.webContents.openDevTools();
|
||||
|
||||
const searchParams = new URLSearchParams({
|
||||
lng: i18next.language,
|
||||
});
|
||||
|
||||
authWindow.loadURL(
|
||||
`${import.meta.env.MAIN_VITE_AUTH_URL}/?${searchParams.toString()}`
|
||||
`${import.meta.env.MAIN_VITE_AUTH_URL}${page}?${searchParams.toString()}`
|
||||
);
|
||||
|
||||
authWindow.once("ready-to-show", () => {
|
||||
@@ -181,7 +191,95 @@ export class WindowManager {
|
||||
authWindow.close();
|
||||
|
||||
HydraApi.handleExternalAuth(url);
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.startsWith("hydralauncher://update-account")) {
|
||||
authWindow.close();
|
||||
|
||||
WindowManager.mainWindow?.webContents.send("on-account-updated");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static openEditorWindow(themeId: string) {
|
||||
if (this.mainWindow) {
|
||||
const existingWindow = this.editorWindows.get(themeId);
|
||||
if (existingWindow) {
|
||||
if (existingWindow.isMinimized()) {
|
||||
existingWindow.restore();
|
||||
}
|
||||
existingWindow.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
const editorWindow = new BrowserWindow({
|
||||
width: 600,
|
||||
height: 720,
|
||||
minWidth: 600,
|
||||
minHeight: 540,
|
||||
backgroundColor: "#1c1c1c",
|
||||
titleBarStyle: process.platform === "linux" ? "default" : "hidden",
|
||||
...(process.platform === "linux" ? { icon } : {}),
|
||||
trafficLightPosition: { x: 16, y: 16 },
|
||||
titleBarOverlay: {
|
||||
symbolColor: "#DADBE1",
|
||||
color: "#151515",
|
||||
height: 34,
|
||||
},
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "../preload/index.mjs"),
|
||||
sandbox: false,
|
||||
},
|
||||
show: false,
|
||||
});
|
||||
|
||||
this.editorWindows.set(themeId, editorWindow);
|
||||
|
||||
editorWindow.removeMenu();
|
||||
|
||||
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
|
||||
editorWindow.loadURL(
|
||||
`${process.env["ELECTRON_RENDERER_URL"]}#/theme-editor?themeId=${themeId}`
|
||||
);
|
||||
} else {
|
||||
editorWindow.loadFile(path.join(__dirname, "../renderer/index.html"), {
|
||||
hash: `theme-editor?themeId=${themeId}`,
|
||||
});
|
||||
}
|
||||
|
||||
editorWindow.once("ready-to-show", () => {
|
||||
editorWindow.show();
|
||||
this.mainWindow?.webContents.openDevTools();
|
||||
if (isStaging) {
|
||||
editorWindow.webContents.openDevTools();
|
||||
}
|
||||
});
|
||||
|
||||
editorWindow.webContents.on("before-input-event", (event, input) => {
|
||||
if (input.key === "F12") {
|
||||
event.preventDefault();
|
||||
this.mainWindow?.webContents.toggleDevTools();
|
||||
}
|
||||
});
|
||||
|
||||
editorWindow.on("close", () => {
|
||||
this.mainWindow?.webContents.closeDevTools();
|
||||
this.editorWindows.delete(themeId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static closeEditorWindow(themeId?: string) {
|
||||
if (themeId) {
|
||||
const editorWindow = this.editorWindows.get(themeId);
|
||||
if (editorWindow) {
|
||||
editorWindow.close();
|
||||
}
|
||||
} else {
|
||||
this.editorWindows.forEach((editorWindow) => {
|
||||
editorWindow.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -207,17 +305,19 @@ export class WindowManager {
|
||||
}
|
||||
|
||||
const updateSystemTray = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
executablePath: Not(IsNull()),
|
||||
lastTimePlayed: Not(IsNull()),
|
||||
},
|
||||
take: 5,
|
||||
order: {
|
||||
lastTimePlayed: "DESC",
|
||||
},
|
||||
});
|
||||
const games = await gamesSublevel
|
||||
.values()
|
||||
.all()
|
||||
.then((games) => {
|
||||
const filteredGames = games.filter(
|
||||
(game) =>
|
||||
!game.isDeleted && game.executablePath && game.lastTimePlayed
|
||||
);
|
||||
|
||||
const sortedGames = sortBy(filteredGames, "lastTimePlayed", "DESC");
|
||||
|
||||
return slice(sortedGames, 5);
|
||||
});
|
||||
|
||||
const recentlyPlayedGames: Array<MenuItemConstructorOptions | MenuItem> =
|
||||
games.map(({ title, executablePath }) => ({
|
||||
|
||||
Reference in New Issue
Block a user