aboutsummaryrefslogtreecommitdiff
path: root/libs/nak/src/steam/paths.rs
blob: 8ef5013f00e211ad668705f2140c9808d7d8908c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! Steam path detection utilities

use std::fs;
use std::path::PathBuf;

use crate::logging::{log_info, log_warning};

// ============================================================================
// Core Path Detection
// ============================================================================

/// Find the Steam installation path.
#[must_use]
pub fn find_steam_path() -> Option<PathBuf> {
    let home = std::env::var("HOME").ok()?;

    let steam_paths = [
        format!("{}/.steam/steam", home),
        format!("{}/.local/share/Steam", home),
        format!("{}/.var/app/com.valvesoftware.Steam/.steam/steam", home),
        format!("{}/snap/steam/common/.steam/steam", home),
    ];

    steam_paths
        .iter()
        .map(PathBuf::from)
        .find(|p| p.exists())
}

/// Find the Steam userdata directory.
#[must_use]
pub fn find_userdata_path() -> Option<PathBuf> {
    let config = crate::config::AppConfig::load();
    if !config.selected_steam_account.is_empty() {
        if let Some(path) = find_userdata_path_for_account(&config.selected_steam_account) {
            return Some(path);
        }
    }

    let steam_path = find_steam_path()?;
    let userdata = steam_path.join("userdata");

    if !userdata.exists() {
        return None;
    }

    let accounts = get_steam_accounts();

    if let Some(most_recent) = accounts.iter().find(|a| a.most_recent) {
        let path = userdata.join(&most_recent.account_id);
        if path.exists() {
            log_info(&format!(
                "Using Steam account from loginusers.vdf (MostRecent): {} ({})",
                most_recent.persona_name, most_recent.account_id
            ));
            return Some(path);
        }
    }

    if let Some(first_account) = accounts.first() {
        let path = userdata.join(&first_account.account_id);
        if path.exists() {
            log_info(&format!(
                "Using Steam account from loginusers.vdf (most recent timestamp): {} ({})",
                first_account.persona_name, first_account.account_id
            ));
            return Some(path);
        }
    }

    log_warning("Could not determine active Steam account from loginusers.vdf, falling back to directory modification time");

    let mut user_dirs: Vec<PathBuf> = Vec::new();

    if let Ok(entries) = fs::read_dir(&userdata) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                if let Some(name) = path.file_name() {
                    let name_str = name.to_string_lossy();
                    if name_str != "0" && name_str.chars().all(|c| c.is_ascii_digit()) {
                        user_dirs.push(path);
                    }
                }
            }
        }
    }

    user_dirs.sort_by(|a, b| {
        let a_time = fs::metadata(a).and_then(|m| m.modified()).ok();
        let b_time = fs::metadata(b).and_then(|m| m.modified()).ok();
        b_time.cmp(&a_time)
    });

    user_dirs.into_iter().next()
}

// ============================================================================
// Steam Account Detection (loginusers.vdf parsing)
// ============================================================================

/// Information about a Steam user account
#[derive(Debug, Clone)]
pub struct SteamAccount {
    pub account_id: String,
    pub persona_name: String,
    pub most_recent: bool,
    pub timestamp: u64,
}

/// Get all Steam accounts from loginusers.vdf with their display names
#[must_use]
pub fn get_steam_accounts() -> Vec<SteamAccount> {
    let Some(steam_path) = find_steam_path() else {
        return Vec::new();
    };

    let loginusers_path = steam_path.join("config/loginusers.vdf");
    let userdata_path = steam_path.join("userdata");

    let Ok(content) = fs::read_to_string(&loginusers_path) else {
        return Vec::new();
    };

    let mut accounts = Vec::new();

    let mut current_steam_id: Option<String> = None;
    let mut current_account: Option<SteamAccountBuilder> = None;

    for line in content.lines() {
        let trimmed = line.trim();

        if trimmed.starts_with('"') && trimmed.ends_with('"') {
            let id = trimmed.trim_matches('"');
            if id.len() == 17 && id.starts_with("7656") && id.chars().all(|c| c.is_ascii_digit()) {
                if let (Some(steam_id), Some(builder)) = (current_steam_id.take(), current_account.take()) {
                    if let Some(account) = builder.build(&steam_id, &userdata_path) {
                        accounts.push(account);
                    }
                }
                current_steam_id = Some(id.to_string());
                current_account = Some(SteamAccountBuilder::default());
            }
        }

        if let Some(ref mut builder) = current_account {
            if let Some((key, value)) = parse_vdf_kv(trimmed) {
                match key.to_lowercase().as_str() {
                    "accountname" => builder.account_name = Some(value),
                    "personaname" => builder.persona_name = Some(value),
                    "mostrecent" => builder.most_recent = value == "1",
                    "timestamp" => builder.timestamp = value.parse().unwrap_or(0),
                    _ => {}
                }
            }
        }
    }

    if let (Some(steam_id), Some(builder)) = (current_steam_id, current_account) {
        if let Some(account) = builder.build(&steam_id, &userdata_path) {
            accounts.push(account);
        }
    }

    accounts.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

    accounts
}

#[derive(Default)]
struct SteamAccountBuilder {
    account_name: Option<String>,
    persona_name: Option<String>,
    most_recent: bool,
    timestamp: u64,
}

impl SteamAccountBuilder {
    fn build(self, steam_id: &str, userdata_base: &std::path::Path) -> Option<SteamAccount> {
        let account_name = self.account_name?;
        let persona_name = self.persona_name.unwrap_or_else(|| account_name.clone());

        let steam64: u64 = steam_id.parse().ok()?;
        let account_id = (steam64 - 76561197960265728).to_string();

        let userdata_path = userdata_base.join(&account_id);

        if !userdata_path.exists() {
            return None;
        }

        Some(SteamAccount {
            account_id,
            persona_name,
            most_recent: self.most_recent,
            timestamp: self.timestamp,
        })
    }
}

/// Parse a VDF key-value pair like: "Key"    "Value"
fn parse_vdf_kv(line: &str) -> Option<(String, String)> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;

    for c in line.chars() {
        match c {
            '"' => {
                if in_quotes {
                    parts.push(current.clone());
                    current.clear();
                }
                in_quotes = !in_quotes;
            }
            _ if in_quotes => current.push(c),
            _ => {}
        }
    }

    if parts.len() >= 2 {
        Some((parts[0].clone(), parts[1].clone()))
    } else {
        None
    }
}

/// Find the userdata path for a specific Steam account
#[must_use]
pub fn find_userdata_path_for_account(account_id: &str) -> Option<PathBuf> {
    let steam_path = find_steam_path()?;
    let userdata = steam_path.join("userdata").join(account_id);

    if userdata.exists() {
        Some(userdata)
    } else {
        None
    }
}

// ============================================================================
// Convenience Wrappers
// ============================================================================

/// Detect the Steam installation path with logging.
#[must_use]
pub fn detect_steam_path_checked() -> Option<String> {
    match find_steam_path() {
        Some(path) => {
            let path_str = path.to_string_lossy().to_string();
            log_info(&format!("Steam detected at: {}", path_str));
            Some(path_str)
        }
        None => {
            log_warning("Steam installation not detected! NaK requires Steam to be installed.");
            None
        }
    }
}