add existing settings

This commit is contained in:
Purp1e
2024-09-20 09:52:13 +08:00
parent 3be7e8a0b5
commit 7229e79cd4
30 changed files with 503 additions and 6 deletions

41
src-tauri/src/steam/id.rs Normal file
View File

@@ -0,0 +1,41 @@
// Steam users, including:
// - ID Conversion
#![allow(unused)]
static ID_BASE: u64 = 76561197960265728;
pub fn id64_to_32(id64: u64) -> u32 {
(id64 - ID_BASE) as u32
}
pub fn id32_to_64(id32: u32) -> u64 {
(id32 as u64) + ID_BASE
}
#[cfg(test)]
mod tests {
use super::*;
const ID32: [&str; 2] = ["354813078", "146859713"];
const ID64: [&str; 2] = ["76561198315078806", "76561198107125441"];
#[test]
fn test_id32_to_64() {
for (i32, i64) in ID32.iter().zip(ID64.iter()) {
assert_eq!(
id32_to_64(i32.parse::<u32>().unwrap()),
i64.parse::<u64>().unwrap()
);
}
}
#[test]
fn test_id64_to_32() {
for (i32, i64) in ID32.iter().zip(ID64.iter()) {
assert_eq!(
id64_to_32(i64.parse::<u64>().unwrap()),
i32.parse::<u32>().unwrap()
);
}
}
}

View File

@@ -0,0 +1,38 @@
// Manage sub modules
pub mod id;
pub mod path;
pub mod reg;
// common steam utils
use std::error::Error;
use std::process::{Command, Output};
pub fn launch_game(
steam_path: &str,
launch_option: &str,
server: &str,
) -> Result<(), Box<dyn Error>> {
let mut opt = launch_option.replace("\n", " ");
if server == "perfectworld" {
opt = opt.replace("-worldwide", "") + " -perfectworld";
} else if server == "worldwide" {
opt = opt.replace("-perfectworld", "") + " -worldwide";
}
let opts = format!("-applaunch 730 {}", opt);
let opts_split = opts.split_whitespace().collect::<Vec<&str>>();
let output: Output = Command::new(steam_path).args(opts_split).output()?;
if !output.status.success() {
let error_message = String::from_utf8_lossy(&output.stderr);
println!("Error: {}", error_message);
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to launch game",
)));
}
Ok(())
}

View File

@@ -0,0 +1,48 @@
// Steam related path utils, including:
// - Steam Path
// - CS2(CS:GO) Path
#![allow(unused)]
use crate::tool::common::get_exe_path;
use super::reg;
pub fn get_steam_path<'a>() -> Result<String, &'a str> {
// Windows Registry
#[cfg(target_os = "windows")]
if let Ok(reg) = reg::SteamReg::get_all() {
return Ok(reg.steam_path);
}
// Running steam.exe
#[cfg(target_os = "windows")]
if let Ok(steam_path) = get_exe_path("steam.exe") {
return Ok(steam_path);
}
Err("no steam path found")
}
pub fn get_cs_path<'a>(name: &str) -> Result<String, &'a str> {
if name != "csgo" || name != "cs2" {
return Err("invalid cs name");
}
#[cfg(target_os = "windows")]
if let Ok(cs_path) = get_exe_path(&(name.to_owned() + ".exe")) {
return Ok(cs_path);
}
Err("no cs path found")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_steam_path() {
let path = get_steam_path().unwrap();
println!("{}", path);
}
}

101
src-tauri/src/steam/reg.rs Normal file
View File

@@ -0,0 +1,101 @@
// Steam metadata parsing from reg, including:
// - Steam Path
// - Users (ID32)
#![allow(unused)]
use crate::tool::common::kill;
use crate::tool::common::run_steam;
use std::process::Command;
#[cfg(target_os = "windows")]
use winreg::enums::*;
#[cfg(target_os = "windows")]
use winreg::RegKey;
#[cfg(target_os = "windows")]
static STEAM_REG: &str = r"Software\Valve\Steam";
#[derive(Debug)]
pub struct SteamReg {
pub steam_path: String,
pub auto_login_user: String,
pub suppress_auto_run: u32,
pub remember_password: u32,
pub language: String,
pub users: Vec<String>,
}
impl SteamReg {
#[cfg(target_os = "windows")]
pub fn get_all<'a>() -> Result<Self, &'a str> {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_ver = hkcu.open_subkey(STEAM_REG).expect("steam reg");
let steam_path: String = cur_ver.get_value("SteamExe").expect("steam path");
let auto_login_user: String = cur_ver.get_value("AutoLoginUser").expect("auto login user");
let suppress_auto_run: u32 = cur_ver
.get_value("SuppressAutoRun")
.expect("suppress auto run");
let remember_password: u32 = cur_ver
.get_value("RememberPassword")
.expect("remember password");
let language: String = cur_ver.get_value("Language").expect("language");
let users = cur_ver
.open_subkey("Users")
.expect("users")
.enum_keys()
.map(|x| x.unwrap().to_string())
.collect::<Vec<String>>();
Ok(Self {
steam_path,
auto_login_user,
suppress_auto_run,
remember_password,
language,
users: users,
})
}
}
#[cfg(target_os = "windows")]
pub fn set_auto_login_user(user: &str) -> Result<(), &str> {
// ensure steam.exe is not running
kill("steam.exe");
// set reg key value
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_ver = hkcu
.open_subkey_with_flags(STEAM_REG, KEY_WRITE)
.expect("steam reg");
cur_ver
.set_value("AutoLoginUser", &user)
.expect("auto login user");
cur_ver
.set_value("RememberPassword", &1u32)
.expect("remember passwd");
// open steam.exe again
let output = run_steam().expect("failed to execute process");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(target_os = "windows")]
fn test_get_steam_path() {
let path = SteamReg::get_all().unwrap();
println!("{:?}", path);
}
#[test]
#[cfg(target_os = "windows")]
fn test_set_auto_login() {
// set_auto_login_user("_im_ai_");
set_auto_login_user("_jerry_dota2");
}
}