[feat] support %command% env args to windows and oculus

* refactored additionalArgs to command and change its type to string instead of string array
This commit is contained in:
silentrald
2025-01-28 13:41:16 +08:00
parent 889e792b3c
commit 1c2321b438
19 changed files with 74 additions and 73 deletions
@@ -8,6 +8,7 @@ import { LinuxService } from "../linux.service";
import { BsmShellLog, bsmSpawn } from "main/helpers/os.helpers";
import { IS_FLATPAK } from "main/constants";
import { LaunchMods } from "shared/models/bs-launch/launch-option.interface";
import { parseEnvString } from "main/helpers/env.helpers";
export function buildBsLaunchArgs(launchOptions: LaunchOption): string[] {
const launchArgs = [];
@@ -29,8 +30,8 @@ export function buildBsLaunchArgs(launchOptions: LaunchOption): string[] {
launchArgs.push("editor");
}
if (launchOptions.additionalArgs) {
launchArgs.push(...launchOptions.additionalArgs);
if (launchOptions.command) {
launchArgs.push(launchOptions.command);
}
return Array.from(new Set(launchArgs).values());
@@ -46,6 +47,8 @@ export abstract class AbstractLauncherService {
this.localVersions = BSLocalVersionService.getInstance();
}
private readonly COMMAND_FORMAT = "%command%";
protected launchBSProcess(bsExePath: string, args: string[], options?: SpawnBsProcessOptions): ChildProcessWithoutNullStreams {
const spawnOptions: SpawnOptionsWithoutStdio = { detached: true, cwd: path.dirname(bsExePath), ...(options || {}) };
@@ -116,6 +119,34 @@ export abstract class AbstractLauncherService {
return { process, exit };
}
protected injectAdditionalArgsEnvs(
launchOptions: LaunchOption,
env: Record<string, string>
) {
if (!launchOptions.command) {
return;
}
const { command } = launchOptions;
const index = command.indexOf(this.COMMAND_FORMAT);
if (index === -1) {
return;
}
const envString = command.substring(0, index);
log.info("Parsing env string ", `"${envString}"`)
for (const [ key, value ] of Object.entries(parseEnvString(envString))) {
if (key in env) {
log.warn("Ignoring", `${key}=${value}`, "already set env launch command");
} else {
log.info("Injecting", `${key}="${value}"`, "to the env launch command");
}
}
launchOptions.command = command.substring(index + this.COMMAND_FORMAT.length);
}
}
export type SpawnBsProcessOptions = {
@@ -95,10 +95,6 @@ export class BSLauncherService {
const params = objectFromEntries(shortcutLink.searchParams.entries()) as ShortcutParams;
if(typeof params.additionalArgs === "string"){
params.additionalArgs = [params.additionalArgs];
}
return params;
}
@@ -123,7 +119,7 @@ export class BSLauncherService {
oculus: params.versionOculus === "true",
ino: +params.versionIno
},
additionalArgs: params.additionalArgs,
command: params.command,
launchMods,
};
@@ -141,7 +137,7 @@ export class BSLauncherService {
if(launchOptions.launchMods?.includes(LaunchMods.OCULUS)){ res.oculusMode = "true"; }
if(launchOptions.launchMods?.includes(LaunchMods.FPFC)){ res.desktopMode = "true"; }
if(launchOptions.launchMods?.includes(LaunchMods.DEBUG)){ res.debug = "true"; }
if(launchOptions.additionalArgs){ res.additionalArgs = launchOptions.additionalArgs; }
if(launchOptions.command){ res.command = launchOptions.command; }
if(launchOptions.launchMods?.includes(LaunchMods.SKIP_STEAM)){ res.skipSteam = "true"; }
if(launchOptions.launchMods?.includes(LaunchMods.PROTON_LOGS)){ res.protonLogs = "true"; }
@@ -287,7 +283,7 @@ type ShortcutParams = {
oculusMode?: string;
desktopMode?: string;
debug?: string;
additionalArgs?: string[];
command?: string;
skipSteam?: string;
protonLogs?: string;
version: string;
@@ -1,4 +1,4 @@
import { Observable, ReplaySubject } from "rxjs";
import { Observable } from "rxjs";
import { StoreLauncherInterface } from "./store-launcher.interface";
import { BSLaunchError, BSLaunchEvent, BSLaunchEventData, LaunchOption } from "../../../shared/models/bs-launch";
import { OculusService } from "../oculus.service";
@@ -9,7 +9,6 @@ import { pathExists } from "fs-extra";
import { AbstractLauncherService, buildBsLaunchArgs } from "./abstract-launcher.service";
import { isProcessRunning } from "../../helpers/os.helpers";
import { CustomError } from "../../../shared/models/exceptions/custom-error.class";
import { UtilsService } from "../utils.service";
export class OculusLauncherService extends AbstractLauncherService implements StoreLauncherInterface {
@@ -23,14 +22,10 @@ export class OculusLauncherService extends AbstractLauncherService implements St
}
private readonly oculus: OculusService;
private readonly util: UtilsService;
private readonly oculusLib$ = new ReplaySubject<string>();
private constructor() {
super();
this.oculus = OculusService.getInstance();
this.util = UtilsService.getInstance();
}
public launch(launchOptions: LaunchOption): Observable<BSLaunchEventData> {
@@ -54,10 +49,17 @@ export class OculusLauncherService extends AbstractLauncherService implements St
// Make sure Oculus is running
await this.oculus.startOculus().catch(err => log.error("Error while starting Oculus", err));
const env: Record<string, string> = {};
this.injectAdditionalArgsEnvs(launchOptions, env);
obs.next({type: BSLaunchEvent.BS_LAUNCHING});
// Launch Beat Saber
const process = this.launchBs(exePath, buildBsLaunchArgs(launchOptions));
const process = this.launchBs(
exePath,
buildBsLaunchArgs(launchOptions),
{ env }
);
return process.exit.catch(err => {
throw CustomError.fromError(err, BSLaunchError.BS_EXIT_ERROR);
@@ -102,7 +102,6 @@ export class SteamLauncherService extends AbstractLauncherService implements Sto
await this.restoreSteamVR().catch(log.error);
}
const launchArgs = buildBsLaunchArgs(launchOptions);
const steamPath = await this.steam.getSteamPath();
const env = {
@@ -122,6 +121,9 @@ export class SteamLauncherService extends AbstractLauncherService implements Sto
Object.assign(env, linuxSetup.env);
}
this.injectAdditionalArgsEnvs(launchOptions, env);
const launchArgs = buildBsLaunchArgs(launchOptions);
obs.next({type: BSLaunchEvent.BS_LAUNCHING});
const spawnOpts = { env, cwd: bsFolderPath };
-20
View File
@@ -10,7 +10,6 @@ import { BsmShellLog, bsmExec } from "main/helpers/os.helpers";
import { LaunchMods } from "shared/models/bs-launch/launch-option.interface";
import { SteamShortcutData } from "shared/models/steam/shortcut.model";
import { buildBsLaunchArgs } from "./bs-launcher/abstract-launcher.service";
import { parseEnvString } from "main/helpers/env.helpers";
export class LinuxService {
private static instance: LinuxService;
@@ -25,7 +24,6 @@ export class LinuxService {
private readonly installLocationService: InstallationLocationService;
private readonly staticConfig: StaticConfigurationService;
private readonly COMMAND_FORMAT = "%command%";
private nixOS: boolean | undefined;
private constructor() {
@@ -114,24 +112,6 @@ export class LinuxService {
envVars.PROTON_LOG_DIR = path.join(bsFolderPath, "Logs");
}
if (launchOptions.additionalArgs) {
const additionalArgs = launchOptions.additionalArgs.join(" ");
const index = additionalArgs.indexOf(this.COMMAND_FORMAT);
if (index > -1) {
const envString = additionalArgs.substring(0, index);
log.info("Parsing env string ", `"${envString}"`)
for (const [ key, value ] of Object.entries(parseEnvString(envString))) {
if (key in envVars) {
log.warn("Ignoring", `${key}=${value}`, "already set env launch command");
} else {
log.info("Injecting", `${key}="${value}"`, "to the env launch command");
}
}
}
launchOptions.additionalArgs = [ additionalArgs.substring(index + this.COMMAND_FORMAT.length) ];
}
return envVars;
}