Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit 61962b1

Browse files
author
Fodor Levente
committed
feat: can now wait for login, added README.md
misc: renamed to steamed
1 parent 7bcbe05 commit 61962b1

File tree

5 files changed

+53
-37
lines changed

5 files changed

+53
-37
lines changed

Cargo.lock

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "learn-rust"
2+
name = "steamed"
33
description = "Awesomeness"
44
version = "0.1.0"
55
edition = "2021"

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Steamed

src/auto_steam.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ pub mod auto_steam {
4242

4343
pub(super) trait MyPrivateRegTraits {
4444
fn get_running_appid(&self) -> u32;
45+
fn get_current_userid(&self) -> u32;
4546
fn get_steam_pid(&self) -> usize;
4647
fn get_app_running_value(&self, app_reg: &RegKey) -> u32;
4748
fn get_app_updating_value(&self, app_reg: &RegKey) -> u32;
4849
fn get_app_installed_value(&self, app_reg: &RegKey) -> u32;
4950
fn get_app_name_value(&self, app_reg: &RegKey) -> u32;
50-
fn create_new_command(&self) -> Command;
51+
fn create_new_steam_command(&self) -> Command;
5152
fn handle_a_game_is_already_running(&self);
5253
}
5354
}
@@ -58,6 +59,11 @@ pub mod auto_steam {
5859
return running;
5960
}
6061

62+
fn get_current_userid(&self) -> u32 {
63+
let userid: u32 = self.active_process_reg_key.get_value("ActiveUser").unwrap();
64+
return userid;
65+
}
66+
6167
fn get_steam_pid(&self) -> usize {
6268
let pid: u32 = self.active_process_reg_key.get_value("pid").unwrap();
6369
return pid as usize;
@@ -83,7 +89,7 @@ pub mod auto_steam {
8389
return name;
8490
}
8591

86-
fn create_new_command(&self) -> Command {
92+
fn create_new_steam_command(&self) -> Command {
8793
let mut command = Command::new(&self.steam_path);
8894
command.stdin(Stdio::null())
8995
.stdout(Stdio::null())
@@ -107,6 +113,7 @@ pub mod auto_steam {
107113
}
108114

109115
pub trait MyRegTraits {
116+
fn wait_for_login(&self) -> bool;
110117
fn wait_for_game_start(&self, app_id: &str) -> bool;
111118
fn wait_for_game_exit(&self, app_id: &str) -> ();
112119
fn list_installed_and_choose(&self) -> String;
@@ -118,6 +125,17 @@ pub mod auto_steam {
118125
}
119126

120127
impl MyRegTraits for MyRegVars {
128+
fn wait_for_login(&self) -> bool {
129+
if self.get_current_userid() != 0 { return true; }
130+
131+
let now = Instant::now();
132+
loop {
133+
watch_reg(&self.active_process_reg_key, 3000);
134+
if self.get_current_userid() != 0 { return true; }
135+
if now.elapsed().as_secs() > 60 { return false; }
136+
}
137+
}
138+
121139
fn wait_for_game_start(&self, appid: &str) -> bool {
122140
let app_reg_key = self.apps_reg_key
123141
.open_subkey(appid)
@@ -193,7 +211,7 @@ pub mod auto_steam {
193211
}
194212

195213
fn shut_down_steam(&self) -> Child {
196-
return self.create_new_command()
214+
return self.create_new_steam_command()
197215
.arg("-shutdown")
198216
.spawn()
199217
.expect("Something went wrong when shutting down steam.exe");
@@ -222,21 +240,14 @@ pub mod auto_steam {
222240
}
223241

224242
fn start_steam_login(&self, user: &str, pass: &str, appid: &str) -> Child {
225-
return self.create_new_command()
226-
.args([
227-
"-silent",
228-
"-login",
229-
user,
230-
pass,
231-
"-applaunch",
232-
appid
233-
])
243+
return self.create_new_steam_command()
244+
.args(["-silent", "-login", user, pass, "-applaunch", appid])
234245
.spawn()
235246
.expect("Something went wrong when starting steam.exe");
236247
}
237248

238249
fn start_steam(&self) -> Child {
239-
return self.create_new_command()
250+
return self.create_new_steam_command()
240251
.spawn()
241252
.expect("Something went wrong when starting steam.exe");
242253
}

src/main.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ fn main() {
4545
// I could shut down asynchronously, make choice and then await the async shutdown
4646
auto_steam.shutdown_steam_if_running();
4747

48-
// todo: check if login successful, by registry attribute of CurrentUserID or something
4948
let mut main_child = auto_steam.start_steam_login(&user, &pass, &appid);
5049

51-
if auto_steam.wait_for_game_start(&appid) {
52-
auto_steam.wait_for_game_exit(&appid);
53-
auto_steam.shut_down_steam()
54-
.wait()
55-
.expect("failed to wait on child");
56-
main_child.wait().expect("failed to wait on child");
57-
auto_steam.start_steam();
50+
if auto_steam.wait_for_login() {
51+
if auto_steam.wait_for_game_start(&appid) {
52+
auto_steam.wait_for_game_exit(&appid);
53+
auto_steam.shut_down_steam()
54+
.wait()
55+
.expect("failed to wait on child");
56+
main_child.wait().expect("failed to wait on child");
57+
auto_steam.start_steam();
58+
} else {
59+
println!("wait_for_game_start was false");
60+
main_child.wait().expect("failed to wait on child");
61+
}
5862
} else {
59-
println!("wait_for_game_start was false");
63+
println!("wait_for_login was false");
6064
main_child.wait().expect("failed to wait on child");
6165
}
6266
}

0 commit comments

Comments
 (0)