[feat-578] simplified getting the wine binary

This commit is contained in:
silentrald
2024-10-31 18:42:18 +08:00
parent 599161b7b2
commit b04f199fff
5 changed files with 25 additions and 47 deletions
+1
View File
@@ -20,4 +20,5 @@ export const HTTP_STATUS_CODES = constants;
// Linux related stuff
export const PROTON_BINARY_PREFIX = "proton";
export const WINE_BINARY_PREFIX = path.join("files", "bin", "wine64");
+1 -1
View File
@@ -3,9 +3,9 @@ import { IpcService } from "../services/ipc.service";
import { of } from "rxjs";
const ipc = IpcService.getInstance();
const linuxService = LinuxService.getInstance();
ipc.on("linux.verify-proton-folder", (_, reply) => {
const linuxService = LinuxService.getInstance();
reply(of(linuxService.verifyProtonPath()));
});
+14 -36
View File
@@ -1,8 +1,6 @@
import log from "electron-log";
import path from "path";
import { pathExistsSync } from "fs-extra";
import { PROTON_BINARY_PREFIX } from "main/constants";
import { exec } from "child_process";
import { PROTON_BINARY_PREFIX, WINE_BINARY_PREFIX } from "main/constants";
import { StaticConfigurationService } from "./static-configuration.service";
export class LinuxService {
@@ -15,12 +13,8 @@ export class LinuxService {
return LinuxService.instance;
}
private readonly WINE_BINARY_PREFIX = path.join("files", "bin", "wine");
private readonly staticConfig: StaticConfigurationService;
private winePath = "";
private constructor() {
this.staticConfig = StaticConfigurationService.getInstance();
}
@@ -35,39 +29,23 @@ export class LinuxService {
}
const protonPath = path.join(protonFolder, PROTON_BINARY_PREFIX);
return pathExistsSync(protonPath);
const winePath = path.join(protonFolder, WINE_BINARY_PREFIX);
return pathExistsSync(protonPath) && pathExistsSync(winePath);
}
public async getWinePath(): Promise<string> {
if (this.winePath) {
return this.winePath;
public getWinePath(): string {
if (!this.staticConfig.has("proton-folder")) {
throw new Error("proton-folder variable not set");
}
return new Promise((resolve, reject) => {
// Get the default installed wine
exec("wine --version", (error, stdout) => {
if (!error) {
log.info("found", stdout);
this.winePath = "wine";
return resolve("wine");
}
const winePath = path.join(
this.staticConfig.get("proton-folder"),
WINE_BINARY_PREFIX
);
if (!pathExistsSync(winePath)) {
throw new Error(`"${winePath}" binary file not found`);
}
log.warn("wine is not installed, falling back to proton's wine binary");
log.warn("wine warn:", error);
// Fallback to the use the wine bundled in proton
if (!this.staticConfig.has("proton-folder")) {
return reject(new Error("proton folder not in configuration setup"));
}
const tmpWinePath = path.join(this.staticConfig.get("proton-folder"), this.WINE_BINARY_PREFIX);
if (!pathExistsSync(tmpWinePath)) {
return reject(new Error("wine binary not in proton folder"));
}
this.winePath = tmpWinePath;
resolve(this.winePath);
});
});
return winePath;
}
}
@@ -18,6 +18,7 @@ import { ensureDir, pathExistsSync } from "fs-extra";
import { CustomError } from "shared/models/exceptions/custom-error.class";
import { popElement } from "shared/helpers/array.helpers";
import { LinuxService } from "../linux.service";
import { tryit } from "shared/helpers/error.helpers";
export class BsModsManagerService {
private static instance: BsModsManagerService;
@@ -143,17 +144,14 @@ export class BsModsManagerService {
return false;
}
let cmd = "";
let cmd = `"${ipaPath}" "${bsExePath}" ${args.join(" ")}`;
if (process.platform === "linux") {
try {
const winePath = await this.linuxService.getWinePath();
cmd = `"${winePath}" "${ipaPath}" "${bsExePath}" ${args.join(" ")}`;
} catch (error) {
log.error("wine not found", error);
const { error, result: winePath } = tryit(() => this.linuxService.getWinePath());
if (error) {
log.error(error);
return false;
}
} else {
cmd = `"${ipaPath}" "${bsExePath}" ${args.join(" ")}`;
cmd = `"${winePath}" ${cmd}`;
}
return new Promise<boolean>(resolve => {
@@ -1,7 +1,7 @@
import ElectronStore from "electron-store";
import { pathExistsSync } from "fs-extra";
import path from "path";
import { PROTON_BINARY_PREFIX } from "main/constants";
import { PROTON_BINARY_PREFIX, WINE_BINARY_PREFIX } from "main/constants";
import { Observable, Subject } from "rxjs";
import { BSVersion } from "shared/bs-version.interface";
import { CustomError } from "shared/models/exceptions/custom-error.class";
@@ -60,7 +60,8 @@ export class StaticConfigurationService {
private validateProtonFolder(protonFolder: string): void {
const protonPath = path.join(protonFolder, PROTON_BINARY_PREFIX);
if (!pathExistsSync(protonPath)) {
const winePath = path.join(protonFolder, WINE_BINARY_PREFIX);
if (!pathExistsSync(protonPath) || !pathExistsSync(winePath)) {
throw new CustomError("Invalid proton folder path", "invalid-folder");
}
}