config rework

This commit is contained in:
MathieuG-P
2022-12-31 22:52:45 +01:00
parent f0110dcbb8
commit b922c47fc2
2 changed files with 55 additions and 59 deletions
+22 -36
View File
@@ -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<string, ElectronStore>;
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<T>(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<T>(key: string): T{
return this.store.get(key) as T;
}
public delete(key: string): void{
this.store.delete(key);
}
}
@@ -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<Listener> = 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<string>(this.STORE_INSTALLATION_PATH_KEY) || app.getPath("documents");
this._installationDirectory = this.installPathConfig.get<string>(this.STORE_INSTALLATION_PATH_KEY) as string || app.getPath("documents");
}
private triggerListeners(): void{
this.updateListeners.forEach(listener => listener());
}
public setInstallationDirectory(newDir: string): Promise<string>{
@@ -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;