diff --git a/Cargo.lock b/Cargo.lock index 71562c2..39e7511 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -247,6 +247,7 @@ dependencies = [ "serde_json", "steam_shortcuts_util", "steamgriddb_api", + "sysinfo", "tokio", "toml", "winreg 0.10.1", @@ -2751,6 +2752,21 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "sysinfo" +version = "0.23.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eea2ed6847da2e0c7289f72cb4f285f0bd704694ca067d32be811b2a45ea858" +dependencies = [ + "cfg-if 1.0.0", + "core-foundation-sys 0.8.3", + "libc", + "ntapi", + "once_cell", + "rayon", + "winapi", +] + [[package]] name = "tempfile" version = "3.3.0" diff --git a/Cargo.toml b/Cargo.toml index 6e33955..77c1241 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ eframe = { version = "0.17.0", optional = true } egui = { version = "0.17.0", optional = true } image = { version = "0.24.1", optional = true, features = ["png"] } toml = { version = "^0.5.8", optional = true } +sysinfo = "0.23.10" [features] #default = ["ui"] diff --git a/src/defaultconfig.toml b/src/defaultconfig.toml index 9be602e..a2cbd69 100644 --- a/src/defaultconfig.toml +++ b/src/defaultconfig.toml @@ -36,4 +36,5 @@ enabled = true [steam] create_collections = false optimize_for_big_picture = false - +stop_steam = false +start_steam = false diff --git a/src/main.rs b/src/main.rs index a69ae4c..d75e74a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,10 +20,17 @@ use std::error::Error; #[cfg(not(feature = "ui"))] #[tokio::main] async fn main() -> Result<(), Box> { + let settings = settings::Settings::new()?; + if settings.steam.stop_steam{ + crate::steam::ensure_steam_stopped(); + } settings::Settings::write_config_if_missing(); let usersinfo = sync::run_sync(&settings,&mut None).unwrap(); sync::download_images(&settings,&usersinfo,&mut None).await; + if settings.steam.start_steam{ + crate::steam::ensure_steam_started(&settings.steam); + } Ok(()) } diff --git a/src/steam/mod.rs b/src/steam/mod.rs index 672d5ff..e0948c9 100644 --- a/src/steam/mod.rs +++ b/src/steam/mod.rs @@ -2,9 +2,11 @@ mod settings; mod utils; mod collections; mod proton_vdf_util; +mod restarter; pub use settings::SteamSettings; pub use utils::*; pub use collections::*; -pub use proton_vdf_util::*; \ No newline at end of file +pub use proton_vdf_util::*; +pub use restarter::*; \ No newline at end of file diff --git a/src/steam/restarter.rs b/src/steam/restarter.rs new file mode 100644 index 0000000..2989827 --- /dev/null +++ b/src/steam/restarter.rs @@ -0,0 +1,57 @@ +use std::{path::Path, process::Command, thread::sleep, time::Duration}; + +use sysinfo::{ProcessExt, System, SystemExt}; + +pub fn ensure_steam_stopped() { + #[cfg(target_os = "windows")] + let steam_name = "steam.exe"; + #[cfg(target_family = "unix")] + let steam_name = "steam"; + + let s = System::new_all(); + let processes = s.processes_by_name(&steam_name); + for process in processes { + let mut s = System::new(); + process.kill_with(sysinfo::Signal::Quit); + process.kill_with(sysinfo::Signal::Kill); + while s.refresh_process(process.pid()) { + println!("Waiting for steam to stop"); + sleep(Duration::from_millis(500)); + process.kill_with(sysinfo::Signal::Quit); + process.kill_with(sysinfo::Signal::Kill); + } + } +} +#[cfg(target_os = "windows")] +pub fn ensure_steam_started(settings: &super::SteamSettings) { + let steam_name = "steam.exe"; + let s = System::new_all(); + let mut processes = s.processes_by_name(&steam_name); + if processes.next().is_none() { + //no steam, we need to start it + println!("Starting steam"); + let folder = super::get_steam_path(settings); + if let Ok(folder) = folder { + let path = Path::new(&folder).join(steam_name); + let mut command = Command::new(&path); + if let Err(e) = command.spawn() { + println!("Failed to start steam: {:?}", e); + }; + } + } +} + +#[cfg(target_family = "unix")] +pub fn ensure_steam_started(_settings: &super::SteamSettings) { + let steam_name = "steam"; + let s = System::new_all(); + let mut processes = s.processes_by_name(&steam_name); + if processes.next().is_none() { + //no steam, we need to start it + println!("Starting steam"); + let mut command = Command::new(&steam_name); + if let Err(e) = command.spawn() { + println!("Failed to start steam: {:?}", e); + }; + } +} \ No newline at end of file diff --git a/src/steam/settings.rs b/src/steam/settings.rs index b3a3f0e..0bc53c8 100644 --- a/src/steam/settings.rs +++ b/src/steam/settings.rs @@ -5,4 +5,6 @@ pub struct SteamSettings { pub location: Option, pub create_collections: bool, pub optimize_for_big_picture: bool, + pub stop_steam: bool, + pub start_steam: bool, } diff --git a/src/steam/utils.rs b/src/steam/utils.rs index 59d43c6..7095be2 100644 --- a/src/steam/utils.rs +++ b/src/steam/utils.rs @@ -53,11 +53,7 @@ pub struct SteamUsersInfo { pub fn get_shortcuts_paths( settings: &SteamSettings, ) -> Result, Box> { - let user_location = settings.location.clone(); - let steam_path_str = match user_location { - Some(location) => location, - None => get_default_location()?, - }; + let steam_path_str = get_steam_path(settings)?; let steam_path = Path::new(&steam_path_str); if !steam_path.exists() { return Result::Err(Box::new(SteamFolderNotFound { @@ -107,6 +103,15 @@ pub fn get_shortcuts_paths( Ok(users_info) } +pub fn get_steam_path(settings: &SteamSettings) -> Result> { + let user_location = settings.location.clone(); + let steam_path_str = match user_location { + Some(location) => location, + None => get_default_location()?, + }; + Ok(steam_path_str) +} + pub fn get_default_location() -> Result> { #[cfg(target_os = "windows")] let path_string = { @@ -198,3 +203,6 @@ pub fn get_users_images(user: &SteamUsersInfo) -> Result, Box