Merge pull request #355 from Zagrios/bugfix/prevent-being-able-to-launch-currently-downloading-version

Bugfix/prevent being able to launch currently downloading version
This commit is contained in:
MathieuG-P
2023-11-17 21:56:32 +01:00
committed by GitHub
3 changed files with 47 additions and 19 deletions
+32 -16
View File
@@ -11,9 +11,9 @@ import log from "electron-log";
import { OculusService } from "./oculus.service";
import { DownloadLinkType } from "shared/models/mods";
import sanitize from "sanitize-filename";
import { Progression, copyDirectoryWithJunctions, deleteFolder, ensurePathNotAlreadyExist, getFoldersInFolder, pathExist, rxCopy } from "../helpers/fs.helpers";
import { Progression, copyDirectoryWithJunctions, deleteFolder, ensurePathNotAlreadyExist, getFoldersInFolder, rxCopy } from "../helpers/fs.helpers";
import { FolderLinkerService } from "./folder-linker.service";
import { ReadStream, createReadStream, readFile, writeFile } from "fs-extra";
import { ReadStream, createReadStream, pathExists, readFile, writeFile } from "fs-extra";
import readline from "readline";
import { Observable, Subject, catchError, finalize, from, map, switchMap, throwError } from "rxjs";
import { BsStore } from "../../shared/models/bs-store.enum";
@@ -53,7 +53,10 @@ export class BSLocalVersionService {
private async getVersionFromGlobalGameManagerFile(versionFilePath: string): Promise<BSVersion> {
if(!(await pathExist(versionFilePath))){ return null; }
if(!(await pathExists(versionFilePath))){
log.info("globalgamemanagers file not found", versionFilePath);
return null;
}
const versionsDict = await this.remoteVersionService.getAvailableVersions();
@@ -81,6 +84,8 @@ export class BSLocalVersionService {
stream?.close();
}
log.info("unable to get version from globalgamemanagers file", versionFilePath);
return null;
}
@@ -92,6 +97,8 @@ export class BSLocalVersionService {
}
): Promise<BSVersion>{
log.info("getVersionOfBSFolder", bsPath, options);
if(!bsPath){ return null; }
const versionFilePath = path.join(bsPath, 'Beat Saber_Data', 'globalgamemanagers');
@@ -129,7 +136,7 @@ export class BSLocalVersionService {
const contents = await readFile(path.join(versionPath, this.METADATA_FILE), "utf-8");
return JSON.parse(contents);
})().catch(e => {
log.warn(e);
log.warn("Not a critical error", e);
return {};
}).then(metadata => ({...defaultMetadata, ...metadata}));
}
@@ -139,7 +146,7 @@ export class BSLocalVersionService {
const metadata = await this.getAllVersionMetadata(version);
return metadata?.[key];
})().catch(e => {
log.warn(e);
log.warn("Not a critical error", e);
return null;
}).then(value => (value ?? defaultValue) as T);
}
@@ -196,7 +203,7 @@ export class BSLocalVersionService {
*/
public async getInstalledVersionPath(version: BSVersion): Promise<string>{
const versionPath = await this.getVersionPath(version);
if(await pathExist(versionPath)){ return versionPath; }
if(await pathExists(versionPath)){ return versionPath; }
const versionFolders = await getFoldersInFolder(await this.installLocationService.versionsDirectory());
@@ -227,7 +234,7 @@ export class BSLocalVersionService {
private async getSteamVersion(): Promise<BSVersion> {
const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber");
if (!steamBsFolder || !(await pathExist(steamBsFolder))) {
if (!steamBsFolder || !(await pathExists(steamBsFolder))) {
return null;
}
@@ -247,26 +254,35 @@ export class BSLocalVersionService {
public async getInstalledVersions(): Promise<BSVersion[]> {
const versions: BSVersion[] = [];
const steamVersion = await this.getSteamVersion();
const steamVersion = await this.getSteamVersion().catch(e => {
log.error("unable to get original Steam version", e);
});
if (steamVersion) {
versions.push(steamVersion);
}
const oculusVersion = await this.getOculusVersion();
const oculusVersion = await this.getOculusVersion().catch(e => {
log.error("unable to get original Oculus version", e);
});
if (oculusVersion) {
versions.push(oculusVersion);
}
if (!(await pathExist(await this.installLocationService.versionsDirectory()))) {
if (!(await pathExists(await this.installLocationService.versionsDirectory()))) {
return versions;
}
const folderInInstallation = await getFoldersInFolder(await this.installLocationService.versionsDirectory());
for (const f of folderInInstallation) {
log.info("try get version from folder", f);
log.info("Finded versions folders", folderInInstallation);
const version = await this.getVersionOfBSFolder(f);
for (const f of folderInInstallation) {
const version = await this.getVersionOfBSFolder(f).catch(e => {
log.error("unable to get version of folder", f, e);
});
if(!version){ continue; }
@@ -283,7 +299,7 @@ export class BSLocalVersionService {
public async deleteVersion(version: BSVersion): Promise<boolean>{
if(version.steam || version.oculus){ return false; }
const versionFolder = await this.getVersionPath(version);
if(!(await pathExist(versionFolder))){ return true; }
if(!(await pathExists(versionFolder))){ return true; }
return deleteFolder(versionFolder)
.then(() => { return true; })
@@ -304,7 +320,7 @@ export class BSLocalVersionService {
return editedVersion;
}
if((await pathExist(newPath)) && newPath === oldPath){ throw {title: "VersionAlreadExist"} as BsmException; }
if((await pathExists(newPath)) && newPath === oldPath){ throw {title: "VersionAlreadExist"} as BsmException; }
return rename(oldPath, newPath).then(() => {
this.deleteCustomVersion(version);
@@ -328,7 +344,7 @@ export class BSLocalVersionService {
this.addCustomVersion(cloneVersion);
}
if(await pathExist(newPath)){ throw {title: "VersionAlreadExist"} as BsmException; }
if(await pathExists(newPath)){ throw {title: "VersionAlreadExist"} as BsmException; }
return copyDirectoryWithJunctions(originPath, newPath).then(() => {
this.addCustomVersion(cloneVersion);
@@ -1,7 +1,7 @@
import { BSVersion } from "shared/bs-version.interface";
import { Link, useLocation } from "react-router-dom";
import { useState } from "react";
import { distinctUntilChanged, lastValueFrom, map, of, Subscription, switchMap } from "rxjs";
import { distinctUntilChanged, lastValueFrom, map, of, Subscription, switchMap, take } from "rxjs";
import { BSLauncherService, LaunchMods } from "renderer/services/bs-launcher.service";
import { ConfigurationService } from "renderer/services/configuration.service";
import { BSUninstallerService } from "renderer/services/bs-uninstaller.service";
@@ -55,7 +55,9 @@ export function BsVersionItem(props: { version: BSVersion }) {
return props.version?.BSVersion === state?.BSVersion && props?.version.steam === state?.steam && props?.version.oculus === state?.oculus && props?.version.name === state?.name;
};
const handleDoubleClick = () => {
const handleDoubleClick = async () => {
const downloadingVersion = await lastValueFrom(bsDownloader.downloadingVersion$.pipe(take(1)));
if(equal(downloadingVersion, props.version)){ return; }
const launch$ = launcherService.launch({
version: state,
oculus: !!configService.get<boolean>(LaunchMods.OCULUS_MOD),
@@ -12,6 +12,8 @@ import { BsmImage } from "renderer/components/shared/bsm-image.component";
import { useService } from "renderer/hooks/use-service.hook";
import { BsStore } from "shared/models/bs-store.enum";
import { lastValueFrom } from "rxjs";
import { BsDownloaderService } from "renderer/services/bs-version-download/bs-downloader.service";
import equal from "fast-deep-equal";
type Props = { version: BSVersion };
@@ -20,12 +22,14 @@ export function LaunchSlide({ version }: Props) {
const configService = useService(ConfigurationService);
const bsLauncherService = useService(BSLauncherService);
const bsDownloader = useService(BsDownloaderService);
const [oculusMode, setOculusMode] = useState(!!configService.get<boolean>(LaunchMods.OCULUS_MOD));
const [desktopMode, setDesktopMode] = useState(!!configService.get<boolean>(LaunchMods.DESKTOP_MOD));
const [debugMode, setDebugMode] = useState(!!configService.get<boolean>(LaunchMods.DEBUG_MOD));
const [advancedLaunch, setAdvancedLaunch] = useState(false);
const [additionalArgsString, setAdditionalArgsString] = useState<string>(configService.get<string>("additionnal-args") || "");
const versionDownloading = useObservable(bsDownloader.downloadingVersion$);
const versionRunning = useObservable(bsLauncherService.versionRunning$);
@@ -96,7 +100,13 @@ export function LaunchSlide({ version }: Props) {
</motion.div>
</div>
<div className='grow flex justify-center items-center'>
<BsmButton onClick={launch} active={JSON.stringify(version) === JSON.stringify(versionRunning)} className='relative -translate-y-1/2 text-5xl text-gray-800 dark:text-gray-200 font-bold tracking-wide pt-1 pb-3 px-7 rounded-lg shadow-md italic shadow-black active:scale-90 transition-transform' text="misc.launch"/>
<BsmButton
onClick={launch}
active={JSON.stringify(version) === JSON.stringify(versionRunning)}
className='relative -translate-y-1/2 text-5xl text-gray-800 dark:text-gray-200 font-bold tracking-wide pt-1 pb-3 px-7 rounded-lg shadow-md italic shadow-black active:scale-90 transition-transform'
text="misc.launch"
disabled={equal(version, versionDownloading)}
/>
</div>
</div>
);