clean code

This commit is contained in:
MathieuG-P
2022-07-17 19:47:07 +02:00
parent e203fb603d
commit 4fd3a1da2f
27 changed files with 137 additions and 158 deletions
+8 -8
View File
@@ -1,7 +1,7 @@
import { ipcMain } from 'electron';
import { BSVersion } from '../services/bs-version-manager.service';
import { BSVersion } from '../services/bs-version-lib.service';
import { BSInstallerService, DownloadEventType } from '../services/bs-installer.service';
import { IpcRequest } from '../../shared/models/ipc-models.model';
import { IpcRequest } from 'shared/models/ipc';
import { InstallationLocationService } from '../services/installation-location.service';
import { UtilsService } from '../services/utils.service';
@@ -25,9 +25,9 @@ export interface DownloadInfo {
ipcMain.on('bs-download.start', async (event, request: IpcRequest<DownloadInfo>) => {
BSInstallerService.getInstance().downloadBsVersion(request.args).then(res => {
UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: true, data: res});
UtilsService.getInstance().ipcSend(request.responceChannel, {success: true, data: res});
}).catch(e => {
UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: false, data: e});
UtilsService.getInstance().ipcSend(request.responceChannel, {success: false, data: e});
});
});
@@ -37,22 +37,22 @@ ipcMain.on(`bs-download.${"[2FA]" as DownloadEventType}`, async (event, args: Ip
ipcMain.on('bs-download.kill', async (event, request: IpcRequest<void>) => {
const res = await BSInstallerService.getInstance().killDownloadProcess();
if(request.responceChannel){ UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: res}); }
if(request.responceChannel){ UtilsService.getInstance().ipcSend(request.responceChannel, {success: res}); }
});
ipcMain.on('bs-download.installation-folder', async (event, request: IpcRequest<void>) => {
const installationFolder = InstallationLocationService.getInstance().installationDirectory;
UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: true, data: installationFolder});
UtilsService.getInstance().ipcSend(request.responceChannel, {success: true, data: installationFolder});
});
ipcMain.on('bs-download.set-installation-folder', (event, request: IpcRequest<string>) => {
const installerService = InstallationLocationService.getInstance();
installerService.setInstallationDirectory(request.args).then(res => {
console.log("déplacement terminer");
UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: true, data: res});
UtilsService.getInstance().ipcSend(request.responceChannel, {success: true, data: res});
}).catch(err => {
console.log(err);
UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: false, error: {title: err, type: 'error'}});
UtilsService.getInstance().ipcSend(request.responceChannel, {success: false, error: {title: err, type: 'error'}});
});
})
+3 -3
View File
@@ -2,15 +2,15 @@ import { ipcMain } from 'electron';
import { UtilsService } from '../services/utils.service'
import { LauchOption } from "../../shared/models/launch-models.model";
import { BSLauncherService } from "../services/bs-launcher.service"
import { IpcRequest } from 'shared/models/ipc-models.model';
import { IpcRequest } from 'shared/models/ipc';
ipcMain.on('bs-launch.launch', (event, request: IpcRequest<LauchOption>) => {
const launcherService = BSLauncherService.getInstance();
const utilsService = UtilsService.getInstance();
launcherService.launch(request.args).then(res => {
utilsService.newIpcSenc(request.responceChannel, {success: true, data: res});
utilsService.ipcSend(request.responceChannel, {success: true, data: res});
}).catch(err => {
utilsService.newIpcSenc(request.responceChannel, {success: false, error: {title: err, type: 'error'}});
utilsService.ipcSend(request.responceChannel, {success: false, error: {title: err, type: 'error'}});
})
});
+4 -4
View File
@@ -1,7 +1,7 @@
import { ipcMain } from "electron";
import { BSVersion } from "main/services/bs-version-manager.service";
import { BSVersion } from "main/services/bs-version-lib.service";
import { UtilsService } from "../services/utils.service";
import { IpcRequest } from "shared/models/ipc-models.model";
import { IpcRequest } from "shared/models/ipc";
import { BSLocalVersionService } from "../services/bs-local-version.service";
ipcMain.on('bs.uninstall', async (event, request: IpcRequest<BSVersion>) => {
@@ -9,8 +9,8 @@ ipcMain.on('bs.uninstall', async (event, request: IpcRequest<BSVersion>) => {
const bsLocalVersionService = BSLocalVersionService.getInstance();
const utilsService = UtilsService.getInstance();
if(!request.args){ utilsService.newIpcSenc(request.responceChannel, {success: false}); }
if(!request.args){ utilsService.ipcSend(request.responceChannel, {success: false}); }
const res = await bsLocalVersionService.deleteVersion(request.args);
utilsService.newIpcSenc(request.responceChannel, {success: res});
utilsService.ipcSend(request.responceChannel, {success: res});
});
+10 -10
View File
@@ -1,33 +1,33 @@
import { ipcMain } from 'electron';
import { UtilsService } from '../services/utils.service';
import { BSVersionManagerService } from '../services/bs-version-manager.service'
import { BSVersion } from "main/services/bs-version-manager.service";
import { BSVersionLibService } from '../services/bs-version-lib.service'
import { BSVersion } from "main/services/bs-version-lib.service";
import path from 'path';
import { exec } from 'child_process';
import { SteamService } from '../services/steam.service';
import { BS_APP_ID } from '../constants';
import { IpcRequest } from 'shared/models/ipc-models.model';
import { IpcRequest } from 'shared/models/ipc';
import { BSLocalVersionService } from '../services/bs-local-version.service';
import { InstallationLocationService } from '../services/installation-location.service';
ipcMain.on('bs-version.get-version-dict', (event, req: IpcRequest<void>) => {
BSVersionManagerService.getInstance().getAvailableVersions().then(versions => {
UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: true, data: versions});
BSVersionLibService.getInstance().getAvailableVersions().then(versions => {
UtilsService.getInstance().ipcSend(req.responceChannel, {success: true, data: versions});
}).catch(() => {
UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: false});
UtilsService.getInstance().ipcSend(req.responceChannel, {success: false});
})
});
ipcMain.on('bs-version.installed-versions', async (event, req: IpcRequest<void>) => {
BSLocalVersionService.getInstance().getInstalledVersions().then(versions => {
UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: true, data: versions});
UtilsService.getInstance().ipcSend(req.responceChannel, {success: true, data: versions});
}).catch(() => {
UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: false});
UtilsService.getInstance().ipcSend(req.responceChannel, {success: false});
})
});
ipcMain.on("bs-version.open-folder", async (event, args: BSVersion) => {
ipcMain.on("bs-version.open-folder", async (event, req: IpcRequest<BSVersion>) => {
const locationService = InstallationLocationService.getInstance();
const versionFolder = args.steam ? await SteamService.getInstance().getGameFolder(BS_APP_ID, "Beat Saber") : path.join(locationService.versionsDirectory, args.BSVersion);
const versionFolder = req.args.steam ? await SteamService.getInstance().getGameFolder(BS_APP_ID, "Beat Saber") : path.join(locationService.versionsDirectory, req.args.BSVersion);
UtilsService.getInstance().folderExist(versionFolder) && exec(`start "" "${versionFolder}"`);
});
+2 -3
View File
@@ -1,8 +1,7 @@
import { ipcMain, shell, dialog } from 'electron';
import { UtilsService } from '../services/utils.service';
import { IpcRequest } from '../../shared/models/ipc-models.model';
import { IpcRequest } from 'shared/models/ipc';
import { getMainWindow } from '../main';
import { request } from 'http';
ipcMain.on('window.close', async () => {
getMainWindow()?.close();
@@ -26,7 +25,7 @@ ipcMain.on('new-window', async (event, request: IpcRequest<string>) => {
ipcMain.on('choose-folder', async (event, request: IpcRequest<void>) => {
dialog.showOpenDialog({properties: ['openDirectory']}).then(res => {
UtilsService.getInstance().newIpcSenc(request.responceChannel, {success: true, data: res});
UtilsService.getInstance().ipcSend(request.responceChannel, {success: true, data: res});
});
});
+5 -5
View File
@@ -1,6 +1,6 @@
import { BS_APP_ID, BS_DEPOT } from "../constants";
import path from "path";
import { BSVersion, BSVersionManagerService } from "./bs-version-manager.service";
import { BSVersion, BSVersionLibService } from "./bs-version-lib.service";
import { UtilsService } from "./utils.service";
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import log from "electron-log";
@@ -12,13 +12,13 @@ export class BSInstallerService{
private static instance: BSInstallerService;
private readonly utils: UtilsService;
private readonly bsVersionService: BSVersionManagerService;
private readonly bsVersionService: BSVersionLibService;
private readonly installLocationService: InstallationLocationService;
private downloadProcess: ChildProcessWithoutNullStreams;
private constructor(){
this.bsVersionService = BSVersionManagerService.getInstance();
this.bsVersionService = BSVersionLibService.getInstance();
this.utils = UtilsService.getInstance();
this.installLocationService = InstallationLocationService.getInstance();
}
@@ -34,7 +34,7 @@ export class BSInstallerService{
private sendDownloadEvent(event: DownloadEventType, data?: string|number, success: boolean = true): void{
if(typeof data === "string"){ data = data.replaceAll(/\[|\]/g, ""); }
this.utils.newIpcSenc(`bs-download.${event}`, { success: success, data: data });
this.utils.ipcSend(`bs-download.${event}`, { success: success, data: data });
}
public sendInputProcess(input: string){
@@ -57,7 +57,7 @@ export class BSInstallerService{
public async downloadBsVersion(downloadInfos: DownloadInfo): Promise<DownloadEvent>{
if(this.downloadProcess && !this.downloadProcess?.killed){ console.log("*** AlreadyDownloading ***"); return {type: "[AlreadyDownloading]"}; }
const bsVersion = this.bsVersionService.getVersionDetailFromVersionNumber(downloadInfos.bsVersion.BSVersion);
const bsVersion = this.bsVersionService.getVersionDetails(downloadInfos.bsVersion.BSVersion);
if(!bsVersion){ return {type: "[Error]"}; }
this.utils.createFolderIfNotExist(this.installLocationService.versionsDirectory);
+7 -7
View File
@@ -1,19 +1,19 @@
import path from "path";
import { BsLaunchResult, LauchOption } from "shared/models/launch-models.model";
import { BSInstallerService } from "./bs-installer.service";
import { BSVersion } from "./bs-version-manager.service";
import { BSVersion } from "./bs-version-lib.service";
import { UtilsService } from "./utils.service";
import { BS_EXECUTABLE, BS_APP_ID } from "../constants";
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { SteamService } from "./steam.service";
import { InstallationLocationService } from "./installation-location.service";
export class BSLauncherService{
private static instance: BSLauncherService;
private readonly utilsService: UtilsService;
private readonly installerService: BSInstallerService;
private readonly steamService: SteamService;
private readonly installLocationService: InstallationLocationService;
private bsProcess: ChildProcessWithoutNullStreams;
@@ -24,8 +24,8 @@ export class BSLauncherService{
private constructor(){
this.utilsService = UtilsService.getInstance();
this.installerService = BSInstallerService.getInstance();
this.steamService = SteamService.getInstance();
this.installLocationService = InstallationLocationService.getInstance();
}
public isBsRunning(): boolean{
@@ -33,7 +33,7 @@ export class BSLauncherService{
}
public isExeExist(version: BSVersion): boolean{
const exePath = path.join(this.installerService.installationFolder, version.BSVersion, BS_EXECUTABLE);
const exePath = path.join(this.installLocationService.versionsDirectory, version.BSVersion, BS_EXECUTABLE);
return this.utilsService.pathExist(exePath);
}
@@ -42,7 +42,7 @@ export class BSLauncherService{
if(!this.isExeExist(launchOptions.version)){ return "EXE_NOT_FINDED"; }
if(this.isBsRunning()){ return "BS_ALREADY_RUNNING" }
const cwd = launchOptions.version.steam ? await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber") : path.join(this.installerService.installationFolder, launchOptions.version.BSVersion);
const cwd = launchOptions.version.steam ? await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber") : path.join(this.installLocationService.versionsDirectory, launchOptions.version.BSVersion);
const exePath = path.join(cwd, BS_EXECUTABLE);
const launchMods = [launchOptions.oculus && "-vrmode oculus", launchOptions.desktop && "fpfc", launchOptions.debug && "--verbose"];
@@ -50,7 +50,7 @@ export class BSLauncherService{
this.bsProcess.on('message', msg => { /** EMIT HERE **/ });
this.bsProcess.on('error', err => { /** EMIT HERE **/ });
this.bsProcess.on('exit', code => this.utilsService.newIpcSenc("bs-launch.exit", {data: code, success: true}));
this.bsProcess.on('exit', code => this.utilsService.ipcSend("bs-launch.exit", {data: code, success: true}));
return "LAUNCHED";
}
@@ -1,4 +1,4 @@
import { BSVersion, BSVersionManagerService } from "./bs-version-manager.service";
import { BSVersion, BSVersionLibService } from "./bs-version-lib.service";
import { InstallationLocationService } from "./installation-location.service";
import { SteamService } from "./steam.service";
import { UtilsService } from "./utils.service";
@@ -14,7 +14,7 @@ export class BSLocalVersionService{
private readonly installLocationService: InstallationLocationService;
private readonly utilsService: UtilsService;
private readonly steamService: SteamService;
private readonly remoteVersionService: BSVersionManagerService;
private readonly remoteVersionService: BSVersionLibService;
public static getInstance(): BSLocalVersionService{
if(!BSLocalVersionService.instance){ BSLocalVersionService.instance = new BSLocalVersionService(); }
@@ -25,7 +25,7 @@ export class BSLocalVersionService{
this.installLocationService = InstallationLocationService.getInstance();
this.utilsService = UtilsService.getInstance();
this.steamService = SteamService.getInstance();
this.remoteVersionService = BSVersionManagerService.getInstance();
this.remoteVersionService = BSVersionLibService.getInstance();
}
private async getVersionOfBSFolder(bsPath: string): Promise<string>{
@@ -53,7 +53,7 @@ export class BSLocalVersionService{
if(steamBsFolder && this.utilsService.pathExist(steamBsFolder)){
const steamBsVersion = await this.getVersionOfBSFolder(steamBsFolder);
if(steamBsVersion){
const steamVersionDetails = this.remoteVersionService.getVersionDetailFromVersionNumber(steamBsVersion);
const steamVersionDetails = this.remoteVersionService.getVersionDetails(steamBsVersion);
versions.push(steamVersionDetails ? {...steamVersionDetails, steam: true} : {BSVersion: steamBsVersion, steam: true});
}
}
@@ -63,7 +63,7 @@ export class BSLocalVersionService{
const folderInInstallation = this.utilsService.listDirsInDir(this.installLocationService.versionsDirectory);
folderInInstallation.forEach(f => {
const version = this.remoteVersionService.getVersionDetailFromVersionNumber(f);
const version = this.remoteVersionService.getVersionDetails(f);
versions.push(version);
})
return versions;
@@ -1,15 +1,14 @@
import { get } from 'https'
import { UtilsService } from './utils.service';
import path, { resolve } from 'path';
import { createReadStream, writeFileSync } from 'fs';
import { createInterface } from 'readline';
import path from 'path';
import { writeFileSync } from 'fs';
export class BSVersionManagerService{
export class BSVersionLibService{
private static readonly REMOTE_BS_VERSIONS_URL: string = "https://raw.githubusercontent.com/Zagrios/bs-manager/master/assets/jsons/bs-versions.json"
private static readonly VERSIONS_FILE: string = "bs-versions.json";
private readonly REMOTE_BS_VERSIONS_URL: string = "https://raw.githubusercontent.com/Zagrios/bs-manager/master/assets/jsons/bs-versions.json"
private readonly VERSIONS_FILE: string = "bs-versions.json";
private static instance: BSVersionManagerService;
private static instance: BSVersionLibService;
private utilsService: UtilsService;
@@ -20,15 +19,15 @@ export class BSVersionManagerService{
}
public static getInstance(): BSVersionManagerService{
if(!BSVersionManagerService.instance){ BSVersionManagerService.instance = new BSVersionManagerService(); }
return BSVersionManagerService.instance;
public static getInstance(): BSVersionLibService{
if(!BSVersionLibService.instance){ BSVersionLibService.instance = new BSVersionLibService(); }
return BSVersionLibService.instance;
}
private getRemoteVersions(): Promise<BSVersion[]>{
return new Promise<BSVersion[]>((resolve, reject) => {
let body = ''
get(BSVersionManagerService.REMOTE_BS_VERSIONS_URL, (res) => {
get(this.REMOTE_BS_VERSIONS_URL, (res) => {
res.on('data', chunk => body += chunk);
res.on('end', () => {
this.bsVersions = JSON.parse(body);
@@ -40,18 +39,17 @@ export class BSVersionManagerService{
}
private async getLocalVersions(): Promise<BSVersion[]>{
const localVersionsPath = path.join(this.utilsService.getAssestsJsonsPath(), BSVersionManagerService.VERSIONS_FILE);
const localVersionsPath = path.join(this.utilsService.getAssestsJsonsPath(), this.VERSIONS_FILE);
const rawVersion = (await this.utilsService.readFileAsync(localVersionsPath)).toString();
return JSON.parse(rawVersion);
}
private async updateLocalVersions(versions: BSVersion[]): Promise<void>{
const localVersionsPath = path.join(this.utilsService.getAssestsJsonsPath(), BSVersionManagerService.VERSIONS_FILE);
const localVersionsPath = path.join(this.utilsService.getAssestsJsonsPath(), this.VERSIONS_FILE);
writeFileSync(localVersionsPath, JSON.stringify(versions, null, "\t"), {encoding: 'utf-8', flag: 'w'});
};
public async loadBsVersions(): Promise<BSVersion[]>{
if(this.bsVersions && this.bsVersions.length){ return this.bsVersions; }
private async loadBsVersions(): Promise<BSVersion[]>{
const [localVersions, remoteVersions] = await Promise.all([
this.getLocalVersions(), this.getRemoteVersions()
]);
@@ -62,33 +60,14 @@ export class BSVersionManagerService{
}
public async getAvailableVersions(): Promise<BSVersion[]>{
if(this.bsVersions && this.bsVersions.length){ return this.bsVersions; }
const bsVersions = await this.loadBsVersions();
if(!bsVersions || !bsVersions.length){ return []; }
return bsVersions;
}
public async getVersionOfBSFolder(bsPath: string): Promise<string>{
const versionFilePath = path.join(bsPath, 'Beat Saber_Data', 'globalgamemanagers');
if(!this.utilsService.pathExist(versionFilePath)){ return null; }
const versionsAvailable = await this.getAvailableVersions();
return new Promise<string>(async (resolve, reject) => {
const readLine = createInterface({ input: createReadStream(versionFilePath) });
let findVersion: string = null;
readLine.on('line', (line) => {
for(const version of versionsAvailable){
if(line.includes(version.BSVersion)){ findVersion = version.BSVersion; readLine.close(); }
}
});
readLine.on('close', () => {
if(findVersion){ resolve(findVersion) }
resolve(findVersion);
});
})
}
public getVersionDetailFromVersionNumber(version: string): BSVersion{
public getVersionDetails(version: string): BSVersion{
return this.bsVersions.find(v => v.BSVersion === version);
}
+2 -6
View File
@@ -4,7 +4,7 @@ import { homedir } from "os";
import path from "path";
import { BrowserWindow } from "electron";
import { rm } from "fs/promises";
import { IpcResponse } from "shared/models/ipc-models.model";
import { IpcResponse } from "shared/models/ipc";
export class UtilsService{
@@ -68,11 +68,7 @@ export class UtilsService{
return rm(folderPath, {recursive: true});
}
public ipcSend(channel: string, args?: any){
this.mainWindow.webContents.send(channel, args);
}
public newIpcSenc(channel: string, response: IpcResponse<any>): void{
public ipcSend(channel: string, response: IpcResponse<any>): void{
this.mainWindow.webContents.send(channel, response);
}