summaryrefslogtreecommitdiffstats
path: root/src/properties.rs
blob: de391cc52c0b7fcd3236dcc37a49b7689ca5ba94 (plain) (blame)
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
use std::collections::HashMap;
use std::path::Path;

/// Parse a Java .properties file into key-value pairs.
pub fn read_properties(path: &Path) -> HashMap<String, String> {
    let mut map = HashMap::new();
    let Ok(contents) = std::fs::read_to_string(path) else {
        return map;
    };
    for line in contents.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') || line.starts_with('!') {
            continue;
        }
        if let Some((key, value)) = line.split_once('=') {
            map.insert(key.trim().to_string(), value.trim().to_string());
        } else if let Some((key, value)) = line.split_once(':') {
            map.insert(key.trim().to_string(), value.trim().to_string());
        }
    }
    map
}

/// Read the level-seed from an instance's server.properties.
/// Returns None if the file doesn't exist or the seed is empty.
pub fn read_instance_seed(server_dir: &Path, instance_name: &str) -> Option<String> {
    let props_path = server_dir
        .join("instances")
        .join(instance_name)
        .join("data")
        .join("server.properties");
    let props = read_properties(&props_path);
    let seed = props.get("level-seed")?;
    if seed.is_empty() {
        None
    } else {
        Some(seed.clone())
    }
}