From 6bdf47678e9baeb32f4d83e2697744dc0f4aae9f Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Mon, 11 Sep 2023 21:49:43 +0200 Subject: [PATCH 1/2] [chore] Change default contents path to avoid OneDrive issues --- src/main/ipcs/bs-download-ipcs.ts | 2 +- .../local-maps-manager.service.ts | 2 +- .../local-models-manager.service.ts | 2 +- src/main/services/bs-installer.service.ts | 4 +- src/main/services/bs-local-version.service.ts | 8 ++-- src/main/services/configuration.service.ts | 4 +- src/main/services/folder-linker.service.ts | 12 ++--- .../services/installation-location.service.ts | 44 ++++++++++++------- 8 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/main/ipcs/bs-download-ipcs.ts b/src/main/ipcs/bs-download-ipcs.ts index e98db7ae..49f992e1 100644 --- a/src/main/ipcs/bs-download-ipcs.ts +++ b/src/main/ipcs/bs-download-ipcs.ts @@ -12,7 +12,7 @@ ipc.on("is-dotnet-6-installed", (_, reply) => { ipc.on("bs-download.installation-folder", (_, reply) => { const installLocation = InstallationLocationService.getInstance(); - reply(of(installLocation.installationDirectory)); + reply(from(installLocation.installationDirectory())); }); ipc.on("bs-download.set-installation-folder", (req, reply) => { diff --git a/src/main/services/additional-content/local-maps-manager.service.ts b/src/main/services/additional-content/local-maps-manager.service.ts index c37dfa60..c50aa522 100644 --- a/src/main/services/additional-content/local-maps-manager.service.ts +++ b/src/main/services/additional-content/local-maps-manager.service.ts @@ -75,7 +75,7 @@ export class LocalMapsManagerService { if (version) { return path.join(await this.localVersion.getVersionPath(version), LocalMapsManagerService.LEVELS_ROOT_FOLDER, LocalMapsManagerService.CUSTOM_LEVELS_FOLDER); } - const sharedMapsPath = path.join(this.installLocation.sharedContentPath, LocalMapsManagerService.SHARED_MAPS_FOLDER, LocalMapsManagerService.CUSTOM_LEVELS_FOLDER); + const sharedMapsPath = path.join(await this.installLocation.sharedContentPath(), LocalMapsManagerService.SHARED_MAPS_FOLDER, LocalMapsManagerService.CUSTOM_LEVELS_FOLDER); if (!(await pathExist(sharedMapsPath))) { await ensureFolderExist(sharedMapsPath); } diff --git a/src/main/services/additional-content/local-models-manager.service.ts b/src/main/services/additional-content/local-models-manager.service.ts index 18a814db..bdaead49 100644 --- a/src/main/services/additional-content/local-models-manager.service.ts +++ b/src/main/services/additional-content/local-models-manager.service.ts @@ -73,7 +73,7 @@ export class LocalModelsManagerService { } private async getModelFolderPath(type: MSModelType, version?: BSVersion): Promise { - const rootPath = version ? await this.localVersion.getVersionPath(version) : this.installPaths.sharedContentPath; + const rootPath = await (version ? this.localVersion.getVersionPath(version) : this.installPaths.sharedContentPath()); const modelFolderPath = path.join(rootPath, MODEL_TYPE_FOLDERS[type]); await ensureFolderExist(modelFolderPath); diff --git a/src/main/services/bs-installer.service.ts b/src/main/services/bs-installer.service.ts index 162497d4..160da5db 100644 --- a/src/main/services/bs-installer.service.ts +++ b/src/main/services/bs-installer.service.ts @@ -79,7 +79,7 @@ export class BSInstallerService { qr } - await ensureDir(this.installLocationService.versionsDirectory); + await ensureDir(await this.installLocationService.versionsDirectory()); const isLinux = process.platform === 'linux'; const exePath = this.getDepotDownloaderExePath(); @@ -88,7 +88,7 @@ export class BSInstallerService { return new DepotDownloader({ command: isLinux ? 'dotnet' : exePath, args: isLinux ? [exePath, ...args] : args, - options: { cwd: this.installLocationService.versionsDirectory }, + options: { cwd: await this.installLocationService.versionsDirectory() }, echoStartData: downloadVersion }, log); } diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts index 57871f5e..7966d443 100644 --- a/src/main/services/bs-local-version.service.ts +++ b/src/main/services/bs-local-version.service.ts @@ -142,7 +142,7 @@ export class BSLocalVersionService { if(version.oculus){ return this.oculusService.getGameFolder(OCULUS_BS_DIR); } return path.join( - this.installLocationService.versionsDirectory, + await this.installLocationService.versionsDirectory(), this.getVersionFolder(version) ); } @@ -156,7 +156,7 @@ export class BSLocalVersionService { const versionPath = await this.getVersionPath(version); if(await pathExist(versionPath)){ return versionPath; } - const versionFolders = await getFoldersInFolder(this.installLocationService.versionsDirectory); + const versionFolders = await getFoldersInFolder(await this.installLocationService.versionsDirectory()); for(const folder of versionFolders){ const stats = await lstat(folder); @@ -215,11 +215,11 @@ export class BSLocalVersionService { versions.push(oculusVersion); } - if (!(await pathExist(this.installLocationService.versionsDirectory))) { + if (!(await pathExist(await this.installLocationService.versionsDirectory()))) { return versions; } - const folderInInstallation = await getFoldersInFolder(this.installLocationService.versionsDirectory); + const folderInInstallation = await getFoldersInFolder(await this.installLocationService.versionsDirectory()); for (const f of folderInInstallation) { log.info("try get version from folder", f); diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index a4eb5d44..29b22c6b 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -22,8 +22,8 @@ export class ConfigurationService { this.locations.onInstallLocationUpdate(() => this.initStore()); } - private initStore() { - const contentPath = this.locations.installationDirectory; + private async initStore() { + const contentPath = await this.locations.installationDirectory(); this.store = new ElectronStore({ cwd: contentPath, name: "config", diff --git a/src/main/services/folder-linker.service.ts b/src/main/services/folder-linker.service.ts index f8460c73..99316a21 100644 --- a/src/main/services/folder-linker.service.ts +++ b/src/main/services/folder-linker.service.ts @@ -21,12 +21,12 @@ export class FolderLinkerService { this.installLocationService = InstallationLocationService.getInstance(); } - private get sharedFolder(): string { - return this.installLocationService.sharedContentPath; + private async sharedFolder(): Promise { + return this.installLocationService.sharedContentPath(); } - private getSharedFolder(folderPath: string, intermediateFolder?: string): string { - return path.join(this.sharedFolder, intermediateFolder ?? "", path.basename(folderPath)); + private async getSharedFolder(folderPath: string, intermediateFolder?: string): Promise { + return path.join(await this.sharedFolder(), intermediateFolder ?? "", path.basename(folderPath)); } private getBackupFolder(folderPath: string): string { @@ -50,7 +50,7 @@ export class FolderLinkerService { } public async linkFolder(folderPath: string, options?: LinkOptions): Promise { - const sharedPath = this.getSharedFolder(folderPath, options?.intermediateFolder); + const sharedPath = await this.getSharedFolder(folderPath, options?.intermediateFolder); if (await this.isFolderSymlink(folderPath)) { const isTargetedToSharedPath = await readlink(folderPath) @@ -86,7 +86,7 @@ export class FolderLinkerService { } await unlinkPath(folderPath); - const sharedPath = this.getSharedFolder(folderPath, options?.intermediateFolder); + const sharedPath = await this.getSharedFolder(folderPath, options?.intermediateFolder); await ensureFolderExist(folderPath); diff --git a/src/main/services/installation-location.service.ts b/src/main/services/installation-location.service.ts index 678973ed..5d35f5b2 100644 --- a/src/main/services/installation-location.service.ts +++ b/src/main/services/installation-location.service.ts @@ -1,7 +1,7 @@ import path from "path"; import { app } from "electron"; import ElectronStore from "electron-store"; -import { copyDirectoryWithJunctions, deleteFolder, ensureFolderExist } from "../helpers/fs.helpers"; +import { copyDirectoryWithJunctions, deleteFolder, ensureFolderExist, pathExist } from "../helpers/fs.helpers"; export class InstallationLocationService { private static instance: InstallationLocationService; @@ -27,51 +27,61 @@ export class InstallationLocationService { private constructor() { this.installPathConfig = new ElectronStore({ watch: true }); - this.initInstallationLocation(); this.installPathConfig.onDidChange(this.STORE_INSTALLATION_PATH_KEY, () => { this.triggerListeners(); }); } - private initInstallationLocation(): void { - this._installationDirectory = (this.installPathConfig.get(this.STORE_INSTALLATION_PATH_KEY) as string) || app.getPath("documents"); - } - private triggerListeners(): void { this.updateListeners.forEach(listener => listener()); } public async setInstallationDirectory(newDir: string): Promise { - const oldDir = this.installationDirectory; - const newDest = path.join(newDir, this.INSTALLATION_FOLDER); + newDir = path.basename(newDir) === this.INSTALLATION_FOLDER ? newDir : path.join(newDir, this.INSTALLATION_FOLDER); + const oldDir = await this.installationDirectory(); await ensureFolderExist(oldDir); - - await copyDirectoryWithJunctions(oldDir, newDest, { overwrite: true }); + await copyDirectoryWithJunctions(oldDir, newDir, { overwrite: true }); this._installationDirectory = newDir; this.installPathConfig.set(this.STORE_INSTALLATION_PATH_KEY, newDir); deleteFolder(oldDir); - return this.installationDirectory; + return this.installationDirectory(); } public onInstallLocationUpdate(fn: Listener) { this.updateListeners.add(fn); } - public get installationDirectory(): string { - return path.join(this._installationDirectory, this.INSTALLATION_FOLDER); + public async installationDirectory(): Promise { + if(this._installationDirectory) { + return this._installationDirectory; + } + + if(this.installPathConfig.has(this.STORE_INSTALLATION_PATH_KEY)) { + this._installationDirectory = this.installPathConfig.get(this.STORE_INSTALLATION_PATH_KEY) as string; + return this._installationDirectory; + } + + const oldPath = path.join(app.getPath("documents"), this.INSTALLATION_FOLDER); + if(await pathExist(oldPath)){ + this._installationDirectory = oldPath; + return this._installationDirectory; + } + + this._installationDirectory = path.join(app.getPath("home"), this.INSTALLATION_FOLDER); + return this._installationDirectory; } - public get versionsDirectory(): string { - return path.join(this.installationDirectory, this.VERSIONS_FOLDER); + public async versionsDirectory(): Promise { + return path.join(await this.installationDirectory(), this.VERSIONS_FOLDER); } - public get sharedContentPath(): string { - return path.join(this.installationDirectory, this.SHARED_CONTENT_FOLDER); + public async sharedContentPath(): Promise { + return path.join(await this.installationDirectory(), this.SHARED_CONTENT_FOLDER); } } From 2e376b03fc6ecfffead22707e532b68584f737ec Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Mon, 11 Sep 2023 21:53:58 +0200 Subject: [PATCH 2/2] [chore] fix sonarcloud bug --- src/main/services/configuration.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index 29b22c6b..bc9713b0 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -19,7 +19,7 @@ export class ConfigurationService { this.locations = InstallationLocationService.getInstance(); this.initStore(); - this.locations.onInstallLocationUpdate(() => this.initStore()); + this.locations.onInstallLocationUpdate(() => { this.initStore() }); } private async initStore() {