From 797f5ee0d8ab712091fbc6d91e2a47c0970d426a Mon Sep 17 00:00:00 2001 From: JackEnx Date: Sun, 21 Apr 2024 16:36:34 -0300 Subject: [PATCH 1/2] refactor: change game delete to soft delete --- src/main/entity/game.entity.ts | 3 ++ src/main/events/index.ts | 5 +-- .../events/library/add-game-to-library.ts | 35 ++++++++++++++----- .../events/library/get-game-by-object-id.ts | 1 + src/main/events/library/get-library.ts | 3 ++ .../library/remove-game-from-library.ts | 13 +++++++ src/main/events/library/remove-game.ts | 11 ------ .../events/torrenting/cancel-game-download.ts | 3 +- .../delete-game-folder.ts | 5 ++- .../torrenting/remove-game-from-download.ts | 34 ++++++++++++++++++ .../events/torrenting/start-game-download.ts | 8 +---- src/preload.ts | 5 ++- src/renderer/declaration.d.ts | 3 +- src/renderer/hooks/use-download.ts | 6 ++-- src/renderer/hooks/use-library.ts | 7 +++- src/renderer/pages/downloads/downloads.tsx | 6 ++-- .../pages/game-details/hero-panel-actions.tsx | 11 +++--- 17 files changed, 115 insertions(+), 44 deletions(-) create mode 100644 src/main/events/library/remove-game-from-library.ts delete mode 100644 src/main/events/library/remove-game.ts rename src/main/events/{library => torrenting}/delete-game-folder.ts (89%) create mode 100644 src/main/events/torrenting/remove-game-from-download.ts diff --git a/src/main/entity/game.entity.ts b/src/main/entity/game.entity.ts index b1598b8b..811ecf74 100644 --- a/src/main/entity/game.entity.ts +++ b/src/main/entity/game.entity.ts @@ -61,6 +61,9 @@ export class Game { @JoinColumn() repack: Repack; + @Column("boolean", { default: false }) + isDeleted: boolean; + @CreateDateColumn() createdAt: Date; diff --git a/src/main/events/index.ts b/src/main/events/index.ts index 5eaceff5..6148bd8a 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -10,13 +10,13 @@ import "./catalogue/search-games"; import "./hardware/get-disk-free-space"; import "./library/add-game-to-library"; import "./library/close-game"; -import "./library/delete-game-folder"; +import "./torrenting/delete-game-folder"; import "./library/get-game-by-object-id"; import "./library/get-library"; import "./library/get-repackers-friendly-names"; import "./library/open-game"; import "./library/open-game-installer"; -import "./library/remove-game"; +import "./library/remove-game-from-library"; import "./misc/get-or-cache-image"; import "./misc/open-external"; import "./misc/show-open-dialog"; @@ -24,6 +24,7 @@ import "./torrenting/cancel-game-download"; import "./torrenting/pause-game-download"; import "./torrenting/resume-game-download"; import "./torrenting/start-game-download"; +import "./torrenting/remove-game-from-download"; import "./user-preferences/get-user-preferences"; import "./user-preferences/update-user-preferences"; diff --git a/src/main/events/library/add-game-to-library.ts b/src/main/events/library/add-game-to-library.ts index abbba592..ba528dbc 100644 --- a/src/main/events/library/add-game-to-library.ts +++ b/src/main/events/library/add-game-to-library.ts @@ -13,15 +13,34 @@ const addGameToLibrary = async ( gameShop: GameShop, executablePath: string ) => { - const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID)); - - return gameRepository.insert({ - title, - iconUrl, - objectID, - shop: gameShop, - executablePath, + const game = await gameRepository.findOne({ + where: { + objectID, + }, }); + + if (game) { + return gameRepository.update( + { + id: game.id, + }, + { + shop: gameShop, + executablePath, + isDeleted: false, + } + ); + } else { + const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID)); + + return gameRepository.insert({ + title, + iconUrl, + objectID, + shop: gameShop, + executablePath, + }); + } }; registerEvent(addGameToLibrary, { diff --git a/src/main/events/library/get-game-by-object-id.ts b/src/main/events/library/get-game-by-object-id.ts index 522d3bbb..0aa5976a 100644 --- a/src/main/events/library/get-game-by-object-id.ts +++ b/src/main/events/library/get-game-by-object-id.ts @@ -9,6 +9,7 @@ const getGameByObjectID = async ( gameRepository.findOne({ where: { objectID, + isDeleted: false, }, relations: { repack: true, diff --git a/src/main/events/library/get-library.ts b/src/main/events/library/get-library.ts index e22f508a..c86d1902 100644 --- a/src/main/events/library/get-library.ts +++ b/src/main/events/library/get-library.ts @@ -8,6 +8,9 @@ import sortBy from "lodash/sortBy"; const getLibrary = async (_event: Electron.IpcMainInvokeEvent) => gameRepository .find({ + where: { + isDeleted: false, + }, order: { createdAt: "desc", }, diff --git a/src/main/events/library/remove-game-from-library.ts b/src/main/events/library/remove-game-from-library.ts new file mode 100644 index 00000000..8767ee08 --- /dev/null +++ b/src/main/events/library/remove-game-from-library.ts @@ -0,0 +1,13 @@ +import { registerEvent } from "../register-event"; +import { gameRepository } from "../../repository"; + +const removeGameFromLibrary = async ( + _event: Electron.IpcMainInvokeEvent, + gameId: number +) => { + gameRepository.update({ id: gameId }, { isDeleted: true }); +}; + +registerEvent(removeGameFromLibrary, { + name: "removeGameFromLibrary", +}); diff --git a/src/main/events/library/remove-game.ts b/src/main/events/library/remove-game.ts deleted file mode 100644 index 64e84b7d..00000000 --- a/src/main/events/library/remove-game.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { registerEvent } from "../register-event"; -import { gameRepository } from "../../repository"; - -const removeGame = async ( - _event: Electron.IpcMainInvokeEvent, - gameId: number -) => gameRepository.delete({ id: gameId }); - -registerEvent(removeGame, { - name: "removeGame", -}); diff --git a/src/main/events/torrenting/cancel-game-download.ts b/src/main/events/torrenting/cancel-game-download.ts index 686033cc..a1a2e6b7 100644 --- a/src/main/events/torrenting/cancel-game-download.ts +++ b/src/main/events/torrenting/cancel-game-download.ts @@ -25,14 +25,13 @@ const cancelGameDownload = async ( if (!game) return; - gameRepository + await gameRepository .update( { id: game.id, }, { status: GameStatus.Cancelled, - downloadPath: null, bytesDownloaded: 0, progress: 0, } diff --git a/src/main/events/library/delete-game-folder.ts b/src/main/events/torrenting/delete-game-folder.ts similarity index 89% rename from src/main/events/library/delete-game-folder.ts rename to src/main/events/torrenting/delete-game-folder.ts index eaf791ed..c8821415 100644 --- a/src/main/events/library/delete-game-folder.ts +++ b/src/main/events/torrenting/delete-game-folder.ts @@ -22,7 +22,10 @@ const deleteGameFolder = async ( if (!game) return; if (game.folderName) { - const folderPath = path.join(await getDownloadsPath(), game.folderName); + const folderPath = path.join( + game.downloadPath ?? (await getDownloadsPath()), + game.folderName + ); if (fs.existsSync(folderPath)) { return new Promise((resolve, reject) => { diff --git a/src/main/events/torrenting/remove-game-from-download.ts b/src/main/events/torrenting/remove-game-from-download.ts new file mode 100644 index 00000000..47c1ebe6 --- /dev/null +++ b/src/main/events/torrenting/remove-game-from-download.ts @@ -0,0 +1,34 @@ +import { GameStatus } from "@main/constants"; +import { gameRepository } from "@main/repository"; + +import { registerEvent } from "../register-event"; + +const removeGameFromDownload = async ( + _event: Electron.IpcMainInvokeEvent, + gameId: number +) => { + const game = await gameRepository.findOne({ + where: { + id: gameId, + status: GameStatus.Cancelled, + }, + }); + + if (!game) return; + + gameRepository.update( + { + id: game.id, + }, + { + status: null, + downloadPath: null, + bytesDownloaded: 0, + progress: 0, + } + ); +}; + +registerEvent(removeGameFromDownload, { + name: "removeGameFromDownload", +}); diff --git a/src/main/events/torrenting/start-game-download.ts b/src/main/events/torrenting/start-game-download.ts index 570fd2ec..9319b555 100644 --- a/src/main/events/torrenting/start-game-download.ts +++ b/src/main/events/torrenting/start-game-download.ts @@ -59,6 +59,7 @@ const startGameDownload = async ( status: GameStatus.DownloadingMetadata, downloadPath: downloadsPath, repack: { id: repackId }, + isDeleted: false, } ); @@ -71,13 +72,6 @@ const startGameDownload = async ( game.status = GameStatus.DownloadingMetadata; - writePipe.write({ - action: "start", - game_id: game.id, - magnet: repack.magnet, - save_path: downloadsPath, - }); - return game; } else { const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID)); diff --git a/src/preload.ts b/src/preload.ts index 97dcca0b..fd8578f4 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -71,7 +71,10 @@ contextBridge.exposeInMainWorld("electron", { openGame: (gameId: number, executablePath: string) => ipcRenderer.invoke("openGame", gameId, executablePath), closeGame: (gameId: number) => ipcRenderer.invoke("closeGame", gameId), - removeGame: (gameId: number) => ipcRenderer.invoke("removeGame", gameId), + removeGameFromLibrary: (gameId: number) => + ipcRenderer.invoke("removeGameFromLibrary", gameId), + removeGameFromDownload: (gameId: number) => + ipcRenderer.invoke("removeGameFromDownload", gameId), deleteGameFolder: (gameId: number) => ipcRenderer.invoke("deleteGameFolder", gameId), getGameByObjectID: (objectID: string) => diff --git a/src/renderer/declaration.d.ts b/src/renderer/declaration.d.ts index 6fd533db..925ca7cf 100644 --- a/src/renderer/declaration.d.ts +++ b/src/renderer/declaration.d.ts @@ -62,7 +62,8 @@ declare global { openGameInstaller: (gameId: number) => Promise; openGame: (gameId: number, executablePath: string) => Promise; closeGame: (gameId: number) => Promise; - removeGame: (gameId: number) => Promise; + removeGameFromLibrary: (gameId: number) => Promise; + removeGameFromDownload: (gameId: number) => Promise; deleteGameFolder: (gameId: number) => Promise; getGameByObjectID: (objectID: string) => Promise; onPlaytime: (cb: (gameId: number) => void) => () => Electron.IpcRenderer; diff --git a/src/renderer/hooks/use-download.ts b/src/renderer/hooks/use-download.ts index 522dd2c2..427d6fde 100644 --- a/src/renderer/hooks/use-download.ts +++ b/src/renderer/hooks/use-download.ts @@ -57,8 +57,8 @@ export function useDownload() { deleteGame(gameId); }); - const removeGame = (gameId: number) => - window.electron.removeGame(gameId).then(() => { + const removeGameFromDownload = (gameId: number) => + window.electron.removeGameFromDownload(gameId).then(() => { updateLibrary(); }); @@ -123,7 +123,7 @@ export function useDownload() { pauseDownload, resumeDownload, cancelDownload, - removeGame, + removeGameFromDownload, deleteGame, isGameDeleting, clearDownload: () => dispatch(clearDownload()), diff --git a/src/renderer/hooks/use-library.ts b/src/renderer/hooks/use-library.ts index f7310df0..076b91a7 100644 --- a/src/renderer/hooks/use-library.ts +++ b/src/renderer/hooks/use-library.ts @@ -12,5 +12,10 @@ export function useLibrary() { .then((updatedLibrary) => dispatch(setLibrary(updatedLibrary))); }, [dispatch]); - return { library, updateLibrary }; + const removeGameFromLibrary = (gameId: number) => + window.electron.removeGameFromLibrary(gameId).then(() => { + updateLibrary(); + }); + + return { library, updateLibrary, removeGameFromLibrary }; } diff --git a/src/renderer/pages/downloads/downloads.tsx b/src/renderer/pages/downloads/downloads.tsx index aecf7b5e..c7268a5c 100644 --- a/src/renderer/pages/downloads/downloads.tsx +++ b/src/renderer/pages/downloads/downloads.tsx @@ -52,8 +52,8 @@ export function Downloads() { updateLibrary(); }); - const removeGame = (gameId: number) => - window.electron.removeGame(gameId).then(() => { + const removeGameFromDownload = (gameId: number) => + window.electron.removeGameFromDownload(gameId).then(() => { updateLibrary(); }); @@ -194,7 +194,7 @@ export function Downloads() {