Merge pull request #566 from silentrald/feat/560

[feat-560] ask user for installation folder/path
This commit is contained in:
MathieuG-P
2024-09-11 12:45:37 +02:00
committed by GitHub
24 changed files with 400 additions and 65 deletions
+27
View File
@@ -0,0 +1,27 @@
import { pathExistsSync } from "fs-extra";
import { from, of } from "rxjs";
import { InstallationLocationService } from "main/services/installation-location.service";
import { IpcService } from "main/services/ipc.service";
const ipc = IpcService.getInstance();
ipc.on("bs-installer.folder-exists", (_, reply) => {
const service = InstallationLocationService.getInstance();
reply(of(pathExistsSync(service.installationDirectory())));
});
ipc.on("bs-installer.default-install-path", (_, reply) => {
const service = InstallationLocationService.getInstance();
reply(of(service.defaultInstallationDirectory()));
});
ipc.on("bs-installer.install-path", (_, reply) => {
const service = InstallationLocationService.getInstance();
reply(of(service.installationDirectory()));
});
ipc.on("bs-installer.set-install-path", (args, reply) => {
const service = InstallationLocationService.getInstance();
reply(from(service.setInstallationDirectory(args.path, args.move)));
});
@@ -1,8 +1,7 @@
import { BsOculusDownloaderService } from "../../services/bs-version-download/bs-oculus-downloader.service";
import { BsSteamDownloaderService } from "../../services/bs-version-download/bs-steam-downloader.service";
import { InstallationLocationService } from "../../services/installation-location.service";
import { IpcService } from "../../services/ipc.service";
import { from, of } from "rxjs";
import { of } from "rxjs";
import { BSLocalVersionService } from "../../services/bs-local-version.service";
const ipc = IpcService.getInstance();
@@ -14,16 +13,6 @@ ipc.on("import-version", (args, reply) => {
// #region Steam
ipc.on("bs-download.installation-folder", (_, reply) => {
const installLocation = InstallationLocationService.getInstance();
reply(of(installLocation.installationDirectory()));
});
ipc.on("bs-download.set-installation-folder", (args, reply) => {
const installerService = InstallationLocationService.getInstance();
reply(from(installerService.setInstallationDirectory(args)));
});
ipc.on("auto-download-bs-version", (args, reply) => {
const bsInstaller = BsSteamDownloaderService.getInstance();
reply(bsInstaller.autoDownloadBsVersion(args));
+1
View File
@@ -1,4 +1,5 @@
import "./os-controls-ipcs";
import "./bs-installer-ipcs.ts";
import "./bs-launcher-ipcs";
import "./bs-version-ipcs";
import "./bs-uninstall-ipcs";
+3
View File
@@ -24,6 +24,9 @@ contextBridge.exposeInMainWorld("electron", {
},
path: {
sep,
basename: (path: string): string => {
return !path ? "" : path.split(sep).at(-1);
},
join: (...args: string[]): string => {
return args.join(sep);
}
+21 -3
View File
@@ -1,5 +1,7 @@
import ElectronStore from "electron-store";
import fs from "fs-extra";
import { InstallationLocationService } from "./installation-location.service";
import { CustomError } from "shared/models/exceptions/custom-error.class";
export class ConfigurationService {
private static instance: ConfigurationService;
@@ -13,17 +15,23 @@ export class ConfigurationService {
private readonly locations: InstallationLocationService;
private contentPath: string;
private store: ElectronStore;
private constructor() {
this.locations = InstallationLocationService.getInstance();
this.initStore();
this.initStore(false);
this.locations.onInstallLocationUpdate(() => { this.initStore() });
this.locations.onInstallLocationUpdate(() => this.initStore(true));
}
private async initStore() {
private async initStore(createFolder: boolean) {
const contentPath = this.locations.installationDirectory();
if (!createFolder && !fs.pathExistsSync(contentPath)) {
return;
}
this.contentPath = contentPath;
this.store = new ElectronStore({
cwd: contentPath,
name: "config",
@@ -32,15 +40,25 @@ export class ConfigurationService {
});
}
private checkStore(): void {
// Can be null if config.cfg does not exist or corrupted
if (!this.store) {
throw CustomError.fromError(new Error(`Can't read config.cfg on ${this.contentPath}`));
}
}
public set(key: string, value: unknown): void {
this.checkStore();
this.store.set(key, value);
}
public get<T>(key: string): T {
this.checkStore();
return this.store.get(key) as T;
}
public delete(key: string): void {
this.checkStore();
this.store.delete(key);
}
}
@@ -40,18 +40,22 @@ export class InstallationLocationService {
this.updateListeners.forEach(listener => listener());
}
public async setInstallationDirectory(newDir: string): Promise<string> {
/**
* @param move - if true, move the old installation path to the path param
*/
public async setInstallationDirectory(newDir: string, move: boolean): Promise<string> {
newDir = path.basename(newDir) === this.INSTALLATION_FOLDER ? path.join(newDir, "..") : newDir;
const oldDir = this.installationDirectory();
await ensureFolderExist(oldDir);
await copyDirectoryWithJunctions(oldDir, path.join(newDir, this.INSTALLATION_FOLDER), { overwrite: true });
if (move) {
const oldDir = this.installationDirectory();
await ensureFolderExist(oldDir);
await copyDirectoryWithJunctions(oldDir, path.join(newDir, this.INSTALLATION_FOLDER), { overwrite: true });
deleteFolder(oldDir);
}
this._installationDirectory = newDir;
this.staticConfig.set(this.STORE_INSTALLATION_PATH_KEY, newDir);
deleteFolder(oldDir);
return this.installationDirectory();
}
@@ -59,6 +63,12 @@ export class InstallationLocationService {
this.updateListeners.add(fn);
}
public defaultInstallationDirectory(): string {
const { result: oldPath } = tryit(() => path.join(app.getPath("documents"), this.INSTALLATION_FOLDER));
const installationDirectory = (oldPath && pathExistsSync(oldPath)) ? app.getPath("documents") : app.getPath("home");
return path.join(installationDirectory, this.INSTALLATION_FOLDER);
}
public installationDirectory(): string {
const installParentPath = () => {