Restart steam option (#82)

Adds options to start and stop steam when importing
This commit is contained in:
Philip Kristoffersen
2022-04-15 09:38:43 +02:00
committed by GitHub
parent 9396b06765
commit fcc3a4abc3
9 changed files with 110 additions and 7 deletions
Generated
+16
View File
@@ -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"
+1
View File
@@ -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"]
+2 -1
View File
@@ -36,4 +36,5 @@ enabled = true
[steam]
create_collections = false
optimize_for_big_picture = false
stop_steam = false
start_steam = false
+7
View File
@@ -20,10 +20,17 @@ use std::error::Error;
#[cfg(not(feature = "ui"))]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
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(())
}
+3 -1
View File
@@ -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::*;
pub use proton_vdf_util::*;
pub use restarter::*;
+57
View File
@@ -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);
};
}
}
+2
View File
@@ -5,4 +5,6 @@ pub struct SteamSettings {
pub location: Option<String>,
pub create_collections: bool,
pub optimize_for_big_picture: bool,
pub stop_steam: bool,
pub start_steam: bool,
}
+13 -5
View File
@@ -53,11 +53,7 @@ pub struct SteamUsersInfo {
pub fn get_shortcuts_paths(
settings: &SteamSettings,
) -> Result<Vec<SteamUsersInfo>, Box<dyn Error + Sync + Send>> {
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<String, Box<dyn Error + Sync + Send>> {
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<String, Box<dyn Error + Sync + Send>> {
#[cfg(target_os = "windows")]
let path_string = {
@@ -198,3 +203,6 @@ pub fn get_users_images(user: &SteamUsersInfo) -> Result<Vec<String>, Box<dyn Er
.collect();
Ok(file_names)
}
+9
View File
@@ -43,6 +43,10 @@ impl MyEguiApp {
pub fn run_sync(&mut self) {
let (sender,mut reciever ) = watch::channel(SyncProgress::NotStarted);
let settings = self.settings.clone();
if settings.steam.stop_steam{
crate::steam::ensure_steam_stopped();
}
self.status_reciever = reciever;
self.rt.spawn_blocking(move || {
@@ -55,6 +59,9 @@ impl MyEguiApp {
if let Some(sender) = some_sender{
let _ = sender.send(SyncProgress::Done);
}
if settings.steam.start_steam{
crate::steam::ensure_steam_started(&settings.steam);
}
});
}
@@ -236,6 +243,8 @@ impl MyEguiApp{
});
ui.checkbox(&mut self.settings.steam.create_collections, "Create collections").on_hover_text("Tries to create a games collection for each platform");
ui.checkbox(&mut self.settings.steam.optimize_for_big_picture, "Optimize for big picture").on_hover_text("Set icons to be larger horizontal images, this looks nice in steam big picture mode, but a bit off in desktop mode");
ui.checkbox(&mut self.settings.steam.stop_steam, "Stop Steam before import").on_hover_text("Stops Steam if it is running when import starts");
ui.checkbox(&mut self.settings.steam.start_steam, "Start Steam after import").on_hover_text("Starts Steam is it is not running after the import");
ui.separator();