toolblox_lib/
roblox_process.rs1use std::collections::HashSet;
7
8#[cfg_attr(not(windows), allow(dead_code))]
10pub fn roblox_pids() -> Result<HashSet<u32>, String> {
11 #[cfg(windows)]
12 {
13 use std::os::windows::process::CommandExt;
14
15 const CREATE_NO_WINDOW: u32 = 0x08000000;
16 let output = std::process::Command::new("tasklist.exe")
17 .args([
18 "/FI",
19 "IMAGENAME eq RobloxPlayerBeta.exe",
20 "/FO",
21 "CSV",
22 "/NH",
23 ])
24 .creation_flags(CREATE_NO_WINDOW)
25 .output()
26 .map_err(|_| {
27 "Toolblox couldn't inspect running Roblox processes. Can you try again?".to_string()
28 })?;
29 Ok(parse_tasklist(&String::from_utf8_lossy(&output.stdout)))
30 }
31 #[cfg(target_os = "macos")]
32 {
33 let output = std::process::Command::new("/usr/bin/pgrep")
34 .args(["-x", "RobloxPlayer"])
35 .output()
36 .map_err(|_| {
37 "Toolblox couldn't inspect running Roblox processes. Can you try again?".to_string()
38 })?;
39 Ok(String::from_utf8_lossy(&output.stdout)
40 .lines()
41 .filter_map(|line| line.trim().parse().ok())
42 .collect())
43 }
44 #[cfg(not(any(windows, target_os = "macos")))]
45 {
46 Ok(HashSet::new())
47 }
48}
49
50const CLOSE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
52
53pub fn close_all() -> Result<usize, String> {
58 let count = roblox_pids()?.len();
59 if count == 0 {
60 return Ok(0);
61 }
62 let _ = signal(false);
63 let deadline = std::time::Instant::now() + CLOSE_TIMEOUT;
64 while std::time::Instant::now() < deadline {
65 std::thread::sleep(std::time::Duration::from_millis(250));
66 if roblox_pids()?.is_empty() {
67 return Ok(count);
68 }
69 }
70 let forced = signal(true).is_ok_and(|status| status.success());
71 if forced || roblox_pids()?.is_empty() {
72 Ok(count)
73 } else {
74 Err("Toolblox couldn't close the Roblox clients. Can you try again?".to_string())
75 }
76}
77
78fn signal(force: bool) -> std::io::Result<std::process::ExitStatus> {
80 #[cfg(windows)]
81 {
82 use std::os::windows::process::CommandExt;
83
84 const CREATE_NO_WINDOW: u32 = 0x08000000;
85 let mut command = std::process::Command::new("taskkill.exe");
86 if force {
87 command.arg("/F");
88 }
89 command
90 .args(["/T", "/IM", "RobloxPlayerBeta.exe"])
91 .creation_flags(CREATE_NO_WINDOW)
92 .status()
93 }
94 #[cfg(target_os = "macos")]
95 {
96 std::process::Command::new("/usr/bin/pkill")
97 .args([if force { "-KILL" } else { "-TERM" }, "-x", "RobloxPlayer"])
98 .status()
99 }
100 #[cfg(not(any(windows, target_os = "macos")))]
101 {
102 let _ = force;
103 Err(std::io::ErrorKind::Unsupported.into())
104 }
105}
106
107#[cfg_attr(not(windows), allow(dead_code))]
109fn parse_tasklist(text: &str) -> HashSet<u32> {
110 text.lines()
111 .filter_map(|line| line.split("\",\"").nth(1))
112 .filter_map(|value| value.trim_matches('"').parse().ok())
113 .collect()
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn tasklist_rows_give_process_ids() {
122 let text = "\"RobloxPlayerBeta.exe\",\"4120\",\"Console\",\"1\",\"512,000 K\"\r\n\
123 \"RobloxPlayerBeta.exe\",\"88\",\"Console\",\"1\",\"1 K\"\r\n\
124 INFO: No tasks are running which match the specified criteria.\r\n";
125 assert_eq!(parse_tasklist(text), HashSet::from([4120, 88]));
126 }
127}