diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts index a1fadeb7..08fb4f06 100644 --- a/src/main/services/bs-local-version.service.ts +++ b/src/main/services/bs-local-version.service.ts @@ -5,7 +5,6 @@ import { SteamService } from "./steam.service"; import { UtilsService } from "./utils.service"; import { BS_APP_ID, OCULUS_BS_DIR } from "../constants"; import path from "path"; -import { createInterface } from "readline"; import { createReadStream } from "fs"; import fs from "fs-extra"; import { ConfigurationService } from "./configuration.service"; @@ -14,6 +13,7 @@ import { BsmException } from "shared/models/bsm-exception.model"; import log from "electron-log"; import { OculusService } from "./oculus.service"; import { DownloadLinkType } from "shared/models/mods"; +import sanitize from "sanitize-filename"; export class BSLocalVersionService{ @@ -47,21 +47,27 @@ export class BSLocalVersionService{ if(!this.utilsService.pathExist(versionFilePath)){ return null; } const versionsAvailable = await this.remoteVersionService.getAvailableVersions(); return new Promise(resolve => { - const readLine = createInterface({ input: createReadStream(versionFilePath) }); + const stream = createReadStream(versionFilePath); let findVersion: string = null; - readLine.on('line', (line) => { + stream.on('data', (line) => { + line = line.toString(); for(const version of versionsAvailable){ if(line.includes(version.BSVersion)){ findVersion = version.BSVersion; - readLine.close(); + stream.close(); + stream.destroy(); break; } } }); - readLine.on('close', () => { + stream.on('end', () => { if(findVersion){ resolve(findVersion) } resolve(findVersion); }); + stream.on('close', () => { + if(findVersion){ resolve(findVersion) } + resolve(findVersion); + }) }) } @@ -92,11 +98,11 @@ export class BSLocalVersionService{ } private removeSpecialChar(seq: string): string{ - return seq.replace( /[<>:"\/\\|?*]+/g, '' ); + return sanitize(seq); } - public getVersionFolder(version: BSVersion){ - return version.name ? `${version.BSVersion}-${version.name}` : version.BSVersion; + public getVersionFolder(version: BSVersion): string{ + return version.name ?? version.BSVersion; } public getVersionType(version: BSVersion): DownloadLinkType{ @@ -107,8 +113,10 @@ export class BSLocalVersionService{ private async getSteamVersion(): Promise{ const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber"); + if(!steamBsFolder || !this.utilsService.pathExist(steamBsFolder)){ return null; } const steamBsVersion = await this.getVersionOfBSFolder(steamBsFolder); + if(!steamBsVersion){ return null; } const version = await this.remoteVersionService.getVersionDetails(steamBsVersion); if(!version){ return null; } @@ -127,30 +135,37 @@ export class BSLocalVersionService{ public async getInstalledVersions(): Promise{ - const versions: BSVersion[] = []; - const steamVersion = await this.getSteamVersion(); - if(steamVersion){ versions.push(steamVersion); } - const oculusVersion = await this.getOculusVersion(); - if(oculusVersion){ versions.push(oculusVersion); } + const versions: BSVersion[] = []; - if(!this.utilsService.pathExist(this.installLocationService.versionsDirectory)){ return versions } + const steamVersion = await this.getSteamVersion(); + if(steamVersion){ versions.push(steamVersion); } - const folderInInstallation = this.utilsService.listDirsInDir(this.installLocationService.versionsDirectory); - for(const f of folderInInstallation){ - log.info("try get version from folder", f); - let version = await this.remoteVersionService.getVersionDetails(f); - if(version){ version = this.getCustomVersions().find(v => v.BSVersion === version.BSVersion && v.name === version.name) ?? version; } - else { version = this.getCustomVersions().find(v => { - const [version, ...rest] = f.split("-"); - if(rest.length < 1){ return false; } - const name = rest.join("-"); - return name === v.name && version === v.BSVersion; - }) - } - version && versions.push(version); - }; - this.setCustomVersions(versions.filter(v => !!v.name || !!v.color)); - return versions; + const oculusVersion = await this.getOculusVersion(); + if(oculusVersion){ versions.push(oculusVersion); } + + if(!this.utilsService.pathExist(this.installLocationService.versionsDirectory)){ return versions } + + const folderInInstallation = this.utilsService.listDirsInDir(this.installLocationService.versionsDirectory, true); + + for(const f of folderInInstallation){ + log.info("try get version from folder", f); + const rawVersion = await this.getVersionOfBSFolder(f); + if(!rawVersion){ continue; } + const bsVersion = {...await this.remoteVersionService.getVersionDetails(rawVersion)}; + if(!bsVersion){ continue; } + bsVersion.name = path.basename(f) !== bsVersion.BSVersion ? path.basename(f) : undefined; + + const customVersion = this.getCustomVersions().find(custom => custom.BSVersion === bsVersion.BSVersion && custom.name === bsVersion.name); + + if(customVersion){ + bsVersion.color = customVersion.color; + } + + versions.push(bsVersion); + }; + this.setCustomVersions(versions.filter(v => !!v.color)); + + return versions; } public async deleteVersion(version: BSVersion): Promise{ @@ -177,13 +192,14 @@ export class BSLocalVersionService{ return editedVersion; } - if(this.utilsService.pathExist(newPath)){ throw {title: "VersionAlreadExist"} as BsmException; } + if(this.utilsService.pathExist(newPath) && newPath === oldPath){ throw {title: "VersionAlreadExist"} as BsmException; } return rename(oldPath, newPath).then(() => { this.deleteCustomVersion(version); this.addCustomVersion(editedVersion); return editedVersion; }).catch((err: Error) => { + log.error("edit version error", err, version, name, color); throw {title: "CantRename", error: err} as BsmException; }); } @@ -198,7 +214,6 @@ export class BSLocalVersionService{ if(originPath === newPath){ this.deleteCustomVersion(version); this.addCustomVersion(cloneVersion); - return cloneVersion; } if(this.utilsService.pathExist(newPath)){ throw {title: "VersionAlreadExist"} as BsmException; } @@ -207,7 +222,7 @@ export class BSLocalVersionService{ this.addCustomVersion(cloneVersion); return cloneVersion; }).catch((err: Error) => { - log.error("CLONE", err, version); + log.error("clone version error", err, version, name, color); throw {title: "CantClone", error: err} as BsmException }) }