[feat-560] ask user for installation folder/path

This commit is contained in:
silentrald
2024-08-23 22:09:32 +08:00
parent eba2b6fb03
commit 5e6837b8cd
22 changed files with 314 additions and 43 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)));
});
@@ -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);
}
+8 -3
View File
@@ -1,4 +1,5 @@
import ElectronStore from "electron-store";
import fs from "fs-extra";
import { InstallationLocationService } from "./installation-location.service";
export class ConfigurationService {
@@ -17,13 +18,17 @@ export class ConfigurationService {
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.store = new ElectronStore({
cwd: contentPath,
name: "config",
@@ -59,6 +59,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 = () => {
@@ -0,0 +1,103 @@
import { lastValueFrom } from "rxjs";
import { useEffect, useState } from "react";
import { useTranslation } from "renderer/hooks/use-translation.hook";
import { useService } from "renderer/hooks/use-service.hook";
import { IpcService } from "renderer/services/ipc.service";
import { ModalComponent, ModalExitCode } from "renderer/services/modale.service";
import { BsmButton } from "renderer/components/shared/bsm-button.component";
export const AskInstallPathModal: ModalComponent<{ installPath: string }, {}> = ({ resolver }) => {
const t = useTranslation();
const ipcService = useService(IpcService);
const [installPath, setInstallPath] = useState("");
const [installFolder, setInstallFolder] = useState("");
const [defaultInstallPath, setDefaultInstallPath] = useState("");
useEffect(() => {
lastValueFrom(ipcService.sendV2("bs-installer.default-install-path"))
.then(defaultPath => {
setInstallPath(defaultPath);
setDefaultInstallPath(defaultPath);
setInstallFolder(window.electron.path.basename(defaultPath));
});
}, []);
const selectInstallPath = async () => {
const response = await lastValueFrom(ipcService.sendV2("choose-folder"));
if (response.canceled || !response.filePaths?.length) {
return;
}
const path = response.filePaths[0];
setInstallPath(
window.electron.path.basename(path) === installFolder ?
path :
window.electron.path.join(response.filePaths[0], installFolder)
);
}
const onDefaultButtonPressed = () => {
setInstallPath(defaultInstallPath);
}
const onConfirmButtonPressed = () => {
resolver({
data: { installPath },
exitCode: ModalExitCode.COMPLETED
});
}
return (
<form
className="static min-w-96"
onSubmit={event => {
event.preventDefault();
onConfirmButtonPressed();
}}>
<h1 className="
tracking-wide w-full
uppercase text-3xl text-center text-gray-800 dark:text-gray-200
">
{t("modals.ask-install-path.title")}
</h1>
<div className="
relative rounded-md pl-2 py-1 my-3
flex items-center justify-between
w-full h-8 bg-light-main-color-1 dark:bg-main-color-1
">
<span className="block text-ellipsis overflow-hidden min-w-0" title={installPath}>
{installPath}
</span>
<BsmButton
onClick={selectInstallPath}
className="shrink-1 whitespace-nowrap mr-2 px-2 font-bold italic text-sm rounded-md"
text={"modals.ask-install-path.choose-folder"}
withBar={false}
/>
</div>
<div className="grid grid-flow-col grid-cols-4 row-start-3 gap-4">
<BsmButton
typeColor="cancel"
className="col-start-3 rounded-md text-center transition-all"
onClick={onDefaultButtonPressed}
withBar={false}
text={"modals.ask-install-path.default"}
/>
<BsmButton
typeColor="primary"
className="col-start-4 z-0 px-1 rounded-md text-center transition-all"
type="submit"
withBar={false}
text={"misc.confirm"}
/>
</div>
</form>
)
}
@@ -17,7 +17,7 @@ export function Modal() {
useEffect(() => {
const onEscape = (e: KeyboardEvent) => {
if (e.key !== "Escape") {
if (currentModal.options.closable === false || e.key !== "Escape") {
return;
}
currentModal.resolver({ exitCode: ModalExitCode.CLOSED });
@@ -34,6 +34,20 @@ export function Modal() {
};
}, [currentModal]);
const renderCloseButton = (modal: ModalObject) => {
return (
<div
className="w-2.5 h-2.5 absolute top-2.5 right-1.5 cursor-pointer"
onClick={e => {
e.stopPropagation();
modal.resolver({ exitCode: ModalExitCode.CLOSED });
}}
>
<BsmIcon className="size-full" icon="cross" />
</div>
)
}
const renderModal = (modal: ModalObject) => {
if (!modal?.modal) { return null; }
@@ -44,23 +58,23 @@ export function Modal() {
return (
<div className="relative p-4 text-gray-800 dark:text-gray-200 rounded-md shadow-lg shadow-black bg-gradient-to-br from-light-main-color-3 to-light-main-color-2 dark:from-main-color-3 dark:to-main-color-2">
<ThemeColorGradientSpliter className="absolute top-0 w-full left-0 h-1 rounded-t-md overflow-hidden"/>
<div
className="w-2.5 h-2.5 absolute top-2.5 right-1.5 cursor-pointer"
onClick={e => {
e.stopPropagation();
modal.resolver({ exitCode: ModalExitCode.CLOSED });
}}
>
<BsmIcon className="size-full" icon="cross" />
</div>
{modal.options?.closable === false ? undefined : renderCloseButton(modal)}
<modal.modal resolver={modal.resolver} options={modal.options} />
</div>
)
}
const onOverlayClicked = () => {
if (currentModal.options.closable === false) {
return;
}
currentModal.resolver({ exitCode: ModalExitCode.NO_CHOICE });
}
return (
<AnimatePresence>
{currentModal ? <motion.span key={crypto.randomUUID()} onClick={() => currentModal.resolver({ exitCode: ModalExitCode.NO_CHOICE })} className="fixed size-full bg-black z-[90]" initial={{ opacity: 0 }} animate={{ opacity: currentModal && 0.6 }} exit={{ opacity: 0 }} transition={{ duration: 0.2 }} /> : undefined}
{currentModal ? <motion.span key={crypto.randomUUID()} onClick={onOverlayClicked} className="fixed size-full bg-black z-[90]" initial={{ opacity: 0 }} animate={{ opacity: currentModal && 0.6 }} exit={{ opacity: 0 }} transition={{ duration: 0.2 }} /> : undefined}
{modals?.map(modal => (
<motion.div key={crypto.randomUUID()} className="fixed z-[90] top-1/2 left-1/2" initial={{ y: "100vh", x: "-50%" }} animate={{y: "-50%", scale: modal === currentModal ? 1 : 0, opacity: modal === currentModal ? 1 : 0, display: modal === currentModal ? "block" : ["block", "none"]}} exit={{ y: "100vh" }}>
{renderModal(modal)}
+1
View File
@@ -10,6 +10,7 @@ declare global {
};
path: {
sep: "/"|"\\";
basename: (path: string) => string;
join: (...args: string[]) => string;
};
};
@@ -8,7 +8,6 @@ import { NotificationService } from "../notification.service";
import { ProgressBarService } from "../progress-bar.service";
import { LoginToSteamModal } from "renderer/components/modal/modal-types/bs-downgrade/login-to-steam-modal.component";
import { SteamGuardModal } from "renderer/components/modal/modal-types/bs-downgrade/steam-guard-modal.component";
import { LinkOpenerService } from "../link-opener.service";
import { DepotDownloaderErrorEvent, DepotDownloaderEvent, DepotDownloaderEventType, DepotDownloaderInfoEvent, DepotDownloaderWarningEvent } from "../../../shared/models/bs-version-download/depot-downloader.model";
import { SteamMobileApproveModal } from "renderer/components/modal/modal-types/bs-downgrade/steam-mobile-approve-modal.component";
import { DownloaderServiceInterface } from "./bs-store-downloader.interface";
@@ -30,7 +29,6 @@ export class SteamDownloaderService extends AbstractBsDownloaderService implemen
private readonly ipcService: IpcService;
private readonly progressBarService: ProgressBarService;
private readonly notificationService: NotificationService;
private readonly linkOpener: LinkOpenerService;
private readonly STEAM_SESSION_USERNAME_KEY = "STEAM-USERNAME";
@@ -42,7 +40,6 @@ export class SteamDownloaderService extends AbstractBsDownloaderService implemen
this.modalService = ModalService.getInstance();
this.progressBarService = ProgressBarService.getInstance();
this.notificationService = NotificationService.getInstance();
this.linkOpener = LinkOpenerService.getInstance();
}
private setSteamSession(username: string): void { localStorage.setItem(this.STEAM_SESSION_USERNAME_KEY, username); }
@@ -50,12 +47,14 @@ export class SteamDownloaderService extends AbstractBsDownloaderService implemen
public deleteSteamSession(): void { localStorage.removeItem(this.STEAM_SESSION_USERNAME_KEY); }
public sessionExist(): boolean { return !!localStorage.getItem(this.STEAM_SESSION_USERNAME_KEY); }
// TODO: Move to another service in the future
public async getInstallationFolder(): Promise<string> {
return lastValueFrom(this.ipcService.sendV2("bs-download.installation-folder"));
return lastValueFrom(this.ipcService.sendV2("bs-installer.install-path"));
}
// TODO: Move to another service in the future
public setInstallationFolder(path: string): Observable<string> {
return this.ipcService.sendV2("bs-download.set-installation-folder", path);
return this.ipcService.sendV2("bs-installer.set-install-path", path);
}
// ### Downloading
+1 -1
View File
@@ -39,7 +39,7 @@ export class ModalService {
}
}
export type ModalOptions<T = unknown> = { readonly data?: T, readonly noStyle?: boolean }
export type ModalOptions<T = unknown> = { readonly data?: T, readonly noStyle?: boolean, readonly closable?: boolean }
export type ModalComponent<Return = unknown, Receive = unknown> = ({ resolver, options }: { readonly resolver: (x: ModalResponse<Return>) => void; readonly options?: ModalOptions<Receive> }) => JSX.Element;
export type ModalObject = {modal: ModalComponent, resolver: (value: ModalResponse | PromiseLike<ModalResponse>) => void, options: ModalOptions};
+66
View File
@@ -0,0 +1,66 @@
import { lastValueFrom } from "rxjs";
import { logRenderError } from "renderer";
import { BSVersionManagerService } from "./bs-version-manager.service";
import { IpcService } from "./ipc.service";
import { ModalExitCode, ModalResponse, ModalService } from "./modale.service";
import { SteamDownloaderService } from "./bs-version-download/steam-downloader.service";
import { AskInstallPathModal } from "renderer/components/modal/modal-types/ask-install-path.component";
// Handle setup modals/prompts, ordering of the modals/prompts may be done here
export class SetupService {
private static instance: SetupService;
private readonly ipcService: IpcService;
private readonly modalService: ModalService;
private readonly steamDownloaderService: SteamDownloaderService;
private readonly versionManagerService: BSVersionManagerService;
private constructor() {
this.ipcService = IpcService.getInstance();
this.modalService = ModalService.getInstance();
this.steamDownloaderService = SteamDownloaderService.getInstance();
this.versionManagerService = BSVersionManagerService.getInstance();
}
public static getInstance(): SetupService {
if (!SetupService.instance) {
SetupService.instance = new SetupService();
}
return SetupService.instance;
}
public async check(): Promise<void> {
try {
await this.checkInstallationPath();
} catch (error) {
logRenderError(error);
}
}
private async checkInstallationPath(): Promise<void> {
try {
const exists = await lastValueFrom(this.ipcService.sendV2("bs-installer.folder-exists"))
if (exists) {
return;
}
let modalResponse: ModalResponse<{ installPath: string }> = { exitCode: ModalExitCode.NO_CHOICE };
while (modalResponse.exitCode !== ModalExitCode.COMPLETED) {
modalResponse = await this.modalService.openModal(
AskInstallPathModal,
{ closable: false }
);
}
await lastValueFrom(this.steamDownloaderService.setInstallationFolder(modalResponse.data.installPath));
// Refresh the versions tab
await this.versionManagerService.askInstalledVersions()
} catch (error) {
logRenderError(error);
}
}
}
+6 -1
View File
@@ -23,6 +23,7 @@ import { ConfigurationService } from "renderer/services/configuration.service";
import { OsDiagnosticService } from "renderer/services/os-diagnostic.service";
import { useService } from "renderer/hooks/use-service.hook";
import { AutoUpdaterService } from "renderer/services/auto-updater.service";
import { SetupService } from "renderer/services/setup.service";
import { gt, parse } from "semver"
import { logRenderError } from "renderer";
@@ -35,13 +36,17 @@ export default function App() {
const notification = useService(NotificationService);
const config = useService(ConfigurationService);
const autoUpdater = useService(AutoUpdaterService);
const setup = useService(SetupService);
const location = useLocation();
const navigate = useNavigate();
useEffect(() => {
checkIsUpdated();
checkOneClicks();
setup.check()
.then(() => {
checkOneClicks();
})
}, []);
const checkIsUpdated = async () => {
+6 -2
View File
@@ -27,8 +27,6 @@ export interface IpcChannelMapping {
/* ** bs-download-ipcs ** */
"import-version": { request: ImportVersionOptions, response: Progression<BSVersion>};
"bs-download.installation-folder": { request: void, response: string};
"bs-download.set-installation-folder": { request: string, response: string};
"auto-download-bs-version": { request: DownloadSteamInfo, response: DepotDownloaderEvent};
"download-bs-version": { request: DownloadSteamInfo, response: DepotDownloaderEvent};
"download-bs-version-qr": { request: DownloadSteamInfo, response: DepotDownloaderEvent};
@@ -44,6 +42,12 @@ export interface IpcChannelMapping {
"bsv-search-playlist": {request: PlaylistSearchParams, response: BsvPlaylist[]};
"bsv-get-playlist-details-by-id": {request: {id: string, page: number}, response: BsvPlaylistPage};
/* ** bs-installer-ipcs ** */
"bs-installer.folder-exists": { require: void, response: boolean };
"bs-installer.default-install-path": { request: void, response: string };
"bs-installer.install-path": { request: void, response: string};
"bs-installer.set-install-path": { request: string, response: string};
/* ** bs-launcher-ipcs ** */
"create-launch-shortcut": { request: LaunchOption, response: boolean };
"bs-launch.need-start-as-admin": { request: void, response: boolean };