Skip to content

Commit

Permalink
Restart steam option (#82)
Browse files Browse the repository at this point in the history
Adds options to start and stop steam when importing
  • Loading branch information
PhilipK authored Apr 15, 2022
1 parent 9396b06 commit fcc3a4a
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 2 additions & 1 deletion src/defaultconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ enabled = true
[steam]
create_collections = false
optimize_for_big_picture = false

stop_steam = false
start_steam = false
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
4 changes: 3 additions & 1 deletion src/steam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 57 additions & 0 deletions src/steam/restarter.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions src/steam/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
18 changes: 13 additions & 5 deletions src/steam/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -198,3 +203,6 @@ pub fn get_users_images(user: &SteamUsersInfo) -> Result<Vec<String>, Box<dyn Er
.collect();
Ok(file_names)
}



9 changes: 9 additions & 0 deletions src/ui/uiapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {

Expand All @@ -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);
}
});
}

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit fcc3a4a

Please sign in to comment.