From e203fb603d80927c6df562130886a95d28caa252 Mon Sep 17 00:00:00 2001 From: MathieuG-P Date: Sun, 17 Jul 2022 17:42:21 +0200 Subject: [PATCH] split installerService into two and no need for uninstallerService now --- src/main/ipcs/bs-uninstall-ipcs.ts | 6 +- src/main/ipcs/bs-version-ipcs.ts | 25 ++---- src/main/services/bs-installer.service.ts | 43 ++-------- src/main/services/bs-local-version.service.ts | 86 +++++++++++++++++++ src/main/services/bs-uninstaller.service.ts | 33 ------- .../services/installation-location.service.ts | 6 +- .../services/bs-version-manager.service.ts | 2 +- 7 files changed, 107 insertions(+), 94 deletions(-) create mode 100644 src/main/services/bs-local-version.service.ts delete mode 100644 src/main/services/bs-uninstaller.service.ts diff --git a/src/main/ipcs/bs-uninstall-ipcs.ts b/src/main/ipcs/bs-uninstall-ipcs.ts index 386c5b8b..09a9ec3b 100644 --- a/src/main/ipcs/bs-uninstall-ipcs.ts +++ b/src/main/ipcs/bs-uninstall-ipcs.ts @@ -1,16 +1,16 @@ import { ipcMain } from "electron"; -import { BSUninstallerService } from "../services/bs-uninstaller.service"; import { BSVersion } from "main/services/bs-version-manager.service"; import { UtilsService } from "../services/utils.service"; import { IpcRequest } from "shared/models/ipc-models.model"; +import { BSLocalVersionService } from "../services/bs-local-version.service"; ipcMain.on('bs.uninstall', async (event, request: IpcRequest) => { - const bsUninstallerService = BSUninstallerService.getInstance(); + const bsLocalVersionService = BSLocalVersionService.getInstance(); const utilsService = UtilsService.getInstance(); if(!request.args){ utilsService.newIpcSenc(request.responceChannel, {success: false}); } - const res = await bsUninstallerService.uninstall(request.args); + const res = await bsLocalVersionService.deleteVersion(request.args); utilsService.newIpcSenc(request.responceChannel, {success: res}); }); \ No newline at end of file diff --git a/src/main/ipcs/bs-version-ipcs.ts b/src/main/ipcs/bs-version-ipcs.ts index 23914528..a89cb3e0 100644 --- a/src/main/ipcs/bs-version-ipcs.ts +++ b/src/main/ipcs/bs-version-ipcs.ts @@ -1,6 +1,5 @@ import { ipcMain } from 'electron'; import { UtilsService } from '../services/utils.service'; -import { BSInstallerService } from '../services/bs-installer.service'; import { BSVersionManagerService } from '../services/bs-version-manager.service' import { BSVersion } from "main/services/bs-version-manager.service"; import path from 'path'; @@ -8,8 +7,10 @@ import { exec } from 'child_process'; import { SteamService } from '../services/steam.service'; import { BS_APP_ID } from '../constants'; import { IpcRequest } from 'shared/models/ipc-models.model'; +import { BSLocalVersionService } from '../services/bs-local-version.service'; +import { InstallationLocationService } from '../services/installation-location.service'; -ipcMain.on('bs-version.request-versions', (event, req: IpcRequest) => { +ipcMain.on('bs-version.get-version-dict', (event, req: IpcRequest) => { BSVersionManagerService.getInstance().getAvailableVersions().then(versions => { UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: true, data: versions}); }).catch(() => { @@ -18,7 +19,7 @@ ipcMain.on('bs-version.request-versions', (event, req: IpcRequest) => { }); ipcMain.on('bs-version.installed-versions', async (event, req: IpcRequest) => { - BSInstallerService.getInstance().getInstalledBsVersion().then(versions => { + BSLocalVersionService.getInstance().getInstalledVersions().then(versions => { UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: true, data: versions}); }).catch(() => { UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: false}); @@ -26,17 +27,7 @@ ipcMain.on('bs-version.installed-versions', async (event, req: IpcRequest) }); ipcMain.on("bs-version.open-folder", async (event, args: BSVersion) => { - const bsInstallerService = BSInstallerService.getInstance(); - let versionFolder = "" - if(args.steam){ - const steamService = SteamService.getInstance(); - versionFolder = await steamService.getGameFolder(BS_APP_ID, "Beat Saber"); - console.log(versionFolder) - } - else{ - versionFolder = path.join(bsInstallerService.installationFolder, args.BSVersion); - } - if(UtilsService.getInstance().folderExist(versionFolder)){ - exec(`start "" "${versionFolder}"`); - } -}) + const locationService = InstallationLocationService.getInstance(); + const versionFolder = args.steam ? await SteamService.getInstance().getGameFolder(BS_APP_ID, "Beat Saber") : path.join(locationService.versionsDirectory, args.BSVersion); + UtilsService.getInstance().folderExist(versionFolder) && exec(`start "" "${versionFolder}"`); +}); diff --git a/src/main/services/bs-installer.service.ts b/src/main/services/bs-installer.service.ts index 4a22a7ec..c98e4634 100644 --- a/src/main/services/bs-installer.service.ts +++ b/src/main/services/bs-installer.service.ts @@ -1,7 +1,6 @@ import { BS_APP_ID, BS_DEPOT } from "../constants"; import path from "path"; import { BSVersion, BSVersionManagerService } from "./bs-version-manager.service"; -import { SteamService } from "./steam.service"; import { UtilsService } from "./utils.service"; import { ChildProcessWithoutNullStreams, spawn } from "child_process"; import log from "electron-log"; @@ -10,12 +9,9 @@ import { ctrlc } from "ctrlc-windows"; export class BSInstallerService{ - private static readonly INSTALLATION_FOLDER = "BSInstances"; - private static instance: BSInstallerService; private readonly utils: UtilsService; - private readonly steamService: SteamService; private readonly bsVersionService: BSVersionManagerService; private readonly installLocationService: InstallationLocationService; @@ -24,14 +20,9 @@ export class BSInstallerService{ private constructor(){ this.bsVersionService = BSVersionManagerService.getInstance(); this.utils = UtilsService.getInstance(); - this.steamService = SteamService.getInstance(); this.installLocationService = InstallationLocationService.getInstance(); } - public get installationFolder(): string{ - return path.join(this.installLocationService.installationDirectory, BSInstallerService.INSTALLATION_FOLDER); - } - public static getInstance(){ if(!BSInstallerService.instance){ BSInstallerService.instance = new BSInstallerService(); } return BSInstallerService.instance; @@ -41,27 +32,10 @@ export class BSInstallerService{ return path.join(this.utils.getAssetsScriptsPath(), 'depot-downloader', 'DepotDownloader.exe'); } - public async getInstalledBsVersion(): Promise{ - const versions: BSVersion[] = []; - const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber") - if(steamBsFolder && this.utils.pathExist(steamBsFolder)){ - const steamBsVersion = await this.bsVersionService.getVersionOfBSFolder(steamBsFolder); - if(steamBsVersion){ - const steamVersionDetails = this.bsVersionService.getVersionDetailFromVersionNumber(steamBsVersion); - versions.push(steamVersionDetails ? {...steamVersionDetails, steam: true} : {BSVersion: steamBsVersion, steam: true}); - } - } - - if(!this.utils.folderExist(this.installationFolder)){ return versions } - - const folderInInstallation = this.utils.listDirsInDir(this.installationFolder); - - folderInInstallation.forEach(f => { - const version = this.bsVersionService.getVersionDetailFromVersionNumber(f); - versions.push(version); - }) - return versions; - } + private sendDownloadEvent(event: DownloadEventType, data?: string|number, success: boolean = true): void{ + if(typeof data === "string"){ data = data.replaceAll(/\[|\]/g, ""); } + this.utils.newIpcSenc(`bs-download.${event}`, { success: success, data: data }); + } public sendInputProcess(input: string){ if(this.downloadProcess.stdin.writable){ @@ -86,7 +60,7 @@ export class BSInstallerService{ const bsVersion = this.bsVersionService.getVersionDetailFromVersionNumber(downloadInfos.bsVersion.BSVersion); if(!bsVersion){ return {type: "[Error]"}; } - this.utils.createFolderIfNotExist(this.installationFolder); + this.utils.createFolderIfNotExist(this.installLocationService.versionsDirectory); this.downloadProcess = spawn( this.getDepotDownloaderExePath(), [ @@ -97,7 +71,7 @@ export class BSInstallerService{ `-dir ${bsVersion.BSVersion}`, (downloadInfos.stay || !downloadInfos.password) && "-remember-password" ], - {shell: true, cwd: this.installationFolder} + {shell: true, cwd: this.installLocationService.versionsDirectory} ); return await new Promise((resolve, reject) => { @@ -162,11 +136,6 @@ export class BSInstallerService{ }) } - private sendDownloadEvent(event: DownloadEventType, data?: string|number, success: boolean = true): void{ - if(typeof data === "string"){ data = data.replaceAll(/\[|\]/g, ""); } - this.utils.newIpcSenc(`bs-download.${event}`, { success: success, data: data }); - } - } diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts new file mode 100644 index 00000000..30a494d8 --- /dev/null +++ b/src/main/services/bs-local-version.service.ts @@ -0,0 +1,86 @@ +import { BSVersion, BSVersionManagerService } from "./bs-version-manager.service"; +import { InstallationLocationService } from "./installation-location.service"; +import { SteamService } from "./steam.service"; +import { UtilsService } from "./utils.service"; +import { BS_APP_ID } from "../constants"; +import path from "path"; +import { createInterface } from "readline"; +import { createReadStream } from "fs"; + +export class BSLocalVersionService{ + + private static instance: BSLocalVersionService; + + private readonly installLocationService: InstallationLocationService; + private readonly utilsService: UtilsService; + private readonly steamService: SteamService; + private readonly remoteVersionService: BSVersionManagerService; + + public static getInstance(): BSLocalVersionService{ + if(!BSLocalVersionService.instance){ BSLocalVersionService.instance = new BSLocalVersionService(); } + return BSLocalVersionService.instance; + } + + private constructor(){ + this.installLocationService = InstallationLocationService.getInstance(); + this.utilsService = UtilsService.getInstance(); + this.steamService = SteamService.getInstance(); + this.remoteVersionService = BSVersionManagerService.getInstance(); + } + + private async getVersionOfBSFolder(bsPath: string): Promise{ + const versionFilePath = path.join(bsPath, 'Beat Saber_Data', 'globalgamemanagers'); + if(!this.utilsService.pathExist(versionFilePath)){ return null; } + const versionsAvailable = await this.remoteVersionService.getAvailableVersions(); + return new Promise(async (resolve, reject) => { + const readLine = createInterface({ input: createReadStream(versionFilePath) }); + let findVersion: string = null; + readLine.on('line', (line) => { + for(const version of versionsAvailable){ + if(line.includes(version.BSVersion)){ findVersion = version.BSVersion; readLine.close(); } + } + }); + readLine.on('close', () => { + if(findVersion){ resolve(findVersion) } + resolve(findVersion); + }); + }) + } + + public async getInstalledVersions(): Promise{ + const versions: BSVersion[] = []; + const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber") + if(steamBsFolder && this.utilsService.pathExist(steamBsFolder)){ + const steamBsVersion = await this.getVersionOfBSFolder(steamBsFolder); + if(steamBsVersion){ + const steamVersionDetails = this.remoteVersionService.getVersionDetailFromVersionNumber(steamBsVersion); + versions.push(steamVersionDetails ? {...steamVersionDetails, steam: true} : {BSVersion: steamBsVersion, steam: true}); + } + } + + if(!this.utilsService.folderExist(this.installLocationService.versionsDirectory)){ return versions } + + const folderInInstallation = this.utilsService.listDirsInDir(this.installLocationService.versionsDirectory); + + folderInInstallation.forEach(f => { + const version = this.remoteVersionService.getVersionDetailFromVersionNumber(f); + versions.push(version); + }) + return versions; + } + + public async deleteVersion(version: BSVersion): Promise{ + if(version.steam){ return false; } + const versionFolder = path.join(this.installLocationService.versionsDirectory, version.BSVersion); + if(!this.utilsService.folderExist(versionFolder)){ return true; } + + return this.utilsService.deleteFolder(versionFolder) + .then(() => { return true; }) + .catch(() => { return false; }) + } + + public cloneVersion(version: BSVersion, name: string): boolean{ + return true; + } + +} \ No newline at end of file diff --git a/src/main/services/bs-uninstaller.service.ts b/src/main/services/bs-uninstaller.service.ts deleted file mode 100644 index 7ca0d1c8..00000000 --- a/src/main/services/bs-uninstaller.service.ts +++ /dev/null @@ -1,33 +0,0 @@ -import path from "path"; -import { BSInstallerService } from "./bs-installer.service"; -import { BSVersion } from "./bs-version-manager.service"; -import { UtilsService } from "./utils.service"; - -export class BSUninstallerService { - - private static instance: BSUninstallerService; - - private readonly utilsService: UtilsService; - private readonly bsInstallerService: BSInstallerService; - - public static getInstance(): BSUninstallerService { - if(!BSUninstallerService.instance){ BSUninstallerService.instance = new BSUninstallerService(); } - return BSUninstallerService.instance; - } - - private constructor(){ - this.utilsService = UtilsService.getInstance(); - this.bsInstallerService = BSInstallerService.getInstance(); - } - - public async uninstall(version :BSVersion): Promise{ - if(version.steam){ return false; } - const versionFolder = path.join(this.bsInstallerService.installationFolder, version.BSVersion); - if(!this.utilsService.folderExist(versionFolder)){ return true; } - - return this.utilsService.deleteFolder(versionFolder) - .then(() => { return true; }) - .catch(() => { return false; }) - } - -} \ No newline at end of file diff --git a/src/main/services/installation-location.service.ts b/src/main/services/installation-location.service.ts index 79f9c906..1fca3a74 100644 --- a/src/main/services/installation-location.service.ts +++ b/src/main/services/installation-location.service.ts @@ -9,6 +9,7 @@ export class InstallationLocationService { private static instance: InstallationLocationService; private readonly INSTALLATION_FOLDER = "BSManager"; + private readonly VERSIONS_FOLDER = "BSInstances"; private readonly STORE_INSTALLATION_PATH_KEY = "installation-folder"; @@ -33,9 +34,8 @@ export class InstallationLocationService { this._installationDirectory = (this.configService.get(this.STORE_INSTALLATION_PATH_KEY) || this.utilsService.getUserDocumentsFolder()) as string; } - public get installationDirectory(): string{ - return path.join(this._installationDirectory, this.INSTALLATION_FOLDER); - } + public get installationDirectory(): string{ return path.join(this._installationDirectory, this.INSTALLATION_FOLDER); } + public get versionsDirectory(): string { return path.join(this.installationDirectory, this.VERSIONS_FOLDER); } public setInstallationDirectory(newDir: string): Promise{ const oldDir = this.installationDirectory; diff --git a/src/renderer/services/bs-version-manager.service.ts b/src/renderer/services/bs-version-manager.service.ts index 8d90eb80..d3b5e3c0 100644 --- a/src/renderer/services/bs-version-manager.service.ts +++ b/src/renderer/services/bs-version-manager.service.ts @@ -37,7 +37,7 @@ export class BSVersionManagerService { } public askAvailableVersions(): void{ - this.ipcService.send("bs-version.request-versions").then(res => this.availableVersions$.next(res.data)); + this.ipcService.send("bs-version.get-version-dict").then(res => this.availableVersions$.next(res.data)); } public askInstalledVersions(): void{