From 9fbf81f8f2d47586dd71b7e9867971e2f33a5e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Mon, 12 Dec 2022 22:14:06 +0100 Subject: [PATCH 1/5] init change --- src/main/services/configuration.service.ts | 31 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index 1aa2d540..00400873 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -1,10 +1,13 @@ +import { InstallationLocationService } from '../services/installation-location.service'; import ElectronStore from "electron-store"; +import { Template } from 'webpack'; export class ConfigurationService { private static instance: ConfigurationService; private readonly _store: ElectronStore; + private readonly _versionBsStore: ElectronStore; public static getInstance(): ConfigurationService{ if(!ConfigurationService.instance){ ConfigurationService.instance = new ConfigurationService(); } @@ -13,20 +16,40 @@ export class ConfigurationService { private constructor(){ this._store = new ElectronStore(); + this._versionBsStore = new ElectronStore({cwd : InstallationLocationService.getInstance().installationDirectory}); } public get store(): ElectronStore{ return this._store; } + public get versionBsStore() : ElectronStore{ return this._versionBsStore;} - public set(key: string, value: any): void{ + public set(key: string, value: any, storeSelected : string): void{ + if (storeSelected === "store"){ this.store.set(key, value); + } + else if (storeSelected === "versionBsStore") + { + this.versionBsStore.set(key, value); + } + else return null; } - public get(key: string): T{ + public get(key: string,storeSelected : string): T{ + + if (storeSelected === "store"){ return this.store.get(key) as T; + } + else if (storeSelected === "versionBsStore"){ + return this.store.get(key) as T; + } } - public delete(key: string): void{ + public delete(key: string,storeSelected : string): void{ + if (storeSelected === "store"){ this.store.delete(key); + } + else if (storeSelected === "versionBsStore"){ + this.store.delete(key); + } } -} \ No newline at end of file +} From 77ab238863f34b357959e76a8cd70ebb655a56b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Tue, 13 Dec 2022 11:05:12 +0100 Subject: [PATCH 2/5] Config congiguration.service.ts --- src/main/services/configuration.service.ts | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index 00400873..1b3a4d72 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -1,13 +1,13 @@ -import { InstallationLocationService } from '../services/installation-location.service'; import ElectronStore from "electron-store"; -import { Template } from 'webpack'; export class ConfigurationService { private static instance: ConfigurationService; - private readonly _store: ElectronStore; - private readonly _versionBsStore: ElectronStore; + private readonly _stores: Map; + private readonly _store: ElectronStore + + public static getInstance(): ConfigurationService{ if(!ConfigurationService.instance){ ConfigurationService.instance = new ConfigurationService(); } @@ -15,22 +15,22 @@ export class ConfigurationService { } private constructor(){ - this._store = new ElectronStore(); - this._versionBsStore = new ElectronStore({cwd : InstallationLocationService.getInstance().installationDirectory}); + this._stores = new Map(); } - public get store(): ElectronStore{ return this._store; } - public get versionBsStore() : ElectronStore{ return this._versionBsStore;} + public get stores(): Map {return this._stores} + public get store(): ElectronStore {return this._store} - public set(key: string, value: any, storeSelected : string): void{ - if (storeSelected === "store"){ - this.store.set(key, value); + + + public set(key: string, value: any, store?: string, options?: ElectronStore.Options>): void{ + + if(store && !this.stores.has(store)){ + this.stores.set(store, new ElectronStore(options)); } - else if (storeSelected === "versionBsStore") - { - this.versionBsStore.set(key, value); - } - else return null; + + const eStore = !store ? this._store : this.stores.get(store) + eStore.set(key, value); } public get(key: string,storeSelected : string): T{ @@ -38,7 +38,7 @@ export class ConfigurationService { if (storeSelected === "store"){ return this.store.get(key) as T; } - else if (storeSelected === "versionBsStore"){ + if (storeSelected === "versionBsStore"){ return this.store.get(key) as T; } } From b2f76d7aaa57e44b756b2e9daca14f72abe49b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Tue, 13 Dec 2022 22:21:35 +0100 Subject: [PATCH 3/5] change and separe data config.json --- src/main/services/bs-local-version.service.ts | 16 +++--- src/main/services/configuration.service.ts | 55 ++++++++++--------- .../services/installation-location.service.ts | 8 +-- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts index d908505b..d2c09148 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; @@ -66,7 +66,7 @@ export class BSLocalVersionService{ } private setCustomVersions(versions: BSVersion[]): void{ - this.configService.set(this.CUSTOM_VERSIONS_KEY, versions); + this.configService.set(this.CUSTOM_VERSIONS_KEY, versions,this.CUSTOM_VERSIONS_KEY,this.installLocationService.installationDirectory); } private addCustomVersion(version: BSVersion): void{ @@ -74,7 +74,7 @@ export class BSLocalVersionService{ } private getCustomVersions(): BSVersion[]{ - return this.configService.get(this.CUSTOM_VERSIONS_KEY) || []; + return this.configService.get(this.CUSTOM_VERSIONS_KEY,this.CUSTOM_VERSIONS_KEY) || []; } private deleteCustomVersion(version: BSVersion): void{ @@ -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); @@ -211,4 +211,4 @@ export class BSLocalVersionService{ }) } -} \ No newline at end of file +} diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index 1b3a4d72..ce158e1b 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -1,11 +1,14 @@ +import { Console } from "console"; import ElectronStore from "electron-store"; + + export class ConfigurationService { private static instance: ConfigurationService; private readonly _stores: Map; - private readonly _store: ElectronStore + private readonly _storeFixe: ElectronStore @@ -15,41 +18,43 @@ export class ConfigurationService { } private constructor(){ + this._stores = new Map(); + this._storeFixe = new ElectronStore(); } - public get stores(): Map {return this._stores} - public get store(): ElectronStore {return this._store} - - - - public set(key: string, value: any, store?: string, options?: ElectronStore.Options>): void{ - - if(store && !this.stores.has(store)){ - this.stores.set(store, new ElectronStore(options)); + private getPropperStore(selectedStore?: string) : ElectronStore { + if(selectedStore){ + return this._stores.get(selectedStore); } + return this._storeFixe; - const eStore = !store ? this._store : this.stores.get(store) - eStore.set(key, value); } - public get(key: string,storeSelected : string): T{ - if (storeSelected === "store"){ - return this.store.get(key) as T; - } - if (storeSelected === "versionBsStore"){ - return this.store.get(key) as T; + public set(key: string, value: any, selectedStore?: string, path?: string): void{ + if(selectedStore && !this._stores.has(selectedStore)){ + this._stores.set(selectedStore, new ElectronStore({cwd:path,name:selectedStore})); } + + const eStore = this.getPropperStore(selectedStore); + eStore.set(key,value); + } - public delete(key: string,storeSelected : string): void{ - if (storeSelected === "store"){ - this.store.delete(key); - } - else if (storeSelected === "versionBsStore"){ - this.store.delete(key); - } + public get(key: string,selectedStore?: string): T{ + + const eStore = this.getPropperStore(selectedStore); + + if(!eStore){ return; } + return eStore.get(key) as T; + + } + + public delete(key: string,selectedStore : string): void{ + + this.getPropperStore(selectedStore)?.delete(key); + } } diff --git a/src/main/services/installation-location.service.ts b/src/main/services/installation-location.service.ts index 4503d552..48a58c66 100644 --- a/src/main/services/installation-location.service.ts +++ b/src/main/services/installation-location.service.ts @@ -23,7 +23,7 @@ export class InstallationLocationService { public static getInstance(): InstallationLocationService{ if(!InstallationLocationService.instance){ InstallationLocationService.instance = new InstallationLocationService(); } - return InstallationLocationService.instance; + return InstallationLocationService.instance; } private constructor(){ @@ -46,14 +46,14 @@ 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.configService.set(this.STORE_INSTALLATION_PATH_KEY, newDir); resolve(this.installationDirectory); }).catch((err: Error) => { reject({title: "CantMoveFolder", error: err} as BsmException); log.error(err); }) }) - + } -} \ No newline at end of file +} From f0110dcbb8b0980cbf73733634acacd058c69e62 Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Sat, 31 Dec 2022 22:51:55 +0100 Subject: [PATCH 4/5] remove config changes AND fix bug on clone version with linked maps --- src/main/services/bs-local-version.service.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/services/bs-local-version.service.ts b/src/main/services/bs-local-version.service.ts index d2c09148..a1fadeb7 100644 --- a/src/main/services/bs-local-version.service.ts +++ b/src/main/services/bs-local-version.service.ts @@ -66,7 +66,7 @@ export class BSLocalVersionService{ } private setCustomVersions(versions: BSVersion[]): void{ - this.configService.set(this.CUSTOM_VERSIONS_KEY, versions,this.CUSTOM_VERSIONS_KEY,this.installLocationService.installationDirectory); + this.configService.set(this.CUSTOM_VERSIONS_KEY, versions); } private addCustomVersion(version: BSVersion): void{ @@ -74,7 +74,7 @@ export class BSLocalVersionService{ } private getCustomVersions(): BSVersion[]{ - return this.configService.get(this.CUSTOM_VERSIONS_KEY,this.CUSTOM_VERSIONS_KEY) || []; + return this.configService.get(this.CUSTOM_VERSIONS_KEY) || []; } private deleteCustomVersion(version: BSVersion): void{ @@ -203,10 +203,11 @@ 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 }) } From b922c47fc2e41af0c883bdd978789d9757a0f5d7 Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Sat, 31 Dec 2022 22:52:45 +0100 Subject: [PATCH 5/5] config rework --- src/main/services/configuration.service.ts | 58 +++++++------------ .../services/installation-location.service.ts | 56 ++++++++++-------- 2 files changed, 55 insertions(+), 59 deletions(-) diff --git a/src/main/services/configuration.service.ts b/src/main/services/configuration.service.ts index ce158e1b..ad431e57 100644 --- a/src/main/services/configuration.service.ts +++ b/src/main/services/configuration.service.ts @@ -1,60 +1,46 @@ -import { Console } from "console"; import ElectronStore from "electron-store"; - - +import { InstallationLocationService } from "./installation-location.service"; export class ConfigurationService { private static instance: ConfigurationService; - private readonly _stores: Map; - private readonly _storeFixe: 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.locations = InstallationLocationService.getInstance(); + this.initStore(); - this._stores = new Map(); - this._storeFixe = new ElectronStore(); - } - - private getPropperStore(selectedStore?: string) : ElectronStore { - if(selectedStore){ - return this._stores.get(selectedStore); - } - return this._storeFixe; + this.locations.onInstallLocationUpdate(() => this.initStore()); } - - public set(key: string, value: any, selectedStore?: string, path?: string): void{ - if(selectedStore && !this._stores.has(selectedStore)){ - this._stores.set(selectedStore, new ElectronStore({cwd:path,name:selectedStore})); - } - - const eStore = this.getPropperStore(selectedStore); - eStore.set(key,value); - + private initStore(){ + const contentPath = this.locations.installationDirectory; + this.store = new ElectronStore({ + cwd: contentPath, + name: "config", + fileExtension: "cfg", + }); } - public get(key: string,selectedStore?: string): T{ - - const eStore = this.getPropperStore(selectedStore); - - if(!eStore){ return; } - return eStore.get(key) as T; - + public set(key: string, value: unknown): void{ + this.store.set(key, value); } - public delete(key: string,selectedStore : string): void{ - - this.getPropperStore(selectedStore)?.delete(key); + public get(key: string): T{ + return this.store.get(key) as T; + } + public delete(key: string): void{ + this.store.delete(key); } } diff --git a/src/main/services/installation-location.service.ts b/src/main/services/installation-location.service.ts index 17a77986..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; - 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 _installationDirectory: string; - 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 STORE_INSTALLATION_PATH_KEY = "installation-folder"; + private readonly utilsService: UtilsService; + + private readonly installPathConfig: ElectronStore; + private readonly updateListeners: Set = new Set(); + + private _installationDirectory: string; + 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.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); } - } + +type Listener = () => void;