Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Zagrios/bs-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Zagrios committed Jan 24, 2023
2 parents d889c85 + cc4d033 commit 2154d90
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
34 changes: 23 additions & 11 deletions src/main/services/bs-installer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,26 @@ export class BSInstallerService{
if(!(await isOnline({timeout: 1500}))){ throw "no-internet"; }

this.utils.createFolderIfNotExist(this.installLocationService.versionsDirectory);

const dest = this.getPathNotAleardyExist(await this.localVersionService.getVersionPath(bsVersion));

const downloadVersion: BSVersion = {...downloadInfos.bsVersion, ...(path.basename(dest) !== downloadInfos.bsVersion.BSVersion && {name: path.basename(dest)})}

this.downloadProcess = spawn(
this.getDepotDownloaderExePath(),
[
`-app ${BS_APP_ID}`,
`-depot ${BS_DEPOT}`,
`-manifest ${bsVersion.BSManifest}`,
`-username ${downloadInfos.username}`,
`-dir \"${this.localVersionService.getVersionFolder(bsVersion)}\"`,
`-dir \"${this.localVersionService.getVersionFolder(downloadVersion)}\"`,
(downloadInfos.stay || !downloadInfos.password) && "-remember-password"
],
{shell: true, cwd: this.installLocationService.versionsDirectory}
);

this.utils.ipcSend("start-download-version", {success: true, data: downloadVersion});

return new Promise((resolve, reject) => {

this.downloadProcess.stdout.on('data', data => {
Expand Down Expand Up @@ -160,23 +167,28 @@ export class BSInstallerService{
})
}

public async importVersion(path: string): Promise<PartialBSVersion>{

const rawBsVersion = await this.localVersionService.getVersionOfBSFolder(path);

if(!rawBsVersion){ throw new Error("NOT_BS_FOLDER"); }

const originalPath = await this.localVersionService.getVersionPath(rawBsVersion);
let destPath = originalPath;
private getPathNotAleardyExist(path: string): string{
let destPath = path;
let folderExist = this.utils.pathExist(destPath);
let i = 0;

while(folderExist){
i++;
destPath = `${originalPath} (${i})`;
destPath = `${path} (${i})`;
folderExist = this.utils.pathExist(destPath);
}

return destPath
}

public async importVersion(path: string): Promise<PartialBSVersion>{

const rawBsVersion = await this.localVersionService.getVersionOfBSFolder(path);

if(!rawBsVersion){ throw new Error("NOT_BS_FOLDER"); }

const destPath = this.getPathNotAleardyExist(await this.localVersionService.getVersionPath(rawBsVersion));

await copy(path, destPath, {dereference: true});

return rawBsVersion;
Expand All @@ -197,7 +209,7 @@ export interface DownloadInfo {

export interface DownloadEvent{
type: DownloadEventType,
data?: unknown,
data?: unknown
}

export type DownloadEventType = "[Password]" | "[Guard]" | "[2FA]" | "[Progress]" | "[Validated]" | "[Finished]" | "[AlreadyDownloading]" | "[Error]" | "[Warning]" | "[SteamID]" | "[Exit]" | "[NoInternet]";
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ export function BsVersionItem(props: {version: BSVersion}) {
)
}

const cancel = () => {
const versionDownload = downloaderService.currentBsVersionDownload$.value;
downloaderService.cancelDownload().then(async res => {
if(!res.success){ return; }
if(!downloaderService.isVerification){
bsUninstallerService.uninstall(versionDownload).then(res => res && verionManagerService.askInstalledVersions());
}
const cancel = () => {
const versionDownload = downloaderService.currentBsVersionDownload$.value;
downloaderService.cancelDownload().then(async res => {
if(!res.success){ return; }
bsUninstallerService.uninstall(versionDownload).then(res => res && verionManagerService.askInstalledVersions());
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/available-versions-list.components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function AvailableVersionsList() {

const startDownload = () => {
setDownloading(() => true)
bsDownloaderService.download(versionSelected, versionManagerService.isVersionInstalled(versionSelected)).finally(() => {
bsDownloaderService.download(versionSelected).finally(() => {
setDownloading(() => false);
});
}
Expand Down Expand Up @@ -91,7 +91,7 @@ export function AvailableVersionsList() {
<AnimatePresence>
{ versionSelected && !downloading && (
<motion.div initial={{y:"150%"}} animate={{y:"0%"}} exit={{y:"150%"}} className="absolute bottom-5" onClick={startDownload}>
<BsmButton text={versionManagerService.isVersionInstalled(versionSelected) ? "misc.verify" : "misc.download"} className="relative text-gray-800 dark:text-gray-100 rounded-md text-3xl font-bold italic tracking-wide px-3 pb-2 pt-1 shadow-md shadow-black"/>
<BsmButton text="misc.download" className="relative text-gray-800 dark:text-gray-100 rounded-md text-3xl font-bold italic tracking-wide px-3 pb-2 pt-1 shadow-md shadow-black"/>
</motion.div>
)}
</AnimatePresence>
Expand Down
17 changes: 8 additions & 9 deletions src/renderer/services/bs-downloader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export class BsDownloaderService{
private readonly notificationService: NotificationService;
private readonly linkOpener: LinkOpenerService;

private _isVerification: boolean = false;

public readonly currentBsVersionDownload$: BehaviorSubject<BSVersion> = new BehaviorSubject(null);
public readonly downloadProgress$: BehaviorSubject<number> = new BehaviorSubject(0);
public readonly selectedBsVersion$: BehaviorSubject<BSVersion> = new BehaviorSubject(null);
Expand Down Expand Up @@ -71,10 +69,15 @@ export class BsDownloaderService{
this.ipcService.sendLazy('bs-download.[2FA]', {args: res.data});
});

this.ipcService.watch<BSVersion>("start-download-version").subscribe(res => {
this.currentBsVersionDownload$.next(res.data);
});

this.currentBsVersionDownload$.subscribe(version => {
if(version){ this.bsVersionManager.setInstalledVersions([...this.bsVersionManager.installedVersions$.value, version]); }
else{ this.bsVersionManager.askInstalledVersions(); }
});

}

private resetDownload(): void{
Expand All @@ -91,7 +94,7 @@ export class BsDownloaderService{
return this.ipcService.send<boolean>("is-dotnet-6-installed").then(res => res.success && res.data)
}

public async download(bsVersion: BSVersion, isVerification?: boolean, isFirstCall = true): Promise<IpcResponse<DownloadEvent>>{
public async download(bsVersion: BSVersion, isFirstCall = true): Promise<IpcResponse<DownloadEvent>>{
if(isFirstCall && !this.progressBarService.require()){ return {success: false}; }

if(isFirstCall && !(await this.isDotNet6Installed())){
Expand All @@ -111,7 +114,6 @@ export class BsDownloaderService{
}

this.progressBarService.show(this.downloadProgress$);
this._isVerification = !!isVerification;

let promise;
if(!this.authService.sessionExist()){
Expand All @@ -127,25 +129,22 @@ export class BsDownloaderService{
promise = this.ipcService.send<DownloadEvent>('bs-download.start', {args: {bsVersion, username: this.authService.getSteamUsername()}});
}

this.currentBsVersionDownload$.next(bsVersion);

let res = await promise;

if(res.data?.type === "[Password]"){
this.authService.deleteSteamSession();
res = await this.download(bsVersion, isVerification, false);
res = await this.download(bsVersion, false);
}

this.progressBarService.hide(true);
this.resetDownload();
if(res.success && isFirstCall){ this.notificationService.notifySuccess({title: `notifications.bs-download.success.titles.${isVerification ? "verification-finished" : "download-success"}`, duration: 3000}); }
if(res.success && isFirstCall){ this.notificationService.notifySuccess({title: "notifications.bs-download.success.titles.download-success", duration: 3000}); }
else if(res.data && isFirstCall){ this.notificationService.notifyError({title: `notifications.types.error`, desc: `notifications.bs-download.errors.msg.${res.data}`, duration: 3000}); }

return res;
}

public get isDownloading(): boolean{ return !!this.currentBsVersionDownload$.value; }
public get isVerification(): boolean{ return this._isVerification; }

public async getInstallationFolder(): Promise<string>{
const res = await this.ipcService.send<string>("bs-download.installation-folder");
Expand Down

0 comments on commit 2154d90

Please sign in to comment.