From 2d7b1bd8f3f40ac47faf306fa3ca0700d51890c5 Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Sun, 16 Jul 2023 15:10:00 +0200 Subject: [PATCH] [chore-245] clean "setInstallationDirectory" process --- src/main/ipcs/bs-download-ipcs.ts | 16 +++----- .../services/installation-location.service.ts | 22 ++++------ .../pages/settings-page.component.tsx | 40 +++++++++---------- .../services/bs-downloader.service.ts | 6 +-- 4 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/main/ipcs/bs-download-ipcs.ts b/src/main/ipcs/bs-download-ipcs.ts index d48c277c..04074058 100644 --- a/src/main/ipcs/bs-download-ipcs.ts +++ b/src/main/ipcs/bs-download-ipcs.ts @@ -3,8 +3,9 @@ import { BSInstallerService, DownloadEventType, DownloadInfo } from "../services import { IpcRequest } from "shared/models/ipc"; import { InstallationLocationService } from "../services/installation-location.service"; import { UtilsService } from "../services/utils.service"; -import { BsmException } from "shared/models/bsm-exception.model"; import { LocalMapsManagerService } from "../services/additional-content/local-maps-manager.service"; +import { IpcService } from "../services/ipc.service"; +import { from } from "rxjs"; export interface InitDownloadInfoInterface { cwd: string; @@ -16,6 +17,8 @@ export interface InitDownloadInfoInterface { stay: boolean; } +const ipc = IpcService.getInstance(); + ipcMain.on("is-dotnet-6-installed", async (event, request: IpcRequest) => { const installer = BSInstallerService.getInstance(); const utils = UtilsService.getInstance(); @@ -54,16 +57,9 @@ ipcMain.on("bs-download.installation-folder", async (event, request: IpcRequest< UtilsService.getInstance().ipcSend(request.responceChannel, { success: true, data: installationFolder }); }); -ipcMain.on("bs-download.set-installation-folder", (event, request: IpcRequest) => { +ipc.on("bs-download.set-installation-folder", (req, reply) => { const installerService = InstallationLocationService.getInstance(); - installerService - .setInstallationDirectory(request.args) - .then(res => { - UtilsService.getInstance().ipcSend(request.responceChannel, { success: true, data: res }); - }) - .catch((err: BsmException) => { - UtilsService.getInstance().ipcSend(request.responceChannel, { success: false, error: err }); - }); + reply(from(installerService.setInstallationDirectory(req.args))); }); ipcMain.on("bs-download.import-version", (event, request: IpcRequest) => { diff --git a/src/main/services/installation-location.service.ts b/src/main/services/installation-location.service.ts index 797c3e14..e8ae6eab 100644 --- a/src/main/services/installation-location.service.ts +++ b/src/main/services/installation-location.service.ts @@ -43,26 +43,20 @@ export class InstallationLocationService { this.updateListeners.forEach(listener => listener()); } - public setInstallationDirectory(newDir: string): Promise { + public async setInstallationDirectory(newDir: string): Promise { const oldDir = this.installationDirectory; const newDest = path.join(newDir, this.INSTALLATION_FOLDER); - return new Promise(async (resolve, reject) => { - await ensureFolderExist(oldDir); - try { - await copyDirectoryWithJunctions(oldDir, newDest, { overwrite: true }); + await ensureFolderExist(oldDir); - this._installationDirectory = newDir; - this.installPathConfig.set(this.STORE_INSTALLATION_PATH_KEY, newDir); + await copyDirectoryWithJunctions(oldDir, newDest, { overwrite: true }); - deleteFolder(oldDir); + this._installationDirectory = newDir; + this.installPathConfig.set(this.STORE_INSTALLATION_PATH_KEY, newDir); - return resolve(this.installationDirectory); - } catch (err) { - log.error(err); - reject(err); - } - }); + deleteFolder(oldDir); + + return this.installationDirectory; } public onInstallLocationUpdate(fn: Listener) { diff --git a/src/renderer/pages/settings-page.component.tsx b/src/renderer/pages/settings-page.component.tsx index 0bbf60ce..3650d1d1 100644 --- a/src/renderer/pages/settings-page.component.tsx +++ b/src/renderer/pages/settings-page.component.tsx @@ -32,6 +32,8 @@ import { ModelsManagerService } from "renderer/services/models-management/models import { useTranslation } from "renderer/hooks/use-translation.hook"; import { VersionFolderLinkerService } from "renderer/services/version-folder-linker.service"; import { useService } from "renderer/hooks/use-service.hook"; +import { lastValueFrom } from "rxjs"; +import { BsmException } from "shared/models/bsm-exception.model"; export function SettingsPage() { @@ -125,31 +127,29 @@ export function SettingsPage() { notificationService.notifySuccess({ title: "notifications.settings.move-folder.success.titles.transfer-started", desc: "notifications.settings.move-folder.success.descs.transfer-started" }); - downloaderService.setInstallationFolder(fileChooserRes.filePaths[0]).then(async res => { - setTimeout(() => { - progressBarService.complete(); - setTimeout(() => progressBarService.hide(true), 1000); - }, 1000); + lastValueFrom(downloaderService.setInstallationFolder(fileChooserRes.filePaths[0])).then(res => { - if (res.success) { - setInstallationFolder(res.data); + progressBarService.complete(); + progressBarService.hide(true); - notificationService.notifySuccess({ title: "notifications.settings.move-folder.success.titles.transfer-finished", duration: 3000 }); + setInstallationFolder(res); - versionLinker - .relinkAllVersionsFolders() - .toPromise() - .catch(() => { - notificationService.notifyError({ title: "notifications.types.error", desc: "notifications.settings.move-folder.errors.descs.restore-linked-folders", duration: 15_000 }); - }); - } else { - if (res?.error?.code === "COPY_TO_SUBPATH") { - notificationService.notifyError({ title: "notifications.settings.move-folder.errors.titles.transfer-failed", desc: "notifications.settings.move-folder.errors.descs.COPY_TO_SUBPATH", duration: 10_000 }); - return; - } + notificationService.notifySuccess({ title: "notifications.settings.move-folder.success.titles.transfer-finished", duration: 3000 }); - notificationService.notifyError({ title: "notifications.settings.move-folder.errors.titles.transfer-failed" }); + // Restore links of external BS versions (steam, oculus, etc.) + lastValueFrom(versionLinker.relinkAllVersionsFolders()).catch(() => { + notificationService.notifyError({ title: "notifications.types.error", desc: "notifications.settings.move-folder.errors.descs.restore-linked-folders", duration: 15_000 }); + }); + + }).catch((err: BsmException) => { + progressBarService.hide(true); + + if (err?.code === "COPY_TO_SUBPATH") { + notificationService.notifyError({ title: "notifications.settings.move-folder.errors.titles.transfer-failed", desc: "notifications.settings.move-folder.errors.descs.COPY_TO_SUBPATH", duration: 10_000 }); + return; } + + notificationService.notifyError({ title: "notifications.settings.move-folder.errors.titles.transfer-failed" }); }); } }); diff --git a/src/renderer/services/bs-downloader.service.ts b/src/renderer/services/bs-downloader.service.ts index bd4bd379..10d2b730 100644 --- a/src/renderer/services/bs-downloader.service.ts +++ b/src/renderer/services/bs-downloader.service.ts @@ -1,5 +1,5 @@ import { DownloadEvent, DownloadInfo } from "main/services/bs-installer.service"; -import { BehaviorSubject } from "rxjs"; +import { BehaviorSubject, Observable } from "rxjs"; import { distinctUntilChanged, filter, throttleTime } from "rxjs/operators"; import { IpcResponse } from "shared/models/ipc"; import { BSVersion } from "shared/bs-version.interface"; @@ -197,8 +197,8 @@ export class BsDownloaderService { return this._isVerification; } - public setInstallationFolder(path: string): Promise> { - return this.ipcService.send("bs-download.set-installation-folder", { args: path }); + public setInstallationFolder(path: string): Observable { + return this.ipcService.sendV2("bs-download.set-installation-folder", { args: path }); } public async importVersion(pathToImport: string): Promise {