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] 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;