[feat] handle edge cases on last version launched

This commit is contained in:
silentrald
2024-10-05 12:43:09 +08:00
parent 0a64e71cf8
commit 7af4c2ab45
6 changed files with 83 additions and 9 deletions
@@ -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)));
});
@@ -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<BSVersion[]>;
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<BSVersion[]>();
}
@@ -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<BSVersion[]>(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);
@@ -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<BSVersion[]> | null = null;
public readonly installedVersions$: BehaviorSubject<BSVersion[]> = new BehaviorSubject([]);
public readonly availableVersions$: BehaviorSubject<BSVersion[]> = 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<BSVersion[]> {
return lastValueFrom(this.ipcService.sendV2("bs-version.installed-versions")).then(res => {
this.setInstalledVersions(res);
return res;
});
public async askInstalledVersions(): Promise<BSVersion[]> {
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<boolean> {
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<BSVersion> {
@@ -27,4 +27,8 @@ export class StaticConfigurationService {
return lastValueFrom(this.ipc.sendV2("static-configuration.set", { key, value }));
}
public delete<K extends StaticConfigKeys>(key: K): Promise<void> {
return lastValueFrom(this.ipc.sendV2("static-configuration.delete", key));
}
}
+7
View File
@@ -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 });
};
+1
View File
@@ -156,6 +156,7 @@ export interface IpcChannelMapping {
/* ** static-configuration.ipcs ** */
"static-configuration.get": StaticConfigGetIpcRequestResponse<StaticConfigKeys>;
"static-configuration.set": StaticConfigSetIpcRequest<StaticConfigKeys>;
"static-configuration.delete": { request: StaticConfigKeys; response: void };
/* ** linux.ipcs ** */
"linux.verify-proton-folder": { request: void, response: boolean };