Fix v0.4.0 process handler, add override menu (#430)
* Fix Windows and Linux launch * Add process handler selector, pin Prisma * Regenerate lcofkiel * Fix torrential inclusion in image * Fix layouting * Implement tree kill for Windows * Fix server lint
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
use std::{fs::create_dir_all, path::PathBuf, process::Command};
|
||||
use std::{
|
||||
fs::create_dir_all,
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use client::compat::{COMPAT_INFO, UMU_LAUNCHER_EXECUTABLE};
|
||||
use database::{
|
||||
Database, DownloadableMetadata, GameVersion, db::DATA_ROOT_DIR, platform::Platform,
|
||||
};
|
||||
|
||||
use crate::{error::ProcessError, process_manager::ProcessHandler};
|
||||
use crate::{error::ProcessError, parser::ParsedCommand, process_manager::ProcessHandler};
|
||||
|
||||
pub struct MacLauncher;
|
||||
impl ProcessHandler for MacLauncher {
|
||||
@@ -25,11 +29,89 @@ impl ProcessHandler for MacLauncher {
|
||||
}
|
||||
|
||||
fn modify_command(&self, _command: &mut Command) {}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"macos"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Direct"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Launches the game directly on macOS."
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
|
||||
#[cfg_attr(not(target_os = "windows"), allow(unused_variables))]
|
||||
fn apply_no_window(command: &mut Command) {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
command.creation_flags(CREATE_NO_WINDOW);
|
||||
}
|
||||
}
|
||||
|
||||
enum WindowsLaunchStrategy {
|
||||
Direct,
|
||||
Cmd,
|
||||
Powershell,
|
||||
}
|
||||
|
||||
// Wrap a launch command for Windows; with no strategy, detect it from the file extension.
|
||||
fn windows_launch_command(
|
||||
launch_command: String,
|
||||
current_dir: &str,
|
||||
strategy: Option<WindowsLaunchStrategy>,
|
||||
) -> Result<String, ProcessError> {
|
||||
let mut parsed = ParsedCommand::parse(launch_command)?;
|
||||
|
||||
let strategy = strategy.unwrap_or_else(|| {
|
||||
let extension = Path::new(&parsed.command)
|
||||
.extension()
|
||||
.and_then(|ext| ext.to_str())
|
||||
.map(str::to_ascii_lowercase);
|
||||
match extension.as_deref() {
|
||||
Some("ps1") => WindowsLaunchStrategy::Powershell,
|
||||
Some("exe") | Some("com") => WindowsLaunchStrategy::Direct,
|
||||
_ => WindowsLaunchStrategy::Cmd,
|
||||
}
|
||||
});
|
||||
|
||||
match strategy {
|
||||
// PowerShell scripts
|
||||
WindowsLaunchStrategy::Powershell => {
|
||||
parsed.make_absolute(PathBuf::from(current_dir));
|
||||
let script = std::mem::replace(&mut parsed.command, "powershell".to_owned());
|
||||
let mut args = vec![
|
||||
"-NoProfile".to_owned(),
|
||||
"-ExecutionPolicy".to_owned(),
|
||||
"Bypass".to_owned(),
|
||||
"-File".to_owned(),
|
||||
script,
|
||||
];
|
||||
args.append(&mut parsed.args);
|
||||
parsed.args = args;
|
||||
}
|
||||
// Direct executables
|
||||
WindowsLaunchStrategy::Direct => {
|
||||
parsed.make_absolute(PathBuf::from(current_dir));
|
||||
}
|
||||
// cmd.exe, for batch files, builtins, PATHEXT resolution, %VAR% expansion, etc.
|
||||
WindowsLaunchStrategy::Cmd => {
|
||||
let command = std::mem::replace(&mut parsed.command, "cmd".to_owned());
|
||||
let mut args = vec!["/C".to_owned(), command];
|
||||
args.append(&mut parsed.args);
|
||||
parsed.args = args;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(parsed.reconstruct())
|
||||
}
|
||||
|
||||
pub struct WindowsLauncher;
|
||||
impl ProcessHandler for WindowsLauncher {
|
||||
fn create_launch_process(
|
||||
@@ -37,22 +119,169 @@ impl ProcessHandler for WindowsLauncher {
|
||||
_meta: &DownloadableMetadata,
|
||||
launch_command: String,
|
||||
_game_version: &GameVersion,
|
||||
_current_dir: &str,
|
||||
current_dir: &str,
|
||||
_database: &Database,
|
||||
) -> Result<String, ProcessError> {
|
||||
Ok(format!("pwsh \"cmd /C \"{}\"\"", launch_command))
|
||||
windows_launch_command(launch_command, current_dir, None)
|
||||
}
|
||||
|
||||
fn valid_for_platform(&self, _db: &Database, _target: &Platform) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn modify_command(&self, command: &mut Command) {
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::os::windows::process::CommandExt;
|
||||
#[cfg(target_os = "windows")]
|
||||
command.creation_flags(CREATE_NO_WINDOW);
|
||||
apply_no_window(command);
|
||||
}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"windows-auto"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Automatic"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Detects the file type and launches it directly, or through cmd or PowerShell."
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WindowsDirectLauncher;
|
||||
impl ProcessHandler for WindowsDirectLauncher {
|
||||
fn create_launch_process(
|
||||
&self,
|
||||
_meta: &DownloadableMetadata,
|
||||
launch_command: String,
|
||||
_game_version: &GameVersion,
|
||||
current_dir: &str,
|
||||
_database: &Database,
|
||||
) -> Result<String, ProcessError> {
|
||||
windows_launch_command(launch_command, current_dir, Some(WindowsLaunchStrategy::Direct))
|
||||
}
|
||||
|
||||
fn valid_for_platform(&self, _db: &Database, _target: &Platform) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn modify_command(&self, command: &mut Command) {
|
||||
apply_no_window(command);
|
||||
}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"windows-direct"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Direct executable"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Runs the executable directly, without a shell."
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WindowsCmdLauncher;
|
||||
impl ProcessHandler for WindowsCmdLauncher {
|
||||
fn create_launch_process(
|
||||
&self,
|
||||
_meta: &DownloadableMetadata,
|
||||
launch_command: String,
|
||||
_game_version: &GameVersion,
|
||||
current_dir: &str,
|
||||
_database: &Database,
|
||||
) -> Result<String, ProcessError> {
|
||||
windows_launch_command(launch_command, current_dir, Some(WindowsLaunchStrategy::Cmd))
|
||||
}
|
||||
|
||||
fn valid_for_platform(&self, _db: &Database, _target: &Platform) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn modify_command(&self, command: &mut Command) {
|
||||
apply_no_window(command);
|
||||
}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"windows-cmd"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Command Prompt (cmd)"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Launches through cmd.exe. Supports batch files, builtins and %VAR% expansion."
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WindowsPowershellLauncher;
|
||||
impl ProcessHandler for WindowsPowershellLauncher {
|
||||
fn create_launch_process(
|
||||
&self,
|
||||
_meta: &DownloadableMetadata,
|
||||
launch_command: String,
|
||||
_game_version: &GameVersion,
|
||||
current_dir: &str,
|
||||
_database: &Database,
|
||||
) -> Result<String, ProcessError> {
|
||||
windows_launch_command(
|
||||
launch_command,
|
||||
current_dir,
|
||||
Some(WindowsLaunchStrategy::Powershell),
|
||||
)
|
||||
}
|
||||
|
||||
fn valid_for_platform(&self, _db: &Database, _target: &Platform) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn modify_command(&self, command: &mut Command) {
|
||||
apply_no_window(command);
|
||||
}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"windows-powershell"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"PowerShell"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Runs the command as a PowerShell script (-File)."
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LinuxNativeLauncher;
|
||||
impl ProcessHandler for LinuxNativeLauncher {
|
||||
fn create_launch_process(
|
||||
&self,
|
||||
_meta: &DownloadableMetadata,
|
||||
launch_command: String,
|
||||
_game_version: &GameVersion,
|
||||
_current_dir: &str,
|
||||
_database: &Database,
|
||||
) -> Result<String, ProcessError> {
|
||||
// Run native Linux games directly, no umu-run wrapper
|
||||
Ok(launch_command)
|
||||
}
|
||||
|
||||
fn valid_for_platform(&self, _db: &Database, _target: &Platform) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn modify_command(&self, _command: &mut Command) {}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"linux-native"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Native (direct)"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Runs the native Linux game directly on the host."
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +330,18 @@ impl ProcessHandler for UMUNativeLauncher {
|
||||
}
|
||||
|
||||
fn modify_command(&self, _command: &mut Command) {}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"linux-umu"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Steam Linux Runtime (umu-run)"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Runs the native Linux game inside umu-run's Steam Linux Runtime."
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UMUCompatLauncher;
|
||||
@@ -168,6 +409,18 @@ impl ProcessHandler for UMUCompatLauncher {
|
||||
}
|
||||
|
||||
fn modify_command(&self, _command: &mut Command) {}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"proton-umu"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Proton (umu-run)"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Runs the Windows game through Proton using umu-run."
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsahiMuvmLauncher;
|
||||
@@ -228,4 +481,16 @@ impl ProcessHandler for AsahiMuvmLauncher {
|
||||
}
|
||||
|
||||
fn modify_command(&self, _command: &mut Command) {}
|
||||
|
||||
fn id(&self) -> &'static str {
|
||||
"proton-muvm"
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Proton + muvm (Asahi)"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Runs through Proton inside a muvm microVM, for Apple Silicon / Asahi Linux."
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,8 @@ use crate::{
|
||||
format::DropFormatArgs,
|
||||
parser::{LaunchParameters, ParsedCommand},
|
||||
process_handlers::{
|
||||
AsahiMuvmLauncher, MacLauncher, UMUCompatLauncher, UMUNativeLauncher, WindowsLauncher,
|
||||
AsahiMuvmLauncher, LinuxNativeLauncher, MacLauncher, UMUCompatLauncher, UMUNativeLauncher,
|
||||
WindowsCmdLauncher, WindowsDirectLauncher, WindowsLauncher, WindowsPowershellLauncher,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -54,6 +55,13 @@ pub struct LaunchOption {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ProcessHandlerOption {
|
||||
id: String,
|
||||
name: String,
|
||||
description: String,
|
||||
}
|
||||
|
||||
impl ProcessManager<'_> {
|
||||
pub fn new(app_handle: AppHandle) -> Self {
|
||||
let log_output_dir = DATA_ROOT_DIR.join("logs");
|
||||
@@ -76,6 +84,22 @@ impl ProcessManager<'_> {
|
||||
(Platform::Windows, Platform::Windows),
|
||||
&WindowsLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||
),
|
||||
(
|
||||
(Platform::Windows, Platform::Windows),
|
||||
&WindowsDirectLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||
),
|
||||
(
|
||||
(Platform::Windows, Platform::Windows),
|
||||
&WindowsCmdLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||
),
|
||||
(
|
||||
(Platform::Windows, Platform::Windows),
|
||||
&WindowsPowershellLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||
),
|
||||
(
|
||||
(Platform::Linux, Platform::Linux),
|
||||
&LinuxNativeLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||
),
|
||||
(
|
||||
(Platform::Linux, Platform::Linux),
|
||||
&UMUNativeLauncher {} as &(dyn ProcessHandler + Sync + Send + 'static),
|
||||
@@ -101,7 +125,7 @@ impl ProcessManager<'_> {
|
||||
match self.processes.get_mut(&game_id) {
|
||||
Some(process) => {
|
||||
process.manually_killed = true;
|
||||
process.handle.kill()?;
|
||||
kill_process_tree(&process.handle)?;
|
||||
let exit_status = process.handle.wait()?;
|
||||
info!("exit status: {:?}", exit_status);
|
||||
Ok(())
|
||||
@@ -188,7 +212,21 @@ impl ProcessManager<'_> {
|
||||
&self,
|
||||
db_lock: &Database,
|
||||
target_platform: &Platform,
|
||||
override_id: Option<&str>,
|
||||
) -> Result<&(dyn ProcessHandler + Send + Sync), ProcessError> {
|
||||
// An explicit override wins, as long as it's valid for the current platform.
|
||||
if let Some(override_id) = override_id
|
||||
&& let Some(handler) = self.game_launchers.iter().find(|e| {
|
||||
let (e_current, e_target) = e.0;
|
||||
e_current == self.current_platform
|
||||
&& e_target == *target_platform
|
||||
&& e.1.id() == override_id
|
||||
&& e.1.valid_for_platform(db_lock, target_platform)
|
||||
})
|
||||
{
|
||||
return Ok(handler.1);
|
||||
}
|
||||
|
||||
Ok(self
|
||||
.game_launchers
|
||||
.iter()
|
||||
@@ -204,10 +242,44 @@ impl ProcessManager<'_> {
|
||||
|
||||
pub fn valid_platform(&self, platform: &Platform) -> bool {
|
||||
let db_lock = borrow_db_checked();
|
||||
let process_handler = self.fetch_process_handler(&db_lock, platform);
|
||||
let process_handler = self.fetch_process_handler(&db_lock, platform, None);
|
||||
process_handler.is_ok()
|
||||
}
|
||||
|
||||
pub fn get_process_handlers(
|
||||
&self,
|
||||
game_id: String,
|
||||
) -> Result<Vec<ProcessHandlerOption>, ProcessError> {
|
||||
let db_lock = borrow_db_checked();
|
||||
|
||||
let meta = db_lock
|
||||
.applications
|
||||
.installed_game_version
|
||||
.get(&game_id)
|
||||
.cloned()
|
||||
.ok_or(ProcessError::NotInstalled)?;
|
||||
|
||||
let target_platform = meta.target_platform;
|
||||
|
||||
let handlers = self
|
||||
.game_launchers
|
||||
.iter()
|
||||
.filter(|e| {
|
||||
let (e_current, e_target) = e.0;
|
||||
e_current == self.current_platform
|
||||
&& e_target == target_platform
|
||||
&& e.1.valid_for_platform(&db_lock, &target_platform)
|
||||
})
|
||||
.map(|e| ProcessHandlerOption {
|
||||
id: e.1.id().to_string(),
|
||||
name: e.1.name().to_string(),
|
||||
description: e.1.description().to_string(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(handlers)
|
||||
}
|
||||
|
||||
pub fn get_launch_options(game_id: String) -> Result<Vec<LaunchOption>, ProcessError> {
|
||||
let db_lock = borrow_db_checked();
|
||||
|
||||
@@ -310,7 +382,12 @@ impl ProcessManager<'_> {
|
||||
|
||||
let target_platform = meta.target_platform;
|
||||
|
||||
let process_handler = self.fetch_process_handler(&db_lock, &target_platform)?;
|
||||
let process_handler = self.fetch_process_handler(
|
||||
&db_lock,
|
||||
&target_platform,
|
||||
game_version.user_configuration.override_handler.as_deref(),
|
||||
)?;
|
||||
debug!("using process handler {:?}", process_handler.id());
|
||||
|
||||
let (target_command, emulator) = match game_status {
|
||||
GameDownloadStatus::Installed {
|
||||
@@ -516,6 +593,30 @@ impl ProcessManager<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn kill_process_tree(handle: &SharedChild) -> io::Result<()> {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// handle.kill() only terminates the launched process (often a cmd or
|
||||
// powershell wrapper), orphaning the actual game. taskkill /T kills the
|
||||
// whole process tree.
|
||||
use std::os::windows::process::CommandExt;
|
||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
let pid = handle.id().to_string();
|
||||
let killed = Command::new("taskkill")
|
||||
.args(["/F", "/T", "/PID", pid.as_str()])
|
||||
.creation_flags(CREATE_NO_WINDOW)
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.status()
|
||||
.map(|status| status.success())
|
||||
.unwrap_or(false);
|
||||
if killed {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
handle.kill()
|
||||
}
|
||||
|
||||
pub trait ProcessHandler: Send + 'static {
|
||||
fn create_launch_process(
|
||||
&self,
|
||||
@@ -529,4 +630,8 @@ pub trait ProcessHandler: Send + 'static {
|
||||
fn valid_for_platform(&self, db: &Database, target: &Platform) -> bool;
|
||||
|
||||
fn modify_command(&self, command: &mut Command);
|
||||
|
||||
fn id(&self) -> &'static str;
|
||||
fn name(&self) -> &'static str;
|
||||
fn description(&self) -> &'static str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user