mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
split installerService into two and no need for uninstallerService now
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import { ipcMain } from "electron";
|
||||
import { BSUninstallerService } from "../services/bs-uninstaller.service";
|
||||
import { BSVersion } from "main/services/bs-version-manager.service";
|
||||
import { UtilsService } from "../services/utils.service";
|
||||
import { IpcRequest } from "shared/models/ipc-models.model";
|
||||
import { BSLocalVersionService } from "../services/bs-local-version.service";
|
||||
|
||||
ipcMain.on('bs.uninstall', async (event, request: IpcRequest<BSVersion>) => {
|
||||
|
||||
const bsUninstallerService = BSUninstallerService.getInstance();
|
||||
const bsLocalVersionService = BSLocalVersionService.getInstance();
|
||||
const utilsService = UtilsService.getInstance();
|
||||
|
||||
if(!request.args){ utilsService.newIpcSenc(request.responceChannel, {success: false}); }
|
||||
|
||||
const res = await bsUninstallerService.uninstall(request.args);
|
||||
const res = await bsLocalVersionService.deleteVersion(request.args);
|
||||
utilsService.newIpcSenc(request.responceChannel, {success: res});
|
||||
});
|
||||
@@ -1,6 +1,5 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { UtilsService } from '../services/utils.service';
|
||||
import { BSInstallerService } from '../services/bs-installer.service';
|
||||
import { BSVersionManagerService } from '../services/bs-version-manager.service'
|
||||
import { BSVersion } from "main/services/bs-version-manager.service";
|
||||
import path from 'path';
|
||||
@@ -8,8 +7,10 @@ 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 { BSLocalVersionService } from '../services/bs-local-version.service';
|
||||
import { InstallationLocationService } from '../services/installation-location.service';
|
||||
|
||||
ipcMain.on('bs-version.request-versions', (event, req: IpcRequest<void>) => {
|
||||
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});
|
||||
}).catch(() => {
|
||||
@@ -18,7 +19,7 @@ ipcMain.on('bs-version.request-versions', (event, req: IpcRequest<void>) => {
|
||||
});
|
||||
|
||||
ipcMain.on('bs-version.installed-versions', async (event, req: IpcRequest<void>) => {
|
||||
BSInstallerService.getInstance().getInstalledBsVersion().then(versions => {
|
||||
BSLocalVersionService.getInstance().getInstalledVersions().then(versions => {
|
||||
UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: true, data: versions});
|
||||
}).catch(() => {
|
||||
UtilsService.getInstance().newIpcSenc(req.responceChannel, {success: false});
|
||||
@@ -26,17 +27,7 @@ ipcMain.on('bs-version.installed-versions', async (event, req: IpcRequest<void>)
|
||||
});
|
||||
|
||||
ipcMain.on("bs-version.open-folder", async (event, args: BSVersion) => {
|
||||
const bsInstallerService = BSInstallerService.getInstance();
|
||||
let versionFolder = ""
|
||||
if(args.steam){
|
||||
const steamService = SteamService.getInstance();
|
||||
versionFolder = await steamService.getGameFolder(BS_APP_ID, "Beat Saber");
|
||||
console.log(versionFolder)
|
||||
}
|
||||
else{
|
||||
versionFolder = path.join(bsInstallerService.installationFolder, args.BSVersion);
|
||||
}
|
||||
if(UtilsService.getInstance().folderExist(versionFolder)){
|
||||
exec(`start "" "${versionFolder}"`);
|
||||
}
|
||||
})
|
||||
const locationService = InstallationLocationService.getInstance();
|
||||
const versionFolder = args.steam ? await SteamService.getInstance().getGameFolder(BS_APP_ID, "Beat Saber") : path.join(locationService.versionsDirectory, args.BSVersion);
|
||||
UtilsService.getInstance().folderExist(versionFolder) && exec(`start "" "${versionFolder}"`);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { BS_APP_ID, BS_DEPOT } from "../constants";
|
||||
import path from "path";
|
||||
import { BSVersion, BSVersionManagerService } from "./bs-version-manager.service";
|
||||
import { SteamService } from "./steam.service";
|
||||
import { UtilsService } from "./utils.service";
|
||||
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
|
||||
import log from "electron-log";
|
||||
@@ -10,12 +9,9 @@ import { ctrlc } from "ctrlc-windows";
|
||||
|
||||
export class BSInstallerService{
|
||||
|
||||
private static readonly INSTALLATION_FOLDER = "BSInstances";
|
||||
|
||||
private static instance: BSInstallerService;
|
||||
|
||||
private readonly utils: UtilsService;
|
||||
private readonly steamService: SteamService;
|
||||
private readonly bsVersionService: BSVersionManagerService;
|
||||
private readonly installLocationService: InstallationLocationService;
|
||||
|
||||
@@ -24,14 +20,9 @@ export class BSInstallerService{
|
||||
private constructor(){
|
||||
this.bsVersionService = BSVersionManagerService.getInstance();
|
||||
this.utils = UtilsService.getInstance();
|
||||
this.steamService = SteamService.getInstance();
|
||||
this.installLocationService = InstallationLocationService.getInstance();
|
||||
}
|
||||
|
||||
public get installationFolder(): string{
|
||||
return path.join(this.installLocationService.installationDirectory, BSInstallerService.INSTALLATION_FOLDER);
|
||||
}
|
||||
|
||||
public static getInstance(){
|
||||
if(!BSInstallerService.instance){ BSInstallerService.instance = new BSInstallerService(); }
|
||||
return BSInstallerService.instance;
|
||||
@@ -41,27 +32,10 @@ export class BSInstallerService{
|
||||
return path.join(this.utils.getAssetsScriptsPath(), 'depot-downloader', 'DepotDownloader.exe');
|
||||
}
|
||||
|
||||
public async getInstalledBsVersion(): Promise<BSVersion[]>{
|
||||
const versions: BSVersion[] = [];
|
||||
const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber")
|
||||
if(steamBsFolder && this.utils.pathExist(steamBsFolder)){
|
||||
const steamBsVersion = await this.bsVersionService.getVersionOfBSFolder(steamBsFolder);
|
||||
if(steamBsVersion){
|
||||
const steamVersionDetails = this.bsVersionService.getVersionDetailFromVersionNumber(steamBsVersion);
|
||||
versions.push(steamVersionDetails ? {...steamVersionDetails, steam: true} : {BSVersion: steamBsVersion, steam: true});
|
||||
}
|
||||
}
|
||||
|
||||
if(!this.utils.folderExist(this.installationFolder)){ return versions }
|
||||
|
||||
const folderInInstallation = this.utils.listDirsInDir(this.installationFolder);
|
||||
|
||||
folderInInstallation.forEach(f => {
|
||||
const version = this.bsVersionService.getVersionDetailFromVersionNumber(f);
|
||||
versions.push(version);
|
||||
})
|
||||
return versions;
|
||||
}
|
||||
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 });
|
||||
}
|
||||
|
||||
public sendInputProcess(input: string){
|
||||
if(this.downloadProcess.stdin.writable){
|
||||
@@ -86,7 +60,7 @@ export class BSInstallerService{
|
||||
const bsVersion = this.bsVersionService.getVersionDetailFromVersionNumber(downloadInfos.bsVersion.BSVersion);
|
||||
if(!bsVersion){ return {type: "[Error]"}; }
|
||||
|
||||
this.utils.createFolderIfNotExist(this.installationFolder);
|
||||
this.utils.createFolderIfNotExist(this.installLocationService.versionsDirectory);
|
||||
this.downloadProcess = spawn(
|
||||
this.getDepotDownloaderExePath(),
|
||||
[
|
||||
@@ -97,7 +71,7 @@ export class BSInstallerService{
|
||||
`-dir ${bsVersion.BSVersion}`,
|
||||
(downloadInfos.stay || !downloadInfos.password) && "-remember-password"
|
||||
],
|
||||
{shell: true, cwd: this.installationFolder}
|
||||
{shell: true, cwd: this.installLocationService.versionsDirectory}
|
||||
);
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
@@ -162,11 +136,6 @@ 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 });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { BSVersion, BSVersionManagerService } from "./bs-version-manager.service";
|
||||
import { InstallationLocationService } from "./installation-location.service";
|
||||
import { SteamService } from "./steam.service";
|
||||
import { UtilsService } from "./utils.service";
|
||||
import { BS_APP_ID } from "../constants";
|
||||
import path from "path";
|
||||
import { createInterface } from "readline";
|
||||
import { createReadStream } from "fs";
|
||||
|
||||
export class BSLocalVersionService{
|
||||
|
||||
private static instance: BSLocalVersionService;
|
||||
|
||||
private readonly installLocationService: InstallationLocationService;
|
||||
private readonly utilsService: UtilsService;
|
||||
private readonly steamService: SteamService;
|
||||
private readonly remoteVersionService: BSVersionManagerService;
|
||||
|
||||
public static getInstance(): BSLocalVersionService{
|
||||
if(!BSLocalVersionService.instance){ BSLocalVersionService.instance = new BSLocalVersionService(); }
|
||||
return BSLocalVersionService.instance;
|
||||
}
|
||||
|
||||
private constructor(){
|
||||
this.installLocationService = InstallationLocationService.getInstance();
|
||||
this.utilsService = UtilsService.getInstance();
|
||||
this.steamService = SteamService.getInstance();
|
||||
this.remoteVersionService = BSVersionManagerService.getInstance();
|
||||
}
|
||||
|
||||
private 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.remoteVersionService.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 async getInstalledVersions(): Promise<BSVersion[]>{
|
||||
const versions: BSVersion[] = [];
|
||||
const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber")
|
||||
if(steamBsFolder && this.utilsService.pathExist(steamBsFolder)){
|
||||
const steamBsVersion = await this.getVersionOfBSFolder(steamBsFolder);
|
||||
if(steamBsVersion){
|
||||
const steamVersionDetails = this.remoteVersionService.getVersionDetailFromVersionNumber(steamBsVersion);
|
||||
versions.push(steamVersionDetails ? {...steamVersionDetails, steam: true} : {BSVersion: steamBsVersion, steam: true});
|
||||
}
|
||||
}
|
||||
|
||||
if(!this.utilsService.folderExist(this.installLocationService.versionsDirectory)){ return versions }
|
||||
|
||||
const folderInInstallation = this.utilsService.listDirsInDir(this.installLocationService.versionsDirectory);
|
||||
|
||||
folderInInstallation.forEach(f => {
|
||||
const version = this.remoteVersionService.getVersionDetailFromVersionNumber(f);
|
||||
versions.push(version);
|
||||
})
|
||||
return versions;
|
||||
}
|
||||
|
||||
public async deleteVersion(version: BSVersion): Promise<boolean>{
|
||||
if(version.steam){ return false; }
|
||||
const versionFolder = path.join(this.installLocationService.versionsDirectory, version.BSVersion);
|
||||
if(!this.utilsService.folderExist(versionFolder)){ return true; }
|
||||
|
||||
return this.utilsService.deleteFolder(versionFolder)
|
||||
.then(() => { return true; })
|
||||
.catch(() => { return false; })
|
||||
}
|
||||
|
||||
public cloneVersion(version: BSVersion, name: string): boolean{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import path from "path";
|
||||
import { BSInstallerService } from "./bs-installer.service";
|
||||
import { BSVersion } from "./bs-version-manager.service";
|
||||
import { UtilsService } from "./utils.service";
|
||||
|
||||
export class BSUninstallerService {
|
||||
|
||||
private static instance: BSUninstallerService;
|
||||
|
||||
private readonly utilsService: UtilsService;
|
||||
private readonly bsInstallerService: BSInstallerService;
|
||||
|
||||
public static getInstance(): BSUninstallerService {
|
||||
if(!BSUninstallerService.instance){ BSUninstallerService.instance = new BSUninstallerService(); }
|
||||
return BSUninstallerService.instance;
|
||||
}
|
||||
|
||||
private constructor(){
|
||||
this.utilsService = UtilsService.getInstance();
|
||||
this.bsInstallerService = BSInstallerService.getInstance();
|
||||
}
|
||||
|
||||
public async uninstall(version :BSVersion): Promise<boolean>{
|
||||
if(version.steam){ return false; }
|
||||
const versionFolder = path.join(this.bsInstallerService.installationFolder, version.BSVersion);
|
||||
if(!this.utilsService.folderExist(versionFolder)){ return true; }
|
||||
|
||||
return this.utilsService.deleteFolder(versionFolder)
|
||||
.then(() => { return true; })
|
||||
.catch(() => { return false; })
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export class InstallationLocationService {
|
||||
private static instance: InstallationLocationService;
|
||||
|
||||
private readonly INSTALLATION_FOLDER = "BSManager";
|
||||
private readonly VERSIONS_FOLDER = "BSInstances";
|
||||
private readonly STORE_INSTALLATION_PATH_KEY = "installation-folder";
|
||||
|
||||
|
||||
@@ -33,9 +34,8 @@ export class InstallationLocationService {
|
||||
this._installationDirectory = (this.configService.get(this.STORE_INSTALLATION_PATH_KEY) || this.utilsService.getUserDocumentsFolder()) as string;
|
||||
}
|
||||
|
||||
public get installationDirectory(): string{
|
||||
return path.join(this._installationDirectory, this.INSTALLATION_FOLDER);
|
||||
}
|
||||
public get installationDirectory(): string{ return path.join(this._installationDirectory, this.INSTALLATION_FOLDER); }
|
||||
public get versionsDirectory(): string { return path.join(this.installationDirectory, this.VERSIONS_FOLDER); }
|
||||
|
||||
public setInstallationDirectory(newDir: string): Promise<string>{
|
||||
const oldDir = this.installationDirectory;
|
||||
|
||||
@@ -37,7 +37,7 @@ export class BSVersionManagerService {
|
||||
}
|
||||
|
||||
public askAvailableVersions(): void{
|
||||
this.ipcService.send<BSVersion[]>("bs-version.request-versions").then(res => this.availableVersions$.next(res.data));
|
||||
this.ipcService.send<BSVersion[]>("bs-version.get-version-dict").then(res => this.availableVersions$.next(res.data));
|
||||
}
|
||||
|
||||
public askInstalledVersions(): void{
|
||||
|
||||
Reference in New Issue
Block a user