aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/get_mc_dir.rs
blob: df2a81aa35d85218c9838833a24a2768273b1326 (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
//! Find out where the user's .minecraft directory is.
//!
//! Used for the auth cache.

use std::path::PathBuf;

/// Return the location of the user's .minecraft directory.
///
/// Windows: `%appdata%\.minecraft`\
/// Mac: `$HOME/Library/Application Support/minecraft`\
/// Linux: `$HOME/.minecraft`
///
/// If the environment variable is not set, this will return `None`.
pub fn minecraft_dir() -> Option<PathBuf> {
    let env_var = home_env_var();
    let home = std::env::var(env_var).ok()?;
    let path = PathBuf::from(home).join(minecraft_dir_relative());
    Some(path)
}

/// Return the name of the environment variable that's used for the home folder
/// on the user's operating system.
pub fn home_env_var() -> &'static str {
    #[cfg(target_os = "windows")]
    {
        "APPDATA"
    }
    #[cfg(target_os = "macos")]
    {
        "HOME"
    }
    #[cfg(target_os = "linux")]
    {
        "HOME"
    }
    #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
    {
        "HOME"
    }
}

/// Return the path relative to the home folder where we expect to find the
/// .minecraft directory.
pub fn minecraft_dir_relative() -> &'static str {
    #[cfg(target_os = "windows")]
    {
        ".minecraft"
    }
    #[cfg(target_os = "macos")]
    {
        "Library/Application Support/minecraft"
    }
    #[cfg(target_os = "linux")]
    {
        ".minecraft"
    }
    #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
    {
        ".minecraft"
    }
}