From 7af4c2ab4521e8accfeee07c29f9d171a82705c1 Mon Sep 17 00:00:00 2001 From: silentrald Date: Sat, 5 Oct 2024 12:43:09 +0800 Subject: [PATCH] [feat] handle edge cases on last version launched --- src/main/ipcs/static-configuration.ipcs.ts | 4 ++ src/main/services/bs-local-version.service.ts | 23 ++++++++ .../services/bs-version-manager.service.ts | 53 +++++++++++++++---- .../services/static-configuration.service.ts | 4 ++ src/renderer/windows/App.tsx | 7 +++ src/shared/models/ipc/ipc-routes.ts | 1 + 6 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/main/ipcs/static-configuration.ipcs.ts b/src/main/ipcs/static-configuration.ipcs.ts index d3dc7b93..43767db8 100644 --- a/src/main/ipcs/static-configuration.ipcs.ts +++ b/src/main/ipcs/static-configuration.ipcs.ts @@ -12,3 +12,7 @@ ipc.on("static-configuration.get", (args, reply) => { ipc.on("static-configuration.set", (args, reply) => { reply(from(staticConfig.set(args.key, args.value))); }); + +ipc.on("static-configuration.delete", (key, reply) => { + reply(of(staticConfig.delete(key))); +}); diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts index dcdd4e15..a224015c 100644 --- a/src/main/services/bs-local-version.service.ts +++ b/src/main/services/bs-local-version.service.ts @@ -18,6 +18,7 @@ import { Observable, Subject, catchError, finalize, from, map, switchMap, throwE import { BsStore } from "../../shared/models/bs-store.enum"; import { CustomError } from "../../shared/models/exceptions/custom-error.class"; import crypto from "crypto"; +import { StaticConfigurationService } from "./static-configuration.service"; export class BSLocalVersionService { @@ -32,6 +33,7 @@ export class BSLocalVersionService { private readonly remoteVersionService: BSVersionLibService; private readonly configService: ConfigurationService; private readonly linker: FolderLinkerService; + private readonly staticConfig: StaticConfigurationService; private readonly _loadedVersions$: Subject; public static getInstance(): BSLocalVersionService { @@ -48,6 +50,7 @@ export class BSLocalVersionService { this.remoteVersionService = BSVersionLibService.getInstance(); this.configService = ConfigurationService.getInstance(); this.linker = FolderLinkerService.getInstance(); + this.staticConfig = StaticConfigurationService.getInstance(); this._loadedVersions$ = new Subject(); } @@ -171,6 +174,24 @@ export class BSLocalVersionService { this.setCustomVersions([...this.getCustomVersions() ?? [], version]); } + private updateLastVersionLaunched(version: BSVersion, editedVersion: BSVersion): void { + const lastVersion = this.staticConfig.get("last-version-launched"); + if (!lastVersion) { + return; + } + + if ( + version.BSVersion !== lastVersion.BSVersion + || version.name !== lastVersion.name + || version.steam !== lastVersion.steam + || version.oculus !== lastVersion.oculus + ) { + return; + } + + this.staticConfig.set("last-version-launched", editedVersion); + } + private getCustomVersions(): BSVersion[]{ return this.configService.get(this.CUSTOM_VERSIONS_KEY) || []; } @@ -317,6 +338,7 @@ export class BSLocalVersionService { if(oldPath === newPath){ this.deleteCustomVersion(version); this.addCustomVersion(editedVersion); + this.updateLastVersionLaunched(version, editedVersion); return editedVersion; } @@ -327,6 +349,7 @@ export class BSLocalVersionService { return rename(oldPath, newPath).then(() => { this.deleteCustomVersion(version); this.addCustomVersion(editedVersion); + this.updateLastVersionLaunched(version, editedVersion); return editedVersion; }).catch((err: Error) => { log.error("edit version error", err, version, name, color); diff --git a/src/renderer/services/bs-version-manager.service.ts b/src/renderer/services/bs-version-manager.service.ts index 696e7d85..6d92ffa0 100644 --- a/src/renderer/services/bs-version-manager.service.ts +++ b/src/renderer/services/bs-version-manager.service.ts @@ -1,5 +1,5 @@ import { BSVersion } from "shared/bs-version.interface"; -import { BehaviorSubject, Observable, Subscription, lastValueFrom, shareReplay, throwError } from "rxjs"; +import { BehaviorSubject, Observable, Subscription, lastValueFrom, map, share, shareReplay, throwError } from "rxjs"; import { IpcService } from "./ipc.service"; import { ModalExitCode, ModalService } from "./modale.service"; import { NotificationService } from "./notification.service"; @@ -19,6 +19,7 @@ export class BSVersionManagerService { private readonly progressBar: ProgressBarService; private readonly modals: ModalService; + private askInstalledVersionsObserver$: Observable | null = null; public readonly installedVersions$: BehaviorSubject = new BehaviorSubject([]); public readonly availableVersions$: BehaviorSubject = new BehaviorSubject([]); @@ -28,7 +29,9 @@ export class BSVersionManagerService { this.notification = NotificationService.getInstance(); this.progressBar = ProgressBarService.getInstance(); this.modals = ModalService.getInstance(); - this.askAvailableVersions().then(() => this.askInstalledVersions()); + + this.askAvailableVersions(); + this.askInstalledVersions(); } public static getInstance() { @@ -54,15 +57,47 @@ export class BSVersionManagerService { }); } - public askInstalledVersions(): Promise { - return lastValueFrom(this.ipcService.sendV2("bs-version.installed-versions")).then(res => { - this.setInstalledVersions(res); - return res; - }); + public async askInstalledVersions(): Promise { + if (this.askInstalledVersionsObserver$) { + return lastValueFrom(this.askInstalledVersionsObserver$); + } + + this.askInstalledVersionsObserver$ = this.ipcService + .sendV2("bs-version.installed-versions") + .pipe( + map(versions => { + let processed = BSVersionManagerService.sortVersions(versions); + processed = BSVersionManagerService.removeDuplicateVersions(processed); + return processed; + }), + share(), + ); + + return lastValueFrom(this.askInstalledVersionsObserver$) + .then(versions => { + this.setInstalledVersions(versions); + return versions; + }) + .finally(() => { + this.askInstalledVersionsObserver$ = null; + }); } - public isVersionInstalled(version: BSVersion): boolean { - return !!this.getInstalledVersions().find(v => v.BSVersion === version.BSVersion && v.steam === version.steam && v.oculus === version.oculus); + public async isVersionInstalled(version: BSVersion): Promise { + try { + const versions: BSVersion[] = this.askInstalledVersionsObserver$ + ? await lastValueFrom(this.askInstalledVersionsObserver$) + : this.getInstalledVersions(); + + return !!versions.find(v => + v.BSVersion === version.BSVersion + && v.name === version.name + && v.steam === version.steam + && v.oculus === version.oculus + ); + } catch (error) { + return false; + } } public async editVersion(version: BSVersion): Promise { diff --git a/src/renderer/services/static-configuration.service.ts b/src/renderer/services/static-configuration.service.ts index 69d177a9..5cc93487 100644 --- a/src/renderer/services/static-configuration.service.ts +++ b/src/renderer/services/static-configuration.service.ts @@ -27,4 +27,8 @@ export class StaticConfigurationService { return lastValueFrom(this.ipc.sendV2("static-configuration.set", { key, value })); } + public delete(key: K): Promise { + return lastValueFrom(this.ipc.sendV2("static-configuration.delete", key)); + } + } diff --git a/src/renderer/windows/App.tsx b/src/renderer/windows/App.tsx index 83f70588..e49e56e7 100644 --- a/src/renderer/windows/App.tsx +++ b/src/renderer/windows/App.tsx @@ -24,6 +24,7 @@ import { OsDiagnosticService } from "renderer/services/os-diagnostic.service"; import { useService } from "renderer/hooks/use-service.hook"; import { SetupService } from "renderer/services/setup.service"; import { StaticConfigurationService } from "renderer/services/static-configuration.service"; +import { BSVersionManagerService } from "renderer/services/bs-version-manager.service"; export default function App() { @@ -36,6 +37,7 @@ export default function App() { const config = useService(ConfigurationService); const setup = useService(SetupService); const staticConfig = useService(StaticConfigurationService); + const versionManager = useService(BSVersionManagerService); const location = useLocation(); const navigate = useNavigate(); @@ -54,6 +56,11 @@ export default function App() { return; } + if (!await versionManager.isVersionInstalled(version)) { + await staticConfig.delete("last-version-launched"); + return; + } + navigate(`/bs-version/${version.BSVersion}`, { state: version }); }; diff --git a/src/shared/models/ipc/ipc-routes.ts b/src/shared/models/ipc/ipc-routes.ts index 950c2495..ce87b62d 100644 --- a/src/shared/models/ipc/ipc-routes.ts +++ b/src/shared/models/ipc/ipc-routes.ts @@ -156,6 +156,7 @@ export interface IpcChannelMapping { /* ** static-configuration.ipcs ** */ "static-configuration.get": StaticConfigGetIpcRequestResponse; "static-configuration.set": StaticConfigSetIpcRequest; + "static-configuration.delete": { request: StaticConfigKeys; response: void }; /* ** linux.ipcs ** */ "linux.verify-proton-folder": { request: void, response: boolean };