Merge branch 'main' into hyd-228-investigate-why-users-are-being-logged-out-when-updating

# Conflicts:
#	src/main/events/user-preferences/auto-launch.ts
This commit is contained in:
Zamitto
2024-07-03 16:32:25 -03:00
24 changed files with 142 additions and 154 deletions

View File

@@ -1,6 +1,6 @@
import { Game } from "@main/entity";
import { Downloader } from "@shared";
import { TorrentDownloader } from "./torrent-downloader";
import { PythonInstance } from "./python-instance";
import { WindowManager } from "../window-manager";
import { downloadQueueRepository, gameRepository } from "@main/repository";
import { publishDownloadCompleteNotification } from "../notifications";
@@ -16,7 +16,7 @@ export class DownloadManager {
if (this.currentDownloader === Downloader.RealDebrid) {
status = await RealDebridDownloader.getStatus();
} else {
status = await TorrentDownloader.getStatus();
status = await PythonInstance.getStatus();
}
if (status) {
@@ -65,7 +65,7 @@ export class DownloadManager {
if (this.currentDownloader === Downloader.RealDebrid) {
RealDebridDownloader.pauseDownload();
} else {
await TorrentDownloader.pauseDownload();
await PythonInstance.pauseDownload();
}
WindowManager.mainWindow?.setProgressBar(-1);
@@ -77,7 +77,7 @@ export class DownloadManager {
RealDebridDownloader.startDownload(game);
this.currentDownloader = Downloader.RealDebrid;
} else {
TorrentDownloader.startDownload(game);
PythonInstance.startDownload(game);
this.currentDownloader = Downloader.Torrent;
}
}
@@ -86,7 +86,7 @@ export class DownloadManager {
if (this.currentDownloader === Downloader.RealDebrid) {
RealDebridDownloader.cancelDownload();
} else {
TorrentDownloader.cancelDownload(gameId);
PythonInstance.cancelDownload(gameId);
}
WindowManager.mainWindow?.setProgressBar(-1);
@@ -98,7 +98,7 @@ export class DownloadManager {
RealDebridDownloader.startDownload(game);
this.currentDownloader = Downloader.RealDebrid;
} else {
TorrentDownloader.startDownload(game);
PythonInstance.startDownload(game);
this.currentDownloader = Downloader.Torrent;
}
}

View File

@@ -1,2 +1,2 @@
export * from "./download-manager";
export * from "./torrent-downloader";
export * from "./python-instance";

View File

@@ -1,7 +1,11 @@
import cp from "node:child_process";
import { Game } from "@main/entity";
import { RPC_PASSWORD, RPC_PORT, startTorrentClient } from "./torrent-client";
import {
RPC_PASSWORD,
RPC_PORT,
startTorrentClient as startRPCClient,
} from "./torrent-client";
import { gameRepository } from "@main/repository";
import { DownloadProgress } from "@types";
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
@@ -13,10 +17,11 @@ import {
PauseDownloadPayload,
LibtorrentStatus,
LibtorrentPayload,
ProcessPayload,
} from "./types";
export class TorrentDownloader {
private static torrentClient: cp.ChildProcess | null = null;
export class PythonInstance {
private static pythonProcess: cp.ChildProcess | null = null;
private static downloadingGameId = -1;
private static rpc = axios.create({
@@ -26,18 +31,31 @@ export class TorrentDownloader {
},
});
private static spawn(args: StartDownloadPayload) {
this.torrentClient = startTorrentClient(args);
public static spawn(args?: StartDownloadPayload) {
this.pythonProcess = startRPCClient(args);
}
public static kill() {
if (this.torrentClient) {
this.torrentClient.kill();
this.torrentClient = null;
if (this.pythonProcess) {
this.pythonProcess.kill();
this.pythonProcess = null;
this.downloadingGameId = -1;
}
}
public static killTorrent() {
if (this.pythonProcess) {
this.rpc.post("/action", { action: "kill-torrent" });
this.downloadingGameId = -1;
}
}
public static async getProcessList() {
return (
(await this.rpc.get<ProcessPayload[] | null>("/process-list")).data || []
);
}
public static async getStatus() {
if (this.downloadingGameId === -1) return null;
@@ -113,7 +131,7 @@ export class TorrentDownloader {
}
static async startDownload(game: Game) {
if (!this.torrentClient) {
if (!this.pythonProcess) {
this.spawn({
game_id: game.id,
magnet: game.uri!,

View File

@@ -15,12 +15,12 @@ export const BITTORRENT_PORT = "5881";
export const RPC_PORT = "8084";
export const RPC_PASSWORD = crypto.randomBytes(32).toString("hex");
export const startTorrentClient = (args: StartDownloadPayload) => {
export const startTorrentClient = (args?: StartDownloadPayload) => {
const commonArgs = [
BITTORRENT_PORT,
RPC_PORT,
RPC_PASSWORD,
encodeURIComponent(JSON.stringify(args)),
args ? encodeURIComponent(JSON.stringify(args)) : "",
];
if (app.isPackaged) {

View File

@@ -31,3 +31,8 @@ export interface LibtorrentPayload {
status: LibtorrentStatus;
gameId: number;
}
export interface ProcessPayload {
exe: string;
pid: number;
}

View File

@@ -1,11 +1,9 @@
import path from "node:path";
import { IsNull, Not } from "typeorm";
import { gameRepository } from "@main/repository";
import { getProcesses } from "@main/helpers";
import { WindowManager } from "./window-manager";
import { createGame, updateGamePlaytime } from "./library-sync";
import { GameRunning } from "@types";
import { PythonInstance } from "./download";
export const gamesPlaytime = new Map<
number,
@@ -21,23 +19,13 @@ export const watchProcesses = async () => {
});
if (games.length === 0) return;
const processes = await getProcesses();
const processes = await PythonInstance.getProcessList();
for (const game of games) {
const executablePath = game.executablePath!;
const basename = path.win32.basename(executablePath);
const basenameWithoutExtension = path.win32.basename(
executablePath,
path.extname(executablePath)
);
const gameProcess = processes.find((runningProcess) => {
if (process.platform === "win32") {
return runningProcess.name === basename;
}
return [basename, basenameWithoutExtension].includes(runningProcess.name);
return executablePath == runningProcess.exe;
});
if (gameProcess) {