-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
config.rs
76 lines (68 loc) · 2.39 KB
/
config.rs
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
use log::{error, info};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use crate::package;
#[derive(Deserialize, Serialize, Debug)]
#[serde(default)]
pub struct Config {
pub host_url: String,
pub should_do_update: bool,
pub disable_default_confirm: bool,
pub enable_steam_cloud: bool,
pub hash_check_install: bool,
pub close_client_on_launch: bool,
pub steam_app_id_install_wait_in_seconds: u32,
pub additional_remote_packages: Option<Vec<String>>,
}
impl Default for Config {
fn default() -> Config {
Config {
host_url: "https://luxtorpeda-dev.github.io".to_string(),
should_do_update: true,
disable_default_confirm: false,
enable_steam_cloud: false,
hash_check_install: true,
close_client_on_launch: false,
steam_app_id_install_wait_in_seconds: 600,
additional_remote_packages: None,
}
}
}
impl Config {
pub fn from_config_file() -> Config {
let config_file_path = Config::config_file_path();
if config_file_path.exists() {
info!("config_file_path exists, reading");
match fs::read_to_string(config_file_path) {
Ok(s) => match serde_json::from_str::<Config>(&s) {
Ok(config) => config,
Err(err) => {
error!("error parsing config_file: {:?}", err);
Config::default_config_and_save()
}
},
Err(err) => {
error!("error reading config_file: {:?}", err);
Config::default_config_and_save()
}
}
} else {
info!("config_file_path does not exist, using default");
Config::default_config_and_save()
}
}
fn config_file_path() -> PathBuf {
let config_path = package::path_to_config();
config_path.join("config.json")
}
fn default_config_and_save() -> Config {
let default_config: Config = Default::default();
let config_file_path = Config::config_file_path();
info!("writing config_file to {:?}", config_file_path);
serde_json::to_string_pretty(&default_config)
.ok()
.and_then(|config_json| std::fs::write(config_file_path, config_json).ok());
default_config
}
}