diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts index d908505b..a1fadeb7 100644 --- a/src/main/services/bs-local-version.service.ts +++ b/src/main/services/bs-local-version.service.ts @@ -46,12 +46,12 @@ export class BSLocalVersionService{ const versionFilePath = path.join(bsPath, 'Beat Saber_Data', 'globalgamemanagers'); if(!this.utilsService.pathExist(versionFilePath)){ return null; } const versionsAvailable = await this.remoteVersionService.getAvailableVersions(); - return new Promise(resolve => { + return new Promise(resolve => { const readLine = createInterface({ input: createReadStream(versionFilePath) }); let findVersion: string = null; readLine.on('line', (line) => { for(const version of versionsAvailable){ - if(line.includes(version.BSVersion)){ + if(line.includes(version.BSVersion)){ findVersion = version.BSVersion; readLine.close(); break; @@ -126,7 +126,7 @@ export class BSLocalVersionService{ } public async getInstalledVersions(): Promise{ - + const versions: BSVersion[] = []; const steamVersion = await this.getSteamVersion(); if(steamVersion){ versions.push(steamVersion); } @@ -166,7 +166,7 @@ export class BSLocalVersionService{ public async editVersion(version: BSVersion, name: string, color: string): Promise{ if(version.steam || version.oculus){ throw {title: "CantEditSteam", msg: "CantEditSteam"} as BsmException; } const oldPath = await this.getVersionPath(version); - const editedVersion: BSVersion = version.BSVersion === name + const editedVersion: BSVersion = version.BSVersion === name ? {...version, name: undefined, color} : {...version, name: this.removeSpecialChar(name), color}; const newPath = await this.getVersionPath(editedVersion); @@ -190,7 +190,7 @@ export class BSLocalVersionService{ public async cloneVersion(version: BSVersion, name: string, color: string): Promise{ const originPath = await this.getVersionPath(version); - const cloneVersion: BSVersion = version.BSVersion === name + const cloneVersion: BSVersion = version.BSVersion === name ? {...version, name: undefined, color} : {...version, name: this.removeSpecialChar(name), color, steam: false, oculus: false}; const newPath = await this.getVersionPath(cloneVersion); @@ -203,12 +203,13 @@ export class BSLocalVersionService{ if(this.utilsService.pathExist(newPath)){ throw {title: "VersionAlreadExist"} as BsmException; } - return fs.copy(originPath, newPath).then(() => { + return fs.copy(originPath, newPath, {dereference: true}).then(() => { this.addCustomVersion(cloneVersion); return cloneVersion; }).catch((err: Error) => { + log.error("CLONE", err, version); throw {title: "CantClone", error: err} as BsmException }) } -} \ No newline at end of file +} diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index 1aa2d540..ad431e57 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -1,23 +1,37 @@ import ElectronStore from "electron-store"; +import { InstallationLocationService } from "./installation-location.service"; export class ConfigurationService { private static instance: ConfigurationService; - private readonly _store: ElectronStore; - public static getInstance(): ConfigurationService{ if(!ConfigurationService.instance){ ConfigurationService.instance = new ConfigurationService(); } return ConfigurationService.instance; } + private readonly locations: InstallationLocationService; + + private store: ElectronStore; + private constructor(){ - this._store = new ElectronStore(); + this.locations = InstallationLocationService.getInstance(); + this.initStore(); + + this.locations.onInstallLocationUpdate(() => this.initStore()); + } - public get store(): ElectronStore{ return this._store; } + private initStore(){ + const contentPath = this.locations.installationDirectory; + this.store = new ElectronStore({ + cwd: contentPath, + name: "config", + fileExtension: "cfg", + }); + } - public set(key: string, value: any): void{ + public set(key: string, value: unknown): void{ this.store.set(key, value); } @@ -29,4 +43,4 @@ export class ConfigurationService { this.store.delete(key); } -} \ No newline at end of file +} diff --git a/src/main/services/installation-location.service.ts b/src/main/services/installation-location.service.ts index a5cc14ae..dfe6013c 100644 --- a/src/main/services/installation-location.service.ts +++ b/src/main/services/installation-location.service.ts @@ -1,44 +1,50 @@ import path from "path"; -import { ConfigurationService } from "./configuration.service"; import { UtilsService } from "./utils.service"; import fs from 'fs-extra'; import log from "electron-log"; import { app } from "electron"; import { BsmException } from "shared/models/bsm-exception.model"; +import ElectronStore from "electron-store"; export class InstallationLocationService { private static instance: InstallationLocationService; + public static getInstance(): InstallationLocationService{ + if(!InstallationLocationService.instance){ InstallationLocationService.instance = new InstallationLocationService(); } + return InstallationLocationService.instance; + } + private readonly INSTALLATION_FOLDER = "BSManager"; private readonly VERSIONS_FOLDER = "BSInstances"; private readonly SHARED_CONTENT_FOLDER = "SharedContent"; private readonly SHARED_MAPS_FOLDER = "SharedMaps"; - private readonly SHARED_PLAYLISTS_FOLDER = "SharedPlaylists"; private readonly STORE_INSTALLATION_PATH_KEY = "installation-folder"; - - - private readonly configService: ConfigurationService; private readonly utilsService: UtilsService; + private readonly installPathConfig: ElectronStore; + private readonly updateListeners: Set = new Set(); private _installationDirectory: string; - public static getInstance(): InstallationLocationService{ - if(!InstallationLocationService.instance){ InstallationLocationService.instance = new InstallationLocationService(); } - return InstallationLocationService.instance; - } - private constructor(){ - this.configService = ConfigurationService.getInstance(); + this.installPathConfig = new ElectronStore({watch: true}); this.utilsService = UtilsService.getInstance(); this.initInstallationLocation(); + + this.installPathConfig.onDidChange(this.STORE_INSTALLATION_PATH_KEY, () => { + this.triggerListeners(); + }) } private initInstallationLocation(): void{ - this._installationDirectory = this.configService.get(this.STORE_INSTALLATION_PATH_KEY) || app.getPath("documents"); + this._installationDirectory = this.installPathConfig.get(this.STORE_INSTALLATION_PATH_KEY) as string || app.getPath("documents"); + } + + private triggerListeners(): void{ + this.updateListeners.forEach(listener => listener()); } public setInstallationDirectory(newDir: string): Promise{ @@ -48,13 +54,17 @@ export class InstallationLocationService { if(!this.utilsService.pathExist(oldDir)){ this.utilsService.createFolderIfNotExist(oldDir); } fs.move(oldDir, newDest, { overwrite: true }).then(() => { this._installationDirectory = newDir; - this.configService.store.set(this.STORE_INSTALLATION_PATH_KEY, newDir); + this.installPathConfig.set(this.STORE_INSTALLATION_PATH_KEY, newDir); resolve(this.installationDirectory); }).catch((err: Error) => { reject({title: "CantMoveFolder", error: err} as BsmException); log.error(err); }) - }) + }); + } + + public onInstallLocationUpdate(fn: Listener){ + this.updateListeners.add(fn); } public get installationDirectory(): string{ return path.join(this._installationDirectory, this.INSTALLATION_FOLDER); } @@ -63,7 +73,7 @@ export class InstallationLocationService { public get sharedContentPath(): string { return path.join(this.installationDirectory, this.SHARED_CONTENT_FOLDER); } public get sharedMapsPath(): string { return path.join(this.sharedContentPath, this.SHARED_MAPS_FOLDER); } - public get sharedPlaylistsPath(): string { return path.join(this.sharedContentPath, this.SHARED_PLAYLISTS_FOLDER); } +} -} \ No newline at end of file +type Listener = () => void;