@@ -1,6 +1,10 @@ |
| 1 | 1 | //! Game launching with WeMod |
| 2 | 2 | //! |
| 3 | 3 | //! Handles launching Steam games alongside WeMod through Proton. |
| 4 | +//! |
| 5 | +//! Key insight: WeMod and the game MUST run in the same Wine prefix |
| 6 | +//! for WeMod to be able to hook into the game process. We achieve this by |
| 7 | +//! running both from wanda's prefix (which has .NET and WeMod installed). |
| 4 | 8 | |
| 5 | 9 | use crate::error::{Result, WandaError}; |
| 6 | 10 | use crate::prefix::WandaPrefix; |
@@ -10,7 +14,7 @@ use std::path::PathBuf; |
| 10 | 14 | use std::process::Stdio; |
| 11 | 15 | use std::time::Duration; |
| 12 | 16 | use tokio::process::{Child, Command}; |
| 13 | | -use tracing::{debug, info, warn}; |
| 17 | +use tracing::info; |
| 14 | 18 | |
| 15 | 19 | /// Configuration for launching a game |
| 16 | 20 | #[derive(Debug, Clone)] |
@@ -54,8 +58,8 @@ impl LaunchHandle { |
| 54 | 58 | pub fn is_running(&mut self) -> bool { |
| 55 | 59 | if let Some(ref mut wemod) = self.wemod_process { |
| 56 | 60 | match wemod.try_wait() { |
| 57 | | - Ok(Some(_)) => return false, // WeMod exited |
| 58 | | - Ok(None) => return true, // Still running |
| 61 | + Ok(Some(_)) => return false, |
| 62 | + Ok(None) => return true, |
| 59 | 63 | Err(_) => return false, |
| 60 | 64 | } |
| 61 | 65 | } |
@@ -86,11 +90,8 @@ impl LaunchHandle { |
| 86 | 90 | |
| 87 | 91 | /// Game launcher that coordinates WeMod and game startup |
| 88 | 92 | pub struct GameLauncher<'a> { |
| 89 | | - /// Steam installation |
| 90 | 93 | steam: &'a SteamInstallation, |
| 91 | | - /// WANDA prefix with WeMod |
| 92 | 94 | prefix: &'a WandaPrefix, |
| 93 | | - /// Proton version to use |
| 94 | 95 | proton: &'a ProtonVersion, |
| 95 | 96 | } |
| 96 | 97 | |
@@ -101,42 +102,27 @@ impl<'a> GameLauncher<'a> { |
| 101 | 102 | prefix: &'a WandaPrefix, |
| 102 | 103 | proton: &'a ProtonVersion, |
| 103 | 104 | ) -> Self { |
| 104 | | - Self { |
| 105 | | - steam, |
| 106 | | - prefix, |
| 107 | | - proton, |
| 108 | | - } |
| 105 | + Self { steam, prefix, proton } |
| 109 | 106 | } |
| 110 | 107 | |
| 111 | 108 | /// Build environment variables for launching |
| 112 | | - fn build_env(&self, game: &SteamApp) -> HashMap<String, String> { |
| 109 | + fn build_env(&self) -> HashMap<String, String> { |
| 113 | 110 | let mut env = HashMap::new(); |
| 114 | 111 | |
| 115 | | - // Wine prefix (WANDA's prefix for WeMod) |
| 116 | 112 | env.insert( |
| 117 | 113 | "WINEPREFIX".to_string(), |
| 114 | + self.prefix.pfx_path().to_string_lossy().to_string(), |
| 115 | + ); |
| 116 | + env.insert( |
| 117 | + "STEAM_COMPAT_DATA_PATH".to_string(), |
| 118 | 118 | self.prefix.path.to_string_lossy().to_string(), |
| 119 | 119 | ); |
| 120 | | - |
| 121 | | - // Steam compatibility data path (for the game's own prefix if needed) |
| 122 | | - if let Some(ref compat_path) = game.compat_data_path { |
| 123 | | - env.insert( |
| 124 | | - "STEAM_COMPAT_DATA_PATH".to_string(), |
| 125 | | - compat_path.to_string_lossy().to_string(), |
| 126 | | - ); |
| 127 | | - } |
| 128 | | - |
| 129 | | - // Steam client install path |
| 130 | 120 | env.insert( |
| 131 | 121 | "STEAM_COMPAT_CLIENT_INSTALL_PATH".to_string(), |
| 132 | 122 | self.steam.root_path.to_string_lossy().to_string(), |
| 133 | 123 | ); |
| 134 | | - |
| 135 | | - // Proton flags that may help stability |
| 136 | 124 | env.insert("PROTON_NO_ESYNC".to_string(), "1".to_string()); |
| 137 | 125 | env.insert("PROTON_NO_FSYNC".to_string(), "1".to_string()); |
| 138 | | - |
| 139 | | - // Reduce Wine debug noise |
| 140 | 126 | env.insert("WINEDEBUG".to_string(), "-all".to_string()); |
| 141 | 127 | |
| 142 | 128 | env |
@@ -157,28 +143,30 @@ impl<'a> GameLauncher<'a> { |
| 157 | 143 | let game = self |
| 158 | 144 | .steam |
| 159 | 145 | .find_game(config.app_id) |
| 160 | | - .ok_or(WandaError::GameNotFound { |
| 161 | | - app_id: config.app_id, |
| 162 | | - })?; |
| 146 | + .ok_or(WandaError::GameNotFound { app_id: config.app_id })?; |
| 163 | 147 | |
| 164 | 148 | info!( |
| 165 | 149 | "Launching {} (AppID: {}) {}", |
| 166 | 150 | game.name, |
| 167 | 151 | game.app_id, |
| 168 | | - if config.with_wemod { |
| 169 | | - "with WeMod" |
| 170 | | - } else { |
| 171 | | - "without WeMod" |
| 172 | | - } |
| 152 | + if config.with_wemod { "with WeMod" } else { "without WeMod" } |
| 173 | 153 | ); |
| 174 | 154 | |
| 175 | | - let mut env = self.build_env(game); |
| 155 | + info!("Using WANDA prefix: {}", self.prefix.path.display()); |
| 156 | + |
| 157 | + // Kill stale wineservers to avoid conflicts |
| 158 | + info!("Cleaning up stale wineservers..."); |
| 159 | + let _ = Command::new("pkill").args(["-9", "wineserver"]).output().await; |
| 160 | + tokio::time::sleep(Duration::from_secs(1)).await; |
| 161 | + |
| 162 | + let mut env = self.build_env(); |
| 163 | + env.insert("SteamAppId".to_string(), config.app_id.to_string()); |
| 164 | + env.insert("SteamGameId".to_string(), config.app_id.to_string()); |
| 176 | 165 | env.extend(config.extra_env); |
| 177 | 166 | |
| 178 | 167 | let wine = self.wine_exe(); |
| 179 | 168 | let mut wemod_process = None; |
| 180 | 169 | |
| 181 | | - // Start WeMod first if requested |
| 182 | 170 | if config.with_wemod { |
| 183 | 171 | let wemod_exe = self.prefix.wemod_exe(); |
| 184 | 172 | if !wemod_exe.exists() { |
@@ -186,11 +174,14 @@ impl<'a> GameLauncher<'a> { |
| 186 | 174 | } |
| 187 | 175 | |
| 188 | 176 | info!("Starting WeMod..."); |
| 177 | + info!("WeMod path: {}", wemod_exe.display()); |
| 178 | + |
| 189 | 179 | let child = Command::new(&wine) |
| 190 | 180 | .arg(&wemod_exe) |
| 181 | + .arg("--no-sandbox") |
| 191 | 182 | .envs(&env) |
| 192 | | - .stdout(Stdio::null()) |
| 193 | | - .stderr(Stdio::null()) |
| 183 | + .stdout(Stdio::inherit()) |
| 184 | + .stderr(Stdio::inherit()) |
| 194 | 185 | .spawn() |
| 195 | 186 | .map_err(|e| WandaError::LaunchFailed { |
| 196 | 187 | reason: format!("Failed to start WeMod: {}", e), |
@@ -198,18 +189,12 @@ impl<'a> GameLauncher<'a> { |
| 198 | 189 | |
| 199 | 190 | wemod_process = Some(child); |
| 200 | 191 | |
| 201 | | - // Wait for WeMod to initialize |
| 202 | | - info!( |
| 203 | | - "Waiting {} seconds for WeMod to initialize...", |
| 204 | | - config.wemod_delay |
| 205 | | - ); |
| 192 | + info!("Waiting {} seconds for WeMod to initialize...", config.wemod_delay); |
| 206 | 193 | tokio::time::sleep(Duration::from_secs(config.wemod_delay)).await; |
| 207 | 194 | } |
| 208 | 195 | |
| 209 | | - // Launch the game via Steam |
| 210 | | - // Using steam:// URL protocol ensures Steam handles Proton setup correctly |
| 211 | | - info!("Launching game via Steam..."); |
| 212 | | - self.launch_via_steam(config.app_id).await?; |
| 196 | + info!("Launching game via Proton..."); |
| 197 | + self.launch_game_via_proton(&game, &config.extra_args, &env).await?; |
| 213 | 198 | |
| 214 | 199 | Ok(LaunchHandle { |
| 215 | 200 | wemod_process, |
@@ -218,97 +203,42 @@ impl<'a> GameLauncher<'a> { |
| 218 | 203 | }) |
| 219 | 204 | } |
| 220 | 205 | |
| 221 | | - /// Launch a game via Steam's URL protocol |
| 222 | | - async fn launch_via_steam(&self, app_id: u32) -> Result<()> { |
| 223 | | - // Use xdg-open to launch via steam:// protocol |
| 224 | | - // This ensures Steam handles all the Proton setup correctly |
| 225 | | - let steam_url = format!("steam://rungameid/{}", app_id); |
| 226 | | - |
| 227 | | - let status = Command::new("xdg-open") |
| 228 | | - .arg(&steam_url) |
| 229 | | - .stdout(Stdio::null()) |
| 230 | | - .stderr(Stdio::null()) |
| 231 | | - .status() |
| 232 | | - .await |
| 233 | | - .map_err(|e| WandaError::LaunchFailed { |
| 234 | | - reason: format!("Failed to launch Steam URL: {}", e), |
| 235 | | - })?; |
| 236 | | - |
| 237 | | - if !status.success() { |
| 238 | | - // Try alternative: steam command directly |
| 239 | | - let status = Command::new("steam") |
| 240 | | - .arg(&steam_url) |
| 241 | | - .stdout(Stdio::null()) |
| 242 | | - .stderr(Stdio::null()) |
| 243 | | - .status() |
| 244 | | - .await |
| 245 | | - .map_err(|e| WandaError::LaunchFailed { |
| 246 | | - reason: format!("Failed to launch via steam command: {}", e), |
| 247 | | - })?; |
| 248 | | - |
| 249 | | - if !status.success() { |
| 250 | | - warn!("Steam launch returned non-zero, game may still start"); |
| 251 | | - } |
| 252 | | - } |
| 253 | | - |
| 254 | | - debug!("Game launch initiated via Steam"); |
| 255 | | - Ok(()) |
| 256 | | - } |
| 257 | | - |
| 258 | | - /// Launch a game directly via Proton (without Steam) |
| 259 | | - /// This is an alternative method that gives more control |
| 260 | | - #[allow(dead_code)] |
| 261 | | - async fn launch_directly(&self, game: &SteamApp, args: &[String]) -> Result<Child> { |
| 206 | + /// Launch game via Proton |
| 207 | + async fn launch_game_via_proton( |
| 208 | + &self, |
| 209 | + game: &SteamApp, |
| 210 | + args: &[String], |
| 211 | + env: &HashMap<String, String>, |
| 212 | + ) -> Result<()> { |
| 262 | 213 | let proton_exe = self.proton.proton_exe(); |
| 263 | | - |
| 264 | 214 | if !proton_exe.exists() { |
| 265 | 215 | return Err(WandaError::ProtonNotFound); |
| 266 | 216 | } |
| 267 | 217 | |
| 268 | | - // Find the game executable |
| 269 | 218 | let game_exe = self.find_game_executable(game)?; |
| 270 | | - |
| 271 | | - let mut env = self.build_env(game); |
| 272 | | - |
| 273 | | - // Set up Proton environment |
| 274 | | - env.insert("STEAM_COMPAT_DATA_PATH".to_string(), |
| 275 | | - game.compat_data_path |
| 276 | | - .as_ref() |
| 277 | | - .map(|p| p.to_string_lossy().to_string()) |
| 278 | | - .unwrap_or_else(|| { |
| 279 | | - self.steam.root_path |
| 280 | | - .join("steamapps/compatdata") |
| 281 | | - .join(game.app_id.to_string()) |
| 282 | | - .to_string_lossy() |
| 283 | | - .to_string() |
| 284 | | - }) |
| 285 | | - ); |
| 219 | + info!("Game executable: {}", game_exe.display()); |
| 286 | 220 | |
| 287 | 221 | let mut cmd = Command::new(&proton_exe); |
| 288 | | - cmd.arg("run").arg(&game_exe); |
| 289 | | - cmd.args(args); |
| 290 | | - cmd.envs(&env); |
| 291 | | - cmd.stdout(Stdio::null()); |
| 292 | | - cmd.stderr(Stdio::null()); |
| 222 | + cmd.arg("run").arg(&game_exe).args(args).envs(env); |
| 223 | + cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit()); |
| 224 | + |
| 225 | + info!("Running: {} run {}", proton_exe.display(), game_exe.display()); |
| 293 | 226 | |
| 294 | | - let child = cmd.spawn().map_err(|e| WandaError::LaunchFailed { |
| 227 | + cmd.spawn().map_err(|e| WandaError::LaunchFailed { |
| 295 | 228 | reason: format!("Failed to start game: {}", e), |
| 296 | 229 | })?; |
| 297 | 230 | |
| 298 | | - Ok(child) |
| 231 | + Ok(()) |
| 299 | 232 | } |
| 300 | 233 | |
| 301 | | - /// Try to find the main executable for a game |
| 234 | + /// Find the main executable for a game |
| 302 | 235 | fn find_game_executable(&self, game: &SteamApp) -> Result<PathBuf> { |
| 303 | 236 | let install_path = &game.install_path; |
| 304 | 237 | |
| 305 | 238 | if !install_path.exists() { |
| 306 | | - return Err(WandaError::GameNotFound { |
| 307 | | - app_id: game.app_id, |
| 308 | | - }); |
| 239 | + return Err(WandaError::GameNotFound { app_id: game.app_id }); |
| 309 | 240 | } |
| 310 | 241 | |
| 311 | | - // Common executable patterns |
| 312 | 242 | let patterns = [ |
| 313 | 243 | format!("{}.exe", game.install_dir), |
| 314 | 244 | "game.exe".to_string(), |
@@ -316,7 +246,6 @@ impl<'a> GameLauncher<'a> { |
| 316 | 246 | "launcher.exe".to_string(), |
| 317 | 247 | ]; |
| 318 | 248 | |
| 319 | | - // Try to find a matching executable |
| 320 | 249 | for pattern in &patterns { |
| 321 | 250 | let exe_path = install_path.join(pattern); |
| 322 | 251 | if exe_path.exists() { |
@@ -324,7 +253,6 @@ impl<'a> GameLauncher<'a> { |
| 324 | 253 | } |
| 325 | 254 | } |
| 326 | 255 | |
| 327 | | - // Walk the directory looking for .exe files |
| 328 | 256 | for entry in walkdir::WalkDir::new(install_path) |
| 329 | 257 | .max_depth(2) |
| 330 | 258 | .into_iter() |
@@ -334,7 +262,6 @@ impl<'a> GameLauncher<'a> { |
| 334 | 262 | if let Some(ext) = path.extension() { |
| 335 | 263 | if ext == "exe" { |
| 336 | 264 | let name = path.file_name().unwrap_or_default().to_string_lossy(); |
| 337 | | - // Skip common non-game executables |
| 338 | 265 | if !name.to_lowercase().contains("unins") |
| 339 | 266 | && !name.to_lowercase().contains("redist") |
| 340 | 267 | && !name.to_lowercase().contains("setup") |